CRM-13992 : custom data single record view handling, DELETE action handling, QA check...
[civicrm-core.git] / CRM / Profile / Page / MultipleRecordFieldsListing.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
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 protected $_pageViewType = NULL;
54
55 protected $_contactType = NULL;
56 /**
57 * Get BAO Name
58 *
59 * @return string Classname of BAO.
60 */
61 function getBAOName() {
62 return '';
63 }
64
65 /**
66 * Get action Links
67 *
68 * @return array (reference) of action links
69 */
70 function &links() {
71 if (!(self::$_links[$this->_pageViewType])) {
72 // helper variable for nicer formatting
73 $links = array();
74
75 $view = array_search(CRM_Core_Action::VIEW, CRM_Core_Action::$_names);
76 $update = array_search(CRM_Core_Action::UPDATE, CRM_Core_Action::$_names);
77 $delete = array_search(CRM_Core_Action::DELETE, CRM_Core_Action::$_names);
78
79 $links[CRM_Core_Action::VIEW] = array(
80 'name' => ts('View'),
81 'title' => ts('View %1', array( 1 => $this->_customGroupTitle . ' record')),
82 );
83
84 if ($this->_pageViewType == 'profileDataView') {
85 $links[CRM_Core_Action::VIEW]['url'] = 'civicrm/profile/view';
86 $links[CRM_Core_Action::VIEW]['qs'] = "reset=1&id=%%id%%&recordId=%%recordId%%&gid=%%gid%%&multiRecord={$view}&snippet=1&context=multiProfileDialog&onPopupClose=%%onPopupClose%%";
87 }
88 elseif ($this->_pageViewType == 'customDataView') {
89 // custom data specific view links
90 $links[CRM_Core_Action::VIEW]['url'] = 'civicrm/contact/view/cd';
91 $links[CRM_Core_Action::VIEW]['qs'] = 'reset=1&snippet=1&gid=%%gid%%&cid=%%cid%%&recId=%%recId%%&multiRecordDisplay=single';
92 }
93
94 $links[CRM_Core_Action::UPDATE] = array(
95 'name' => ts('Edit'),
96 'title' => ts('Edit %1', array( 1 => $this->_customGroupTitle . ' record')),
97 );
98
99 if ($this->_pageViewType == 'profileDataView') {
100 $links[CRM_Core_Action::UPDATE]['url'] = 'civicrm/profile/edit';
101 $links[CRM_Core_Action::UPDATE]['qs'] = "reset=1&id=%%id%%&recordId=%%recordId%%&gid=%%gid%%&multiRecord={$update}&snippet=1&context=multiProfileDialog&onPopupClose=%%onPopupClose%%";
102 }
103 elseif ($this->_pageViewType == 'customDataView') {
104 // custom data specific update links
105 $links[CRM_Core_Action::UPDATE]['url'] = 'civicrm/contact/view/cd/edit';
106 $links[CRM_Core_Action::UPDATE]['qs'] = 'reset=1&snippet=1&type=%%type%%&groupID=%%groupID%%&entityID=%%entityID%%&cgcount=%%cgcount%%&multiRecordDisplay=single';
107 }
108
109 $links[CRM_Core_Action::DELETE] = array(
110 'name' => ts('Delete'),
111 'title' => ts('Delete %1', array( 1 => $this->_customGroupTitle . ' record')),
112 );
113
114 if ($this->_pageViewType == 'profileDataView') {
115 $links[CRM_Core_Action::DELETE]['url'] = 'civicrm/profile/edit';
116 $links[CRM_Core_Action::DELETE]['qs'] = "reset=1&id=%%id%%&recordId=%%recordId%%&gid=%%gid%%&multiRecord={$delete}&snippet=1&context=multiProfileDialog&onPopupClose=%%onPopupClose%%";
117 }
118 // NOTE : links for DELETE action for customDataView is handled in browse
119
120 self::$_links[$this->_pageViewType] = $links;
121 }
122 return self::$_links[$this->_pageViewType];
123 }
124
125 /**
126 * Run the page
127 *
128 * This method is called after the page is created. It checks for the type
129 * of action and executes that action. Finally it calls the parent's run
130 * method.
131 *
132 * @return void
133 * @access public
134 *
135 */
136 function run() {
137 // get the requested action, default to 'browse'
138 $action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, FALSE);
139 $this->_onPopupClose = CRM_Utils_Request::retrieve('onPopupClose', 'String', $this);
140
141 // assign vars to templates
142 $this->assign('action', $action);
143 $profileId = CRM_Utils_Request::retrieve('profileId', 'Positive', $this, FALSE);
144 if (!is_array($profileId) && is_numeric($profileId)) {
145 $this->_profileId = $profileId;
146 }
147
148 $this->_contactId = CRM_Utils_Request::retrieve('contactId', 'Positive', $this, FALSE);
149 $this->_pageViewType = CRM_Utils_Request::retrieve('pageViewType', 'Positive', $this, FALSE, 'profileDataView');
150 $this->_customGroupId = CRM_Utils_Request::retrieve('customGroupId', 'Positive', $this, FALSE, 0);
151 $this->_contactType = CRM_Utils_Request::retrieve('contactType', 'String', $this, FALSE);
152 if ($action & CRM_Core_Action::BROWSE) {
153 //browse
154 $this->browse();
155 return;
156 }
157 // parent run
158 return parent::run();
159 }
160
161 /**
162 * Browse the listing
163 *
164 * @return void
165 * @access public
166 */
167 function browse() {
168 if ($this->_pageViewType == 'profileDataView' && $this->_profileId) {
169 $fields = CRM_Core_BAO_UFGroup::getFields($this->_profileId, FALSE, NULL,
170 NULL, NULL,
171 FALSE, NULL,
172 FALSE,
173 NULL,
174 CRM_Core_Permission::EDIT
175 );
176 $multiRecordFields = array( );
177 $fieldIDs = NULL;
178 $result = NULL;
179 $multiRecordFieldsWithSummaryListing = CRM_Core_BAO_UFGroup::shiftMultiRecordFields($fields, $multiRecordFields, TRUE);
180
181 $multiFieldId = CRM_Core_BAO_CustomField::getKeyID(key($multiRecordFields));
182 $customGroupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $multiFieldId, 'custom_group_id');
183 $reached = CRM_Core_BAO_CustomGroup::hasReachedMaxLimit($customGroupId, $this->_contactId);
184 if (!$reached) {
185 $this->assign('contactId', $this->_contactId);
186 $this->assign('gid', $this->_profileId);
187 }
188 $this->assign('reachedMax', $reached);
189
190 if ($multiRecordFieldsWithSummaryListing && !empty($multiRecordFieldsWithSummaryListing)) {
191 $fieldIDs = array_keys($multiRecordFieldsWithSummaryListing);
192 }
193 }
194 elseif ($this->_pageViewType == 'customDataView') {
195 // require custom group id for _pageViewType of customDataView
196 $customGroupId = $this->_customGroupId;
197 $reached = CRM_Core_BAO_CustomGroup::hasReachedMaxLimit($customGroupId, $this->_contactId);
198 if (!$reached) {
199 $this->assign('contactId', $this->_contactId);
200 $this->assign('customGroupId', $customGroupId);
201 $this->assign('ctype', $this->_contactType);
202 }
203 $this->assign('reachedMax', $reached);
204 // custom group info : this consists of the field title of group fields
205 $groupDetail = CRM_Core_BAO_CustomGroup::getGroupDetail($customGroupId, NULL, CRM_Core_DAO::$_nullObject, TRUE);
206 // field ids of fields in_selector for the custom group id provided
207 $fieldIDs = array_keys($groupDetail[$customGroupId]['fields']);
208 // field labels for headers
209 $fieldLabels = $groupDetail[$customGroupId]['fields'];
210
211 // from the above customGroupInfo we can get $this->_customGroupTitle
212 $this->_customGroupTitle = $groupDetail[$customGroupId]['title'];
213 }
214 if ($fieldIDs && !empty($fieldIDs) && $this->_contactId) {
215 $options = array( );
216 $returnProperities = array('html_type', 'data_type', 'date_format', 'time_format');
217 foreach ($fieldIDs as $key => $fieldID) {
218 $fieldIDs[$key] = !is_numeric($fieldID) ? CRM_Core_BAO_CustomField::getKeyID($fieldID) : $fieldID;
219 $param = array('id' => $fieldIDs[$key]);
220 $returnValues = array( );
221 CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_CustomField', $param, $returnValues, $returnProperities);
222
223 $optionValuePairs = CRM_Core_BAO_CustomOption::getCustomOption($fieldIDs[$key]);
224 if (!empty($optionValuePairs)) {
225 foreach ($optionValuePairs as $optionPairs) {
226 $options[$fieldIDs[$key]][$optionPairs['value']] = $optionPairs['label'];
227 }
228 }
229
230 $options[$fieldIDs[$key]]['attributes']['html_type'] = $returnValues['html_type'];
231 $options[$fieldIDs[$key]]['attributes']['data_type'] = $returnValues['data_type'];
232
233 $options[$fieldIDs[$key]]['attributes']['format'] =
234 $options[$fieldIDs[$key]]['attributes']['date_format'] = CRM_Utils_Array::value('date_format', $returnValues);
235 $options[$fieldIDs[$key]]['attributes']['time_format'] = CRM_Utils_Array::value('time_format', $returnValues);
236 }
237
238 // commonly used for both views i.e profile listing view (profileDataView) and custom data listing view (customDataView)
239 $result = CRM_Core_BAO_CustomValueTable::getEntityValues($this->_contactId, NULL, $fieldIDs, TRUE);
240
241 if ($this->_pageViewType == 'profileDataView') {
242 if (!empty($fieldIDs)) {
243 //get the group info of multi rec fields in listing view
244 $fieldInput = $fieldIDs;
245 $fieldIdInput = $fieldIDs[0];
246 }
247 else {
248 //if no listing fields exist, take the group title for display
249 $nonListingFieldIds = array_keys($multiRecordFields);
250 $singleField = CRM_Core_BAO_CustomField::getKeyID($nonListingFieldIds[0]);
251 $fieldIdInput = $singleField;
252 $singleField = array($singleField);
253 $fieldInput = $singleField;
254 }
255 $customGroupInfo = CRM_Core_BAO_CustomGroup::getGroupTitles($fieldInput);
256 $this->_customGroupTitle = $customGroupInfo[$fieldIdInput]['groupTitle'];
257 }
258 // $cgcount is defined before 'if' condition as enitiy may have no record
259 // and $cgcount is used to build new record url
260 $cgcount = 1;
261 if ($result && !empty($result)) {
262 $links = self::links();
263 if ($this->_pageViewType == 'profileDataView') {
264 $pageCheckSum = $this->get('pageCheckSum');
265 if ($pageCheckSum) {
266 foreach ($links as $key => $link) {
267 $links[$key] = $link['qs'] . "&cs=%%cs%%";
268 }
269 }
270 }
271 $linkAction = array_sum(array_keys($this->links()));
272 foreach ($result as $recId => &$value) {
273 foreach ($value as $fieldId => &$val) {
274 if (is_numeric($fieldId)) {
275 $customValue = &$val;
276 $customValue = CRM_Core_BAO_CustomField::getDisplayValue($customValue, $fieldId, $options);
277 if (!$customValue) {
278 $customValue = "";
279 }
280
281 $op = NULL;
282 if ($this->_pageViewType == 'profileDataView') {
283 $actionParams = array('recordId' => $recId, 'gid' => $this->_profileId,
284 'id' => $this->_contactId, 'onPopupClose' => $this->_onPopupClose);
285 $op = 'profile.multiValue.row';
286 }
287 else {
288 // different set of url params
289 $actionParams['gid'] = $actionParams['groupID'] = $this->_customGroupId;
290 $actionParams['cid'] = $actionParams['entityID'] = $this->_contactId;
291 $actionParams['recId'] = $recId;
292 $actionParams['type'] = $this->_contactType;
293 $actionParams['cgcount'] = $cgcount;
294
295 // DELETE action links
296 $links[CRM_Core_Action::DELETE]['url'] = '#';
297 $links[CRM_Core_Action::DELETE]['extra'] = " onclick='showDeleteInDialog({$recId}, {$this->_customGroupId}, {$this->_contactId})' ";
298 $links[CRM_Core_Action::DELETE]['class'] = 'ignore-jshref';
299 }
300 if (!empty($pageCheckSum)) {
301 $actionParams['cs'] = $pageCheckSum;
302 }
303
304 $value['action'] = CRM_Core_Action::formLink(
305 $links,
306 $linkAction,
307 $actionParams,
308 ts('more'),
309 FALSE,
310 $op,
311 'customValue',
312 $fieldId // not ideal, but the one thing not sent in $actionParams
313 );
314 }
315 }
316 $cgcount++;
317 }
318 }
319 }
320
321 $headers = array( );
322 if (!empty($fieldIDs)) {
323 foreach ($fieldIDs as $fieldID) {
324 $headers[$fieldID] = ($this->_pageViewType == 'profileDataView') ? $customGroupInfo[$fieldID]['fieldLabel'] : $fieldLabels[$fieldID]['label'];
325 }
326 }
327 $this->assign('cgcount', $cgcount);
328 $this->assign('customGroupTitle', $this->_customGroupTitle);
329 $this->assign('headers', $headers);
330 $this->assign('records', $result);
331 }
332
333 /**
334 * Get name of edit form
335 *
336 * @return string classname of edit form
337 */
338 function editForm() {
339 return '';
340 }
341
342 /**
343 * Get edit form name
344 *
345 * @return string name of this page
346 */
347 function editName() {
348 return '';
349 }
350
351 /**
352 * Get user context
353 *
354 * @return string user context
355 */
356 function userContext($mode = NULL) {
357 return '';
358 }
359 }
360