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