Merge pull request #4896 from totten/master-movedep
[civicrm-core.git] / CRM / Case / Form / Activity / ChangeCaseStatus.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * This class generates form components for OpenCase Activity
38 *
39 */
40 class CRM_Case_Form_Activity_ChangeCaseStatus {
41
42 /**
43 * @param CRM_Core_Form $form
44 *
45 * @throws Exception
46 */
47 public static function preProcess(&$form) {
48 if (!isset($form->_caseId)) {
49 CRM_Core_Error::fatal(ts('Case Id not found.'));
50 }
51 }
52
53 /**
54 * Set default values for the form. For edit/view mode
55 * the default values are retrieved from the database
56 *
57 *
58 * @param CRM_Core_Form $form
59 *
60 * @return void
61 */
62 public static function setDefaultValues(&$form) {
63 $defaults = array();
64 // Retrieve current case status
65 $defaults['case_status_id'] = $form->_defaultCaseStatus;
66
67 return $defaults;
68 }
69
70 /**
71 * @param CRM_Core_Form $form
72 */
73 public static function buildQuickForm(&$form) {
74 $form->removeElement('status_id');
75 $form->removeElement('priority_id');
76
77 $form->_caseStatus = CRM_Case_PseudoConstant::caseStatus();
78
79 foreach ($form->_caseId as $key => $val) {
80 $form->_oldCaseStatus[] = $form->_defaultCaseStatus[] = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_Case', $val, 'status_id');
81 }
82
83 foreach ($form->_defaultCaseStatus as $keydefault => $valdefault) {
84 if (!array_key_exists($valdefault, $form->_caseStatus)) {
85 $form->_caseStatus[$valdefault] = CRM_Core_OptionGroup::getLabel('case_status',
86 $valdefault,
87 FALSE
88 );
89 }
90 }
91 $element = $form->add('select', 'case_status_id', ts('Case Status'),
92 $form->_caseStatus, TRUE
93 );
94 // check if the case status id passed in url is a valid one, set as default and freeze
95 if (CRM_Utils_Request::retrieve('case_status_id', 'Positive', $form)) {
96 $caseStatusId = CRM_Utils_Request::retrieve('case_status_id', 'Positive', $form);
97 $caseStatus = CRM_Case_PseudoConstant::caseStatus();
98 $form->_defaultCaseStatus = array_key_exists($caseStatusId, $caseStatus) ? $caseStatusId : NULL;
99 $element->freeze();
100 }
101 }
102
103 /**
104 * Global validation rules for the form
105 *
106 * @param array $values
107 * Posted values of the form.
108 *
109 * @param $files
110 * @param CRM_Core_Form $form
111 *
112 * @return array
113 * list of errors to be posted back to the form
114 * @static
115 */
116 public static function formRule($values, $files, $form) {
117 return TRUE;
118 }
119
120 /**
121 * Process the form submission
122 *
123 *
124 * @param CRM_Core_Form $form
125 * @param array $params
126 *
127 * @return void
128 */
129 public static function beginPostProcess(&$form, &$params) {
130 $params['id'] = CRM_Utils_Array::value('case_id', $params);
131 }
132
133 /**
134 * Process the form submission
135 *
136 *
137 * @param CRM_Core_Form $form
138 * @param array $params
139 * @param $activity
140 *
141 * @return void
142 */
143 public static function endPostProcess(&$form, &$params, $activity) {
144 $groupingValues = CRM_Core_OptionGroup::values('case_status', FALSE, TRUE, FALSE, NULL, 'value');
145
146 // Set case end_date if we're closing the case. Clear end_date if we're (re)opening it.
147 if (CRM_Utils_Array::value($params['case_status_id'], $groupingValues) == 'Closed' && !empty($params['activity_date_time'])) {
148 $params['end_date'] = $params['activity_date_time'];
149
150 // End case-specific relationships (roles)
151 foreach ($params['target_contact_id'] as $cid) {
152 $rels = CRM_Case_BAO_Case::getCaseRoles($cid, $params['case_id']);
153 // FIXME: Is there an existing function to close a relationship?
154 $query = 'UPDATE civicrm_relationship SET end_date=%2 WHERE id=%1';
155 foreach ($rels as $relId => $relData) {
156 $relParams = array(
157 1 => array($relId, 'Integer'),
158 2 => array($params['end_date'], 'Timestamp'),
159 );
160 CRM_Core_DAO::executeQuery($query, $relParams);
161 }
162 }
163 }
164 elseif (CRM_Utils_Array::value($params['case_status_id'], $groupingValues) == 'Opened') {
165 $params['end_date'] = "null";
166
167 // Reopen case-specific relationships (roles)
168 foreach ($params['target_contact_id'] as $cid) {
169 $rels = CRM_Case_BAO_Case::getCaseRoles($cid, $params['case_id']);
170 // FIXME: Is there an existing function?
171 $query = 'UPDATE civicrm_relationship SET end_date=NULL WHERE id=%1';
172 foreach ($rels as $relId => $relData) {
173 $relParams = array(1 => array($relId, 'Integer'));
174 CRM_Core_DAO::executeQuery($query, $relParams);
175 }
176 }
177 }
178 $params['status_id'] = CRM_Core_OptionGroup::getValue('activity_status', 'Completed', 'name');
179 $activity->status_id = $params['status_id'];
180 $params['priority_id'] = CRM_Core_OptionGroup::getValue('priority', 'Normal', 'name');
181 $activity->priority_id = $params['priority_id'];
182
183 foreach ($form->_oldCaseStatus as $statuskey => $statusval) {
184 if ($activity->subject == 'null') {
185 $activity->subject = ts('Case status changed from %1 to %2', array(
186 1 => CRM_Utils_Array::value($statusval, $form->_caseStatus),
187 2 => CRM_Utils_Array::value($params['case_status_id'], $form->_caseStatus),
188 )
189 );
190 $activity->save();
191 }
192 }
193
194 // FIXME: does this do anything ?
195 $params['statusMsg'] = ts('Case Status changed successfully.');
196 }
197 }