CRM-13966 - Simplify reassign case form with ajax framework and select2
[civicrm-core.git] / CRM / Case / Form / EditClient.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2013
32 * $Id$
33 *
34 */
35
36 /**
37 * This class assigns the current case to another client
38 *
39 */
40 class CRM_Case_Form_EditClient extends CRM_Core_Form {
41
42 /**
43 * build all the data structures needed to build the form
44 *
45 * @return void
46 * @access public
47 */
48 public function preProcess() {
49 $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE);
50 CRM_Utils_Request::retrieve('id', 'Positive', $this, TRUE);
51 $context = CRM_Utils_Request::retrieve('context', 'String', $this);
52
53 //get current client name.
54 $this->assign('currentClientName', CRM_Contact_BAO_Contact::displayName($cid));
55
56 //set the context.
57 $url = CRM_Utils_System::url('civicrm/contact/view', "reset=1&force=1&cid={$cid}&selectedChild=case");
58 if ($context == 'search') {
59 $qfKey = CRM_Utils_Request::retrieve('key', 'String', $this);
60 //validate the qfKey
61 $urlParams = 'force=1';
62 if (CRM_Utils_Rule::qfKey($qfKey)) {
63 $urlParams .= "&qfKey=$qfKey";
64 }
65 $url = CRM_Utils_System::url('civicrm/case/search', $urlParams);
66 }
67 elseif ($context == 'dashboard') {
68 $url = CRM_Utils_System::url('civicrm/case', 'reset=1');
69 }
70 elseif (in_array($context, array(
71 'dashlet', 'dashletFullscreen'))) {
72 $url = CRM_Utils_System::url('civicrm/dashboard', 'reset=1');
73 }
74 $session = CRM_Core_Session::singleton();
75 $session->pushUserContext($url);
76 }
77
78 /**
79 * Function to build the form
80 *
81 * @return void
82 * @access public
83 */
84 public function buildQuickForm() {
85 $this->addEntityRef('reassign_contact_id', ts('Select Contact'), array('create' => TRUE), TRUE);
86 $this->addButtons(array(
87 array(
88 'type' => 'cancel',
89 'name' => ts('Cancel'),
90 ),
91 array(
92 'type' => 'done',
93 'name' => ts('Reassign Case'),
94 ),
95 ));
96
97 // This form may change the url structure so should not submit via ajax
98 $this->preventAjaxSubmit();
99 }
100
101
102 function addRules() {
103 $this->addFormRule(array(get_class($this), 'formRule'), $this);
104 }
105
106 static function formRule($vals, $rule, $form) {
107 $errors = array();
108 if (empty($vals['reassign_contact_id']) || $vals['reassign_contact_id'] == $form->get('cid')) {
109 $errors['reassign_contact_id'] = ts("Please select a different contact.");
110 }
111 return $errors;
112 }
113
114 /**
115 * Process the form
116 *
117 * @return void
118 * @access public
119 */
120 public function postProcess() {
121 $params = $this->controller->exportValues($this->_name);
122
123 //assign case to another client.
124 $mainCaseId = CRM_Case_BAO_Case::mergeCases($params['reassign_contact_id'], $this->get('id'), $this->get('cid'), NULL, TRUE);
125
126 // user context
127 $url = CRM_Utils_System::url('civicrm/contact/view/case',
128 "reset=1&action=view&cid={$params['reassign_contact_id']}&id={$mainCaseId[0]}&show=1"
129 );
130 CRM_Core_Session::singleton()->pushUserContext($url);
131 }
132 }
133