Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
bc77d7c0 TO |
3 | +--------------------------------------------------------------------+ |
4 | | Copyright CiviCRM LLC. All rights reserved. | | |
5 | | | | |
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 | | |
9 | +--------------------------------------------------------------------+ | |
e70a7fc0 | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
18 | /** | |
19 | * this class builds custom data | |
20 | */ | |
21 | class CRM_Custom_Form_CustomData { | |
22 | ||
6452294d MW |
23 | /** |
24 | * Generic wrapper to add custom data to a form via a single line in preProcess. | |
25 | * | |
26 | * $this->getDefaultEntity() must be defined for the form class for this to work. | |
27 | * | |
28 | * If the postProcess form cannot use the api & instead uses a BAO function it will need. | |
29 | * $params['custom'] = CRM_Core_BAO_CustomField::postProcess($submitted, $this->_id, $this->getDefaultEntity()); | |
30 | * | |
31 | * @param CRM_Core_Form $form | |
ed706f8d | 32 | * @param null|string $entitySubType values stored in civicrm_custom_group.extends_entity_column_value |
6452294d MW |
33 | * e.g Student for contact type |
34 | * @param null|string $subName value in civicrm_custom_group.extends_entity_column_id | |
35 | * @param null|int $groupCount number of entities that could have custom data | |
2fcb4a7f | 36 | * @param null|int $contact_id contact ID associated with the custom data. |
6452294d MW |
37 | * |
38 | * @throws \CRM_Core_Exception | |
39 | */ | |
ed706f8d | 40 | public static function addToForm(&$form, $entitySubType = NULL, $subName = NULL, $groupCount = 1, $contact_id = NULL) { |
6452294d | 41 | $entityName = $form->getDefaultEntity(); |
e4f5c851 | 42 | $entityID = $form->getEntityId(); |
ed706f8d MWMC |
43 | // If the form has been converted to use entityFormTrait then getEntitySubTypeId() will exist. |
44 | if (method_exists($form, 'getEntitySubTypeId') && empty($entitySubType)) { | |
45 | $entitySubType = $form->getEntitySubTypeId(); | |
2c024dc6 | 46 | } |
6452294d | 47 | |
48c242dc MWMC |
48 | if ($form->getAction() == CRM_Core_Action::VIEW) { |
49 | // Viewing custom data (Use with {include file="CRM/Custom/Page/CustomDataView.tpl"} in template) | |
50 | $groupTree = CRM_Core_BAO_CustomGroup::getTree($entityName, NULL, $entityID, 0, $entitySubType); | |
51 | CRM_Core_BAO_CustomGroup::buildCustomDataView($form, $groupTree, FALSE, NULL, NULL, NULL, $entityID); | |
52 | } | |
53 | else { | |
54 | // Editing custom data (Use with {include file="CRM/common/customDataBlock.tpl"} in template) | |
55 | if (!empty($_POST['hidden_custom'])) { | |
56 | self::preProcess($form, $subName, $entitySubType, $groupCount, $entityName, $entityID); | |
57 | self::buildQuickForm($form); | |
58 | self::setDefaultValues($form); | |
59 | } | |
6452294d MW |
60 | } |
61 | // need to assign custom data type and subtype to the template | |
62 | $form->assign('customDataType', $entityName); | |
9f1073d2 | 63 | $form->assign('customDataSubType', $entitySubType); |
e4f5c851 | 64 | $form->assign('entityID', $entityID); |
2fcb4a7f | 65 | $form->assign('cid', $contact_id); |
6452294d MW |
66 | } |
67 | ||
6a488035 TO |
68 | /** |
69 | * @param CRM_Core_Form $form | |
70 | * @param null|string $subName | |
71 | * @param null|string $subType | |
72 | * @param null|int $groupCount | |
e8646905 | 73 | * @param string $type |
6a488035 | 74 | * @param null|int $entityID |
77b97be7 | 75 | * @param null $onlySubType |
6452294d MW |
76 | * |
77 | * @throws \CRM_Core_Exception | |
6a488035 | 78 | */ |
2da40d21 | 79 | public static function preProcess( |
40b55be5 | 80 | &$form, $subName = NULL, $subType = NULL, |
63dbed83 | 81 | $groupCount = NULL, $type = NULL, $entityID = NULL, $onlySubType = NULL |
6a488035 TO |
82 | ) { |
83 | if ($type) { | |
84 | $form->_type = $type; | |
85 | } | |
86 | else { | |
87 | $form->_type = CRM_Utils_Request::retrieve('type', 'String', $form); | |
88 | } | |
89 | ||
90 | if (isset($subType)) { | |
91 | $form->_subType = $subType; | |
92 | } | |
93 | else { | |
94 | $form->_subType = CRM_Utils_Request::retrieve('subType', 'String', $form); | |
95 | } | |
96 | ||
97 | if ($form->_subType == 'null') { | |
98 | $form->_subType = NULL; | |
99 | } | |
100 | ||
101 | if (isset($subName)) { | |
102 | $form->_subName = $subName; | |
103 | } | |
104 | else { | |
105 | $form->_subName = CRM_Utils_Request::retrieve('subName', 'String', $form); | |
106 | } | |
107 | ||
108 | if ($form->_subName == 'null') { | |
109 | $form->_subName = NULL; | |
110 | } | |
111 | ||
112 | if ($groupCount) { | |
113 | $form->_groupCount = $groupCount; | |
114 | } | |
115 | else { | |
116 | $form->_groupCount = CRM_Utils_Request::retrieve('cgcount', 'Positive', $form); | |
117 | } | |
118 | ||
119 | $form->assign('cgCount', $form->_groupCount); | |
120 | ||
121 | //carry qf key, since this form is not inhereting core form. | |
a3d827a7 | 122 | if ($qfKey = CRM_Utils_Request::retrieve('qfKey', 'String')) { |
6a488035 TO |
123 | $form->assign('qfKey', $qfKey); |
124 | } | |
125 | ||
126 | if ($entityID) { | |
127 | $form->_entityId = $entityID; | |
128 | } | |
129 | else { | |
130 | $form->_entityId = CRM_Utils_Request::retrieve('entityID', 'Positive', $form); | |
131 | } | |
132 | ||
a3d827a7 CW |
133 | $typeCheck = CRM_Utils_Request::retrieve('type', 'String'); |
134 | $urlGroupId = CRM_Utils_Request::retrieve('groupID', 'Positive'); | |
481a74f4 | 135 | if (isset($typeCheck) && $urlGroupId) { |
6a488035 | 136 | $form->_groupID = $urlGroupId; |
0db6c3e1 TO |
137 | } |
138 | else { | |
6a488035 TO |
139 | $form->_groupID = CRM_Utils_Request::retrieve('groupID', 'Positive', $form); |
140 | } | |
141 | ||
142 | $gid = (isset($form->_groupID)) ? $form->_groupID : NULL; | |
2e1f50d6 | 143 | $getCachedTree = $form->_getCachedTree ?? TRUE; |
6a488035 TO |
144 | |
145 | $subType = $form->_subType; | |
146 | if (!is_array($subType) && strstr($subType, CRM_Core_DAO::VALUE_SEPARATOR)) { | |
147 | $subType = str_replace(CRM_Core_DAO::VALUE_SEPARATOR, ',', trim($subType, CRM_Core_DAO::VALUE_SEPARATOR)); | |
148 | } | |
8ef12e64 | 149 | |
5b15263a EM |
150 | self::setGroupTree($form, $subType, $gid, $onlySubType, $getCachedTree); |
151 | } | |
152 | ||
153 | /** | |
154 | * @param CRM_Core_Form $form | |
155 | * | |
156 | * @return array | |
157 | */ | |
158 | public static function setDefaultValues(&$form) { | |
be2fb01f | 159 | $defaults = []; |
5b15263a EM |
160 | CRM_Core_BAO_CustomGroup::setDefaults($form->_groupTree, $defaults, FALSE, FALSE, $form->get('action')); |
161 | return $defaults; | |
162 | } | |
163 | ||
164 | /** | |
165 | * @param CRM_Core_Form $form | |
166 | */ | |
167 | public static function buildQuickForm(&$form) { | |
168 | $form->addElement('hidden', 'hidden_custom', 1); | |
169 | $form->addElement('hidden', "hidden_custom_group_count[{$form->_groupID}]", $form->_groupCount); | |
170 | CRM_Core_BAO_CustomGroup::buildQuickForm($form, $form->_groupTree); | |
171 | } | |
172 | ||
173 | /** | |
1203c8df | 174 | * Add the group data as a formatted array to the form. |
175 | * | |
176 | * @param CRM_Core_Form $form | |
177 | * @param string $subType | |
041ecc95 | 178 | * @param int $gid |
1203c8df | 179 | * @param bool $onlySubType |
180 | * @param bool $getCachedTree | |
5b15263a EM |
181 | * |
182 | * @return array | |
1203c8df | 183 | * @throws \CRM_Core_Exception |
5b15263a | 184 | */ |
1203c8df | 185 | public static function setGroupTree(&$form, $subType, $gid, $onlySubType = NULL, $getCachedTree = TRUE) { |
e1d48b72 | 186 | $singleRecord = NULL; |
1b56bd01 | 187 | if (!empty($form->_groupCount) && !empty($form->_multiRecordDisplay) && $form->_multiRecordDisplay == 'single') { |
e1d48b72 | 188 | $singleRecord = $form->_groupCount; |
189 | } | |
190 | $mode = CRM_Utils_Request::retrieve('mode', 'String', $form); | |
1b56bd01 | 191 | // when a new record is being added for multivalued custom fields. |
192 | if (isset($form->_groupCount) && $form->_groupCount == 0 && $mode == 'add' && | |
193 | !empty($form->_multiRecordDisplay) && $form->_multiRecordDisplay == 'single') { | |
e1d48b72 | 194 | $singleRecord = 'new'; |
195 | } | |
5b15263a EM |
196 | |
197 | $groupTree = CRM_Core_BAO_CustomGroup::getTree($form->_type, | |
0b330e6d | 198 | NULL, |
6a488035 TO |
199 | $form->_entityId, |
200 | $gid, | |
201 | $subType, | |
202 | $form->_subName, | |
63dbed83 | 203 | $getCachedTree, |
e1d48b72 | 204 | $onlySubType, |
205 | FALSE, | |
206 | TRUE, | |
207 | $singleRecord | |
6a488035 TO |
208 | ); |
209 | ||
210 | if (property_exists($form, '_customValueCount') && !empty($groupTree)) { | |
ec3de1a9 | 211 | $form->_customValueCount = CRM_Core_BAO_CustomGroup::buildCustomDataView($form, $groupTree, TRUE, NULL, NULL, NULL, $form->_entityId); |
6a488035 TO |
212 | } |
213 | // we should use simplified formatted groupTree | |
214 | $groupTree = CRM_Core_BAO_CustomGroup::formatGroupTree($groupTree, $form->_groupCount, $form); | |
215 | ||
216 | if (isset($form->_groupTree) && is_array($form->_groupTree)) { | |
217 | $keys = array_keys($groupTree); | |
218 | foreach ($keys as $key) { | |
219 | $form->_groupTree[$key] = $groupTree[$key]; | |
220 | } | |
be2fb01f | 221 | return [$form, $groupTree]; |
6a488035 TO |
222 | } |
223 | else { | |
224 | $form->_groupTree = $groupTree; | |
be2fb01f | 225 | return [$form, $groupTree]; |
6a488035 TO |
226 | } |
227 | } | |
228 | ||
6a488035 | 229 | } |