Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2014-02-05-17-16-30
[civicrm-core.git] / CRM / Activity / Form / Task / FileOnCase.php
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 provides the functionality to email a group of contacts
38 */
39 class CRM_Activity_Form_Task_FileOnCase extends CRM_Activity_Form_Task {
40
41 /**
42 * the title of the group
43 *
44 * @var string
45 */
46 protected $_title;
47
48 /**
49 * variable to store redirect path
50 *
51 */
52 protected $_userContext;
53
54 /**
55 * variable to store contact Ids
56 *
57 */
58 public $_contacts;
59
60 /**
61 * build all the data structures needed to build the form
62 *
63 * @return void
64 * @access public
65 */ function preProcess() {
66 /*
67 * initialize the task and row fields
68 */
69
70 parent::preProcess();
71 $session = CRM_Core_Session::singleton();
72 $this->_userContext = $session->readUserContext();
73
74 CRM_Utils_System::setTitle(ts('File on Case'));
75
76 $validationFailed = FALSE;
77
78 // insert validations here
79
80 // then redirect
81 if ($validationFailed) {
82 CRM_Utils_System::redirect($this->_userContext);
83 }
84 }
85
86 /**
87 * Build the form
88 *
89 * @access public
90 *
91 * @return void
92 */
93 function buildQuickForm() {
94 $this->addElement('text', 'unclosed_cases', ts('Select Case'));
95 $this->add('hidden', 'unclosed_case_id', '', array('id' => 'unclosed_case_id'));
96 $this->addDefaultButtons(ts('Continue >>'));
97 }
98
99 /**
100 * Add local and global form rules
101 *
102 * @access protected
103 *
104 * @return void
105 */
106 function addRules() {
107 $this->addFormRule(array('CRM_Activity_Form_Task_FileOnCase', 'formRule'));
108 }
109
110 /**
111 * global validation rules for the form
112 *
113 * @param array $fields posted values of the form
114 *
115 * @return array list of errors to be posted back to the form
116 * @static
117 * @access public
118 */
119 static
120 function formRule($fields) {
121 $errors = array();
122 if (empty($fields['unclosed_case_id'])) {
123 $errors['unclosed_case_id'] = ts('Case is a required field.');
124 }
125 return $errors;
126 }
127
128 /**
129 * process the form after the input has been submitted and validated
130 *
131 * @access public
132 *
133 * @return void
134 */
135
136 public function postProcess() {
137 $formparams = $this->exportValues();
138 $caseId = $formparams['unclosed_case_id'];
139 $filedActivities = 0;
140 foreach ($this->_activityHolderIds as $key => $id) {
141 $targetContactValues = $defaults = array();
142 $params = array('id' => $id);
143 CRM_Activity_BAO_Activity::retrieve($params, $defaults);
144 if (CRM_Case_BAO_Case::checkPermission($id, 'File On Case', $defaults['activity_type_id'])) {
145
146 if (!CRM_Utils_Array::crmIsEmptyArray($defaults['target_contact'])) {
147 $targetContactValues = array_combine(array_unique($defaults['target_contact']),
148 explode(';', trim($defaults['target_contact_value']))
149 );
150 $targetContactValues = implode(',', array_keys($targetContactValues));
151 }
152
153 $params = array(
154 'caseID' => $caseId,
155 'activityID' => $id,
156 'newSubject' => empty($defaults['subject']) ? '' : $defaults['subject'],
157 'targetContactIds' => $targetContactValues,
158 'mode' => 'file',
159 );
160
161 $error_msg = CRM_Activity_Page_AJAX::_convertToCaseActivity($params);
162 if (empty($error_msg['error_msg'])) {
163 $filedActivities++;
164 }
165 else {
166 CRM_Core_Session::setStatus($error_msg['error_msg'], ts("Error"), "error");
167 }
168 }
169 else {
170 CRM_Core_Session::setStatus(ts('Not permitted to file activity %1 %2.', array(
171 1 => empty($defaults['subject']) ? '' : $defaults['subject'],
172 2 => $defaults['activity_date_time'])),
173 ts("Error"), "error");
174 }
175 }
176
177 CRM_Core_Session::setStatus($filedActivities, ts("Filed Activities"), "success");
178 CRM_Core_Session::setStatus("", ts('Total Selected Activities: %1', array(1 => count($this->_activityHolderIds))), "info");
179 }
180 }
181