From f25af7290c31119ee72ac37c5a1629f8b32550d5 Mon Sep 17 00:00:00 2001 From: Jaap Jansma <jaap@edeveloper.nl> Date: Fri, 13 Mar 2015 21:55:23 +0100 Subject: [PATCH] add condition for group id --- CRM/CivirulesActions/Form/GroupContact.php | 1 - .../FieldValueComparison.mgd.php | 17 ++++ CRM/CivirulesConditions/Form/Form.php | 95 +++++++++++++++++++ .../Form/GroupContact/GroupId.php | 64 +++++++++++++ .../GroupContact/GroupId.mgd.php | 17 ++++ .../GroupContact/GroupId.php | 68 +++++++++++++ .../Form/GroupContact/GroupId.tpl | 11 +++ xml/Menu/civirules.xml | 6 ++ 8 files changed, 278 insertions(+), 1 deletion(-) create mode 100644 CRM/CivirulesConditions/FieldValueComparison.mgd.php create mode 100644 CRM/CivirulesConditions/Form/Form.php create mode 100644 CRM/CivirulesConditions/Form/GroupContact/GroupId.php create mode 100644 CRM/CivirulesConditions/GroupContact/GroupId.mgd.php create mode 100644 CRM/CivirulesConditions/GroupContact/GroupId.php create mode 100644 templates/CRM/CivirulesConditions/Form/GroupContact/GroupId.tpl diff --git a/CRM/CivirulesActions/Form/GroupContact.php b/CRM/CivirulesActions/Form/GroupContact.php index d5bdae6..8c22569 100644 --- a/CRM/CivirulesActions/Form/GroupContact.php +++ b/CRM/CivirulesActions/Form/GroupContact.php @@ -44,7 +44,6 @@ class CRM_CivirulesActions_Form_GroupContact extends CRM_Core_Form { * @return array * @access protected */ - protected function getGroups() { return array('' => ts('-- please select --')) + CRM_Contact_BAO_GroupContact::getGroupList(); } diff --git a/CRM/CivirulesConditions/FieldValueComparison.mgd.php b/CRM/CivirulesConditions/FieldValueComparison.mgd.php new file mode 100644 index 0000000..a84392d --- /dev/null +++ b/CRM/CivirulesConditions/FieldValueComparison.mgd.php @@ -0,0 +1,17 @@ +<?php + +return array ( + 0 => + array ( + 'name' => 'Civirules:Condition.FieldValueComparison', + 'entity' => 'CiviRuleCondition', + 'params' => + array ( + 'version' => 3, + 'name' => 'field_value_comparison', + 'label' => 'Field value comparison', + 'class_name' => 'CRM_CivirulesConditions_FieldValueComparison', + 'is_active' => 1 + ), + ), +); \ No newline at end of file diff --git a/CRM/CivirulesConditions/Form/Form.php b/CRM/CivirulesConditions/Form/Form.php new file mode 100644 index 0000000..2bb50da --- /dev/null +++ b/CRM/CivirulesConditions/Form/Form.php @@ -0,0 +1,95 @@ +<?php + +class CRM_CivirulesConditions_Form_Form extends CRM_Core_Form +{ + + protected $ruleConditionId = false; + + protected $ruleCondition; + + protected $condition; + + protected $rule; + + protected $event; + + /** + * @var CRM_Civirules_Event + */ + protected $eventClass; + + /** + * 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(); + + $this->setFormTitle(); + } + + /** + * Overridden parent method to set default values + * + * @return array $defaultValues + * @access public + */ + public function setDefaultValues() { + $defaultValues = array(); + $defaultValues['rule_condition_id'] = $this->ruleConditionId; + return $defaultValues; + } + + public function postProcess() { + $session = CRM_Core_Session::singleton(); + $session->setStatus('Condition '.$this->condition->label.' parameters updated to CiviRule '.$this->rule->label, 'Condition parameters updated', 'success'); + + $redirectUrl = CRM_Utils_System::url('civicrm/civirule/form/rule', 'action=update&id='.$this->ruleId, TRUE); + CRM_Utils_System::redirect($redirectUrl); + } + + /** + * Method to set the form title + * + * @access protected + */ + protected function setFormTitle() { + $title = 'CiviRules Edit Condition parameters'; + $this->assign('ruleConditionHeader', 'Edit Condition '.$this->condition->label.' of CiviRule '.$this->rule->label); + CRM_Utils_System::setTitle($title); + } + +} \ No newline at end of file diff --git a/CRM/CivirulesConditions/Form/GroupContact/GroupId.php b/CRM/CivirulesConditions/Form/GroupContact/GroupId.php new file mode 100644 index 0000000..7f76966 --- /dev/null +++ b/CRM/CivirulesConditions/Form/GroupContact/GroupId.php @@ -0,0 +1,64 @@ +<?php +/** + * Class for CiviRules ValueComparison Form + * + * @author Jaap Jansma (CiviCooP) <jaap.jansma@civicoop.org> + * @license AGPL-3.0 + */ + +class CRM_CivirulesConditions_Form_GroupContact_GroupId extends CRM_CivirulesConditions_Form_Form { + + /** + * Method to get groups + * + * @return array + * @access protected + */ + protected function getGroups() { + return array('' => ts('-- please select --')) + CRM_Contact_BAO_GroupContact::getGroupList(); + } + + /** + * Overridden parent method to build form + * + * @access public + */ + public function buildQuickForm() { + $this->add('hidden', 'rule_condition_id'); + + $this->add('select', 'group_id', ts('Group'), $this->getGroups(), true); + + $this->addButtons(array( + array('type' => 'next', 'name' => ts('Save'), 'isDefault' => TRUE,), + array('type' => 'cancel', 'name' => ts('Cancel')))); + } + + /** + * Overridden parent method to set default values + * + * @return array $defaultValues + * @access public + */ + public function setDefaultValues() { + $defaultValues = parent::setDefaultValues(); + $data = unserialize($this->ruleCondition->condition_params); + if (!empty($data['group_id'])) { + $defaultValues['group_id'] = $data['group_id']; + } + return $defaultValues; + } + + /** + * Overridden parent method to process form data after submission + * + * @throws Exception when rule condition not found + * @access public + */ + public function postProcess() { + $data['group_id'] = $this->_submitValues['group_id']; + $this->ruleCondition->condition_params = serialize($data); + $this->ruleCondition->save(); + + parent::postProcess(); + } +} \ No newline at end of file diff --git a/CRM/CivirulesConditions/GroupContact/GroupId.mgd.php b/CRM/CivirulesConditions/GroupContact/GroupId.mgd.php new file mode 100644 index 0000000..38a99e5 --- /dev/null +++ b/CRM/CivirulesConditions/GroupContact/GroupId.mgd.php @@ -0,0 +1,17 @@ +<?php + +return array ( + 0 => + array ( + 'name' => 'Civirules:Condition.GroupContact.GroupId', + 'entity' => 'CiviRuleCondition', + 'params' => + array ( + 'version' => 3, + 'name' => 'groupcontact_group_id', + 'label' => 'Group is', + 'class_name' => 'CRM_CivirulesConditions_GroupContact_GroupId', + 'is_active' => 1 + ), + ), +); \ No newline at end of file diff --git a/CRM/CivirulesConditions/GroupContact/GroupId.php b/CRM/CivirulesConditions/GroupContact/GroupId.php new file mode 100644 index 0000000..256cbca --- /dev/null +++ b/CRM/CivirulesConditions/GroupContact/GroupId.php @@ -0,0 +1,68 @@ +<?php + +class CRM_CivirulesConditions_GroupContact_GroupId extends CRM_Civirules_Condition { + + private $conditionParams = array(); + + /** + * Method to set the Rule Condition data + * + * @param array $ruleCondition + * @access public + */ + public function setRuleConditionData($ruleCondition) { + parent::setRuleConditionData($ruleCondition); + $this->conditionParams = array(); + if (!empty($this->ruleCondition['condition_params'])) { + $this->conditionParams = unserialize($this->ruleCondition['condition_params']); + } + } + + public function isConditionValid(CRM_Civirules_EventData_EventData $eventData) { + $groupContact = $eventData->getEntityData('GroupContact'); + if ($groupContact['group_id'] == $this->conditionParams['group_id']) { + return true; + } + return false; + } + + /** + * Returns a redirect url to extra data input from the user after adding a condition + * + * Return false if you do not need extra data input + * + * @param int $ruleConditionId + * @return bool|string + * @access public + * @abstract + */ + public function getExtraDataInputUrl($ruleConditionId) { + return CRM_Utils_System::url('civicrm/civirule/form/condition/groupcontact/groupid/', 'rule_condition_id='.$ruleConditionId); + } + + /** + * Returns a user friendly text explaining the condition params + * e.g. 'Older than 65' + * + * @return string + * @access public + */ + public function userFriendlyConditionParams() { + if (!empty($this->conditionParams['group_id'])) { + $group = civicrm_api3('Group', 'getvalue', array('return' => 'title', 'id' => $this->conditionParams['group_id'])); + return ts('Group is %1', array(1 => $group)); + } + return ''; + } + + /** + * Returns an array with required entity names + * + * @return array + * @access public + */ + public function requiredEntities() { + return array('GroupContact'); + } + +} \ No newline at end of file diff --git a/templates/CRM/CivirulesConditions/Form/GroupContact/GroupId.tpl b/templates/CRM/CivirulesConditions/Form/GroupContact/GroupId.tpl new file mode 100644 index 0000000..6f665d5 --- /dev/null +++ b/templates/CRM/CivirulesConditions/Form/GroupContact/GroupId.tpl @@ -0,0 +1,11 @@ +<h3>{$ruleConditionHeader}</h3> +<div class="crm-block crm-form-block crm-civirule-rule_condition-block-group_id"> + <div class="crm-section"> + <div class="label">{$form.group_id.label}</div> + <div class="content">{$form.group_id.html}</div> + <div class="clear"></div> + </div> +</div> +<div class="crm-submit-buttons"> + {include file="CRM/common/formButtons.tpl" location="bottom"} +</div> \ No newline at end of file diff --git a/xml/Menu/civirules.xml b/xml/Menu/civirules.xml index b362282..b92fbcb 100755 --- a/xml/Menu/civirules.xml +++ b/xml/Menu/civirules.xml @@ -36,6 +36,12 @@ <title>Value comparison</title> <access_arguments>access CiviCRM</access_arguments> </item> + <item> + <path>civicrm/civirule/form/condition/groupcontact/groupid</path> + <page_callback>CRM_CivirulesConditions_Form_GroupContact_GroupId</page_callback> + <title>Group Id</title> + <access_arguments>access CiviCRM</access_arguments> + </item> <item> <path>civicrm/civirule/form/condition/contributionstatus</path> <page_callback>CRM_CivirulesConditions_Form_ContributionStatus</page_callback> -- GitLab