CRM-12595 fix formatting in CRM/P* files
[civicrm-core.git] / CRM / Profile / Page / MultipleRecordFieldsListing.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
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 */
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 * 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);
74
75 $links[CRM_Core_Action::VIEW] = array(
76 'name' => ts('View'),
77 'url' => 'civicrm/profile/view',
78 'qs' => "id=%%id%%&recordId=%%recordId%%&gid=%%gid%%&multiRecord={$view}&snippet=1&context=multiProfileDialog",
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',
85 'qs' => "id=%%id%%&recordId=%%recordId%%&gid=%%gid%%&multiRecord={$update}&snippet=1&context=multiProfileDialog",
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',
92 'qs' => "id=%%id%%&recordId=%%recordId%%&gid=%%gid%%&multiRecord={$delete}&snippet=1&context=multiProfileDialog",
93 'title' => ts('Delete %1', array( 1 => $this->_customGroupTitle . ' record')),
94 );
95
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);
115
116 // assign vars to templates
117 $this->assign('action', $action);
118 $profileId = CRM_Utils_Request::retrieve('profileId', 'Positive', $this, FALSE);
119 if (!is_array($profileId) && is_numeric($profileId)) {
120 $this->_profileId = $profileId;
121 }
122 //record id
123 $recid = CRM_Utils_Request::retrieve('recid', 'Positive', $this, FALSE, 0);
124 //custom group id
125 $groupId = CRM_Utils_Request::retrieve('groupId', 'Positive', $this, FALSE, 0);
126
127 $this->_contactId = CRM_Utils_Request::retrieve('contactId', 'Positive', $this, FALSE);
128
129 if ($action & CRM_Core_Action::BROWSE) {
130 //browse
131 $this->browse();
132 return;
133 }
134 // parent run
135 return parent::run();
136 }
137
138 /**
139 * Browse the listing
140 *
141 * @return void
142 * @access public
143 */
144 function browse() {
145 if ($this->_profileId) {
146 $fields = CRM_Core_BAO_UFGroup::getFields($this->_profileId, FALSE, NULL,
147 NULL, NULL,
148 FALSE, NULL,
149 FALSE,
150 NULL,
151 CRM_Core_Permission::EDIT
152 );
153 $multiRecordFields = array( );
154 $fieldIDs = NULL;
155 $result = NULL;
156 $multiRecordFieldsWithSummaryListing = CRM_Core_BAO_UFGroup::shiftMultiRecordFields($fields, $multiRecordFields, TRUE);
157
158 $multiFieldId = CRM_Core_BAO_CustomField::getKeyID(key($multiRecordFields));
159 $customGroupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $multiFieldId, 'custom_group_id');
160 $reached = CRM_Core_BAO_CustomGroup::hasReachedMaxLimit($customGroupId, $this->_contactId);
161 if (!$reached) {
162 $this->assign('contactId', $this->_contactId);
163 $this->assign('gid', $this->_profileId);
164 }
165 $this->assign('reachedMax', $reached);
166
167 if ($multiRecordFieldsWithSummaryListing && !empty($multiRecordFieldsWithSummaryListing)) {
168 $fieldIDs = array_keys($multiRecordFieldsWithSummaryListing);
169 }
170 }
171
172 if ($fieldIDs && !empty($fieldIDs) && $this->_contactId) {
173 $options = array( );
174 $returnProperities = array('html_type', 'data_type', 'date_format', 'time_format');
175 foreach ($fieldIDs as $key => $fieldID) {
176 $fieldIDs[$key] = CRM_Core_BAO_CustomField::getKeyID($fieldID);
177 $param = array('id' => $fieldIDs[$key]);
178 $returnValues = array( );
179 CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_CustomField', $param, $returnValues, $returnProperities);
180
181 $optionValuePairs = CRM_Core_BAO_CustomOption::getCustomOption($fieldIDs[$key]);
182 if (!empty($optionValuePairs)) {
183 foreach ($optionValuePairs as $optionPairs) {
184 $options[$fieldIDs[$key]][$optionPairs['value']] = $optionPairs['label'];
185 }
186 }
187
188 $options[$fieldIDs[$key]]['attributes']['html_type'] = $returnValues['html_type'];
189 $options[$fieldIDs[$key]]['attributes']['data_type'] = $returnValues['data_type'];
190
191 $options[$fieldIDs[$key]]['attributes']['format'] =
192 $options[$fieldIDs[$key]]['attributes']['date_format'] = CRM_Utils_Array::value('date_format', $returnValues);
193 $options[$fieldIDs[$key]]['attributes']['time_format'] = CRM_Utils_Array::value('time_format', $returnValues);
194 }
195
196 $result = CRM_Core_BAO_CustomValueTable::getEntityValues($this->_contactId, NULL, $fieldIDs, TRUE);
197
198 if (!empty($fieldIDs)) {
199 //get the group info of multi rec fields in listing view
200 $fieldInput = $fieldIDs;
201 $fieldIdInput = $fieldIDs[0];
202 } else {
203 //if no listing fields exist, take the group title for display
204 $nonListingFieldIds = array_keys($multiRecordFields);
205 $singleField = CRM_Core_BAO_CustomField::getKeyID($nonListingFieldIds[0]);
206 $fieldIdInput = $singleField;
207 $singleField = array($singleField);
208 $fieldInput = $singleField;
209 }
210 $customGroupInfo = CRM_Core_BAO_CustomGroup::getGroupTitles($fieldInput);
211 $this->_customGroupTitle = $customGroupInfo[$fieldIdInput]['groupTitle'];
212
213 if ($result && !empty($result)) {
214 $links = self::links();
215 $pageCheckSum = $this->get('pageCheckSum');
216 if ($pageCheckSum) {
217 foreach ($links as $key => $link) {
218 $links[$key] = $link['qs'] . "&cs=%%cs%%";
219 }
220 }
221 $linkAction = array_sum(array_keys($this->links()));
222
223 foreach ($result as $recId => &$value) {
224 foreach ($value as $fieldId => &$val) {
225 if (is_numeric($fieldId)) {
226 $customValue = &$val;
227 $customValue = CRM_Core_BAO_CustomField::getDisplayValue($customValue, $fieldId, $options);
228 if (!$customValue) {
229 $customValue = "";
230 }
231 $actionParams = array('recordId' => $recId, 'gid' => $this->_profileId,
232 'id' => $this->_contactId);
233 if ($pageCheckSum) {
234 $actionParams['cs'] = $pageCheckSum;
235 }
236 $value['action'] =
237 CRM_Core_Action::formLink($links, $linkAction, $actionParams);
238 }
239 }
240 }
241 }
242 }
243
244 $headers = array( );
245 if (!empty($fieldIDs)) {
246 foreach ($fieldIDs as $fieldID) {
247 $headers[$fieldID] = $customGroupInfo[$fieldID]['fieldLabel'];
248 }
249 }
250 $this->assign('customGroupTitle', $this->_customGroupTitle);
251 $this->assign('headers', $headers);
252 $this->assign('records', $result);
253 }
254
255 /**
256 * Get name of edit form
257 *
258 * @return string classname of edit form
259 */
260 function editForm() {
261 return '';
262 }
263
264 /**
265 * Get edit form name
266 *
267 * @return string name of this page
268 */
269 function editName() {
270 return '';
271 }
272
273 /**
274 * Get user context
275 *
276 * @return string user context
277 */
278 function userContext($mode = NULL) {
279 return '';
280 }
281 }
282