3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2018
35 * This class generates form components for OpenCase Activity.
37 class CRM_Case_Form_Activity_ChangeCaseStatus
{
40 * @param CRM_Core_Form $form
44 public static function preProcess(&$form) {
45 if (!isset($form->_caseId
)) {
46 CRM_Core_Error
::fatal(ts('Case Id not found.'));
49 $form->addElement('checkbox', 'updateLinkedCases', NULL, NULL, array('class' => 'select-row'));
51 $caseID = CRM_Utils_Array
::first($form->_caseId
);
52 $cases = CRM_Case_BAO_Case
::getRelatedCases($caseID);
53 $form->assign('linkedCases', $cases);
57 * Set default values for the form.
59 * For edit/view mode the default values are retrieved from the database.
62 * @param CRM_Core_Form $form
66 public static function setDefaultValues(&$form) {
68 // Retrieve current case status
69 $defaults['case_status_id'] = $form->_defaultCaseStatus
;
75 * @param CRM_Core_Form $form
77 public static function buildQuickForm(&$form) {
78 $form->removeElement('status_id');
79 $form->removeElement('priority_id');
83 $form->_caseStatus
= CRM_Case_PseudoConstant
::caseStatus();
84 $statusNames = CRM_Case_PseudoConstant
::caseStatus('name');
86 // Limit case statuses to allowed types for these case(s)
87 $allCases = civicrm_api3('Case', 'get', array('return' => 'case_type_id', 'id' => array('IN' => (array) $form->_caseId
)));
88 foreach ($allCases['values'] as $case) {
89 $caseTypes[$case['case_type_id']] = $case['case_type_id'];
91 $caseTypes = civicrm_api3('CaseType', 'get', array('id' => array('IN' => $caseTypes)));
92 foreach ($caseTypes['values'] as $ct) {
93 if (!empty($ct['definition']['statuses'])) {
94 foreach ($form->_caseStatus
as $id => $label) {
95 if (!in_array($statusNames[$id], $ct['definition']['statuses'])) {
96 unset($form->_caseStatus
[$id]);
102 foreach ($form->_caseId
as $key => $val) {
103 $form->_oldCaseStatus
[] = $form->_defaultCaseStatus
[] = CRM_Core_DAO
::getFieldValue('CRM_Case_DAO_Case', $val, 'status_id');
106 foreach ($form->_defaultCaseStatus
as $keydefault => $valdefault) {
107 if (!array_key_exists($valdefault, $form->_caseStatus
)) {
108 $form->_caseStatus
[$valdefault] = CRM_Core_OptionGroup
::getLabel('case_status',
114 $element = $form->add('select', 'case_status_id', ts('Case Status'),
115 $form->_caseStatus
, TRUE
117 // check if the case status id passed in url is a valid one, set as default and freeze
118 if (CRM_Utils_Request
::retrieve('case_status_id', 'Positive', $form)) {
119 $caseStatusId = CRM_Utils_Request
::retrieve('case_status_id', 'Positive', $form);
120 $caseStatus = CRM_Case_PseudoConstant
::caseStatus();
121 $form->_defaultCaseStatus
= array_key_exists($caseStatusId, $caseStatus) ?
$caseStatusId : NULL;
127 * Global validation rules for the form.
129 * @param array $values
130 * Posted values of the form.
133 * @param CRM_Core_Form $form
136 * list of errors to be posted back to the form
138 public static function formRule($values, $files, $form) {
143 * Process the form submission.
145 * @param CRM_Core_Form $form
146 * @param array $params
148 public static function beginPostProcess(&$form, &$params) {
149 $params['id'] = CRM_Utils_Array
::value('case_id', $params);
151 if ($params['updateLinkedCases'] === '1') {
152 $caseID = CRM_Utils_Array
::first($form->_caseId
);
153 $cases = CRM_Case_BAO_Case
::getRelatedCases($caseID);
155 foreach ($cases as $currentCase) {
156 if ($currentCase['status_id'] != $params['case_status_id']) {
157 $form->_caseId
[] = $currentCase['case_id'];
164 * Process the form submission.
166 * @param CRM_Core_Form $form
167 * @param array $params
168 * @param CRM_Activity_BAO_Activity $activity
170 public static function endPostProcess(&$form, &$params, $activity) {
172 $groupingValues = CRM_Core_OptionGroup
::values('case_status', FALSE, TRUE, FALSE, NULL, 'value');
174 // Set case end_date if we're closing the case. Clear end_date if we're (re)opening it.
175 if (CRM_Utils_Array
::value($params['case_status_id'], $groupingValues) == 'Closed' && !empty($params['activity_date_time'])) {
176 $params['end_date'] = $params['activity_date_time'];
178 // End case-specific relationships (roles)
179 foreach ($params['target_contact_id'] as $cid) {
180 $rels = CRM_Case_BAO_Case
::getCaseRoles($cid, $params['case_id']);
181 // FIXME: Is there an existing function to close a relationship?
182 $query = 'UPDATE civicrm_relationship SET end_date=%2 WHERE id=%1';
183 foreach ($rels as $relId => $relData) {
185 1 => array($relId, 'Integer'),
186 2 => array($params['end_date'], 'Timestamp'),
188 CRM_Core_DAO
::executeQuery($query, $relParams);
192 elseif (CRM_Utils_Array
::value($params['case_status_id'], $groupingValues) == 'Opened') {
193 $params['end_date'] = "null";
195 // Reopen case-specific relationships (roles)
196 foreach ($params['target_contact_id'] as $cid) {
197 $rels = CRM_Case_BAO_Case
::getCaseRoles($cid, $params['case_id']);
198 // FIXME: Is there an existing function?
199 $query = 'UPDATE civicrm_relationship SET end_date=NULL WHERE id=%1';
200 foreach ($rels as $relId => $relData) {
201 $relParams = array(1 => array($relId, 'Integer'));
202 CRM_Core_DAO
::executeQuery($query, $relParams);
206 $params['status_id'] = CRM_Core_PseudoConstant
::getKey('CRM_Activity_BAO_Activity', 'activity_status_id', 'Completed');
207 $activity->status_id
= $params['status_id'];
208 $params['priority_id'] = CRM_Core_OptionGroup
::getValue('priority', 'Normal', 'name');
209 $activity->priority_id
= $params['priority_id'];
211 foreach ($form->_oldCaseStatus
as $statuskey => $statusval) {
212 if ($activity->subject
== 'null') {
213 $activity->subject
= ts('Case status changed from %1 to %2', array(
214 1 => CRM_Utils_Array
::value($statusval, $form->_caseStatus
),
215 2 => CRM_Utils_Array
::value($params['case_status_id'], $form->_caseStatus
),