Merge pull request #18714 from MegaphoneJon/financial-111
[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 */
17class CRM_Profile_Page_MultipleRecordFieldsListing extends CRM_Core_Page_Basic {
18
19 /**
fe482240 20 * The action links that we need to display for the browse screen.
6a488035
TO
21 *
22 * @var array
6a488035 23 */
c86d4e7c 24 public static $_links = NULL;
6a488035
TO
25
26 protected $_fields = NULL;
27
28 protected $_profileId = NULL;
366fe2a3 29
5a2c7bb9 30 public $_contactId = NULL;
6a488035 31
5a2c7bb9 32 public $_customGroupTitle = NULL;
e41f4660 33
5a2c7bb9 34 public $_pageViewType = NULL;
e41f4660 35
5a2c7bb9 36 public $_contactType = NULL;
fcc8f207 37
6a488035 38 /**
fe482240 39 * Get BAO Name.
6a488035 40 *
a6c01b45
CW
41 * @return string
42 * Classname of BAO.
6a488035 43 */
00be9182 44 public function getBAOName() {
6a488035
TO
45 return '';
46 }
47
48 /**
fe482240 49 * Get action Links.
6a488035 50 *
a6c01b45
CW
51 * @return array
52 * (reference) of action links
6a488035 53 */
00be9182 54 public function &links() {
32c3b33f 55 if (!isset(self::$_links[$this->_pageViewType])) {
6a488035 56 // helper variable for nicer formatting
be2fb01f 57 $links = [];
6a488035
TO
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);
366fe2a3 62
1dde099b 63 // names and titles
be2fb01f 64 $links[CRM_Core_Action::VIEW] = [
6a488035 65 'name' => ts('View'),
be2fb01f
CW
66 'title' => ts('View %1', [1 => $this->_customGroupTitle . ' record']),
67 ];
6a488035 68
be2fb01f 69 $links[CRM_Core_Action::UPDATE] = [
6a488035 70 'name' => ts('Edit'),
be2fb01f
CW
71 'title' => ts('Edit %1', [1 => $this->_customGroupTitle . ' record']),
72 ];
6a488035 73
be2fb01f 74 $links[CRM_Core_Action::DELETE] = [
6a488035 75 'name' => ts('Delete'),
be2fb01f
CW
76 'title' => ts('Delete %1', [1 => $this->_customGroupTitle . ' record']),
77 ];
366fe2a3 78
1dde099b 79 // urls and queryStrings
e41f4660 80 if ($this->_pageViewType == 'profileDataView') {
1dde099b 81 $links[CRM_Core_Action::VIEW]['url'] = 'civicrm/profile/view';
7ab9bfe8 82 $links[CRM_Core_Action::VIEW]['qs'] = "reset=1&id=%%id%%&recordId=%%recordId%%&gid=%%gid%%&multiRecord={$view}";
1dde099b
PJ
83
84 $links[CRM_Core_Action::UPDATE]['url'] = 'civicrm/profile/edit';
7ab9bfe8 85 $links[CRM_Core_Action::UPDATE]['qs'] = "reset=1&id=%%id%%&recordId=%%recordId%%&gid=%%gid%%&multiRecord={$update}";
1dde099b 86
e41f4660 87 $links[CRM_Core_Action::DELETE]['url'] = 'civicrm/profile/edit';
7ab9bfe8 88 $links[CRM_Core_Action::DELETE]['qs'] = "reset=1&id=%%id%%&recordId=%%recordId%%&gid=%%gid%%&multiRecord={$delete}";
1dde099b
PJ
89
90 }
91 elseif ($this->_pageViewType == 'customDataView') {
92 // custom data specific view links
93 $links[CRM_Core_Action::VIEW]['url'] = 'civicrm/contact/view/cd';
e1d48b72 94 $links[CRM_Core_Action::VIEW]['qs'] = 'reset=1&gid=%%gid%%&cid=%%cid%%&recId=%%recId%%&cgcount=%%cgcount%%&multiRecordDisplay=single&mode=view';
1dde099b
PJ
95
96 // custom data specific update links
97 $links[CRM_Core_Action::UPDATE]['url'] = 'civicrm/contact/view/cd/edit';
525faea3 98 $links[CRM_Core_Action::UPDATE]['qs'] = 'reset=1&type=%%type%%&groupID=%%groupID%%&entityID=%%entityID%%&cgcount=%%cgcount%%&multiRecordDisplay=single&mode=edit';
1dde099b
PJ
99 // NOTE : links for DELETE action for customDataView is handled in browse
100
101 // copy action
be2fb01f 102 $links[CRM_Core_Action::COPY] = [
1dde099b 103 'name' => ts('Copy'),
be2fb01f 104 'title' => ts('Copy %1', [1 => $this->_customGroupTitle . ' record']),
1dde099b 105 'url' => 'civicrm/contact/view/cd/edit',
21dfd5f5 106 'qs' => 'reset=1&type=%%type%%&groupID=%%groupID%%&entityID=%%entityID%%&cgcount=%%newCgCount%%&multiRecordDisplay=single&copyValueId=%%cgcount%%&mode=copy',
be2fb01f 107 ];
e41f4660
PJ
108 }
109
110 self::$_links[$this->_pageViewType] = $links;
6a488035 111 }
e41f4660 112 return self::$_links[$this->_pageViewType];
6a488035
TO
113 }
114
115 /**
fe482240 116 * Run the page.
6a488035
TO
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 *
6a488035 122 */
00be9182 123 public function run() {
6a488035
TO
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)) {
366fe2a3 131 $this->_profileId = $profileId;
6a488035 132 }
366fe2a3 133
6a488035 134 $this->_contactId = CRM_Utils_Request::retrieve('contactId', 'Positive', $this, FALSE);
e41f4660
PJ
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);
6a488035 138 if ($action & CRM_Core_Action::BROWSE) {
366fe2a3 139 //browse
6a488035
TO
140 $this->browse();
141 return;
142 }
143 // parent run
144 return parent::run();
145 }
146
147 /**
fe482240 148 * Browse the listing.
6a488035 149 *
6a488035 150 */
00be9182 151 public function browse() {
b05a0fb6 152 $dateFields = NULL;
d4c2682f 153 $newCgCount = $cgcount = 0;
be2fb01f 154 $attributes = $result = $headerAttr = [];
b05a0fb6 155 $dateFieldsVals = NULL;
e41f4660 156 if ($this->_pageViewType == 'profileDataView' && $this->_profileId) {
6a488035
TO
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 );
be2fb01f 164 $multiRecordFields = [];
6a488035
TO
165 $fieldIDs = NULL;
166 $result = NULL;
167 $multiRecordFieldsWithSummaryListing = CRM_Core_BAO_UFGroup::shiftMultiRecordFields($fields, $multiRecordFields, TRUE);
366fe2a3 168
6a488035
TO
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);
366fe2a3 177
6a488035
TO
178 if ($multiRecordFieldsWithSummaryListing && !empty($multiRecordFieldsWithSummaryListing)) {
179 $fieldIDs = array_keys($multiRecordFieldsWithSummaryListing);
180 }
181 }
e41f4660
PJ
182 elseif ($this->_pageViewType == 'customDataView') {
183 // require custom group id for _pageViewType of customDataView
184 $customGroupId = $this->_customGroupId;
ac9dd8dc 185 $this->assign('customGroupId', $customGroupId);
e41f4660
PJ
186 $reached = CRM_Core_BAO_CustomGroup::hasReachedMaxLimit($customGroupId, $this->_contactId);
187 if (!$reached) {
188 $this->assign('contactId', $this->_contactId);
e41f4660
PJ
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 }
9082df1a 202 if (!empty($fieldIDs) && $this->_contactId) {
be2fb01f
CW
203 $options = [];
204 $returnProperities = [
353ffa53
TO
205 'html_type',
206 'data_type',
207 'date_format',
208 'time_format',
209 'default_value',
acb1052e 210 'is_required',
a5f3119b 211 'is_view',
be2fb01f 212 ];
6a488035 213 foreach ($fieldIDs as $key => $fieldID) {
e41f4660 214 $fieldIDs[$key] = !is_numeric($fieldID) ? CRM_Core_BAO_CustomField::getKeyID($fieldID) : $fieldID;
be2fb01f
CW
215 $param = ['id' => $fieldIDs[$key]];
216 $returnValues = [];
6a488035 217 CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_CustomField', $param, $returnValues, $returnProperities);
b05a0fb6
PJ
218 if ($returnValues['data_type'] == 'Date') {
219 $dateFields[$fieldIDs[$key]] = 1;
ed0ca248 220 $actualPHPFormats = CRM_Utils_Date::datePluginToPHPFormats();
791590df 221 $dateFormat = (array) CRM_Utils_Array::value($returnValues['date_format'], $actualPHPFormats);
9c1bc317 222 $timeFormat = $returnValues['time_format'] ?? NULL;
b05a0fb6 223 }
366fe2a3 224
6a488035
TO
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 }
366fe2a3 231
6a488035
TO
232 $options[$fieldIDs[$key]]['attributes']['html_type'] = $returnValues['html_type'];
233 $options[$fieldIDs[$key]]['attributes']['data_type'] = $returnValues['data_type'];
4411002f 234 $options[$fieldIDs[$key]]['attributes']['is_required'] = !empty($returnValues['is_required']);
9c1bc317
CW
235 $options[$fieldIDs[$key]]['attributes']['default_value'] = $returnValues['default_value'] ?? NULL;
236 $options[$fieldIDs[$key]]['attributes']['is_view'] = $returnValues['is_view'] ?? NULL;
366fe2a3 237
acb1052e 238 $options[$fieldIDs[$key]]['attributes']['format']
9c1bc317
CW
239 = $options[$fieldIDs[$key]]['attributes']['date_format'] = $returnValues['date_format'] ?? NULL;
240 $options[$fieldIDs[$key]]['attributes']['time_format'] = $returnValues['time_format'] ?? NULL;
6a488035 241 }
9082df1a 242 $linkAction = array_sum(array_keys($this->links()));
243 }
366fe2a3 244
d4c2682f 245 if (!empty($fieldIDs) && $this->_contactId) {
c693f065 246 $DTparams = !empty($this->_DTparams) ? $this->_DTparams : NULL;
e41f4660 247 // commonly used for both views i.e profile listing view (profileDataView) and custom data listing view (customDataView)
e525d6af 248 $result = CRM_Core_BAO_CustomValueTable::getEntityValues($this->_contactId, NULL, $fieldIDs, TRUE, $DTparams);
f5eda27f 249 $resultCount = !empty($result['count']) ? $result['count'] : count($result);
be2fb01f 250 $sortedResult = !empty($result['sortedResult']) ? $result['sortedResult'] : [];
c693f065 251 unset($result['count']);
e525d6af 252 unset($result['sortedResult']);
366fe2a3 253
e41f4660
PJ
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;
be2fb01f 265 $singleField = [$singleField];
353ffa53 266 $fieldInput = $singleField;
e41f4660
PJ
267 }
268 $customGroupInfo = CRM_Core_BAO_CustomGroup::getGroupTitles($fieldInput);
269 $this->_customGroupTitle = $customGroupInfo[$fieldIdInput]['groupTitle'];
270 }
abdd5e3e 271 // $cgcount is defined before 'if' condition as entity may have no record
e41f4660
PJ
272 // and $cgcount is used to build new record url
273 $cgcount = 1;
d4c2682f 274 $newCgCount = (!$reached) ? $resultCount + 1 : NULL;
275 if (!empty($result) && empty($this->_headersOnly)) {
eb2b4bdb 276 $links = self::links();
e41f4660
PJ
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 }
1dde099b
PJ
285
286 if ($reached) {
287 unset($links[CRM_Core_Action::COPY]);
288 }
c693f065 289 if (!empty($DTparams)) {
290 $this->_total = $resultCount;
291 $cgcount = $DTparams['offset'] + 1;
5a2c7bb9 292 }
e41f4660
PJ
293 foreach ($result as $recId => &$value) {
294 foreach ($value as $fieldId => &$val) {
295 if (is_numeric($fieldId)) {
296 $customValue = &$val;
5f1aa3a5 297 if (!empty($dateFields) && array_key_exists($fieldId, $dateFields)) {
b44e3f84 298 // formatted date capture value capture
8cee0c70 299 $dateFieldsVals[$fieldId][$recId] = CRM_Core_BAO_CustomField::displayValue($customValue, $fieldId);
791590df 300
301 //set date and time format
302 switch ($timeFormat) {
303 case 1:
32bb1fb6 304 $dateFormat[1] = 'g:iA';
791590df 305 break;
306
307 case 2:
32bb1fb6 308 $dateFormat[1] = 'G:i';
791590df 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));
b05a0fb6
PJ
316 }
317 else {
318 // assign to $result
8cee0c70 319 $customValue = CRM_Core_BAO_CustomField::displayValue($customValue, $fieldId);
e41f4660
PJ
320 }
321
4411002f
CW
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
be2fb01f 325 $fieldAttributes = ['class' => "crmf-custom_{$fieldId}_$recId"];
4411002f 326 $editable = FALSE;
13f7c844 327 if (!$options[$fieldId]['attributes']['is_view'] && $this->_pageViewType == 'customDataView' && $linkAction & CRM_Core_Action::UPDATE) {
4411002f
CW
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 :(
be2fb01f 333 $supportedDataTypes = ['Float', 'String', 'Int'];
4411002f
CW
334 $editable = in_array($spec['data_type'], $supportedDataTypes);
335 break;
dc98079b 336
4411002f
CW
337 case 'TextArea':
338 $editable = TRUE;
339 $fieldAttributes['data-type'] = 'textarea';
340 break;
dc98079b 341
4411002f
CW
342 case 'Radio':
343 case 'Select':
4411002f
CW
344 $editable = TRUE;
345 $fieldAttributes['data-type'] = $spec['data_type'] == 'Boolean' ? 'boolean' : 'select';
346 if (!$spec['is_required']) {
347 $fieldAttributes['data-empty-option'] = ts('- none -');
348 }
349 break;
350 }
351 }
352 if ($editable) {
353 $fieldAttributes['class'] .= ' crm-editable';
354 }
355 $attributes[$fieldId][$recId] = $fieldAttributes;
356
e41f4660
PJ
357 $op = NULL;
358 if ($this->_pageViewType == 'profileDataView') {
be2fb01f 359 $actionParams = [
353ffa53
TO
360 'recordId' => $recId,
361 'gid' => $this->_profileId,
acb1052e 362 'id' => $this->_contactId,
be2fb01f 363 ];
e41f4660
PJ
364 $op = 'profile.multiValue.row';
365 }
366 else {
367 // different set of url params
368 $actionParams['gid'] = $actionParams['groupID'] = $this->_customGroupId;
369 $actionParams['cid'] = $actionParams['entityID'] = $this->_contactId;
370 $actionParams['recId'] = $recId;
371 $actionParams['type'] = $this->_contactType;
1c66bdc7 372 $actionParams['cgcount'] = empty($DTparams['sort']) ? $cgcount : $sortedResult[$recId];
1dde099b 373 $actionParams['newCgCount'] = $newCgCount;
eb2b4bdb
PJ
374
375 // DELETE action links
be2fb01f 376 $deleteData = [
a801aa5c
CW
377 'valueID' => $recId,
378 'groupID' => $this->_customGroupId,
379 'contactId' => $this->_contactId,
380 'key' => CRM_Core_Key::get('civicrm/ajax/customvalue'),
be2fb01f 381 ];
eb2b4bdb 382 $links[CRM_Core_Action::DELETE]['url'] = '#';
86bfa4f6 383 $links[CRM_Core_Action::DELETE]['extra'] = ' data-delete_params="' . htmlspecialchars(json_encode($deleteData)) . '"';
a801aa5c 384 $links[CRM_Core_Action::DELETE]['class'] = 'delete-custom-row';
e41f4660
PJ
385 }
386 if (!empty($pageCheckSum)) {
387 $actionParams['cs'] = $pageCheckSum;
388 }
eb2b4bdb 389
d51e02d3 390 $value['action'] = CRM_Core_Action::formLink(
e41f4660
PJ
391 $links,
392 $linkAction,
393 $actionParams,
394 ts('more'),
395 FALSE,
396 $op,
397 'customValue',
c5c263ca 398 $fieldId
e41f4660
PJ
399 );
400 }
401 }
402 $cgcount++;
403 }
404 }
6a488035 405 }
366fe2a3 406
be2fb01f 407 $headers = [];
6a488035 408 if (!empty($fieldIDs)) {
a7bcb32d 409 $fields = ['Radio', 'Select'];
6a488035 410 foreach ($fieldIDs as $fieldID) {
f1eae8d1 411 if ($this->_pageViewType == 'profileDataView') {
412 $headers[$fieldID] = $customGroupInfo[$fieldID]['fieldLabel'];
413 }
9082df1a 414 elseif (!empty($this->_headersOnly)) {
415 if (!$options[$fieldID]['attributes']['is_view'] && $linkAction & CRM_Core_Action::UPDATE) {
416 $spec = $options[$fieldID]['attributes'];
417
418 if (in_array($spec['html_type'], $fields)) {
419 $headerAttr[$fieldID]['dataType'] = $spec['data_type'] == 'Boolean' ? 'boolean' : 'select';
420 if (!$spec['is_required']) {
421 $headerAttr[$fieldID]['dataEmptyOption'] = ts('- none -');
422 }
423 }
424 elseif ($spec['html_type'] == 'TextArea') {
425 $headerAttr[$fieldID]['dataType'] = 'textarea';
426 }
427 }
f1eae8d1 428 $headers[$fieldID] = $fieldLabels[$fieldID]['label'];
429 $headerAttr[$fieldID]['columnName'] = $fieldLabels[$fieldID]['column_name'];
430 }
6a488035
TO
431 }
432 }
b05a0fb6 433 $this->assign('dateFields', $dateFields);
dc98079b 434 $this->assign('dateFieldsVals', $dateFieldsVals);
e41f4660 435 $this->assign('cgcount', $cgcount);
d4c2682f 436 $this->assign('newCgCount', $newCgCount);
5a2c7bb9 437 $this->assign('contactId', $this->_contactId);
438 $this->assign('contactType', $this->_contactType);
6a488035
TO
439 $this->assign('customGroupTitle', $this->_customGroupTitle);
440 $this->assign('headers', $headers);
c693f065 441 $this->assign('headerAttr', $headerAttr);
6a488035 442 $this->assign('records', $result);
c693f065 443 $this->assign('attributes', $attributes);
5a2c7bb9 444
be2fb01f 445 return [$result, $attributes];
6a488035
TO
446 }
447
448 /**
fe482240 449 * Get name of edit form.
6a488035 450 *
a6c01b45
CW
451 * @return string
452 * classname of edit form
6a488035 453 */
00be9182 454 public function editForm() {
6a488035
TO
455 return '';
456 }
457
458 /**
fe482240 459 * Get edit form name.
6a488035 460 *
a6c01b45
CW
461 * @return string
462 * name of this page
6a488035 463 */
00be9182 464 public function editName() {
6a488035
TO
465 return '';
466 }
467
468 /**
fe482240 469 * Get user context.
6a488035 470 *
77b97be7
EM
471 * @param null $mode
472 *
a6c01b45
CW
473 * @return string
474 * user context
6a488035 475 */
00be9182 476 public function userContext($mode = NULL) {
6a488035
TO
477 return '';
478 }
96025800 479
6a488035 480}