| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 5 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
| 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-2018 |
| 32 | */ |
| 33 | |
| 34 | /** |
| 35 | * This class generates form components for case activity. |
| 36 | */ |
| 37 | class CRM_Case_Form_Case extends CRM_Core_Form { |
| 38 | |
| 39 | /** |
| 40 | * The context |
| 41 | * |
| 42 | * @var string |
| 43 | */ |
| 44 | public $_context = 'case'; |
| 45 | |
| 46 | /** |
| 47 | * Case Id |
| 48 | */ |
| 49 | public $_caseId = NULL; |
| 50 | |
| 51 | /** |
| 52 | * Client Id |
| 53 | */ |
| 54 | public $_currentlyViewedContactId = NULL; |
| 55 | |
| 56 | /** |
| 57 | * Activity Type File |
| 58 | */ |
| 59 | public $_activityTypeFile = NULL; |
| 60 | |
| 61 | /** |
| 62 | * Logged in contact Id |
| 63 | */ |
| 64 | public $_currentUserId = NULL; |
| 65 | |
| 66 | /** |
| 67 | * Activity type Id |
| 68 | */ |
| 69 | public $_activityTypeId = NULL; |
| 70 | |
| 71 | /** |
| 72 | * Activity type Id |
| 73 | */ |
| 74 | public $_activityId = NULL; |
| 75 | |
| 76 | /** |
| 77 | * Action |
| 78 | */ |
| 79 | public $_action; |
| 80 | |
| 81 | /** |
| 82 | * Case type id |
| 83 | */ |
| 84 | public $_caseTypeId = NULL; |
| 85 | |
| 86 | /** |
| 87 | * Build the form object. |
| 88 | */ |
| 89 | public function preProcess() { |
| 90 | |
| 91 | $this->_caseId = CRM_Utils_Request::retrieve('id', 'Positive', $this); |
| 92 | |
| 93 | $this->_currentlyViewedContactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this); |
| 94 | |
| 95 | if ($this->_action & CRM_Core_Action::ADD && !$this->_currentlyViewedContactId) { |
| 96 | // check for add contacts permissions |
| 97 | if (!CRM_Core_Permission::check('add contacts')) { |
| 98 | CRM_Utils_System::permissionDenied(); |
| 99 | return; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | //CRM-4418 |
| 104 | if (!CRM_Core_Permission::checkActionPermission('CiviCase', $this->_action)) { |
| 105 | CRM_Core_Error::fatal(ts('You do not have permission to access this page.')); |
| 106 | } |
| 107 | |
| 108 | if ($this->_action & CRM_Core_Action::DELETE || $this->_action & CRM_Core_Action::RENEW) { |
| 109 | return TRUE; |
| 110 | } |
| 111 | |
| 112 | if (!$this->_caseId) { |
| 113 | $caseAttributes = array( |
| 114 | 'case_type_id' => ts('Case Type'), |
| 115 | 'status_id' => ts('Case Status'), |
| 116 | 'medium_id' => ts('Activity Medium'), |
| 117 | ); |
| 118 | |
| 119 | foreach ($caseAttributes as $key => $label) { |
| 120 | if (!CRM_Case_BAO_Case::buildOptions($key, 'create')) { |
| 121 | CRM_Core_Error::fatal(ts('You do not have any active %1', array(1 => $label))); |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | if ($this->_action & CRM_Core_Action::ADD) { |
| 127 | $this->_activityTypeId = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Open Case'); |
| 128 | if (!$this->_activityTypeId) { |
| 129 | CRM_Core_Error::fatal(ts('The Open Case activity type is missing or disabled. Please have your site administrator check Administer > Option Lists > Activity Types for the CiviCase component.')); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | //check for case permissions. |
| 134 | if (!CRM_Case_BAO_Case::accessCiviCase()) { |
| 135 | CRM_Core_Error::fatal(ts('You are not authorized to access this page.')); |
| 136 | } |
| 137 | if (($this->_action & CRM_Core_Action::ADD) && |
| 138 | (!CRM_Core_Permission::check('access all cases and activities') && |
| 139 | !CRM_Core_Permission::check('add cases') |
| 140 | ) |
| 141 | ) { |
| 142 | CRM_Core_Error::fatal(ts('You are not authorized to access this page.')); |
| 143 | } |
| 144 | |
| 145 | if ($this->_activityTypeFile = CRM_Activity_BAO_Activity::getFileForActivityTypeId($this->_activityTypeId, |
| 146 | 'Case' |
| 147 | ) |
| 148 | ) { |
| 149 | $this->assign('activityTypeFile', $this->_activityTypeFile); |
| 150 | } |
| 151 | |
| 152 | $details = CRM_Case_PseudoConstant::caseActivityType(FALSE); |
| 153 | |
| 154 | CRM_Utils_System::setTitle($details[$this->_activityTypeId]['label']); |
| 155 | $this->assign('activityType', $details[$this->_activityTypeId]['label']); |
| 156 | $this->assign('activityTypeDescription', $details[$this->_activityTypeId]['description']); |
| 157 | |
| 158 | if (isset($this->_currentlyViewedContactId)) { |
| 159 | $contact = new CRM_Contact_DAO_Contact(); |
| 160 | $contact->id = $this->_currentlyViewedContactId; |
| 161 | if (!$contact->find(TRUE)) { |
| 162 | CRM_Core_Error::statusBounce(ts('Client contact does not exist: %1', array(1 => $this->_currentlyViewedContactId))); |
| 163 | } |
| 164 | $this->assign('clientName', $contact->display_name); |
| 165 | } |
| 166 | |
| 167 | $session = CRM_Core_Session::singleton(); |
| 168 | $this->_currentUserId = $session->get('userID'); |
| 169 | |
| 170 | //when custom data is included in this page |
| 171 | CRM_Custom_Form_CustomData::preProcess($this, NULL, $this->_activityTypeId, 1, 'Activity'); |
| 172 | $className = "CRM_Case_Form_Activity_{$this->_activityTypeFile}"; |
| 173 | $className::preProcess($this); |
| 174 | $activityGroupTree = $this->_groupTree; |
| 175 | |
| 176 | // for case custom fields to populate with defaults |
| 177 | if (!empty($_POST['hidden_custom'])) { |
| 178 | $params = CRM_Utils_Request::exportValues(); |
| 179 | CRM_Custom_Form_CustomData::preProcess($this, NULL, CRM_Utils_Array::value('case_type_id', $params, $this->_caseTypeId), 1, 'Case', $this->_caseId); |
| 180 | CRM_Custom_Form_CustomData::buildQuickForm($this); |
| 181 | } |
| 182 | |
| 183 | // so that grouptree is not populated with case fields, since the grouptree is used |
| 184 | // for populating activity custom fields. |
| 185 | $this->_groupTree = $activityGroupTree; |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Set default values for the form. |
| 190 | */ |
| 191 | public function setDefaultValues() { |
| 192 | if ($this->_action & CRM_Core_Action::DELETE || $this->_action & CRM_Core_Action::RENEW) { |
| 193 | return TRUE; |
| 194 | } |
| 195 | $className = "CRM_Case_Form_Activity_{$this->_activityTypeFile}"; |
| 196 | $defaults = $className::setDefaultValues($this); |
| 197 | $defaults = array_merge($defaults, CRM_Custom_Form_CustomData::setDefaultValues($this)); |
| 198 | return $defaults; |
| 199 | } |
| 200 | |
| 201 | public function buildQuickForm() { |
| 202 | $xmlProcessorProcess = new CRM_Case_XMLProcessor_Process(); |
| 203 | $isMultiClient = $xmlProcessorProcess->getAllowMultipleCaseClients(); |
| 204 | $this->assign('multiClient', $isMultiClient); |
| 205 | |
| 206 | if ($this->_action & CRM_Core_Action::DELETE || $this->_action & CRM_Core_Action::RENEW) { |
| 207 | $title = ts('Delete'); |
| 208 | if ($this->_action & CRM_Core_Action::RENEW) { |
| 209 | $title = ts('Restore'); |
| 210 | } |
| 211 | $this->addButtons(array( |
| 212 | array( |
| 213 | 'type' => 'next', |
| 214 | 'name' => $title, |
| 215 | 'spacing' => ' ', |
| 216 | 'isDefault' => TRUE, |
| 217 | ), |
| 218 | array( |
| 219 | 'type' => 'cancel', |
| 220 | 'name' => ts('Cancel'), |
| 221 | ), |
| 222 | ) |
| 223 | ); |
| 224 | return; |
| 225 | } |
| 226 | |
| 227 | //need to assign custom data type and subtype to the template |
| 228 | $this->assign('customDataType', 'Case'); |
| 229 | |
| 230 | CRM_Custom_Form_CustomData::buildQuickForm($this); |
| 231 | // we don't want to show button on top of custom form |
| 232 | $this->assign('noPreCustomButton', TRUE); |
| 233 | |
| 234 | $s = CRM_Core_DAO::getAttribute('CRM_Activity_DAO_Activity', 'subject'); |
| 235 | if (!is_array($s)) { |
| 236 | $s = array(); |
| 237 | } |
| 238 | $this->add('text', 'activity_subject', ts('Subject'), |
| 239 | array_merge($s, array( |
| 240 | 'maxlength' => '128', |
| 241 | )), TRUE |
| 242 | ); |
| 243 | |
| 244 | $tags = CRM_Core_BAO_Tag::getColorTags('civicrm_case'); |
| 245 | |
| 246 | if (!empty($tags)) { |
| 247 | $this->add('select2', 'tag', ts('Tags'), $tags, FALSE, |
| 248 | array('class' => 'huge', 'multiple' => 'multiple') |
| 249 | ); |
| 250 | } |
| 251 | |
| 252 | // build tag widget |
| 253 | $parentNames = CRM_Core_BAO_Tag::getTagSet('civicrm_case'); |
| 254 | CRM_Core_Form_Tag::buildQuickForm($this, $parentNames, 'civicrm_case', NULL, FALSE, TRUE); |
| 255 | |
| 256 | $this->addButtons(array( |
| 257 | array( |
| 258 | 'type' => 'next', |
| 259 | 'name' => ts('Save'), |
| 260 | 'spacing' => ' ', |
| 261 | 'isDefault' => TRUE, |
| 262 | ), |
| 263 | array( |
| 264 | 'type' => 'cancel', |
| 265 | 'name' => ts('Cancel'), |
| 266 | ), |
| 267 | ) |
| 268 | ); |
| 269 | |
| 270 | $className = "CRM_Case_Form_Activity_{$this->_activityTypeFile}"; |
| 271 | $className::buildQuickForm($this); |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Add local and global form rules. |
| 276 | * |
| 277 | * @return bool |
| 278 | */ |
| 279 | public function addRules() { |
| 280 | if ($this->_action & CRM_Core_Action::DELETE || $this->_action & CRM_Core_Action::RENEW) { |
| 281 | return TRUE; |
| 282 | } |
| 283 | $className = "CRM_Case_Form_Activity_{$this->_activityTypeFile}"; |
| 284 | $this->addFormRule(array($className, 'formRule'), $this); |
| 285 | $this->addFormRule(array('CRM_Case_Form_Case', 'formRule'), $this); |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * Global validation rules for the form. |
| 290 | * |
| 291 | * @param array $values |
| 292 | * Posted values of the form. |
| 293 | * |
| 294 | * @param $files |
| 295 | * @param CRM_Core_Form $form |
| 296 | * |
| 297 | * @return array |
| 298 | * list of errors to be posted back to the form |
| 299 | */ |
| 300 | public static function formRule($values, $files, $form) { |
| 301 | return TRUE; |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * Process the form submission. |
| 306 | */ |
| 307 | public function postProcess() { |
| 308 | $transaction = new CRM_Core_Transaction(); |
| 309 | |
| 310 | // check if dedupe button, if so return. |
| 311 | $buttonName = $this->controller->getButtonName(); |
| 312 | if (isset($this->_dedupeButtonName) && $buttonName == $this->_dedupeButtonName) { |
| 313 | return; |
| 314 | } |
| 315 | |
| 316 | if ($this->_action & CRM_Core_Action::DELETE) { |
| 317 | $caseDelete = CRM_Case_BAO_Case::deleteCase($this->_caseId, TRUE); |
| 318 | if ($caseDelete) { |
| 319 | CRM_Core_Session::setStatus(ts('You can view and / or restore deleted cases by checking the "Deleted Cases" option under Find Cases.'), ts('Case Deleted'), 'success'); |
| 320 | } |
| 321 | return; |
| 322 | } |
| 323 | |
| 324 | if ($this->_action & CRM_Core_Action::RENEW) { |
| 325 | $caseRestore = CRM_Case_BAO_Case::restoreCase($this->_caseId); |
| 326 | if ($caseRestore) { |
| 327 | CRM_Core_Session::setStatus(ts('The selected case has been restored.'), ts('Restored'), 'success'); |
| 328 | } |
| 329 | return; |
| 330 | } |
| 331 | // store the submitted values in an array |
| 332 | $params = $this->controller->exportValues($this->_name); |
| 333 | $params['now'] = date("Ymd"); |
| 334 | |
| 335 | // 1. call begin post process |
| 336 | if ($this->_activityTypeFile) { |
| 337 | $className = "CRM_Case_Form_Activity_{$this->_activityTypeFile}"; |
| 338 | $className::beginPostProcess($this, $params); |
| 339 | } |
| 340 | |
| 341 | if (!empty($params['hidden_custom']) && |
| 342 | !isset($params['custom']) |
| 343 | ) { |
| 344 | $params['custom'] = CRM_Core_BAO_CustomField::postProcess( |
| 345 | $params, |
| 346 | NULL, |
| 347 | 'Case' |
| 348 | ); |
| 349 | } |
| 350 | |
| 351 | // 2. create/edit case |
| 352 | if (!empty($params['case_type_id'])) { |
| 353 | $params['case_type'] = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_CaseType', $params['case_type_id'], 'name', 'id'); |
| 354 | $params['subject'] = $params['activity_subject']; |
| 355 | } |
| 356 | $caseObj = CRM_Case_BAO_Case::create($params); |
| 357 | $this->_caseId = $params['case_id'] = $caseObj->id; |
| 358 | // unset any ids, custom data |
| 359 | unset($params['id'], $params['custom']); |
| 360 | |
| 361 | // add tags if exists |
| 362 | $tagParams = array(); |
| 363 | if (!empty($params['tag'])) { |
| 364 | $tagParams = array(); |
| 365 | if (!is_array($params['tag'])) { |
| 366 | $params['tag'] = explode(',', $params['tag']); |
| 367 | } |
| 368 | foreach ($params['tag'] as $tag) { |
| 369 | $tagParams[$tag] = 1; |
| 370 | } |
| 371 | } |
| 372 | CRM_Core_BAO_EntityTag::create($tagParams, 'civicrm_case', $caseObj->id); |
| 373 | |
| 374 | //save free tags |
| 375 | if (isset($params['case_taglist']) && !empty($params['case_taglist'])) { |
| 376 | CRM_Core_Form_Tag::postProcess($params['case_taglist'], $caseObj->id, 'civicrm_case', $this); |
| 377 | } |
| 378 | |
| 379 | // user context |
| 380 | $url = CRM_Utils_System::url('civicrm/contact/view/case', |
| 381 | "reset=1&action=view&cid={$this->_currentlyViewedContactId}&id={$caseObj->id}" |
| 382 | ); |
| 383 | CRM_Core_Session::singleton()->pushUserContext($url); |
| 384 | |
| 385 | // 3. format activity custom data |
| 386 | if (!empty($params['hidden_custom'])) { |
| 387 | $customFields = CRM_Core_BAO_CustomField::getFields('Activity', FALSE, FALSE, $this->_activityTypeId); |
| 388 | $customFields = CRM_Utils_Array::crmArrayMerge($customFields, |
| 389 | CRM_Core_BAO_CustomField::getFields('Activity', FALSE, FALSE, |
| 390 | NULL, NULL, TRUE |
| 391 | ) |
| 392 | ); |
| 393 | $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params, |
| 394 | $this->_activityId, |
| 395 | 'Activity' |
| 396 | ); |
| 397 | } |
| 398 | |
| 399 | // 4. call end post process |
| 400 | if ($this->_activityTypeFile) { |
| 401 | $className::endPostProcess($this, $params); |
| 402 | } |
| 403 | |
| 404 | CRM_Core_Session::setStatus($params['statusMsg'], ts('Saved'), 'success'); |
| 405 | } |
| 406 | |
| 407 | } |