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