Merge pull request #18474 from civicrm/5.30
[civicrm-core.git] / CRM / Case / Form / EditClient.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * This class assigns the current case to another client.
20 */
21 class CRM_Case_Form_EditClient extends CRM_Core_Form {
22
23 /**
24 * Build all the data structures needed to build the form.
25 */
26 public function preProcess() {
27 $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE);
28 CRM_Utils_Request::retrieve('id', 'Positive', $this, TRUE);
29 $context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this);
30
31 //get current client name.
32 $this->assign('currentClientName', CRM_Contact_BAO_Contact::displayName($cid));
33
34 //set the context.
35 $url = CRM_Utils_System::url('civicrm/contact/view', "reset=1&force=1&cid={$cid}&selectedChild=case");
36 if ($context == 'search') {
37 $qfKey = CRM_Utils_Request::retrieve('key', 'String', $this);
38 //validate the qfKey
39 $urlParams = 'force=1';
40 if (CRM_Utils_Rule::qfKey($qfKey)) {
41 $urlParams .= "&qfKey=$qfKey";
42 }
43 $url = CRM_Utils_System::url('civicrm/case/search', $urlParams);
44 }
45 elseif ($context == 'dashboard') {
46 $url = CRM_Utils_System::url('civicrm/case', 'reset=1');
47 }
48 elseif (in_array($context, [
49 'dashlet',
50 'dashletFullscreen',
51 ])) {
52 $url = CRM_Utils_System::url('civicrm/dashboard', 'reset=1');
53 }
54 $session = CRM_Core_Session::singleton();
55 $session->pushUserContext($url);
56 }
57
58 /**
59 * Build the form object.
60 */
61 public function buildQuickForm() {
62 $this->addEntityRef('reassign_contact_id', ts('Select Contact'), ['create' => TRUE], TRUE);
63 $this->addButtons([
64 [
65 'type' => 'done',
66 'name' => ts('Reassign Case'),
67 ],
68 [
69 'type' => 'cancel',
70 'name' => ts('Cancel'),
71 ],
72 ]);
73
74 // This form may change the url structure so should not submit via ajax
75 $this->preventAjaxSubmit();
76 }
77
78 public function addRules() {
79 $this->addFormRule([get_class($this), 'formRule'], $this);
80 }
81
82 /**
83 * @param $vals
84 * @param $rule
85 * @param CRM_Core_Form $form
86 *
87 * @return array
88 */
89 public static function formRule($vals, $rule, $form) {
90 $errors = [];
91 if (empty($vals['reassign_contact_id']) || $vals['reassign_contact_id'] == $form->get('cid')) {
92 $errors['reassign_contact_id'] = ts("Please select a different contact.");
93 }
94 return $errors;
95 }
96
97 /**
98 * Process the form.
99 */
100 public function postProcess() {
101 $params = $this->controller->exportValues($this->_name);
102
103 //assign case to another client.
104 $mainCaseId = CRM_Case_BAO_Case::mergeCases($params['reassign_contact_id'], $this->get('id'), $this->get('cid'), NULL, TRUE);
105
106 // user context
107 $url = CRM_Utils_System::url('civicrm/contact/view/case',
108 "reset=1&action=view&cid={$params['reassign_contact_id']}&id={$mainCaseId[0]}&show=1"
109 );
110 CRM_Core_Session::singleton()->pushUserContext($url);
111 }
112
113 }