CRM-14463 - Fix custom address fields to support location type
[civicrm-core.git] / CRM / Profile / Page / MultipleRecordFieldsListing.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
232624b1 4 | CiviCRM version 4.4 |
6a488035
TO
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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;
6a488035
TO
52 /**
53 * Get BAO Name
54 *
55 * @return string Classname of BAO.
56 */
57 function getBAOName() {
58 return '';
59 }
60
61 /**
62 * Get action Links
63 *
64 * @return array (reference) of action links
65 */
66 function &links() {
67 if (!(self::$_links)) {
68 // helper variable for nicer formatting
69 $links = array();
70
71 $view = array_search(CRM_Core_Action::VIEW, CRM_Core_Action::$_names);
72 $update = array_search(CRM_Core_Action::UPDATE, CRM_Core_Action::$_names);
73 $delete = array_search(CRM_Core_Action::DELETE, CRM_Core_Action::$_names);
366fe2a3 74
6a488035
TO
75 $links[CRM_Core_Action::VIEW] = array(
76 'name' => ts('View'),
77 'url' => 'civicrm/profile/view',
3a2e7d2b 78 'qs' => "reset=1&id=%%id%%&recordId=%%recordId%%&gid=%%gid%%&multiRecord={$view}&snippet=1&context=multiProfileDialog&onPopupClose=%%onPopupClose%%",
6a488035
TO
79 'title' => ts('View %1', array( 1 => $this->_customGroupTitle . ' record')),
80 );
81
82 $links[CRM_Core_Action::UPDATE] = array(
83 'name' => ts('Edit'),
84 'url' => 'civicrm/profile/edit',
3a2e7d2b 85 'qs' => "reset=1&id=%%id%%&recordId=%%recordId%%&gid=%%gid%%&multiRecord={$update}&snippet=1&context=multiProfileDialog&onPopupClose=%%onPopupClose%%",
6a488035
TO
86 'title' => ts('Edit %1', array( 1 => $this->_customGroupTitle . ' record')),
87 );
88
89 $links[CRM_Core_Action::DELETE] = array(
90 'name' => ts('Delete'),
91 'url' => 'civicrm/profile/edit',
3a2e7d2b 92 'qs' => "reset=1&id=%%id%%&recordId=%%recordId%%&gid=%%gid%%&multiRecord={$delete}&snippet=1&context=multiProfileDialog&onPopupClose=%%onPopupClose%%",
6a488035
TO
93 'title' => ts('Delete %1', array( 1 => $this->_customGroupTitle . ' record')),
94 );
366fe2a3 95
6a488035
TO
96 self::$_links = $links;
97 }
98 return self::$_links;
99 }
100
101 /**
102 * Run the page
103 *
104 * This method is called after the page is created. It checks for the type
105 * of action and executes that action. Finally it calls the parent's run
106 * method.
107 *
108 * @return void
109 * @access public
110 *
111 */
112 function run() {
113 // get the requested action, default to 'browse'
114 $action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, FALSE);
3a2e7d2b 115 $this->_onPopupClose = CRM_Utils_Request::retrieve('onPopupClose', 'String', $this);
6a488035
TO
116
117 // assign vars to templates
118 $this->assign('action', $action);
119 $profileId = CRM_Utils_Request::retrieve('profileId', 'Positive', $this, FALSE);
120 if (!is_array($profileId) && is_numeric($profileId)) {
366fe2a3 121 $this->_profileId = $profileId;
6a488035
TO
122 }
123 //record id
124 $recid = CRM_Utils_Request::retrieve('recid', 'Positive', $this, FALSE, 0);
125 //custom group id
126 $groupId = CRM_Utils_Request::retrieve('groupId', 'Positive', $this, FALSE, 0);
366fe2a3 127
6a488035 128 $this->_contactId = CRM_Utils_Request::retrieve('contactId', 'Positive', $this, FALSE);
366fe2a3 129
6a488035 130 if ($action & CRM_Core_Action::BROWSE) {
366fe2a3 131 //browse
6a488035
TO
132 $this->browse();
133 return;
134 }
135 // parent run
136 return parent::run();
137 }
138
139 /**
140 * Browse the listing
141 *
142 * @return void
143 * @access public
144 */
145 function browse() {
146 if ($this->_profileId) {
147 $fields = CRM_Core_BAO_UFGroup::getFields($this->_profileId, FALSE, NULL,
148 NULL, NULL,
149 FALSE, NULL,
150 FALSE,
151 NULL,
152 CRM_Core_Permission::EDIT
153 );
154 $multiRecordFields = array( );
155 $fieldIDs = NULL;
156 $result = NULL;
157 $multiRecordFieldsWithSummaryListing = CRM_Core_BAO_UFGroup::shiftMultiRecordFields($fields, $multiRecordFields, TRUE);
366fe2a3 158
6a488035
TO
159 $multiFieldId = CRM_Core_BAO_CustomField::getKeyID(key($multiRecordFields));
160 $customGroupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $multiFieldId, 'custom_group_id');
161 $reached = CRM_Core_BAO_CustomGroup::hasReachedMaxLimit($customGroupId, $this->_contactId);
162 if (!$reached) {
163 $this->assign('contactId', $this->_contactId);
164 $this->assign('gid', $this->_profileId);
165 }
166 $this->assign('reachedMax', $reached);
366fe2a3 167
6a488035
TO
168 if ($multiRecordFieldsWithSummaryListing && !empty($multiRecordFieldsWithSummaryListing)) {
169 $fieldIDs = array_keys($multiRecordFieldsWithSummaryListing);
170 }
171 }
366fe2a3 172
6a488035
TO
173 if ($fieldIDs && !empty($fieldIDs) && $this->_contactId) {
174 $options = array( );
175 $returnProperities = array('html_type', 'data_type', 'date_format', 'time_format');
176 foreach ($fieldIDs as $key => $fieldID) {
177 $fieldIDs[$key] = CRM_Core_BAO_CustomField::getKeyID($fieldID);
178 $param = array('id' => $fieldIDs[$key]);
179 $returnValues = array( );
180 CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_CustomField', $param, $returnValues, $returnProperities);
366fe2a3 181
6a488035
TO
182 $optionValuePairs = CRM_Core_BAO_CustomOption::getCustomOption($fieldIDs[$key]);
183 if (!empty($optionValuePairs)) {
184 foreach ($optionValuePairs as $optionPairs) {
185 $options[$fieldIDs[$key]][$optionPairs['value']] = $optionPairs['label'];
186 }
187 }
366fe2a3 188
6a488035
TO
189 $options[$fieldIDs[$key]]['attributes']['html_type'] = $returnValues['html_type'];
190 $options[$fieldIDs[$key]]['attributes']['data_type'] = $returnValues['data_type'];
366fe2a3 191
192 $options[$fieldIDs[$key]]['attributes']['format'] =
6a488035
TO
193 $options[$fieldIDs[$key]]['attributes']['date_format'] = CRM_Utils_Array::value('date_format', $returnValues);
194 $options[$fieldIDs[$key]]['attributes']['time_format'] = CRM_Utils_Array::value('time_format', $returnValues);
195 }
366fe2a3 196
6a488035 197 $result = CRM_Core_BAO_CustomValueTable::getEntityValues($this->_contactId, NULL, $fieldIDs, TRUE);
366fe2a3 198
6a488035
TO
199 if (!empty($fieldIDs)) {
200 //get the group info of multi rec fields in listing view
201 $fieldInput = $fieldIDs;
202 $fieldIdInput = $fieldIDs[0];
203 } else {
204 //if no listing fields exist, take the group title for display
205 $nonListingFieldIds = array_keys($multiRecordFields);
206 $singleField = CRM_Core_BAO_CustomField::getKeyID($nonListingFieldIds[0]);
207 $fieldIdInput = $singleField;
208 $singleField = array($singleField);
209 $fieldInput = $singleField;
210 }
211 $customGroupInfo = CRM_Core_BAO_CustomGroup::getGroupTitles($fieldInput);
212 $this->_customGroupTitle = $customGroupInfo[$fieldIdInput]['groupTitle'];
366fe2a3 213
6a488035
TO
214 if ($result && !empty($result)) {
215 $links = self::links();
216 $pageCheckSum = $this->get('pageCheckSum');
217 if ($pageCheckSum) {
218 foreach ($links as $key => $link) {
219 $links[$key] = $link['qs'] . "&cs=%%cs%%";
220 }
221 }
222 $linkAction = array_sum(array_keys($this->links()));
366fe2a3 223
6a488035
TO
224 foreach ($result as $recId => &$value) {
225 foreach ($value as $fieldId => &$val) {
226 if (is_numeric($fieldId)) {
227 $customValue = &$val;
228 $customValue = CRM_Core_BAO_CustomField::getDisplayValue($customValue, $fieldId, $options);
229 if (!$customValue) {
230 $customValue = "";
231 }
232 $actionParams = array('recordId' => $recId, 'gid' => $this->_profileId,
3a2e7d2b 233 'id' => $this->_contactId, 'onPopupClose' => $this->_onPopupClose);
6a488035
TO
234 if ($pageCheckSum) {
235 $actionParams['cs'] = $pageCheckSum;
236 }
237 $value['action'] =
238 CRM_Core_Action::formLink($links, $linkAction, $actionParams);
239 }
240 }
241 }
242 }
243 }
366fe2a3 244
6a488035
TO
245 $headers = array( );
246 if (!empty($fieldIDs)) {
247 foreach ($fieldIDs as $fieldID) {
248 $headers[$fieldID] = $customGroupInfo[$fieldID]['fieldLabel'];
249 }
250 }
251 $this->assign('customGroupTitle', $this->_customGroupTitle);
252 $this->assign('headers', $headers);
253 $this->assign('records', $result);
254 }
255
256 /**
257 * Get name of edit form
258 *
259 * @return string classname of edit form
260 */
261 function editForm() {
262 return '';
263 }
264
265 /**
266 * Get edit form name
267 *
268 * @return string name of this page
269 */
270 function editName() {
271 return '';
272 }
273
274 /**
275 * Get user context
276 *
277 * @return string user context
278 */
279 function userContext($mode = NULL) {
280 return '';
281 }
282}
283