/**
  * Method to execute a single action
  *
  * @param object CRM_Civirules_TriggerData_TriggerData $triggerData
  * @param array $ruleAction
  * @access protected
  * @static
  */
 protected static function executeAction(CRM_Civirules_TriggerData_TriggerData $triggerData, $ruleAction)
 {
     $object = CRM_Civirules_BAO_Action::getActionObjectById($ruleAction['action_id'], true);
     if (!$object) {
         return;
     }
     $object->setRuleActionData($ruleAction);
     //determine if the action should be executed with a delay
     $delay = self::getActionDelay($ruleAction, $object);
     if ($delay instanceof DateTime) {
         self::delayAction($delay, $object, $triggerData);
     } else {
         //there is no delay so process action immediatly
         $object->processAction($triggerData);
     }
 }
/**
 * CiviRuleAction.Create API
 *
 * @param array $params
 * @return array API result descriptor
 * @see civicrm_api3_create_success
 * @see civicrm_api3_create_error
 */
function civicrm_api3_civi_rule_action_create($params)
{
    if (!isset($params['id']) && empty($params['label'])) {
        return civicrm_api3_create_error('Label can not be empty when adding a new CiviRule Action');
    }
    if (empty($params['class_name']) && !isset($params['id'])) {
        return civicrm_api3_create_error('Class_name can not be empty');
    }
    /*
     * set created or modified date and user_id
     */
    $session = CRM_Core_Session::singleton();
    $userId = $session->get('userID');
    if (isset($params['id'])) {
        $params['modified_date'] = date('Ymd');
        $params['modified_user_id'] = $userId;
    } else {
        $params['created_date'] = date('Ymd');
        $params['created_user_id'] = $userId;
    }
    $returnValues = CRM_Civirules_BAO_Action::add($params);
    return civicrm_api3_create_success($returnValues, $params, 'CiviRuleAction', 'Create');
}
 /**
  * Function to perform post save processing (extends parent function)
  *
  * @access public
  */
 function postProcess()
 {
     $saveParams = array('rule_id' => $this->_submitValues['rule_id'], 'action_id' => $this->_submitValues['rule_action_select'], 'delay' => 'null');
     if ($this->ruleActionId) {
         $saveParams['id'] = $this->ruleActionId;
     }
     if (!empty($this->_submitValues['delay_select'])) {
         $delayClass = CRM_Civirules_Delay_Factory::getDelayClassByName($this->_submitValues['delay_select']);
         $delayClass->setValues($this->_submitValues);
         $saveParams['delay'] = serialize($delayClass);
     }
     $ruleAction = CRM_Civirules_BAO_RuleAction::add($saveParams);
     $session = CRM_Core_Session::singleton();
     $session->setStatus('Action added to CiviRule ' . CRM_Civirules_BAO_Rule::getRuleLabelWithId($this->_submitValues['rule_id']), 'Action added', 'success');
     $action = CRM_Civirules_BAO_Action::getActionObjectById($ruleAction['action_id'], true);
     $redirectUrl = $action->getExtraDataInputUrl($ruleAction['id']);
     if (empty($redirectUrl) || $this->ruleActionId) {
         $redirectUrl = CRM_Utils_System::url('civicrm/civirule/form/rule', 'action=update&id=' . $this->_submitValues['rule_id'], TRUE);
     } elseif (!$this->ruleActionId) {
         $redirectUrl .= '&action=add';
     }
     CRM_Utils_System::redirect($redirectUrl);
 }
 /**
  * Function to get the rule actions for the rule
  *
  * @return array $ruleActions
  * @access protected
  */
 protected function getRuleActions()
 {
     $actionParams = array('is_active' => 1, 'rule_id' => $this->ruleId);
     $ruleActions = CRM_Civirules_BAO_RuleAction::getValues($actionParams);
     foreach ($ruleActions as $ruleActionId => $ruleAction) {
         $actionClass = CRM_Civirules_BAO_Action::getActionObjectById($ruleAction['action_id']);
         $actionClass->setRuleActionData($ruleAction);
         $ruleActions[$ruleActionId]['label'] = CRM_Civirules_BAO_Action::getActionLabelWithId($ruleAction['action_id']);
         $ruleActions[$ruleActionId]['actions'] = $this->setRuleActionActions($ruleActionId, $actionClass);
         $ruleActions[$ruleActionId]['formattedConditionParams'] = $actionClass->userFriendlyConditionParams();
         $ruleActions[$ruleActionId]['formattedDelay'] = '';
         if (!empty($ruleAction['delay'])) {
             $delayClass = unserialize($ruleAction['delay']);
             $ruleActions[$ruleActionId]['formattedDelay'] = $delayClass->getDelayExplanation();
         }
     }
     return $ruleActions;
 }
Ejemplo n.º 5
0
/**
 * CiviRuleAction.Get API
 *
 * @param array $params
 * @return array API result descriptor
 * @see civicrm_api3_create_success
 * @see civicrm_api3_create_error
 * @throws API_Exception
 */
function civicrm_api3_civi_rule_action_get($params)
{
    $returnValues = CRM_Civirules_BAO_Action::getValues($params);
    return civicrm_api3_create_success($returnValues, $params, 'CiviRuleAction', 'Get');
}
/**
 * CiviRuleAction.Create API
 *
 * @param array $params
 * @return array API result descriptor
 * @see civicrm_api3_create_success
 * @see civicrm_api3_create_error
 */
function civicrm_api3_civi_rule_action_delete($params)
{
    CRM_Civirules_BAO_Action::deleteWithId($params['id']);
    $returnValues[$params['id']] = array();
    return civicrm_api3_create_success($returnValues, $params, 'CiviRuleAction', 'Create');
}
Ejemplo n.º 7
0
 /**
  * Function to build the action list
  *
  * @return array $actionList
  * @access public
  * @static
  */
 public static function buildActionList()
 {
     $actionList = array();
     $actions = CRM_Civirules_BAO_Action::getValues(array());
     foreach ($actions as $actionId => $action) {
         $actionList[$actionId] = $action['label'];
     }
     return $actionList;
 }
 /**
  * Get the action class for this condition
  *
  * @param $actionId
  * @param bool $abort if true this function will throw an exception if class could not be instantiated
  * @return CRM_Civirules_Action
  * @throws Exception if abort is set to true and class does not exist or is not valid
  */
 public static function getActionObjectById($actionId, $abort = true)
 {
     $action = new CRM_Civirules_BAO_Action();
     $action->id = $actionId;
     if (!$action->find(true)) {
         if ($abort) {
             throw new Exception('CiviRule could not find action');
         }
         return false;
     }
     $className = $action->class_name;
     if (!class_exists($className)) {
         if ($abort) {
             throw new Exception('CiviRule action class "' . $className . '" does not exist');
         }
         return false;
     }
     $object = new $className();
     if (!$object instanceof CRM_Civirules_Action) {
         if ($abort) {
             throw new Exception('CiviRule action class "' . $className . '" is not a subclass of CRM_Civirules_Action');
         }
         return false;
     }
     $actionData = array();
     CRM_Core_DAO::storeValues($action, $actionData);
     $object->setActionData($actionData);
     return $object;
 }
 /**
  * Logs a message to the logger
  *
  * @param $message
  * @param \CRM_Civirules_TriggerData_TriggerData|NULL $triggerData
  * @param string $level Should be one of \Psr\Log\LogLevel
  */
 protected function logAction($message, CRM_Civirules_TriggerData_TriggerData $triggerData = null, $level = \Psr\Log\LogLevel::INFO)
 {
     $context = array();
     $context['message'] = $message;
     $context['rule_id'] = $this->ruleAction['rule_id'];
     $rule = new CRM_Civirules_BAO_Rule();
     $rule->id = $this->ruleAction['rule_id'];
     $context['rule_title'] = '';
     if ($rule->find(true)) {
         $context['rule_title'] = $rule->label;
     }
     $context['rule_action_id'] = $this->ruleAction['id'];
     $context['action_label'] = CRM_Civirules_BAO_Action::getActionLabelWithId($this->ruleAction['action_id']);
     $context['action_parameters'] = $this->userFriendlyConditionParams();
     $context['contact_id'] = $triggerData ? $triggerData->getContactId() : -1;
     $msg = "{action_label} (ID: {rule_action_id})\r\n\r\n{message}\r\n\r\nRule: '{rule_title}' with id {rule_id}";
     if ($context['contact_id'] > 0) {
         $msg .= "\r\nFor contact: {contact_id}";
     }
     CRM_Civirules_Utils_LoggerFactory::log($msg, $context, $level);
 }