Example #1
0
 /**
  * Executes a flow, starting by checking the trigger, passing params to each trigger/action,
  * and calling {@link X2Flow::executeBranch()}
  *
  * @param X2Flow &$flow the object representing the flow to run
  * @param array &$params an associative array of params, usually including 'model'=>$model,
  * @param mixed $actionId a unique id for the flow action where flow execution should start.
  *  This can also be an array of directions to the flow action (legacy option). 
  * Will skip checking the trigger conditions if not null, otherwise runs the entire flow.
  */
 private static function _executeFlow(&$flow, &$params, $actionId = null, $triggerLogId = null)
 {
     $error = '';
     //array($flow->name);
     $flowData = $flow->getFlow();
     if ($flowData !== false && isset($flowData['trigger']['type'], $flowData['items'][0]['type'])) {
         if ($actionId === null) {
             $trigger = X2FlowTrigger::create($flowData['trigger']);
             assert($trigger !== null);
             if ($trigger === null) {
                 $error = array('trace' => array(false, 'failed to load trigger class'));
             }
             $validateRetArr = $trigger->validate($params, $flow->id);
             if (!$validateRetArr[0]) {
                 $error = $validateRetArr;
                 return array('trace' => $error);
             } else {
                 if (sizeof($validateRetArr) === 3) {
                     // trigger has return value
                     return array('trace' => $validateRetArr, 'retVal' => $validateRetArr[2]);
                 }
             }
             $checkRetArr = $trigger->check($params);
             if (!$checkRetArr[0]) {
                 $error = $checkRetArr;
             }
             if (empty($error)) {
                 try {
                     $flowTrace = array(true, $flow->executeBranch($flowData['items'], $params, $triggerLogId));
                     $flowRetVal = self::extractRetValFromTrace($flowTrace);
                     if (!$flowRetVal) {
                         $flowRetVal = $trigger->getDefaultReturnVal($flow->id);
                     }
                     return array('trace' => $flowTrace, 'retVal' => $flowRetVal);
                 } catch (Exception $e) {
                     return array('trace' => array(false, $e->getMessage()));
                     // whatever.
                 }
             } else {
                 return array('trace' => $error);
             }
         } else {
             // $actionId provided, skip to the specified position using X2Flow::traverse()
             try {
                 return array('trace' => array(true, $flow->traverse($actionId, $flowData['items'], $params, $triggerLogId)));
             } catch (Exception $e) {
                 return array('trace' => array(false, $e->getMessage()));
                 // whatever.
             }
         }
     } else {
         return array('trace' => array(false, 'invalid flow data'));
     }
 }
Example #2
0
 /**
  * Executes a flow, starting by checking the trigger, passing params to each trigger/action,
  * and calling {@link X2Flow::executeBranch()}
  *
  * @param X2Flow &$flow the object representing the flow to run
  * @param array &$params an associative array of params, usually including 'model'=>$model,
  * @param mixed $flowPath an array of directions to a specific point in the flow. Defaults to
  *  null.
  * Will skip checking the trigger conditions if not null, otherwise runs the entire flow.
  */
 private static function executeFlow(&$flow, &$params, $flowPath = null, $triggerLogId = null)
 {
     $error = '';
     //array($flow->name);
     $flowData = CJSON::decode($flow->flow);
     // parse JSON flow data
     // file_put_contents('triggerLog.txt',"\n".print_r($flowData,true),FILE_APPEND);
     if ($flowData !== false && isset($flowData['trigger']['type'], $flowData['items'][0]['type'])) {
         if ($flowPath === null) {
             //AuxLib::debugLogR ('creating trigger');
             $trigger = X2FlowTrigger::create($flowData['trigger']);
             assert($trigger !== null);
             if ($trigger === null) {
                 $error = array('trace' => array(false, 'failed to load trigger class'));
             }
             //AuxLib::debugLogR ('validating');
             $validateRetArr = $trigger->validate($params, $flow->id);
             if (!$validateRetArr[0]) {
                 $error = $validateRetArr;
                 return array('trace' => $error);
             } else {
                 if (sizeof($validateRetArr) === 3) {
                     // trigger has return value
                     return array('trace' => $validateRetArr, 'retVal' => $validateRetArr[2]);
                 }
             }
             //AuxLib::debugLogR ('checking');
             $checkRetArr = $trigger->check($params);
             if (!$checkRetArr[0]) {
                 $error = $checkRetArr;
             }
             //AuxLib::debugLogR ('done checking');
             if (empty($error)) {
                 try {
                     //AuxLib::debugLogR ('executeBranch');
                     $flowTrace = array(true, $flow->executeBranch(array(0), $flowData['items'], $params, 0, $triggerLogId));
                     //AuxLib::debugLog ('executing branch complete');
                     //AuxLib::debugLogR ($flowTrace);
                     $flowRetVal = self::extractRetValFromTrace($flowTrace);
                     //AuxLib::debugLog ('extractRetValFromTrace ret');
                     if (!$flowRetVal) {
                         $flowRetVal = $trigger->getDefaultReturnVal($flow->id);
                     }
                     return array('trace' => $flowTrace, 'retVal' => $flowRetVal);
                 } catch (Exception $e) {
                     //AuxLib::debugLogR ($e->getTrace ());
                     //AuxLib::debugLogR ($e->getMessage ());
                     return array('trace' => array(false, $e->getMessage()));
                     // whatever.
                 }
             } else {
                 return array('trace' => $error);
             }
         } else {
             // $flowPath provided, skip to the specified position using X2Flow::traverse()
             try {
                 return array('trace' => array(true, $flow->traverse($flowPath, $flowData['items'], $params, 0, $triggerLogId)));
             } catch (Exception $e) {
                 //AuxLib::debugLogR ($e->getTrace ());
                 //AuxLib::debugLogR ($e->getMessage ());
                 return array('trace' => array(false, $e->getMessage()));
                 // whatever.
             }
         }
     } else {
         return array('trace' => array(false, 'invalid flow data'));
     }
 }