/**
  * 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;
     $this->condition = new CRM_Civirules_BAO_Condition();
     $this->rule = new CRM_Civirules_BAO_Rule();
     $this->event = new CRM_Civirules_BAO_Event();
     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->event->id = $this->rule->event_id;
     if (!$this->event->find(true)) {
         throw new Exception('Civirules could not find event');
     }
     $this->eventClass = CRM_Civirules_BAO_Event::getPostEventObjectByClassName($this->event->class_name, true);
     $this->eventClass->setEventId($this->event->id);
     parent::preProcess();
 }
 /**
  * Overridden parent method to perform processing before form is build
  *
  * @access public
  */
 public function preProcess()
 {
     $this->ruleActionId = CRM_Utils_Request::retrieve('rule_action_id', 'Integer');
     $this->ruleAction = new CRM_Civirules_BAO_RuleAction();
     $this->ruleAction->id = $this->ruleActionId;
     $this->action = new CRM_Civirules_BAO_Action();
     $this->rule = new CRM_Civirules_BAO_Rule();
     $this->event = new CRM_Civirules_BAO_Event();
     if (!$this->ruleAction->find(true)) {
         throw new Exception('Civirules could not find ruleAction');
     }
     $this->action->id = $this->ruleAction->action_id;
     if (!$this->action->find(true)) {
         throw new Exception('Civirules could not find action');
     }
     $this->rule->id = $this->ruleAction->rule_id;
     if (!$this->rule->find(true)) {
         throw new Exception('Civirules could not find rule');
     }
     $this->event->id = $this->rule->event_id;
     if (!$this->event->find(true)) {
         throw new Exception('Civirules could not find event');
     }
     $this->eventClass = CRM_Civirules_BAO_Event::getPostEventObjectByClassName($this->event->class_name, true);
     $this->eventClass->setEventId($this->event->id);
     //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);
     parent::preProcess();
     $this->setFormTitle();
 }
 /**
  * @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;
 }
 /**
  * Returns an array with rules which should be triggered imeditaly
  *
  * @param $objectName ObjectName in the Post hook
  * @param $op op in the Post hook
  * @return array
  */
 public static function findRulesByObjectNameAndOp($objectName, $op)
 {
     $events = array();
     $sql = "SELECT r.id AS rule_id, e.id AS event_id, e.class_name, r.event_params\n            FROM `civirule_rule` r\n            INNER JOIN `civirule_event` e ON r.event_id = e.id AND e.is_active = 1\n            WHERE r.`is_active` = 1 AND e.cron = 0 AND e.object_name = %1 AND e.op = %2";
     $params[1] = array($objectName, 'String');
     $params[2] = array($op, 'String');
     $dao = CRM_Core_DAO::executeQuery($sql, $params);
     while ($dao->fetch()) {
         $eventObject = CRM_Civirules_BAO_Event::getPostEventObjectByClassName($dao->class_name, false);
         if ($eventObject !== false) {
             $eventObject->setEventId($dao->event_id);
             $eventObject->setRuleId($dao->rule_id);
             $eventObject->setEventParams($dao->event_params);
             $events[] = $eventObject;
         }
     }
     return $events;
 }