Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | * $Id$ |
17 | * | |
18 | */ | |
19 | ||
20 | /** | |
21 | * This class generates form components for previewing custom data | |
22 | * | |
23 | * It delegates the work to lower level subclasses and integrates the changes | |
24 | * back in. It also uses a lot of functionality with the CRM API's, so any change | |
25 | * made here could potentially affect the API etc. Be careful, be aware, use unit tests. | |
26 | * | |
27 | */ | |
28 | class CRM_Custom_Form_Preview extends CRM_Core_Form { | |
29 | ||
30 | /** | |
fe482240 | 31 | * The group tree data. |
6a488035 TO |
32 | * |
33 | * @var array | |
34 | */ | |
35 | protected $_groupTree; | |
36 | ||
37 | /** | |
100fef9d | 38 | * Pre processing work done here. |
6a488035 TO |
39 | * |
40 | * gets session variables for group or field id | |
41 | * | |
6a488035 | 42 | * @return void |
8ef12e64 | 43 | */ |
00be9182 | 44 | public function preProcess() { |
6a488035 TO |
45 | // get the controller vars |
46 | $this->_groupId = $this->get('groupId'); | |
47 | $this->_fieldId = $this->get('fieldId'); | |
48 | if ($this->_fieldId) { | |
49 | // field preview | |
be2fb01f CW |
50 | $defaults = []; |
51 | $params = ['id' => $this->_fieldId]; | |
6a488035 TO |
52 | $fieldDAO = new CRM_Core_DAO_CustomField(); |
53 | CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_CustomField', $params, $defaults); | |
54 | ||
a7488080 | 55 | if (!empty($defaults['is_view'])) { |
6a488035 TO |
56 | CRM_Core_Error::statusBounce(ts('This field is view only so it will not display on edit form.')); |
57 | } | |
de6c59ca | 58 | elseif (empty($defaults['is_active'])) { |
6a488035 TO |
59 | CRM_Core_Error::statusBounce(ts('This field is inactive so it will not display on edit form.')); |
60 | } | |
61 | ||
be2fb01f | 62 | $groupTree = []; |
6a488035 | 63 | $groupTree[$this->_groupId]['id'] = 0; |
be2fb01f | 64 | $groupTree[$this->_groupId]['fields'] = []; |
6a488035 TO |
65 | $groupTree[$this->_groupId]['fields'][$this->_fieldId] = $defaults; |
66 | $this->_groupTree = CRM_Core_BAO_CustomGroup::formatGroupTree($groupTree, 1, $this); | |
67 | $this->assign('preview_type', 'field'); | |
68 | } | |
69 | else { | |
70 | $groupTree = CRM_Core_BAO_CustomGroup::getGroupDetail($this->_groupId); | |
71 | $this->_groupTree = CRM_Core_BAO_CustomGroup::formatGroupTree($groupTree, TRUE, $this); | |
72 | $this->assign('preview_type', 'group'); | |
73 | } | |
74 | } | |
75 | ||
76 | /** | |
fe482240 | 77 | * Set the default form values. |
6a488035 | 78 | * |
a6c01b45 CW |
79 | * @return array |
80 | * the default array reference | |
6a488035 | 81 | */ |
00be9182 | 82 | public function setDefaultValues() { |
be2fb01f | 83 | $defaults = []; |
6a488035 TO |
84 | |
85 | CRM_Core_BAO_CustomGroup::setDefaults($this->_groupTree, $defaults, FALSE, FALSE); | |
86 | ||
87 | return $defaults; | |
88 | } | |
89 | ||
90 | /** | |
fe482240 | 91 | * Build the form object. |
6a488035 | 92 | * |
6a488035 | 93 | * @return void |
6a488035 TO |
94 | */ |
95 | public function buildQuickForm() { | |
c8f8a3ad | 96 | if (is_array($this->_groupTree) && !empty($this->_groupTree[$this->_groupId])) { |
6a488035 TO |
97 | foreach ($this->_groupTree[$this->_groupId]['fields'] as & $field) { |
98 | //add the form elements | |
3a7773be | 99 | CRM_Core_BAO_CustomField::addQuickFormElement($this, $field['element_name'], $field['id'], CRM_Utils_Array::value('is_required', $field)); |
6a488035 TO |
100 | } |
101 | ||
102 | $this->assign('groupTree', $this->_groupTree); | |
103 | } | |
be2fb01f | 104 | $this->addButtons([ |
518fa0ee SL |
105 | [ |
106 | 'type' => 'cancel', | |
107 | 'name' => ts('Done with Preview'), | |
108 | 'isDefault' => TRUE, | |
109 | ], | |
110 | ]); | |
6a488035 | 111 | } |
96025800 | 112 | |
6a488035 | 113 | } |