3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class generates form components for OpenCase Activity.
21 class CRM_Case_Form_Activity_ChangeCaseType
{
24 * @param CRM_Core_Form $form
28 public static function preProcess(&$form) {
29 if (!isset($form->_caseId
)) {
30 CRM_Core_Error
::fatal(ts('Case Id not found.'));
35 * Set default values for the form.
37 * For edit/view mode the default values are retrieved from the database.
39 * @param CRM_Core_Form $form
43 public static function setDefaultValues(&$form) {
46 $defaults['is_reset_timeline'] = 1;
48 $defaults['reset_date_time'] = date('Y-m-d H:i:s');
49 $defaults['case_type_id'] = $form->_caseTypeId
;
55 * @param CRM_Core_Form $form
57 public static function buildQuickForm(&$form) {
58 $form->removeElement('status_id');
59 $form->removeElement('priority_id');
61 $caseId = CRM_Utils_Array
::first($form->_caseId
);
62 $form->_caseType
= CRM_Case_BAO_Case
::buildOptions('case_type_id', 'create');
63 $form->_caseTypeId
= CRM_Core_DAO
::getFieldValue('CRM_Case_DAO_Case',
67 if (!in_array($form->_caseTypeId
, $form->_caseType
)) {
68 $form->_caseType
[$form->_caseTypeId
] = CRM_Core_DAO
::getFieldValue('CRM_Case_DAO_CaseType', $form->_caseTypeId
, 'title');
71 $form->addField('case_type_id', ['context' => 'create', 'entity' => 'Case']);
74 $form->addYesNo('is_reset_timeline', ts('Reset Case Timeline?'), NULL, TRUE);
75 $form->add('datepicker', 'reset_date_time', ts('Reset Start Date'), NULL, FALSE, ['allowClear' => FALSE]);
79 * Global validation rules for the form.
81 * @param array $values
82 * Posted values of the form.
85 * @param CRM_Core_Form $form
88 * list of errors to be posted back to the form
90 public static function formRule($values, $files, $form) {
95 * Process the form submission.
98 * @param CRM_Core_Form $form
99 * @param array $params
101 public static function beginPostProcess(&$form, &$params) {
102 if ($form->_context
== 'case') {
103 $params['id'] = $form->_id
;
106 if (empty($params['is_reset_timeline'])) {
107 unset($params['reset_date_time']);
112 * Process the form submission.
115 * @param CRM_Core_Form $form
116 * @param array $params
119 public static function endPostProcess(&$form, &$params, $activity) {
120 if (!$form->_caseId
) {
121 // always expecting a change, so case-id is a must.
125 $caseTypes = CRM_Case_PseudoConstant
::caseType('name');
126 $allCaseTypes = CRM_Case_PseudoConstant
::caseType('title', FALSE);
128 if (!empty($caseTypes[$params['case_type_id']])) {
129 $caseType = $caseTypes[$params['case_type_id']];
132 if (!$form->_currentlyViewedContactId ||
133 !$form->_currentUserId ||
134 !$params['case_type_id'] ||
137 CRM_Core_Error
::fatal('Required parameter missing for ChangeCaseType - end post processing');
140 $params['status_id'] = CRM_Core_PseudoConstant
::getKey('CRM_Activity_BAO_Activity', 'activity_status_id', 'Completed');
141 $activity->status_id
= $params['status_id'];
142 $params['priority_id'] = CRM_Core_PseudoConstant
::getKey('CRM_Activity_BAO_Activity', 'priority_id', 'Normal');
143 $activity->priority_id
= $params['priority_id'];
145 if ($activity->subject
== 'null') {
146 $activity->subject
= ts('Case type changed from %1 to %2',
148 1 => CRM_Utils_Array
::value($form->_defaults
['case_type_id'], $allCaseTypes),
149 2 => CRM_Utils_Array
::value($params['case_type_id'], $allCaseTypes),
155 // 1. initiate xml processor
156 $xmlProcessor = new CRM_Case_XMLProcessor_Process();
157 $caseId = CRM_Utils_Array
::first($form->_caseId
);
158 $xmlProcessorParams = [
159 'clientID' => $form->_currentlyViewedContactId
,
160 'creatorID' => $form->_currentUserId
,
161 'standardTimeline' => 1,
162 'activityTypeName' => 'Change Case Type',
163 'activity_date_time' => CRM_Utils_Array
::value('reset_date_time', $params),
165 'resetTimeline' => CRM_Utils_Array
::value('is_reset_timeline', $params),
168 $xmlProcessor->run($caseType, $xmlProcessorParams);
170 $params['statusMsg'] = ts('Case Type changed successfully.');