copyright and version fixes
[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 /**
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 */
52 protected $_maxFields = 9;
53
54 /**
55 * variable to store redirect path
56 *
57 */
58 protected $_userContext;
59
60 /**
61 * build all the data structures needed to build the form
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 );
86 $this->assign('contactDetails', $contactDetails);
87 $this->assign('readOnlyFields', $readOnlyFields);
88 }
89
90 /**
91 * Build the form
92 *
93 * @access public
94 *
95 * @return void
96 */
97 function buildQuickForm() {
98 $ufGroupId = $this->get('ufGroupId');
99
100 if (!$ufGroupId) {
101 CRM_Core_Error::fatal('ufGroupId is missing');
102 }
103 $this->_title = ts('Batch Update for Activities') . ' - ' . CRM_Core_BAO_UFGroup::getTitle($ufGroupId);
104 CRM_Utils_System::setTitle($this->_title);
105
106 $this->addDefaultButtons(ts('Save'));
107 $this->_fields = array();
108 $this->_fields = CRM_Core_BAO_UFGroup::getFields($ufGroupId, FALSE, CRM_Core_Action::VIEW);
109
110 // remove file type field and then limit fields
111 $suppressFields = FALSE;
112 $removehtmlTypes = array('File', 'Autocomplete-Select');
113 foreach ($this->_fields as $name => $field) {
114 if ($cfID = CRM_Core_BAO_CustomField::getKeyID($name) &&
115 in_array($this->_fields[$name]['html_type'], $removehtmlTypes)
116 ) {
117 $suppressFields = TRUE;
118 unset($this->_fields[$name]);
119 }
120
121 //fix to reduce size as we are using this field in grid
122 if (is_array($field['attributes']) && $this->_fields[$name]['attributes']['size'] > 19) {
123 //shrink class to "form-text-medium"
124 $this->_fields[$name]['attributes']['size'] = 19;
125 }
126 }
127
128 $this->_fields = array_slice($this->_fields, 0, $this->_maxFields);
129
130 $this->addButtons(array(
131 array(
132 'type' => 'submit',
133 'name' => ts('Update Activities'),
134 'isDefault' => TRUE,
135 ),
136 array(
137 'type' => 'cancel',
138 'name' => ts('Cancel'),
139 ),
140 )
141 );
142
143
144 $this->assign('profileTitle', $this->_title);
145 $this->assign('componentIds', $this->_activityHolderIds);
146 $fileFieldExists = FALSE;
147
148 //load all campaigns.
149 if (array_key_exists('activity_campaign_id', $this->_fields)) {
150 $this->_componentCampaigns = array();
151 CRM_Core_PseudoConstant::populate($this->_componentCampaigns,
152 'CRM_Activity_DAO_Activity',
153 TRUE, 'campaign_id', 'id',
154 ' id IN (' . implode(' , ', array_values($this->_activityHolderIds)) . ' ) '
155 );
156 }
157
158 $customFields = CRM_Core_BAO_CustomField::getFields('Activity');
159
160 foreach ($this->_activityHolderIds as $activityId) {
161 $typeId = CRM_Core_DAO::getFieldValue("CRM_Activity_DAO_Activity", $activityId, 'activity_type_id');
162 foreach ($this->_fields as $name => $field) {
163 if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($name)) {
164 $customValue = CRM_Utils_Array::value($customFieldID, $customFields);
a7488080 165 if (!empty($customValue['extends_entity_column_value'])) {
6a488035
TO
166 $entityColumnValue = explode(CRM_Core_DAO::VALUE_SEPARATOR,
167 $customValue['extends_entity_column_value']
168 );
169 }
a7488080 170 if (!empty($entityColumnValue[$typeId]) ||
6a488035
TO
171 CRM_Utils_System::isNull($entityColumnValue[$typeId])
172 ) {
173 CRM_Core_BAO_UFGroup::buildProfile($this, $field, NULL, $activityId);
174 }
175 }
176 else {
177 // handle non custom fields
178 CRM_Core_BAO_UFGroup::buildProfile($this, $field, NULL, $activityId);
179 }
180 }
181 }
182
183 $this->assign('fields', $this->_fields);
184
185 // don't set the status message when form is submitted.
186 // $buttonName = $this->controller->getButtonName('submit');
187
188 if ($suppressFields) {
189 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 have been excluded"), "info");
190 }
191
192 $this->addDefaultButtons(ts('Update Activities'));
193 }
194
195 /**
196 * This function sets the default values for the form.
197 *
198 * @access public
199 *
355ba699 200 * @return void
6a488035
TO
201 */
202 function setDefaultValues() {
203 if (empty($this->_fields)) {
204 return;
205 }
206
207 $defaults = array();
208 foreach ($this->_activityHolderIds as $activityId) {
209 $details[$activityId] = array();
210 CRM_Core_BAO_UFGroup::setProfileDefaults(NULL, $this->_fields, $defaults, FALSE, $activityId, 'Activity');
211 }
212
213 return $defaults;
214 }
215
216 /**
217 * process the form after the input has been submitted and validated
218 *
219 * @access public
220 *
355ba699 221 * @return void
6a488035
TO
222 */
223 public function postProcess() {
224 $params = $this->exportValues();
225
226 if (isset($params['field'])) {
227 foreach ($params['field'] as $key => $value) {
228
229 $value['custom'] = CRM_Core_BAO_CustomField::postProcess($value,
230 CRM_Core_DAO::$_nullObject,
231 $key, 'Activity'
232 );
233 $value['id'] = $key;
234
235 if ($value['activity_date_time']) {
236 $value['activity_date_time'] = CRM_Utils_Date::processDate($value['activity_date_time'], $value['activity_date_time_time']);
237 }
238
a7488080 239 if (!empty($value['activity_status_id'])) {
6a488035
TO
240 $value['status_id'] = $value['activity_status_id'];
241 }
242
a7488080 243 if (!empty($value['activity_details'])) {
6a488035
TO
244 $value['details'] = $value['activity_details'];
245 }
246
a7488080 247 if (!empty($value['activity_duration'])) {
6a488035
TO
248 $value['duration'] = $value['activity_duration'];
249 }
250
a7488080 251 if (!empty($value['activity_location'])) {
6a488035
TO
252 $value['location'] = $value['activity_location'];
253 }
254
a7488080 255 if (!empty($value['activity_subject'])) {
6a488035
TO
256 $value['subject'] = $value['activity_subject'];
257 }
258
259 $query = "
1adb1bc9 260SELECT a.activity_type_id, ac.contact_id
261FROM civicrm_activity a
262JOIN civicrm_activity_contact ac ON ( ac.activity_id = a.id
263AND ac.record_type_id = %2 )
264WHERE a.id = %1 ";
c1a315f4 265 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
1adb1bc9 266 $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts);
267 $params = array(1 => array($key, 'Integer'), 2 => array($sourceID, 'Integer'));
6a488035
TO
268 $dao = CRM_Core_DAO::executeQuery($query, $params);
269 $dao->fetch();
270
271 // Get Activity Type ID
272 $value['activity_type_id'] = $dao->activity_type_id;
273
274 // Get Conatct ID
1adb1bc9 275 $value['source_contact_id'] = $dao->contact_id;
6a488035
TO
276
277 // make call use API 3
278 $value['version'] = 3;
279
280 $activityId = civicrm_api('activity', 'update', $value);
281
282 // add custom field values
a7488080 283 if (!empty($value['custom']) &&
6a488035
TO
284 is_array($value['custom'])
285 ) {
286 CRM_Core_BAO_CustomValueTable::store($value['custom'], 'civicrm_activity', $activityId['id']);
287 }
288 }
289 CRM_Core_Session::setStatus("", ts("Updates Saved"), "success");
290 }
291 else {
292 CRM_Core_Session::setStatus("", ts("No Updates Saved"), "info");
293 }
294 }
295 //end of function
296}
297