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