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_OpenCase
{
43 * the id of the client associated with this case
50 static function preProcess(&$form) {
51 //get multi client case configuration
52 $xmlProcessorProcess = new CRM_Case_XMLProcessor_Process();
53 $form->_allowMultiClient
= (bool) $xmlProcessorProcess->getAllowMultipleCaseClients();
55 if ($form->_context
== 'caseActivity') {
56 $contactID = CRM_Utils_Request
::retrieve('cid', 'Positive', $form);
57 $atype = CRM_Core_OptionGroup
::getValue('activity_type',
58 'Change Case Start Date',
61 $form->assign('changeStartURL', CRM_Utils_System
::url('civicrm/case/activity',
62 "action=add&reset=1&cid=$contactID&caseid={$form->_caseId}&atype=$atype"
68 $form->_context
= CRM_Utils_Request
::retrieve('context', 'String', $form);
69 $form->_contactID
= CRM_Utils_Request
::retrieve('cid', 'Positive', $form);
70 $form->assign('context', $form->_context
);
72 // check if the case type id passed in url is a valid one
73 $caseTypeId = CRM_Utils_Request
::retrieve('ctype', 'Positive', $form);
74 $caseTypes = CRM_Case_PseudoConstant
::caseType();
75 $form->_caseTypeId
= array_key_exists($caseTypeId, $caseTypes) ?
$caseTypeId : NULL;
77 // check if the case status id passed in url is a valid one
78 $caseStatusId = CRM_Utils_Request
::retrieve('cStatus', 'Positive', $form);
79 $caseStatus = CRM_Case_PseudoConstant
::caseStatus();
80 $form->_caseStatusId
= array_key_exists($caseStatusId, $caseStatus) ?
$caseStatusId : NULL;
83 CRM_Core_BAO_File
::buildAttachment( $form, 'civicrm_activity', $form->_activityId
);
87 * This function sets the default values for the form. For edit/view mode
88 * the default values are retrieved from the database
94 static function setDefaultValues(&$form) {
96 if ($form->_context
== 'caseActivity') {
100 list($defaults['start_date'], $defaults['start_date_time']) = CRM_Utils_Date
::setDateDefaults(NULL, 'activityDateTime');
102 // set default case status, case type, encounter medium, location type and phone type defaults are set in DB
103 if ($form->_caseStatusId
) {
104 $caseStatus = $form->_caseStatusId
;
107 $caseStatus = CRM_Core_OptionGroup
::values('case_status', FALSE, FALSE, FALSE, 'AND is_default = 1');
108 if (count($caseStatus) == 1) {
109 $caseStatus = key($caseStatus); //$defaults['status_id'] = key($caseStatus);
112 $defaults['status_id'] = $caseStatus;
114 // set default case type passed in url
115 if ($form->_caseTypeId
) {
116 $caseType = $form->_caseTypeId
;
119 // set default case type if only one of it exists
120 $caseType = CRM_Core_OptionGroup
::values('case_type', FALSE, FALSE, FALSE, 'AND is_default = 1');
121 if (count($caseType) == 1) {
122 $caseType = key($caseType);
125 $defaults['case_type_id'] = $caseType;
127 $medium = CRM_Core_OptionGroup
::values('encounter_medium', FALSE, FALSE, FALSE, 'AND is_default = 1');
128 if (count($medium) == 1) {
129 $defaults['medium_id'] = key($medium);
132 $defaultLocationType = CRM_Core_BAO_LocationType
::getDefault();
133 if ($defaultLocationType->id
) {
134 $defaults['location[1][location_type_id]'] = $defaultLocationType->id
;
137 $phoneType = CRM_Core_OptionGroup
::values('phone_type', FALSE, FALSE, FALSE, 'AND is_default = 1');
138 if (count($phoneType) == 1) {
139 $defaults['location[1][phone][1][phone_type_id]'] = key($phoneType);
146 * @param CRM_Case_Form_Case $form
148 static function buildQuickForm(&$form) {
149 if ($form->_context
== 'caseActivity') {
152 if ($form->_context
== 'standalone') {
153 $form->addEntityRef('client_id', ts('Client'), array('create' => TRUE, 'multiple' => $form->_allowMultiClient
), TRUE);
156 $element = $form->addSelect(
158 array('onchange' => "CRM.buildCustomData('Case', this.value);"),
162 if ($form->_caseTypeId
) {
166 $csElement = $form->addSelect('status_id', array(), TRUE);
167 if ($form->_caseStatusId
) {
168 $csElement->freeze();
171 $form->add('text', 'duration', ts('Activity Duration'), array('size' => 4, 'maxlength' => 8));
172 $form->addRule('duration', ts('Please enter the duration as number of minutes (integers only).'), 'positiveInteger');
174 if ($form->_currentlyViewedContactId
) {
175 list($displayName) = CRM_Contact_BAO_Contact
::getDisplayAndImage($form->_currentlyViewedContactId
);
176 $form->assign('clientName', $displayName);
179 $form->addDate('start_date', ts('Case Start Date'), TRUE, array('formatType' => 'activityDateTime'));
181 $form->addSelect('medium_id', array('entity' => 'activity'), TRUE);
183 // calling this field activity_location to prevent conflict with contact location fields
184 $form->add('text', 'activity_location', ts('Location'), CRM_Core_DAO
::getAttribute('CRM_Activity_DAO_Activity', 'location'));
186 $form->addWysiwyg('activity_details', ts('Details'), array('rows' => 4, 'cols' => 60), FALSE);
188 $form->addButtons(array(
191 'name' => ts('Save'),
196 'name' => ts('Save and New'),
201 'name' => ts('Cancel'),
208 * Function to process the form
214 static function beginPostProcess(&$form, &$params) {
215 if ($form->_context
== 'caseActivity') {
219 if ($form->_context
== 'standalone') {
220 $params['client_id'] = explode(',', $params['client_id']);
221 $form->_currentlyViewedContactId
= $params['client_id'][0];
224 // for open case start date should be set to current date
225 $params['start_date'] = CRM_Utils_Date
::processDate($params['start_date'], $params['start_date_time']);
226 $caseStatus = CRM_Case_PseudoConstant
::caseStatus('name');
227 // for resolved case the end date should set to now
228 if ($params['status_id'] == array_search('Closed', $caseStatus)) {
229 $params['end_date'] = $params['now'];
232 // rename activity_location param to the correct column name for activity DAO
233 $params['location'] = CRM_Utils_Array
::value('activity_location', $params);
236 CRM_Core_BAO_File
::formatAttachment(
246 * global validation rules for the form
248 * @param array $values posted values of the form
250 * @return array list of errors to be posted back to the form
254 static function formRule($fields, $files, $form) {
255 if ($form->_context
== 'caseActivity') {
264 * Function to process the form
270 static function endPostProcess(&$form, &$params) {
271 if ($form->_context
== 'caseActivity') {
275 $xmlProcessorProcess = new CRM_Case_XMLProcessor_Process();
276 $isMultiClient = $xmlProcessorProcess->getAllowMultipleCaseClients();
278 if (!$isMultiClient && !$form->_currentlyViewedContactId
) {
279 CRM_Core_Error
::fatal('Required parameter missing for OpenCase - end post processing');
282 if (!$form->_currentUserId ||
283 !$params['case_id'] ||
284 !$params['case_type']
286 CRM_Core_Error
::fatal('Required parameter missing for OpenCase - end post processing');
289 // 1. create case-contact
290 if ($isMultiClient && $form->_context
== 'standalone') {
291 foreach ($params['client_id'] as $cliId) {
293 CRM_Core_Error
::fatal('client_id cannot be empty');
295 $contactParams = array(
296 'case_id' => $params['case_id'],
297 'contact_id' => $cliId,
299 CRM_Case_BAO_Case
::addCaseToContact($contactParams);
303 $contactParams = array(
304 'case_id' => $params['case_id'],
305 'contact_id' => $form->_currentlyViewedContactId
,
307 CRM_Case_BAO_Case
::addCaseToContact($contactParams);
310 // 2. initiate xml processor
311 $xmlProcessor = new CRM_Case_XMLProcessor_Process();
313 $xmlProcessorParams = array(
314 'clientID' => $form->_currentlyViewedContactId
,
315 'creatorID' => $form->_currentUserId
,
316 'standardTimeline' => 1,
317 'activityTypeName' => 'Open Case',
318 'caseID' => $params['case_id'],
319 'subject' => $params['activity_subject'],
320 'location' => $params['location'],
321 'activity_date_time' => $params['start_date'],
322 'duration' => CRM_Utils_Array
::value('duration', $params),
323 'medium_id' => $params['medium_id'],
324 'details' => $params['activity_details'],
327 if (array_key_exists('custom', $params) && is_array($params['custom'])) {
328 $xmlProcessorParams['custom'] = $params['custom'];
331 // Add parameters for attachments
332 $numAttachments = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
, 'max_attachments');
333 for ( $i = 1; $i <= $numAttachments; $i++
) {
334 $attachName = "attachFile_$i";
335 if ( isset( $params[$attachName] ) && !empty( $params[$attachName] ) ) {
336 $xmlProcessorParams[$attachName] = $params[$attachName];
340 $xmlProcessor->run($params['case_type'], $xmlProcessorParams);
343 $params['statusMsg'] = ts('Case opened successfully.');
345 $buttonName = $form->controller
->getButtonName();
346 $session = CRM_Core_Session
::singleton();
347 if ($buttonName == $form->getButtonName('upload', 'new')) {
348 if ($form->_context
== 'standalone') {
349 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/case/add',
350 'reset=1&action=add&context=standalone'
354 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/contact/view/case',
355 "reset=1&action=add&context=case&cid={$form->_contactID}"