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