Merge pull request #15808 from civicrm/5.20
[civicrm-core.git] / CRM / Case / Form / Activity / ChangeCaseType.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
f299f7db 6 | Copyright CiviCRM LLC (c) 2004-2020 |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
f299f7db 31 * @copyright CiviCRM LLC (c) 2004-2020
6a488035
TO
32 */
33
34/**
3819f101 35 * This class generates form components for OpenCase Activity.
6a488035
TO
36 */
37class CRM_Case_Form_Activity_ChangeCaseType {
38
4c6ce474 39 /**
c490a46a 40 * @param CRM_Core_Form $form
4c6ce474
EM
41 *
42 * @throws Exception
43 */
00be9182 44 public static function preProcess(&$form) {
6a488035
TO
45 if (!isset($form->_caseId)) {
46 CRM_Core_Error::fatal(ts('Case Id not found.'));
47 }
48 }
49
50 /**
3819f101 51 * Set default values for the form.
6a488035 52 *
3819f101 53 * For edit/view mode the default values are retrieved from the database.
6a488035 54 *
c490a46a 55 * @param CRM_Core_Form $form
77b97be7 56 *
3819f101 57 * @return array
6a488035 58 */
00be9182 59 public static function setDefaultValues(&$form) {
be2fb01f 60 $defaults = [];
6a488035
TO
61
62 $defaults['is_reset_timeline'] = 1;
63
c70de000 64 $defaults['reset_date_time'] = date('Y-m-d H:i:s');
6a488035
TO
65 $defaults['case_type_id'] = $form->_caseTypeId;
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
c22c1324 77 $caseId = CRM_Utils_Array::first($form->_caseId);
7a5c0c6c 78 $form->_caseType = CRM_Case_BAO_Case::buildOptions('case_type_id', 'create');
8ffdec17 79 $form->_caseTypeId = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_Case',
c22c1324 80 $caseId,
8ffdec17
ARW
81 'case_type_id'
82 );
6a488035 83 if (!in_array($form->_caseTypeId, $form->_caseType)) {
d14b35a4 84 $form->_caseType[$form->_caseTypeId] = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_CaseType', $form->_caseTypeId, 'title');
6a488035
TO
85 }
86
be2fb01f 87 $form->addField('case_type_id', ['context' => 'create', 'entity' => 'Case']);
6a488035
TO
88
89 // timeline
c70de000
CW
90 $form->addYesNo('is_reset_timeline', ts('Reset Case Timeline?'), NULL, TRUE);
91 $form->add('datepicker', 'reset_date_time', ts('Reset Start Date'), NULL, FALSE, ['allowClear' => FALSE]);
6a488035
TO
92 }
93
94 /**
d2e5d2ce 95 * Global validation rules for the form.
6a488035 96 *
64bd5a0e
TO
97 * @param array $values
98 * Posted values of the form.
6a488035 99 *
77b97be7 100 * @param $files
c490a46a 101 * @param CRM_Core_Form $form
77b97be7 102 *
a6c01b45
CW
103 * @return array
104 * list of errors to be posted back to the form
6a488035 105 */
00be9182 106 public static function formRule($values, $files, $form) {
6a488035
TO
107 return TRUE;
108 }
109
110 /**
d2e5d2ce 111 * Process the form submission.
6a488035 112 *
6a488035 113 *
c490a46a
CW
114 * @param CRM_Core_Form $form
115 * @param array $params
6a488035 116 */
00be9182 117 public static function beginPostProcess(&$form, &$params) {
6a488035
TO
118 if ($form->_context == 'case') {
119 $params['id'] = $form->_id;
120 }
121
de6c59ca 122 if (empty($params['is_reset_timeline'])) {
6a488035
TO
123 unset($params['reset_date_time']);
124 }
6a488035
TO
125 }
126
127 /**
d2e5d2ce 128 * Process the form submission.
6a488035 129 *
6a488035 130 *
c490a46a
CW
131 * @param CRM_Core_Form $form
132 * @param array $params
fd31fa4c 133 * @param $activity
6a488035 134 */
00be9182 135 public static function endPostProcess(&$form, &$params, $activity) {
6a488035
TO
136 if (!$form->_caseId) {
137 // always expecting a change, so case-id is a must.
138 return;
139 }
140
141 $caseTypes = CRM_Case_PseudoConstant::caseType('name');
42047ce4 142 $allCaseTypes = CRM_Case_PseudoConstant::caseType('title', FALSE);
6a488035 143
a7488080 144 if (!empty($caseTypes[$params['case_type_id']])) {
6a488035
TO
145 $caseType = $caseTypes[$params['case_type_id']];
146 }
147
148 if (!$form->_currentlyViewedContactId ||
149 !$form->_currentUserId ||
150 !$params['case_type_id'] ||
151 !$caseType
152 ) {
153 CRM_Core_Error::fatal('Required parameter missing for ChangeCaseType - end post processing');
154 }
155
9c248a42 156 $params['status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_status_id', 'Completed');
6a488035 157 $activity->status_id = $params['status_id'];
0571e734 158 $params['priority_id'] = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'priority_id', 'Normal');
6a488035
TO
159 $activity->priority_id = $params['priority_id'];
160
161 if ($activity->subject == 'null') {
162 $activity->subject = ts('Case type changed from %1 to %2',
be2fb01f 163 [
353ffa53 164 1 => CRM_Utils_Array::value($form->_defaults['case_type_id'], $allCaseTypes),
6a488035 165 2 => CRM_Utils_Array::value($params['case_type_id'], $allCaseTypes),
be2fb01f 166 ]
6a488035
TO
167 );
168 $activity->save();
169 }
170
171 // 1. initiate xml processor
172 $xmlProcessor = new CRM_Case_XMLProcessor_Process();
c22c1324 173 $caseId = CRM_Utils_Array::first($form->_caseId);
be2fb01f 174 $xmlProcessorParams = [
6a488035
TO
175 'clientID' => $form->_currentlyViewedContactId,
176 'creatorID' => $form->_currentUserId,
177 'standardTimeline' => 1,
178 'activityTypeName' => 'Change Case Type',
179 'activity_date_time' => CRM_Utils_Array::value('reset_date_time', $params),
c22c1324 180 'caseID' => $caseId,
6a488035 181 'resetTimeline' => CRM_Utils_Array::value('is_reset_timeline', $params),
be2fb01f 182 ];
6a488035
TO
183
184 $xmlProcessor->run($caseType, $xmlProcessorParams);
185 // status msg
186 $params['statusMsg'] = ts('Case Type changed successfully.');
187 }
96025800 188
6a488035 189}