style fixes based upon updated phpcs Drupal standard
[civicrm-core.git] / CRM / Activity / Form / Task / Batch.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
32 */
33
34 /**
35 * This class provides the functionality for batch profile update for Activities
36 */
37 class CRM_Activity_Form_Task_Batch extends CRM_Activity_Form_Task {
38
39 /**
40 * The title of the group.
41 *
42 * @var string
43 */
44 protected $_title;
45
46 /**
47 * Maximum profile fields that will be displayed.
48 */
49 protected $_maxFields = 9;
50
51 /**
52 * Variable to store redirect path.
53 */
54 protected $_userContext;
55
56 /**
57 * Build all the data structures needed to build the form.
58 */
59 public function preProcess() {
60
61 // Initialize the task and row fields.
62 parent::preProcess();
63
64 // Get the contact read only fields to display.
65 $readOnlyFields = array_merge(array('sort_name' => ts('Added By'), 'target_sort_name' => ts('With Contact')),
66 CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
67 'contact_autocomplete_options',
68 TRUE, NULL, FALSE, 'name', TRUE
69 )
70 );
71
72 // Get the read only field data.
73 $returnProperties = array_fill_keys(array_keys($readOnlyFields), 1);
74 $contactDetails = CRM_Contact_BAO_Contact_Utils::contactDetails($this->_activityHolderIds,
75 'Activity', $returnProperties
76 );
77 $readOnlyFields['assignee_display_name'] = ts('Assigned to');
78 if (!empty($contactDetails)) {
79 foreach ($contactDetails as $key => $value) {
80 $assignee = CRM_Activity_BAO_ActivityAssignment::retrieveAssigneeIdsByActivityId($key);
81 foreach ($assignee as $keys => $values) {
82 $assigneeContact[] = CRM_Contact_BAO_Contact::displayname($values);
83 }
84 $contactDetails[$key]['assignee_display_name'] = !empty($assigneeContact) ? implode(';', $assigneeContact) : NULL;
85 }
86 }
87 $this->assign('contactDetails', $contactDetails);
88 $this->assign('readOnlyFields', $readOnlyFields);
89 }
90
91 /**
92 * Build the form object.
93 */
94 public function buildQuickForm() {
95 $ufGroupId = $this->get('ufGroupId');
96
97 if (!$ufGroupId) {
98 CRM_Core_Error::fatal('ufGroupId is missing');
99 }
100 $this->_title = ts('Update multiple activities') . ' - ' . CRM_Core_BAO_UFGroup::getTitle($ufGroupId);
101 CRM_Utils_System::setTitle($this->_title);
102
103 $this->addDefaultButtons(ts('Save'));
104 $this->_fields = array();
105 $this->_fields = CRM_Core_BAO_UFGroup::getFields($ufGroupId, FALSE, CRM_Core_Action::VIEW);
106
107 // remove file type field and then limit fields
108 $suppressFields = FALSE;
109 $removehtmlTypes = array('File', 'Autocomplete-Select');
110 foreach ($this->_fields as $name => $field) {
111 if ($cfID = CRM_Core_BAO_CustomField::getKeyID($name) &&
112 in_array($this->_fields[$name]['html_type'], $removehtmlTypes)
113 ) {
114 $suppressFields = TRUE;
115 unset($this->_fields[$name]);
116 }
117
118 // Fix to reduce size as we are using this field in grid.
119 if (is_array($field['attributes']) && !empty($this->_fields[$name]['attributes']['size']) && $this->_fields[$name]['attributes']['size'] > 19) {
120 // Shrink class to "form-text-medium".
121 $this->_fields[$name]['attributes']['size'] = 19;
122 }
123 }
124
125 $this->_fields = array_slice($this->_fields, 0, $this->_maxFields);
126
127 $this->addButtons(array(
128 array(
129 'type' => 'submit',
130 'name' => ts('Update Activities'),
131 'isDefault' => TRUE,
132 ),
133 array(
134 'type' => 'cancel',
135 'name' => ts('Cancel'),
136 ),
137 ));
138
139 $this->assign('profileTitle', $this->_title);
140 $this->assign('componentIds', $this->_activityHolderIds);
141 $fileFieldExists = FALSE;
142
143 // Load all campaigns.
144 if (array_key_exists('activity_campaign_id', $this->_fields)) {
145 $this->_componentCampaigns = array();
146 CRM_Core_PseudoConstant::populate($this->_componentCampaigns,
147 'CRM_Activity_DAO_Activity',
148 TRUE, 'campaign_id', 'id',
149 ' id IN (' . implode(' , ', array_values($this->_activityHolderIds)) . ' ) '
150 );
151 }
152
153 $customFields = CRM_Core_BAO_CustomField::getFields('Activity');
154
155 foreach ($this->_activityHolderIds as $activityId) {
156 $typeId = CRM_Core_DAO::getFieldValue("CRM_Activity_DAO_Activity", $activityId, 'activity_type_id');
157 foreach ($this->_fields as $name => $field) {
158 if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($name)) {
159 $customValue = CRM_Utils_Array::value($customFieldID, $customFields);
160 if (!empty($customValue['extends_entity_column_value'])) {
161 $entityColumnValue = explode(CRM_Core_DAO::VALUE_SEPARATOR,
162 $customValue['extends_entity_column_value']
163 );
164 }
165 if (!empty($entityColumnValue[$typeId]) ||
166 CRM_Utils_System::isNull($entityColumnValue[$typeId])
167 ) {
168 CRM_Core_BAO_UFGroup::buildProfile($this, $field, NULL, $activityId);
169 }
170 }
171 else {
172 // Handle non custom fields.
173 CRM_Core_BAO_UFGroup::buildProfile($this, $field, NULL, $activityId);
174 }
175 }
176 }
177
178 $this->assign('fields', $this->_fields);
179
180 // Don't set the status message when form is submitted.
181 // $buttonName = $this->controller->getButtonName('submit');
182
183 if ($suppressFields) {
184 CRM_Core_Session::setStatus(ts("File or Autocomplete-Select type field(s) in the selected profile are not supported for Update multiple activities."), ts('Some Fields Excluded'), 'info');
185 }
186
187 $this->addDefaultButtons(ts('Update Activities'));
188 }
189
190 /**
191 * Set default values for the form.
192 */
193 public function setDefaultValues() {
194 if (empty($this->_fields)) {
195 return;
196 }
197
198 $defaults = array();
199 foreach ($this->_activityHolderIds as $activityId) {
200 $details[$activityId] = array();
201 CRM_Core_BAO_UFGroup::setProfileDefaults(NULL, $this->_fields, $defaults, FALSE, $activityId, 'Activity');
202 }
203
204 return $defaults;
205 }
206
207 /**
208 * Process the form after the input has been submitted and validated.
209 */
210 public function postProcess() {
211 $params = $this->exportValues();
212
213 if (isset($params['field'])) {
214 foreach ($params['field'] as $key => $value) {
215
216 $value['custom'] = CRM_Core_BAO_CustomField::postProcess($value,
217 $key, 'Activity'
218 );
219 $value['id'] = $key;
220
221 if (!empty($value['activity_date_time'])) {
222 $value['activity_date_time'] = CRM_Utils_Date::processDate($value['activity_date_time'], $value['activity_date_time_time']);
223 }
224
225 if (!empty($value['activity_status_id'])) {
226 $value['status_id'] = $value['activity_status_id'];
227 }
228
229 if (!empty($value['activity_details'])) {
230 $value['details'] = $value['activity_details'];
231 }
232
233 if (!empty($value['activity_duration'])) {
234 $value['duration'] = $value['activity_duration'];
235 }
236
237 if (!empty($value['activity_location'])) {
238 $value['location'] = $value['activity_location'];
239 }
240
241 if (!empty($value['activity_subject'])) {
242 $value['subject'] = $value['activity_subject'];
243 }
244
245 $query = "
246 SELECT a.activity_type_id, ac.contact_id
247 FROM civicrm_activity a
248 JOIN civicrm_activity_contact ac ON ( ac.activity_id = a.id
249 AND ac.record_type_id = %2 )
250 WHERE a.id = %1 ";
251 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
252 $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts);
253 $params = array(1 => array($key, 'Integer'), 2 => array($sourceID, 'Integer'));
254 $dao = CRM_Core_DAO::executeQuery($query, $params);
255 $dao->fetch();
256
257 // Get Activity Type ID
258 $value['activity_type_id'] = $dao->activity_type_id;
259
260 // Get Conatct ID
261 $value['source_contact_id'] = $dao->contact_id;
262
263 // make call use API 3
264 $value['version'] = 3;
265
266 $activityId = civicrm_api('activity', 'update', $value);
267
268 // add custom field values
269 if (!empty($value['custom']) &&
270 is_array($value['custom'])
271 ) {
272 CRM_Core_BAO_CustomValueTable::store($value['custom'], 'civicrm_activity', $activityId['id']);
273 }
274 }
275 CRM_Core_Session::setStatus("", ts("Updates Saved"), "success");
276 }
277 else {
278 CRM_Core_Session::setStatus("", ts("No Updates Saved"), "info");
279 }
280 }
281
282 }