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