3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 class CRM_Profile_Page_MultipleRecordFieldsListing
extends CRM_Core_Page_Basic
{
20 * The action links that we need to display for the browse screen.
24 public static $_links = NULL;
26 protected $_fields = NULL;
28 protected $_profileId = NULL;
30 public $_contactId = NULL;
32 public $_customGroupTitle = NULL;
34 public $_pageViewType = NULL;
36 public $_contactType = NULL;
44 public function getBAOName() {
52 * (reference) of action links
54 public function &links() {
55 if (!isset(self
::$_links[$this->_pageViewType
])) {
56 // helper variable for nicer formatting
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);
64 $links[CRM_Core_Action
::VIEW
] = [
66 'title' => ts('View %1', [1 => $this->_customGroupTitle
. ' record']),
69 $links[CRM_Core_Action
::UPDATE
] = [
71 'title' => ts('Edit %1', [1 => $this->_customGroupTitle
. ' record']),
74 $links[CRM_Core_Action
::DELETE
] = [
75 'name' => ts('Delete'),
76 'title' => ts('Delete %1', [1 => $this->_customGroupTitle
. ' record']),
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}";
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}";
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}";
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';
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
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©ValueId=%%cgcount%%&mode=copy',
110 self
::$_links[$this->_pageViewType
] = $links;
112 return self
::$_links[$this->_pageViewType
];
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
123 public function run() {
124 // get the requested action, default to 'browse'
125 $action = CRM_Utils_Request
::retrieve('action', 'String', $this, FALSE, FALSE);
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;
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
) {
144 return parent
::run();
148 * Browse the listing.
151 public function browse() {
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,
162 CRM_Core_Permission
::EDIT
164 $multiRecordFields = [];
167 $multiRecordFieldsWithSummaryListing = CRM_Core_BAO_UFGroup
::shiftMultiRecordFields($fields, $multiRecordFields, TRUE);
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
);
173 $this->assign('contactId', $this->_contactId
);
174 $this->assign('gid', $this->_profileId
);
176 $this->assign('reachedMax', $reached);
178 if ($multiRecordFieldsWithSummaryListing && !empty($multiRecordFieldsWithSummaryListing)) {
179 $fieldIDs = array_keys($multiRecordFieldsWithSummaryListing);
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
);
188 $this->assign('contactId', $this->_contactId
);
189 $this->assign('ctype', $this->_contactType
);
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'];
199 // from the above customGroupInfo we can get $this->_customGroupTitle
200 $this->_customGroupTitle
= $groupDetail[$customGroupId]['title'];
202 if (!empty($fieldIDs) && $this->_contactId
) {
204 $returnProperities = [
213 foreach ($fieldIDs as $key => $fieldID) {
214 $fieldIDs[$key] = !is_numeric($fieldID) ? CRM_Core_BAO_CustomField
::getKeyID($fieldID) : $fieldID;
215 $param = ['id' => $fieldIDs[$key]];
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;
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'];
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;
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;
242 $linkAction = array_sum(array_keys($this->links()));
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']);
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];
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;
268 $customGroupInfo = CRM_Core_BAO_CustomGroup
::getGroupTitles($fieldInput);
269 $this->_customGroupTitle
= $customGroupInfo[$fieldIdInput]['groupTitle'];
271 // $cgcount is defined before 'if' condition as entity may have no record
272 // and $cgcount is used to build new record url
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');
280 foreach ($links as $key => $link) {
281 $links[$key] = $link['qs'] . "&cs=%%cs%%";
287 unset($links[CRM_Core_Action
::COPY
]);
289 if (!empty($DTparams)) {
290 $this->_total
= $resultCount;
291 $cgcount = $DTparams['offset'] +
1;
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);
301 //set date and time format
302 switch ($timeFormat) {
304 $dateFormat[1] = 'g:iA';
308 $dateFormat[1] = 'G:i';
312 // if time is not selected remove time from value
313 $result[$recId][$fieldId] = substr($result[$recId][$fieldId], 0, 10);
315 $result[$recId][$fieldId] = CRM_Utils_Date
::processDate($result[$recId][$fieldId], NULL, FALSE, implode(" ", $dateFormat));
319 $customValue = CRM_Core_BAO_CustomField
::displayValue($customValue, $fieldId);
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"];
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']) {
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);
339 $fieldAttributes['data-type'] = 'textarea';
345 $fieldAttributes['data-type'] = $spec['data_type'] == 'Boolean' ?
'boolean' : 'select';
346 if (!$spec['is_required']) {
347 $fieldAttributes['data-empty-option'] = ts('- none -');
353 $fieldAttributes['class'] .= ' crm-editable';
355 $attributes[$fieldId][$recId] = $fieldAttributes;
358 if ($this->_pageViewType
== 'profileDataView') {
360 'recordId' => $recId,
361 'gid' => $this->_profileId
,
362 'id' => $this->_contactId
,
364 $op = 'profile.multiValue.row';
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
;
372 $actionParams['cgcount'] = empty($DTparams['sort']) ?
$cgcount : $sortedResult[$recId];
373 $actionParams['newCgCount'] = $newCgCount;
375 // DELETE action links
378 'groupID' => $this->_customGroupId
,
379 'contactId' => $this->_contactId
,
380 'key' => CRM_Core_Key
::get('civicrm/ajax/customvalue'),
382 $links[CRM_Core_Action
::DELETE
]['url'] = '#';
383 $links[CRM_Core_Action
::DELETE
]['extra'] = ' data-delete_params="' . htmlspecialchars(json_encode($deleteData)) . '"';
384 $links[CRM_Core_Action
::DELETE
]['class'] = 'delete-custom-row';
386 if (!empty($pageCheckSum)) {
387 $actionParams['cs'] = $pageCheckSum;
390 $value['action'] = CRM_Core_Action
::formLink(
408 if (!empty($fieldIDs)) {
409 $fields = ['Radio', 'Select'];
410 foreach ($fieldIDs as $fieldID) {
411 if ($this->_pageViewType
== 'profileDataView') {
412 $headers[$fieldID] = $customGroupInfo[$fieldID]['fieldLabel'];
414 elseif (!empty($this->_headersOnly
)) {
415 if (!$options[$fieldID]['attributes']['is_view'] && $linkAction & CRM_Core_Action
::UPDATE
) {
416 $spec = $options[$fieldID]['attributes'];
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 -');
424 elseif ($spec['html_type'] == 'TextArea') {
425 $headerAttr[$fieldID]['dataType'] = 'textarea';
428 $headers[$fieldID] = $fieldLabels[$fieldID]['label'];
429 $headerAttr[$fieldID]['columnName'] = $fieldLabels[$fieldID]['column_name'];
433 $this->assign('dateFields', $dateFields);
434 $this->assign('dateFieldsVals', $dateFieldsVals);
435 $this->assign('cgcount', $cgcount);
436 $this->assign('newCgCount', $newCgCount);
437 $this->assign('contactId', $this->_contactId
);
438 $this->assign('contactType', $this->_contactType
);
439 $this->assign('customGroupTitle', $this->_customGroupTitle
);
440 $this->assign('headers', $headers);
441 $this->assign('headerAttr', $headerAttr);
442 $this->assign('records', $result);
443 $this->assign('attributes', $attributes);
445 return [$result, $attributes];
449 * Get name of edit form.
452 * classname of edit form
454 public function editForm() {
459 * Get edit form name.
464 public function editName() {
476 public function userContext($mode = NULL) {