From f1d7f28771898ee57df458495bedda54a3017780 Mon Sep 17 00:00:00 2001 From: Camilo Rodriguez Date: Tue, 5 Dec 2017 12:37:14 +0000 Subject: [PATCH] CRM-21520: Implement Search Action to Add Contacts to Case --- CRM/Case/Form/AddContact.php | 31 +++++++++++++++ CRM/Case/Form/AddToCaseAsRole.php | 33 ++++++++++++++++ CRM/Case/FormBuilder.php | 43 +++++++++++++++++++++ CRM/Case/xml/Menu/Case.xml | 7 ++++ CRM/Contact/BAO/Contact.php | 8 ++++ CRM/Contact/Task.php | 6 +++ templates/CRM/Case/Form/AddContact.tpl | 1 + templates/CRM/Case/Form/AddToCaseAsRole.tpl | 19 +++++++++ 8 files changed, 148 insertions(+) create mode 100644 CRM/Case/Form/AddContact.php create mode 100644 CRM/Case/Form/AddToCaseAsRole.php create mode 100644 CRM/Case/FormBuilder.php create mode 100644 templates/CRM/Case/Form/AddContact.tpl create mode 100644 templates/CRM/Case/Form/AddToCaseAsRole.tpl diff --git a/CRM/Case/Form/AddContact.php b/CRM/Case/Form/AddContact.php new file mode 100644 index 0000000000..810767786d --- /dev/null +++ b/CRM/Case/Form/AddContact.php @@ -0,0 +1,31 @@ +build(); + } + + public function postProcess() { + $values = $this->controller->exportValues(); + + $caseId = (int)$values['assign_to']; + $roleTypeId = (int)$values['role_type']; + $contacts = array((int)CRM_Utils_Request::retrieve('cid', 'Positive')); + + $clients = CRM_Case_BAO_Case::getCaseClients($caseId); + + $params = array( + 'contact_id_a' => $clients[0], + 'contact_id_b' => $contacts, + 'case_id' => $caseId, + 'relationship_type_id' => $roleTypeId + ); + + CRM_Contact_BAO_Relationship::createMultiple($params, 'a'); + + CRM_Core_Session::setStatus(ts('Contact has been added to case.'), 'Information', 'success'); + } +} diff --git a/CRM/Case/Form/AddToCaseAsRole.php b/CRM/Case/Form/AddToCaseAsRole.php new file mode 100644 index 0000000000..5085d66761 --- /dev/null +++ b/CRM/Case/Form/AddToCaseAsRole.php @@ -0,0 +1,33 @@ +build(); + } + + public function postProcess() { + $values = $this->controller->exportValues(); + + $caseId = (int)$values['assign_to']; + $roleTypeId = (int)$values['role_type']; + $contacts = $this->_contactIds; + + $clients = CRM_Case_BAO_Case::getCaseClients($caseId); + + $params = array( + 'contact_id_a' => $clients[0], + 'contact_id_b' => $contacts, + 'case_id' => $caseId, + 'relationship_type_id' => $roleTypeId + ); + + CRM_Contact_BAO_Relationship::createMultiple($params, 'a'); + + $url = CRM_Utils_System::url( + 'civicrm/contact/view/case', + sprintf('cid=%d&id=%d', $clients[0], $caseId) + ); + CRM_Utils_System::redirect($url); + } +} diff --git a/CRM/Case/FormBuilder.php b/CRM/Case/FormBuilder.php new file mode 100644 index 0000000000..b62707ee74 --- /dev/null +++ b/CRM/Case/FormBuilder.php @@ -0,0 +1,43 @@ +form = $form; + } + + public function build() { + $this->form->add('text', 'assign_to', ts('Assign to')); + $roleTypes = $this->getRoleTypes(); + + $this->form->add( + 'select', + 'role_type', + ts('Relationship Type'), + array('' => ts('- select type -')) + $roleTypes, + FALSE, + array('class' => 'crm-select2 twenty') + ); + + $this->form->addButtons(array( + array( + 'type' => 'submit', + 'name' => ts('Submit'), + 'isDefault' => true + ) + )); + } + + /** + * @return array + */ + private function getRoleTypes() { + $relType = CRM_Contact_BAO_Relationship::getRelationType('Individual'); + $roleTypes = array(); + foreach ($relType as $k => $v) { + $roleTypes[substr($k, 0, strpos($k, '_'))] = $v; + } + return $roleTypes; + } +} diff --git a/CRM/Case/xml/Menu/Case.xml b/CRM/Case/xml/Menu/Case.xml index 386cdc131d..976c04aeb6 100644 --- a/CRM/Case/xml/Menu/Case.xml +++ b/CRM/Case/xml/Menu/Case.xml @@ -136,4 +136,11 @@ civicrm/ajax/delcaserole CRM_Case_Page_AJAX::deleteCaseRoles + + civicrm/case/add-contact + CRM_Case_Form_AddContact + AddContact + access CiviCRM + 0 + diff --git a/CRM/Contact/BAO/Contact.php b/CRM/Contact/BAO/Contact.php index 39de40240b..be48686335 100644 --- a/CRM/Contact/BAO/Contact.php +++ b/CRM/Contact/BAO/Contact.php @@ -3105,6 +3105,14 @@ AND civicrm_openid.is_primary = 1"; ); } + $menu['add-to-case-as-role'] = array( + 'title' => 'Add to case as role', + 'href' => CRM_Utils_System::url('civicrm/case/add-contact', 'reset=1'), + 'weight' => 100, + 'ref' => 'add-to-case-as-role', + 'key' => 'add-to-case-as-role' + ); + CRM_Utils_Hook::summaryActions($menu, $contactId); //1. check for component is active. //2. check for user permissions. diff --git a/CRM/Contact/Task.php b/CRM/Contact/Task.php index 99f3a790a5..abe2c39d6d 100644 --- a/CRM/Contact/Task.php +++ b/CRM/Contact/Task.php @@ -275,6 +275,12 @@ class CRM_Contact_Task { ); } + self::$_tasks[] = array( + 'title' => 'Add to case as role', + 'class' => 'CRM_Case_Form_AddToCaseAsRole', + 'result' => FALSE + ); + self::$_tasks += CRM_Core_Component::taskList(); CRM_Utils_Hook::searchTasks('contact', self::$_tasks); diff --git a/templates/CRM/Case/Form/AddContact.tpl b/templates/CRM/Case/Form/AddContact.tpl new file mode 100644 index 0000000000..39d32412ef --- /dev/null +++ b/templates/CRM/Case/Form/AddContact.tpl @@ -0,0 +1 @@ +{include file="CRM/Caseroles/Form/AddToCaseAsRole.tpl"} diff --git a/templates/CRM/Case/Form/AddToCaseAsRole.tpl b/templates/CRM/Case/Form/AddToCaseAsRole.tpl new file mode 100644 index 0000000000..88e5d0a2dc --- /dev/null +++ b/templates/CRM/Case/Form/AddToCaseAsRole.tpl @@ -0,0 +1,19 @@ +
+
+ +
{$form.role_type.label}
+
{$form.role_type.html}

+ +
{include file="CRM/common/formButtons.tpl" location="bottom"}
+ +{literal} + +{/literal} -- 2.25.1