PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://phpfor.googlecode.com/
PHP | 333 lines | 248 code | 30 blank | 55 comment | 30 complexity | c103a797acba529b737fb0e5631f29da 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. //error_log($type,3,HOME_DIR.'/type.txt');
  81. $this->system->messenger = &$this->system->loadModel('system/messenger');
  82. $this->system->_msgList = $this->system->messenger->actions();
  83. error_log($type.'-'.$action."\n",3,HOME_DIR.'/action.txt');
  84. if($this->system->_msgList[$type.'-'.$action]){
  85. error_log($type.'-'.$action."\n",3,HOME_DIR.'/action.txt');
  86. $this->system->messenger->actionSend($type.'-'.$action,$object,$member_id);
  87. }
  88. if(defined('DISABLE_TRIGGER') && DISABLE_TRIGGER){
  89. return true;
  90. }else{
  91. $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');
  92. if($all_triggers){
  93. $events = $target->events();
  94. if (!$events){
  95. $instance=$this->system->loadModel($modelName);
  96. $events = $instance->events();
  97. }else{
  98. $instance = $target;
  99. }
  100. $object['_event_date_'] = time();
  101. $object['ip'] = remote_addr();
  102. foreach($all_triggers as $trigger){
  103. $trigger = unserialize($trigger['trigger_define']);
  104. if($this->__test_role($trigger['filter_mode'],$trigger['filter'],$object,$events[$action]['params'],$instance)){
  105. $this->__call_actions($trigger['actions'],$object);
  106. }
  107. }
  108. }
  109. $appmgr = &$this->system->loadModel('system/appmgr');
  110. $data=array_merge((array)$this->listeners['*'],
  111. (array)$this->listeners[$target->modelName.':*'],
  112. (array)$this->listeners[$target->modelName.':'.$action]);
  113. foreach($data as $func){
  114. list($mod,$func) = $appmgr->get_func($func);
  115. if($func)$mod->$func($action,$object);
  116. }
  117. return true;
  118. }
  119. //$log = ob_get_contents();
  120. //ob_end_clean();
  121. }
  122. /**
  123. * __test_role
  124. * ????????
  125. *
  126. * @param mixed $mode
  127. * @param mixed $filter
  128. * @param mixed $data TRIGGER_LOG
  129. * @param mixed $params_define
  130. * @access protected
  131. * @return void
  132. */
  133. function __test_role($mode , $filter , &$data , &$params_define,&$target){
  134. if($mode=='every'){
  135. return true;
  136. }else{
  137. if($mode=='any'){
  138. foreach($filter as $test){
  139. if(!$this->__prepare_k_data($val,$data,$test['key'],$target)) continue;
  140. if($this->__test_filter($val,$params_define[$test['key']]['type'],$test['test'],$test['val'])){
  141. return true;
  142. }
  143. }
  144. return false;
  145. }elseif($mode=='all'){
  146. foreach($filter as $test){
  147. if(!$this->__prepare_k_data($val,$data,$test['key'],$target)) return false;
  148. if(!$this->__test_filter($val,$params_define[$test['key']]['type'],$test['test'],$test['val'])){
  149. return false;
  150. }
  151. }
  152. return true;
  153. }
  154. }
  155. }
  156. function __prepare_k_data(&$val,&$data,$key,&$target){
  157. if(isset($data[$key])){
  158. $val = $data[$key];
  159. return true;
  160. }else{
  161. if(method_exists($target,$func = '_get_'.$key)){
  162. $val = $data[$key] = $target->$func($data[$target->idColumn]);
  163. return true;
  164. }else{
  165. return false;
  166. }
  167. }
  168. }
  169. /**
  170. * __test_filter
  171. * ??????????
  172. *
  173. * @param mixed $data
  174. * @param mixed $type
  175. * @param mixed $test
  176. * @param mixed $val
  177. * @access protected
  178. * @return void
  179. */
  180. function __test_filter($data,$type,$test=null,$val=null){
  181. switch($type){
  182. case 'bool':
  183. if(!is_bool($test)) eval( '$test = '.$test.';' ); //????bool???????bool
  184. return ($data == $test);
  185. case 'string':
  186. switch($test){
  187. case TEST_INCLUDE:
  188. return (false === strpos($data,$val));
  189. case TEST_EXCLUDE:
  190. return (false !== strpos($data,$val));
  191. case TEST_BEGIN:
  192. return ($val == substr($data,0,strlen($val)));
  193. case TEST_END:
  194. return ($val == substr($data,0-strlen($val)));
  195. default:
  196. return false;
  197. }
  198. case 'number':
  199. switch($test){
  200. case TEST_GREAT_THAN:
  201. return ($data > $val);
  202. case TEST_LESS_THAN:
  203. return ($data < $val);
  204. case TEST_EQUAL:
  205. return ($data == $val);
  206. case TEST_NOT_EQUAL:
  207. return ($data != $val);
  208. default:
  209. return false;
  210. }
  211. case 'ip':
  212. if(!function_exists('shop_match_network')){
  213. require(CORE_INCLUDE_DIR.'/shop/core.match_network.php');
  214. }
  215. switch($test){
  216. case TEST_INCLUDE:
  217. return shop_match_network($val,$data);
  218. case TEST_EXCLUDE:
  219. return !shop_match_network($val,$data);
  220. default:
  221. return false;
  222. }
  223. case 'date':
  224. switch($test){
  225. case TEST_EQUAL:
  226. return ($data == $val);
  227. case TEST_NOT_EQUAL:
  228. return ($data !== $val);
  229. case TEST_EARLY_THAN:
  230. return ($data < $val);
  231. case TEST_LATE_THAN:
  232. return ($data > $val);
  233. case TEST_WEEKEND:
  234. $d = date('w',$date);
  235. return ($d==5 || $d==6);
  236. case TEST_IS_WORKDAY:
  237. $d = date('w',$date);
  238. return ($d!=5 && $d!=6);
  239. default:
  240. return false;
  241. }
  242. default:
  243. return $data;
  244. }
  245. }
  246. /**
  247. * __call_actions
  248. * ?????????
  249. *
  250. * @param mixed $action
  251. * @param mixed $data
  252. * @access protected
  253. * @return void
  254. */
  255. function __call_actions($action,&$data){
  256. $plugin = &$this->system->loadModel('system/addons');
  257. foreach($action as $act){
  258. list($part,$func) = explode(':',$act['act']);
  259. if(!isset($this->_action_mod[$part])){
  260. $this->_action_mod[$part] = $plugin->load($part,'action');
  261. }
  262. $args = is_array($data)?$data:array(&$data);
  263. $args = is_array($act['args'])?array_merge($args,$act['args']):$args;
  264. call_user_func_array(array($this->_action_mod[$part],$func),array($args,$act['args']));
  265. }
  266. }
  267. /**
  268. * param_types
  269. * ??????????
  270. *
  271. * @access public
  272. * @return void
  273. */
  274. function param_types(){
  275. return array(
  276. 'bool'=>array('true'=>array('label'=>__('?')),'false'=>array('label'=>__('?'))),
  277. 'string'=>array(
  278. TEST_INCLUDE=>array('label'=>__('??'),'input'=>'text'),
  279. TEST_EXCLUDE=>array('label'=>__('???'),'input'=>'text'),
  280. TEST_BEGIN=>array('label'=>__('????'),'input'=>'text'),
  281. TEST_END=>array('label'=>__('????'),'input'=>'text')
  282. ),
  283. 'ip'=>array(
  284. TEST_INCLUDE=>array('label'=>__('????'),'input'=>'text'),
  285. TEST_EXCLUDE=>array('label'=>__('?????'),'input'=>'text')
  286. ),
  287. 'number'=>array(
  288. TEST_GREAT_THAN=>array('label'=>__('??'),'input'=>'number'),
  289. TEST_LESS_THAN=>array('label'=>__('??'),'input'=>'number'),
  290. TEST_EQUAL=>array('label'=>__('??'),'input'=>'number'),
  291. TEST_NOT_EQUAL=>array('label'=>__('???'),'input'=>'number'),
  292. ),
  293. 'date'=>array(
  294. TEST_EQUAL=>array('label'=>__('?'),'input'=>'date'),
  295. TEST_NOT_EQUAL=>array('label'=>__('??'),'input'=>'date'),
  296. TEST_EARLY_THAN=>array('label'=>__('??'),'input'=>'date'),
  297. TEST_LATE_THAN=>array('label'=>__('??'),'input'=>'date'),
  298. TEST_WEEKEND=>array('label'=>__('???')),
  299. TEST_IS_WORKDAY=>array('label'=>__('????'))
  300. ),
  301. );
  302. }
  303. }