Commit | Line | Data |
---|---|---|
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 | */ | |
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; | |
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©ValueId=%%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 | ); | |
dc98079b | 183 | $multiRecordFields = array(); |
6a488035 TO |
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 | 221 | if ($fieldIDs && !empty($fieldIDs) && $this->_contactId) { |
dc98079b | 222 | $options = array(); |
4411002f | 223 | $returnProperities = array('html_type', 'data_type', 'date_format', 'time_format', 'default_value', 'is_required'); |
6a488035 | 224 | foreach ($fieldIDs as $key => $fieldID) { |
e41f4660 | 225 | $fieldIDs[$key] = !is_numeric($fieldID) ? CRM_Core_BAO_CustomField::getKeyID($fieldID) : $fieldID; |
6a488035 | 226 | $param = array('id' => $fieldIDs[$key]); |
dc98079b | 227 | $returnValues = array(); |
6a488035 | 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']; | |
4411002f CW |
242 | $options[$fieldIDs[$key]]['attributes']['is_required'] = !empty($returnValues['is_required']); |
243 | $options[$fieldIDs[$key]]['attributes']['default_value'] = CRM_Utils_Array::value('default_value', $returnValues); | |
366fe2a3 | 244 | |
245 | $options[$fieldIDs[$key]]['attributes']['format'] = | |
6a488035 TO |
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 | } | |
366fe2a3 | 249 | |
e41f4660 | 250 | // commonly used for both views i.e profile listing view (profileDataView) and custom data listing view (customDataView) |
6a488035 | 251 | $result = CRM_Core_BAO_CustomValueTable::getEntityValues($this->_contactId, NULL, $fieldIDs, TRUE); |
366fe2a3 | 252 | |
e41f4660 PJ |
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)) { | |
eb2b4bdb | 274 | $links = self::links(); |
e41f4660 PJ |
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())); | |
1dde099b PJ |
284 | |
285 | if ($reached) { | |
286 | unset($links[CRM_Core_Action::COPY]); | |
287 | } | |
288 | $newCgCount = (!$reached) ? count($result) + 1 : NULL; | |
e41f4660 PJ |
289 | foreach ($result as $recId => &$value) { |
290 | foreach ($value as $fieldId => &$val) { | |
291 | if (is_numeric($fieldId)) { | |
292 | $customValue = &$val; | |
5f1aa3a5 | 293 | if (!empty($dateFields) && array_key_exists($fieldId, $dateFields)) { |
b05a0fb6 PJ |
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 | } | |
4411002f CW |
301 | // FIXME: getDisplayValue should always return a string so why is this necessary? |
302 | if (!$customValue && $customValue !== '0') { | |
e41f4660 PJ |
303 | $customValue = ""; |
304 | } | |
305 | ||
4411002f CW |
306 | // Set field attributes to support crmEditable |
307 | // Note that $fieldAttributes[data-type] actually refers to the html type not the sql data type | |
308 | // TODO: Not all widget types and validation rules are supported by crmEditable so some fields will not be in-place editable | |
309 | $fieldAttributes = array('class' => "crmf-custom_{$fieldId}_$recId"); | |
310 | $editable = FALSE; | |
311 | if ($linkAction & CRM_Core_Action::UPDATE) { | |
312 | $spec = $options[$fieldId]['attributes']; | |
313 | switch ($spec['html_type']) { | |
314 | case 'Text': | |
315 | // Other data types like money would require some extra validation | |
316 | // FIXME: crmEditable currently does not support any validation rules :( | |
317 | $supportedDataTypes = array('Float', 'String', 'Int'); | |
318 | $editable = in_array($spec['data_type'], $supportedDataTypes); | |
319 | break; | |
dc98079b | 320 | |
4411002f CW |
321 | case 'TextArea': |
322 | $editable = TRUE; | |
323 | $fieldAttributes['data-type'] = 'textarea'; | |
324 | break; | |
dc98079b | 325 | |
4411002f CW |
326 | case 'Radio': |
327 | case 'Select': | |
328 | case 'Select Country': | |
329 | case 'Select State/Province': | |
330 | $editable = TRUE; | |
331 | $fieldAttributes['data-type'] = $spec['data_type'] == 'Boolean' ? 'boolean' : 'select'; | |
332 | if (!$spec['is_required']) { | |
333 | $fieldAttributes['data-empty-option'] = ts('- none -'); | |
334 | } | |
335 | break; | |
336 | } | |
337 | } | |
338 | if ($editable) { | |
339 | $fieldAttributes['class'] .= ' crm-editable'; | |
340 | } | |
341 | $attributes[$fieldId][$recId] = $fieldAttributes; | |
342 | ||
e41f4660 PJ |
343 | $op = NULL; |
344 | if ($this->_pageViewType == 'profileDataView') { | |
dc98079b TO |
345 | $actionParams = array( |
346 | 'recordId' => $recId, | |
347 | 'gid' => $this->_profileId, | |
7ab9bfe8 | 348 | 'id' => $this->_contactId); |
e41f4660 PJ |
349 | $op = 'profile.multiValue.row'; |
350 | } | |
351 | else { | |
352 | // different set of url params | |
353 | $actionParams['gid'] = $actionParams['groupID'] = $this->_customGroupId; | |
354 | $actionParams['cid'] = $actionParams['entityID'] = $this->_contactId; | |
355 | $actionParams['recId'] = $recId; | |
356 | $actionParams['type'] = $this->_contactType; | |
357 | $actionParams['cgcount'] = $cgcount; | |
1dde099b | 358 | $actionParams['newCgCount'] = $newCgCount; |
eb2b4bdb PJ |
359 | |
360 | // DELETE action links | |
a801aa5c CW |
361 | $deleteData = array( |
362 | 'valueID' => $recId, | |
363 | 'groupID' => $this->_customGroupId, | |
364 | 'contactId' => $this->_contactId, | |
365 | 'key' => CRM_Core_Key::get('civicrm/ajax/customvalue'), | |
366 | ); | |
eb2b4bdb | 367 | $links[CRM_Core_Action::DELETE]['url'] = '#'; |
a801aa5c CW |
368 | $links[CRM_Core_Action::DELETE]['extra'] = ' data-delete_params="'. htmlspecialchars(json_encode($deleteData)) .'"'; |
369 | $links[CRM_Core_Action::DELETE]['class'] = 'delete-custom-row'; | |
e41f4660 PJ |
370 | } |
371 | if (!empty($pageCheckSum)) { | |
372 | $actionParams['cs'] = $pageCheckSum; | |
373 | } | |
eb2b4bdb | 374 | |
e41f4660 PJ |
375 | $value['action'] = CRM_Core_Action::formLink( |
376 | $links, | |
377 | $linkAction, | |
378 | $actionParams, | |
379 | ts('more'), | |
380 | FALSE, | |
381 | $op, | |
382 | 'customValue', | |
383 | $fieldId // not ideal, but the one thing not sent in $actionParams | |
384 | ); | |
385 | } | |
386 | } | |
387 | $cgcount++; | |
388 | } | |
389 | } | |
6a488035 | 390 | } |
366fe2a3 | 391 | |
dc98079b | 392 | $headers = array(); |
6a488035 TO |
393 | if (!empty($fieldIDs)) { |
394 | foreach ($fieldIDs as $fieldID) { | |
e41f4660 | 395 | $headers[$fieldID] = ($this->_pageViewType == 'profileDataView') ? $customGroupInfo[$fieldID]['fieldLabel'] : $fieldLabels[$fieldID]['label']; |
6a488035 TO |
396 | } |
397 | } | |
b05a0fb6 | 398 | $this->assign('dateFields', $dateFields); |
dc98079b | 399 | $this->assign('dateFieldsVals', $dateFieldsVals); |
e41f4660 | 400 | $this->assign('cgcount', $cgcount); |
6a488035 TO |
401 | $this->assign('customGroupTitle', $this->_customGroupTitle); |
402 | $this->assign('headers', $headers); | |
403 | $this->assign('records', $result); | |
4411002f | 404 | $this->assign('attributes', $attributes); |
6a488035 TO |
405 | } |
406 | ||
407 | /** | |
408 | * Get name of edit form | |
409 | * | |
410 | * @return string classname of edit form | |
411 | */ | |
00be9182 | 412 | public function editForm() { |
6a488035 TO |
413 | return ''; |
414 | } | |
415 | ||
416 | /** | |
417 | * Get edit form name | |
418 | * | |
419 | * @return string name of this page | |
420 | */ | |
00be9182 | 421 | public function editName() { |
6a488035 TO |
422 | return ''; |
423 | } | |
424 | ||
425 | /** | |
426 | * Get user context | |
427 | * | |
77b97be7 EM |
428 | * @param null $mode |
429 | * | |
6a488035 TO |
430 | * @return string user context |
431 | */ | |
00be9182 | 432 | public function userContext($mode = NULL) { |
6a488035 TO |
433 | return ''; |
434 | } | |
435 | } |