Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
0f03f337 | 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
0f03f337 | 31 | * @copyright CiviCRM LLC (c) 2004-2017 |
6a488035 TO |
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 | /** | |
fe482240 | 40 | * The title of the group. |
6a488035 TO |
41 | * |
42 | * @var string | |
43 | */ | |
44 | protected $_title; | |
45 | ||
46 | /** | |
fe482240 | 47 | * Maximum profile fields that will be displayed. |
6a488035 TO |
48 | */ |
49 | protected $_maxFields = 9; | |
50 | ||
51 | /** | |
fe482240 | 52 | * Variable to store redirect path. |
6a488035 TO |
53 | */ |
54 | protected $_userContext; | |
55 | ||
56 | /** | |
fe482240 | 57 | * Build all the data structures needed to build the form. |
6a488035 | 58 | */ |
00be9182 | 59 | public function preProcess() { |
6a488035 | 60 | |
7808aae6 | 61 | // Initialize the task and row fields. |
6a488035 TO |
62 | parent::preProcess(); |
63 | ||
7808aae6 | 64 | // Get the contact read only fields to display. |
6a488035 TO |
65 | $readOnlyFields = array_merge(array('sort_name' => ts('Added By'), 'target_sort_name' => ts('With Contact')), |
66 | CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, | |
67 | 'contact_autocomplete_options', | |
68 | TRUE, NULL, FALSE, 'name', TRUE | |
69 | ) | |
70 | ); | |
71 | ||
7808aae6 | 72 | // Get the read only field data. |
6a488035 TO |
73 | $returnProperties = array_fill_keys(array_keys($readOnlyFields), 1); |
74 | $contactDetails = CRM_Contact_BAO_Contact_Utils::contactDetails($this->_activityHolderIds, | |
75 | 'Activity', $returnProperties | |
76 | ); | |
3bd48a28 | 77 | $readOnlyFields['assignee_display_name'] = ts('Assigned to'); |
22e263ad | 78 | if (!empty($contactDetails)) { |
9b873358 | 79 | foreach ($contactDetails as $key => $value) { |
bb03a208 | 80 | $assignee = CRM_Activity_BAO_ActivityAssignment::retrieveAssigneeIdsByActivityId($key); |
363544d7 | 81 | foreach ($assignee as $values) { |
82 | $assigneeContact[] = CRM_Contact_BAO_Contact::displayName($values); | |
bb03a208 RK |
83 | } |
84 | $contactDetails[$key]['assignee_display_name'] = !empty($assigneeContact) ? implode(';', $assigneeContact) : NULL; | |
85 | } | |
86 | } | |
6a488035 TO |
87 | $this->assign('contactDetails', $contactDetails); |
88 | $this->assign('readOnlyFields', $readOnlyFields); | |
89 | } | |
90 | ||
91 | /** | |
2e2605fe | 92 | * Build the form object. |
6a488035 | 93 | */ |
00be9182 | 94 | public function buildQuickForm() { |
6a488035 TO |
95 | $ufGroupId = $this->get('ufGroupId'); |
96 | ||
97 | if (!$ufGroupId) { | |
2a66c165 | 98 | throw new CRM_Core_Exception('The profile id is missing'); |
6a488035 | 99 | } |
b581842f | 100 | $this->_title = ts('Update multiple activities') . ' - ' . CRM_Core_BAO_UFGroup::getTitle($ufGroupId); |
6a488035 TO |
101 | CRM_Utils_System::setTitle($this->_title); |
102 | ||
103 | $this->addDefaultButtons(ts('Save')); | |
104 | $this->_fields = array(); | |
105 | $this->_fields = CRM_Core_BAO_UFGroup::getFields($ufGroupId, FALSE, CRM_Core_Action::VIEW); | |
106 | ||
107 | // remove file type field and then limit fields | |
108 | $suppressFields = FALSE; | |
657c89d9 | 109 | $removehtmlTypes = array('File'); |
6a488035 | 110 | foreach ($this->_fields as $name => $field) { |
2a66c165 | 111 | if (CRM_Core_BAO_CustomField::getKeyID($name) && |
6a488035 TO |
112 | in_array($this->_fields[$name]['html_type'], $removehtmlTypes) |
113 | ) { | |
114 | $suppressFields = TRUE; | |
115 | unset($this->_fields[$name]); | |
116 | } | |
117 | ||
7808aae6 | 118 | // Fix to reduce size as we are using this field in grid. |
bb03a208 | 119 | if (is_array($field['attributes']) && !empty($this->_fields[$name]['attributes']['size']) && $this->_fields[$name]['attributes']['size'] > 19) { |
7808aae6 | 120 | // Shrink class to "form-text-medium". |
6a488035 TO |
121 | $this->_fields[$name]['attributes']['size'] = 19; |
122 | } | |
123 | } | |
124 | ||
125 | $this->_fields = array_slice($this->_fields, 0, $this->_maxFields); | |
126 | ||
127 | $this->addButtons(array( | |
c5c263ca AH |
128 | array( |
129 | 'type' => 'submit', | |
130 | 'name' => ts('Update Activities'), | |
131 | 'isDefault' => TRUE, | |
132 | ), | |
133 | array( | |
134 | 'type' => 'cancel', | |
135 | 'name' => ts('Cancel'), | |
136 | ), | |
137 | )); | |
6a488035 | 138 | |
6a488035 TO |
139 | $this->assign('profileTitle', $this->_title); |
140 | $this->assign('componentIds', $this->_activityHolderIds); | |
6a488035 | 141 | |
7808aae6 | 142 | // Load all campaigns. |
6a488035 TO |
143 | if (array_key_exists('activity_campaign_id', $this->_fields)) { |
144 | $this->_componentCampaigns = array(); | |
145 | CRM_Core_PseudoConstant::populate($this->_componentCampaigns, | |
146 | 'CRM_Activity_DAO_Activity', | |
147 | TRUE, 'campaign_id', 'id', | |
148 | ' id IN (' . implode(' , ', array_values($this->_activityHolderIds)) . ' ) ' | |
149 | ); | |
150 | } | |
151 | ||
152 | $customFields = CRM_Core_BAO_CustomField::getFields('Activity'); | |
2a66c165 | 153 | // It is possible to have fields that are required in CiviCRM not be required in the |
154 | // profile. Overriding that here. Perhaps a better approach would be to | |
155 | // make them required in the schema & read that up through getFields functionality. | |
156 | $requiredFields = array('activity_date_time'); | |
6a488035 TO |
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); | |
a7488080 | 163 | if (!empty($customValue['extends_entity_column_value'])) { |
6a488035 TO |
164 | $entityColumnValue = explode(CRM_Core_DAO::VALUE_SEPARATOR, |
165 | $customValue['extends_entity_column_value'] | |
166 | ); | |
167 | } | |
a7488080 | 168 | if (!empty($entityColumnValue[$typeId]) || |
6a488035 TO |
169 | CRM_Utils_System::isNull($entityColumnValue[$typeId]) |
170 | ) { | |
171 | CRM_Core_BAO_UFGroup::buildProfile($this, $field, NULL, $activityId); | |
172 | } | |
173 | } | |
174 | else { | |
7808aae6 | 175 | // Handle non custom fields. |
2a66c165 | 176 | if (in_array($field['name'], $requiredFields)) { |
177 | $field['is_required'] = TRUE; | |
178 | } | |
6a488035 TO |
179 | CRM_Core_BAO_UFGroup::buildProfile($this, $field, NULL, $activityId); |
180 | } | |
181 | } | |
182 | } | |
183 | ||
184 | $this->assign('fields', $this->_fields); | |
185 | ||
7808aae6 | 186 | // Don't set the status message when form is submitted. |
6a488035 TO |
187 | // $buttonName = $this->controller->getButtonName('submit'); |
188 | ||
189 | if ($suppressFields) { | |
657c89d9 | 190 | CRM_Core_Session::setStatus(ts("File type field(s) in the selected profile are not supported for Update multiple activities."), ts('Some Fields Excluded'), 'info'); |
6a488035 TO |
191 | } |
192 | ||
193 | $this->addDefaultButtons(ts('Update Activities')); | |
194 | } | |
195 | ||
196 | /** | |
c490a46a | 197 | * Set default values for the form. |
6a488035 | 198 | */ |
00be9182 | 199 | public function setDefaultValues() { |
6a488035 TO |
200 | if (empty($this->_fields)) { |
201 | return; | |
202 | } | |
203 | ||
204 | $defaults = array(); | |
205 | foreach ($this->_activityHolderIds as $activityId) { | |
6a488035 TO |
206 | CRM_Core_BAO_UFGroup::setProfileDefaults(NULL, $this->_fields, $defaults, FALSE, $activityId, 'Activity'); |
207 | } | |
208 | ||
209 | return $defaults; | |
210 | } | |
211 | ||
212 | /** | |
fe482240 | 213 | * Process the form after the input has been submitted and validated. |
6a488035 TO |
214 | */ |
215 | public function postProcess() { | |
216 | $params = $this->exportValues(); | |
217 | ||
218 | if (isset($params['field'])) { | |
219 | foreach ($params['field'] as $key => $value) { | |
220 | ||
221 | $value['custom'] = CRM_Core_BAO_CustomField::postProcess($value, | |
6a488035 TO |
222 | $key, 'Activity' |
223 | ); | |
224 | $value['id'] = $key; | |
225 | ||
a7488080 | 226 | if (!empty($value['activity_status_id'])) { |
6a488035 TO |
227 | $value['status_id'] = $value['activity_status_id']; |
228 | } | |
229 | ||
a7488080 | 230 | if (!empty($value['activity_details'])) { |
6a488035 TO |
231 | $value['details'] = $value['activity_details']; |
232 | } | |
233 | ||
a7488080 | 234 | if (!empty($value['activity_location'])) { |
6a488035 TO |
235 | $value['location'] = $value['activity_location']; |
236 | } | |
237 | ||
a7488080 | 238 | if (!empty($value['activity_subject'])) { |
6a488035 TO |
239 | $value['subject'] = $value['activity_subject']; |
240 | } | |
241 | ||
2a66c165 | 242 | $activityId = civicrm_api3('activity', 'create', $value); |
6a488035 | 243 | |
2a66c165 | 244 | // @todo this would be done by the api call above if the parames were passed through. |
a7488080 | 245 | if (!empty($value['custom']) && |
6a488035 TO |
246 | is_array($value['custom']) |
247 | ) { | |
248 | CRM_Core_BAO_CustomValueTable::store($value['custom'], 'civicrm_activity', $activityId['id']); | |
249 | } | |
250 | } | |
251 | CRM_Core_Session::setStatus("", ts("Updates Saved"), "success"); | |
252 | } | |
253 | else { | |
254 | CRM_Core_Session::setStatus("", ts("No Updates Saved"), "info"); | |
255 | } | |
256 | } | |
96025800 | 257 | |
6a488035 | 258 | } |