| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.4 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2013 | |
| 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-2013 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | /** |
| 37 | * This class generates form components for OpenCase Activity |
| 38 | * |
| 39 | */ |
| 40 | class CRM_Case_Form_Activity_ChangeCaseType { |
| 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 | * @return void |
| 55 | */ |
| 56 | static function setDefaultValues(&$form) { |
| 57 | $defaults = array(); |
| 58 | |
| 59 | $defaults['is_reset_timeline'] = 1; |
| 60 | |
| 61 | $defaults['reset_date_time'] = array(); |
| 62 | list($defaults['reset_date_time'], $defaults['reset_date_time_time']) = CRM_Utils_Date::setDateDefaults(NULL, 'activityDateTime'); |
| 63 | $defaults['case_type_id'] = $form->_caseTypeId; |
| 64 | |
| 65 | return $defaults; |
| 66 | } |
| 67 | |
| 68 | static function buildQuickForm(&$form) { |
| 69 | $form->removeElement('status_id'); |
| 70 | $form->removeElement('priority_id'); |
| 71 | |
| 72 | $form->_caseType = CRM_Case_PseudoConstant::caseType(); |
| 73 | $caseTypeId = explode(CRM_Case_BAO_Case::VALUE_SEPARATOR, CRM_Core_DAO::getFieldValue('CRM_Case_DAO_Case', |
| 74 | $form->_caseId, |
| 75 | 'case_type_id' |
| 76 | )); |
| 77 | $form->_caseTypeId = $caseTypeId[1]; |
| 78 | if (!in_array($form->_caseTypeId, $form->_caseType)) { |
| 79 | $form->_caseType[$form->_caseTypeId] = CRM_Core_OptionGroup::getLabel('case_type', $form->_caseTypeId, FALSE); |
| 80 | } |
| 81 | |
| 82 | $form->add('select', 'case_type_id', ts('New Case Type'), |
| 83 | $form->_caseType, TRUE |
| 84 | ); |
| 85 | |
| 86 | // timeline |
| 87 | $form->addYesNo('is_reset_timeline', ts('Reset Case Timeline?'), NULL, TRUE, array('onclick' => "return showHideByValue('is_reset_timeline','','resetTimeline','table-row','radio',false);")); |
| 88 | $form->addDateTime('reset_date_time', ts('Reset Start Date'), FALSE, array('formatType' => 'activityDateTime')); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * global validation rules for the form |
| 93 | * |
| 94 | * @param array $values posted values of the form |
| 95 | * |
| 96 | * @return array list of errors to be posted back to the form |
| 97 | * @static |
| 98 | * @access public |
| 99 | */ |
| 100 | static function formRule($values, $files, $form) { |
| 101 | return TRUE; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Function to process the form |
| 106 | * |
| 107 | * @access public |
| 108 | * |
| 109 | * @return void |
| 110 | */ |
| 111 | static function beginPostProcess(&$form, &$params) { |
| 112 | if ($form->_context == 'case') { |
| 113 | $params['id'] = $form->_id; |
| 114 | } |
| 115 | |
| 116 | if (CRM_Utils_Array::value('is_reset_timeline', $params) == 0) { |
| 117 | unset($params['reset_date_time']); |
| 118 | } |
| 119 | else { |
| 120 | // store the date with proper format |
| 121 | $params['reset_date_time'] = CRM_Utils_Date::processDate($params['reset_date_time'], $params['reset_date_time_time']); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Function to process the form |
| 127 | * |
| 128 | * @access public |
| 129 | * |
| 130 | * @return void |
| 131 | */ |
| 132 | static function endPostProcess(&$form, &$params, $activity) { |
| 133 | if (!$form->_caseId) { |
| 134 | // always expecting a change, so case-id is a must. |
| 135 | return; |
| 136 | } |
| 137 | |
| 138 | $caseTypes = CRM_Case_PseudoConstant::caseType('name'); |
| 139 | $allCaseTypes = CRM_Case_PseudoConstant::caseType('label', FALSE); |
| 140 | |
| 141 | if (!empty($caseTypes[$params['case_type_id']])) { |
| 142 | $caseType = $caseTypes[$params['case_type_id']]; |
| 143 | } |
| 144 | |
| 145 | if (!$form->_currentlyViewedContactId || |
| 146 | !$form->_currentUserId || |
| 147 | !$params['case_type_id'] || |
| 148 | !$caseType |
| 149 | ) { |
| 150 | CRM_Core_Error::fatal('Required parameter missing for ChangeCaseType - end post processing'); |
| 151 | } |
| 152 | |
| 153 | $params['status_id'] = CRM_Core_OptionGroup::getValue('activity_status', 'Completed', 'name'); |
| 154 | $activity->status_id = $params['status_id']; |
| 155 | $params['priority_id'] = CRM_Core_OptionGroup::getValue('priority', 'Normal', 'name'); |
| 156 | $activity->priority_id = $params['priority_id']; |
| 157 | |
| 158 | if ($activity->subject == 'null') { |
| 159 | $activity->subject = ts('Case type changed from %1 to %2', |
| 160 | array(1 => CRM_Utils_Array::value($form->_defaults['case_type_id'], $allCaseTypes), |
| 161 | 2 => CRM_Utils_Array::value($params['case_type_id'], $allCaseTypes), |
| 162 | ) |
| 163 | ); |
| 164 | $activity->save(); |
| 165 | } |
| 166 | |
| 167 | // 1. initiate xml processor |
| 168 | $xmlProcessor = new CRM_Case_XMLProcessor_Process(); |
| 169 | $xmlProcessorParams = array( |
| 170 | 'clientID' => $form->_currentlyViewedContactId, |
| 171 | 'creatorID' => $form->_currentUserId, |
| 172 | 'standardTimeline' => 1, |
| 173 | 'activityTypeName' => 'Change Case Type', |
| 174 | 'activity_date_time' => CRM_Utils_Array::value('reset_date_time', $params), |
| 175 | 'caseID' => $form->_caseId, |
| 176 | 'resetTimeline' => CRM_Utils_Array::value('is_reset_timeline', $params), |
| 177 | ); |
| 178 | |
| 179 | $xmlProcessor->run($caseType, $xmlProcessorParams); |
| 180 | // status msg |
| 181 | $params['statusMsg'] = ts('Case Type changed successfully.'); |
| 182 | } |
| 183 | } |
| 184 | |