Skip to content
This repository was archived by the owner on Sep 10, 2021. It is now read-only.

Commit 1cfcd98

Browse files
author
Charles Ma
committed
ENH: added scheduler
removed task module finished notification system fixed bug #158
1 parent 1c654e2 commit 1cfcd98

File tree

34 files changed

+567
-301
lines changed

34 files changed

+567
-301
lines changed

core/Bootstrap.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,8 @@ protected function _initRouter()
197197
$listeModule[] = $key;
198198
}
199199
}
200+
201+
// loading modules elements
200202
foreach($listeModule as $m)
201203
{
202204
$route = $m;
@@ -229,6 +231,26 @@ protected function _initRouter()
229231
{
230232
require_once BASE_PATH . "/modules/".$route."/models/AppModel.php";
231233
}
234+
if(file_exists(BASE_PATH . "/modules/".$route."/constant/module.php"))
235+
{
236+
require_once BASE_PATH . "/modules/".$route."/constant/module.php";
237+
}
238+
239+
$dir = BASE_PATH . "/modules/".$route."/models/base";
240+
if(is_dir($dir))
241+
{
242+
$objects = scandir($dir);
243+
foreach($objects as $object)
244+
{
245+
if($object != "." && $object != "..")
246+
{
247+
if(filetype($dir."/".$object) != "dir")
248+
{
249+
require_once $dir."/".$object;
250+
}
251+
}
252+
}
253+
}
232254
}
233255
Zend_Registry::set('modulesEnable', $listeModule);
234256
return $router;

core/Notification.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,15 @@
1414
class Notification extends MIDAS_Notification
1515
{
1616
public $_components = array('Utility');
17+
1718
/** init notification process*/
18-
public function init($type, $params)
19+
public function init()
1920
{
20-
switch ($type)
21-
{
22-
case MIDAS_NOTIFY_GET_DASBOARD:
23-
return $this->_getDasboard();
24-
break;
25-
26-
default:
27-
break;
28-
}
21+
$this->addCallBack('CALLBACK_CORE_GET_DASHBOARD', 'getDasboard');
2922
}//end init
3023

3124
/** generate Dasboard information */
32-
private function _getDasboard()
25+
public function getDasboard()
3326
{
3427
$return = array();
3528
$return['Database'] = array(true); //If you are here it works...

core/constant/notification.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,8 @@
1010
PURPOSE. See the above copyright notices for more information.
1111
=========================================================================*/
1212

13-
define("MIDAS_NOTIFY_LOGIN", 0);
14-
define("MIDAS_NOTIFY_GET_CONFIG_TABS", 1);
15-
define("MIDAS_NOTIFY_GET_DASBOARD", 2);
16-
17-
18-
/** visualization*/
19-
define("MIDAS_NOTIFY_CAN_VISUALIZE", 100);
20-
define("MIDAS_NOTIFY_CREATE_THUMBNAIL", 101);
13+
define("MIDAS_EVENT_PRIORITY_NORMAL",1);
14+
define("MIDAS_EVENT_PRIORITY_LOW",0);
15+
define("MIDAS_EVENT_PRIORITY_HIGH",2);
2116

2217
?>

core/controllers/AdminController.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,9 @@ function dashboardAction()
303303

304304
$this->_helper->layout->disableLayout();
305305

306-
$this->view->dashboard = Zend_Registry::get('notifier')->notify(MIDAS_NOTIFY_GET_DASBOARD);
306+
$this->view->dashboard = Zend_Registry::get('notifier')->callback("CALLBACK_CORE_GET_DASHBOARD");
307+
308+
ksort($this->view->dashboard);
307309

308310
}//end dashboardAction
309311

core/controllers/ItemController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ function viewAction()
119119
$this->view->metadatavalues = $this->ItemRevision->getMetadata($itemRevision);
120120

121121

122-
$tmp = Zend_Registry::get('notifier')->notify(MIDAS_NOTIFY_CAN_VISUALIZE, array('item' => $itemDao));
122+
$tmp = Zend_Registry::get('notifier')->callback("CALLBACK_VISUALIZE_CAN_VISUALIZE", array('item' => $itemDao));
123123
if(isset($tmp['visualize']) && $tmp['visualize'] == true)
124124
{
125125
$this->view->preview = true;
@@ -160,7 +160,7 @@ function viewAction()
160160

161161
foreach($items as $key => $item)
162162
{
163-
$tmp = Zend_Registry::get('notifier')->notify(MIDAS_NOTIFY_CAN_VISUALIZE, array('item' => $item));
163+
$tmp = Zend_Registry::get('notifier')->callback("CALLBACK_VISUALIZE_CAN_VISUALIZE", array('item' => $item));
164164
if(isset($tmp['visualize']) && $tmp['visualize'] == true)
165165
{
166166
$items[$key]->preview = 'true';

core/controllers/UserController.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ function loginAction()
174174
$previousUri = $this->_getParam('previousuri');
175175
if($form->isValid($this->getRequest()->getPost()))
176176
{
177-
$notifications = Zend_Registry::get('notifier')->notify(MIDAS_NOTIFY_LOGIN, array('email' => $form->getValue('email'), 'password' => $form->getValue('password')));
177+
$notifications = Zend_Registry::get('notifier')->callback("CALLBACK_CORE_AUTHENTIFICATION", array('email' => $form->getValue('email'), 'password' => $form->getValue('password')));
178178

179179
if(!empty($notifications['ldap']) && $notifications['ldap'] != false)
180180
{
@@ -282,7 +282,7 @@ public function validentryAction()
282282
return;
283283
}
284284

285-
$notifications = Zend_Registry::get('notifier')->notify(MIDAS_NOTIFY_LOGIN, array('email' => $entry, 'password' => $password));
285+
$notifications = Zend_Registry::get('notifier')->callback("CALLBACK_CORE_AUTHENTIFICATION", array('email' => $entry, 'password' => $password));
286286
if(!empty($notifications['ldap']) && $notifications['ldap'] != false)
287287
{
288288
echo "true";
@@ -601,7 +601,7 @@ public function settingsAction()
601601
$this->view->jsonSettings['passwordErrorMatch'] = $this->t('The passwords are not the same');
602602
$this->view->jsonSettings = JsonComponent::encode($this->view->jsonSettings);
603603

604-
$this->view->customTabs = Zend_Registry::get('notifier')->notify(MIDAS_NOTIFY_GET_CONFIG_TABS, array());
604+
$this->view->customTabs = Zend_Registry::get('notifier')->callback('CALLBACK_CORE_GET_CONFI_TABS', array());
605605
}
606606

607607
/** User page action*/

core/models/base/ItemRevisionModelBase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ function addBitstream($itemRevisionDao, $bitstreamDao)
6060
$item->setSizebytes($this->getSize($itemRevisionDao));
6161
$item->setDateCreation(date('c'));
6262

63-
$modulesThumbnail = Zend_Registry::get('notifier')->notify(MIDAS_NOTIFY_CREATE_THUMBNAIL);
63+
$modulesThumbnail = Zend_Registry::get('notifier')->notifyEvent("EVENT_CORE_CREATE_THUMBNAIL", array($item));
6464
if(empty($modulesThumbnail))
6565
{
6666
$mime = $bitstreamDao->getMimetype();

library/MIDAS/MIDASNotifier.php

Lines changed: 129 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212

1313
/** Notify modules using this class*/
1414
class MIDAS_Notifier
15-
{
16-
17-
private $modules = array();
15+
{
16+
public $modules = array();
17+
public $tasks = array();
18+
private $tasksByModule = array();
19+
public $notifications = array();
1820

1921
/** init the notifier*/
2022
public function __construct()
@@ -44,18 +46,137 @@ public function __construct()
4446
}
4547
$this->modules['core'] = new $name();
4648
}
49+
50+
foreach($this->modules as $module => $notificationClass)
51+
{
52+
$tasks = $notificationClass->getTasks();
53+
foreach($tasks as $name => $task)
54+
{
55+
if(isset($this->tasks[$name]))
56+
{
57+
throw new Zend_Exception('Task already exits');
58+
}
59+
if(strpos($name, "TASK_") === false || !is_string($name))
60+
{
61+
throw new Zend_Exception('Task name should be a string: TASK_MODULE_NAME');
62+
}
63+
$this->tasks[$name] = $task;
64+
$this->tasks[$name]['module'] = $module;
65+
66+
$this->tasksByModule[$module] = $task;
67+
$this->tasksByModule[$module]['name'] = $name;
68+
}
69+
}
70+
71+
foreach($this->modules as $module => $notificationClass)
72+
{
73+
$notifications = $notificationClass->getNotifications();
74+
foreach($notifications as $name => $notificationArray)
75+
{
76+
if(!isset($this->notifications[$name]))
77+
{
78+
$this->notifications[$name] = array();
79+
}
80+
81+
foreach($notificationArray as $notification)
82+
{
83+
if($notification['type'] == 'callback')
84+
{
85+
if(strpos($name, "CALLBACK_") === false || !is_string($name))
86+
{
87+
throw new Zend_Exception('Callback name should be a string: CALLBACK_MODULE_NAME. Was '.$name);
88+
}
89+
}
90+
else
91+
{
92+
if(strpos($name, "EVENT_") === false || !is_string($name))
93+
{
94+
throw new Zend_Exception('Event name should be a string: EVENT_MODULE_NAME. Was '.$name);
95+
}
96+
if(!isset($this->tasks[$notification['call']]))
97+
{
98+
throw new Zend_Exception('The task doesn\'t exit');
99+
}
100+
}
101+
$notification['module'] = $module;
102+
$this->notifications[$name][] = $notification;
103+
}
104+
}
105+
}
47106
}//end contruct()
48107

49108
/** notify enabled modules*/
50-
public function notify($type, $params = null)
109+
public function notifyEvent($name, $params = null, $moduleFilter = array())
110+
{
111+
if(strpos($name, "EVENT_") === false || !is_string($name))
112+
{
113+
throw new Zend_Exception('Event name should be a string: EVENT_MODULE_NAME. Was '.$name);
114+
}
115+
if(!isset($this->notifications[$name]))
116+
{
117+
return;
118+
}
119+
if(is_string($moduleFilter))
120+
{
121+
$moduleFilter = array($moduleFilter);
122+
}
123+
$return = array();
124+
foreach($this->notifications[$name] as $key => $notification)
125+
{
126+
$module = $this->modules[$notification['module']];
127+
if(empty($moduleFilter) || in_array($module, $moduleFilter))
128+
{
129+
$this->_setTask($notification['call'], $params, $notification['priority']);
130+
}
131+
}
132+
return $return;
133+
}//end notify
134+
135+
/** schedule or execute a task*/
136+
private function _setTask($name, $params, $priority)
137+
{
138+
$modules = Zend_Registry::get('modulesEnable');
139+
if(!isset($this->tasks[$name]))
140+
{
141+
return;
142+
}
143+
if(isset($this->modules['scheduler']))
144+
{
145+
$params = array('task' => $name, 'priority' => $priority, 'params' => $params, 'run_only_once' => true, 'fire_time' => date('c'));
146+
call_user_func(array($this->modules['scheduler'], $this->tasks['TASK_SCHEDULER_SCHEDULE_TASK']['method']), $params);
147+
}
148+
else
149+
{
150+
call_user_func(array($this->modules[$this->tasks[$name]['module']], $this->tasks[$name]['method']), $params);
151+
}
152+
}
153+
154+
/** notify enabled modules*/
155+
public function callback($name, $params = null, $moduleFilter = array())
51156
{
157+
if(strpos($name, "CALLBACK_") === false || !is_string($name))
158+
{
159+
throw new Zend_Exception('Callback name should be a string: CALLBACK_MODULE_NAME. Was '.$name);
160+
}
161+
if(!isset($this->notifications[$name]))
162+
{
163+
return array();
164+
}
165+
if(is_string($moduleFilter))
166+
{
167+
$moduleFilter = array($moduleFilter);
168+
}
52169
$return = array();
53-
foreach($this->modules as $key => $module)
170+
foreach($this->notifications[$name] as $key => $notification)
54171
{
55-
$tmp = call_user_func(array($module, 'init'), $type, $params);
56-
if($tmp != null)
172+
$module = $this->modules[$notification['module']];
173+
if(empty($moduleFilter) || in_array($module, $moduleFilter))
57174
{
58-
$return[$key] = $tmp;
175+
$tmp = call_user_func(array($module, $notification['call']), $params);
176+
if($tmp != null)
177+
{
178+
$return[$notification['module']] = $tmp;
179+
}
59180
}
60181
}
61182
return $return;

library/MIDAS/notification/GlobalNotification.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,56 @@
1515
*/
1616
class MIDAS_Notification
1717
{
18+
19+
private $_task = array();
20+
private $_notification = array();
21+
1822
/** contructor*/
1923
public function __construct()
2024
{
2125
$this->loadElements();
2226
$this->loadModuleElements();
27+
$this->init();
2328
}
29+
30+
/** register task*/
31+
public function addTask($name, $method, $comment)
32+
{
33+
$this->_task[$name] = array('method' => $method, 'comment' => $comment);
34+
}// end assTask
35+
36+
/** register callback*/
37+
public function addCallBack($name, $method)
38+
{
39+
if(isset($this->_notification[$name]))
40+
{
41+
$this->_notification[$name] = array();
42+
}
43+
$this->_notification[$name][] = array('type' => 'callback', 'call' => $method);
44+
}// end addCallBack
45+
46+
/** register callback*/
47+
public function addEvent($name, $task, $priority = MIDAS_EVENT_PRIORITY_NORMAL)
48+
{
49+
if(isset($this->_notification[$name]))
50+
{
51+
$this->_notification[$name] = array();
52+
}
53+
$this->_notification[$name][] = array('type' => 'task', 'call' => $task, 'priority' => $priority);
54+
}// end addCallBack
2455

56+
/** get Tasks */
57+
public function getTasks()
58+
{
59+
return $this->_task;
60+
}
61+
62+
/** get Tasks */
63+
public function getNotifications()
64+
{
65+
return $this->_notification;
66+
}
67+
2568
/**
2669
* Get Logger
2770
* @return Zend_Log

modules/api/Notification.php

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,17 @@ class Api_Notification extends MIDAS_Notification
55
public $_models=array('User');
66

77
/** init notification process*/
8-
public function init($type, $params)
8+
public function init()
99
{
10-
switch ($type)
11-
{
12-
case MIDAS_NOTIFY_GET_CONFIG_TABS:
13-
$fc = Zend_Controller_Front::getInstance();
14-
$moduleWebroot = $fc->getBaseUrl().'/api';
15-
return array('Api' => $moduleWebroot.'/config/usertab');
16-
break;
17-
18-
default:
19-
break;
20-
}
21-
}//end init
10+
$this->addCallBack('CALLBACK_CORE_GET_CONFI_TABS', 'getConfigTabs');
11+
}//end init
12+
13+
/** get Config Tabs */
14+
public function getConfigTabs()
15+
{
16+
$fc = Zend_Controller_Front::getInstance();
17+
$moduleWebroot = $fc->getBaseUrl().'/api';
18+
return array('Api' => $moduleWebroot.'/config/usertab');
19+
}
2220
} //end class
2321
?>

0 commit comments

Comments
 (0)