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