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