Merge pull request #18059 from civicrm/5.28
[civicrm-core.git] / CRM / Profile / Page / MultipleRecordFieldsListing.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17 class CRM_Profile_Page_MultipleRecordFieldsListing extends CRM_Core_Page_Basic {
18
19 /**
20 * The action links that we need to display for the browse screen.
21 *
22 * @var array
23 */
24 public static $_links = NULL;
25
26 protected $_fields = NULL;
27
28 protected $_profileId = NULL;
29
30 public $_contactId = NULL;
31
32 public $_customGroupTitle = NULL;
33
34 public $_pageViewType = NULL;
35
36 public $_contactType = NULL;
37
38 /**
39 * Get BAO Name.
40 *
41 * @return string
42 * Classname of BAO.
43 */
44 public function getBAOName() {
45 return '';
46 }
47
48 /**
49 * Get action Links.
50 *
51 * @return array
52 * (reference) of action links
53 */
54 public function &links() {
55 if (!isset(self::$_links[$this->_pageViewType])) {
56 // helper variable for nicer formatting
57 $links = [];
58
59 $view = array_search(CRM_Core_Action::VIEW, CRM_Core_Action::$_names);
60 $update = array_search(CRM_Core_Action::UPDATE, CRM_Core_Action::$_names);
61 $delete = array_search(CRM_Core_Action::DELETE, CRM_Core_Action::$_names);
62
63 // names and titles
64 $links[CRM_Core_Action::VIEW] = [
65 'name' => ts('View'),
66 'title' => ts('View %1', [1 => $this->_customGroupTitle . ' record']),
67 ];
68
69 $links[CRM_Core_Action::UPDATE] = [
70 'name' => ts('Edit'),
71 'title' => ts('Edit %1', [1 => $this->_customGroupTitle . ' record']),
72 ];
73
74 $links[CRM_Core_Action::DELETE] = [
75 'name' => ts('Delete'),
76 'title' => ts('Delete %1', [1 => $this->_customGroupTitle . ' record']),
77 ];
78
79 // urls and queryStrings
80 if ($this->_pageViewType == 'profileDataView') {
81 $links[CRM_Core_Action::VIEW]['url'] = 'civicrm/profile/view';
82 $links[CRM_Core_Action::VIEW]['qs'] = "reset=1&id=%%id%%&recordId=%%recordId%%&gid=%%gid%%&multiRecord={$view}";
83
84 $links[CRM_Core_Action::UPDATE]['url'] = 'civicrm/profile/edit';
85 $links[CRM_Core_Action::UPDATE]['qs'] = "reset=1&id=%%id%%&recordId=%%recordId%%&gid=%%gid%%&multiRecord={$update}";
86
87 $links[CRM_Core_Action::DELETE]['url'] = 'civicrm/profile/edit';
88 $links[CRM_Core_Action::DELETE]['qs'] = "reset=1&id=%%id%%&recordId=%%recordId%%&gid=%%gid%%&multiRecord={$delete}";
89
90 }
91 elseif ($this->_pageViewType == 'customDataView') {
92 // custom data specific view links
93 $links[CRM_Core_Action::VIEW]['url'] = 'civicrm/contact/view/cd';
94 $links[CRM_Core_Action::VIEW]['qs'] = 'reset=1&gid=%%gid%%&cid=%%cid%%&recId=%%recId%%&cgcount=%%cgcount%%&multiRecordDisplay=single&mode=view';
95
96 // custom data specific update links
97 $links[CRM_Core_Action::UPDATE]['url'] = 'civicrm/contact/view/cd/edit';
98 $links[CRM_Core_Action::UPDATE]['qs'] = 'reset=1&type=%%type%%&groupID=%%groupID%%&entityID=%%entityID%%&cgcount=%%cgcount%%&multiRecordDisplay=single&mode=edit';
99 // NOTE : links for DELETE action for customDataView is handled in browse
100
101 // copy action
102 $links[CRM_Core_Action::COPY] = [
103 'name' => ts('Copy'),
104 'title' => ts('Copy %1', [1 => $this->_customGroupTitle . ' record']),
105 'url' => 'civicrm/contact/view/cd/edit',
106 'qs' => 'reset=1&type=%%type%%&groupID=%%groupID%%&entityID=%%entityID%%&cgcount=%%newCgCount%%&multiRecordDisplay=single&copyValueId=%%cgcount%%&mode=copy',
107 ];
108 }
109
110 self::$_links[$this->_pageViewType] = $links;
111 }
112 return self::$_links[$this->_pageViewType];
113 }
114
115 /**
116 * Run the page.
117 *
118 * This method is called after the page is created. It checks for the type
119 * of action and executes that action. Finally it calls the parent's run
120 * method.
121 *
122 */
123 public function run() {
124 // get the requested action, default to 'browse'
125 $action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, FALSE);
126
127 // assign vars to templates
128 $this->assign('action', $action);
129 $profileId = CRM_Utils_Request::retrieve('profileId', 'Positive', $this, FALSE);
130 if (!is_array($profileId) && is_numeric($profileId)) {
131 $this->_profileId = $profileId;
132 }
133
134 $this->_contactId = CRM_Utils_Request::retrieve('contactId', 'Positive', $this, FALSE);
135 $this->_pageViewType = CRM_Utils_Request::retrieve('pageViewType', 'Positive', $this, FALSE, 'profileDataView');
136 $this->_customGroupId = CRM_Utils_Request::retrieve('customGroupId', 'Positive', $this, FALSE, 0);
137 $this->_contactType = CRM_Utils_Request::retrieve('contactType', 'String', $this, FALSE);
138 if ($action & CRM_Core_Action::BROWSE) {
139 //browse
140 $this->browse();
141 return;
142 }
143 // parent run
144 return parent::run();
145 }
146
147 /**
148 * Browse the listing.
149 *
150 */
151 public function browse() {
152 $dateFields = NULL;
153 $newCgCount = $cgcount = 0;
154 $attributes = $result = $headerAttr = [];
155 $dateFieldsVals = NULL;
156 if ($this->_pageViewType == 'profileDataView' && $this->_profileId) {
157 $fields = CRM_Core_BAO_UFGroup::getFields($this->_profileId, FALSE, NULL,
158 NULL, NULL,
159 FALSE, NULL,
160 FALSE,
161 NULL,
162 CRM_Core_Permission::EDIT
163 );
164 $multiRecordFields = [];
165 $fieldIDs = NULL;
166 $result = NULL;
167 $multiRecordFieldsWithSummaryListing = CRM_Core_BAO_UFGroup::shiftMultiRecordFields($fields, $multiRecordFields, TRUE);
168
169 $multiFieldId = CRM_Core_BAO_CustomField::getKeyID(key($multiRecordFields));
170 $customGroupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $multiFieldId, 'custom_group_id');
171 $reached = CRM_Core_BAO_CustomGroup::hasReachedMaxLimit($customGroupId, $this->_contactId);
172 if (!$reached) {
173 $this->assign('contactId', $this->_contactId);
174 $this->assign('gid', $this->_profileId);
175 }
176 $this->assign('reachedMax', $reached);
177
178 if ($multiRecordFieldsWithSummaryListing && !empty($multiRecordFieldsWithSummaryListing)) {
179 $fieldIDs = array_keys($multiRecordFieldsWithSummaryListing);
180 }
181 }
182 elseif ($this->_pageViewType == 'customDataView') {
183 // require custom group id for _pageViewType of customDataView
184 $customGroupId = $this->_customGroupId;
185 $this->assign('customGroupId', $customGroupId);
186 $reached = CRM_Core_BAO_CustomGroup::hasReachedMaxLimit($customGroupId, $this->_contactId);
187 if (!$reached) {
188 $this->assign('contactId', $this->_contactId);
189 $this->assign('ctype', $this->_contactType);
190 }
191 $this->assign('reachedMax', $reached);
192 // custom group info : this consists of the field title of group fields
193 $groupDetail = CRM_Core_BAO_CustomGroup::getGroupDetail($customGroupId, NULL, CRM_Core_DAO::$_nullObject, TRUE);
194 // field ids of fields in_selector for the custom group id provided
195 $fieldIDs = array_keys($groupDetail[$customGroupId]['fields']);
196 // field labels for headers
197 $fieldLabels = $groupDetail[$customGroupId]['fields'];
198
199 // from the above customGroupInfo we can get $this->_customGroupTitle
200 $this->_customGroupTitle = $groupDetail[$customGroupId]['title'];
201 }
202 if (!empty($fieldIDs) && $this->_contactId) {
203 $options = [];
204 $returnProperities = [
205 'html_type',
206 'data_type',
207 'date_format',
208 'time_format',
209 'default_value',
210 'is_required',
211 'is_view',
212 ];
213 foreach ($fieldIDs as $key => $fieldID) {
214 $fieldIDs[$key] = !is_numeric($fieldID) ? CRM_Core_BAO_CustomField::getKeyID($fieldID) : $fieldID;
215 $param = ['id' => $fieldIDs[$key]];
216 $returnValues = [];
217 CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_CustomField', $param, $returnValues, $returnProperities);
218 if ($returnValues['data_type'] == 'Date') {
219 $dateFields[$fieldIDs[$key]] = 1;
220 $actualPHPFormats = CRM_Utils_Date::datePluginToPHPFormats();
221 $dateFormat = (array) CRM_Utils_Array::value($returnValues['date_format'], $actualPHPFormats);
222 $timeFormat = $returnValues['time_format'] ?? NULL;
223 }
224
225 $optionValuePairs = CRM_Core_BAO_CustomOption::getCustomOption($fieldIDs[$key]);
226 if (!empty($optionValuePairs)) {
227 foreach ($optionValuePairs as $optionPairs) {
228 $options[$fieldIDs[$key]][$optionPairs['value']] = $optionPairs['label'];
229 }
230 }
231
232 $options[$fieldIDs[$key]]['attributes']['html_type'] = $returnValues['html_type'];
233 $options[$fieldIDs[$key]]['attributes']['data_type'] = $returnValues['data_type'];
234 $options[$fieldIDs[$key]]['attributes']['is_required'] = !empty($returnValues['is_required']);
235 $options[$fieldIDs[$key]]['attributes']['default_value'] = $returnValues['default_value'] ?? NULL;
236 $options[$fieldIDs[$key]]['attributes']['is_view'] = $returnValues['is_view'] ?? NULL;
237
238 $options[$fieldIDs[$key]]['attributes']['format']
239 = $options[$fieldIDs[$key]]['attributes']['date_format'] = $returnValues['date_format'] ?? NULL;
240 $options[$fieldIDs[$key]]['attributes']['time_format'] = $returnValues['time_format'] ?? NULL;
241 }
242 $linkAction = array_sum(array_keys($this->links()));
243 }
244
245 if (!empty($fieldIDs) && $this->_contactId) {
246 $DTparams = !empty($this->_DTparams) ? $this->_DTparams : NULL;
247 // commonly used for both views i.e profile listing view (profileDataView) and custom data listing view (customDataView)
248 $result = CRM_Core_BAO_CustomValueTable::getEntityValues($this->_contactId, NULL, $fieldIDs, TRUE, $DTparams);
249 $resultCount = !empty($result['count']) ? $result['count'] : count($result);
250 $sortedResult = !empty($result['sortedResult']) ? $result['sortedResult'] : [];
251 unset($result['count']);
252 unset($result['sortedResult']);
253
254 if ($this->_pageViewType == 'profileDataView') {
255 if (!empty($fieldIDs)) {
256 //get the group info of multi rec fields in listing view
257 $fieldInput = $fieldIDs;
258 $fieldIdInput = $fieldIDs[0];
259 }
260 else {
261 //if no listing fields exist, take the group title for display
262 $nonListingFieldIds = array_keys($multiRecordFields);
263 $singleField = CRM_Core_BAO_CustomField::getKeyID($nonListingFieldIds[0]);
264 $fieldIdInput = $singleField;
265 $singleField = [$singleField];
266 $fieldInput = $singleField;
267 }
268 $customGroupInfo = CRM_Core_BAO_CustomGroup::getGroupTitles($fieldInput);
269 $this->_customGroupTitle = $customGroupInfo[$fieldIdInput]['groupTitle'];
270 }
271 // $cgcount is defined before 'if' condition as entity may have no record
272 // and $cgcount is used to build new record url
273 $cgcount = 1;
274 $newCgCount = (!$reached) ? $resultCount + 1 : NULL;
275 if (!empty($result) && empty($this->_headersOnly)) {
276 $links = self::links();
277 if ($this->_pageViewType == 'profileDataView') {
278 $pageCheckSum = $this->get('pageCheckSum');
279 if ($pageCheckSum) {
280 foreach ($links as $key => $link) {
281 $links[$key] = $link['qs'] . "&cs=%%cs%%";
282 }
283 }
284 }
285
286 if ($reached) {
287 unset($links[CRM_Core_Action::COPY]);
288 }
289 if (!empty($DTparams)) {
290 $this->_total = $resultCount;
291 $cgcount = $DTparams['offset'] + 1;
292 }
293 foreach ($result as $recId => &$value) {
294 foreach ($value as $fieldId => &$val) {
295 if (is_numeric($fieldId)) {
296 $customValue = &$val;
297 if (!empty($dateFields) && array_key_exists($fieldId, $dateFields)) {
298 // formatted date capture value capture
299 $dateFieldsVals[$fieldId][$recId] = CRM_Core_BAO_CustomField::displayValue($customValue, $fieldId);
300
301 //set date and time format
302 switch ($timeFormat) {
303 case 1:
304 $dateFormat[1] = 'g:iA';
305 break;
306
307 case 2:
308 $dateFormat[1] = 'G:i';
309 break;
310
311 default:
312 // if time is not selected remove time from value
313 $result[$recId][$fieldId] = substr($result[$recId][$fieldId], 0, 10);
314 }
315 $result[$recId][$fieldId] = CRM_Utils_Date::processDate($result[$recId][$fieldId], NULL, FALSE, implode(" ", $dateFormat));
316 }
317 else {
318 // assign to $result
319 $customValue = CRM_Core_BAO_CustomField::displayValue($customValue, $fieldId);
320 }
321
322 // Set field attributes to support crmEditable
323 // Note that $fieldAttributes[data-type] actually refers to the html type not the sql data type
324 // TODO: Not all widget types and validation rules are supported by crmEditable so some fields will not be in-place editable
325 $fieldAttributes = ['class' => "crmf-custom_{$fieldId}_$recId"];
326 $editable = FALSE;
327 if (!$options[$fieldId]['attributes']['is_view'] && $this->_pageViewType == 'customDataView' && $linkAction & CRM_Core_Action::UPDATE) {
328 $spec = $options[$fieldId]['attributes'];
329 switch ($spec['html_type']) {
330 case 'Text':
331 // Other data types like money would require some extra validation
332 // FIXME: crmEditable currently does not support any validation rules :(
333 $supportedDataTypes = ['Float', 'String', 'Int'];
334 $editable = in_array($spec['data_type'], $supportedDataTypes);
335 break;
336
337 case 'TextArea':
338 $editable = TRUE;
339 $fieldAttributes['data-type'] = 'textarea';
340 break;
341
342 case 'Radio':
343 case 'Select':
344 case 'Select Country':
345 case 'Select State/Province':
346 $editable = TRUE;
347 $fieldAttributes['data-type'] = $spec['data_type'] == 'Boolean' ? 'boolean' : 'select';
348 if (!$spec['is_required']) {
349 $fieldAttributes['data-empty-option'] = ts('- none -');
350 }
351 break;
352 }
353 }
354 if ($editable) {
355 $fieldAttributes['class'] .= ' crm-editable';
356 }
357 $attributes[$fieldId][$recId] = $fieldAttributes;
358
359 $op = NULL;
360 if ($this->_pageViewType == 'profileDataView') {
361 $actionParams = [
362 'recordId' => $recId,
363 'gid' => $this->_profileId,
364 'id' => $this->_contactId,
365 ];
366 $op = 'profile.multiValue.row';
367 }
368 else {
369 // different set of url params
370 $actionParams['gid'] = $actionParams['groupID'] = $this->_customGroupId;
371 $actionParams['cid'] = $actionParams['entityID'] = $this->_contactId;
372 $actionParams['recId'] = $recId;
373 $actionParams['type'] = $this->_contactType;
374 $actionParams['cgcount'] = empty($DTparams['sort']) ? $cgcount : $sortedResult[$recId];
375 $actionParams['newCgCount'] = $newCgCount;
376
377 // DELETE action links
378 $deleteData = [
379 'valueID' => $recId,
380 'groupID' => $this->_customGroupId,
381 'contactId' => $this->_contactId,
382 'key' => CRM_Core_Key::get('civicrm/ajax/customvalue'),
383 ];
384 $links[CRM_Core_Action::DELETE]['url'] = '#';
385 $links[CRM_Core_Action::DELETE]['extra'] = ' data-delete_params="' . htmlspecialchars(json_encode($deleteData)) . '"';
386 $links[CRM_Core_Action::DELETE]['class'] = 'delete-custom-row';
387 }
388 if (!empty($pageCheckSum)) {
389 $actionParams['cs'] = $pageCheckSum;
390 }
391
392 $value['action'] = CRM_Core_Action::formLink(
393 $links,
394 $linkAction,
395 $actionParams,
396 ts('more'),
397 FALSE,
398 $op,
399 'customValue',
400 $fieldId
401 );
402 }
403 }
404 $cgcount++;
405 }
406 }
407 }
408
409 $headers = [];
410 if (!empty($fieldIDs)) {
411 $fields = ['Radio', 'Select', 'Select Country', 'Select State/Province'];
412 foreach ($fieldIDs as $fieldID) {
413 if ($this->_pageViewType == 'profileDataView') {
414 $headers[$fieldID] = $customGroupInfo[$fieldID]['fieldLabel'];
415 }
416 elseif (!empty($this->_headersOnly)) {
417 if (!$options[$fieldID]['attributes']['is_view'] && $linkAction & CRM_Core_Action::UPDATE) {
418 $spec = $options[$fieldID]['attributes'];
419
420 if (in_array($spec['html_type'], $fields)) {
421 $headerAttr[$fieldID]['dataType'] = $spec['data_type'] == 'Boolean' ? 'boolean' : 'select';
422 if (!$spec['is_required']) {
423 $headerAttr[$fieldID]['dataEmptyOption'] = ts('- none -');
424 }
425 }
426 elseif ($spec['html_type'] == 'TextArea') {
427 $headerAttr[$fieldID]['dataType'] = 'textarea';
428 }
429 }
430 $headers[$fieldID] = $fieldLabels[$fieldID]['label'];
431 $headerAttr[$fieldID]['columnName'] = $fieldLabels[$fieldID]['column_name'];
432 }
433 }
434 }
435 $this->assign('dateFields', $dateFields);
436 $this->assign('dateFieldsVals', $dateFieldsVals);
437 $this->assign('cgcount', $cgcount);
438 $this->assign('newCgCount', $newCgCount);
439 $this->assign('contactId', $this->_contactId);
440 $this->assign('contactType', $this->_contactType);
441 $this->assign('customGroupTitle', $this->_customGroupTitle);
442 $this->assign('headers', $headers);
443 $this->assign('headerAttr', $headerAttr);
444 $this->assign('records', $result);
445 $this->assign('attributes', $attributes);
446
447 return [$result, $attributes];
448 }
449
450 /**
451 * Get name of edit form.
452 *
453 * @return string
454 * classname of edit form
455 */
456 public function editForm() {
457 return '';
458 }
459
460 /**
461 * Get edit form name.
462 *
463 * @return string
464 * name of this page
465 */
466 public function editName() {
467 return '';
468 }
469
470 /**
471 * Get user context.
472 *
473 * @param null $mode
474 *
475 * @return string
476 * user context
477 */
478 public function userContext($mode = NULL) {
479 return '';
480 }
481
482 }