3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
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.
51 protected $_maxFields = 9;
54 * Variable to store redirect path.
56 protected $_userContext;
59 * Build all the data structures needed to build the form.
63 public function preProcess() {
65 * initialize the task and row fields
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
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
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);
90 $contactDetails[$key]['assignee_display_name'] = !empty($assigneeContact) ?
implode(';', $assigneeContact) : NULL;
93 $this->assign('contactDetails', $contactDetails);
94 $this->assign('readOnlyFields', $readOnlyFields);
98 * Build the form object.
100 public function buildQuickForm() {
101 $ufGroupId = $this->get('ufGroupId');
104 CRM_Core_Error
::fatal('ufGroupId is missing');
106 $this->_title
= ts('Batch Update for Activities') . ' - ' . CRM_Core_BAO_UFGroup
::getTitle($ufGroupId);
107 CRM_Utils_System
::setTitle($this->_title
);
109 $this->addDefaultButtons(ts('Save'));
110 $this->_fields
= array();
111 $this->_fields
= CRM_Core_BAO_UFGroup
::getFields($ufGroupId, FALSE, CRM_Core_Action
::VIEW
);
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)
120 $suppressFields = TRUE;
121 unset($this->_fields
[$name]);
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;
131 $this->_fields
= array_slice($this->_fields
, 0, $this->_maxFields
);
133 $this->addButtons(array(
136 'name' => ts('Update Activities'),
141 'name' => ts('Cancel'),
146 $this->assign('profileTitle', $this->_title
);
147 $this->assign('componentIds', $this->_activityHolderIds
);
148 $fileFieldExists = FALSE;
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
)) . ' ) '
160 $customFields = CRM_Core_BAO_CustomField
::getFields('Activity');
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']
172 if (!empty($entityColumnValue[$typeId]) ||
173 CRM_Utils_System
::isNull($entityColumnValue[$typeId])
175 CRM_Core_BAO_UFGroup
::buildProfile($this, $field, NULL, $activityId);
179 // handle non custom fields
180 CRM_Core_BAO_UFGroup
::buildProfile($this, $field, NULL, $activityId);
185 $this->assign('fields', $this->_fields
);
187 // don't set the status message when form is submitted.
188 // $buttonName = $this->controller->getButtonName('submit');
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');
194 $this->addDefaultButtons(ts('Update Activities'));
198 * Set default values for the form.
200 public function setDefaultValues() {
201 if (empty($this->_fields
)) {
206 foreach ($this->_activityHolderIds
as $activityId) {
207 $details[$activityId] = array();
208 CRM_Core_BAO_UFGroup
::setProfileDefaults(NULL, $this->_fields
, $defaults, FALSE, $activityId, 'Activity');
215 * Process the form after the input has been submitted and validated.
217 public function postProcess() {
218 $params = $this->exportValues();
220 if (isset($params['field'])) {
221 foreach ($params['field'] as $key => $value) {
223 $value['custom'] = CRM_Core_BAO_CustomField
::postProcess($value,
224 CRM_Core_DAO
::$_nullObject,
229 if (!empty($value['activity_date_time'])) {
230 $value['activity_date_time'] = CRM_Utils_Date
::processDate($value['activity_date_time'], $value['activity_date_time_time']);
233 if (!empty($value['activity_status_id'])) {
234 $value['status_id'] = $value['activity_status_id'];
237 if (!empty($value['activity_details'])) {
238 $value['details'] = $value['activity_details'];
241 if (!empty($value['activity_duration'])) {
242 $value['duration'] = $value['activity_duration'];
245 if (!empty($value['activity_location'])) {
246 $value['location'] = $value['activity_location'];
249 if (!empty($value['activity_subject'])) {
250 $value['subject'] = $value['activity_subject'];
254 SELECT a.activity_type_id, ac.contact_id
255 FROM civicrm_activity a
256 JOIN civicrm_activity_contact ac ON ( ac.activity_id = a.id
257 AND ac.record_type_id = %2 )
259 $activityContacts = CRM_Core_OptionGroup
::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
260 $sourceID = CRM_Utils_Array
::key('Activity Source', $activityContacts);
261 $params = array(1 => array($key, 'Integer'), 2 => array($sourceID, 'Integer'));
262 $dao = CRM_Core_DAO
::executeQuery($query, $params);
265 // Get Activity Type ID
266 $value['activity_type_id'] = $dao->activity_type_id
;
269 $value['source_contact_id'] = $dao->contact_id
;
271 // make call use API 3
272 $value['version'] = 3;
274 $activityId = civicrm_api('activity', 'update', $value);
276 // add custom field values
277 if (!empty($value['custom']) &&
278 is_array($value['custom'])
280 CRM_Core_BAO_CustomValueTable
::store($value['custom'], 'civicrm_activity', $activityId['id']);
283 CRM_Core_Session
::setStatus("", ts("Updates Saved"), "success");
286 CRM_Core_Session
::setStatus("", ts("No Updates Saved"), "info");