1: <?php
2: /**
3: *
4: * @package Legacy
5: * @version $Id: Legacy_Module.class.php,v 1.6 2008/09/25 15:11:59 kilica Exp $
6: * @copyright Copyright 2005-2007 XOOPS Cube Project <https://github.com/xoopscube/legacy>
7: * @license https://github.com/xoopscube/legacy/blob/master/docs/GPL_V2.txt GNU GENERAL PUBLIC LICENSE Version 2
8: *
9: */
10:
11: /**
12: * @public
13: * @brief [Abstract] Represents modules and used for Legacy_Controller
14: *
15: * This is an abstract class which has interfaces to connect with the controller about
16: * the module process. Legacy controller makes an interface of this class and uses its
17: * methods to call module programs.
18: *
19: * So modules may define their sub-classes implementing this interface.
20: * The instance is attached to the Legacy_Context after initializing, so modules can
21: * defines members for module's features and can access them. But, most interfaces
22: * defined by this class should be called by only Legacy_Controller.
23: *
24: * @attention
25: * This interfaces are initialized by only Legacy_Controller.
26: *
27: * @see Legacy_Utils::createModule()
28: * @see XoopsModule
29: */
30: class Legacy_AbstractModule
31: {
32: /**
33: * @public
34: * @brief [READ ONLY] Map Array - std::map<string, mixed> - used freely for this module.
35: * @remarks
36: * If references are must, access directly to this member.
37: */
38: var $mAttributes = array();
39:
40: /**
41: * @public
42: * @brief [READ ONLY] XoopsModule
43: */
44: var $mXoopsModule = null;
45:
46: /**
47: * @public
48: * @brief [READ ONLY] Map Array - std::map<string, string>
49: */
50: var $mModuleConfig = array();
51:
52: /**
53: * @private
54: * @brief Legacy_AbstractCacheInformation - The cached instance.
55: * @see getCacheInfo()
56: */
57: var $mCacheInfo = null;
58:
59: /**
60: * @private
61: * @brief XCube_RenderTarget - The render target instance for this module.
62: * @see getRenderTarget()
63: */
64: var $mRender = null;
65:
66: /**
67: * @public
68: * @brief constructor
69: * @param $module XoopsModule
70: * @attention
71: * Basically, only Legacy_Controller and its utility functions should call the
72: * constructor.
73: */
74: function Legacy_AbstractModule(&$module, $loadConfig=true)
75: {
76: $this->setXoopsModule($module);
77:
78: if ($loadConfig && ($module->get('hasconfig') == 1 || $module->get('hascomments') == 1 || $module->get('hasnotification') == 1)) {
79: $handler =& xoops_gethandler('config');
80: $this->setModuleConfig($handler->getConfigsByCat(0, $module->get('mid')));
81: }
82: }
83:
84: /**
85: * @public
86: * @brief Sets $value with $key to attributes.
87: * @param $key string
88: * @param $value mixed
89: * @return void
90: * @remarks
91: * If references are must, access directly to $mAttributes. Because PHP4 can't
92: * handle reference in the signature of this member function.
93: */
94: function setAttribute($key, $value)
95: {
96: $this->mAttributes[$key] = $value;
97: }
98:
99: /**
100: * @public
101: * @brief Gets a value indicating whether the value specified by $key exists.
102: * @param $key string
103: * @return bool
104: */
105: function hasAttribute($key)
106: {
107: return isset($this->mAttributes[$key]);
108: }
109:
110: /**
111: * @public
112: * @brief Gets a value of attributes with $key.
113: * @param string $key
114: * @return mixed - If the value specified by $key doesn't exist in attributes, returns null.
115: */
116: function getAttribute($key)
117: {
118: return isset($this->mAttributes[$key]) ? $this->mAttributes[$key] : null;
119: }
120:
121: /**
122: * @public
123: * @brief Binds an instance of XoopsModule to the property.
124: * @param $xoopsModule XoopsModule
125: * @return void
126: */
127: function setXoopsModule(&$xoopsModule)
128: {
129: $this->mXoopsModule =& $xoopsModule;
130: }
131:
132: /**
133: * @public
134: * @brief Gets the binded XoopsModule instance.
135: * @return XoopsModule
136: */
137: function &getXoopsModule()
138: {
139: return $this->mXoopsModule;
140: }
141:
142: /**
143: * @public
144: * @brief Binds array of xoops module config to the property.
145: * @param $config Map Array - std::map<string, mixed>
146: * @return void
147: */
148: function setModuleConfig($config)
149: {
150: $this->mModuleConfig = $config;
151: }
152:
153: /**
154: * @public
155: * @brief Gets a value form xoops module config with $key.
156: * @param $key string
157: * @return mixed If $key is specified null, returns map array (std::map<string, mixed>)
158: */
159: function getModuleConfig($key = null)
160: {
161: if ($key == null) {
162: return $this->mModuleConfig;
163: }
164:
165: return isset($this->mModuleConfig[$key]) ? $this->mModuleConfig[$key] : null;
166: }
167:
168: /**
169: * @public
170: * @brief Gets the cache information instance.
171: * @return Legacy_ModuleCaceInformation
172: * @see _createChaceInfo()
173: */
174: function &getCacheInfo()
175: {
176: if (!is_object($this->mCacheInfo)) {
177: $this->_createCacheInfo();
178: }
179:
180: return $this->mCacheInfo;
181: }
182:
183: /**
184: * @protected
185: * @brief Creates a cache information instance and returns it.
186: * @return Legacy_ModuleCacheInformation
187: * @remarks
188: * This member function sets the created instance to mCacheInfo because this
189: * instance has to keep the instance for many callbacks.
190: * @see getCacheInfo()
191: */
192: function _createCacheInfo()
193: {
194: $this->mCacheInfo = new Legacy_ModuleCacheInformation();
195: $this->mCacheInfo->mURL = xoops_getenv('REQUEST_URI');
196: $this->mCacheInfo->setModule($this->mXoopsModule);
197: }
198:
199: /**
200: * @public
201: * @brief Gets the render target instance.
202: * @return XCube_RenderTarget
203: * @see _createRenderTarget()
204: */
205: function &getRenderTarget()
206: {
207: if ($this->mRender == null) {
208: $this->_createRenderTarget();
209: }
210:
211: return $this->mRender;
212: }
213:
214: /**
215: * @protected
216: * @brief Creates a render target instance and returns it.
217: * @return XCube_RenderTarget
218: * @remarks
219: * This member function sets the created instance to mRender because this
220: * instance has to keep the instance for many callbacks.
221: * @see getRenderTarget()
222: */
223: function _createRenderTarget()
224: {
225: $renderSystem =& $this->getRenderSystem();
226:
227: $this->mRender =& $renderSystem->createRenderTarget('main');
228: if ($this->mXoopsModule != null) {
229: $this->mRender->setAttribute('legacy_module', $this->mXoopsModule->get('dirname'));
230: }
231: }
232:
233: /**
234: * @public
235: * @brief Gets a name of the dependency render system.
236: * @return string
237: * @remarks
238: * If this module depends on other systems than the main render system by Legacy,
239: * override this.
240: * @see getRenderSystem()
241: */
242: function getRenderSystemName()
243: {
244: $root =& XCube_Root::getSingleton();
245: return $root->mContext->mBaseRenderSystemName;
246: }
247:
248: /**
249: * @public
250: * @brief Gets the dependency render system.
251: * @return XCube_RenderSystem
252: * @remarks
253: * If this module uses the unregistered render system is used, override this.
254: */
255: function &getRenderSystem()
256: {
257: $root =& XCube_Root::getSingleton();
258: $renderSystem =& $root->getRenderSystem($this->getRenderSystemName());
259:
260: return $renderSystem;
261: }
262:
263: /**
264: * @public
265: * @brief Gets a value indicating whether this modules is an active.
266: * @return bool
267: */
268: function isActive()
269: {
270: if (!is_object($this->mXoopsModule)) { //< FIXME
271: return false;
272: }
273:
274: return $this->mXoopsModule->get('isactive') ? true : false;
275: }
276:
277: /**
278: * @public
279: * @brief Gets a value indicating whether the current module has a option of
280: * configurations to use the cache system.
281: * @return bool
282: */
283: function isEnableCache()
284: {
285: if (xoops_getenv('REQUEST_METHOD') == 'POST') {
286: return false;
287: }
288:
289: $root =& XCube_Root::getSingleton();
290:
291: return is_object($this->mXoopsModule) && !empty($root->mContext->mXoopsConfig['module_cache'][$this->mXoopsModule->get('mid')]);
292: }
293:
294: /**
295: * @public
296: * @brief Initializes a cache information object, and returns it.
297: * @return Legacy_ModuleCacheInformation
298: */
299: function &createCacheInfo()
300: {
301: $this->mCacheInfo = new Legacy_ModuleCacheInformation();
302: $this->mCacheInfo->mURL = xoops_getenv('REQUEST_URI');
303: $this->mCacheInfo->setModule($this->mXoopsModule);
304:
305: return $this->mCacheInfo;
306: }
307:
308: /**
309: * @public
310: * @brief [Abstract] This method is called by the controller strategy, if this module
311: * is the current module.
312: * @return void
313: */
314: function startup()
315: {
316: }
317:
318: /**
319: * @public
320: * @brief [Abstract] This method is called back by the action search feature in the
321: * control panel.
322: * @param Legacy_ActionSearchArgs $searchArgs
323: * @return void
324: * @see Legacy_ActionSearchArgs
325: */
326: function doActionSearch(&$searchArgs)
327: {
328: }
329:
330: /**
331: * @public
332: * @brief This method is called back by the xoops global search feature.
333: */
334: function doLegacyGlobalSearch($queries, $andor, $max_hit, $start, $uid)
335: {
336: }
337:
338: /**
339: * @public
340: * @brief Gets a value indicating whether this module has the page controller in
341: * the control panel side.
342: * @return bool
343: * @note
344: * Side menu blocks may not display the admin menu if this member function returns
345: * false.
346: * @attention
347: * Controller fetches the list of modules from DB before. So, 'override' may not be
348: * able to change the process.
349: */
350: function hasAdminIndex()
351: {
352: return false;
353: }
354:
355: /**
356: * @public
357: * @brief [Abstract] Gets an absolute URL indicating the top page of this module for
358: * the control panel side.
359: * @return string
360: * @attention
361: * Controller fetches the list of modules from DB before. So, 'override' may not
362: * be able to change the process.
363: */
364: function getAdminIndex()
365: {
366: return null;
367: }
368:
369: /**
370: * @public
371: * @brief Gets an array having menus for the side menu of the control panel.
372: * @return Complex Array
373: * @see /modules/legacy/admin/templates/blocks/legacy_admin_block_sidemenu.html
374: */
375: function getAdminMenu()
376: {
377: }
378: }
379:
380: /**
381: * @public
382: * @brief Used for adapting $xoopsModule to imitate XOOPS2 responses.
383: * @remarks
384: * This class is the standard class implementing Legacy_AbstractModule, and is helpful
385: * to be used by Legacy_Controller. If a module doesn't define its sub-class of
386: * Legacy_AbstractModule, this class is used as generic Legacy_AbstractModule.
387: */
388: class Legacy_ModuleAdapter extends Legacy_AbstractModule
389: {
390: /**
391: * @private
392: * @brief bool
393: */
394: var $_mAdminMenuLoadedFlag = false;
395:
396: /**
397: * @protected
398: * @brief Complex Array - cached
399: */
400: var $mAdminMenu = null;
401:
402: function Legacy_ModuleAdapter($module, $loadConfig=true)
403: {
404: parent::Legacy_AbstractModule($module, $loadConfig);
405: }
406:
407: /**
408: * @public
409: * @brief This method is called back by the action search feature in the control
410: * panel.
411: * @param Legacy_ActionSearchArgs $searchArgs
412: * @return void
413: * @see Legacy_ActionSearchArgs
414: */
415: function doActionSearch(&$searchArgs)
416: {
417: if(!is_object($searchArgs)) {
418: return;
419: }
420:
421: $this->mXoopsModule->loadAdminMenu();
422: if(count($this->mXoopsModule->adminmenu) == 0 && !isset($this->mXoopsModule->modinfo['config']) ) {
423: return;
424: }
425:
426: //
427: // Search preference
428: //
429: if(isset($this->mXoopsModule->modinfo['config'])&&count($this->mXoopsModule->modinfo['config'])>0) {
430: $findFlag = false;
431: foreach($searchArgs->getKeywords() as $word) {
432: if (stristr(_PREFERENCES, $word) !== false) {
433: $root =& XCube_Root::getSingleton();
434: $searchArgs->addRecord($this->mXoopsModule->getVar('name'), $root->mController->getPreferenceEditUrl($this->mXoopsModule), _PREFERENCES);
435: $findFlag = true;
436: break;
437: }
438: }
439:
440: if (!$findFlag) {
441: $configInfos=array();
442: foreach($this->mXoopsModule->modinfo['config'] as $config) {
443: if(isset($config['title']))
444: $configInfos[]=@constant($config['title']);
445: if(isset($config['description']))
446: $configInfos[]=@constant($config['description']);
447: if(isset($config['options'])&&count($config['options'])>0) {
448: foreach($config['options'] as $key=>$val) {
449: $configInfos[]=(@constant($key) ? @constant($key) : $key);
450: }
451: }
452: }
453:
454: $findFlag=true;
455: foreach($searchArgs->getKeywords() as $word) {
456: $findFlag&=(stristr(implode(" ",$configInfos),$word)!==false);
457: }
458:
459: if($findFlag) {
460: $searchArgs->addRecord($this->mXoopsModule->getVar('name'),
461: XOOPS_URL.'/modules/legacy/admin/index.php?action=PreferenceEdit&confmod_id='.$this->mXoopsModule->getVar('mid'),
462: _PREFERENCES );
463: }
464: }
465: }
466:
467: //
468: // Search AdminMenu
469: //
470: if(count($this->mXoopsModule->adminmenu)>0) {
471: foreach($this->mXoopsModule->adminmenu as $menu) {
472: $findFlag=true;
473: foreach($searchArgs->getKeywords() as $word) {
474: $tmpFlag=false;
475: $tmpFlag|=(stristr($menu['title'],$word)!==false);
476:
477: // Search keyword
478: if(isset($menu['keywords'])) {
479: $keyword=is_array($menu['keywords']) ? implode(" ",$menu['keywords']) : $menu['keywords'];
480: $tmpFlag|=(stristr($keyword,$word)!==false);
481: }
482:
483: $findFlag&=$tmpFlag;
484: }
485:
486: if($findFlag) {
487: //
488: // Create url string with absolute information.
489: //
490: $url="";
491: if(isset($menu['absolute'])&&$menu['absolute']) {
492: $url=$menu['link'];
493: }
494: else {
495: $url=XOOPS_URL."/modules/".$this->mXoopsModule->getVar('dirname')."/".$menu['link'];
496: }
497:
498: //
499: // Add record
500: //
501: $searchArgs->addRecord($this->mXoopsModule->getVar('name'),$url,$menu['title']);
502: }
503: }
504: }
505:
506: //
507: // Search help
508: //
509: if ($this->mXoopsModule->hasHelp()) {
510: $findFlag = false;
511:
512: foreach($searchArgs->getKeywords() as $word) {
513: if (stristr(_HELP, $word) !== false) {
514: $root =& XCube_Root::getSingleton();
515: $searchArgs->addRecord($this->mXoopsModule->getVar('name'), $root->mController->getHelpViewUrl($this->mXoopsModule), _HELP);
516: $findFlag = true;
517: break;
518: }
519: }
520:
521: if (!$findFlag) {
522: $root =& XCube_Root::getSingleton();
523: $language = $root->mContext->getXoopsConfig('language');
524: $helpfile = $this->mXoopsModule->getHelp();
525: $dir = XOOPS_MODULE_PATH . "/" . $this->mXoopsModule->getVar('dirname') . "/language/" . $language. "/help";
526:
527: if (!file_exists($dir . "/" . $helpfile)) {
528: $dir = XOOPS_MODULE_PATH . "/" . $this->mXoopsModule->getVar('dirname') . "/language/english/help";
529: if (!file_exists($dir . "/" . $helpfile)) {
530: return;
531: }
532: }
533: $lines = file($dir . "/" . $helpfile);
534: foreach ($lines as $line) {
535: foreach($searchArgs->getKeywords() as $word) {
536: if (stristr($line, $word) !== false) {
537: $url = XOOPS_MODULE_URL . "/legacy/admin/index.php?action=Help&dirname=" . $this->mXoopsModule->getVar('dirname');
538: $searchArgs->addRecord($this->mXoopsModule->getVar('name'), $url, _HELP);
539: return;
540: }
541: }
542: }
543: }
544: }
545: }
546:
547: function doLegacyGlobalSearch($queries, $andor, $max_hit, $start, $uid)
548: {
549: $ret = array();
550: $results = $this->mXoopsModule->search($queries, $andor, $max_hit, $start, $uid);
551:
552: if (is_array($results) && count($results) > 0) {
553: foreach ($results as $result) {
554: $item = array();
555: if (isset($result['image']) && strlen($result['image']) > 0) {
556: $item['image'] = XOOPS_URL . '/modules/' . $this->mXoopsModule->get('dirname') . '/' . $result['image'];
557: }
558: else {
559: $item['image'] = XOOPS_URL . '/images/icons/posticon2.gif';
560: }
561:
562: $item['link'] = XOOPS_URL . '/modules/' . $this->mXoopsModule->get('dirname') . '/' . $result['link'];
563: $item['title'] = $result['title'];
564: $item['uid'] = $result['uid'];
565:
566: //
567: // TODO If this service will come to web service, we should
568: // change format from unixtime to string by timeoffset.
569: //
570: $item['time'] = isset($result['time']) ? $result['time'] : 0;
571:
572: $ret[] = $item;
573: }
574: }
575:
576: return $ret;
577: }
578:
579: /**
580: * @public
581: * @brief [Final] Gets a value indicating whether this module has the page controller in
582: * the control panel side.
583: * @return bool
584: */
585: function hasAdminIndex()
586: {
587: $dmy =& $this->mXoopsModule->getInfo();
588: return isset($this->mXoopsModule->modinfo['adminindex']) && $this->mXoopsModule->modinfo['adminindex'] != null;
589: }
590:
591: /**
592: * @public
593: * @brief Gets an absolute URL indicating the top page of this module for the control
594: * panel side.
595: * @return string
596: */
597: function getAdminIndex()
598: {
599: $dmy =& $this->mXoopsModule->getInfo();
600: return XOOPS_MODULE_URL . '/' . $this->mXoopsModule->get('dirname') . '/' . $this->mXoopsModule->modinfo['adminindex'];
601: }
602:
603: function getAdminMenu()
604: {
605: if ($this->_mAdminMenuLoadedFlag) {
606: return $this->mAdminMenu;
607: }
608:
609: $info =& $this->mXoopsModule->getInfo();
610: $root =& XCube_Root::getSingleton();
611:
612: //
613: // Load admin menu, and add preference menu by own judge.
614: //
615: $this->mXoopsModule->loadAdminMenu();
616: if ($this->mXoopsModule->get('hasnotification')
617: || (isset($info['config']) && is_array($info['config']))
618: || (isset($info['comments']) && is_array($info['comments']))) {
619: $this->mXoopsModule->adminmenu[] = array(
620: 'link' => $root->mController->getPreferenceEditUrl($this->mXoopsModule),
621: 'title' => _PREFERENCES,
622: 'absolute' => true);
623: }
624:
625: if ($this->mXoopsModule->hasHelp()) {
626: $this->mXoopsModule->adminmenu[] = array('link' => $root->mController->getHelpViewUrl($this->mXoopsModule),
627: 'title' => _HELP,
628: 'absolute' => true);
629: }
630:
631: $this->_mAdminMenuLoadedFlag = true;
632:
633: if ($this->mXoopsModule->adminmenu) {
634: $dirname = $this->mXoopsModule->get('dirname');
635: foreach ($this->mXoopsModule->adminmenu as $menu) {
636: if (!isset($menu['absolute']) || (isset($menu['absolute']) && $menu['absolute'] != true)) {
637: $menu['link'] = XOOPS_MODULE_URL . '/' . $dirname . '/' . $menu['link'];
638: }
639: $this->mAdminMenu[] = $menu;
640: }
641: }
642:
643: return $this->mAdminMenu;
644: }
645: }
646: ?>
647: