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