3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
37 * This class generates form components for OpenCase Activity
40 class CRM_Case_Form_Activity_ChangeCaseStatus
{
42 static function preProcess(&$form) {
43 if (!isset($form->_caseId
)) {
44 CRM_Core_Error
::fatal(ts('Case Id not found.'));
49 * This function sets the default values for the form. For edit/view mode
50 * the default values are retrieved from the database
56 static function setDefaultValues(&$form) {
58 // Retrieve current case status
59 $defaults['case_status_id'] = $form->_defaultCaseStatus
;
64 static function buildQuickForm(&$form) {
65 $form->removeElement('status_id');
66 $form->removeElement('priority_id');
68 $form->_caseStatus
= CRM_Case_PseudoConstant
::caseStatus();
69 $form->_oldCaseStatus
= $form->_defaultCaseStatus
= CRM_Core_DAO
::getFieldValue('CRM_Case_DAO_Case', $form->_caseId
, 'status_id');
71 if (!array_key_exists($form->_defaultCaseStatus
, $form->_caseStatus
)) {
72 $form->_caseStatus
[$form->_defaultCaseStatus
] = CRM_Core_OptionGroup
::getLabel('case_status',
73 $form->_defaultCaseStatus
,
77 $element = $form->add('select', 'case_status_id', ts('Case Status'),
78 $form->_caseStatus
, TRUE
80 // check if the case status id passed in url is a valid one, set as default and freeze
81 if (CRM_Utils_Request
::retrieve('case_status_id', 'Positive', $form)) {
82 $caseStatusId = CRM_Utils_Request
::retrieve('case_status_id', 'Positive', $form);
83 $caseStatus = CRM_Case_PseudoConstant
::caseStatus();
84 $form->_defaultCaseStatus
= array_key_exists($caseStatusId, $caseStatus) ?
$caseStatusId : NULL;
90 * global validation rules for the form
92 * @param array $values posted values of the form
94 * @return array list of errors to be posted back to the form
98 static function formRule($values, $files, $form) {
103 * Function to process the form
109 static function beginPostProcess(&$form, &$params) {
110 $params['id'] = CRM_Utils_Array
::value('case_id', $params);
114 * Function to process the form
120 static function endPostProcess(&$form, &$params, $activity) {
121 $groupingValues = CRM_Core_OptionGroup
::values('case_status', FALSE, TRUE, FALSE, NULL, 'value');
123 // Set case end_date if we're closing the case. Clear end_date if we're (re)opening it.
124 if (CRM_Utils_Array
::value($params['case_status_id'], $groupingValues) == 'Closed' && !empty($params['activity_date_time'])) {
125 $params['end_date'] = $params['activity_date_time'];
127 // End case-specific relationships (roles)
128 foreach ($params['target_contact_id'] as $cid) {
129 $rels = CRM_Case_BAO_Case
::getCaseRoles($cid, $params['case_id']);
130 // FIXME: Is there an existing function to close a relationship?
131 $query = 'UPDATE civicrm_relationship SET end_date=%2 WHERE id=%1';
132 foreach ($rels as $relId => $relData) {
133 $relParams = array(1 => array($relId, 'Integer'),
134 2 => array($params['end_date'], 'Timestamp'),
136 CRM_Core_DAO
::executeQuery($query, $relParams);
140 elseif (CRM_Utils_Array
::value($params['case_status_id'], $groupingValues) == 'Opened') {
141 $params['end_date'] = "null";
143 // Reopen 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?
147 $query = 'UPDATE civicrm_relationship SET end_date=NULL WHERE id=%1';
148 foreach ($rels as $relId => $relData) {
149 $relParams = array(1 => array($relId, 'Integer'));
150 CRM_Core_DAO
::executeQuery($query, $relParams);
154 $params['status_id'] = CRM_Core_OptionGroup
::getValue('activity_status', 'Completed', 'name');
155 $activity->status_id
= $params['status_id'];
156 $params['priority_id'] = CRM_Core_OptionGroup
::getValue('priority', 'Normal', 'name');
157 $activity->priority_id
= $params['priority_id'];
159 if ($activity->subject
== 'null') {
160 $activity->subject
= ts('Case status changed from %1 to %2', array(
161 1 => CRM_Utils_Array
::value($form->_oldCaseStatus
, $form->_caseStatus
),
162 2 => CRM_Utils_Array
::value($params['case_status_id'], $form->_caseStatus
)
168 // FIXME: does this do anything ?
169 $params['statusMsg'] = ts('Case Status changed successfully.');