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