| 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_ChangeCaseStatus { |
| 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 | // Retrieve current case status |
| 59 | $defaults['case_status_id'] = $form->_defaultCaseStatus; |
| 60 | |
| 61 | return $defaults; |
| 62 | } |
| 63 | |
| 64 | static function buildQuickForm(&$form) { |
| 65 | $form->removeElement('status_id'); |
| 66 | $form->removeElement('priority_id'); |
| 67 | |
| 68 | $form->_caseStatus = CRM_Case_PseudoConstant::caseStatus(); |
| 69 | $form->_defaultCaseStatus = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_Case', $form->_caseId, 'status_id'); |
| 70 | |
| 71 | if (!array_key_exists($form->_defaultCaseStatus, $form->_caseStatus)) { |
| 72 | $form->_caseStatus[$form->_defaultCaseStatus] = CRM_Core_OptionGroup::getLabel('case_status', |
| 73 | $form->_defaultCaseStatus, |
| 74 | FALSE |
| 75 | ); |
| 76 | } |
| 77 | $form->add('select', 'case_status_id', ts('Case Status'), |
| 78 | $form->_caseStatus, TRUE |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * global validation rules for the form |
| 84 | * |
| 85 | * @param array $values posted values of the form |
| 86 | * |
| 87 | * @return array list of errors to be posted back to the form |
| 88 | * @static |
| 89 | * @access public |
| 90 | */ |
| 91 | static function formRule($values, $files, $form) { |
| 92 | return TRUE; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Function to process the form |
| 97 | * |
| 98 | * @access public |
| 99 | * |
| 100 | * @return void |
| 101 | */ |
| 102 | static function beginPostProcess(&$form, &$params) { |
| 103 | $params['id'] = CRM_Utils_Array::value('case_id', $params); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Function to process the form |
| 108 | * |
| 109 | * @access public |
| 110 | * |
| 111 | * @return void |
| 112 | */ |
| 113 | static function endPostProcess(&$form, &$params, $activity) { |
| 114 | $groupingValues = CRM_Core_OptionGroup::values('case_status', FALSE, TRUE, FALSE, NULL, 'value'); |
| 115 | |
| 116 | // Set case end_date if we're closing the case. Clear end_date if we're (re)opening it. |
| 117 | if (CRM_Utils_Array::value($params['case_status_id'], $groupingValues) == 'Closed' && !empty($params['activity_date_time'])) { |
| 118 | $params['end_date'] = $params['activity_date_time']; |
| 119 | |
| 120 | // End case-specific relationships (roles) |
| 121 | foreach ($params['target_contact_id'] as $cid) { |
| 122 | $rels = CRM_Case_BAO_Case::getCaseRoles($cid, $params['case_id']); |
| 123 | // FIXME: Is there an existing function to close a relationship? |
| 124 | $query = 'UPDATE civicrm_relationship SET end_date=%2 WHERE id=%1'; |
| 125 | foreach ($rels as $relId => $relData) { |
| 126 | $relParams = array(1 => array($relId, 'Integer'), |
| 127 | 2 => array($params['end_date'], 'Timestamp'), |
| 128 | ); |
| 129 | CRM_Core_DAO::executeQuery($query, $relParams); |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | elseif (CRM_Utils_Array::value($params['case_status_id'], $groupingValues) == 'Opened') { |
| 134 | $params['end_date'] = "null"; |
| 135 | |
| 136 | // Reopen case-specific relationships (roles) |
| 137 | foreach ($params['target_contact_id'] as $cid) { |
| 138 | $rels = CRM_Case_BAO_Case::getCaseRoles($cid, $params['case_id']); |
| 139 | // FIXME: Is there an existing function? |
| 140 | $query = 'UPDATE civicrm_relationship SET end_date=NULL WHERE id=%1'; |
| 141 | foreach ($rels as $relId => $relData) { |
| 142 | $relParams = array(1 => array($relId, 'Integer')); |
| 143 | CRM_Core_DAO::executeQuery($query, $relParams); |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | $params['status_id'] = CRM_Core_OptionGroup::getValue('activity_status', 'Completed', 'name'); |
| 148 | $activity->status_id = $params['status_id']; |
| 149 | $params['priority_id'] = CRM_Core_OptionGroup::getValue('priority', 'Normal', 'name'); |
| 150 | $activity->priority_id = $params['priority_id']; |
| 151 | |
| 152 | if ($activity->subject == 'null') { |
| 153 | $activity->subject = ts('Case status changed from %1 to %2', array( |
| 154 | 1 => CRM_Utils_Array::value($form->_defaults['case_status_id'], $form->_caseStatus), |
| 155 | 2 => CRM_Utils_Array::value($params['case_status_id'], $form->_caseStatus) |
| 156 | ) |
| 157 | ); |
| 158 | $activity->save(); |
| 159 | } |
| 160 | |
| 161 | // FIXME: does this do anything ? |
| 162 | $params['statusMsg'] = ts('Case Status changed successfully.'); |
| 163 | } |
| 164 | } |
| 165 | |