Merge pull request #5716 from jitendrapurohit/CRM-15716_4.6
[civicrm-core.git] / CRM / Profile / Page / MultipleRecordFieldsListing.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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 protected $_contactId = NULL;
49
50 protected $_customGroupTitle = NULL;
51
52 protected $_pageViewType = NULL;
53
54 protected $_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 $dateFieldsVals = NULL;
175 if ($this->_pageViewType == 'profileDataView' && $this->_profileId) {
176 $fields = CRM_Core_BAO_UFGroup::getFields($this->_profileId, FALSE, NULL,
177 NULL, NULL,
178 FALSE, NULL,
179 FALSE,
180 NULL,
181 CRM_Core_Permission::EDIT
182 );
183 $multiRecordFields = array();
184 $fieldIDs = NULL;
185 $result = NULL;
186 $multiRecordFieldsWithSummaryListing = CRM_Core_BAO_UFGroup::shiftMultiRecordFields($fields, $multiRecordFields, TRUE);
187
188 $multiFieldId = CRM_Core_BAO_CustomField::getKeyID(key($multiRecordFields));
189 $customGroupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $multiFieldId, 'custom_group_id');
190 $reached = CRM_Core_BAO_CustomGroup::hasReachedMaxLimit($customGroupId, $this->_contactId);
191 if (!$reached) {
192 $this->assign('contactId', $this->_contactId);
193 $this->assign('gid', $this->_profileId);
194 }
195 $this->assign('reachedMax', $reached);
196
197 if ($multiRecordFieldsWithSummaryListing && !empty($multiRecordFieldsWithSummaryListing)) {
198 $fieldIDs = array_keys($multiRecordFieldsWithSummaryListing);
199 }
200 }
201 elseif ($this->_pageViewType == 'customDataView') {
202 // require custom group id for _pageViewType of customDataView
203 $customGroupId = $this->_customGroupId;
204 $reached = CRM_Core_BAO_CustomGroup::hasReachedMaxLimit($customGroupId, $this->_contactId);
205 if (!$reached) {
206 $this->assign('contactId', $this->_contactId);
207 $this->assign('customGroupId', $customGroupId);
208 $this->assign('ctype', $this->_contactType);
209 }
210 $this->assign('reachedMax', $reached);
211 // custom group info : this consists of the field title of group fields
212 $groupDetail = CRM_Core_BAO_CustomGroup::getGroupDetail($customGroupId, NULL, CRM_Core_DAO::$_nullObject, TRUE);
213 // field ids of fields in_selector for the custom group id provided
214 $fieldIDs = array_keys($groupDetail[$customGroupId]['fields']);
215 // field labels for headers
216 $fieldLabels = $groupDetail[$customGroupId]['fields'];
217
218 // from the above customGroupInfo we can get $this->_customGroupTitle
219 $this->_customGroupTitle = $groupDetail[$customGroupId]['title'];
220 }
221 if ($fieldIDs && !empty($fieldIDs) && $this->_contactId) {
222 $options = array();
223 $returnProperities = array(
224 'html_type',
225 'data_type',
226 'date_format',
227 'time_format',
228 'default_value',
229 'is_required',
230 );
231 foreach ($fieldIDs as $key => $fieldID) {
232 $fieldIDs[$key] = !is_numeric($fieldID) ? CRM_Core_BAO_CustomField::getKeyID($fieldID) : $fieldID;
233 $param = array('id' => $fieldIDs[$key]);
234 $returnValues = array();
235 CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_CustomField', $param, $returnValues, $returnProperities);
236 if ($returnValues['data_type'] == 'Date') {
237 $dateFields[$fieldIDs[$key]] = 1;
238 $actualPHPFormats = CRM_Core_SelectValues::datePluginToPHPFormats();
239 $dateFormat = (array) CRM_Utils_Array::value($returnValues['date_format'], $actualPHPFormats);
240 $timeFormat = CRM_Utils_Array::value('time_format', $returnValues);
241 }
242
243 $optionValuePairs = CRM_Core_BAO_CustomOption::getCustomOption($fieldIDs[$key]);
244 if (!empty($optionValuePairs)) {
245 foreach ($optionValuePairs as $optionPairs) {
246 $options[$fieldIDs[$key]][$optionPairs['value']] = $optionPairs['label'];
247 }
248 }
249
250 $options[$fieldIDs[$key]]['attributes']['html_type'] = $returnValues['html_type'];
251 $options[$fieldIDs[$key]]['attributes']['data_type'] = $returnValues['data_type'];
252 $options[$fieldIDs[$key]]['attributes']['is_required'] = !empty($returnValues['is_required']);
253 $options[$fieldIDs[$key]]['attributes']['default_value'] = CRM_Utils_Array::value('default_value', $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
260 // commonly used for both views i.e profile listing view (profileDataView) and custom data listing view (customDataView)
261 $result = CRM_Core_BAO_CustomValueTable::getEntityValues($this->_contactId, NULL, $fieldIDs, TRUE);
262
263 if ($this->_pageViewType == 'profileDataView') {
264 if (!empty($fieldIDs)) {
265 //get the group info of multi rec fields in listing view
266 $fieldInput = $fieldIDs;
267 $fieldIdInput = $fieldIDs[0];
268 }
269 else {
270 //if no listing fields exist, take the group title for display
271 $nonListingFieldIds = array_keys($multiRecordFields);
272 $singleField = CRM_Core_BAO_CustomField::getKeyID($nonListingFieldIds[0]);
273 $fieldIdInput = $singleField;
274 $singleField = array($singleField);
275 $fieldInput = $singleField;
276 }
277 $customGroupInfo = CRM_Core_BAO_CustomGroup::getGroupTitles($fieldInput);
278 $this->_customGroupTitle = $customGroupInfo[$fieldIdInput]['groupTitle'];
279 }
280 // $cgcount is defined before 'if' condition as enitiy may have no record
281 // and $cgcount is used to build new record url
282 $cgcount = 1;
283 if ($result && !empty($result)) {
284 $links = self::links();
285 if ($this->_pageViewType == 'profileDataView') {
286 $pageCheckSum = $this->get('pageCheckSum');
287 if ($pageCheckSum) {
288 foreach ($links as $key => $link) {
289 $links[$key] = $link['qs'] . "&cs=%%cs%%";
290 }
291 }
292 }
293 $linkAction = array_sum(array_keys($this->links()));
294
295 if ($reached) {
296 unset($links[CRM_Core_Action::COPY]);
297 }
298 $newCgCount = (!$reached) ? count($result) + 1 : NULL;
299 foreach ($result as $recId => &$value) {
300 foreach ($value as $fieldId => &$val) {
301 if (is_numeric($fieldId)) {
302 $customValue = &$val;
303 if (!empty($dateFields) && array_key_exists($fieldId, $dateFields)) {
304 // formated date capture value capture
305 $dateFieldsVals[$fieldId][$recId] = CRM_Core_BAO_CustomField::getDisplayValue($customValue, $fieldId, $options);
306
307 //set date and time format
308 switch ($timeFormat) {
309 case 1:
310 $dateFormat[] = 'g:iA';
311 break;
312
313 case 2:
314 $dateFormat[] = 'G:i';
315 break;
316
317 default:
318 // if time is not selected remove time from value
319 $result[$recId][$fieldId] = substr($result[$recId][$fieldId], 0, 10);
320 }
321 $result[$recId][$fieldId] = CRM_Utils_Date::processDate($result[$recId][$fieldId], NULL, FALSE, implode(" ", $dateFormat));
322 }
323 else {
324 // assign to $result
325 $customValue = CRM_Core_BAO_CustomField::getDisplayValue($customValue, $fieldId, $options);
326 }
327 // FIXME: getDisplayValue should always return a string so why is this necessary?
328 if (!$customValue && $customValue !== '0') {
329 $customValue = "";
330 }
331
332 // Set field attributes to support crmEditable
333 // Note that $fieldAttributes[data-type] actually refers to the html type not the sql data type
334 // TODO: Not all widget types and validation rules are supported by crmEditable so some fields will not be in-place editable
335 $fieldAttributes = array('class' => "crmf-custom_{$fieldId}_$recId");
336 $editable = FALSE;
337 if ($linkAction & CRM_Core_Action::UPDATE) {
338 $spec = $options[$fieldId]['attributes'];
339 switch ($spec['html_type']) {
340 case 'Text':
341 // Other data types like money would require some extra validation
342 // FIXME: crmEditable currently does not support any validation rules :(
343 $supportedDataTypes = array('Float', 'String', 'Int');
344 $editable = in_array($spec['data_type'], $supportedDataTypes);
345 break;
346
347 case 'TextArea':
348 $editable = TRUE;
349 $fieldAttributes['data-type'] = 'textarea';
350 break;
351
352 case 'Radio':
353 case 'Select':
354 case 'Select Country':
355 case 'Select State/Province':
356 $editable = TRUE;
357 $fieldAttributes['data-type'] = $spec['data_type'] == 'Boolean' ? 'boolean' : 'select';
358 if (!$spec['is_required']) {
359 $fieldAttributes['data-empty-option'] = ts('- none -');
360 }
361 break;
362 }
363 }
364 if ($editable) {
365 $fieldAttributes['class'] .= ' crm-editable';
366 }
367 $attributes[$fieldId][$recId] = $fieldAttributes;
368
369 $op = NULL;
370 if ($this->_pageViewType == 'profileDataView') {
371 $actionParams = array(
372 'recordId' => $recId,
373 'gid' => $this->_profileId,
374 'id' => $this->_contactId,
375 );
376 $op = 'profile.multiValue.row';
377 }
378 else {
379 // different set of url params
380 $actionParams['gid'] = $actionParams['groupID'] = $this->_customGroupId;
381 $actionParams['cid'] = $actionParams['entityID'] = $this->_contactId;
382 $actionParams['recId'] = $recId;
383 $actionParams['type'] = $this->_contactType;
384 $actionParams['cgcount'] = $cgcount;
385 $actionParams['newCgCount'] = $newCgCount;
386
387 // DELETE action links
388 $deleteData = array(
389 'valueID' => $recId,
390 'groupID' => $this->_customGroupId,
391 'contactId' => $this->_contactId,
392 'key' => CRM_Core_Key::get('civicrm/ajax/customvalue'),
393 );
394 $links[CRM_Core_Action::DELETE]['url'] = '#';
395 $links[CRM_Core_Action::DELETE]['extra'] = ' data-delete_params="' . htmlspecialchars(json_encode($deleteData)) . '"';
396 $links[CRM_Core_Action::DELETE]['class'] = 'delete-custom-row';
397 }
398 if (!empty($pageCheckSum)) {
399 $actionParams['cs'] = $pageCheckSum;
400 }
401
402 $value['action'] = CRM_Core_Action::formLink(
403 $links,
404 $linkAction,
405 $actionParams,
406 ts('more'),
407 FALSE,
408 $op,
409 'customValue',
410 $fieldId // not ideal, but the one thing not sent in $actionParams
411 );
412 }
413 }
414 $cgcount++;
415 }
416 }
417 }
418
419 $headers = array();
420 if (!empty($fieldIDs)) {
421 foreach ($fieldIDs as $fieldID) {
422 $headers[$fieldID] = ($this->_pageViewType == 'profileDataView') ? $customGroupInfo[$fieldID]['fieldLabel'] : $fieldLabels[$fieldID]['label'];
423 }
424 }
425 $this->assign('dateFields', $dateFields);
426 $this->assign('dateFieldsVals', $dateFieldsVals);
427 $this->assign('cgcount', $cgcount);
428 $this->assign('customGroupTitle', $this->_customGroupTitle);
429 $this->assign('headers', $headers);
430 $this->assign('records', $result);
431 $this->assign('attributes', $attributes);
432 }
433
434 /**
435 * Get name of edit form.
436 *
437 * @return string
438 * classname of edit form
439 */
440 public function editForm() {
441 return '';
442 }
443
444 /**
445 * Get edit form name.
446 *
447 * @return string
448 * name of this page
449 */
450 public function editName() {
451 return '';
452 }
453
454 /**
455 * Get user context.
456 *
457 * @param null $mode
458 *
459 * @return string
460 * user context
461 */
462 public function userContext($mode = NULL) {
463 return '';
464 }
465
466 }