PageRenderTime 81ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/cubi/openbiz/bin/service/doTriggerService.php

http://openbiz-cubi.googlecode.com/
PHP | 419 lines | 207 code | 29 blank | 183 comment | 26 complexity | 1bf9890ec0ce959dfedc403fccd7c037 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-3.0
  1. <?php
  2. /**
  3. * PHPOpenBiz Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. *
  10. * @package openbiz.bin.service
  11. * @copyright Copyright (c) 2005-2011, Rocky Swen
  12. * @license http://www.opensource.org/licenses/bsd-license.php
  13. * @link http://www.phpopenbiz.org/
  14. * @version $Id: doTriggerService.php 3836 2011-04-21 09:44:31Z jixian2003 $
  15. */
  16. /*
  17. <PluginService Name="" Description="" Package="" Class="" BizObjectName="">
  18. <DOTrigger TriggerType="INSERT|UPDATE|DELETE"> *
  19. <TriggerCondition Expression="" ExtraSearchRule="" />
  20. <TriggerActions>
  21. <TriggerAction Action="CallService|ExecuteSQL|ExecuteShell|CreateInboxItem|SendEmail|..." Immediate="Y|N" DelayMinutes="" RepeatMinutes="">
  22. <ActionArgument Name="" Value="" /> *
  23. </TriggerAction>
  24. </TriggerActions>
  25. </DOTrigger>
  26. </PluginService>
  27. */
  28. /**
  29. * class doTriggerService is the plug-in service of handle DataObject trigger
  30. *
  31. * @package openbiz.bin.service
  32. * @author Rocky Swen
  33. * @copyright Copyright (c) 2005-2009, Rocky Swen
  34. * @access public
  35. */
  36. class doTriggerService extends MetaObject
  37. {
  38. /**
  39. * Name of {@link BizDataObj}
  40. *
  41. * @var string
  42. */
  43. public $m_DataObjName;
  44. /**
  45. * List of DOTrigger object
  46. *
  47. * @var array array of {@link DOTrigger}
  48. */
  49. public $m_DOTriggerList = array();
  50. /**
  51. * Initialize chartService with xml array metadata
  52. *
  53. * @param array $xmlArr
  54. * @return void
  55. */
  56. function __construct(&$xmlArr)
  57. {
  58. $this->readMetadata($xmlArr);
  59. }
  60. /**
  61. * Read array meta data, and store to meta object
  62. *
  63. * @param array $xmlArr
  64. * @return void
  65. */
  66. protected function readMetadata(&$xmlArr)
  67. {
  68. parent::readMetaData($xmlArr);
  69. $this->m_DataObjName = $xmlArr["PLUGINSERVICE"]["ATTRIBUTES"]["DATAOBJECTNAME"];
  70. $this->m_DataObjName = $this->prefixPackage($this->m_DataObjName);
  71. $this->readMetaCollection($xmlArr["PLUGINSERVICE"]["DOTRIGGER"], $tmpList);
  72. if (!$tmpList)
  73. return;
  74. foreach ($tmpList as $triggerXml)
  75. {
  76. $this->m_DOTriggerList[] = new DOTrigger($triggerXml);
  77. }
  78. }
  79. /**
  80. * Execute trigger
  81. *
  82. * @param BizDataObj $dataObj
  83. * @param <type> $triggerType
  84. * @return void
  85. */
  86. public function execute($dataObj, $triggerType)
  87. {
  88. /* @var $doTrigger DOTrigger */
  89. foreach ($this->m_DOTriggerList as $doTrigger)
  90. {
  91. if ($doTrigger->m_TriggerType == $triggerType){ ;
  92. $this->executeAllActions($doTrigger, $dataObj);
  93. }
  94. }
  95. }
  96. /**
  97. * Execute all action
  98. *
  99. * @param DOTrigger $doTrigger
  100. * @param BizDataObj $dataObj
  101. * @return void
  102. */
  103. protected function executeAllActions($doTrigger, $dataObj)
  104. {
  105. if (! $this->matchCondition($doTrigger, $dataObj))
  106. return;
  107. /* @var $triggerAction TriggerAction */
  108. foreach ($doTrigger->m_TriggerActions as $triggerAction)
  109. {
  110. $this->executeAction($triggerAction, $dataObj);
  111. }
  112. }
  113. /**
  114. * Match condition
  115. *
  116. * @param DOTrigger $doTrigger
  117. * @param BizDataObj $dataObj
  118. * @return boolean
  119. */
  120. protected function matchCondition($doTrigger, $dataObj)
  121. {
  122. // evaluate expression
  123. $condExpr = $doTrigger->m_TriggerCondition["Expression"];
  124. if ($condExpr)
  125. {
  126. $exprVal = Expression::evaluateExpression($condExpr, $dataObj);
  127. if ($exprVal !== true)
  128. return false;
  129. }
  130. // do query with extra search rule, check if there's any record returned
  131. $extraSearchRule = $doTrigger->m_TriggerCondition["ExtraSearchRule"];
  132. if ($extraSearchRule)
  133. {
  134. $realSearchRule = Expression::evaluateExpression($extraSearchRule, $dataObj);
  135. $recordList = array();
  136. // get one record of the first page with additional searchrule
  137. $dataObj->fetchRecords($realSearchRule, $recordList, 1, 1, false);
  138. if (count($recordList) == 0)
  139. return false;
  140. }
  141. return true;
  142. }
  143. /**
  144. * Execute action
  145. *
  146. * @param TriggerAction $triggerAction
  147. * @param BizDataObj $dataObj
  148. * @return void
  149. */
  150. protected function executeAction($triggerAction, $dataObj)
  151. {
  152. // action method
  153. $methodName = $triggerAction->m_Action;
  154. // action method arguments
  155. if (method_exists($this, $methodName))
  156. {
  157. // evaluate arguments as expression support
  158. foreach ($triggerAction->m_ArgList as $argName => $argValue)
  159. $argList[$argName] = Expression::evaluateExpression($argValue, $dataObj);
  160. // check the immediate flag
  161. if ($triggerAction->m_Immediate == "Y") // call the method if Immediate is "Y"
  162. $this->$methodName($argList);
  163. else
  164. { // put it to a passive queue
  165. /* $passiveQueueSvc->Push($methodName,
  166. $argList,
  167. $triggerAction->m_DelayMinutes,
  168. $triggerAction->m_RepeatMinutes); */
  169. }
  170. }
  171. }
  172. /**
  173. * Compose action message
  174. *
  175. * @param TriggerAction $triggerAction
  176. * @param string $methodName
  177. * @param array $argList
  178. * @return void
  179. */
  180. private function _composeActionMessage($triggerAction, $methodName, $argList)
  181. {
  182. $actionMsg["Method"] = $methodName;
  183. $actionMsg["ArgList"] = $argList;
  184. $actionMsg["DelayMinutes"] = $triggerAction->m_DelayMinutes;
  185. $actionMsg["RepeatMinutes"] = $triggerAction->m_RepeatMinutes;
  186. $actionMsg["StartTime"] = strftime("%Y-%m-%d %H:%M:%S");
  187. }
  188. protected function callService($argList)
  189. {
  190. $svcobj = $argList['Service'];
  191. $method = $argList['Method'];
  192. $svcobj = BizSystem::getObject($svcobj);
  193. if(!method_exists($svcobj,$method))
  194. {
  195. return;
  196. }
  197. unset($argList['Service']);
  198. unset($argList['Method']);
  199. return call_user_func_array(array($svcobj,$method),$argList);
  200. }
  201. /**
  202. * Execute shell
  203. *
  204. * @param array $argList
  205. * @return void
  206. */
  207. protected function executeShell($argList)
  208. {
  209. $Script = $argList["Script"];
  210. $Inputs = $argList["Inputs"];
  211. $command = "$Script $Inputs";
  212. //$result = exec($command, $output);
  213. exec($command);
  214. }
  215. /**
  216. * Execute SQL
  217. *
  218. * @param array $argList
  219. * @return void
  220. */
  221. protected function executeSQL($argList)
  222. {
  223. $dbName = $argList["DBName"];
  224. if (! $dbName)
  225. $dbName = "Default";
  226. $sql = $argList["SQL"];
  227. $db = BizSystem::dbConnection($dbName);
  228. try
  229. {
  230. $resultSet = $db->query($sql);
  231. }
  232. catch (Exception $e)
  233. {
  234. $errorMessage = "Error in run SQL: " . $sql . ". " . $e->getMessage();
  235. }
  236. }
  237. /**
  238. * Send email
  239. *
  240. * @param array $argList
  241. * @return boolean|string if sending error return error messege, if success return true
  242. */
  243. protected function sendEmail($argList)
  244. {
  245. // emailService
  246. $emailServiceName = $argList["EmailService"];
  247. $emailService = BizSystem::getObject($emailServiceName);
  248. if ($emailService == null)
  249. return;
  250. $emailService->useAccount($argList["Account"]);
  251. $TOs = doTriggerService::_makeArray($argList["TOs"]);
  252. $CCs = doTriggerService::_makeArray($argList["CCs"]);
  253. $BCCs = doTriggerService::_makeArray($argList["BCCs"]);
  254. $Attachments = doTriggerService::_makeArray($argList["Attachments"]);
  255. $subject = $argList["Subject"];
  256. $body = $argList["Body"];
  257. $ok = $emailService->sendEmail($TOs, $CCs, $BCCs, $subject, $body, $Attachments);
  258. if ($ok == false)
  259. {
  260. return $emailService->getErrorMsg();
  261. }
  262. return $ok;
  263. }
  264. /**
  265. * Audit trail
  266. *
  267. * @param array $argList
  268. * @return void
  269. */
  270. protected function auditTrail($argList)
  271. {
  272. $auditServiceName = $argList["AuditService"];
  273. $auditService = BizSystem::getObject($auditServiceName);
  274. if ($auditService == null)
  275. return;
  276. $dataObjName = $argList["DataObjectName"];
  277. $ok = $auditService->audit($dataObjName);
  278. if ($ok == false)
  279. {
  280. // log $auditSvc->getErrorMsg();
  281. }
  282. }
  283. /**
  284. * Make array from string with semi colon delimited
  285. *
  286. * @param string $string string with ";" (semi colon) delimited
  287. * @return array
  288. */
  289. static private function _makeArray($string)
  290. {
  291. if (! $string)
  292. return null;
  293. $arr = explode(";", $string);
  294. $size = count($arr);
  295. for ($i = 0; $i < $size; $i ++)
  296. $arr[$i] = trim($arr[$i]);
  297. return $arr;
  298. }
  299. /**
  300. * Create inbox item
  301. *
  302. * @param array $argList
  303. * @return void
  304. */
  305. protected function createInboxItem($argList)
  306. { // call inbox service
  307. }
  308. }
  309. /**
  310. * DOTrigger class
  311. *
  312. * @package openbiz.bin.service
  313. * @author Rocky Swen
  314. * @copyright Copyright (c) 2005-2009, Rocky Swen
  315. * @access public
  316. */
  317. class DOTrigger
  318. {
  319. public $m_TriggerType;
  320. /**
  321. * Trigger condition
  322. *
  323. * @var array
  324. */
  325. public $m_TriggerCondition = array();
  326. /**
  327. * Iterator of TriggerAction
  328. *
  329. * @var MetaIterator
  330. */
  331. public $m_TriggerActions;
  332. /**
  333. * Initialize DOTrigger with xml array metadata
  334. *
  335. * @param array $xmlArr
  336. * @return void
  337. */
  338. public function __construct ($xmlArr)
  339. {
  340. $this->m_TriggerType = $xmlArr["ATTRIBUTES"]["TRIGGERTYPE"];
  341. // read in trigger condition
  342. $this->m_TriggerCondition["Expression"] = $xmlArr["TRIGGERCONDITION"]["ATTRIBUTES"]["EXPRESSION"];
  343. $this->m_TriggerCondition["ExtraSearchRule"] = $xmlArr["TRIGGERCONDITION"]["ATTRIBUTES"]["EXTRASEARCHRULE"];
  344. if($xmlArr["TRIGGERACTIONS"]["TRIGGERACTION"][0]){
  345. foreach($xmlArr["TRIGGERACTIONS"]["TRIGGERACTION"] as $key=>$value){
  346. $this->m_TriggerActions[] = new TriggerAction($xmlArr["TRIGGERACTIONS"]["TRIGGERACTION"][$key]);
  347. }
  348. }else{
  349. $this->m_TriggerActions = new MetaIterator($xmlArr["TRIGGERACTIONS"]["TRIGGERACTION"], "TriggerAction");
  350. }
  351. }
  352. }
  353. /**
  354. * TriggerAction class
  355. *
  356. * @package openbiz.bin.service
  357. * @author Rocky Swen
  358. * @copyright Copyright (c) 2005-2009, Rocky Swen
  359. * @access public
  360. */
  361. class TriggerAction extends MetaObject
  362. {
  363. public $m_Name;
  364. public $m_Action;
  365. public $m_Immediate;
  366. public $m_DelayMinutes;
  367. public $m_RepeatMinutes;
  368. public $m_ArgList = array();
  369. /**
  370. * Initialize TriggerAction with xml array metadata
  371. *
  372. * @param array $xmlArr
  373. * @return void
  374. */
  375. public function __construct ($xmlArr)
  376. {
  377. $this->m_Name = $xmlArr["ATTRIBUTES"]["NAME"];
  378. $this->m_Action = $xmlArr["ATTRIBUTES"]["ACTION"];
  379. $this->m_Immediate = $xmlArr["ATTRIBUTES"]["IMMEDIATE"];
  380. $this->m_DelayMinutes = $xmlArr["ATTRIBUTES"]["DELAYMINUTES"];
  381. $this->m_RepeatMinutes = $xmlArr["ATTRIBUTES"]["REPEATMINUTES"];
  382. $this->readMetaCollection($xmlArr["ACTIONARGUMENT"], $tmpList);
  383. if (! $tmpList)
  384. return;
  385. foreach ($tmpList as $arg)
  386. {
  387. $this->m_ArgList[$arg["ATTRIBUTES"]["NAME"]] = $arg["ATTRIBUTES"]["VALUE"];
  388. }
  389. }
  390. }