3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2013
37 * This class provides the functionality for batch profile update for Activities
39 class CRM_Activity_Form_Task_Batch
extends CRM_Activity_Form_Task
{
42 * the title of the group
49 * maximum profile fields that will be displayed
52 protected $_maxFields = 9;
55 * variable to store redirect path
58 protected $_userContext;
61 * build all the data structures needed to build the form
66 function preProcess() {
68 * initialize the task and row fields
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
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
86 $this->assign('contactDetails', $contactDetails);
87 $this->assign('readOnlyFields', $readOnlyFields);
97 function buildQuickForm() {
98 $ufGroupId = $this->get('ufGroupId');
101 CRM_Core_Error
::fatal('ufGroupId is missing');
103 $this->_title
= ts('Batch Update for Activities') . ' - ' . CRM_Core_BAO_UFGroup
::getTitle($ufGroupId);
104 CRM_Utils_System
::setTitle($this->_title
);
106 $this->addDefaultButtons(ts('Save'));
107 $this->_fields
= array();
108 $this->_fields
= CRM_Core_BAO_UFGroup
::getFields($ufGroupId, FALSE, CRM_Core_Action
::VIEW
);
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)
117 $suppressFields = TRUE;
118 unset($this->_fields
[$name]);
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;
128 $this->_fields
= array_slice($this->_fields
, 0, $this->_maxFields
);
130 $this->addButtons(array(
133 'name' => ts('Update Activities'),
138 'name' => ts('Cancel'),
144 $this->assign('profileTitle', $this->_title
);
145 $this->assign('componentIds', $this->_activityHolderIds
);
146 $fileFieldExists = FALSE;
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
)) . ' ) '
158 $customFields = CRM_Core_BAO_CustomField
::getFields('Activity');
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);
165 if (!empty($customValue['extends_entity_column_value'])) {
166 $entityColumnValue = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
167 $customValue['extends_entity_column_value']
170 if (!empty($entityColumnValue[$typeId]) ||
171 CRM_Utils_System
::isNull($entityColumnValue[$typeId])
173 CRM_Core_BAO_UFGroup
::buildProfile($this, $field, NULL, $activityId);
177 // handle non custom fields
178 CRM_Core_BAO_UFGroup
::buildProfile($this, $field, NULL, $activityId);
183 $this->assign('fields', $this->_fields
);
185 // don't set the status message when form is submitted.
186 // $buttonName = $this->controller->getButtonName('submit');
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");
192 $this->addDefaultButtons(ts('Update Activities'));
196 * This function sets the default values for the form.
202 function setDefaultValues() {
203 if (empty($this->_fields
)) {
208 foreach ($this->_activityHolderIds
as $activityId) {
209 $details[$activityId] = array();
210 CRM_Core_BAO_UFGroup
::setProfileDefaults(NULL, $this->_fields
, $defaults, FALSE, $activityId, 'Activity');
217 * process the form after the input has been submitted and validated
223 public function postProcess() {
224 $params = $this->exportValues();
226 if (isset($params['field'])) {
227 foreach ($params['field'] as $key => $value) {
229 $value['custom'] = CRM_Core_BAO_CustomField
::postProcess($value,
230 CRM_Core_DAO
::$_nullObject,
235 if ($value['activity_date_time']) {
236 $value['activity_date_time'] = CRM_Utils_Date
::processDate($value['activity_date_time'], $value['activity_date_time_time']);
239 if (!empty($value['activity_status_id'])) {
240 $value['status_id'] = $value['activity_status_id'];
243 if (!empty($value['activity_details'])) {
244 $value['details'] = $value['activity_details'];
247 if (!empty($value['activity_duration'])) {
248 $value['duration'] = $value['activity_duration'];
251 if (!empty($value['activity_location'])) {
252 $value['location'] = $value['activity_location'];
255 if (!empty($value['activity_subject'])) {
256 $value['subject'] = $value['activity_subject'];
260 SELECT a.activity_type_id, ac.contact_id
261 FROM civicrm_activity a
262 JOIN civicrm_activity_contact ac ON ( ac.activity_id = a.id
263 AND ac.record_type_id = %2 )
265 $activityContacts = CRM_Core_OptionGroup
::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
266 $sourceID = CRM_Utils_Array
::key('Activity Source', $activityContacts);
267 $params = array(1 => array($key, 'Integer'), 2 => array($sourceID, 'Integer'));
268 $dao = CRM_Core_DAO
::executeQuery($query, $params);
271 // Get Activity Type ID
272 $value['activity_type_id'] = $dao->activity_type_id
;
275 $value['source_contact_id'] = $dao->contact_id
;
277 // make call use API 3
278 $value['version'] = 3;
280 $activityId = civicrm_api('activity', 'update', $value);
282 // add custom field values
283 if (!empty($value['custom']) &&
284 is_array($value['custom'])
286 CRM_Core_BAO_CustomValueTable
::store($value['custom'], 'civicrm_activity', $activityId['id']);
289 CRM_Core_Session
::setStatus("", ts("Updates Saved"), "success");
292 CRM_Core_Session
::setStatus("", ts("No Updates Saved"), "info");