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