00001 <?php
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00038 class doTriggerService extends MetaObject
00039 {
00045 public $m_DataObjName;
00046
00052 public $m_DOTriggerList = array();
00053
00060 function __construct(&$xmlArr)
00061 {
00062 $this->readMetadata($xmlArr);
00063 }
00064
00065
00072 protected function readMetadata(&$xmlArr)
00073 {
00074 parent::readMetaData($xmlArr);
00075
00076 $this->m_DataObjName = $xmlArr["PLUGINSERVICE"]["ATTRIBUTES"]["DATAOBJECTNAME"];
00077 $this->m_DataObjName = $this->prefixPackage($this->m_DataObjName);
00078 $this->readMetaCollection($xmlArr["PLUGINSERVICE"]["DOTRIGGER"], $tmpList);
00079 if (!$tmpList)
00080 return;
00081 foreach ($tmpList as $triggerXml)
00082 {
00083 $this->m_DOTriggerList[] = new DOTrigger($triggerXml);
00084 }
00085 }
00086
00094 public function execute($dataObj, $triggerType)
00095 {
00096
00097 foreach ($this->m_DOTriggerList as $doTrigger)
00098 {
00099 if ($doTrigger->m_TriggerType == $triggerType){ ;
00100 $this->executeAllActions($doTrigger, $dataObj);
00101 }
00102 }
00103 }
00104
00112 protected function executeAllActions($doTrigger, $dataObj)
00113 {
00114 if (! $this->matchCondition($doTrigger, $dataObj))
00115 return;
00116
00117 foreach ($doTrigger->m_TriggerActions as $triggerAction)
00118 {
00119 $this->executeAction($triggerAction, $dataObj);
00120 }
00121 }
00122
00130 protected function matchCondition($doTrigger, $dataObj)
00131 {
00132
00133 $condExpr = $doTrigger->m_TriggerCondition["Expression"];
00134 if ($condExpr)
00135 {
00136 $exprVal = Expression::evaluateExpression($condExpr, $dataObj);
00137 if ($exprVal !== true)
00138 return false;
00139 }
00140
00141 $extraSearchRule = $doTrigger->m_TriggerCondition["ExtraSearchRule"];
00142 if ($extraSearchRule)
00143 {
00144 $realSearchRule = Expression::evaluateExpression($extraSearchRule, $dataObj);
00145 $recordList = array();
00146
00147 $dataObj->fetchRecords($realSearchRule, $recordList, 1, 1, false);
00148 if (count($recordList) == 0)
00149 return false;
00150 }
00151 return true;
00152 }
00153
00161 protected function executeAction($triggerAction, $dataObj)
00162 {
00163
00164 $methodName = $triggerAction->m_Action;
00165
00166 if (method_exists($this, $methodName))
00167 {
00168
00169 foreach ($triggerAction->m_ArgList as $argName => $argValue)
00170 $argList[$argName] = Expression::evaluateExpression($argValue, $dataObj);
00171
00172 if ($triggerAction->m_Immediate == "Y")
00173 $this->$methodName($argList);
00174 else
00175 {
00176
00177
00178
00179
00180 }
00181 }
00182 }
00183
00192 private function _composeActionMessage($triggerAction, $methodName, $argList)
00193 {
00194 $actionMsg["Method"] = $methodName;
00195 $actionMsg["ArgList"] = $argList;
00196 $actionMsg["DelayMinutes"] = $triggerAction->m_DelayMinutes;
00197 $actionMsg["RepeatMinutes"] = $triggerAction->m_RepeatMinutes;
00198 $actionMsg["StartTime"] = strftime("%Y-%m-%d %H:%M:%S");
00199 }
00200
00201 protected function callService($argList)
00202 {
00203 $svcobj = $argList['Service'];
00204 $method = $argList['Method'];
00205 $svcobj = BizSystem::getObject($svcobj);
00206 if(!method_exists($svcobj,$method))
00207 {
00208 return;
00209 }
00210 unset($argList['Service']);
00211 unset($argList['Method']);
00212 return call_user_func_array(array($svcobj,$method),$argList);
00213
00214 }
00221 protected function executeShell($argList)
00222 {
00223 $Script = $argList["Script"];
00224 $Inputs = $argList["Inputs"];
00225 $command = "$Script $Inputs";
00226
00227 exec($command);
00228 }
00229
00230
00237 protected function executeSQL($argList)
00238 {
00239 $dbName = $argList["DBName"];
00240 if (! $dbName)
00241 $dbName = "Default";
00242 $sql = $argList["SQL"];
00243 $db = BizSystem::dbConnection($dbName);
00244 try
00245 {
00246 $resultSet = $db->query($sql);
00247 }
00248 catch (Exception $e)
00249 {
00250 $errorMessage = "Error in run SQL: " . $sql . ". " . $e->getMessage();
00251 }
00252 }
00253
00260 protected function sendEmail($argList)
00261 {
00262
00263 $emailServiceName = $argList["EmailService"];
00264 $emailService = BizSystem::getObject($emailServiceName);
00265 if ($emailService == null)
00266 return;
00267
00268 $emailService->useAccount($argList["Account"]);
00269 $TOs = doTriggerService::_makeArray($argList["TOs"]);
00270 $CCs = doTriggerService::_makeArray($argList["CCs"]);
00271 $BCCs = doTriggerService::_makeArray($argList["BCCs"]);
00272 $Attachments = doTriggerService::_makeArray($argList["Attachments"]);
00273 $subject = $argList["Subject"];
00274 $body = $argList["Body"];
00275 $ok = $emailService->sendEmail($TOs, $CCs, $BCCs, $subject, $body, $Attachments);
00276 if ($ok == false)
00277 {
00278 return $emailService->getErrorMsg();
00279 }
00280 return $ok;
00281 }
00282
00289 protected function auditTrail($argList)
00290 {
00291 $auditServiceName = $argList["AuditService"];
00292 $auditService = BizSystem::getObject($auditServiceName);
00293 if ($auditService == null)
00294 return;
00295 $dataObjName = $argList["DataObjectName"];
00296 $ok = $auditService->audit($dataObjName);
00297 if ($ok == false)
00298 {
00299
00300 }
00301 }
00302
00309 static private function _makeArray($string)
00310 {
00311 if (! $string)
00312 return null;
00313 $arr = explode(";", $string);
00314 $size = count($arr);
00315 for ($i = 0; $i < $size; $i ++)
00316 $arr[$i] = trim($arr[$i]);
00317 return $arr;
00318 }
00319
00326 protected function createInboxItem($argList)
00327 {
00328 }
00329 }
00330
00331
00340 class DOTrigger
00341 {
00342 public $m_TriggerType;
00343
00349 public $m_TriggerCondition = array();
00350
00356 public $m_TriggerActions;
00357
00364 public function __construct ($xmlArr)
00365 {
00366 $this->m_TriggerType = $xmlArr["ATTRIBUTES"]["TRIGGERTYPE"];
00367
00368 $this->m_TriggerCondition["Expression"] = $xmlArr["TRIGGERCONDITION"]["ATTRIBUTES"]["EXPRESSION"];
00369 $this->m_TriggerCondition["ExtraSearchRule"] = $xmlArr["TRIGGERCONDITION"]["ATTRIBUTES"]["EXTRASEARCHRULE"];
00370 if($xmlArr["TRIGGERACTIONS"]["TRIGGERACTION"][0]){
00371 foreach($xmlArr["TRIGGERACTIONS"]["TRIGGERACTION"] as $key=>$value){
00372 $this->m_TriggerActions[] = new TriggerAction($xmlArr["TRIGGERACTIONS"]["TRIGGERACTION"][$key]);
00373 }
00374 }else{
00375 $this->m_TriggerActions = new MetaIterator($xmlArr["TRIGGERACTIONS"]["TRIGGERACTION"], "TriggerAction");
00376 }
00377
00378 }
00379 }
00380
00389 class TriggerAction extends MetaObject
00390 {
00391 public $m_Name;
00392 public $m_Action;
00393 public $m_Immediate;
00394 public $m_DelayMinutes;
00395 public $m_RepeatMinutes;
00396 public $m_ArgList = array();
00397
00404 public function __construct ($xmlArr)
00405 {
00406 $this->m_Name = $xmlArr["ATTRIBUTES"]["NAME"];
00407 $this->m_Action = $xmlArr["ATTRIBUTES"]["ACTION"];
00408 $this->m_Immediate = $xmlArr["ATTRIBUTES"]["IMMEDIATE"];
00409 $this->m_DelayMinutes = $xmlArr["ATTRIBUTES"]["DELAYMINUTES"];
00410 $this->m_RepeatMinutes = $xmlArr["ATTRIBUTES"]["REPEATMINUTES"];
00411 $this->readMetaCollection($xmlArr["ACTIONARGUMENT"], $tmpList);
00412 if (! $tmpList)
00413 return;
00414 foreach ($tmpList as $arg)
00415 {
00416 $this->m_ArgList[$arg["ATTRIBUTES"]["NAME"]] = $arg["ATTRIBUTES"]["VALUE"];
00417 }
00418 }
00419 }