public function getRuleTitle()
 {
     if (empty($this->ruleTitle) && !empty($this->ruleId)) {
         $rule = new CRM_Civirules_BAO_Rule();
         $rule->id = $this->ruleId;
         if ($rule->find(true)) {
             $this->ruleTitle = $rule->label;
         }
     }
     return $this->ruleTitle;
 }
/**
 * CiviRuleRule.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_rule_create($params)
{
    if (empty($params['twitter_name'])) {
        return civicrm_api3_create_error('Twitter_name can not be empty');
    }
    if (empty($params['description'])) {
        return civicrm_api3_create_error('Description can not be empty');
    }
    $returnValues = CRM_Civirules_BAO_Rule::add($params);
    return civicrm_api3_create_success($returnValues, $params, 'CiviRuleRule', 'Create');
}
 /**
  * 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 logCondition($message, CRM_Civirules_TriggerData_TriggerData $triggerData = null, $level = \Psr\Log\LogLevel::INFO)
 {
     $context = array();
     $context['message'] = $message;
     $context['rule_id'] = $this->ruleCondition['rule_id'];
     $rule = new CRM_Civirules_BAO_Rule();
     $rule->id = $this->ruleCondition['rule_id'];
     $context['rule_title'] = '';
     if ($rule->find(true)) {
         $context['rule_title'] = $rule->label;
     }
     $context['rule_condition_id'] = $this->ruleCondition['id'];
     $context['condition_label'] = CRM_Civirules_BAO_Condition::getConditionLabelWithId($this->ruleCondition['condition_id']);
     $context['condition_parameters'] = $this->userFriendlyConditionParams();
     $context['contact_id'] = $triggerData ? $triggerData->getContactId() : -1;
     $msg = "{condition_label} (ID: {rule_condition_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);
 }
Esempio n. 4
0
/**
 * Civirules.Cron 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_civirules_cron($params)
{
    $returnValues = array();
    $rules = CRM_Civirules_BAO_Rule::findRulesForCron();
    foreach ($rules as $rule) {
        $return = $rule->process();
        $triggeredEntities = $return['count'];
        $triggeredActions = $return['is_valid_count'];
        $returnValues[$rule->getRuleId()] = array('rule' => CRM_Civirules_BAO_Rule::getRuleLabelWithId($rule->getRuleId()), 'triggered_entities' => $triggeredEntities, 'triggered_actions' => $triggeredActions);
    }
    return civicrm_api3_create_success($returnValues, $params, 'Civirules', 'cron');
}
 /**
  * Function to get the data 
  * 
  * @return array $rules
  * @access protected
  */
 protected function getRules()
 {
     $rules = CRM_Civirules_BAO_Rule::getValues(array());
     foreach ($rules as $ruleId => $rule) {
         $rules[$ruleId]['actions'] = $this->setRowActions($rule);
         if (isset($rule['event_id']) && !empty($rule['event_id'])) {
             $rules[$ruleId]['event_label'] = CRM_Civirules_BAO_Event::getEventLabelWithId($rule['event_id']);
         }
         $rules[$ruleId]['created_contact_name'] = CRM_Civirules_Utils::getContactName($rule['created_user_id']);
         $rules[$ruleId]['is_active'] = CRM_Civirules_Utils::formatIsActive($rule['is_active']);
     }
     return $rules;
 }
 /**
  * Method post
  *
  * @param string $op
  * @param string $objectName
  * @param int $objectId
  * @param object $objectRef
  * @access public
  * @static
  */
 public static function post($op, $objectName, $objectId, &$objectRef)
 {
     $extensionConfig = CRM_Civirules_Config::singleton();
     if (!in_array($op, $extensionConfig->getValidTriggerOperations())) {
         return;
     }
     //find matching rules for this objectName and op
     $triggers = CRM_Civirules_BAO_Rule::findRulesByObjectNameAndOp($objectName, $op);
     foreach ($triggers as $trigger) {
         if ($trigger instanceof CRM_Civirules_Trigger_Post) {
             $trigger->triggerTrigger($op, $objectName, $objectId, $objectRef);
         }
     }
 }
/**
 * CiviRuleRule.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_rule_create($params)
{
    if (!isset($params['id']) && empty($params['label'])) {
        return civicrm_api3_create_error('Label can not be empty when adding a new CiviRule');
    }
    if (empty($params['trigger_id'])) {
        return civicrm_api3_create_error('Trigger_id 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_Rule::add($params);
    return civicrm_api3_create_success($returnValues, $params, 'CiviRuleRule', 'Create');
}
 /**
  * Function to link a trigger to a rule
  *
  * @param array $formValues
  */
 protected function saveRuleTrigger($formValues)
 {
     if (isset($formValues['rule_trigger_select'])) {
         $ruleParams = array('id' => $this->ruleId, 'trigger_id' => $formValues['rule_trigger_select']);
         CRM_Civirules_BAO_Rule::add($ruleParams);
     }
 }
 /**
  * Overridden parent method to process form data after submission
  *
  * @throws Exception when rule condition not found
  * @access public
  */
 public function postProcess()
 {
     $data = unserialize($this->ruleCondition->condition_params);
     $data['original_operator'] = $this->_submitValues['original_operator'];
     $data['original_value'] = $this->_submitValues['original_value'];
     if (isset($this->_submitValues['original_multi_value'])) {
         $data['original_multi_value'] = explode("\r\n", $this->_submitValues['original_multi_value']);
     }
     $data['operator'] = $this->_submitValues['operator'];
     $data['value'] = $this->_submitValues['value'];
     if (isset($this->_submitValues['multi_value'])) {
         $data['multi_value'] = explode("\r\n", $this->_submitValues['multi_value']);
     }
     $this->ruleCondition->condition_params = serialize($data);
     $this->ruleCondition->save();
     $session = CRM_Core_Session::singleton();
     $session->setStatus('Condition ' . $this->condition->label . ' parameters updated to CiviRule ' . CRM_Civirules_BAO_Rule::getRuleLabelWithId($this->ruleCondition->rule_id), 'Condition parameters updated', 'success');
     $redirectUrl = CRM_Utils_System::url('civicrm/civirule/form/rule', 'action=update&id=' . $this->ruleCondition->rule_id, TRUE);
     CRM_Utils_System::redirect($redirectUrl);
 }
 /**
  * Method to set the form title
  *
  * @access protected
  */
 protected function setFormTitle()
 {
     $conditionLabel = '';
     $ruleCondition = new CRM_Civirules_BAO_RuleCondition();
     $ruleCondition->id = $this->ruleConditionId;
     if ($ruleCondition->find(true)) {
         $condition = new CRM_Civirules_BAO_Condition();
         $condition->id = $ruleCondition->condition_id;
         if ($condition->find(true)) {
             $conditionLabel = $condition->label;
         }
     }
     $title = 'CiviRules Edit Condition parameters';
     $this->assign('ruleConditionHeader', 'Edit Condition ' . $conditionLabel . ' of CiviRule ' . CRM_Civirules_BAO_Rule::getRuleLabelWithId($ruleCondition->rule_id));
     CRM_Utils_System::setTitle($title);
 }
 /**
  * @param $fields
  */
 static function validateConditionEntities($fields)
 {
     $conditionClass = CRM_Civirules_BAO_Condition::getConditionObjectById($fields['rule_condition_select'], false);
     if (!$conditionClass) {
         $errors['rule_condition_select'] = ts('Not a valid condition, condition class is missing');
         return $errors;
     }
     $requiredEntities = $conditionClass->requiredEntities();
     $rule = new CRM_Civirules_BAO_Rule();
     $rule->id = $fields['rule_id'];
     $rule->find(true);
     $event = new CRM_Civirules_BAO_Event();
     $event->id = $rule->event_id;
     $event->find(true);
     $eventObject = CRM_Civirules_BAO_Event::getPostEventObjectByClassName($event->class_name, true);
     $eventObject->setEventId($event->id);
     $availableEntities = array();
     foreach ($eventObject->getProvidedEntities() as $entityDef) {
         $availableEntities[] = strtolower($entityDef->entity);
     }
     foreach ($requiredEntities as $entity) {
         if (!in_array(strtolower($entity), $availableEntities)) {
             $errors['rule_condition_select'] = ts('This condition is not available with event %1', array(1 => $event->label));
             return $errors;
         }
     }
     return true;
 }
/**
 * CiviRuleRule.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_rule_get($params)
{
    $returnValues = CRM_Civirules_BAO_Rule::getValues($params);
    return civicrm_api3_create_success($returnValues, $params, 'CiviRuleRule', 'Get');
}
 /**
  * Function to set the form title based on action and data coming in
  *
  * @access protected
  */
 protected function setFormTitle()
 {
     $title = 'CiviRules Add Action';
     $this->assign('ruleActionHeader', 'Add Action to CiviRule ' . CRM_Civirules_BAO_Rule::getRuleLabelWithId($this->ruleId));
     CRM_Utils_System::setTitle($title);
 }
 public static function getLatestRuleId()
 {
     $rule = new CRM_Civirules_BAO_Rule();
     $query = 'SELECT MAX(id) AS maxId FROM ' . $rule->tableName();
     $dao = CRM_Core_DAO::executeQuery($query);
     if ($dao->fetch()) {
         return $dao->maxId;
     }
 }