Merge pull request #24115 from kcristiano/5.52-token
[civicrm-core.git] / CRM / Custom / Form / CustomData.php
1 <?php
2 /*
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * this class builds custom data
20 */
21 class CRM_Custom_Form_CustomData {
22
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
32 * @param null|string $entitySubType values stored in civicrm_custom_group.extends_entity_column_value
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
36 * @param null|int $contact_id contact ID associated with the custom data.
37 *
38 * @throws \CRM_Core_Exception
39 */
40 public static function addToForm(&$form, $entitySubType = NULL, $subName = NULL, $groupCount = 1, $contact_id = NULL) {
41 $entityName = $form->getDefaultEntity();
42 $entityID = $form->getEntityId();
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();
46 }
47
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 }
60 }
61 // need to assign custom data type and subtype to the template
62 $form->assign('customDataType', $entityName);
63 $form->assign('customDataSubType', $entitySubType);
64 $form->assign('entityID', $entityID);
65 $form->assign('cid', $contact_id);
66 }
67
68 /**
69 * @param CRM_Core_Form $form
70 * @param null|string $subName
71 * @param null|string $subType
72 * @param null|int $groupCount
73 * @param string $type
74 * @param null|int $entityID
75 * @param null $onlySubType
76 *
77 * @throws \CRM_Core_Exception
78 */
79 public static function preProcess(
80 &$form, $subName = NULL, $subType = NULL,
81 $groupCount = NULL, $type = NULL, $entityID = NULL, $onlySubType = NULL
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.
122 if ($qfKey = CRM_Utils_Request::retrieve('qfKey', 'String')) {
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
133 $typeCheck = CRM_Utils_Request::retrieve('type', 'String');
134 $urlGroupId = CRM_Utils_Request::retrieve('groupID', 'Positive');
135 if (isset($typeCheck) && $urlGroupId) {
136 $form->_groupID = $urlGroupId;
137 }
138 else {
139 $form->_groupID = CRM_Utils_Request::retrieve('groupID', 'Positive', $form);
140 }
141
142 $gid = (isset($form->_groupID)) ? $form->_groupID : NULL;
143 $getCachedTree = $form->_getCachedTree ?? TRUE;
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 }
149
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) {
159 $defaults = [];
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 /**
174 * Add the group data as a formatted array to the form.
175 *
176 * @param CRM_Core_Form $form
177 * @param string $subType
178 * @param int $gid
179 * @param bool $onlySubType
180 * @param bool $getCachedTree
181 *
182 * @return array
183 * @throws \CRM_Core_Exception
184 */
185 public static function setGroupTree(&$form, $subType, $gid, $onlySubType = NULL, $getCachedTree = TRUE) {
186 $singleRecord = NULL;
187 if (!empty($form->_groupCount) && !empty($form->_multiRecordDisplay) && $form->_multiRecordDisplay == 'single') {
188 $singleRecord = $form->_groupCount;
189 }
190 $mode = CRM_Utils_Request::retrieve('mode', 'String', $form);
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') {
194 $singleRecord = 'new';
195 }
196
197 $groupTree = CRM_Core_BAO_CustomGroup::getTree($form->_type,
198 NULL,
199 $form->_entityId,
200 $gid,
201 $subType,
202 $form->_subName,
203 $getCachedTree,
204 $onlySubType,
205 FALSE,
206 CRM_Core_Permission::EDIT,
207 $singleRecord
208 );
209
210 if (property_exists($form, '_customValueCount') && !empty($groupTree)) {
211 $form->_customValueCount = CRM_Core_BAO_CustomGroup::buildCustomDataView($form, $groupTree, TRUE, NULL, NULL, NULL, $form->_entityId);
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 }
221 return [$form, $groupTree];
222 }
223 else {
224 $form->_groupTree = $groupTree;
225 return [$form, $groupTree];
226 }
227 }
228
229 }