add class attributes to each cell
[civicrm-core.git] / CRM / Profile / Page / MultipleRecordFieldsListing.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
32 * $Id$
33 *
34 */
35 class CRM_Profile_Page_MultipleRecordFieldsListing extends CRM_Core_Page_Basic {
36
37 /**
38 * The action links that we need to display for the browse screen.
39 *
40 * @var array
41 */
42 static $_links = NULL;
43
44 protected $_fields = NULL;
45
46 protected $_profileId = NULL;
47
48 public $_contactId = NULL;
49
50 public $_customGroupTitle = NULL;
51
52 public $_pageViewType = NULL;
53
54 public $_contactType = NULL;
55
56 /**
57 * Get BAO Name.
58 *
59 * @return string
60 * Classname of BAO.
61 */
62 public function getBAOName() {
63 return '';
64 }
65
66 /**
67 * Get action Links.
68 *
69 * @return array
70 * (reference) of action links
71 */
72 public function &links() {
73 if (!(self::$_links[$this->_pageViewType])) {
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);
80
81 // names and titles
82 $links[CRM_Core_Action::VIEW] = array(
83 'name' => ts('View'),
84 'title' => ts('View %1', array(1 => $this->_customGroupTitle . ' record')),
85 );
86
87 $links[CRM_Core_Action::UPDATE] = array(
88 'name' => ts('Edit'),
89 'title' => ts('Edit %1', array(1 => $this->_customGroupTitle . ' record')),
90 );
91
92 $links[CRM_Core_Action::DELETE] = array(
93 'name' => ts('Delete'),
94 'title' => ts('Delete %1', array(1 => $this->_customGroupTitle . ' record')),
95 );
96
97 // urls and queryStrings
98 if ($this->_pageViewType == 'profileDataView') {
99 $links[CRM_Core_Action::VIEW]['url'] = 'civicrm/profile/view';
100 $links[CRM_Core_Action::VIEW]['qs'] = "reset=1&id=%%id%%&recordId=%%recordId%%&gid=%%gid%%&multiRecord={$view}";
101
102 $links[CRM_Core_Action::UPDATE]['url'] = 'civicrm/profile/edit';
103 $links[CRM_Core_Action::UPDATE]['qs'] = "reset=1&id=%%id%%&recordId=%%recordId%%&gid=%%gid%%&multiRecord={$update}";
104
105 $links[CRM_Core_Action::DELETE]['url'] = 'civicrm/profile/edit';
106 $links[CRM_Core_Action::DELETE]['qs'] = "reset=1&id=%%id%%&recordId=%%recordId%%&gid=%%gid%%&multiRecord={$delete}";
107
108 }
109 elseif ($this->_pageViewType == 'customDataView') {
110 // custom data specific view links
111 $links[CRM_Core_Action::VIEW]['url'] = 'civicrm/contact/view/cd';
112 $links[CRM_Core_Action::VIEW]['qs'] = 'reset=1&gid=%%gid%%&cid=%%cid%%&recId=%%recId%%&multiRecordDisplay=single&mode=view';
113
114 // custom data specific update links
115 $links[CRM_Core_Action::UPDATE]['url'] = 'civicrm/contact/view/cd/edit';
116 $links[CRM_Core_Action::UPDATE]['qs'] = 'reset=1&type=%%type%%&groupID=%%groupID%%&entityID=%%entityID%%&cgcount=%%cgcount%%&multiRecordDisplay=single&mode=edit';
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'),
122 'title' => ts('Copy %1', array(1 => $this->_customGroupTitle . ' record')),
123 'url' => 'civicrm/contact/view/cd/edit',
124 'qs' => 'reset=1&type=%%type%%&groupID=%%groupID%%&entityID=%%entityID%%&cgcount=%%newCgCount%%&multiRecordDisplay=single&copyValueId=%%cgcount%%&mode=copy',
125 );
126 }
127
128 self::$_links[$this->_pageViewType] = $links;
129 }
130 return self::$_links[$this->_pageViewType];
131 }
132
133 /**
134 * Run the page.
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
141 */
142 public function run() {
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)) {
150 $this->_profileId = $profileId;
151 }
152
153 $this->_contactId = CRM_Utils_Request::retrieve('contactId', 'Positive', $this, FALSE);
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);
157 if ($action & CRM_Core_Action::BROWSE) {
158 //browse
159 $this->browse();
160 return;
161 }
162 // parent run
163 return parent::run();
164 }
165
166 /**
167 * Browse the listing.
168 *
169 * @return void
170 */
171 public function browse() {
172 $dateFields = NULL;
173 $cgcount = 0;
174 $attributes = $headerAttr = array();
175 $dateFieldsVals = NULL;
176 if ($this->_pageViewType == 'profileDataView' && $this->_profileId) {
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 );
184 $multiRecordFields = array();
185 $fieldIDs = NULL;
186 $result = NULL;
187 $multiRecordFieldsWithSummaryListing = CRM_Core_BAO_UFGroup::shiftMultiRecordFields($fields, $multiRecordFields, TRUE);
188
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);
197
198 if ($multiRecordFieldsWithSummaryListing && !empty($multiRecordFieldsWithSummaryListing)) {
199 $fieldIDs = array_keys($multiRecordFieldsWithSummaryListing);
200 }
201 }
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 }
222 if ($fieldIDs && !empty($fieldIDs) && $this->_contactId) {
223 $options = array();
224 $returnProperities = array(
225 'html_type',
226 'data_type',
227 'date_format',
228 'time_format',
229 'default_value',
230 'is_required',
231 'is_view',
232 );
233 foreach ($fieldIDs as $key => $fieldID) {
234 $fieldIDs[$key] = !is_numeric($fieldID) ? CRM_Core_BAO_CustomField::getKeyID($fieldID) : $fieldID;
235 $param = array('id' => $fieldIDs[$key]);
236 $returnValues = array();
237 CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_CustomField', $param, $returnValues, $returnProperities);
238 if ($returnValues['data_type'] == 'Date') {
239 $dateFields[$fieldIDs[$key]] = 1;
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);
243 }
244
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 }
251
252 $options[$fieldIDs[$key]]['attributes']['html_type'] = $returnValues['html_type'];
253 $options[$fieldIDs[$key]]['attributes']['data_type'] = $returnValues['data_type'];
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);
256 $options[$fieldIDs[$key]]['attributes']['is_view'] = CRM_Utils_Array::value('is_view', $returnValues);
257
258 $options[$fieldIDs[$key]]['attributes']['format']
259 = $options[$fieldIDs[$key]]['attributes']['date_format'] = CRM_Utils_Array::value('date_format', $returnValues);
260 $options[$fieldIDs[$key]]['attributes']['time_format'] = CRM_Utils_Array::value('time_format', $returnValues);
261 }
262
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 = $result['count'];
267 unset($result['count']);
268
269 if ($this->_pageViewType == 'profileDataView') {
270 if (!empty($fieldIDs)) {
271 //get the group info of multi rec fields in listing view
272 $fieldInput = $fieldIDs;
273 $fieldIdInput = $fieldIDs[0];
274 }
275 else {
276 //if no listing fields exist, take the group title for display
277 $nonListingFieldIds = array_keys($multiRecordFields);
278 $singleField = CRM_Core_BAO_CustomField::getKeyID($nonListingFieldIds[0]);
279 $fieldIdInput = $singleField;
280 $singleField = array($singleField);
281 $fieldInput = $singleField;
282 }
283 $customGroupInfo = CRM_Core_BAO_CustomGroup::getGroupTitles($fieldInput);
284 $this->_customGroupTitle = $customGroupInfo[$fieldIdInput]['groupTitle'];
285 }
286 // $cgcount is defined before 'if' condition as enitiy may have no record
287 // and $cgcount is used to build new record url
288 $cgcount = 1;
289 if ($result && !empty($result)) {
290 $links = self::links();
291 if ($this->_pageViewType == 'profileDataView') {
292 $pageCheckSum = $this->get('pageCheckSum');
293 if ($pageCheckSum) {
294 foreach ($links as $key => $link) {
295 $links[$key] = $link['qs'] . "&cs=%%cs%%";
296 }
297 }
298 }
299 $linkAction = array_sum(array_keys($this->links()));
300
301 if ($reached) {
302 unset($links[CRM_Core_Action::COPY]);
303 }
304 $newCgCount = (!$reached) ? $resultCount + 1 : NULL;
305 if (!empty($DTparams)) {
306 $this->_total = $resultCount;
307 $cgcount = $DTparams['offset'] + 1;
308 }
309 foreach ($result as $recId => &$value) {
310 foreach ($value as $fieldId => &$val) {
311 if (is_numeric($fieldId)) {
312 $customValue = &$val;
313 if (!empty($dateFields) && array_key_exists($fieldId, $dateFields)) {
314 // formatted date capture value capture
315 $dateFieldsVals[$fieldId][$recId] = CRM_Core_BAO_CustomField::displayValue($customValue, $fieldId);
316
317 //set date and time format
318 switch ($timeFormat) {
319 case 1:
320 $dateFormat[1] = 'g:iA';
321 break;
322
323 case 2:
324 $dateFormat[1] = 'G:i';
325 break;
326
327 default:
328 // if time is not selected remove time from value
329 $result[$recId][$fieldId] = substr($result[$recId][$fieldId], 0, 10);
330 }
331 $result[$recId][$fieldId] = CRM_Utils_Date::processDate($result[$recId][$fieldId], NULL, FALSE, implode(" ", $dateFormat));
332 }
333 else {
334 // assign to $result
335 $customValue = CRM_Core_BAO_CustomField::displayValue($customValue, $fieldId);
336 }
337
338 // Set field attributes to support crmEditable
339 // Note that $fieldAttributes[data-type] actually refers to the html type not the sql data type
340 // TODO: Not all widget types and validation rules are supported by crmEditable so some fields will not be in-place editable
341 $fieldAttributes = array('class' => "crmf-custom_{$fieldId}_$recId");
342 $editable = FALSE;
343 if (!$options[$fieldId]['attributes']['is_view'] && $linkAction & CRM_Core_Action::UPDATE) {
344 $spec = $options[$fieldId]['attributes'];
345 switch ($spec['html_type']) {
346 case 'Text':
347 // Other data types like money would require some extra validation
348 // FIXME: crmEditable currently does not support any validation rules :(
349 $supportedDataTypes = array('Float', 'String', 'Int');
350 $editable = in_array($spec['data_type'], $supportedDataTypes);
351 break;
352
353 case 'TextArea':
354 $editable = TRUE;
355 $fieldAttributes['data-type'] = 'textarea';
356 break;
357
358 case 'Radio':
359 case 'Select':
360 case 'Select Country':
361 case 'Select State/Province':
362 $editable = TRUE;
363 $fieldAttributes['data-type'] = $spec['data_type'] == 'Boolean' ? 'boolean' : 'select';
364 if (!$spec['is_required']) {
365 $fieldAttributes['data-empty-option'] = ts('- none -');
366 }
367 break;
368 }
369 }
370 if ($editable) {
371 $fieldAttributes['class'] .= ' crm-editable';
372 }
373 $attributes[$fieldId][$recId] = $fieldAttributes;
374
375 $op = NULL;
376 if ($this->_pageViewType == 'profileDataView') {
377 $actionParams = array(
378 'recordId' => $recId,
379 'gid' => $this->_profileId,
380 'id' => $this->_contactId,
381 );
382 $op = 'profile.multiValue.row';
383 }
384 else {
385 $headerAttr[$fieldId]['dataType'] = CRM_Utils_Array::value('data-type', $fieldAttributes);
386 $headerAttr[$fieldId]['dataEmptyOption'] = CRM_Utils_Array::value('data-empty-option', $fieldAttributes);
387 // different set of url params
388 $actionParams['gid'] = $actionParams['groupID'] = $this->_customGroupId;
389 $actionParams['cid'] = $actionParams['entityID'] = $this->_contactId;
390 $actionParams['recId'] = $recId;
391 $actionParams['type'] = $this->_contactType;
392 $actionParams['cgcount'] = $cgcount;
393 $actionParams['newCgCount'] = $newCgCount;
394
395 // DELETE action links
396 $deleteData = array(
397 'valueID' => $recId,
398 'groupID' => $this->_customGroupId,
399 'contactId' => $this->_contactId,
400 'key' => CRM_Core_Key::get('civicrm/ajax/customvalue'),
401 );
402 $links[CRM_Core_Action::DELETE]['url'] = '#';
403 $links[CRM_Core_Action::DELETE]['extra'] = ' data-delete_params="' . htmlspecialchars(json_encode($deleteData)) . '"';
404 $links[CRM_Core_Action::DELETE]['class'] = 'delete-custom-row';
405 }
406 if (!empty($pageCheckSum)) {
407 $actionParams['cs'] = $pageCheckSum;
408 }
409
410 $value['action'] = CRM_Core_Action::formLink(
411 $links,
412 $linkAction,
413 $actionParams,
414 ts('more'),
415 FALSE,
416 $op,
417 'customValue',
418 $fieldId // not ideal, but the one thing not sent in $actionParams
419 );
420 }
421 }
422 $cgcount++;
423 }
424 }
425 }
426
427 $headers = array();
428 if (!empty($fieldIDs)) {
429 foreach ($fieldIDs as $fieldID) {
430 $headers[$fieldID] = ($this->_pageViewType == 'profileDataView') ? $customGroupInfo[$fieldID]['fieldLabel'] : $fieldLabels[$fieldID]['label'];
431 }
432 }
433 $this->assign('dateFields', $dateFields);
434 $this->assign('dateFieldsVals', $dateFieldsVals);
435 $this->assign('cgcount', $cgcount);
436 $this->assign('contactId', $this->_contactId);
437 $this->assign('contactType', $this->_contactType);
438 $this->assign('customGroupTitle', $this->_customGroupTitle);
439 $this->assign('headers', $headers);
440 $this->assign('headerAttr', $headerAttr);
441 $this->assign('records', $result);
442 $this->assign('attributes', $attributes);
443
444 return array($result, $attributes);
445 }
446
447 /**
448 * Get name of edit form.
449 *
450 * @return string
451 * classname of edit form
452 */
453 public function editForm() {
454 return '';
455 }
456
457 /**
458 * Get edit form name.
459 *
460 * @return string
461 * name of this page
462 */
463 public function editName() {
464 return '';
465 }
466
467 /**
468 * Get user context.
469 *
470 * @param null $mode
471 *
472 * @return string
473 * user context
474 */
475 public function userContext($mode = NULL) {
476 return '';
477 }
478
479 }