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