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