OSDN Git Service

be809b88f70f5f510dff554c7c4804b5ea9c5c7b
[mulab/sd2nd.git] / trust_path / modules / sd2nd / class / Module.class.php
1 <?php
2 /**
3  * @file
4  * @package sd2nd
5  * @version $Id$
6 **/
7
8 if(!defined('XOOPS_ROOT_PATH'))
9 {
10     exit;
11 }
12
13 require_once SD2ND_TRUST_PATH . '/class/AbstractAction.class.php';
14
15 define('SD2ND_FRAME_PERFORM_SUCCESS', 1);
16 define('SD2ND_FRAME_PERFORM_FAIL', 2);
17 define('SD2ND_FRAME_INIT_SUCCESS', 3);
18
19 define('SD2ND_FRAME_VIEW_NONE','none');
20 define('SD2ND_FRAME_VIEW_SUCCESS','success');
21 define('SD2ND_FRAME_VIEW_ERROR','error');
22 define('SD2ND_FRAME_VIEW_INDEX','index');
23 define('SD2ND_FRAME_VIEW_INPUT','input');
24 define('SD2ND_FRAME_VIEW_PREVIEW','preview');
25 define('SD2ND_FRAME_VIEW_CANCEL','cancel');
26
27 /**
28  * Sd2nd_Module
29 **/
30 class Sd2nd_Module extends Legacy_ModuleAdapter
31 {
32     /**
33      * @brief   string
34     **/
35     public $mActionName = null;
36
37     /**
38      * @brief   Sd2nd_AbstractAction
39     **/
40     public $mAction = null;
41
42     /**
43      * @brief   bool
44     **/
45     public $mAdminFlag = false;
46
47     /**
48      * @brief   Sd2nd_AssetManager
49     **/
50     public $mAssetManager = null;
51
52     /**
53      * @brief   string
54     **/
55     protected $_mPreferenceEditUrl = null;
56
57     /**
58      * @brief   string
59     **/
60     protected $_mHelpViewUrl = null;
61
62     /**
63      * @brief   Enum[]
64     **/
65     protected $_mAllowViewNames = array(
66         SD2ND_FRAME_VIEW_NONE,
67         SD2ND_FRAME_VIEW_SUCCESS,
68         SD2ND_FRAME_VIEW_ERROR,
69         SD2ND_FRAME_VIEW_INDEX,
70         SD2ND_FRAME_VIEW_INPUT,
71         SD2ND_FRAME_VIEW_PREVIEW,
72         SD2ND_FRAME_VIEW_CANCEL
73     );
74
75     /**
76      * startup
77      * 
78      * @param   void
79      * 
80      * @return  void
81     **/
82     public function startup()
83     {
84         parent::startup();
85     
86         XCube_DelegateUtils::call('Module.sd2nd.Global.Event.GetAssetManager',new XCube_Ref($this->mAssetManager),$this->mXoopsModule->get('dirname'));
87     
88         $root =& XCube_Root::getSingleton();
89         $root->mController->mExecute->add(array(&$this, 'execute'));
90     
91         //
92         // TODO/Insert your initialization code.
93         //
94     }
95
96     /**
97      * setAdminMode
98      * 
99      * @param   bool  $flag
100      * 
101      * @return  void
102     **/
103     public function setAdminMode(/*** bool ***/ $flag)
104     {
105         $this->mAdminFlag = $flag;
106     }
107
108     /**
109      * _getDefaultActionName
110      * 
111      * @param   void
112      * 
113      * @return  string
114     **/
115     private function _getDefaultActionName()
116     {
117         // TODO insert your default action name
118         return 'index';
119     }
120
121     /**
122      * setActionName
123      * 
124      * @param   string  $name
125      * 
126      * @return  void
127     **/
128     public function setActionName(/*** string ***/ $name)
129     {
130         $this->mActionName = $name;
131     }
132
133     /**
134      * getRenderSystemName
135      * 
136      * @param   void
137      * 
138      * @return  string
139     **/
140     public function getRenderSystemName()
141     {
142         if(!$this->mAdminFlag)
143         {
144             return parent::getRenderSystemName();
145         }
146     
147         // TODO will be use site config
148         if(!defined('SD2ND_ADMIN_RENDER_REGISTED'))
149         {
150             define('SD2ND_ADMIN_RENDER_REGISTED',true);
151             $root =& XCube_Root::getSingleton();
152             $root->overrideSiteConfig(
153                 array(
154                     'RenderSystems' => array(
155                         'Sd2nd_AdminRenderSystem' => 'Sd2nd_AdminRenderSystem'
156                     ),
157                     'Sd2nd_AdminRenderSystem' => array(
158                         'root' => SD2ND_TRUST_PATH,
159                         'path' => '/admin/class/Sd2ndAdminRenderSystem.class.php',
160                         'class' => 'Sd2nd_AdminRenderSystem'
161                     )
162                 )
163             );
164         }
165     
166         return 'Sd2nd_AdminRenderSystem';
167     }
168
169     /**
170      * getAdminMenu
171      * 
172      * @param   void
173      * 
174      * @return  {string 'title',string 'link',string 'keywords',bool 'show',bool 'absolute'}[]
175     **/
176     public function getAdminMenu()
177     {
178         if(is_array($this->mAdminMenu))
179         {
180             return $this->mAdminMenu;
181         }
182     
183         $root =& XCube_Root::getSingleton();
184     
185         // load admin menu
186         $adminMenu = $this->mXoopsModule->getInfo('adminmenu');
187         if(!is_array($adminMenu))
188         {
189             $adminMenu = array();
190         }
191     
192         // add preference menu
193         if($url = $this->getPreferenceEditUrl())
194         {
195             $adminMenu[] = array(
196                 'title'    => _PREFERENCES,
197                 'link'     => $url,
198                 'absolute' => true
199             );
200         }
201     
202         // add help menu
203         if($url = $this->getHelpViewUrl())
204         {
205             $adminMenu[] = array(
206                 'title'    => _HELP,
207                 'link'     => $url,
208                 'absolute' => true
209             );
210         }
211     
212         $this->mAdminMenu = array();
213         foreach($adminMenu as $menu)
214         {
215             if(!(isset($menu['absolute']) && $menu['absolute']))
216             {
217                 $menu['link'] = XOOPS_MODULE_URL . '/' . $this->mXoopsModule->get('dirname') . '/' . $menu['link'];
218             }
219             $this->mAdminMenu[] = $menu;
220         }
221     
222         return $this->mAdminMenu;
223     }
224
225     /**
226      * getPreferenceEditUrl
227      * 
228      * @param   void
229      * 
230      * @return  string
231     **/
232     public function getPreferenceEditUrl()
233     {
234         if($this->_mPreferenceEditUrl === null)
235         {
236             if(is_array($this->mXoopsModule->getInfo('config')) && count($this->mXoopsModule->getInfo('config')) > 0)
237             {
238                 $root =& XCube_Root::getSingleton();
239                 $this->_mPreferenceEditUrl = $root->mController->getPreferenceEditUrl($this->mXoopsModule);
240             }
241             else
242             {
243                 $this->_mPreferenceEditUrl = false;
244             }
245         }
246     
247         return $this->_mPreferenceEditUrl;
248     }
249
250     /**
251      * getHelpViewUrl
252      * 
253      * @param   void
254      * 
255      * @return  string
256     **/
257     public function getHelpViewUrl()
258     {
259         if($this->_mHelpViewUrl === null)
260         {
261             if($this->mXoopsModule->hasHelp())
262             {
263                 $root =& XCube_Root::getSingleton();
264                 $this->_mHelpViewUrl = $root->mController->getHelpViewUrl($this->mXoopsModule);
265             }
266             else
267             {
268                 $this->_mHelpViewUrl = false;
269             }
270         }
271     
272         return $this->_mHelpViewUrl;
273     }
274
275     /**
276      * execute
277      * 
278      * @param   XCube_Controller  &$controller
279      * 
280      * @return  void
281     **/
282     public function execute(/*** XCube_Controller ***/ &$controller)
283     {
284         if($this->_createAction() === false)
285         {
286             $this->doActionNotFoundError();
287             die();
288         }
289     
290         if($this->mAction->prepare() === false)
291         {
292             $this->doPreparationError();
293             die();
294         }
295     
296         if($this->mAction->hasPermission() === false)
297         {
298             $this->doPermissionError();
299             die();
300         }
301     
302         $viewStatus = (Sd2nd_Utils::getEnv('REQUEST_METHOD') == 'POST') ?
303             $this->mAction->execute() :
304             $this->mAction->getDefaultView();
305     
306         if(in_array($viewStatus,$this->_mAllowViewNames))
307         {
308             $methodName = 'executeView' . ucfirst($viewStatus);
309             if(is_callable(array($this->mAction,$methodName)))
310             {
311                 $this->mAction->$methodName($this->getRenderTarget());
312             }
313         }
314     }
315
316     /**
317      * _createAction
318      * 
319      * @param   void
320      * 
321      * @return  bool
322     **/
323     private function _createAction()
324     {
325         $root =& XCube_Root::getSingleton();
326     
327         if($this->mActionName == null)
328         {
329             $this->mActionName = $root->mContext->mRequest->getRequest('action');
330             if($this->mActionName == null)
331             {
332                 $this->mActionName = $this->_getDefaultActionName();
333             }
334         }
335     
336         if(!ctype_alnum($this->mActionName))
337         {
338             return false;
339         }
340     
341         $fileName = ($this->mAdminFlag ? '/admin' : '')
342             . '/actions/' . ucfirst($this->mActionName) . 'Action.class.php';
343         switch(true)
344         {
345             case file_exists(
346                 $path = XOOPS_MODULE_PATH . '/' . $this->mXoopsModule->get('dirname') . $fileName
347             ):
348                 break;
349             case file_exists(
350                 $path = SD2ND_TRUST_PATH . '/' . $fileName
351             ):
352                 break;
353             default:
354                 return false;
355         }
356     
357         require_once $path;
358     
359         $className = 'Sd2nd_' . ($this->mAdminFlag ? 'Admin_' : '')
360             . ucfirst($this->mActionName) . 'Action';
361         if(class_exists($className))
362         {
363             $this->mAction =& new $className();
364         }
365         if(!$this->mAction instanceof Sd2nd_AbstractAction)
366         {
367             return false;
368         }
369     
370         return true;
371     }
372
373     /**
374      * doActionNotFoundError
375      * 
376      * @param   void
377      * 
378      * @return  void
379     **/
380     private function doActionNotFoundError()
381     {
382         /**
383          * Module.sd2nd.Global.Event.Exception.ActionNotFound
384          * 
385          * @param   string  $dirname
386          * 
387          * @return  void
388         **/
389         XCube_DelegateUtils::call('Module.sd2nd.Global.Event.Exception.ActionNotFound',$this->mAssetManager->mDirname);
390         /**
391          * Module.{dirname}.Event.Exception.ActionNotFound
392          * 
393          * @param   void
394          * 
395          * @return  void
396         **/
397         XCube_DelegateUtils::call('Module.' . $this->mXoopsModule->get('dirname') . '.Event.Exception.ActionNotFound');
398         $root =& XCube_Root::getSingleton();
399         $root->mController->executeForward(XOOPS_URL);
400     }
401
402     /**
403      * doPreparationError
404      * 
405      * @param   void
406      * 
407      * @return  void
408     **/
409     private function doPreparationError()
410     {
411         /**
412          * Module.sd2nd.Global.Event.Exception.Preparation
413          * 
414          * @param   string  $dirname
415          * 
416          * @return  void
417         **/
418         XCube_DelegateUtils::call('Module.sd2nd.Global.Event.Exception.Preparation',$this->mAssetManager->mDirname);
419         /**
420          * Module.{dirname}.Event.Exception.Preparation
421          * 
422          * @param   void
423          * 
424          * @return  void
425         **/
426         XCube_DelegateUtils::call('Module.' . $this->mXoopsModule->get('dirname') . '.Event.Exception.Preparation');
427         $root =& XCube_Root::getSingleton();
428         $root->mController->executeForward(XOOPS_URL);
429     }
430
431     /**
432      * doPermissionError
433      * 
434      * @param   void
435      * 
436      * @return  void
437     **/
438     private function doPermissionError()
439     {
440         /**
441          * Module.sd2nd.Global.Event.Exception.Permission
442          * 
443          * @param   string  $dirname
444          * 
445          * @return  void
446         **/
447         XCube_DelegateUtils::call('Module.sd2nd.Global.Event.Exception.Permission',$this->mAssetManager->mDirname);
448         /**
449          * Module.{dirname}.Event.Exception.Permission
450          * 
451          * @param   void
452          * 
453          * @return  void
454         **/
455         XCube_DelegateUtils::call('Module.' . $this->mXoopsModule->get('dirname') . '.Event.Exception.Permission');
456         $root =& XCube_Root::getSingleton();
457         $root->mController->executeForward(XOOPS_URL);
458     }
459 }
460
461 ?>