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