Merge pull request #4806 from civicrm/4.5
[civicrm-core.git] / CRM / Profile / Page / MultipleRecordFieldsListing.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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 * @static
42 */
43 static $_links = NULL;
44
45 protected $_fields = NULL;
46
47 protected $_profileId = NULL;
48
49 protected $_contactId = NULL;
50
51 protected $_customGroupTitle = NULL;
52
53 protected $_pageViewType = NULL;
54
55 protected $_contactType = NULL;
56
57 /**
58 * Get BAO Name
59 *
60 * @return string Classname of BAO.
61 */
62 function getBAOName() {
63 return '';
64 }
65
66 /**
67 * Get action Links
68 *
69 * @return array (reference) of action links
70 */
71 function &links() {
72 if (!(self::$_links[$this->_pageViewType])) {
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);
79
80 // names and titles
81 $links[CRM_Core_Action::VIEW] = array(
82 'name' => ts('View'),
83 'title' => ts('View %1', array( 1 => $this->_customGroupTitle . ' record')),
84 );
85
86 $links[CRM_Core_Action::UPDATE] = array(
87 'name' => ts('Edit'),
88 'title' => ts('Edit %1', array( 1 => $this->_customGroupTitle . ' record')),
89 );
90
91 $links[CRM_Core_Action::DELETE] = array(
92 'name' => ts('Delete'),
93 'title' => ts('Delete %1', array( 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%%&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] = array(
120 'name' => ts('Copy'),
121 'title' => ts('Copy %1', array( 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 * @return void
140 * @access public
141 *
142 */
143 function run() {
144 // get the requested action, default to 'browse'
145 $action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, FALSE);
146
147 // assign vars to templates
148 $this->assign('action', $action);
149 $profileId = CRM_Utils_Request::retrieve('profileId', 'Positive', $this, FALSE);
150 if (!is_array($profileId) && is_numeric($profileId)) {
151 $this->_profileId = $profileId;
152 }
153
154 $this->_contactId = CRM_Utils_Request::retrieve('contactId', 'Positive', $this, FALSE);
155 $this->_pageViewType = CRM_Utils_Request::retrieve('pageViewType', 'Positive', $this, FALSE, 'profileDataView');
156 $this->_customGroupId = CRM_Utils_Request::retrieve('customGroupId', 'Positive', $this, FALSE, 0);
157 $this->_contactType = CRM_Utils_Request::retrieve('contactType', 'String', $this, FALSE);
158 if ($action & CRM_Core_Action::BROWSE) {
159 //browse
160 $this->browse();
161 return;
162 }
163 // parent run
164 return parent::run();
165 }
166
167 /**
168 * Browse the listing
169 *
170 * @return void
171 * @access public
172 */
173 function browse() {
174 $dateFields = NULL;
175 $cgcount = 0;
176 $dateFieldsVals = NULL;
177 if ($this->_pageViewType == 'profileDataView' && $this->_profileId) {
178 $fields = CRM_Core_BAO_UFGroup::getFields($this->_profileId, FALSE, NULL,
179 NULL, NULL,
180 FALSE, NULL,
181 FALSE,
182 NULL,
183 CRM_Core_Permission::EDIT
184 );
185 $multiRecordFields = array( );
186 $fieldIDs = NULL;
187 $result = NULL;
188 $multiRecordFieldsWithSummaryListing = CRM_Core_BAO_UFGroup::shiftMultiRecordFields($fields, $multiRecordFields, TRUE);
189
190 $multiFieldId = CRM_Core_BAO_CustomField::getKeyID(key($multiRecordFields));
191 $customGroupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $multiFieldId, 'custom_group_id');
192 $reached = CRM_Core_BAO_CustomGroup::hasReachedMaxLimit($customGroupId, $this->_contactId);
193 if (!$reached) {
194 $this->assign('contactId', $this->_contactId);
195 $this->assign('gid', $this->_profileId);
196 }
197 $this->assign('reachedMax', $reached);
198
199 if ($multiRecordFieldsWithSummaryListing && !empty($multiRecordFieldsWithSummaryListing)) {
200 $fieldIDs = array_keys($multiRecordFieldsWithSummaryListing);
201 }
202 }
203 elseif ($this->_pageViewType == 'customDataView') {
204 // require custom group id for _pageViewType of customDataView
205 $customGroupId = $this->_customGroupId;
206 $reached = CRM_Core_BAO_CustomGroup::hasReachedMaxLimit($customGroupId, $this->_contactId);
207 if (!$reached) {
208 $this->assign('contactId', $this->_contactId);
209 $this->assign('customGroupId', $customGroupId);
210 $this->assign('ctype', $this->_contactType);
211 }
212 $this->assign('reachedMax', $reached);
213 // custom group info : this consists of the field title of group fields
214 $groupDetail = CRM_Core_BAO_CustomGroup::getGroupDetail($customGroupId, NULL, CRM_Core_DAO::$_nullObject, TRUE);
215 // field ids of fields in_selector for the custom group id provided
216 $fieldIDs = array_keys($groupDetail[$customGroupId]['fields']);
217 // field labels for headers
218 $fieldLabels = $groupDetail[$customGroupId]['fields'];
219
220 // from the above customGroupInfo we can get $this->_customGroupTitle
221 $this->_customGroupTitle = $groupDetail[$customGroupId]['title'];
222 }
223 if ($fieldIDs && !empty($fieldIDs) && $this->_contactId) {
224 $options = array( );
225 $returnProperities = array('html_type', 'data_type', 'date_format', 'time_format');
226 foreach ($fieldIDs as $key => $fieldID) {
227 $fieldIDs[$key] = !is_numeric($fieldID) ? CRM_Core_BAO_CustomField::getKeyID($fieldID) : $fieldID;
228 $param = array('id' => $fieldIDs[$key]);
229 $returnValues = array( );
230 CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_CustomField', $param, $returnValues, $returnProperities);
231 if ($returnValues['data_type'] == 'Date') {
232 $dateFields[$fieldIDs[$key]] = 1;
233 }
234
235 $optionValuePairs = CRM_Core_BAO_CustomOption::getCustomOption($fieldIDs[$key]);
236 if (!empty($optionValuePairs)) {
237 foreach ($optionValuePairs as $optionPairs) {
238 $options[$fieldIDs[$key]][$optionPairs['value']] = $optionPairs['label'];
239 }
240 }
241
242 $options[$fieldIDs[$key]]['attributes']['html_type'] = $returnValues['html_type'];
243 $options[$fieldIDs[$key]]['attributes']['data_type'] = $returnValues['data_type'];
244
245 $options[$fieldIDs[$key]]['attributes']['format'] =
246 $options[$fieldIDs[$key]]['attributes']['date_format'] = CRM_Utils_Array::value('date_format', $returnValues);
247 $options[$fieldIDs[$key]]['attributes']['time_format'] = CRM_Utils_Array::value('time_format', $returnValues);
248 }
249
250 // commonly used for both views i.e profile listing view (profileDataView) and custom data listing view (customDataView)
251 $result = CRM_Core_BAO_CustomValueTable::getEntityValues($this->_contactId, NULL, $fieldIDs, TRUE);
252
253 if ($this->_pageViewType == 'profileDataView') {
254 if (!empty($fieldIDs)) {
255 //get the group info of multi rec fields in listing view
256 $fieldInput = $fieldIDs;
257 $fieldIdInput = $fieldIDs[0];
258 }
259 else {
260 //if no listing fields exist, take the group title for display
261 $nonListingFieldIds = array_keys($multiRecordFields);
262 $singleField = CRM_Core_BAO_CustomField::getKeyID($nonListingFieldIds[0]);
263 $fieldIdInput = $singleField;
264 $singleField = array($singleField);
265 $fieldInput = $singleField;
266 }
267 $customGroupInfo = CRM_Core_BAO_CustomGroup::getGroupTitles($fieldInput);
268 $this->_customGroupTitle = $customGroupInfo[$fieldIdInput]['groupTitle'];
269 }
270 // $cgcount is defined before 'if' condition as enitiy may have no record
271 // and $cgcount is used to build new record url
272 $cgcount = 1;
273 if ($result && !empty($result)) {
274 $links = self::links();
275 if ($this->_pageViewType == 'profileDataView') {
276 $pageCheckSum = $this->get('pageCheckSum');
277 if ($pageCheckSum) {
278 foreach ($links as $key => $link) {
279 $links[$key] = $link['qs'] . "&cs=%%cs%%";
280 }
281 }
282 }
283 $linkAction = array_sum(array_keys($this->links()));
284
285 if ($reached) {
286 unset($links[CRM_Core_Action::COPY]);
287 }
288 $newCgCount = (!$reached) ? count($result) + 1 : NULL;
289 foreach ($result as $recId => &$value) {
290 foreach ($value as $fieldId => &$val) {
291 if (is_numeric($fieldId)) {
292 $customValue = &$val;
293 if (!empty($dateFields) && array_key_exists($fieldId, $dateFields)) {
294 // formated date capture value capture
295 $dateFieldsVals[$fieldId][$recId] = CRM_Core_BAO_CustomField::getDisplayValue($customValue, $fieldId, $options);
296 }
297 else {
298 // assign to $result
299 $customValue = CRM_Core_BAO_CustomField::getDisplayValue($customValue, $fieldId, $options);
300 }
301 if (!$customValue) {
302 $customValue = "";
303 }
304
305 $op = NULL;
306 if ($this->_pageViewType == 'profileDataView') {
307 $actionParams = array('recordId' => $recId, 'gid' => $this->_profileId,
308 'id' => $this->_contactId);
309 $op = 'profile.multiValue.row';
310 }
311 else {
312 // different set of url params
313 $actionParams['gid'] = $actionParams['groupID'] = $this->_customGroupId;
314 $actionParams['cid'] = $actionParams['entityID'] = $this->_contactId;
315 $actionParams['recId'] = $recId;
316 $actionParams['type'] = $this->_contactType;
317 $actionParams['cgcount'] = $cgcount;
318 $actionParams['newCgCount'] = $newCgCount;
319
320 // DELETE action links
321 $deleteData = array(
322 'valueID' => $recId,
323 'groupID' => $this->_customGroupId,
324 'contactId' => $this->_contactId,
325 'key' => CRM_Core_Key::get('civicrm/ajax/customvalue'),
326 );
327 $links[CRM_Core_Action::DELETE]['url'] = '#';
328 $links[CRM_Core_Action::DELETE]['extra'] = ' data-delete_params="'. htmlspecialchars(json_encode($deleteData)) .'"';
329 $links[CRM_Core_Action::DELETE]['class'] = 'delete-custom-row';
330 }
331 if (!empty($pageCheckSum)) {
332 $actionParams['cs'] = $pageCheckSum;
333 }
334
335 $value['action'] = CRM_Core_Action::formLink(
336 $links,
337 $linkAction,
338 $actionParams,
339 ts('more'),
340 FALSE,
341 $op,
342 'customValue',
343 $fieldId // not ideal, but the one thing not sent in $actionParams
344 );
345 }
346 }
347 $cgcount++;
348 }
349 }
350 }
351
352 $headers = array( );
353 if (!empty($fieldIDs)) {
354 foreach ($fieldIDs as $fieldID) {
355 $headers[$fieldID] = ($this->_pageViewType == 'profileDataView') ? $customGroupInfo[$fieldID]['fieldLabel'] : $fieldLabels[$fieldID]['label'];
356 }
357 }
358 $this->assign('dateFields', $dateFields);
359 $this->assign('dateFieldsVals',$dateFieldsVals);
360 $this->assign('cgcount', $cgcount);
361 $this->assign('customGroupTitle', $this->_customGroupTitle);
362 $this->assign('headers', $headers);
363 $this->assign('records', $result);
364 }
365
366 /**
367 * Get name of edit form
368 *
369 * @return string classname of edit form
370 */
371 function editForm() {
372 return '';
373 }
374
375 /**
376 * Get edit form name
377 *
378 * @return string name of this page
379 */
380 function editName() {
381 return '';
382 }
383
384 /**
385 * Get user context
386 *
387 * @param null $mode
388 *
389 * @return string user context
390 */
391 function userContext($mode = NULL) {
392 return '';
393 }
394 }
395