phpcs - Fix error, "Expected 1 newline at end of file; XXX found".
[civicrm-core.git] / CRM / Profile / Page / MultipleRecordFieldsListing.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35class 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 * @static
42 */
43 static $_links = NULL;
44
45 protected $_fields = NULL;
46
47 protected $_profileId = NULL;
366fe2a3 48
6a488035
TO
49 protected $_contactId = NULL;
50
366fe2a3 51 protected $_customGroupTitle = NULL;
e41f4660
PJ
52
53 protected $_pageViewType = NULL;
54
55 protected $_contactType = NULL;
fcc8f207 56
6a488035
TO
57 /**
58 * Get BAO Name
59 *
60 * @return string Classname of BAO.
61 */
00be9182 62 public function getBAOName() {
6a488035
TO
63 return '';
64 }
65
66 /**
67 * Get action Links
68 *
69 * @return array (reference) of action links
70 */
00be9182 71 public function &links() {
e41f4660 72 if (!(self::$_links[$this->_pageViewType])) {
6a488035
TO
73 // helper variable for nicer formatting
74 $links = array();
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);
366fe2a3 79
1dde099b 80 // names and titles
6a488035
TO
81 $links[CRM_Core_Action::VIEW] = array(
82 'name' => ts('View'),
6a488035
TO
83 'title' => ts('View %1', array( 1 => $this->_customGroupTitle . ' record')),
84 );
85
86 $links[CRM_Core_Action::UPDATE] = array(
87 'name' => ts('Edit'),
6a488035
TO
88 'title' => ts('Edit %1', array( 1 => $this->_customGroupTitle . ' record')),
89 );
90
91 $links[CRM_Core_Action::DELETE] = array(
92 'name' => ts('Delete'),
6a488035
TO
93 'title' => ts('Delete %1', array( 1 => $this->_customGroupTitle . ' record')),
94 );
366fe2a3 95
1dde099b 96 // urls and queryStrings
e41f4660 97 if ($this->_pageViewType == 'profileDataView') {
1dde099b 98 $links[CRM_Core_Action::VIEW]['url'] = 'civicrm/profile/view';
7ab9bfe8 99 $links[CRM_Core_Action::VIEW]['qs'] = "reset=1&id=%%id%%&recordId=%%recordId%%&gid=%%gid%%&multiRecord={$view}";
1dde099b
PJ
100
101 $links[CRM_Core_Action::UPDATE]['url'] = 'civicrm/profile/edit';
7ab9bfe8 102 $links[CRM_Core_Action::UPDATE]['qs'] = "reset=1&id=%%id%%&recordId=%%recordId%%&gid=%%gid%%&multiRecord={$update}";
1dde099b 103
e41f4660 104 $links[CRM_Core_Action::DELETE]['url'] = 'civicrm/profile/edit';
7ab9bfe8 105 $links[CRM_Core_Action::DELETE]['qs'] = "reset=1&id=%%id%%&recordId=%%recordId%%&gid=%%gid%%&multiRecord={$delete}";
1dde099b
PJ
106
107 }
108 elseif ($this->_pageViewType == 'customDataView') {
109 // custom data specific view links
110 $links[CRM_Core_Action::VIEW]['url'] = 'civicrm/contact/view/cd';
525faea3 111 $links[CRM_Core_Action::VIEW]['qs'] = 'reset=1&gid=%%gid%%&cid=%%cid%%&recId=%%recId%%&multiRecordDisplay=single&mode=view';
1dde099b
PJ
112
113 // custom data specific update links
114 $links[CRM_Core_Action::UPDATE]['url'] = 'civicrm/contact/view/cd/edit';
525faea3 115 $links[CRM_Core_Action::UPDATE]['qs'] = 'reset=1&type=%%type%%&groupID=%%groupID%%&entityID=%%entityID%%&cgcount=%%cgcount%%&multiRecordDisplay=single&mode=edit';
1dde099b
PJ
116 // NOTE : links for DELETE action for customDataView is handled in browse
117
118 // copy action
119 $links[CRM_Core_Action::COPY] = array(
120 'name' => ts('Copy'),
121 'title' => ts('Copy %1', array( 1 => $this->_customGroupTitle . ' record')),
122 'url' => 'civicrm/contact/view/cd/edit',
525faea3 123 'qs' => 'reset=1&type=%%type%%&groupID=%%groupID%%&entityID=%%entityID%%&cgcount=%%newCgCount%%&multiRecordDisplay=single&copyValueId=%%cgcount%%&mode=copy'
1dde099b 124 );
e41f4660
PJ
125 }
126
127 self::$_links[$this->_pageViewType] = $links;
6a488035 128 }
e41f4660 129 return self::$_links[$this->_pageViewType];
6a488035
TO
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 * @return void
6a488035
TO
140 *
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 /**
167 * Browse the listing
168 *
169 * @return void
6a488035 170 */
00be9182 171 public function browse() {
b05a0fb6 172 $dateFields = NULL;
cbf841b5 173 $cgcount = 0;
b05a0fb6 174 $dateFieldsVals = NULL;
e41f4660 175 if ($this->_pageViewType == 'profileDataView' && $this->_profileId) {
6a488035
TO
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);
366fe2a3 187
6a488035
TO
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);
366fe2a3 196
6a488035
TO
197 if ($multiRecordFieldsWithSummaryListing && !empty($multiRecordFieldsWithSummaryListing)) {
198 $fieldIDs = array_keys($multiRecordFieldsWithSummaryListing);
199 }
200 }
e41f4660
PJ
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 }
6a488035
TO
221 if ($fieldIDs && !empty($fieldIDs) && $this->_contactId) {
222 $options = array( );
223 $returnProperities = array('html_type', 'data_type', 'date_format', 'time_format');
224 foreach ($fieldIDs as $key => $fieldID) {
e41f4660 225 $fieldIDs[$key] = !is_numeric($fieldID) ? CRM_Core_BAO_CustomField::getKeyID($fieldID) : $fieldID;
6a488035
TO
226 $param = array('id' => $fieldIDs[$key]);
227 $returnValues = array( );
228 CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_CustomField', $param, $returnValues, $returnProperities);
b05a0fb6
PJ
229 if ($returnValues['data_type'] == 'Date') {
230 $dateFields[$fieldIDs[$key]] = 1;
231 }
366fe2a3 232
6a488035
TO
233 $optionValuePairs = CRM_Core_BAO_CustomOption::getCustomOption($fieldIDs[$key]);
234 if (!empty($optionValuePairs)) {
235 foreach ($optionValuePairs as $optionPairs) {
236 $options[$fieldIDs[$key]][$optionPairs['value']] = $optionPairs['label'];
237 }
238 }
366fe2a3 239
6a488035
TO
240 $options[$fieldIDs[$key]]['attributes']['html_type'] = $returnValues['html_type'];
241 $options[$fieldIDs[$key]]['attributes']['data_type'] = $returnValues['data_type'];
366fe2a3 242
243 $options[$fieldIDs[$key]]['attributes']['format'] =
6a488035
TO
244 $options[$fieldIDs[$key]]['attributes']['date_format'] = CRM_Utils_Array::value('date_format', $returnValues);
245 $options[$fieldIDs[$key]]['attributes']['time_format'] = CRM_Utils_Array::value('time_format', $returnValues);
246 }
366fe2a3 247
e41f4660 248 // commonly used for both views i.e profile listing view (profileDataView) and custom data listing view (customDataView)
6a488035 249 $result = CRM_Core_BAO_CustomValueTable::getEntityValues($this->_contactId, NULL, $fieldIDs, TRUE);
366fe2a3 250
e41f4660
PJ
251 if ($this->_pageViewType == 'profileDataView') {
252 if (!empty($fieldIDs)) {
253 //get the group info of multi rec fields in listing view
254 $fieldInput = $fieldIDs;
255 $fieldIdInput = $fieldIDs[0];
256 }
257 else {
258 //if no listing fields exist, take the group title for display
259 $nonListingFieldIds = array_keys($multiRecordFields);
260 $singleField = CRM_Core_BAO_CustomField::getKeyID($nonListingFieldIds[0]);
261 $fieldIdInput = $singleField;
262 $singleField = array($singleField);
263 $fieldInput = $singleField;
264 }
265 $customGroupInfo = CRM_Core_BAO_CustomGroup::getGroupTitles($fieldInput);
266 $this->_customGroupTitle = $customGroupInfo[$fieldIdInput]['groupTitle'];
267 }
268 // $cgcount is defined before 'if' condition as enitiy may have no record
269 // and $cgcount is used to build new record url
270 $cgcount = 1;
271 if ($result && !empty($result)) {
eb2b4bdb 272 $links = self::links();
e41f4660
PJ
273 if ($this->_pageViewType == 'profileDataView') {
274 $pageCheckSum = $this->get('pageCheckSum');
275 if ($pageCheckSum) {
276 foreach ($links as $key => $link) {
277 $links[$key] = $link['qs'] . "&cs=%%cs%%";
278 }
279 }
280 }
281 $linkAction = array_sum(array_keys($this->links()));
1dde099b
PJ
282
283 if ($reached) {
284 unset($links[CRM_Core_Action::COPY]);
285 }
286 $newCgCount = (!$reached) ? count($result) + 1 : NULL;
e41f4660
PJ
287 foreach ($result as $recId => &$value) {
288 foreach ($value as $fieldId => &$val) {
289 if (is_numeric($fieldId)) {
290 $customValue = &$val;
5f1aa3a5 291 if (!empty($dateFields) && array_key_exists($fieldId, $dateFields)) {
b05a0fb6
PJ
292 // formated date capture value capture
293 $dateFieldsVals[$fieldId][$recId] = CRM_Core_BAO_CustomField::getDisplayValue($customValue, $fieldId, $options);
294 }
295 else {
296 // assign to $result
297 $customValue = CRM_Core_BAO_CustomField::getDisplayValue($customValue, $fieldId, $options);
298 }
e41f4660
PJ
299 if (!$customValue) {
300 $customValue = "";
301 }
302
303 $op = NULL;
304 if ($this->_pageViewType == 'profileDataView') {
305 $actionParams = array('recordId' => $recId, 'gid' => $this->_profileId,
7ab9bfe8 306 'id' => $this->_contactId);
e41f4660
PJ
307 $op = 'profile.multiValue.row';
308 }
309 else {
310 // different set of url params
311 $actionParams['gid'] = $actionParams['groupID'] = $this->_customGroupId;
312 $actionParams['cid'] = $actionParams['entityID'] = $this->_contactId;
313 $actionParams['recId'] = $recId;
314 $actionParams['type'] = $this->_contactType;
315 $actionParams['cgcount'] = $cgcount;
1dde099b 316 $actionParams['newCgCount'] = $newCgCount;
eb2b4bdb
PJ
317
318 // DELETE action links
a801aa5c
CW
319 $deleteData = array(
320 'valueID' => $recId,
321 'groupID' => $this->_customGroupId,
322 'contactId' => $this->_contactId,
323 'key' => CRM_Core_Key::get('civicrm/ajax/customvalue'),
324 );
eb2b4bdb 325 $links[CRM_Core_Action::DELETE]['url'] = '#';
a801aa5c
CW
326 $links[CRM_Core_Action::DELETE]['extra'] = ' data-delete_params="'. htmlspecialchars(json_encode($deleteData)) .'"';
327 $links[CRM_Core_Action::DELETE]['class'] = 'delete-custom-row';
e41f4660
PJ
328 }
329 if (!empty($pageCheckSum)) {
330 $actionParams['cs'] = $pageCheckSum;
331 }
eb2b4bdb 332
e41f4660
PJ
333 $value['action'] = CRM_Core_Action::formLink(
334 $links,
335 $linkAction,
336 $actionParams,
337 ts('more'),
338 FALSE,
339 $op,
340 'customValue',
341 $fieldId // not ideal, but the one thing not sent in $actionParams
342 );
343 }
344 }
345 $cgcount++;
346 }
347 }
6a488035 348 }
366fe2a3 349
6a488035
TO
350 $headers = array( );
351 if (!empty($fieldIDs)) {
352 foreach ($fieldIDs as $fieldID) {
e41f4660 353 $headers[$fieldID] = ($this->_pageViewType == 'profileDataView') ? $customGroupInfo[$fieldID]['fieldLabel'] : $fieldLabels[$fieldID]['label'];
6a488035
TO
354 }
355 }
b05a0fb6
PJ
356 $this->assign('dateFields', $dateFields);
357 $this->assign('dateFieldsVals',$dateFieldsVals);
e41f4660 358 $this->assign('cgcount', $cgcount);
6a488035
TO
359 $this->assign('customGroupTitle', $this->_customGroupTitle);
360 $this->assign('headers', $headers);
361 $this->assign('records', $result);
362 }
363
364 /**
365 * Get name of edit form
366 *
367 * @return string classname of edit form
368 */
00be9182 369 public function editForm() {
6a488035
TO
370 return '';
371 }
372
373 /**
374 * Get edit form name
375 *
376 * @return string name of this page
377 */
00be9182 378 public function editName() {
6a488035
TO
379 return '';
380 }
381
382 /**
383 * Get user context
384 *
77b97be7
EM
385 * @param null $mode
386 *
6a488035
TO
387 * @return string user context
388 */
00be9182 389 public function userContext($mode = NULL) {
6a488035
TO
390 return '';
391 }
392}