Skip to content
Snippets Groups Projects
Commit 1110735d authored by jaapjansma's avatar jaapjansma
Browse files

added validation of condition

parent 2c14ef5b
No related branches found
No related tags found
No related merge requests found
...@@ -39,6 +39,15 @@ abstract class CRM_Civirules_Condition { ...@@ -39,6 +39,15 @@ abstract class CRM_Civirules_Condition {
return ''; return '';
} }
/**
* Returns an array with required entity names
*
* @return array
*/
public function requiredEntities() {
return array();
}
} }
\ No newline at end of file
...@@ -65,7 +65,7 @@ class CRM_Civirules_Event_EditEntity { ...@@ -65,7 +65,7 @@ class CRM_Civirules_Event_EditEntity {
return array(); return array();
} }
protected static function convertObjectNameToEntity($objectName) { public static function convertObjectNameToEntity($objectName) {
$entity = $objectName; $entity = $objectName;
switch($objectName) { switch($objectName) {
case 'Individual': case 'Individual':
......
...@@ -113,6 +113,39 @@ class CRM_Civirules_Form_RuleCondition extends CRM_Core_Form { ...@@ -113,6 +113,39 @@ class CRM_Civirules_Form_RuleCondition extends CRM_Core_Form {
*/ */
public function addRules() { public function addRules() {
$this->addFormRule(array('CRM_Civirules_Form_RuleCondition', 'validateRuleCondition')); $this->addFormRule(array('CRM_Civirules_Form_RuleCondition', 'validateRuleCondition'));
$this->addFormRule(array('CRM_Civirules_Form_RuleCondition', 'validateConditionEntities'));
}
/**
* @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);
$eventEntities = array('contact');
$eventEntities[] = $event->object_name;
if (CRM_Civirules_Event_EditEntity::convertObjectNameToEntity($event->object_name) != $event->object_name) {
$eventEntities[] = CRM_Civirules_Event_EditEntity::convertObjectNameToEntity($event->object_name);
}
foreach($requiredEntities as $entity) {
if (!in_array(strtolower($entity), $eventEntities)) {
$errors['rule_condition_select'] = ts('This condition is not available with event %1', array(1 => $event->label));
return $errors;
}
}
return true;
} }
/** /**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment