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