ChangeCaseStatus.php 8.09 KB
Newer Older
totten's avatar
totten committed
1 2 3
<?php
/*
 +--------------------------------------------------------------------+
totten's avatar
totten committed
4
 | CiviCRM version 5                                                  |
totten's avatar
totten committed
5
 +--------------------------------------------------------------------+
Seamus Lee's avatar
Seamus Lee committed
6
 | Copyright CiviCRM LLC (c) 2004-2019                                |
totten's avatar
totten committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 +--------------------------------------------------------------------+
 | This file is a part of CiviCRM.                                    |
 |                                                                    |
 | CiviCRM is free software; you can copy, modify, and distribute it  |
 | under the terms of the GNU Affero General Public License           |
 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
 |                                                                    |
 | CiviCRM is distributed in the hope that it will be useful, but     |
 | WITHOUT ANY WARRANTY; without even the implied warranty of         |
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
 | See the GNU Affero General Public License for more details.        |
 |                                                                    |
 | You should have received a copy of the GNU Affero General Public   |
 | License and the CiviCRM Licensing Exception along                  |
 | with this program; if not, contact CiviCRM LLC                     |
 | at info[AT]civicrm[DOT]org. If you have questions about the        |
 | GNU Affero General Public License or the licensing of CiviCRM,     |
 | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
 +--------------------------------------------------------------------+
26
 */
totten's avatar
totten committed
27 28 29 30

/**
 *
 * @package CRM
Seamus Lee's avatar
Seamus Lee committed
31
 * @copyright CiviCRM LLC (c) 2004-2019
totten's avatar
totten committed
32 33 34
 */

/**
eileenmcnaugton's avatar
eileenmcnaugton committed
35
 * This class generates form components for OpenCase Activity.
totten's avatar
totten committed
36 37 38
 */
class CRM_Case_Form_Activity_ChangeCaseStatus {

39
  /**
40
   * @param CRM_Core_Form $form
41 42 43
   *
   * @throws Exception
   */
44
  public static function preProcess(&$form) {
totten's avatar
totten committed
45 46 47
    if (!isset($form->_caseId)) {
      CRM_Core_Error::fatal(ts('Case Id not found.'));
    }
48

49
    $form->addElement('checkbox', 'updateLinkedCases', NULL, NULL, ['class' => 'select-row']);
50 51 52 53

    $caseID = CRM_Utils_Array::first($form->_caseId);
    $cases = CRM_Case_BAO_Case::getRelatedCases($caseID);
    $form->assign('linkedCases', $cases);
totten's avatar
totten committed
54 55 56
  }

  /**
eileenmcnaugton's avatar
eileenmcnaugton committed
57 58 59
   * Set default values for the form.
   *
   * For edit/view mode the default values are retrieved from the database.
totten's avatar
totten committed
60 61
   *
   *
62
   * @param CRM_Core_Form $form
63
   *
eileenmcnaugton's avatar
eileenmcnaugton committed
64
   * @return array
totten's avatar
totten committed
65
   */
66
  public static function setDefaultValues(&$form) {
67
    $defaults = [];
totten's avatar
totten committed
68 69 70 71 72 73
    // Retrieve current case status
    $defaults['case_status_id'] = $form->_defaultCaseStatus;

    return $defaults;
  }

74
  /**
75
   * @param CRM_Core_Form $form
76
   */
77
  public static function buildQuickForm(&$form) {
totten's avatar
totten committed
78 79 80
    $form->removeElement('status_id');
    $form->removeElement('priority_id');

81
    $caseTypes = [];
82

totten's avatar
totten committed
83
    $form->_caseStatus = CRM_Case_PseudoConstant::caseStatus();
84 85 86
    $statusNames = CRM_Case_PseudoConstant::caseStatus('name');

    // Limit case statuses to allowed types for these case(s)
87
    $allCases = civicrm_api3('Case', 'get', ['return' => 'case_type_id', 'id' => ['IN' => (array) $form->_caseId]]);
88 89 90
    foreach ($allCases['values'] as $case) {
      $caseTypes[$case['case_type_id']] = $case['case_type_id'];
    }
91
    $caseTypes = civicrm_api3('CaseType', 'get', ['id' => ['IN' => $caseTypes]]);
92 93 94 95 96 97 98 99 100
    foreach ($caseTypes['values'] as $ct) {
      if (!empty($ct['definition']['statuses'])) {
        foreach ($form->_caseStatus as $id => $label) {
          if (!in_array($statusNames[$id], $ct['definition']['statuses'])) {
            unset($form->_caseStatus[$id]);
          }
        }
      }
    }
totten's avatar
totten committed
101

102 103 104 105 106 107
    foreach ($form->_caseId as $key => $val) {
      $form->_oldCaseStatus[] = $form->_defaultCaseStatus[] = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_Case', $val, 'status_id');
    }

    foreach ($form->_defaultCaseStatus as $keydefault => $valdefault) {
      if (!array_key_exists($valdefault, $form->_caseStatus)) {
108
        $form->_caseStatus[$valdefault] = CRM_Core_PseudoConstant::getLabel('CRM_Case_BAO_Case', 'status_id', $valdefault);
109
      }
totten's avatar
totten committed
110
    }
111
    $element = $form->add('select', 'case_status_id', ts('Case Status'),
totten's avatar
totten committed
112 113
      $form->_caseStatus, TRUE
    );
114 115 116 117 118 119 120
    // check if the case status id passed in url is a valid one, set as default and freeze
    if (CRM_Utils_Request::retrieve('case_status_id', 'Positive', $form)) {
      $caseStatusId = CRM_Utils_Request::retrieve('case_status_id', 'Positive', $form);
      $caseStatus = CRM_Case_PseudoConstant::caseStatus();
      $form->_defaultCaseStatus = array_key_exists($caseStatusId, $caseStatus) ? $caseStatusId : NULL;
      $element->freeze();
    }
totten's avatar
totten committed
121 122 123
  }

  /**
124
   * Global validation rules for the form.
totten's avatar
totten committed
125
   *
126 127
   * @param array $values
   *   Posted values of the form.
totten's avatar
totten committed
128
   *
129
   * @param $files
130
   * @param CRM_Core_Form $form
131
   *
132 133
   * @return array
   *   list of errors to be posted back to the form
totten's avatar
totten committed
134
   */
135
  public static function formRule($values, $files, $form) {
totten's avatar
totten committed
136 137 138 139
    return TRUE;
  }

  /**
140
   * Process the form submission.
totten's avatar
totten committed
141
   *
142 143
   * @param CRM_Core_Form $form
   * @param array $params
totten's avatar
totten committed
144
   */
145
  public static function beginPostProcess(&$form, &$params) {
totten's avatar
totten committed
146
    $params['id'] = CRM_Utils_Array::value('case_id', $params);
147 148 149 150 151 152 153 154 155 156 157

    if ($params['updateLinkedCases'] === '1') {
      $caseID = CRM_Utils_Array::first($form->_caseId);
      $cases = CRM_Case_BAO_Case::getRelatedCases($caseID);

      foreach ($cases as $currentCase) {
        if ($currentCase['status_id'] != $params['case_status_id']) {
          $form->_caseId[] = $currentCase['case_id'];
        }
      }
    }
totten's avatar
totten committed
158 159 160
  }

  /**
161
   * Process the form submission.
totten's avatar
totten committed
162
   *
163 164
   * @param CRM_Core_Form $form
   * @param array $params
eileenmcnaugton's avatar
eileenmcnaugton committed
165
   * @param CRM_Activity_BAO_Activity $activity
totten's avatar
totten committed
166
   */
167
  public static function endPostProcess(&$form, &$params, $activity) {
168

totten's avatar
totten committed
169 170 171
    $groupingValues = CRM_Core_OptionGroup::values('case_status', FALSE, TRUE, FALSE, NULL, 'value');

    // Set case end_date if we're closing the case. Clear end_date if we're (re)opening it.
172
    if (CRM_Utils_Array::value($params['case_status_id'], $groupingValues) == 'Closed' && !empty($params['activity_date_time'])) {
173
      $params['end_date'] = CRM_Utils_Date::isoToMysql($params['activity_date_time']);
totten's avatar
totten committed
174 175 176 177 178 179 180

      // End case-specific relationships (roles)
      foreach ($params['target_contact_id'] as $cid) {
        $rels = CRM_Case_BAO_Case::getCaseRoles($cid, $params['case_id']);
        // FIXME: Is there an existing function to close a relationship?
        $query = 'UPDATE civicrm_relationship SET end_date=%2 WHERE id=%1';
        foreach ($rels as $relId => $relData) {
181 182 183 184
          $relParams = [
            1 => [$relId, 'Integer'],
            2 => [$params['end_date'], 'Timestamp'],
          ];
totten's avatar
totten committed
185 186 187 188 189 190 191 192 193
          CRM_Core_DAO::executeQuery($query, $relParams);
        }
      }
    }
    elseif (CRM_Utils_Array::value($params['case_status_id'], $groupingValues) == 'Opened') {
      $params['end_date'] = "null";

      // Reopen case-specific relationships (roles)
      foreach ($params['target_contact_id'] as $cid) {
194
        $rels = CRM_Case_BAO_Case::getCaseRoles($cid, $params['case_id'], NULL, FALSE);
totten's avatar
totten committed
195 196 197
        // FIXME: Is there an existing function?
        $query = 'UPDATE civicrm_relationship SET end_date=NULL WHERE id=%1';
        foreach ($rels as $relId => $relData) {
198
          $relParams = [1 => [$relId, 'Integer']];
totten's avatar
totten committed
199 200 201 202
          CRM_Core_DAO::executeQuery($query, $relParams);
        }
      }
    }
203
    $params['status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_status_id', 'Completed');
totten's avatar
totten committed
204
    $activity->status_id = $params['status_id'];
205
    $params['priority_id'] = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'priority_id', 'Normal');
totten's avatar
totten committed
206 207
    $activity->priority_id = $params['priority_id'];

208
    foreach ($form->_oldCaseStatus as $statuskey => $statusval) {
209
      if ($activity->subject == 'null') {
210
        $activity->subject = ts('Case status changed from %1 to %2', [
211 212 213
          1 => CRM_Utils_Array::value($statusval, $form->_caseStatus),
          2 => CRM_Utils_Array::value($params['case_status_id'], $form->_caseStatus),
        ]);
214 215
        $activity->save();
      }
totten's avatar
totten committed
216 217
    }
  }
218

totten's avatar
totten committed
219
}