/**
  * 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->trigger = new CRM_Civirules_BAO_Trigger();
     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->trigger->id = $this->rule->trigger_id;
     if (!$this->trigger->find(true)) {
         throw new Exception('Civirules could not find trigger');
     }
     $this->triggerClass = CRM_Civirules_BAO_Trigger::getPostTriggerObjectByClassName($this->trigger->class_name, true);
     $this->triggerClass->setTriggerId($this->trigger->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();
 }
 /**
  * Returns an array with rules which should be triggered immediately
  *
  * @param string $objectName ObjectName in the Post hook
  * @param string $op op in the Post hook
  * @return array
  */
 public static function findRulesByObjectNameAndOp($objectName, $op)
 {
     $triggers = array();
     $sql = "SELECT r.id AS rule_id, t.id AS trigger_id, t.class_name, r.trigger_params\n            FROM `civirule_rule` r\n            INNER JOIN `civirule_trigger` t ON r.trigger_id = t.id AND t.is_active = 1\n            WHERE r.`is_active` = 1 AND t.cron = 0 AND t.object_name = %1 AND t.op = %2";
     $params[1] = array($objectName, 'String');
     $params[2] = array($op, 'String');
     $dao = CRM_Core_DAO::executeQuery($sql, $params);
     while ($dao->fetch()) {
         $triggerObject = CRM_Civirules_BAO_Trigger::getPostTriggerObjectByClassName($dao->class_name, false);
         if ($triggerObject !== false) {
             $triggerObject->setTriggerId($dao->trigger_id);
             $triggerObject->setRuleId($dao->rule_id);
             $triggerObject->setTriggerParams($dao->trigger_params);
             $triggers[] = $triggerObject;
         }
     }
     return $triggers;
 }
 /**
  * @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);
     $trigger = new CRM_Civirules_BAO_Trigger();
     $trigger->id = $rule->trigger_id;
     $trigger->find(true);
     $triggerObject = CRM_Civirules_BAO_Trigger::getPostTriggerObjectByClassName($trigger->class_name, true);
     $triggerObject->setTriggerId($trigger->id);
     $availableEntities = array();
     foreach ($triggerObject->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 trigger %1', array(1 => $trigger->label));
             return $errors;
         }
     }
     return true;
 }