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