diff --git a/CRM/CivirulesActions/Contact/Form/Subtype.php b/CRM/CivirulesActions/Contact/Form/Subtype.php new file mode 100644 index 0000000000000000000000000000000000000000..f5dfed47edff7c2e794b9e7e8cd62743a91e2129 --- /dev/null +++ b/CRM/CivirulesActions/Contact/Form/Subtype.php @@ -0,0 +1,123 @@ +<?php +/** + * Class for CiviRules Group Contact Action Form + * + * @author Jaap Jansma (CiviCooP) <jaap.jansma@civicoop.org> + * @license AGPL-3.0 + */ + +class CRM_CivirulesActions_Contact_Form_Subtype extends CRM_CivirulesActions_Form_Form { + + + /** + * Method to get groups + * + * @return array + * @access protected + */ + protected function getSubtypes() { + $subTypes = CRM_Contact_BAO_ContactType::contactTypeInfo(); + $options = array(); + foreach($subTypes as $name => $type) { + if(!empty($type['parent_id'])) { + $options[$name] = $type['parent_label'].' - '.$type['label']; + } + } + return $options; + } + + /** + * Overridden parent method to build the form + * + * @access public + */ + public function buildQuickForm() { + $this->add('hidden', 'rule_action_id'); + + $this->add('select', 'type', ts('Single/Multiple'), array( + 0 => ts('Set one subtype'), + 1 => ts('Set multiple subtypes'), + )); + + $this->add('select', 'subtype', ts('Contact sub type'), array('' => ts('-- please select --')) + $this->getSubtypes()); + + $multiGroup = $this->addElement('advmultiselect', 'subtypes', ts('Contact sub types'), $this->getSubtypes(), array( + 'size' => 5, + 'style' => 'width:250px', + 'class' => 'advmultiselect', + )); + + $multiGroup->setButtonAttributes('add', array('value' => ts('Add >>'))); + $multiGroup->setButtonAttributes('remove', array('value' => ts('<< Remove'))); + + $this->addButtons(array( + array('type' => 'next', 'name' => ts('Save'), 'isDefault' => TRUE,), + array('type' => 'cancel', 'name' => ts('Cancel')))); + } + + public function addRules() { + $this->addFormRule(array('CRM_CivirulesActions_Contact_Form_Subtype', 'validateSubtype')); + } + + /** + * Function to validate value of rule action form + * + * @param array $fields + * @return array|bool + * @access public + * @static + */ + static function validateSubtype($fields) { + $errors = array(); + if ($fields['type'] == 0 && empty($fields['subtype'])) { + $errors['subtype'] = ts('You have to select at least one subtype'); + } elseif ($fields['type'] == 1 && (empty($fields['subtypes']) || count($fields['subtypes']) < 1)) { + $errors['subtypes'] = ts('You have to select at least one subtype'); + } + + if (count($errors)) { + return $errors; + } + return true; + } + + /** + * Overridden parent method to set default values + * + * @return array $defaultValues + * @access public + */ + public function setDefaultValues() { + $defaultValues = parent::setDefaultValues(); + $data = unserialize($this->ruleAction->action_params); + if (!empty($data['sub_type'])) { + $defaultValues['sub_type'] = reset($data['sub_type']); + $defaultValues['sub_types'] = $data['sub_type']; + } + if (!empty($data['sub_type']) && count($data['sub_type']) <= 1) { + $defaultValues['type'] = 0; + } elseif (!empty($data['sub_type'])) { + $defaultValues['type'] = 1; + } + return $defaultValues; + } + + /** + * Overridden parent method to process form data after submitting + * + * @access public + */ + public function postProcess() { + $data['sub_type'] = array(); + if ($this->_submitValues['type'] == 0) { + $data['sub_type'] = array($this->_submitValues['subtype']); + } else { + $data['sub_type'] = $this->_submitValues['subtypes']; + } + + $this->ruleAction->action_params = serialize($data); + $this->ruleAction->save(); + parent::postProcess(); + } + +} \ No newline at end of file diff --git a/CRM/CivirulesActions/Contact/Subtype.mgd.php b/CRM/CivirulesActions/Contact/Subtype.mgd.php new file mode 100644 index 0000000000000000000000000000000000000000..ecb3eb1a435ade865d3c48c4b1592de673c8c699 --- /dev/null +++ b/CRM/CivirulesActions/Contact/Subtype.mgd.php @@ -0,0 +1,17 @@ +<?php + +return array ( + 0 => + array ( + 'name' => 'Civirules:Action.Contact.Subtype', + 'entity' => 'CiviRuleAction', + 'params' => + array ( + 'version' => 3, + 'name' => 'Subtype', + 'label' => 'Set subtype for contact', + 'class_name' => 'CRM_CivirulesActions_Contact_Subtype', + 'is_active' => 1 + ), + ), +); \ No newline at end of file diff --git a/CRM/CivirulesActions/Contact/Subtype.php b/CRM/CivirulesActions/Contact/Subtype.php new file mode 100644 index 0000000000000000000000000000000000000000..6aa2c3ccdfab72bca1c7f4f55cb61c5a63846975 --- /dev/null +++ b/CRM/CivirulesActions/Contact/Subtype.php @@ -0,0 +1,66 @@ +<?php + +class CRM_CivirulesActions_Contact_Subtype extends CRM_Civirules_Action { + + /** + * Method processAction to execute the action + * + * @param CRM_Civirules_EventData_EventData $eventData + * @access public + * + */ + public function processAction(CRM_Civirules_EventData_EventData $eventData) { + $contactId = $eventData->getContactId(); + + $subTypes = CRM_Contact_BAO_Contact::getContactSubType($contactId); + $contactType = CRM_Contact_BAO_Contact::getContactType($contactId); + + $changed = false; + $action_params = $this->getActionParameters(); + foreach($action_params['sub_type'] as $sub_type) { + if (CRM_Contact_BAO_ContactType::isExtendsContactType($sub_type, $contactType)) { + $subTypes[] = $sub_type; + $changed = true; + } + } + if ($changed) { + $params['id'] = $contactId; + $params['contact_id'] = $contactId; + $params['contact_type'] = $contactType; + $params['contact_sub_type'] = $subTypes; + CRM_Contact_BAO_Contact::add($params); + } + } + + /** + * Method to return the url for additional form processing for action + * and return false if none is needed + * + * @param int $ruleActionId + * @return bool + * @access public + */ + public function getExtraDataInputUrl($ruleActionId) { + return CRM_Utils_System::url('civicrm/civirule/form/action/contact/subtype', 'rule_action_id='.$ruleActionId); + } + + /** + * Returns a user friendly text explaining the condition params + * e.g. 'Older than 65' + * + * @return string + * @access public + */ + public function userFriendlyConditionParams() { + $params = $this->getActionParameters(); + $label = ts('Set contact subtype to: '); + $subTypeLabels = array(); + $subTypes = CRM_Contact_BAO_ContactType::contactTypeInfo(); + foreach($params['sub_type'] as $sub_type) { + $subTypeLabels[] = $subTypes[$sub_type]['parent_label'].' - '.$subTypes[$sub_type]['label']; + } + $label .= implode(', ', $subTypeLabels); + return $label; + } + +} \ No newline at end of file diff --git a/templates/CRM/CivirulesActions/Contact/Form/Subtype.tpl b/templates/CRM/CivirulesActions/Contact/Form/Subtype.tpl new file mode 100644 index 0000000000000000000000000000000000000000..b4e20f6ce117ebde35ef914e5291a3e35fe21b21 --- /dev/null +++ b/templates/CRM/CivirulesActions/Contact/Form/Subtype.tpl @@ -0,0 +1,43 @@ +<h3>{$ruleActionHeader}</h3> +<div class="crm-block crm-form-block crm-civirule-rule_action-block-contact_subtype"> + <div class="crm-section"> + <div class="label">{$form.type.label}</div> + <div class="content">{$form.type.html}</div> + <div class="clear"></div> + </div> + <div class="crm-section sub_type-single"> + <div class="label">{$form.subtype.label}</div> + <div class="content">{$form.subtype.html}</div> + <div class="clear"></div> + </div> + <div class="crm-section sub_type-multiple" style="display: none;"> + <div class="label">{$form.subtypes.label}</div> + <div class="content">{$form.subtypes.html}</div> + <div class="clear"></div> + </div> +</div> +<div class="crm-submit-buttons"> + {include file="CRM/common/formButtons.tpl" location="bottom"} +</div> + +{literal} + <script type="text/javascript"> + cj(function() { + cj('select#type').change(triggerTypeChange); + + triggerTypeChange(); + }); + + function triggerTypeChange() { + cj('.sub_type-multiple').css('display', 'none'); + cj('.sub_type-single').css('display', 'none'); + var val = cj('#type').val(); + if (val == 0 ) { + cj('.sub_type-single').css('display', 'block'); + } else { + cj('.sub_type-multiple').css('display', 'block'); + } + } + </script> + +{/literal} \ No newline at end of file diff --git a/xml/Menu/civirules.xml b/xml/Menu/civirules.xml index 50fc5f259f81c9f467344d3339ad756015810189..29c792a62836bfdbe17b6635690183053cea6944 100755 --- a/xml/Menu/civirules.xml +++ b/xml/Menu/civirules.xml @@ -126,6 +126,12 @@ <title>Tag</title> <access_arguments>access CiviCRM</access_arguments> </item> + <item> + <path>civicrm/civirule/form/action/contact/subtype</path> + <page_callback>CRM_CivirulesActions_Contact_Form_Subtype</page_callback> + <title>Set contact subtype</title> + <access_arguments>access CiviCRM</access_arguments> + </item> <item> <path>civicrm/civirule/form/action/activity</path> <page_callback>CRM_CivirulesActions_Activity_Form_Activity</page_callback>