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 | */ |
17 | ||
18 | /** | |
3a8f58ec | 19 | * Business object for managing custom data groups. |
6a488035 TO |
20 | */ |
21 | class CRM_Core_BAO_CustomGroup extends CRM_Core_DAO_CustomGroup { | |
22 | ||
23 | /** | |
fe482240 | 24 | * Class constructor. |
6a488035 | 25 | */ |
00be9182 | 26 | public function __construct() { |
6a488035 TO |
27 | parent::__construct(); |
28 | } | |
29 | ||
30 | /** | |
fe482240 | 31 | * Takes an associative array and creates a custom group object. |
6a488035 TO |
32 | * |
33 | * This function is invoked from within the web form layer and also from the api layer | |
34 | * | |
6a0b768e TO |
35 | * @param array $params |
36 | * (reference) an assoc array of name/value pairs. | |
6a488035 | 37 | * |
16b10e64 | 38 | * @return CRM_Core_DAO_CustomGroup |
aa965915 | 39 | * @throws \Exception |
6a488035 | 40 | */ |
00be9182 | 41 | public static function create(&$params) { |
6a488035 TO |
42 | // create custom group dao, populate fields and then save. |
43 | $group = new CRM_Core_DAO_CustomGroup(); | |
117cc973 EM |
44 | if (isset($params['title'])) { |
45 | $group->title = $params['title']; | |
46 | } | |
6a488035 | 47 | |
d128501c | 48 | $extends = CRM_Utils_Array::value('extends', $params, []); |
9c1bc317 | 49 | $extendsEntity = $extends[0] ?? NULL; |
d128501c | 50 | |
be2fb01f | 51 | $participantEntities = [ |
d128501c MD |
52 | 'ParticipantRole', |
53 | 'ParticipantEventName', | |
54 | 'ParticipantEventType', | |
be2fb01f | 55 | ]; |
d128501c MD |
56 | |
57 | if (in_array($extendsEntity, $participantEntities)) { | |
6a488035 TO |
58 | $group->extends = 'Participant'; |
59 | } | |
60 | else { | |
d128501c | 61 | $group->extends = $extendsEntity; |
6a488035 TO |
62 | } |
63 | ||
64 | $group->extends_entity_column_id = 'null'; | |
d128501c | 65 | if (in_array($extendsEntity, $participantEntities) |
6a488035 | 66 | ) { |
d128501c | 67 | $group->extends_entity_column_id = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $extendsEntity, 'value', 'name'); |
6a488035 TO |
68 | } |
69 | ||
aa965915 | 70 | // this is format when form get submit. |
9c1bc317 | 71 | $extendsChildType = $extends[1] ?? NULL; |
aa965915 | 72 | // lets allow user to pass direct child type value, CRM-6893 |
a7488080 | 73 | if (!empty($params['extends_entity_column_value'])) { |
6a488035 TO |
74 | $extendsChildType = $params['extends_entity_column_value']; |
75 | } | |
76 | if (!CRM_Utils_System::isNull($extendsChildType)) { | |
77 | $extendsChildType = implode(CRM_Core_DAO::VALUE_SEPARATOR, $extendsChildType); | |
d128501c | 78 | if (CRM_Utils_Array::value(0, $extends) == 'Relationship') { |
be2fb01f | 79 | $extendsChildType = str_replace(['_a_b', '_b_a'], [ |
353ffa53 TO |
80 | '', |
81 | '', | |
be2fb01f | 82 | ], $extendsChildType); |
6a488035 TO |
83 | } |
84 | if (substr($extendsChildType, 0, 1) != CRM_Core_DAO::VALUE_SEPARATOR) { | |
9d72cede EM |
85 | $extendsChildType = CRM_Core_DAO::VALUE_SEPARATOR . $extendsChildType . |
86 | CRM_Core_DAO::VALUE_SEPARATOR; | |
6a488035 TO |
87 | } |
88 | } | |
89 | else { | |
90 | $extendsChildType = 'null'; | |
91 | } | |
92 | $group->extends_entity_column_value = $extendsChildType; | |
93 | ||
94 | if (isset($params['id'])) { | |
95 | $oldWeight = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $params['id'], 'weight', 'id'); | |
96 | } | |
97 | else { | |
98 | $oldWeight = 0; | |
99 | } | |
100 | $group->weight = CRM_Utils_Weight::updateOtherWeights('CRM_Core_DAO_CustomGroup', $oldWeight, CRM_Utils_Array::value('weight', $params, FALSE)); | |
be2fb01f | 101 | $fields = [ |
9d72cede EM |
102 | 'style', |
103 | 'collapse_display', | |
104 | 'collapse_adv_display', | |
105 | 'help_pre', | |
106 | 'help_post', | |
107 | 'is_active', | |
21dfd5f5 | 108 | 'is_multiple', |
2af06525 | 109 | 'icon', |
be2fb01f | 110 | ]; |
da0577d8 CW |
111 | $current_db_version = CRM_Core_BAO_Domain::version(); |
112 | $is_public_version = version_compare($current_db_version, '4.7.19', '>='); | |
f7ed3a24 SL |
113 | if ($is_public_version) { |
114 | $fields[] = 'is_public'; | |
115 | } | |
6a488035 | 116 | foreach ($fields as $field) { |
e2cb881c MD |
117 | if (isset($params[$field])) { |
118 | $group->$field = $params[$field]; | |
117cc973 | 119 | } |
6a488035 TO |
120 | } |
121 | $group->max_multiple = isset($params['is_multiple']) ? (isset($params['max_multiple']) && | |
122 | $params['max_multiple'] >= '0' | |
123 | ) ? $params['max_multiple'] : 'null' : 'null'; | |
124 | ||
c746070e | 125 | $tableName = $tableNameNeedingIndexUpdate = NULL; |
6a488035 TO |
126 | if (isset($params['id'])) { |
127 | $group->id = $params['id']; | |
6a488035 | 128 | |
c746070e | 129 | if (isset($params['is_multiple'])) { |
aa965915 | 130 | // check whether custom group was changed from single-valued to multiple-valued |
c746070e | 131 | $isMultiple = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', |
6a488035 | 132 | $params['id'], |
c746070e | 133 | 'is_multiple' |
6a488035 | 134 | ); |
c746070e | 135 | |
1f85becb SL |
136 | // dev/core#227 Fix issue where is_multiple in params maybe an empty string if checkbox is not rendered on the form. |
137 | $paramsIsMultiple = empty($params['is_multiple']) ? 0 : 1; | |
138 | if ($paramsIsMultiple != $isMultiple) { | |
c746070e | 139 | $tableNameNeedingIndexUpdate = CRM_Core_DAO::getFieldValue( |
140 | 'CRM_Core_DAO_CustomGroup', | |
141 | $params['id'], | |
142 | 'table_name' | |
143 | ); | |
144 | } | |
6a488035 TO |
145 | } |
146 | } | |
147 | else { | |
9c1bc317 CW |
148 | $group->created_id = $params['created_id'] ?? NULL; |
149 | $group->created_date = $params['created_date'] ?? NULL; | |
6a488035 TO |
150 | |
151 | // we do this only once, so name never changes | |
152 | if (isset($params['name'])) { | |
153 | $group->name = CRM_Utils_String::munge($params['name'], '_', 64); | |
154 | } | |
155 | else { | |
156 | $group->name = CRM_Utils_String::munge($group->title, '_', 64); | |
157 | } | |
158 | ||
8b3b9a2e ARW |
159 | if (isset($params['table_name'])) { |
160 | $tableName = $params['table_name']; | |
161 | ||
162 | if (CRM_Core_DAO_AllCoreTables::isCoreTable($tableName)) { | |
163 | // Bad idea. Prevent group creation because it might lead to a broken configuration. | |
36769590 | 164 | throw new CRM_Core_Exception(ts('Cannot create custom table because %1 is already a core table.', ['1' => $tableName])); |
8b3b9a2e ARW |
165 | } |
166 | } | |
6a488035 TO |
167 | } |
168 | ||
4e8506a4 | 169 | if (array_key_exists('is_reserved', $params)) { |
1155dd58 | 170 | $group->is_reserved = $params['is_reserved'] ? 1 : 0; |
f329e1de | 171 | } |
202f05c9 FG |
172 | $op = isset($params['id']) ? 'edit' : 'create'; |
173 | CRM_Utils_Hook::pre($op, 'CustomGroup', CRM_Utils_Array::value('id', $params), $params); | |
174 | ||
6a488035 TO |
175 | // enclose the below in a transaction |
176 | $transaction = new CRM_Core_Transaction(); | |
177 | ||
178 | $group->save(); | |
8b3b9a2e ARW |
179 | if (!isset($params['id'])) { |
180 | if (!isset($params['table_name'])) { | |
2caf0feb | 181 | $munged_title = strtolower(CRM_Utils_String::munge($group->title, '_', 13)); |
8b3b9a2e ARW |
182 | $tableName = "civicrm_value_{$munged_title}_{$group->id}"; |
183 | } | |
6a488035 TO |
184 | $group->table_name = $tableName; |
185 | CRM_Core_DAO::setFieldValue('CRM_Core_DAO_CustomGroup', | |
186 | $group->id, | |
187 | 'table_name', | |
188 | $tableName | |
189 | ); | |
190 | ||
191 | // now create the table associated with this group | |
192 | self::createTable($group); | |
193 | } | |
c746070e | 194 | elseif ($tableNameNeedingIndexUpdate) { |
195 | CRM_Core_BAO_SchemaHandler::changeUniqueToIndex($tableNameNeedingIndexUpdate, CRM_Utils_Array::value('is_multiple', $params)); | |
6a488035 TO |
196 | } |
197 | ||
198 | if (CRM_Utils_Array::value('overrideFKConstraint', $params) == 1) { | |
199 | $table = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', | |
200 | $params['id'], | |
201 | 'table_name' | |
202 | ); | |
d128501c | 203 | CRM_Core_BAO_SchemaHandler::changeFKConstraint($table, self::mapTableName($extendsEntity)); |
6a488035 TO |
204 | } |
205 | $transaction->commit(); | |
206 | ||
207 | // reset the cache | |
208 | CRM_Utils_System::flushCache(); | |
209 | ||
210 | if ($tableName) { | |
211 | CRM_Utils_Hook::post('create', 'CustomGroup', $group->id, $group); | |
212 | } | |
213 | else { | |
214 | CRM_Utils_Hook::post('edit', 'CustomGroup', $group->id, $group); | |
215 | } | |
216 | ||
217 | return $group; | |
218 | } | |
219 | ||
220 | /** | |
fe482240 | 221 | * Fetch object based on array of properties. |
6a488035 | 222 | * |
6a0b768e TO |
223 | * @param array $params |
224 | * (reference ) an assoc array of name/value pairs. | |
225 | * @param array $defaults | |
226 | * (reference ) an assoc array to hold the flattened values. | |
6a488035 | 227 | * |
16b10e64 | 228 | * @return CRM_Core_DAO_CustomGroup |
6a488035 | 229 | */ |
00be9182 | 230 | public static function retrieve(&$params, &$defaults) { |
6a488035 TO |
231 | return CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_CustomGroup', $params, $defaults); |
232 | } | |
233 | ||
234 | /** | |
fe482240 | 235 | * Update the is_active flag in the db. |
6a488035 | 236 | * |
6a0b768e TO |
237 | * @param int $id |
238 | * Id of the database record. | |
a1a2a83d | 239 | * @param bool $is_active |
6a0b768e | 240 | * Value we want to set the is_active field. |
6a488035 | 241 | * |
8a4fede3 | 242 | * @return bool |
243 | * true if we found and updated the object, else false | |
6a488035 | 244 | */ |
00be9182 | 245 | public static function setIsActive($id, $is_active) { |
6a488035 | 246 | // reset the cache |
9cdf85c1 | 247 | Civi::cache('fields')->flush(); |
5e601882 SL |
248 | // reset ACL and system caches. |
249 | CRM_Core_BAO_Cache::resetCaches(); | |
6a488035 TO |
250 | |
251 | if (!$is_active) { | |
252 | CRM_Core_BAO_UFField::setUFFieldStatus($id, $is_active); | |
253 | } | |
254 | ||
255 | return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_CustomGroup', $id, 'is_active', $is_active); | |
256 | } | |
257 | ||
258 | /** | |
259 | * Determine if given entity (sub)type has any custom groups | |
260 | * | |
6a0b768e TO |
261 | * @param string $extends |
262 | * E.g. "Individual", "Activity". | |
263 | * @param int $columnId | |
264 | * E.g. custom-group matching mechanism (usu NULL for matching on sub type-id); see extends_entity_column_id. | |
265 | * @param string $columnValue | |
266 | * E.g. "Student" or "3" or "3\05"; see extends_entity_column_value. | |
77b97be7 EM |
267 | * |
268 | * @return bool | |
6a488035 TO |
269 | */ |
270 | public static function hasCustomGroup($extends, $columnId, $columnValue) { | |
271 | $dao = new CRM_Core_DAO_CustomGroup(); | |
9d72cede | 272 | $dao->extends = $extends; |
6a488035 | 273 | $dao->extends_entity_column_id = $columnId; |
6c552737 | 274 | $escapedValue = CRM_Core_DAO::VALUE_SEPARATOR . CRM_Core_DAO::escapeString($columnValue) . CRM_Core_DAO::VALUE_SEPARATOR; |
6a488035 TO |
275 | $dao->whereAdd("extends_entity_column_value LIKE \"%$escapedValue%\""); |
276 | //$dao->extends_entity_column_value = $columnValue; | |
63d76404 | 277 | return (bool) $dao->find(); |
6a488035 TO |
278 | } |
279 | ||
280 | /** | |
281 | * Determine if there are any CustomGroups for the given $activityTypeId. | |
282 | * If none found, create one. | |
283 | * | |
284 | * @param int $activityTypeId | |
9d72cede | 285 | * |
a6c01b45 CW |
286 | * @return bool |
287 | * TRUE if a group is found or created; FALSE on error | |
6a488035 TO |
288 | */ |
289 | public static function autoCreateByActivityType($activityTypeId) { | |
290 | if (self::hasCustomGroup('Activity', NULL, $activityTypeId)) { | |
291 | return TRUE; | |
292 | } | |
cf9ccf98 TO |
293 | // everything |
294 | $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'label', TRUE, FALSE); | |
be2fb01f | 295 | $params = [ |
6a488035 TO |
296 | 'version' => 3, |
297 | 'extends' => 'Activity', | |
298 | 'extends_entity_column_id' => NULL, | |
be2fb01f CW |
299 | 'extends_entity_column_value' => CRM_Utils_Array::implodePadded([$activityTypeId]), |
300 | 'title' => ts('%1 Questions', [1 => $activityTypes[$activityTypeId]]), | |
6a488035 TO |
301 | 'style' => 'Inline', |
302 | 'is_active' => 1, | |
be2fb01f | 303 | ]; |
6a488035 | 304 | $result = civicrm_api('CustomGroup', 'create', $params); |
9d72cede | 305 | return !$result['is_error']; |
6a488035 TO |
306 | } |
307 | ||
308 | /** | |
c43cedc8 | 309 | * Get custom groups/fields data for type of entity in a tree structure representing group->field hierarchy |
310 | * This may also include entity specific data values. | |
6a488035 TO |
311 | * |
312 | * An array containing all custom groups and their custom fields is returned. | |
313 | * | |
6a0b768e TO |
314 | * @param string $entityType |
315 | * Of the contact whose contact type is needed. | |
0b330e6d CW |
316 | * @param array $toReturn |
317 | * What data should be returned. ['custom_group' => ['id', 'name', etc.], 'custom_field' => ['id', 'label', etc.]] | |
c490a46a CW |
318 | * @param int $entityID |
319 | * @param int $groupID | |
79363422 | 320 | * @param array $subTypes |
c43cedc8 | 321 | * @param string $subName |
c490a46a CW |
322 | * @param bool $fromCache |
323 | * @param bool $onlySubType | |
b62bc939 EM |
324 | * Only return specified subtype or return specified subtype + unrestricted fields. |
325 | * @param bool $returnAll | |
326 | * Do not restrict by subtype at all. (The parameter feels a bit cludgey but is only used from the | |
327 | * api - through which it is properly tested - so can be refactored with some comfort.) | |
6a488035 | 328 | * |
e9ff5391 | 329 | * @param bool $checkPermission |
0b330e6d | 330 | * @param string|int $singleRecord |
e1d48b72 | 331 | * holds 'new' or id if view/edit/copy form for a single record is being loaded. |
460931ca | 332 | * @param bool $showPublicOnly |
e9ff5391 | 333 | * |
a6c01b45 | 334 | * @return array |
e9ff5391 | 335 | * Custom field 'tree'. |
336 | * | |
16b10e64 CW |
337 | * The returned array is keyed by group id and has the custom group table fields |
338 | * and a subkey 'fields' holding the specific custom fields. | |
339 | * If entityId is passed in the fields keys have a subkey 'customValue' which holds custom data | |
340 | * if set for the given entity. This is structured as an array of values with each one having the keys 'id', 'data' | |
c43cedc8 | 341 | * |
342 | * @todo - review this - It also returns an array called 'info' with tables, select, from, where keys | |
16b10e64 CW |
343 | * The reason for the info array in unclear and it could be determined from parsing the group tree after creation |
344 | * With caching the performance impact would be small & the function would be cleaner | |
aa965915 MWMC |
345 | * |
346 | * @throws \CRM_Core_Exception | |
6a488035 | 347 | */ |
27dd6252 | 348 | public static function getTree( |
0e6e8724 | 349 | $entityType, |
be2fb01f | 350 | $toReturn = [], |
6a488035 | 351 | $entityID = NULL, |
9d72cede | 352 | $groupID = NULL, |
be2fb01f | 353 | $subTypes = [], |
9d72cede | 354 | $subName = NULL, |
63dbed83 | 355 | $fromCache = TRUE, |
b62bc939 | 356 | $onlySubType = NULL, |
e9ff5391 | 357 | $returnAll = FALSE, |
e1d48b72 | 358 | $checkPermission = TRUE, |
460931ca JP |
359 | $singleRecord = NULL, |
360 | $showPublicOnly = FALSE | |
6a488035 TO |
361 | ) { |
362 | if ($entityID) { | |
363 | $entityID = CRM_Utils_Type::escape($entityID, 'Integer'); | |
364 | } | |
79363422 E |
365 | if (!is_array($subTypes)) { |
366 | if (empty($subTypes)) { | |
be2fb01f | 367 | $subTypes = []; |
79363422 E |
368 | } |
369 | else { | |
45e5c872 | 370 | if (stristr($subTypes, ',')) { |
35e83f39 | 371 | $subTypes = explode(',', $subTypes); |
372 | } | |
373 | else { | |
374 | $subTypes = explode(CRM_Core_DAO::VALUE_SEPARATOR, trim($subTypes, CRM_Core_DAO::VALUE_SEPARATOR)); | |
375 | } | |
79363422 E |
376 | } |
377 | } | |
6a488035 TO |
378 | |
379 | // create a new tree | |
6a488035 | 380 | |
0b330e6d | 381 | // legacy hardcoded list of data to return |
be2fb01f CW |
382 | $tableData = [ |
383 | 'custom_field' => [ | |
6a488035 | 384 | 'id', |
24871985 | 385 | 'name', |
6a488035 TO |
386 | 'label', |
387 | 'column_name', | |
388 | 'data_type', | |
389 | 'html_type', | |
390 | 'default_value', | |
391 | 'attributes', | |
392 | 'is_required', | |
393 | 'is_view', | |
394 | 'help_pre', | |
395 | 'help_post', | |
396 | 'options_per_line', | |
397 | 'start_date_years', | |
398 | 'end_date_years', | |
399 | 'date_format', | |
400 | 'time_format', | |
401 | 'option_group_id', | |
21dfd5f5 | 402 | 'in_selector', |
be2fb01f CW |
403 | ], |
404 | 'custom_group' => [ | |
6a488035 TO |
405 | 'id', |
406 | 'name', | |
407 | 'table_name', | |
408 | 'title', | |
409 | 'help_pre', | |
410 | 'help_post', | |
411 | 'collapse_display', | |
f6ad11c1 | 412 | 'style', |
6a488035 TO |
413 | 'is_multiple', |
414 | 'extends', | |
415 | 'extends_entity_column_id', | |
416 | 'extends_entity_column_value', | |
417 | 'max_multiple', | |
be2fb01f CW |
418 | ], |
419 | ]; | |
da0577d8 CW |
420 | $current_db_version = CRM_Core_BAO_Domain::version(); |
421 | $is_public_version = version_compare($current_db_version, '4.7.19', '>='); | |
422 | $serialize_version = version_compare($current_db_version, '5.27.alpha1', '>='); | |
f7ed3a24 SL |
423 | if ($is_public_version) { |
424 | $tableData['custom_group'][] = 'is_public'; | |
425 | } | |
da0577d8 CW |
426 | if ($serialize_version) { |
427 | $tableData['custom_field'][] = 'serialize'; | |
428 | } | |
0b330e6d CW |
429 | if (!$toReturn || !is_array($toReturn)) { |
430 | $toReturn = $tableData; | |
431 | } | |
432 | else { | |
ee21211c CW |
433 | // Supply defaults and remove unknown array keys |
434 | $toReturn = array_intersect_key(array_filter($toReturn) + $tableData, $tableData); | |
0b330e6d | 435 | // Merge in required fields that we must have |
be2fb01f CW |
436 | $toReturn['custom_field'] = array_unique(array_merge($toReturn['custom_field'], ['id', 'column_name', 'data_type'])); |
437 | $toReturn['custom_group'] = array_unique(array_merge($toReturn['custom_group'], ['id', 'is_multiple', 'table_name', 'name'])); | |
0b330e6d CW |
438 | // Validate return fields |
439 | $toReturn['custom_field'] = array_intersect($toReturn['custom_field'], array_keys(CRM_Core_DAO_CustomField::fieldKeys())); | |
440 | $toReturn['custom_group'] = array_intersect($toReturn['custom_group'], array_keys(CRM_Core_DAO_CustomGroup::fieldKeys())); | |
441 | } | |
6a488035 TO |
442 | |
443 | // create select | |
be2fb01f | 444 | $select = []; |
0b330e6d | 445 | foreach ($toReturn as $tableName => $tableColumn) { |
6a488035 | 446 | foreach ($tableColumn as $columnName) { |
0b330e6d | 447 | $select[] = "civicrm_{$tableName}.{$columnName} as civicrm_{$tableName}_{$columnName}"; |
6a488035 TO |
448 | } |
449 | } | |
450 | $strSelect = "SELECT " . implode(', ', $select); | |
451 | ||
452 | // from, where, order by | |
453 | $strFrom = " | |
454 | FROM civicrm_custom_group | |
455 | LEFT JOIN civicrm_custom_field ON (civicrm_custom_field.custom_group_id = civicrm_custom_group.id) | |
456 | "; | |
457 | ||
458 | // if entity is either individual, organization or household pls get custom groups for 'contact' too. | |
9d72cede EM |
459 | if ($entityType == "Individual" || $entityType == 'Organization' || |
460 | $entityType == 'Household' | |
461 | ) { | |
6a488035 TO |
462 | $in = "'$entityType', 'Contact'"; |
463 | } | |
464 | elseif (strpos($entityType, "'") !== FALSE) { | |
465 | // this allows the calling function to send in multiple entity types | |
466 | $in = $entityType; | |
467 | } | |
468 | else { | |
469 | // quote it | |
470 | $in = "'$entityType'"; | |
471 | } | |
472 | ||
be2fb01f | 473 | $params = []; |
babf3eef | 474 | $sqlParamKey = 1; |
b2ccd9fe | 475 | $subType = ''; |
79363422 E |
476 | if (!empty($subTypes)) { |
477 | foreach ($subTypes as $key => $subType) { | |
8cefa956 | 478 | $subTypeClauses[] = self::whereListHas("civicrm_custom_group.extends_entity_column_value", self::validateSubTypeByEntity($entityType, $subType)); |
6a488035 | 479 | } |
cf9ccf98 | 480 | $subTypeClause = '(' . implode(' OR ', $subTypeClauses) . ')'; |
79363422 E |
481 | if (!$onlySubType) { |
482 | $subTypeClause = '(' . $subTypeClause . ' OR civicrm_custom_group.extends_entity_column_value IS NULL )'; | |
993c3848 CB |
483 | } |
484 | ||
6a488035 TO |
485 | $strWhere = " |
486 | WHERE civicrm_custom_group.is_active = 1 | |
487 | AND civicrm_custom_field.is_active = 1 | |
488 | AND civicrm_custom_group.extends IN ($in) | |
489 | AND $subTypeClause | |
490 | "; | |
491 | if ($subName) { | |
babf3eef | 492 | $strWhere .= " AND civicrm_custom_group.extends_entity_column_id = %{$sqlParamKey}"; |
be2fb01f | 493 | $params[$sqlParamKey] = [$subName, 'String']; |
babf3eef | 494 | $sqlParamKey = $sqlParamKey + 1; |
6a488035 TO |
495 | } |
496 | } | |
497 | else { | |
498 | $strWhere = " | |
499 | WHERE civicrm_custom_group.is_active = 1 | |
500 | AND civicrm_custom_field.is_active = 1 | |
501 | AND civicrm_custom_group.extends IN ($in) | |
6a488035 | 502 | "; |
b62bc939 EM |
503 | if (!$returnAll) { |
504 | $strWhere .= "AND civicrm_custom_group.extends_entity_column_value IS NULL"; | |
505 | } | |
6a488035 TO |
506 | } |
507 | ||
6a488035 TO |
508 | if ($groupID > 0) { |
509 | // since we want a specific group id we add it to the where clause | |
babf3eef | 510 | $strWhere .= " AND civicrm_custom_group.id = %{$sqlParamKey}"; |
be2fb01f | 511 | $params[$sqlParamKey] = [$groupID, 'Integer']; |
6a488035 TO |
512 | } |
513 | elseif (!$groupID) { | |
514 | // since groupID is false we need to show all Inline groups | |
515 | $strWhere .= " AND civicrm_custom_group.style = 'Inline'"; | |
516 | } | |
e9ff5391 | 517 | if ($checkPermission) { |
518 | // ensure that the user has access to these custom groups | |
519 | $strWhere .= " AND " . | |
520 | CRM_Core_Permission::customGroupClause(CRM_Core_Permission::VIEW, | |
521 | 'civicrm_custom_group.' | |
522 | ); | |
523 | } | |
6a488035 | 524 | |
f7ed3a24 | 525 | if ($showPublicOnly && $is_public_version) { |
460931ca JP |
526 | $strWhere .= "AND civicrm_custom_group.is_public = 1"; |
527 | } | |
528 | ||
6a488035 TO |
529 | $orderBy = " |
530 | ORDER BY civicrm_custom_group.weight, | |
531 | civicrm_custom_group.title, | |
532 | civicrm_custom_field.weight, | |
533 | civicrm_custom_field.label | |
534 | "; | |
535 | ||
536 | // final query string | |
537 | $queryString = "$strSelect $strFrom $strWhere $orderBy"; | |
538 | ||
539 | // lets see if we can retrieve the groupTree from cache | |
540 | $cacheString = $queryString; | |
9d72cede | 541 | if ($groupID > 0) { |
6a488035 | 542 | $cacheString .= "_{$groupID}"; |
9d72cede EM |
543 | } |
544 | else { | |
6a488035 TO |
545 | $cacheString .= "_Inline"; |
546 | } | |
547 | ||
548 | $cacheKey = "CRM_Core_DAO_CustomGroup_Query " . md5($cacheString); | |
6c552737 | 549 | $multipleFieldGroupCacheKey = "CRM_Core_DAO_CustomGroup_QueryMultipleFields " . md5($cacheString); |
6a488035 | 550 | $cache = CRM_Utils_Cache::singleton(); |
6a488035 TO |
551 | if ($fromCache) { |
552 | $groupTree = $cache->get($cacheKey); | |
c43cedc8 | 553 | $multipleFieldGroups = $cache->get($multipleFieldGroupCacheKey); |
6a488035 TO |
554 | } |
555 | ||
556 | if (empty($groupTree)) { | |
b2ccd9fe | 557 | list($multipleFieldGroups, $groupTree) = self::buildGroupTree($entityType, $toReturn, $subTypes, $queryString, $params, $subType); |
6a488035 TO |
558 | |
559 | $cache->set($cacheKey, $groupTree); | |
c43cedc8 | 560 | $cache->set($multipleFieldGroupCacheKey, $multipleFieldGroups); |
6a488035 | 561 | } |
aa965915 | 562 | // entitySelectClauses is an array of select clauses for custom value tables which are not multiple |
c43cedc8 | 563 | // and have data for the given entities. $entityMultipleSelectClauses is the same for ones with multiple |
be2fb01f CW |
564 | $entitySingleSelectClauses = $entityMultipleSelectClauses = $groupTree['info']['select'] = []; |
565 | $singleFieldTables = []; | |
6a488035 TO |
566 | // now that we have all the groups and fields, lets get the values |
567 | // since we need to know the table and field names | |
6a488035 | 568 | // add info to groupTree |
c43cedc8 | 569 | |
9d72cede | 570 | if (isset($groupTree['info']) && !empty($groupTree['info']) && |
e1d48b72 | 571 | !empty($groupTree['info']['tables']) && $singleRecord != 'new' |
9d72cede | 572 | ) { |
be2fb01f | 573 | $select = $from = $where = []; |
c43cedc8 | 574 | $groupTree['info']['where'] = NULL; |
6a488035 | 575 | |
c43cedc8 | 576 | foreach ($groupTree['info']['tables'] as $table => $fields) { |
9d72cede | 577 | $groupTree['info']['from'][] = $table; |
be2fb01f | 578 | $select = [ |
9d72cede | 579 | "{$table}.id as {$table}_id", |
21dfd5f5 | 580 | "{$table}.entity_id as {$table}_entity_id", |
be2fb01f | 581 | ]; |
6a488035 TO |
582 | foreach ($fields as $column => $dontCare) { |
583 | $select[] = "{$table}.{$column} as {$table}_{$column}"; | |
584 | } | |
c43cedc8 | 585 | $groupTree['info']['select'] = array_merge($groupTree['info']['select'], $select); |
6a488035 | 586 | if ($entityID) { |
c43cedc8 | 587 | $groupTree['info']['where'][] = "{$table}.entity_id = $entityID"; |
9d72cede EM |
588 | if (in_array($table, $multipleFieldGroups) && |
589 | self::customGroupDataExistsForEntity($entityID, $table) | |
590 | ) { | |
c43cedc8 | 591 | $entityMultipleSelectClauses[$table] = $select; |
592 | } | |
9d72cede | 593 | else { |
c43cedc8 | 594 | $singleFieldTables[] = $table; |
595 | $entitySingleSelectClauses = array_merge($entitySingleSelectClauses, $select); | |
596 | } | |
597 | ||
6a488035 TO |
598 | } |
599 | } | |
c43cedc8 | 600 | if ($entityID && !empty($singleFieldTables)) { |
e8cb3963 | 601 | self::buildEntityTreeSingleFields($groupTree, $entityID, $entitySingleSelectClauses, $singleFieldTables); |
c43cedc8 | 602 | } |
603 | $multipleFieldTablesWithEntityData = array_keys($entityMultipleSelectClauses); | |
9d72cede | 604 | if (!empty($multipleFieldTablesWithEntityData)) { |
e1d48b72 | 605 | self::buildEntityTreeMultipleFields($groupTree, $entityID, $entityMultipleSelectClauses, $multipleFieldTablesWithEntityData, $singleRecord); |
c43cedc8 | 606 | } |
6a488035 | 607 | |
9d72cede | 608 | } |
c43cedc8 | 609 | return $groupTree; |
610 | } | |
6a488035 | 611 | |
79363422 E |
612 | /** |
613 | * Clean and validate the filter before it is used in a db query. | |
614 | * | |
615 | * @param string $entityType | |
616 | * @param string $subType | |
617 | * | |
618 | * @return string | |
619 | * @throws \CRM_Core_Exception | |
79363422 E |
620 | */ |
621 | protected static function validateSubTypeByEntity($entityType, $subType) { | |
622 | $subType = trim($subType, CRM_Core_DAO::VALUE_SEPARATOR); | |
623 | if (is_numeric($subType)) { | |
624 | return $subType; | |
625 | } | |
93ecb8e8 | 626 | |
627 | $contactTypes = CRM_Contact_BAO_ContactType::basicTypeInfo(TRUE); | |
be2fb01f | 628 | $contactTypes = array_merge($contactTypes, ['Event' => 1]); |
2072f97d | 629 | |
93ecb8e8 | 630 | if ($entityType != 'Contact' && !array_key_exists($entityType, $contactTypes)) { |
79363422 E |
631 | throw new CRM_Core_Exception('Invalid Entity Filter'); |
632 | } | |
93ecb8e8 | 633 | $subTypes = CRM_Contact_BAO_ContactType::subTypeInfo($entityType, TRUE); |
2072f97d | 634 | $subTypes = array_merge($subTypes, CRM_Event_PseudoConstant::eventType()); |
93ecb8e8 | 635 | if (!array_key_exists($subType, $subTypes)) { |
79363422 E |
636 | throw new CRM_Core_Exception('Invalid Filter'); |
637 | } | |
638 | return $subType; | |
639 | } | |
640 | ||
8cefa956 TO |
641 | /** |
642 | * Suppose you have a SQL column, $column, which includes a delimited list, and you want | |
643 | * a WHERE condition for rows that include $value. Use whereListHas(). | |
644 | * | |
645 | * @param string $column | |
646 | * @param string $value | |
647 | * @param string $delimiter | |
648 | * @return string | |
649 | * SQL condition. | |
650 | */ | |
518fa0ee | 651 | private static function whereListHas($column, $value, $delimiter = CRM_Core_DAO::VALUE_SEPARATOR) { |
cf9ccf98 TO |
652 | // ? |
653 | $bareValue = trim($value, $delimiter); | |
8cefa956 TO |
654 | $escapedValue = CRM_Utils_Type::escape("%{$delimiter}{$bareValue}{$delimiter}%", 'String', FALSE); |
655 | return "($column LIKE \"$escapedValue\")"; | |
656 | } | |
657 | ||
c43cedc8 | 658 | /** |
659 | * Check whether the custom group has any data for the given entity. | |
660 | * | |
6a0b768e TO |
661 | * @param int $entityID |
662 | * Id of entity for whom we are checking data for. | |
663 | * @param string $table | |
664 | * Table that we are checking. | |
c43cedc8 | 665 | * |
2a6da8d7 EM |
666 | * @param bool $getCount |
667 | * | |
5c766a0b | 668 | * @return bool |
a6c01b45 | 669 | * does this entity have data in this custom table |
c43cedc8 | 670 | */ |
518fa0ee | 671 | public static function customGroupDataExistsForEntity($entityID, $table, $getCount = FALSE) { |
c43cedc8 | 672 | $query = " |
673 | SELECT count(id) | |
674 | FROM $table | |
675 | WHERE entity_id = $entityID | |
676 | "; | |
677 | $recordExists = CRM_Core_DAO::singleValueQuery($query); | |
bf076628 PJ |
678 | if ($getCount) { |
679 | return $recordExists; | |
680 | } | |
63d76404 | 681 | return (bool) $recordExists; |
c43cedc8 | 682 | } |
6a488035 | 683 | |
9d72cede EM |
684 | /** |
685 | * Build the group tree for Custom fields which are not 'is_multiple' | |
686 | * | |
687 | * The combination of all these fields in one query with a 'using' join was not working for | |
688 | * multiple fields. These now have a new behaviour (one at a time) but the single fields still use this | |
689 | * mechanism as it seemed to be acceptable in this context | |
690 | * | |
6a0b768e TO |
691 | * @param array $groupTree |
692 | * (reference) group tree array which is being built. | |
693 | * @param int $entityID | |
694 | * Id of entity for whom the tree is being build up. | |
695 | * @param array $entitySingleSelectClauses | |
696 | * Array of select clauses relevant to the entity. | |
697 | * @param array $singleFieldTablesWithEntityData | |
698 | * Array of tables in which this entity has data. | |
9d72cede | 699 | */ |
518fa0ee | 700 | public static function buildEntityTreeSingleFields(&$groupTree, $entityID, $entitySingleSelectClauses, $singleFieldTablesWithEntityData) { |
c43cedc8 | 701 | $select = implode(', ', $entitySingleSelectClauses); |
702 | $fromSQL = " (SELECT $entityID as entity_id ) as first "; | |
703 | foreach ($singleFieldTablesWithEntityData as $table) { | |
704 | $fromSQL .= "\nLEFT JOIN $table USING (entity_id)"; | |
705 | } | |
6a488035 | 706 | |
c43cedc8 | 707 | $query = " |
708 | SELECT $select | |
709 | FROM $fromSQL | |
710 | WHERE first.entity_id = $entityID | |
711 | "; | |
e8cb3963 | 712 | self::buildTreeEntityDataFromQuery($groupTree, $query, $singleFieldTablesWithEntityData); |
c43cedc8 | 713 | } |
6a488035 | 714 | |
c43cedc8 | 715 | /** |
9d72cede EM |
716 | * Build the group tree for Custom fields which are 'is_multiple' |
717 | * | |
718 | * This is done one table at a time to avoid Cross-Joins resulting in too many rows being returned | |
719 | * | |
6a0b768e TO |
720 | * @param array $groupTree |
721 | * (reference) group tree array which is being built. | |
722 | * @param int $entityID | |
723 | * Id of entity for whom the tree is being build up. | |
724 | * @param array $entityMultipleSelectClauses | |
725 | * Array of select clauses relevant to the entity. | |
726 | * @param array $multipleFieldTablesWithEntityData | |
727 | * Array of tables in which this entity has data. | |
aa965915 | 728 | * @param string|int $singleRecord |
e1d48b72 | 729 | * holds 'new' or id if view/edit/copy form for a single record is being loaded. |
9d72cede | 730 | */ |
518fa0ee | 731 | public static function buildEntityTreeMultipleFields(&$groupTree, $entityID, $entityMultipleSelectClauses, $multipleFieldTablesWithEntityData, $singleRecord = NULL) { |
c43cedc8 | 732 | foreach ($entityMultipleSelectClauses as $table => $selectClauses) { |
733 | $select = implode(',', $selectClauses); | |
734 | $query = " | |
735 | SELECT $select | |
736 | FROM $table | |
737 | WHERE entity_id = $entityID | |
738 | "; | |
e1d48b72 | 739 | if ($singleRecord) { |
740 | $offset = $singleRecord - 1; | |
741 | $query .= " LIMIT {$offset}, 1"; | |
742 | } | |
be2fb01f | 743 | self::buildTreeEntityDataFromQuery($groupTree, $query, [$table], $singleRecord); |
c43cedc8 | 744 | } |
745 | } | |
746 | ||
747 | /** | |
748 | * Build the tree entity data - starting from a query retrieving the custom fields build the group | |
749 | * tree data for the relevant entity (entity is included in the query). | |
750 | * | |
751 | * This function represents shared code between the buildEntityTreeMultipleFields & the buildEntityTreeSingleFields function | |
752 | * | |
6a0b768e TO |
753 | * @param array $groupTree |
754 | * (reference) group tree array which is being built. | |
c43cedc8 | 755 | * @param string $query |
6a0b768e TO |
756 | * @param array $includedTables |
757 | * Tables to include - required because the function (for historical reasons). | |
16b10e64 | 758 | * iterates through the group tree |
aa965915 | 759 | * @param string|int $singleRecord |
e1d48b72 | 760 | * holds 'new' OR id if view/edit/copy form for a single record is being loaded. |
c43cedc8 | 761 | */ |
518fa0ee | 762 | public static function buildTreeEntityDataFromQuery(&$groupTree, $query, $includedTables, $singleRecord = NULL) { |
c43cedc8 | 763 | $dao = CRM_Core_DAO::executeQuery($query); |
764 | while ($dao->fetch()) { | |
765 | foreach ($groupTree as $groupID => $group) { | |
766 | if ($groupID === 'info') { | |
767 | continue; | |
768 | } | |
769 | $table = $groupTree[$groupID]['table_name']; | |
770 | //working from the groupTree instead of the table list means we have to iterate & exclude. | |
771 | // this could possibly be re-written as other parts of the function have been refactored | |
772 | // for now we just check if the given table is to be included in this function | |
9d72cede | 773 | if (!in_array($table, $includedTables)) { |
c43cedc8 | 774 | continue; |
775 | } | |
776 | foreach ($group['fields'] as $fieldID => $dontCare) { | |
e1d48b72 | 777 | self::buildCustomFieldData($dao, $groupTree, $table, $groupID, $fieldID, $singleRecord); |
c43cedc8 | 778 | } |
779 | } | |
780 | } | |
781 | } | |
782 | ||
783 | /** | |
784 | * Build the entity-specific custom data into the group tree on a per-field basis | |
785 | * | |
6a0b768e TO |
786 | * @param object $dao |
787 | * Object representing the custom field to be populated into the groupTree. | |
788 | * @param array $groupTree | |
789 | * (reference) the group tree being build. | |
790 | * @param string $table | |
791 | * Table name. | |
3a8f58ec | 792 | * @param int $groupID |
6a0b768e | 793 | * Custom group ID. |
3a8f58ec | 794 | * @param int $fieldID |
6a0b768e | 795 | * Custom field ID. |
aa965915 | 796 | * @param string|int $singleRecord |
e1d48b72 | 797 | * holds 'new' or id if loading view/edit/copy for a single record. |
c43cedc8 | 798 | */ |
518fa0ee | 799 | public static function buildCustomFieldData($dao, &$groupTree, $table, $groupID, $fieldID, $singleRecord = NULL) { |
9d72cede EM |
800 | $column = $groupTree[$groupID]['fields'][$fieldID]['column_name']; |
801 | $idName = "{$table}_id"; | |
c43cedc8 | 802 | $fieldName = "{$table}_{$column}"; |
803 | $dataType = $groupTree[$groupID]['fields'][$fieldID]['data_type']; | |
804 | if ($dataType == 'File') { | |
805 | if (isset($dao->$fieldName)) { | |
9d72cede EM |
806 | $config = CRM_Core_Config::singleton(); |
807 | $fileDAO = new CRM_Core_DAO_File(); | |
c43cedc8 | 808 | $fileDAO->id = $dao->$fieldName; |
809 | ||
810 | if ($fileDAO->find(TRUE)) { | |
811 | $entityIDName = "{$table}_entity_id"; | |
efddd94f | 812 | $fileHash = CRM_Core_BAO_File::generateFileHash($dao->$entityIDName, $fileDAO->id); |
c43cedc8 | 813 | $customValue['id'] = $dao->$idName; |
814 | $customValue['data'] = $fileDAO->uri; | |
815 | $customValue['fid'] = $fileDAO->id; | |
ff9aeadb | 816 | $customValue['fileURL'] = CRM_Utils_System::url('civicrm/file', "reset=1&id={$fileDAO->id}&eid={$dao->$entityIDName}&fcs=$fileHash"); |
c43cedc8 | 817 | $customValue['displayURL'] = NULL; |
818 | $deleteExtra = ts('Are you sure you want to delete attached file.'); | |
be2fb01f CW |
819 | $deleteURL = [ |
820 | CRM_Core_Action::DELETE => [ | |
c43cedc8 | 821 | 'name' => ts('Delete Attached File'), |
822 | 'url' => 'civicrm/file', | |
ff9aeadb | 823 | 'qs' => 'reset=1&id=%%id%%&eid=%%eid%%&fid=%%fid%%&action=delete&fcs=%%fcs%%', |
a1a2a83d TO |
824 | 'extra' => 'onclick = "if (confirm( \'' . $deleteExtra |
825 | . '\' ) ) this.href+=\'&confirmed=1\'; else return false;"', | |
be2fb01f CW |
826 | ], |
827 | ]; | |
c43cedc8 | 828 | $customValue['deleteURL'] = CRM_Core_Action::formLink($deleteURL, |
829 | CRM_Core_Action::DELETE, | |
be2fb01f | 830 | [ |
c43cedc8 | 831 | 'id' => $fileDAO->id, |
832 | 'eid' => $dao->$entityIDName, | |
833 | 'fid' => $fieldID, | |
ff9aeadb | 834 | 'fcs' => $fileHash, |
be2fb01f | 835 | ], |
87dab4a4 AH |
836 | ts('more'), |
837 | FALSE, | |
838 | 'file.manage.delete', | |
839 | 'File', | |
840 | $fileDAO->id | |
c43cedc8 | 841 | ); |
842 | $customValue['deleteURLArgs'] = CRM_Core_BAO_File::deleteURLArgs($table, $dao->$entityIDName, $fileDAO->id); | |
843 | $customValue['fileName'] = CRM_Utils_File::cleanFileName(basename($fileDAO->uri)); | |
844 | if ($fileDAO->mime_type == "image/jpeg" || | |
845 | $fileDAO->mime_type == "image/pjpeg" || | |
846 | $fileDAO->mime_type == "image/gif" || | |
847 | $fileDAO->mime_type == "image/x-png" || | |
848 | $fileDAO->mime_type == "image/png" | |
849 | ) { | |
850 | $customValue['displayURL'] = $customValue['fileURL']; | |
851 | $entityId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_EntityFile', | |
852 | $fileDAO->id, | |
853 | 'entity_id', | |
854 | 'file_id' | |
855 | ); | |
6c552737 | 856 | $customValue['imageURL'] = str_replace('persist/contribute', 'custom', $config->imageUploadURL) . |
9d72cede | 857 | $fileDAO->uri; |
9bbc34f1 | 858 | list($path) = CRM_Core_BAO_File::path($fileDAO->id, $entityId); |
a52de115 CB |
859 | if ($path && file_exists($path)) { |
860 | list($imageWidth, $imageHeight) = getimagesize($path); | |
861 | list($imageThumbWidth, $imageThumbHeight) = CRM_Contact_BAO_Contact::getThumbSize($imageWidth, $imageHeight); | |
862 | $customValue['imageThumbWidth'] = $imageThumbWidth; | |
863 | $customValue['imageThumbHeight'] = $imageThumbHeight; | |
864 | } | |
6a488035 TO |
865 | } |
866 | } | |
867 | } | |
c43cedc8 | 868 | else { |
be2fb01f | 869 | $customValue = [ |
c43cedc8 | 870 | 'id' => $dao->$idName, |
871 | 'data' => '', | |
be2fb01f | 872 | ]; |
c43cedc8 | 873 | } |
874 | } | |
875 | else { | |
be2fb01f | 876 | $customValue = [ |
c43cedc8 | 877 | 'id' => $dao->$idName, |
878 | 'data' => $dao->$fieldName, | |
be2fb01f | 879 | ]; |
6a488035 TO |
880 | } |
881 | ||
c43cedc8 | 882 | if (!array_key_exists('customValue', $groupTree[$groupID]['fields'][$fieldID])) { |
be2fb01f | 883 | $groupTree[$groupID]['fields'][$fieldID]['customValue'] = []; |
c43cedc8 | 884 | } |
e1d48b72 | 885 | if (empty($groupTree[$groupID]['fields'][$fieldID]['customValue']) && !empty($singleRecord)) { |
be2fb01f | 886 | $groupTree[$groupID]['fields'][$fieldID]['customValue'] = [$singleRecord => $customValue]; |
e1d48b72 | 887 | } |
888 | elseif (empty($groupTree[$groupID]['fields'][$fieldID]['customValue'])) { | |
be2fb01f | 889 | $groupTree[$groupID]['fields'][$fieldID]['customValue'] = [1 => $customValue]; |
c43cedc8 | 890 | } |
891 | else { | |
892 | $groupTree[$groupID]['fields'][$fieldID]['customValue'][] = $customValue; | |
893 | } | |
6a488035 TO |
894 | } |
895 | ||
896 | /** | |
897 | * Get the group title. | |
898 | * | |
6a0b768e TO |
899 | * @param int $id |
900 | * Id of group. | |
6a488035 | 901 | * |
a6c01b45 CW |
902 | * @return string |
903 | * title | |
6a488035 TO |
904 | */ |
905 | public static function getTitle($id) { | |
906 | return CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $id, 'title'); | |
907 | } | |
908 | ||
909 | /** | |
910 | * Get custom group details for a group. | |
911 | * | |
912 | * An array containing custom group details (including their custom field) is returned. | |
913 | * | |
6a0b768e TO |
914 | * @param int $groupId |
915 | * Group id whose details are needed. | |
916 | * @param bool $searchable | |
917 | * Is this field searchable. | |
918 | * @param array $extends | |
919 | * Which table does it extend if any. | |
dd244018 | 920 | * |
aa965915 | 921 | * @param bool $inSelector |
6a488035 | 922 | * |
a6c01b45 CW |
923 | * @return array |
924 | * array consisting of all group and field details | |
6a488035 | 925 | */ |
e41f4660 | 926 | public static function &getGroupDetail($groupId = NULL, $searchable = NULL, &$extends = NULL, $inSelector = NULL) { |
6a488035 | 927 | // create a new tree |
be2fb01f | 928 | $groupTree = []; |
6a488035 TO |
929 | |
930 | // using tableData to build the queryString | |
be2fb01f CW |
931 | $tableData = [ |
932 | 'civicrm_custom_field' => [ | |
6a488035 TO |
933 | 'id', |
934 | 'label', | |
935 | 'data_type', | |
936 | 'html_type', | |
937 | 'default_value', | |
938 | 'attributes', | |
939 | 'is_required', | |
940 | 'help_pre', | |
941 | 'help_post', | |
942 | 'options_per_line', | |
943 | 'is_searchable', | |
944 | 'start_date_years', | |
945 | 'end_date_years', | |
946 | 'is_search_range', | |
947 | 'date_format', | |
948 | 'time_format', | |
949 | 'note_columns', | |
950 | 'note_rows', | |
951 | 'column_name', | |
952 | 'is_view', | |
953 | 'option_group_id', | |
e41f4660 | 954 | 'in_selector', |
be2fb01f CW |
955 | ], |
956 | 'civicrm_custom_group' => [ | |
6a488035 TO |
957 | 'id', |
958 | 'name', | |
959 | 'title', | |
960 | 'help_pre', | |
961 | 'help_post', | |
962 | 'collapse_display', | |
963 | 'collapse_adv_display', | |
964 | 'extends', | |
965 | 'extends_entity_column_value', | |
966 | 'table_name', | |
e41f4660 | 967 | 'is_multiple', |
be2fb01f CW |
968 | ], |
969 | ]; | |
6a488035 TO |
970 | |
971 | // create select | |
be2fb01f | 972 | $s = []; |
6a488035 TO |
973 | foreach ($tableData as $tableName => $tableColumn) { |
974 | foreach ($tableColumn as $columnName) { | |
975 | $s[] = "{$tableName}.{$columnName} as {$tableName}_{$columnName}"; | |
976 | } | |
977 | } | |
978 | $select = 'SELECT ' . implode(', ', $s); | |
be2fb01f | 979 | $params = []; |
6a488035 TO |
980 | // from, where, order by |
981 | $from = " FROM civicrm_custom_field, civicrm_custom_group"; | |
982 | $where = " WHERE civicrm_custom_field.custom_group_id = civicrm_custom_group.id | |
983 | AND civicrm_custom_group.is_active = 1 | |
984 | AND civicrm_custom_field.is_active = 1 "; | |
985 | if ($groupId) { | |
be2fb01f | 986 | $params[1] = [$groupId, 'Integer']; |
6a488035 TO |
987 | $where .= " AND civicrm_custom_group.id = %1"; |
988 | } | |
989 | ||
990 | if ($searchable) { | |
991 | $where .= " AND civicrm_custom_field.is_searchable = 1"; | |
992 | } | |
993 | ||
e41f4660 PJ |
994 | if ($inSelector) { |
995 | $where .= " AND civicrm_custom_field.in_selector = 1 AND civicrm_custom_group.is_multiple = 1 "; | |
996 | } | |
997 | ||
6a488035 | 998 | if ($extends) { |
be2fb01f | 999 | $clause = []; |
6a488035 TO |
1000 | foreach ($extends as $e) { |
1001 | $clause[] = "civicrm_custom_group.extends = '$e'"; | |
1002 | } | |
1003 | $where .= " AND ( " . implode(' OR ', $clause) . " ) "; | |
1004 | ||
1005 | //include case activities customdata if case is enabled | |
1006 | if (in_array('Activity', $extends)) { | |
1007 | $extendValues = implode(',', array_keys(CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'label', TRUE))); | |
1008 | $where .= " AND ( civicrm_custom_group.extends_entity_column_value IS NULL OR REPLACE( civicrm_custom_group.extends_entity_column_value, %2, ' ') IN ($extendValues) ) "; | |
be2fb01f | 1009 | $params[2] = [CRM_Core_DAO::VALUE_SEPARATOR, 'String']; |
6a488035 TO |
1010 | } |
1011 | } | |
1012 | ||
1013 | // ensure that the user has access to these custom groups | |
9d72cede EM |
1014 | $where .= " AND " . |
1015 | CRM_Core_Permission::customGroupClause(CRM_Core_Permission::VIEW, | |
1016 | 'civicrm_custom_group.' | |
1017 | ); | |
6a488035 TO |
1018 | |
1019 | $orderBy = " ORDER BY civicrm_custom_group.weight, civicrm_custom_field.weight"; | |
1020 | ||
1021 | // final query string | |
1022 | $queryString = $select . $from . $where . $orderBy; | |
1023 | ||
1024 | // dummy dao needed | |
1025 | $crmDAO = CRM_Core_DAO::executeQuery($queryString, $params); | |
1026 | ||
1027 | // process records | |
1028 | while ($crmDAO->fetch()) { | |
1029 | $groupId = $crmDAO->civicrm_custom_group_id; | |
1030 | $fieldId = $crmDAO->civicrm_custom_field_id; | |
1031 | ||
1032 | // create an array for groups if it does not exist | |
1033 | if (!array_key_exists($groupId, $groupTree)) { | |
be2fb01f | 1034 | $groupTree[$groupId] = []; |
6a488035 TO |
1035 | $groupTree[$groupId]['id'] = $groupId; |
1036 | ||
1037 | foreach ($tableData['civicrm_custom_group'] as $v) { | |
1038 | $fullField = "civicrm_custom_group_" . $v; | |
1039 | ||
1040 | if ($v == 'id' || is_null($crmDAO->$fullField)) { | |
1041 | continue; | |
1042 | } | |
1043 | ||
1044 | $groupTree[$groupId][$v] = $crmDAO->$fullField; | |
1045 | } | |
1046 | ||
be2fb01f | 1047 | $groupTree[$groupId]['fields'] = []; |
6a488035 TO |
1048 | } |
1049 | ||
1050 | // add the fields now (note - the query row will always contain a field) | |
be2fb01f | 1051 | $groupTree[$groupId]['fields'][$fieldId] = []; |
6a488035 TO |
1052 | $groupTree[$groupId]['fields'][$fieldId]['id'] = $fieldId; |
1053 | ||
1054 | foreach ($tableData['civicrm_custom_field'] as $v) { | |
1055 | $fullField = "civicrm_custom_field_" . $v; | |
1056 | if ($v == 'id' || is_null($crmDAO->$fullField)) { | |
1057 | continue; | |
1058 | } | |
1059 | $groupTree[$groupId]['fields'][$fieldId][$v] = $crmDAO->$fullField; | |
1060 | } | |
1061 | } | |
1062 | ||
1063 | return $groupTree; | |
1064 | } | |
1065 | ||
b5c2afd0 EM |
1066 | /** |
1067 | * @param $entityType | |
1068 | * @param $path | |
1069 | * @param string $cidToken | |
1070 | * | |
1071 | * @return array | |
1072 | */ | |
6a488035 TO |
1073 | public static function &getActiveGroups($entityType, $path, $cidToken = '%%cid%%') { |
1074 | // for Group's | |
1075 | $customGroupDAO = new CRM_Core_DAO_CustomGroup(); | |
1076 | ||
e41f4660 PJ |
1077 | // get 'Tab' and 'Tab with table' groups |
1078 | $customGroupDAO->whereAdd("style IN ('Tab', 'Tab with table')"); | |
6a488035 TO |
1079 | $customGroupDAO->whereAdd("is_active = 1"); |
1080 | ||
1081 | // add whereAdd for entity type | |
1082 | self::_addWhereAdd($customGroupDAO, $entityType, $cidToken); | |
1083 | ||
be2fb01f | 1084 | $groups = []; |
6a488035 TO |
1085 | |
1086 | $permissionClause = CRM_Core_Permission::customGroupClause(CRM_Core_Permission::VIEW, NULL, TRUE); | |
1087 | $customGroupDAO->whereAdd($permissionClause); | |
1088 | ||
1089 | // order by weight | |
1090 | $customGroupDAO->orderBy('weight'); | |
1091 | $customGroupDAO->find(); | |
1092 | ||
1093 | // process each group with menu tab | |
1094 | while ($customGroupDAO->fetch()) { | |
be2fb01f | 1095 | $group = []; |
9d72cede EM |
1096 | $group['id'] = $customGroupDAO->id; |
1097 | $group['path'] = $path; | |
1098 | $group['title'] = "$customGroupDAO->title"; | |
1099 | $group['query'] = "reset=1&gid={$customGroupDAO->id}&cid={$cidToken}"; | |
be2fb01f | 1100 | $group['extra'] = ['gid' => $customGroupDAO->id]; |
6a488035 | 1101 | $group['table_name'] = $customGroupDAO->table_name; |
a97040f5 | 1102 | $group['is_multiple'] = $customGroupDAO->is_multiple; |
2af06525 | 1103 | $group['icon'] = $customGroupDAO->icon; |
9d72cede | 1104 | $groups[] = $group; |
6a488035 TO |
1105 | } |
1106 | ||
1107 | return $groups; | |
1108 | } | |
1109 | ||
1110 | /** | |
1111 | * Get the table name for the entity type | |
1112 | * currently if entity type is 'Contact', 'Individual', 'Household', 'Organization' | |
1113 | * tableName is 'civicrm_contact' | |
1114 | * | |
6a0b768e TO |
1115 | * @param string $entityType |
1116 | * What entity are we extending here ?. | |
6a488035 | 1117 | * |
a6c01b45 | 1118 | * @return string |
6a488035 | 1119 | * |
6a488035 | 1120 | * |
6de76520 | 1121 | * @see _apachesolr_civiAttachments_dereference_file_parent |
6a488035 | 1122 | */ |
6de76520 | 1123 | public static function getTableNameByEntityName($entityType) { |
6a488035 TO |
1124 | $tableName = ''; |
1125 | switch ($entityType) { | |
1126 | case 'Contact': | |
1127 | case 'Individual': | |
1128 | case 'Household': | |
1129 | case 'Organization': | |
1130 | $tableName = 'civicrm_contact'; | |
1131 | break; | |
1132 | ||
1133 | case 'Contribution': | |
1134 | $tableName = 'civicrm_contribution'; | |
1135 | break; | |
1136 | ||
1137 | case 'Group': | |
1138 | $tableName = 'civicrm_group'; | |
1139 | break; | |
2aa397bc | 1140 | |
6a488035 TO |
1141 | // DRAFTING: Verify if we cannot make it pluggable |
1142 | ||
1143 | case 'Activity': | |
1144 | $tableName = 'civicrm_activity'; | |
1145 | break; | |
1146 | ||
1147 | case 'Relationship': | |
1148 | $tableName = 'civicrm_relationship'; | |
1149 | break; | |
1150 | ||
1151 | case 'Membership': | |
1152 | $tableName = 'civicrm_membership'; | |
1153 | break; | |
1154 | ||
1155 | case 'Participant': | |
1156 | $tableName = 'civicrm_participant'; | |
1157 | break; | |
1158 | ||
1159 | case 'Event': | |
1160 | $tableName = 'civicrm_event'; | |
1161 | break; | |
1162 | ||
1163 | case 'Grant': | |
1164 | $tableName = 'civicrm_grant'; | |
1165 | break; | |
2aa397bc | 1166 | |
6a488035 TO |
1167 | // need to add cases for Location, Address |
1168 | } | |
1169 | ||
1170 | return $tableName; | |
1171 | } | |
1172 | ||
1173 | /** | |
1174 | * Get a list of custom groups which extend a given entity type. | |
1175 | * If there are custom-groups which only apply to certain subtypes, | |
1176 | * those WILL be included. | |
1177 | * | |
5a4f6742 | 1178 | * @param string $entityType |
9d72cede | 1179 | * |
6a488035 TO |
1180 | * @return CRM_Core_DAO_CustomGroup |
1181 | */ | |
00be9182 | 1182 | public static function getAllCustomGroupsByBaseEntity($entityType) { |
6a488035 TO |
1183 | $customGroupDAO = new CRM_Core_DAO_CustomGroup(); |
1184 | self::_addWhereAdd($customGroupDAO, $entityType, NULL, TRUE); | |
1185 | return $customGroupDAO; | |
1186 | } | |
1187 | ||
1188 | /** | |
1189 | * Add the whereAdd clause for the DAO depending on the type of entity | |
1190 | * the custom group is extending. | |
1191 | * | |
6c552737 | 1192 | * @param object $customGroupDAO |
6a0b768e TO |
1193 | * @param string $entityType |
1194 | * What entity are we extending here ?. | |
fd31fa4c | 1195 | * |
6c552737 | 1196 | * @param int $entityID |
fd31fa4c | 1197 | * @param bool $allSubtypes |
6a488035 TO |
1198 | */ |
1199 | private static function _addWhereAdd(&$customGroupDAO, $entityType, $entityID = NULL, $allSubtypes = FALSE) { | |
1200 | $addSubtypeClause = FALSE; | |
6bbc383d | 1201 | // This function isn't really accessible with user data but since the string |
1202 | // is not passed as a param to the query CRM_Core_DAO::escapeString seems like a harmless | |
1203 | // precaution. | |
1204 | $entityType = CRM_Core_DAO::escapeString($entityType); | |
6a488035 TO |
1205 | |
1206 | switch ($entityType) { | |
1207 | case 'Contact': | |
1208 | // if contact, get all related to contact | |
1209 | $extendList = "'Contact','Individual','Household','Organization'"; | |
1210 | $customGroupDAO->whereAdd("extends IN ( $extendList )"); | |
1211 | if (!$allSubtypes) { | |
1212 | $addSubtypeClause = TRUE; | |
1213 | } | |
1214 | break; | |
1215 | ||
1216 | case 'Individual': | |
1217 | case 'Household': | |
1218 | case 'Organization': | |
1219 | // is I/H/O then get I/H/O and contact | |
1220 | $extendList = "'Contact','$entityType'"; | |
1221 | $customGroupDAO->whereAdd("extends IN ( $extendList )"); | |
1222 | if (!$allSubtypes) { | |
1223 | $addSubtypeClause = TRUE; | |
1224 | } | |
1225 | break; | |
1226 | ||
6bbc383d | 1227 | default: |
6a488035 TO |
1228 | $customGroupDAO->whereAdd("extends IN ('$entityType')"); |
1229 | break; | |
1230 | } | |
1231 | ||
1232 | if ($addSubtypeClause) { | |
1233 | $csType = is_numeric($entityID) ? CRM_Contact_BAO_Contact::getContactSubType($entityID) : FALSE; | |
1234 | ||
1235 | if (!empty($csType)) { | |
be2fb01f | 1236 | $subtypeClause = []; |
6a488035 | 1237 | foreach ($csType as $subtype) { |
9d72cede EM |
1238 | $subtype = CRM_Core_DAO::VALUE_SEPARATOR . $subtype . |
1239 | CRM_Core_DAO::VALUE_SEPARATOR; | |
6a488035 TO |
1240 | $subtypeClause[] = "extends_entity_column_value LIKE '%{$subtype}%'"; |
1241 | } | |
1242 | $subtypeClause[] = "extends_entity_column_value IS NULL"; | |
9d72cede EM |
1243 | $customGroupDAO->whereAdd("( " . implode(' OR ', $subtypeClause) . |
1244 | " )"); | |
6a488035 TO |
1245 | } |
1246 | else { | |
1247 | $customGroupDAO->whereAdd("extends_entity_column_value IS NULL"); | |
1248 | } | |
1249 | } | |
1250 | } | |
1251 | ||
1252 | /** | |
1253 | * Delete the Custom Group. | |
1254 | * | |
3a8f58ec EM |
1255 | * @param CRM_Core_BAO_CustomGroup $group |
1256 | * Custom group object. | |
5a4f6742 CW |
1257 | * @param bool $force |
1258 | * whether to force the deletion, even if there are custom fields. | |
6a488035 | 1259 | * |
5c766a0b | 1260 | * @return bool |
3a8f58ec | 1261 | * False if field exists for this group, true if group gets deleted. |
6a488035 TO |
1262 | */ |
1263 | public static function deleteGroup($group, $force = FALSE) { | |
1264 | ||
3a8f58ec | 1265 | //check whether this contain any custom fields |
6a488035 TO |
1266 | $customField = new CRM_Core_DAO_CustomField(); |
1267 | $customField->custom_group_id = $group->id; | |
1268 | $customField->find(); | |
1269 | ||
1270 | // return early if there are custom fields and we're not | |
1271 | // forcing the delete, otherwise delete the fields one by one | |
1272 | while ($customField->fetch()) { | |
1273 | if (!$force) { | |
1274 | return FALSE; | |
1275 | } | |
1276 | CRM_Core_BAO_CustomField::deleteField($customField); | |
1277 | } | |
1278 | ||
1279 | // drop the table associated with this custom group | |
1280 | CRM_Core_BAO_SchemaHandler::dropTable($group->table_name); | |
1281 | ||
1282 | //delete custom group | |
1283 | $group->delete(); | |
1284 | ||
1285 | CRM_Utils_Hook::post('delete', 'CustomGroup', $group->id, $group); | |
1286 | ||
1287 | return TRUE; | |
1288 | } | |
1289 | ||
b5c2afd0 | 1290 | /** |
3a8f58ec EM |
1291 | * Set defaults. |
1292 | * | |
1293 | * @param array $groupTree | |
1294 | * @param array $defaults | |
b5c2afd0 EM |
1295 | * @param bool $viewMode |
1296 | * @param bool $inactiveNeeded | |
1297 | * @param int $action | |
1298 | */ | |
00be9182 | 1299 | public static function setDefaults(&$groupTree, &$defaults, $viewMode = FALSE, $inactiveNeeded = FALSE, $action = CRM_Core_Action::NONE) { |
6a488035 TO |
1300 | foreach ($groupTree as $id => $group) { |
1301 | if (!isset($group['fields'])) { | |
1302 | continue; | |
1303 | } | |
6a488035 | 1304 | foreach ($group['fields'] as $field) { |
015ba54e | 1305 | if (isset($field['element_value'])) { |
6a488035 TO |
1306 | $value = $field['element_value']; |
1307 | } | |
015ba54e | 1308 | elseif (isset($field['default_value']) && |
6a488035 TO |
1309 | ($action != CRM_Core_Action::UPDATE || |
1310 | // CRM-7548 | |
1311 | !array_key_exists('element_value', $field) | |
1312 | ) | |
1313 | ) { | |
1314 | $value = $viewMode ? NULL : $field['default_value']; | |
1315 | } | |
1316 | else { | |
1317 | continue; | |
1318 | } | |
1319 | ||
3dd2f681 BZ |
1320 | if (empty($field['element_name'])) { |
1321 | continue; | |
6df71703 | 1322 | } |
58593e74 | 1323 | |
3dd2f681 | 1324 | $elementName = $field['element_name']; |
e30142ba | 1325 | $serialize = CRM_Core_BAO_CustomField::isSerialized($field); |
58593e74 | 1326 | |
e30142ba CW |
1327 | if ($serialize) { |
1328 | if ($field['data_type'] != 'Country' && $field['data_type'] != 'StateProvince') { | |
be2fb01f | 1329 | $defaults[$elementName] = []; |
6a488035 TO |
1330 | $customOption = CRM_Core_BAO_CustomOption::getCustomOption($field['id'], $inactiveNeeded); |
1331 | if ($viewMode) { | |
1332 | $checkedData = explode(CRM_Core_DAO::VALUE_SEPARATOR, substr($value, 1, -1)); | |
1333 | if (isset($value)) { | |
1334 | foreach ($customOption as $customValue => $customLabel) { | |
1335 | if (in_array($customValue, $checkedData)) { | |
1336 | if ($field['html_type'] == 'CheckBox') { | |
1337 | $defaults[$elementName][$customValue] = 1; | |
1338 | } | |
1339 | else { | |
1340 | $defaults[$elementName][$customValue] = $customValue; | |
1341 | } | |
1342 | } | |
1343 | else { | |
1344 | $defaults[$elementName][$customValue] = 0; | |
1345 | } | |
1346 | } | |
1347 | } | |
1348 | } | |
1349 | else { | |
1350 | if (isset($field['customValue']['data'])) { | |
1351 | $checkedData = explode(CRM_Core_DAO::VALUE_SEPARATOR, substr($field['customValue']['data'], 1, -1)); | |
1352 | foreach ($customOption as $val) { | |
1353 | if (in_array($val['value'], $checkedData)) { | |
1354 | if ($field['html_type'] == 'CheckBox') { | |
1355 | $defaults[$elementName][$val['value']] = 1; | |
1356 | } | |
1357 | else { | |
1358 | $defaults[$elementName][$val['value']] = $val['value']; | |
1359 | } | |
1360 | } | |
1361 | else { | |
1362 | $defaults[$elementName][$val['value']] = 0; | |
1363 | } | |
1364 | } | |
1365 | } | |
1366 | else { | |
b4afac65 | 1367 | // Values may be "array strings" or actual arrays. Handle both. |
fb56e964 | 1368 | if (is_array($value) && count($value)) { |
1369 | CRM_Utils_Array::formatArrayKeys($value); | |
1370 | $checkedValue = $value; | |
1371 | } | |
1372 | else { | |
1373 | $checkedValue = explode(CRM_Core_DAO::VALUE_SEPARATOR, substr($value, 1, -1)); | |
1374 | } | |
6a488035 TO |
1375 | foreach ($customOption as $val) { |
1376 | if (in_array($val['value'], $checkedValue)) { | |
1377 | if ($field['html_type'] == 'CheckBox') { | |
1378 | $defaults[$elementName][$val['value']] = 1; | |
1379 | } | |
1380 | else { | |
1381 | $defaults[$elementName][$val['value']] = $val['value']; | |
1382 | } | |
1383 | } | |
1384 | } | |
1385 | } | |
1386 | } | |
e30142ba CW |
1387 | } |
1388 | else { | |
6a488035 | 1389 | if (isset($value)) { |
b4afac65 CW |
1390 | // Values may be "array strings" or actual arrays. Handle both. |
1391 | if (is_array($value) && count($value)) { | |
1392 | CRM_Utils_Array::formatArrayKeys($value); | |
1393 | $checkedValue = $value; | |
1394 | } | |
1395 | else { | |
1396 | $checkedValue = explode(CRM_Core_DAO::VALUE_SEPARATOR, substr($value, 1, -1)); | |
1397 | } | |
6a488035 TO |
1398 | foreach ($checkedValue as $val) { |
1399 | if ($val) { | |
1400 | $defaults[$elementName][$val] = $val; | |
1401 | } | |
1402 | } | |
1403 | } | |
e30142ba CW |
1404 | } |
1405 | } | |
1406 | else { | |
1407 | if ($field['data_type'] == 'Country') { | |
6a488035 TO |
1408 | if ($value) { |
1409 | $defaults[$elementName] = $value; | |
1410 | } | |
1411 | else { | |
1412 | $config = CRM_Core_Config::singleton(); | |
1413 | $defaults[$elementName] = $config->defaultContactCountry; | |
1414 | } | |
e30142ba CW |
1415 | } |
1416 | else { | |
6a488035 | 1417 | if ($field['data_type'] == "Float") { |
9d72cede | 1418 | $defaults[$elementName] = (float) $value; |
6a488035 TO |
1419 | } |
1420 | elseif ($field['data_type'] == 'Money' && | |
1421 | $field['html_type'] == 'Text' | |
1422 | ) { | |
1423 | $defaults[$elementName] = CRM_Utils_Money::format($value, NULL, '%a'); | |
1424 | } | |
1425 | else { | |
1426 | $defaults[$elementName] = $value; | |
1427 | } | |
e30142ba | 1428 | } |
6a488035 TO |
1429 | } |
1430 | } | |
1431 | } | |
1432 | } | |
1433 | ||
b5c2afd0 | 1434 | /** |
54844703 CW |
1435 | * Old function only called from one place... |
1436 | * @see CRM_Dedupe_Finder::formatParams | |
3a8f58ec EM |
1437 | * |
1438 | * @param array $groupTree | |
c490a46a | 1439 | * @param array $params |
b5c2afd0 EM |
1440 | * @param bool $skipFile |
1441 | */ | |
00be9182 | 1442 | public static function postProcess(&$groupTree, &$params, $skipFile = FALSE) { |
6a488035 | 1443 | // Get the Custom form values and groupTree |
6a488035 TO |
1444 | foreach ($groupTree as $groupID => $group) { |
1445 | if ($groupID === 'info') { | |
1446 | continue; | |
1447 | } | |
1448 | foreach ($group['fields'] as $field) { | |
1449 | $fieldId = $field['id']; | |
54844703 | 1450 | $serialize = CRM_Core_BAO_CustomField::isSerialized($field); |
6a488035 | 1451 | |
54844703 CW |
1452 | // Reset all checkbox, radio and multiselect data |
1453 | if ($field['html_type'] == 'Radio' || $serialize) { | |
6a488035 TO |
1454 | $groupTree[$groupID]['fields'][$fieldId]['customValue']['data'] = 'NULL'; |
1455 | } | |
1456 | ||
1457 | $v = NULL; | |
1458 | foreach ($params as $key => $val) { | |
1459 | if (preg_match('/^custom_(\d+)_?(-?\d+)?$/', $key, $match) && | |
1460 | $match[1] == $field['id'] | |
1461 | ) { | |
1462 | $v = $val; | |
1463 | } | |
1464 | } | |
1465 | ||
6a488035 TO |
1466 | if (!isset($groupTree[$groupID]['fields'][$fieldId]['customValue'])) { |
1467 | // field exists in db so populate value from "form". | |
be2fb01f | 1468 | $groupTree[$groupID]['fields'][$fieldId]['customValue'] = []; |
6a488035 TO |
1469 | } |
1470 | ||
54844703 CW |
1471 | // Serialize checkbox and multi-select data (using array keys for checkbox) |
1472 | if ($serialize) { | |
1473 | $v = ($v && $field['html_type'] === 'Checkbox') ? array_keys($v) : $v; | |
1474 | $v = $v ? CRM_Utils_Array::implodePadded($v) : NULL; | |
1475 | } | |
6a488035 | 1476 | |
54844703 | 1477 | switch ($field['html_type']) { |
6a488035 TO |
1478 | |
1479 | case 'Select Date': | |
1480 | $date = CRM_Utils_Date::processDate($v); | |
1481 | $groupTree[$groupID]['fields'][$fieldId]['customValue']['data'] = $date; | |
1482 | break; | |
1483 | ||
1484 | case 'File': | |
1485 | if ($skipFile) { | |
94b444d0 | 1486 | break; |
6a488035 TO |
1487 | } |
1488 | ||
aa965915 | 1489 | // store the file in d/b |
6a488035 | 1490 | $entityId = explode('=', $groupTree['info']['where'][0]); |
be2fb01f | 1491 | $fileParams = ['upload_date' => date('YmdHis')]; |
6a488035 TO |
1492 | |
1493 | if ($groupTree[$groupID]['fields'][$fieldId]['customValue']['fid']) { | |
1494 | $fileParams['id'] = $groupTree[$groupID]['fields'][$fieldId]['customValue']['fid']; | |
1495 | } | |
1496 | if (!empty($v)) { | |
1497 | $fileParams['uri'] = $v['name']; | |
1498 | $fileParams['mime_type'] = $v['type']; | |
1499 | CRM_Core_BAO_File::filePostProcess($v['name'], | |
1500 | $groupTree[$groupID]['fields'][$fieldId]['customValue']['fid'], | |
1501 | $groupTree[$groupID]['table_name'], | |
1502 | trim($entityId[1]), | |
1503 | FALSE, | |
1504 | TRUE, | |
1505 | $fileParams, | |
1506 | 'custom_' . $fieldId, | |
1507 | $v['type'] | |
1508 | ); | |
1509 | } | |
be2fb01f CW |
1510 | $defaults = []; |
1511 | $paramsFile = [ | |
6a488035 TO |
1512 | 'entity_table' => $groupTree[$groupID]['table_name'], |
1513 | 'entity_id' => $entityId[1], | |
be2fb01f | 1514 | ]; |
6a488035 TO |
1515 | |
1516 | CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_EntityFile', | |
1517 | $paramsFile, | |
1518 | $defaults | |
1519 | ); | |
1520 | ||
1521 | $groupTree[$groupID]['fields'][$fieldId]['customValue']['data'] = $defaults['file_id']; | |
1522 | break; | |
1523 | ||
1524 | default: | |
1525 | $groupTree[$groupID]['fields'][$fieldId]['customValue']['data'] = $v; | |
1526 | break; | |
1527 | } | |
1528 | } | |
1529 | } | |
1530 | } | |
1531 | ||
1532 | /** | |
fe482240 | 1533 | * Generic function to build all the form elements for a specific group tree. |
6a488035 | 1534 | * |
6a0b768e TO |
1535 | * @param CRM_Core_Form $form |
1536 | * The form object. | |
1537 | * @param array $groupTree | |
1538 | * The group tree object. | |
1539 | * @param bool $inactiveNeeded | |
1540 | * Return inactive custom groups. | |
1541 | * @param string $prefix | |
1542 | * Prefix for custom grouptree assigned to template. | |
aa965915 MWMC |
1543 | * |
1544 | * @throws \CiviCRM_API3_Exception | |
6a488035 | 1545 | */ |
00be9182 | 1546 | public static function buildQuickForm(&$form, &$groupTree, $inactiveNeeded = FALSE, $prefix = '') { |
6a488035 | 1547 | $form->assign_by_ref("{$prefix}groupTree", $groupTree); |
6a488035 | 1548 | |
6a488035 | 1549 | foreach ($groupTree as $id => $group) { |
6a488035 | 1550 | foreach ($group['fields'] as $field) { |
9c1bc317 | 1551 | $required = $field['is_required'] ?? NULL; |
6a488035 TO |
1552 | //fix for CRM-1620 |
1553 | if ($field['data_type'] == 'File') { | |
bdadc7ac | 1554 | if (!empty($field['element_value']['data'])) { |
6a488035 TO |
1555 | $required = 0; |
1556 | } | |
1557 | } | |
1558 | ||
1559 | $fieldId = $field['id']; | |
1560 | $elementName = $field['element_name']; | |
3a7773be | 1561 | CRM_Core_BAO_CustomField::addQuickFormElement($form, $elementName, $fieldId, $required); |
25cdc89e MWMC |
1562 | if ($form->getAction() == CRM_Core_Action::VIEW) { |
1563 | $form->getElement($elementName)->freeze(); | |
1564 | } | |
6a488035 TO |
1565 | } |
1566 | } | |
1567 | } | |
1568 | ||
1569 | /** | |
3a8f58ec | 1570 | * Extract the get params from the url, validate and store it in session. |
6a488035 | 1571 | * |
6a0b768e TO |
1572 | * @param CRM_Core_Form $form |
1573 | * The form object. | |
1574 | * @param string $type | |
1575 | * The type of custom group we are using. | |
6a488035 | 1576 | * |
03110609 | 1577 | * @return array |
aa965915 MWMC |
1578 | * @throws \CRM_Core_Exception |
1579 | * @throws \CiviCRM_API3_Exception | |
6a488035 | 1580 | */ |
00be9182 | 1581 | public static function extractGetParams(&$form, $type) { |
6a488035 | 1582 | if (empty($_GET)) { |
be2fb01f | 1583 | return []; |
6a488035 TO |
1584 | } |
1585 | ||
79363422 | 1586 | $groupTree = CRM_Core_BAO_CustomGroup::getTree($type); |
be2fb01f CW |
1587 | $customValue = []; |
1588 | $htmlType = [ | |
9d72cede EM |
1589 | 'CheckBox', |
1590 | 'Multi-Select', | |
9d72cede | 1591 | 'Select', |
21dfd5f5 | 1592 | 'Radio', |
be2fb01f | 1593 | ]; |
6a488035 TO |
1594 | |
1595 | foreach ($groupTree as $group) { | |
1596 | if (!isset($group['fields'])) { | |
1597 | continue; | |
1598 | } | |
1599 | foreach ($group['fields'] as $key => $field) { | |
1600 | $fieldName = 'custom_' . $key; | |
6fc7508a | 1601 | $value = CRM_Utils_Request::retrieve($fieldName, 'String', $form, FALSE, NULL, 'GET'); |
6a488035 TO |
1602 | |
1603 | if ($value) { | |
1604 | $valid = FALSE; | |
1605 | if (!in_array($field['html_type'], $htmlType) || | |
1606 | $field['data_type'] == 'Boolean' | |
1607 | ) { | |
1608 | $valid = CRM_Core_BAO_CustomValue::typecheck($field['data_type'], $value); | |
1609 | } | |
3d86eb05 | 1610 | if (CRM_Core_BAO_CustomField::isSerialized($field)) { |
9d72cede EM |
1611 | $value = str_replace("|", ",", $value); |
1612 | $mulValues = explode(',', $value); | |
6a488035 | 1613 | $customOption = CRM_Core_BAO_CustomOption::getCustomOption($key, TRUE); |
be2fb01f | 1614 | $val = []; |
6a488035 TO |
1615 | foreach ($mulValues as $v1) { |
1616 | foreach ($customOption as $coID => $coValue) { | |
9d72cede EM |
1617 | if (strtolower(trim($coValue['label'])) == |
1618 | strtolower(trim($v1)) | |
1619 | ) { | |
6a488035 TO |
1620 | $val[$coValue['value']] = 1; |
1621 | } | |
1622 | } | |
1623 | } | |
1624 | if (!empty($val)) { | |
1625 | $value = $val; | |
1626 | $valid = TRUE; | |
1627 | } | |
1628 | else { | |
1629 | $value = NULL; | |
1630 | } | |
1631 | } | |
36769590 | 1632 | elseif ($field['html_type'] === 'Select' || |
1633 | ($field['html_type'] === 'Radio' && | |
1634 | $field['data_type'] !== 'Boolean' | |
6a488035 TO |
1635 | ) |
1636 | ) { | |
1637 | $customOption = CRM_Core_BAO_CustomOption::getCustomOption($key, TRUE); | |
1638 | foreach ($customOption as $customID => $coValue) { | |
9d72cede EM |
1639 | if (strtolower(trim($coValue['label'])) == |
1640 | strtolower(trim($value)) | |
1641 | ) { | |
6a488035 TO |
1642 | $value = $coValue['value']; |
1643 | $valid = TRUE; | |
1644 | } | |
1645 | } | |
1646 | } | |
36769590 | 1647 | elseif ($field['data_type'] === 'Date') { |
31349d27 | 1648 | $valid = CRM_Utils_Rule::date($value); |
6a488035 TO |
1649 | } |
1650 | ||
1651 | if ($valid) { | |
1652 | $customValue[$fieldName] = $value; | |
1653 | } | |
1654 | } | |
1655 | } | |
1656 | } | |
1657 | ||
1658 | return $customValue; | |
1659 | } | |
1660 | ||
1661 | /** | |
100fef9d | 1662 | * Check the type of custom field type (eg: Used for Individual, Contribution, etc) |
6a488035 TO |
1663 | * this function is used to get the custom fields of a type (eg: Used for Individual, Contribution, etc ) |
1664 | * | |
6a0b768e TO |
1665 | * @param int $customFieldId |
1666 | * Custom field id. | |
1667 | * @param array $removeCustomFieldTypes | |
1668 | * Remove custom fields of a type eg: array("Individual") ;. | |
6a488035 | 1669 | * |
5c766a0b | 1670 | * @return bool |
a6c01b45 | 1671 | * false if it matches else true |
36769590 | 1672 | * |
1673 | * @throws \CRM_Core_Exception | |
6a488035 | 1674 | */ |
00be9182 | 1675 | public static function checkCustomField($customFieldId, &$removeCustomFieldTypes) { |
36769590 | 1676 | $query = 'SELECT cg.extends as extends |
6a488035 TO |
1677 | FROM civicrm_custom_group as cg, civicrm_custom_field as cf |
1678 | WHERE cg.id = cf.custom_group_id | |
36769590 | 1679 | AND cf.id =' . |
9d72cede | 1680 | CRM_Utils_Type::escape($customFieldId, 'Integer'); |
6a488035 TO |
1681 | |
1682 | $extends = CRM_Core_DAO::singleValueQuery($query); | |
1683 | ||
1684 | if (in_array($extends, $removeCustomFieldTypes)) { | |
1685 | return FALSE; | |
1686 | } | |
1687 | return TRUE; | |
1688 | } | |
1689 | ||
b5c2afd0 EM |
1690 | /** |
1691 | * @param $table | |
1692 | * | |
1693 | * @return string | |
1694 | * @throws Exception | |
1695 | */ | |
00be9182 | 1696 | public static function mapTableName($table) { |
6a488035 TO |
1697 | switch ($table) { |
1698 | case 'Contact': | |
1699 | case 'Individual': | |
1700 | case 'Household': | |
1701 | case 'Organization': | |
1702 | return 'civicrm_contact'; | |
1703 | ||
1704 | case 'Activity': | |
1705 | return 'civicrm_activity'; | |
1706 | ||
1707 | case 'Group': | |
1708 | return 'civicrm_group'; | |
1709 | ||
1710 | case 'Contribution': | |
1711 | return 'civicrm_contribution'; | |
1712 | ||
95974e8e DG |
1713 | case 'ContributionRecur': |
1714 | return 'civicrm_contribution_recur'; | |
1715 | ||
6a488035 TO |
1716 | case 'Relationship': |
1717 | return 'civicrm_relationship'; | |
1718 | ||
1719 | case 'Event': | |
1720 | return 'civicrm_event'; | |
1721 | ||
1722 | case 'Membership': | |
1723 | return 'civicrm_membership'; | |
1724 | ||
1725 | case 'Participant': | |
1726 | case 'ParticipantRole': | |
1727 | case 'ParticipantEventName': | |
1728 | case 'ParticipantEventType': | |
1729 | return 'civicrm_participant'; | |
1730 | ||
1731 | case 'Grant': | |
1732 | return 'civicrm_grant'; | |
1733 | ||
1734 | case 'Pledge': | |
1735 | return 'civicrm_pledge'; | |
1736 | ||
1737 | case 'Address': | |
1738 | return 'civicrm_address'; | |
1739 | ||
1740 | case 'Campaign': | |
1741 | return 'civicrm_campaign'; | |
1742 | ||
1743 | default: | |
1744 | $query = " | |
1745 | SELECT IF( EXISTS(SELECT name FROM civicrm_contact_type WHERE name like %1), 1, 0 )"; | |
be2fb01f | 1746 | $qParams = [1 => [$table, 'String']]; |
6a488035 TO |
1747 | $result = CRM_Core_DAO::singleValueQuery($query, $qParams); |
1748 | ||
1749 | if ($result) { | |
1750 | return 'civicrm_contact'; | |
1751 | } | |
1752 | else { | |
1753 | $extendObjs = CRM_Core_OptionGroup::values('cg_extend_objects', FALSE, FALSE, FALSE, NULL, 'name'); | |
1754 | if (array_key_exists($table, $extendObjs)) { | |
1755 | return $extendObjs[$table]; | |
1756 | } | |
36769590 | 1757 | throw new CRM_Core_Exception('Unknown error'); |
6a488035 TO |
1758 | } |
1759 | } | |
1760 | } | |
1761 | ||
b5c2afd0 EM |
1762 | /** |
1763 | * @param $group | |
aa965915 MWMC |
1764 | * |
1765 | * @throws \Exception | |
b5c2afd0 | 1766 | */ |
00be9182 | 1767 | public static function createTable($group) { |
be2fb01f | 1768 | $params = [ |
6a488035 TO |
1769 | 'name' => $group->table_name, |
1770 | 'is_multiple' => $group->is_multiple ? 1 : 0, | |
1771 | 'extends_name' => self::mapTableName($group->extends), | |
be2fb01f | 1772 | ]; |
6a488035 TO |
1773 | |
1774 | $tableParams = CRM_Core_BAO_CustomField::defaultCustomTableSchema($params); | |
1775 | ||
1776 | CRM_Core_BAO_SchemaHandler::createTable($tableParams); | |
1777 | } | |
1778 | ||
1779 | /** | |
aa965915 | 1780 | * Function returns formatted groupTree, so that form can be easily built in template |
6a488035 | 1781 | * |
6a0b768e | 1782 | * @param array $groupTree |
6a0b768e | 1783 | * @param int $groupCount |
aa965915 MWMC |
1784 | * Group count by default 1, but can vary for multiple value custom data. |
1785 | * @param \CRM_Core_Form $form | |
6a488035 | 1786 | * |
a6c01b45 | 1787 | * @return array |
aa965915 | 1788 | * @throws \CRM_Core_Exception |
6a488035 | 1789 | */ |
18b623d0 | 1790 | public static function formatGroupTree(&$groupTree, $groupCount = 1, &$form = NULL) { |
be2fb01f CW |
1791 | $formattedGroupTree = []; |
1792 | $uploadNames = $formValues = []; | |
fb56e964 | 1793 | |
29a1df58 | 1794 | // retrieve qf key from url |
1795 | $qfKey = CRM_Utils_Request::retrieve('qf', 'String'); | |
1796 | ||
1797 | // fetch submitted custom field values later use to set as a default values | |
1798 | if ($qfKey) { | |
792fce0b | 1799 | $submittedValues = Civi::cache('customData')->get($qfKey); |
fb56e964 | 1800 | } |
6a488035 TO |
1801 | |
1802 | foreach ($groupTree as $key => $value) { | |
1803 | if ($key === 'info') { | |
1804 | continue; | |
1805 | } | |
1806 | ||
1807 | // add group information | |
9c1bc317 CW |
1808 | $formattedGroupTree[$key]['name'] = $value['name'] ?? NULL; |
1809 | $formattedGroupTree[$key]['title'] = $value['title'] ?? NULL; | |
1810 | $formattedGroupTree[$key]['help_pre'] = $value['help_pre'] ?? NULL; | |
1811 | $formattedGroupTree[$key]['help_post'] = $value['help_post'] ?? NULL; | |
1812 | $formattedGroupTree[$key]['collapse_display'] = $value['collapse_display'] ?? NULL; | |
1813 | $formattedGroupTree[$key]['collapse_adv_display'] = $value['collapse_adv_display'] ?? NULL; | |
1814 | $formattedGroupTree[$key]['style'] = $value['style'] ?? NULL; | |
6a488035 TO |
1815 | |
1816 | // this params needed of bulding multiple values | |
9c1bc317 CW |
1817 | $formattedGroupTree[$key]['is_multiple'] = $value['is_multiple'] ?? NULL; |
1818 | $formattedGroupTree[$key]['extends'] = $value['extends'] ?? NULL; | |
1819 | $formattedGroupTree[$key]['extends_entity_column_id'] = $value['extends_entity_column_id'] ?? NULL; | |
1820 | $formattedGroupTree[$key]['extends_entity_column_value'] = $value['extends_entity_column_value'] ?? NULL; | |
1821 | $formattedGroupTree[$key]['subtype'] = $value['subtype'] ?? NULL; | |
1822 | $formattedGroupTree[$key]['max_multiple'] = $value['max_multiple'] ?? NULL; | |
6a488035 TO |
1823 | |
1824 | // add field information | |
1825 | foreach ($value['fields'] as $k => $properties) { | |
1826 | $properties['element_name'] = "custom_{$k}_-{$groupCount}"; | |
9d72cede | 1827 | if (isset($properties['customValue']) && |
fb56e964 | 1828 | !CRM_Utils_System::isNull($properties['customValue']) && |
1829 | !isset($properties['element_value']) | |
9d72cede | 1830 | ) { |
6a488035 TO |
1831 | if (isset($properties['customValue'][$groupCount])) { |
1832 | $properties['element_name'] = "custom_{$k}_{$properties['customValue'][$groupCount]['id']}"; | |
1833 | $formattedGroupTree[$key]['table_id'] = $properties['customValue'][$groupCount]['id']; | |
36769590 | 1834 | if ($properties['data_type'] === 'File') { |
6a488035 TO |
1835 | $properties['element_value'] = $properties['customValue'][$groupCount]; |
1836 | $uploadNames[] = $properties['element_name']; | |
1837 | } | |
1838 | else { | |
1839 | $properties['element_value'] = $properties['customValue'][$groupCount]['data']; | |
1840 | } | |
1841 | } | |
1842 | } | |
16806c95 | 1843 | if ($value = CRM_Utils_Request::retrieve($properties['element_name'], 'String', $form, FALSE, NULL, 'POST')) { |
1844 | $formValues[$properties['element_name']] = $value; | |
1845 | } | |
1846 | elseif (isset($submittedValues[$properties['element_name']])) { | |
1847 | $properties['element_value'] = $submittedValues[$properties['element_name']]; | |
1848 | } | |
6a488035 TO |
1849 | unset($properties['customValue']); |
1850 | $formattedGroupTree[$key]['fields'][$k] = $properties; | |
1851 | } | |
1852 | } | |
1853 | ||
1854 | if ($form) { | |
fb56e964 | 1855 | if (count($formValues)) { |
29a1df58 | 1856 | $qf = $form->get('qfKey'); |
1857 | $form->assign('qfKey', $qf); | |
792fce0b | 1858 | Civi::cache('customData')->set($qf, $formValues); |
fb56e964 | 1859 | } |
1860 | ||
6a488035 TO |
1861 | // hack for field type File |
1862 | $formUploadNames = $form->get('uploadNames'); | |
1863 | if (is_array($formUploadNames)) { | |
1864 | $uploadNames = array_unique(array_merge($formUploadNames, $uploadNames)); | |
1865 | } | |
1866 | ||
1867 | $form->set('uploadNames', $uploadNames); | |
1868 | } | |
1869 | ||
1870 | return $formattedGroupTree; | |
1871 | } | |
1872 | ||
1873 | /** | |
fe482240 | 1874 | * Build custom data view. |
77b97be7 | 1875 | * |
24eba103 | 1876 | * @param CRM_Core_Form|CRM_Core_Page $form |
6a0b768e TO |
1877 | * Page object. |
1878 | * @param array $groupTree | |
6a0b768e TO |
1879 | * @param bool $returnCount |
1880 | * True if customValue count needs to be returned. | |
100fef9d | 1881 | * @param int $gID |
77b97be7 | 1882 | * @param null $prefix |
100fef9d | 1883 | * @param int $customValueId |
080d719e | 1884 | * @param int $entityId |
77b97be7 EM |
1885 | * |
1886 | * @return array|int | |
5d7c2bd2 | 1887 | * @throws \CRM_Core_Exception |
6a488035 | 1888 | */ |
080d719e | 1889 | public static function buildCustomDataView(&$form, &$groupTree, $returnCount = FALSE, $gID = NULL, $prefix = NULL, $customValueId = NULL, $entityId = NULL) { |
be2fb01f | 1890 | $details = []; |
6a488035 TO |
1891 | foreach ($groupTree as $key => $group) { |
1892 | if ($key === 'info') { | |
1893 | continue; | |
1894 | } | |
1895 | ||
1896 | foreach ($group['fields'] as $k => $properties) { | |
1897 | $groupID = $group['id']; | |
1898 | if (!empty($properties['customValue'])) { | |
1899 | foreach ($properties['customValue'] as $values) { | |
e41f4660 PJ |
1900 | if (!empty($customValueId) && $customValueId != $values['id']) { |
1901 | continue; | |
1902 | } | |
9c1bc317 CW |
1903 | $details[$groupID][$values['id']]['title'] = $group['title'] ?? NULL; |
1904 | $details[$groupID][$values['id']]['name'] = $group['name'] ?? NULL; | |
1905 | $details[$groupID][$values['id']]['help_pre'] = $group['help_pre'] ?? NULL; | |
1906 | $details[$groupID][$values['id']]['help_post'] = $group['help_post'] ?? NULL; | |
1907 | $details[$groupID][$values['id']]['collapse_display'] = $group['collapse_display'] ?? NULL; | |
1908 | $details[$groupID][$values['id']]['collapse_adv_display'] = $group['collapse_adv_display'] ?? NULL; | |
1909 | $details[$groupID][$values['id']]['style'] = $group['style'] ?? NULL; | |
be2fb01f | 1910 | $details[$groupID][$values['id']]['fields'][$k] = [ |
6b409353 CW |
1911 | 'field_title' => $properties['label'] ?? NULL, |
1912 | 'field_type' => $properties['html_type'] ?? NULL, | |
1913 | 'field_data_type' => $properties['data_type'] ?? NULL, | |
080d719e | 1914 | 'field_value' => CRM_Core_BAO_CustomField::displayValue($values['data'], $properties['id'], $entityId), |
6b409353 | 1915 | 'options_per_line' => $properties['options_per_line'] ?? NULL, |
be2fb01f | 1916 | ]; |
7dcdb6fa CW |
1917 | // editable = whether this set contains any non-read-only fields |
1918 | if (!isset($details[$groupID][$values['id']]['editable'])) { | |
1919 | $details[$groupID][$values['id']]['editable'] = FALSE; | |
1920 | } | |
1921 | if (empty($properties['is_view'])) { | |
1922 | $details[$groupID][$values['id']]['editable'] = TRUE; | |
1923 | } | |
6a488035 | 1924 | // also return contact reference contact id if user has view all or edit all contacts perm |
9d72cede EM |
1925 | if ((CRM_Core_Permission::check('view all contacts') || |
1926 | CRM_Core_Permission::check('edit all contacts')) | |
1927 | && | |
1928 | $details[$groupID][$values['id']]['fields'][$k]['field_data_type'] == | |
1929 | 'ContactReference' | |
6a488035 | 1930 | ) { |
9c1bc317 | 1931 | $details[$groupID][$values['id']]['fields'][$k]['contact_ref_id'] = $values['data'] ?? NULL; |
6a488035 TO |
1932 | } |
1933 | } | |
1934 | } | |
1935 | else { | |
9c1bc317 CW |
1936 | $details[$groupID][0]['title'] = $group['title'] ?? NULL; |
1937 | $details[$groupID][0]['name'] = $group['name'] ?? NULL; | |
1938 | $details[$groupID][0]['help_pre'] = $group['help_pre'] ?? NULL; | |
1939 | $details[$groupID][0]['help_post'] = $group['help_post'] ?? NULL; | |
1940 | $details[$groupID][0]['collapse_display'] = $group['collapse_display'] ?? NULL; | |
1941 | $details[$groupID][0]['collapse_adv_display'] = $group['collapse_adv_display'] ?? NULL; | |
1942 | $details[$groupID][0]['style'] = $group['style'] ?? NULL; | |
be2fb01f | 1943 | $details[$groupID][0]['fields'][$k] = ['field_title' => CRM_Utils_Array::value('label', $properties)]; |
6a488035 TO |
1944 | } |
1945 | } | |
1946 | } | |
1947 | ||
1948 | if ($returnCount) { | |
aa965915 MWMC |
1949 | // return a single value count if group id is passed to function |
1950 | // else return a groupId and count mapped array | |
9d72cede | 1951 | if (!empty($gID)) { |
6a488035 TO |
1952 | return count($details[$gID]); |
1953 | } | |
1954 | else { | |
be2fb01f | 1955 | $countValue = []; |
9d72cede | 1956 | foreach ($details as $key => $value) { |
6a488035 TO |
1957 | $countValue[$key] = count($details[$key]); |
1958 | } | |
1959 | return $countValue; | |
1960 | } | |
1961 | } | |
1962 | else { | |
1963 | $form->assign_by_ref("{$prefix}viewCustomData", $details); | |
1964 | return $details; | |
1965 | } | |
1966 | } | |
1967 | ||
6a488035 TO |
1968 | /** |
1969 | * Get the custom group titles by custom field ids. | |
1970 | * | |
6a0b768e TO |
1971 | * @param array $fieldIds |
1972 | * Array of custom field ids. | |
6a488035 | 1973 | * |
a1a2a83d | 1974 | * @return array|NULL |
a6c01b45 | 1975 | * array consisting of groups and fields labels with ids. |
6a488035 TO |
1976 | */ |
1977 | public static function getGroupTitles($fieldIds) { | |
1978 | if (!is_array($fieldIds) && empty($fieldIds)) { | |
a1a2a83d | 1979 | return NULL; |
6a488035 TO |
1980 | } |
1981 | ||
be2fb01f | 1982 | $groupLabels = []; |
6a488035 TO |
1983 | $fIds = "(" . implode(',', $fieldIds) . ")"; |
1984 | ||
1985 | $query = " | |
1986 | SELECT civicrm_custom_group.id as groupID, civicrm_custom_group.title as groupTitle, | |
1987 | civicrm_custom_field.label as fieldLabel, civicrm_custom_field.id as fieldID | |
1988 | FROM civicrm_custom_group, civicrm_custom_field | |
1989 | WHERE civicrm_custom_group.id = civicrm_custom_field.custom_group_id | |
1990 | AND civicrm_custom_field.id IN {$fIds}"; | |
1991 | ||
1992 | $dao = CRM_Core_DAO::executeQuery($query); | |
1993 | while ($dao->fetch()) { | |
be2fb01f | 1994 | $groupLabels[$dao->fieldID] = [ |
6a488035 TO |
1995 | 'fieldID' => $dao->fieldID, |
1996 | 'fieldLabel' => $dao->fieldLabel, | |
1997 | 'groupID' => $dao->groupID, | |
1998 | 'groupTitle' => $dao->groupTitle, | |
be2fb01f | 1999 | ]; |
6a488035 TO |
2000 | } |
2001 | ||
2002 | return $groupLabels; | |
2003 | } | |
2004 | ||
00be9182 | 2005 | public static function dropAllTables() { |
6a488035 TO |
2006 | $query = "SELECT table_name FROM civicrm_custom_group"; |
2007 | $dao = CRM_Core_DAO::executeQuery($query); | |
2008 | ||
2009 | while ($dao->fetch()) { | |
2010 | $query = "DROP TABLE IF EXISTS {$dao->table_name}"; | |
2011 | CRM_Core_DAO::executeQuery($query); | |
2012 | } | |
2013 | } | |
2014 | ||
2015 | /** | |
2016 | * Check whether custom group is empty or not. | |
2017 | * | |
6a0b768e TO |
2018 | * @param int $gID |
2019 | * Custom group id. | |
6a488035 | 2020 | * |
a1a2a83d | 2021 | * @return bool|NULL |
a6c01b45 | 2022 | * true if empty otherwise false. |
6a488035 | 2023 | */ |
00be9182 | 2024 | public static function isGroupEmpty($gID) { |
6a488035 | 2025 | if (!$gID) { |
a1a2a83d | 2026 | return NULL; |
6a488035 TO |
2027 | } |
2028 | ||
2029 | $tableName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', | |
9d72cede EM |
2030 | $gID, |
2031 | 'table_name' | |
2032 | ); | |
6a488035 TO |
2033 | |
2034 | $query = "SELECT count(id) FROM {$tableName} WHERE id IS NOT NULL LIMIT 1"; | |
2035 | $value = CRM_Core_DAO::singleValueQuery($query); | |
2036 | ||
2037 | if (empty($value)) { | |
2038 | return TRUE; | |
2039 | } | |
2040 | ||
2041 | return FALSE; | |
2042 | } | |
2043 | ||
2044 | /** | |
2045 | * Get the list of types for objects that a custom group extends to. | |
2046 | * | |
6a0b768e TO |
2047 | * @param array $types |
2048 | * Var which should have the list appended. | |
6a488035 | 2049 | * |
a6c01b45 | 2050 | * @return array |
16b10e64 | 2051 | * Array of types. |
aa965915 | 2052 | * @throws \Exception |
6a488035 | 2053 | */ |
be2fb01f CW |
2054 | public static function getExtendedObjectTypes(&$types = []) { |
2055 | static $flag = FALSE, $objTypes = []; | |
6a488035 TO |
2056 | |
2057 | if (!$flag) { | |
be2fb01f | 2058 | $extendObjs = []; |
fffc190b | 2059 | CRM_Core_OptionValue::getValues(['name' => 'cg_extend_objects'], $extendObjs, 'weight', TRUE); |
6a488035 TO |
2060 | |
2061 | foreach ($extendObjs as $ovId => $ovValues) { | |
2062 | if ($ovValues['description']) { | |
2063 | // description is expected to be a callback func to subtypes | |
2064 | list($callback, $args) = explode(';', trim($ovValues['description'])); | |
2065 | ||
0e6e8724 | 2066 | if (empty($args)) { |
be2fb01f | 2067 | $args = []; |
6a488035 TO |
2068 | } |
2069 | ||
2070 | if (!is_array($args)) { | |
36769590 | 2071 | throw new CRM_Core_Exception('Arg is not of type array'); |
6a488035 TO |
2072 | } |
2073 | ||
2074 | list($className) = explode('::', $callback); | |
aa965915 | 2075 | require_once str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php'; |
6a488035 TO |
2076 | |
2077 | $objTypes[$ovValues['value']] = call_user_func_array($callback, $args); | |
2078 | } | |
2079 | } | |
2080 | $flag = TRUE; | |
2081 | } | |
2082 | ||
2083 | $types = array_merge($types, $objTypes); | |
2084 | return $objTypes; | |
2085 | } | |
2086 | ||
b5c2afd0 | 2087 | /** |
100fef9d CW |
2088 | * @param int $customGroupId |
2089 | * @param int $entityId | |
b5c2afd0 EM |
2090 | * |
2091 | * @return bool | |
2092 | */ | |
00be9182 | 2093 | public static function hasReachedMaxLimit($customGroupId, $entityId) { |
aa965915 | 2094 | // check whether the group is multiple |
6a488035 | 2095 | $isMultiple = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $customGroupId, 'is_multiple'); |
63d76404 | 2096 | $isMultiple = (bool) $isMultiple; |
6a488035 TO |
2097 | $hasReachedMax = FALSE; |
2098 | if ($isMultiple && | |
9d72cede EM |
2099 | ($maxMultiple = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $customGroupId, 'max_multiple')) |
2100 | ) { | |
6a488035 TO |
2101 | if (!$maxMultiple) { |
2102 | $hasReachedMax = FALSE; | |
9d72cede EM |
2103 | } |
2104 | else { | |
6a488035 | 2105 | $tableName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $customGroupId, 'table_name'); |
aa965915 | 2106 | // count the number of entries for a entity |
6a488035 | 2107 | $sql = "SELECT COUNT(id) FROM {$tableName} WHERE entity_id = %1"; |
be2fb01f | 2108 | $params = [1 => [$entityId, 'Integer']]; |
6a488035 TO |
2109 | $count = CRM_Core_DAO::singleValueQuery($sql, $params); |
2110 | ||
2111 | if ($count >= $maxMultiple) { | |
2112 | $hasReachedMax = TRUE; | |
2113 | } | |
2114 | } | |
2115 | } | |
2116 | return $hasReachedMax; | |
2117 | } | |
2118 | ||
b5c2afd0 EM |
2119 | /** |
2120 | * @return array | |
2121 | */ | |
00be9182 | 2122 | public static function getMultipleFieldGroup() { |
be2fb01f | 2123 | $multipleGroup = []; |
9ff5f6c0 | 2124 | $dao = new CRM_Core_DAO_CustomGroup(); |
9d72cede | 2125 | $dao->is_multiple = 1; |
209a3cc3 | 2126 | $dao->is_active = 1; |
9ff5f6c0 | 2127 | $dao->find(); |
9d72cede | 2128 | while ($dao->fetch()) { |
9ff5f6c0 N |
2129 | $multipleGroup[$dao->id] = $dao->title; |
2130 | } | |
2131 | return $multipleGroup; | |
2132 | } | |
96025800 | 2133 | |
b2ccd9fe | 2134 | /** |
2135 | * Build the metadata tree for the custom group. | |
2136 | * | |
2137 | * @param string $entityType | |
2138 | * @param array $toReturn | |
2139 | * @param array $subTypes | |
2140 | * @param string $queryString | |
2141 | * @param array $params | |
2142 | * @param string $subType | |
2143 | * | |
2144 | * @return array | |
2145 | * @throws \CRM_Core_Exception | |
2146 | */ | |
2147 | private static function buildGroupTree($entityType, $toReturn, $subTypes, $queryString, $params, $subType) { | |
2148 | $groupTree = $multipleFieldGroups = []; | |
2149 | $crmDAO = CRM_Core_DAO::executeQuery($queryString, $params); | |
2150 | $customValueTables = []; | |
2151 | ||
2152 | // process records | |
2153 | while ($crmDAO->fetch()) { | |
2154 | // get the id's | |
2155 | $groupID = $crmDAO->civicrm_custom_group_id; | |
2156 | $fieldId = $crmDAO->civicrm_custom_field_id; | |
2157 | if ($crmDAO->civicrm_custom_group_is_multiple) { | |
2158 | $multipleFieldGroups[$groupID] = $crmDAO->civicrm_custom_group_table_name; | |
2159 | } | |
2160 | // create an array for groups if it does not exist | |
2161 | if (!array_key_exists($groupID, $groupTree)) { | |
2162 | $groupTree[$groupID] = []; | |
2163 | $groupTree[$groupID]['id'] = $groupID; | |
2164 | ||
2165 | // populate the group information | |
2166 | foreach ($toReturn['custom_group'] as $fieldName) { | |
2167 | $fullFieldName = "civicrm_custom_group_$fieldName"; | |
2168 | if ($fieldName == 'id' || | |
2169 | is_null($crmDAO->$fullFieldName) | |
2170 | ) { | |
2171 | continue; | |
2172 | } | |
2173 | // CRM-5507 | |
2174 | // This is an old bit of code - per the CRM number & probably does not work reliably if | |
2175 | // that one contact sub-type exists. | |
2176 | if ($fieldName == 'extends_entity_column_value' && !empty($subTypes[0])) { | |
2177 | $groupTree[$groupID]['subtype'] = self::validateSubTypeByEntity($entityType, $subType); | |
2178 | } | |
2179 | $groupTree[$groupID][$fieldName] = $crmDAO->$fullFieldName; | |
2180 | } | |
2181 | $groupTree[$groupID]['fields'] = []; | |
2182 | ||
2183 | $customValueTables[$crmDAO->civicrm_custom_group_table_name] = []; | |
2184 | } | |
2185 | ||
2186 | // add the fields now (note - the query row will always contain a field) | |
2187 | // we only reset this once, since multiple values come is as multiple rows | |
2188 | if (!array_key_exists($fieldId, $groupTree[$groupID]['fields'])) { | |
2189 | $groupTree[$groupID]['fields'][$fieldId] = []; | |
2190 | } | |
2191 | ||
2192 | $customValueTables[$crmDAO->civicrm_custom_group_table_name][$crmDAO->civicrm_custom_field_column_name] = 1; | |
2193 | $groupTree[$groupID]['fields'][$fieldId]['id'] = $fieldId; | |
2194 | // populate information for a custom field | |
2195 | foreach ($toReturn['custom_field'] as $fieldName) { | |
2196 | $fullFieldName = "civicrm_custom_field_$fieldName"; | |
2197 | if ($fieldName == 'id' || | |
2198 | is_null($crmDAO->$fullFieldName) | |
2199 | ) { | |
2200 | continue; | |
2201 | } | |
2202 | $groupTree[$groupID]['fields'][$fieldId][$fieldName] = $crmDAO->$fullFieldName; | |
2203 | } | |
2204 | } | |
2205 | ||
2206 | if (!empty($customValueTables)) { | |
2207 | $groupTree['info'] = ['tables' => $customValueTables]; | |
2208 | } | |
2209 | return [$multipleFieldGroups, $groupTree]; | |
2210 | } | |
2211 | ||
9d72cede | 2212 | } |