PageRenderTime 30ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/aoliz/core/model/system/mdl.trigger.php

http://phpfor.googlecode.com/
PHP | 329 lines | 246 code | 29 blank | 54 comment | 30 complexity | dff6bee58041948e89fb2684d820253f MD5 | raw file
  1. <?php
  2. require_once('shopObject.php');
  3. define('TEST_INCLUDE',1);
  4. define('TEST_EXCLUDE',2);
  5. define('TEST_BEGIN',3);
  6. define('TEST_END',4);
  7. define('TEST_EQUAL',7);
  8. define('TEST_NOT_EQUAL',8);
  9. define('TEST_GREAT_THAN',9);
  10. define('TEST_LESS_THAN',10);
  11. define('TEST_EARLY_THAN',11);
  12. define('TEST_LATE_THAN',12);
  13. define('TEST_IS_WORKDAY',13);
  14. define('TEST_WEEKEND',14);
  15. class mdl_trigger extends shopObject{
  16. var $defaultCols = 'trigger_event,trigger_memo,filter_str,action_str,active';
  17. var $idColumn = 'trigger_id'; //??id??
  18. var $textColumn = 'filter_str';
  19. var $tableName = 'sdb_triggers';
  20. function mdl_trigger(){
  21. parent::shopObject();
  22. $this->trigger_points = array(
  23. // 'goods/products'=>__('??'),
  24. 'trading/order'=>__('??'),
  25. 'member/account'=>__('??'),
  26. // 'system'=>__('??'),
  27. );
  28. $this->listeners = $this->system->getConf('system.event_listener');
  29. }
  30. function getColumns(){
  31. $ret = array('_cmd'=>array('label'=>__('??'),'width'=>75,'html'=>'system/trigger/command.html'));
  32. return array_merge($ret,parent::getColumns());
  33. }
  34. function _filter($filter){
  35. if($filter['target']){
  36. $where = array('trigger_event like "'.$filter['target'].':%"');
  37. unset($filter['target']);
  38. }else{
  39. $where = array();
  40. }
  41. return parent::_filter($filter,$tbase,$where);
  42. }
  43. function modifier_trigger_event($rows){
  44. $event_map = array();
  45. foreach($rows as $k=>$r){
  46. list($target,$event) = explode(':',$r);
  47. if($target=='system'){
  48. $rows[$k] = __('????');
  49. }else{
  50. if(!$event_map[$target]){
  51. $obj = &$this->system->loadModel($target);
  52. $event_map[$target] = $obj->events();
  53. $obj = null;
  54. }
  55. $rows[$k] = $this->trigger_points[$target].$event_map[$target][$event]['label'];
  56. }
  57. }
  58. }
  59. /**
  60. * object_fire_event
  61. * ??????
  62. *
  63. * @param mixed $action
  64. * @param mixed $object
  65. * @param mixed $member_id
  66. * @param mixed $target
  67. * @access public
  68. * @return void
  69. */
  70. function object_fire_event($action , &$object, $member_id,&$target){
  71. //ob_start();'system.event_listener'
  72. if(false===strpos($action,':')){
  73. $trigger_event = $target->modelName.':'.$action;
  74. $modelName = $target->modelName;
  75. }else{
  76. $trigger_event = $action;
  77. list($modelName,$action) = explode(':',$action);
  78. }
  79. $type = $target->typeName;
  80. $this->system->messenger = &$this->system->loadModel('system/messenger');
  81. $this->system->_msgList = $this->system->messenger->actions();
  82. if($this->system->_msgList[$type.'-'.$action]){
  83. $this->system->messenger->actionSend($type.'-'.$action,$object,$member_id);
  84. }
  85. if(defined('DISABLE_TRIGGER') && DISABLE_TRIGGER){
  86. return true;
  87. }else{
  88. $all_triggers = $this->db->select('select trigger_define from sdb_triggers where trigger_event="'.$trigger_event.'" and active="true" and disabled="false" order by trigger_order desc');
  89. if($all_triggers){
  90. $events = $target->events();
  91. if (!$events){
  92. $instance=$this->system->loadModel($modelName);
  93. $events = $instance->events();
  94. }else{
  95. $instance = $target;
  96. }
  97. $object['_event_date_'] = time();
  98. $object['ip'] = remote_addr();
  99. foreach($all_triggers as $trigger){
  100. $trigger = unserialize($trigger['trigger_define']);
  101. if($this->__test_role($trigger['filter_mode'],$trigger['filter'],$object,$events[$action]['params'],$instance)){
  102. $this->__call_actions($trigger['actions'],$object);
  103. }
  104. }
  105. }
  106. $appmgr = &$this->system->loadModel('system/appmgr');
  107. $data=array_merge((array)$this->listeners['*'],
  108. (array)$this->listeners[$target->modelName.':*'],
  109. (array)$this->listeners[$target->modelName.':'.$action]);
  110. foreach($data as $func){
  111. list($mod,$func) = $appmgr->get_func($func);
  112. if($func)$mod->$func($action,$object);
  113. }
  114. return true;
  115. }
  116. //$log = ob_get_contents();
  117. //ob_end_clean();
  118. }
  119. /**
  120. * __test_role
  121. * ????????
  122. *
  123. * @param mixed $mode
  124. * @param mixed $filter
  125. * @param mixed $data TRIGGER_LOG
  126. * @param mixed $params_define
  127. * @access protected
  128. * @return void
  129. */
  130. function __test_role($mode , $filter , &$data , &$params_define,&$target){
  131. if($mode=='every'){
  132. return true;
  133. }else{
  134. if($mode=='any'){
  135. foreach($filter as $test){
  136. if(!$this->__prepare_k_data($val,$data,$test['key'],$target)) continue;
  137. if($this->__test_filter($val,$params_define[$test['key']]['type'],$test['test'],$test['val'])){
  138. return true;
  139. }
  140. }
  141. return false;
  142. }elseif($mode=='all'){
  143. foreach($filter as $test){
  144. if(!$this->__prepare_k_data($val,$data,$test['key'],$target)) return false;
  145. if(!$this->__test_filter($val,$params_define[$test['key']]['type'],$test['test'],$test['val'])){
  146. return false;
  147. }
  148. }
  149. return true;
  150. }
  151. }
  152. }
  153. function __prepare_k_data(&$val,&$data,$key,&$target){
  154. if(isset($data[$key])){
  155. $val = $data[$key];
  156. return true;
  157. }else{
  158. if(method_exists($target,$func = '_get_'.$key)){
  159. $val = $data[$key] = $target->$func($data[$target->idColumn]);
  160. return true;
  161. }else{
  162. return false;
  163. }
  164. }
  165. }
  166. /**
  167. * __test_filter
  168. * ??????????
  169. *
  170. * @param mixed $data
  171. * @param mixed $type
  172. * @param mixed $test
  173. * @param mixed $val
  174. * @access protected
  175. * @return void
  176. */
  177. function __test_filter($data,$type,$test=null,$val=null){
  178. switch($type){
  179. case 'bool':
  180. if(!is_bool($test)) eval( '$test = '.$test.';' ); //????bool???????bool
  181. return ($data == $test);
  182. case 'string':
  183. switch($test){
  184. case TEST_INCLUDE:
  185. return (false === strpos($data,$val));
  186. case TEST_EXCLUDE:
  187. return (false !== strpos($data,$val));
  188. case TEST_BEGIN:
  189. return ($val == substr($data,0,strlen($val)));
  190. case TEST_END:
  191. return ($val == substr($data,0-strlen($val)));
  192. default:
  193. return false;
  194. }
  195. case 'number':
  196. switch($test){
  197. case TEST_GREAT_THAN:
  198. return ($data > $val);
  199. case TEST_LESS_THAN:
  200. return ($data < $val);
  201. case TEST_EQUAL:
  202. return ($data == $val);
  203. case TEST_NOT_EQUAL:
  204. return ($data != $val);
  205. default:
  206. return false;
  207. }
  208. case 'ip':
  209. if(!function_exists('shop_match_network')){
  210. require(CORE_INCLUDE_DIR.'/shop/core.match_network.php');
  211. }
  212. switch($test){
  213. case TEST_INCLUDE:
  214. return shop_match_network($val,$data);
  215. case TEST_EXCLUDE:
  216. return !shop_match_network($val,$data);
  217. default:
  218. return false;
  219. }
  220. case 'date':
  221. switch($test){
  222. case TEST_EQUAL:
  223. return ($data == $val);
  224. case TEST_NOT_EQUAL:
  225. return ($data !== $val);
  226. case TEST_EARLY_THAN:
  227. return ($data < $val);
  228. case TEST_LATE_THAN:
  229. return ($data > $val);
  230. case TEST_WEEKEND:
  231. $d = date('w',$date);
  232. return ($d==5 || $d==6);
  233. case TEST_IS_WORKDAY:
  234. $d = date('w',$date);
  235. return ($d!=5 && $d!=6);
  236. default:
  237. return false;
  238. }
  239. default:
  240. return $data;
  241. }
  242. }
  243. /**
  244. * __call_actions
  245. * ?????????
  246. *
  247. * @param mixed $action
  248. * @param mixed $data
  249. * @access protected
  250. * @return void
  251. */
  252. function __call_actions($action,&$data){
  253. $plugin = &$this->system->loadModel('system/addons');
  254. foreach($action as $act){
  255. list($part,$func) = explode(':',$act['act']);
  256. if(!isset($this->_action_mod[$part])){
  257. $this->_action_mod[$part] = $plugin->load($part,'action');
  258. }
  259. $args = is_array($data)?$data:array(&$data);
  260. $args = is_array($act['args'])?array_merge($args,$act['args']):$args;
  261. call_user_func_array(array($this->_action_mod[$part],$func),array($args,$act['args']));
  262. }
  263. }
  264. /**
  265. * param_types
  266. * ??????????
  267. *
  268. * @access public
  269. * @return void
  270. */
  271. function param_types(){
  272. return array(
  273. 'bool'=>array('true'=>array('label'=>__('?')),'false'=>array('label'=>__('?'))),
  274. 'string'=>array(
  275. TEST_INCLUDE=>array('label'=>__('??'),'input'=>'text'),
  276. TEST_EXCLUDE=>array('label'=>__('???'),'input'=>'text'),
  277. TEST_BEGIN=>array('label'=>__('????'),'input'=>'text'),
  278. TEST_END=>array('label'=>__('????'),'input'=>'text')
  279. ),
  280. 'ip'=>array(
  281. TEST_INCLUDE=>array('label'=>__('????'),'input'=>'text'),
  282. TEST_EXCLUDE=>array('label'=>__('?????'),'input'=>'text')
  283. ),
  284. 'number'=>array(
  285. TEST_GREAT_THAN=>array('label'=>__('??'),'input'=>'number'),
  286. TEST_LESS_THAN=>array('label'=>__('??'),'input'=>'number'),
  287. TEST_EQUAL=>array('label'=>__('??'),'input'=>'number'),
  288. TEST_NOT_EQUAL=>array('label'=>__('???'),'input'=>'number'),
  289. ),
  290. 'date'=>array(
  291. TEST_EQUAL=>array('label'=>__('?'),'input'=>'date'),
  292. TEST_NOT_EQUAL=>array('label'=>__('??'),'input'=>'date'),
  293. TEST_EARLY_THAN=>array('label'=>__('??'),'input'=>'date'),
  294. TEST_LATE_THAN=>array('label'=>__('??'),'input'=>'date'),
  295. TEST_WEEKEND=>array('label'=>__('???')),
  296. TEST_IS_WORKDAY=>array('label'=>__('????'))
  297. ),
  298. );
  299. }
  300. }