/**
  * 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);
 }
 /**
  * Overridden parent method to perform processing before form is build
  *
  * @access public
  */
 public function preProcess()
 {
     $this->ruleConditionId = CRM_Utils_Request::retrieve('rule_condition_id', 'Integer');
     $this->ruleCondition = new CRM_Civirules_BAO_RuleCondition();
     $this->ruleCondition->id = $this->ruleConditionId;
     $ruleConditionData = array();
     CRM_Core_DAO::storeValues($this->ruleCondition, $ruleConditionData);
     $this->condition = new CRM_Civirules_BAO_Condition();
     $this->rule = new CRM_Civirules_BAO_Rule();
     $this->trigger = new CRM_Civirules_BAO_Trigger();
     if (!$this->ruleCondition->find(true)) {
         throw new Exception('Civirules could not find ruleCondition');
     }
     $this->condition->id = $this->ruleCondition->condition_id;
     if (!$this->condition->find(true)) {
         throw new Exception('Civirules could not find condition');
     }
     $this->rule->id = $this->ruleCondition->rule_id;
     if (!$this->rule->find(true)) {
         throw new Exception('Civirules could not find rule');
     }
     $this->trigger->id = $this->rule->trigger_id;
     if (!$this->trigger->find(true)) {
         throw new Exception('Civirules could not find trigger');
     }
     $this->conditionClass = CRM_Civirules_BAO_Condition::getConditionObjectById($this->condition->id, false);
     if ($this->conditionClass) {
         $this->conditionClass->setRuleConditionData($ruleConditionData);
     }
     $this->triggerClass = CRM_Civirules_BAO_Trigger::getTriggerObjectByTriggerId($this->trigger->id, true);
     $this->triggerClass->setTriggerId($this->trigger->id);
     $this->triggerClass->setTriggerParams($this->rule->trigger_params);
     parent::preProcess();
     $this->setFormTitle();
     //set user context
     $session = CRM_Core_Session::singleton();
     $editUrl = CRM_Utils_System::url('civicrm/civirule/form/rule', 'action=update&id=' . $this->rule->id, TRUE);
     $session->pushUserContext($editUrl);
 }
/**
 * CiviRuleCondition.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_condition_create($params)
{
    if (!isset($params['id']) && empty($params['label'])) {
        return civicrm_api3_create_error('Label can not be empty when adding a new CiviRule Condition');
    }
    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_Condition::add($params);
    return civicrm_api3_create_success($returnValues, $params, 'CiviRuleCondition', 'Create');
}
 /**
  * Function to get the rule conditions for the rule
  *
  * @return array $ruleConditions
  * @access protected
  */
 protected function getRuleConditions()
 {
     $conditionParams = array('is_active' => 1, 'rule_id' => $this->ruleId);
     $ruleConditions = CRM_Civirules_BAO_RuleCondition::getValues($conditionParams);
     foreach ($ruleConditions as $ruleConditionId => $ruleCondition) {
         $conditionClass = CRM_Civirules_BAO_Condition::getConditionObjectById($ruleCondition['condition_id']);
         $conditionClass->setRuleConditionData($ruleCondition);
         $ruleConditions[$ruleConditionId]['name'] = CRM_Civirules_BAO_Condition::getConditionLabelWithId($ruleCondition['condition_id']);
         $ruleConditions[$ruleConditionId]['actions'] = $this->setRuleConditionActions($ruleConditionId, $conditionClass);
         $ruleConditions[$ruleConditionId]['formattedConditionParams'] = $conditionClass->userFriendlyConditionParams();
     }
     return $ruleConditions;
 }
 /**
  * Get the condition class for this condition
  *
  * @param $conditionId
  * @param bool $abort if true this function will throw an exception if class could not be instanciated
  * @return CRM_Civirules_Condition
  * @throws Exception if abort is set to true and class does not exist or is not valid
  */
 public static function getConditionObjectById($conditionId, $abort = true)
 {
     $condition = new CRM_Civirules_BAO_Condition();
     $condition->id = $conditionId;
     if (!$condition->find(true)) {
         if ($abort) {
             throw new Exception('CiviRule could not find condition');
         }
         return false;
     }
     $className = $condition->class_name;
     if (!class_exists($className)) {
         if ($abort) {
             throw new Exception('CiviRule condition class "' . $className . '" does not exist');
         }
         return false;
     }
     $object = new $className();
     if (!$object instanceof CRM_Civirules_Condition) {
         if ($abort) {
             throw new Exception('CiviRule condition class "' . $className . '" is not a subclass of CRM_Civirules_Condition');
         }
         return false;
     }
     return $object;
 }
 /**
  * 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);
 }
/**
 * 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_condition_delete($params)
{
    CRM_Civirules_BAO_Condition::deleteWithId($params['id']);
    $returnValues[$params['id']] = array();
    return civicrm_api3_create_success($returnValues, $params, 'CiviRuleAction', 'Create');
}
 /**
  * @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;
 }
 /**
  * Function to build the conditions list
  *
  * @return array $conditionList
  * @access public
  * @static
  */
 public static function buildConditionList()
 {
     $conditionList = array();
     $conditions = CRM_Civirules_BAO_Condition::getValues(array());
     foreach ($conditions as $conditionId => $condition) {
         $conditionList[$conditionId] = $condition['label'];
     }
     return $conditionList;
 }
 /**
  * Method to check condition
  *
  * @param array $ruleCondition
  * @param object CRM_Civirules_TriggerData_TriggerData $triggerData
  * @return bool
  * @access protected
  * @static
  */
 protected static function checkCondition($ruleCondition, CRM_Civirules_TriggerData_TriggerData $triggerData)
 {
     $condition = CRM_Civirules_BAO_Condition::getConditionObjectById($ruleCondition['condition_id'], false);
     if (!$condition) {
         return false;
     }
     $condition->setRuleConditionData($ruleCondition);
     $isValid = $condition->isConditionValid($triggerData);
     return $isValid;
 }
/**
 * CiviRuleCondition.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_condition_get($params)
{
    $returnValues = CRM_Civirules_BAO_Condition::getValues($params);
    return civicrm_api3_create_success($returnValues, $params, 'CiviRuleCondition', 'Get');
}