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