Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
18 | /** | |
19 | * form to process actions on the set aspect of Custom Data | |
20 | */ | |
21 | class CRM_Custom_Form_Group extends CRM_Core_Form { | |
22 | ||
23 | /** | |
fe482240 | 24 | * The set id saved to the session for an update. |
6a488035 TO |
25 | * |
26 | * @var int | |
6a488035 TO |
27 | */ |
28 | protected $_id; | |
29 | ||
30 | /** | |
fe482240 | 31 | * set is empty or not. |
6a488035 TO |
32 | * |
33 | * @var bool | |
6a488035 TO |
34 | */ |
35 | protected $_isGroupEmpty = TRUE; | |
36 | ||
37 | /** | |
fe482240 | 38 | * Array of existing subtypes set for a custom set. |
6a488035 TO |
39 | * |
40 | * @var array | |
6a488035 | 41 | */ |
be2fb01f | 42 | protected $_subtypes = []; |
6a488035 | 43 | |
6a488035 | 44 | /** |
fe482240 | 45 | * Set variables up before form is built. |
6a488035 | 46 | * |
6a488035 TO |
47 | * |
48 | * @return void | |
6a488035 TO |
49 | */ |
50 | public function preProcess() { | |
2af06525 CW |
51 | Civi::resources()->addScriptFile('civicrm', 'js/jquery/jquery.crmIconPicker.js'); |
52 | ||
6a488035 TO |
53 | // current set id |
54 | $this->_id = $this->get('id'); | |
55 | ||
148c89c8 | 56 | if ($this->_id && $isReserved = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $this->_id, 'is_reserved', 'id')) { |
fbdcf459 | 57 | CRM_Core_Error::statusBounce("You cannot edit the settings of a reserved custom field-set."); |
d06700a7 | 58 | } |
6a488035 TO |
59 | // setting title for html page |
60 | if ($this->_action == CRM_Core_Action::UPDATE) { | |
61 | $title = CRM_Core_BAO_CustomGroup::getTitle($this->_id); | |
be2fb01f | 62 | CRM_Utils_System::setTitle(ts('Edit %1', [1 => $title])); |
6a488035 TO |
63 | } |
64 | elseif ($this->_action == CRM_Core_Action::VIEW) { | |
65 | $title = CRM_Core_BAO_CustomGroup::getTitle($this->_id); | |
be2fb01f | 66 | CRM_Utils_System::setTitle(ts('Preview %1', [1 => $title])); |
6a488035 TO |
67 | } |
68 | else { | |
69 | CRM_Utils_System::setTitle(ts('New Custom Field Set')); | |
70 | } | |
71 | ||
72 | if (isset($this->_id)) { | |
be2fb01f | 73 | $params = ['id' => $this->_id]; |
6a488035 TO |
74 | CRM_Core_BAO_CustomGroup::retrieve($params, $this->_defaults); |
75 | ||
9c1bc317 | 76 | $subExtends = $this->_defaults['extends_entity_column_value'] ?? NULL; |
6a488035 TO |
77 | if (!empty($subExtends)) { |
78 | $this->_subtypes = explode(CRM_Core_DAO::VALUE_SEPARATOR, substr($subExtends, 1, -1)); | |
79 | } | |
80 | } | |
81 | } | |
82 | ||
83 | /** | |
fe482240 | 84 | * Global form rule. |
6a488035 | 85 | * |
c4ca4892 TO |
86 | * @param array $fields |
87 | * The input form values. | |
88 | * @param array $files | |
89 | * The uploaded files if any. | |
dd244018 EM |
90 | * @param $self |
91 | * | |
6a488035 | 92 | * |
72b3a70c CW |
93 | * @return bool|array |
94 | * true if no errors, else array of errors | |
6a488035 | 95 | */ |
00be9182 | 96 | public static function formRule($fields, $files, $self) { |
be2fb01f | 97 | $errors = []; |
6a488035 TO |
98 | |
99 | //validate group title as well as name. | |
353ffa53 TO |
100 | $title = $fields['title']; |
101 | $name = CRM_Utils_String::munge($title, '_', 64); | |
9d5dc965 | 102 | $query = 'select count(*) from civicrm_custom_group where ( name like %1) and id != %2'; |
be2fb01f CW |
103 | $grpCnt = CRM_Core_DAO::singleValueQuery($query, [ |
104 | 1 => [$name, 'String'], | |
105 | 2 => [(int) $self->_id, 'Integer'], | |
106 | ]); | |
6a488035 | 107 | if ($grpCnt) { |
be2fb01f | 108 | $errors['title'] = ts('Custom group \'%1\' already exists in Database.', [1 => $title]); |
6a488035 TO |
109 | } |
110 | ||
a7488080 | 111 | if (!empty($fields['extends'][1])) { |
6a488035 TO |
112 | if (in_array('', $fields['extends'][1]) && count($fields['extends'][1]) > 1) { |
113 | $errors['extends'] = ts("Cannot combine other option with 'Any'."); | |
114 | } | |
115 | } | |
116 | ||
117 | if (empty($fields['extends'][0])) { | |
118 | $errors['extends'] = ts("You need to select the type of record that this set of custom fields is applicable for."); | |
119 | } | |
120 | ||
be2fb01f | 121 | $extends = ['Activity', 'Relationship', 'Group', 'Contribution', 'Membership', 'Event', 'Participant']; |
6a488035 TO |
122 | if (in_array($fields['extends'][0], $extends) && $fields['style'] == 'Tab') { |
123 | $errors['style'] = ts("Display Style should be Inline for this Class"); | |
124 | $self->assign('showStyle', TRUE); | |
125 | } | |
126 | ||
a7488080 | 127 | if (!empty($fields['is_multiple'])) { |
e8646905 | 128 | $self->assign('showMultiple', TRUE); |
6a488035 TO |
129 | } |
130 | ||
b05a0fb6 | 131 | if (empty($fields['is_multiple']) && $fields['style'] == 'Tab with table') { |
e232751e | 132 | $errors['style'] = ts("Display Style 'Tab with table' is only supported for multiple-record custom field sets."); |
b05a0fb6 PJ |
133 | } |
134 | ||
6a488035 TO |
135 | //checks the given custom set doesnot start with digit |
136 | $title = $fields['title']; | |
137 | if (!empty($title)) { | |
138 | // gives the ascii value | |
139 | $asciiValue = ord($title{0}); | |
140 | if ($asciiValue >= 48 && $asciiValue <= 57) { | |
5ab78070 | 141 | $errors['title'] = ts("Name cannot not start with a digit"); |
6a488035 TO |
142 | } |
143 | } | |
144 | ||
145 | return empty($errors) ? TRUE : $errors; | |
146 | } | |
147 | ||
148 | /** | |
dc195289 | 149 | * add the rules (mainly global rules) for form. |
6a488035 TO |
150 | * All local rules are added near the element |
151 | * | |
6a488035 TO |
152 | * |
153 | * @return void | |
6a488035 TO |
154 | * @see valid_date |
155 | */ | |
00be9182 | 156 | public function addRules() { |
be2fb01f | 157 | $this->addFormRule(['CRM_Custom_Form_Group', 'formRule'], $this); |
6a488035 TO |
158 | } |
159 | ||
160 | /** | |
fe482240 | 161 | * Build the form object. |
6a488035 | 162 | * |
6a488035 TO |
163 | * |
164 | * @return void | |
6a488035 TO |
165 | */ |
166 | public function buildQuickForm() { | |
167 | $this->applyFilter('__ALL__', 'trim'); | |
168 | ||
169 | $attributes = CRM_Core_DAO::getAttribute('CRM_Core_DAO_CustomGroup'); | |
170 | ||
171 | //title | |
172 | $this->add('text', 'title', ts('Set Name'), $attributes['title'], TRUE); | |
173 | ||
174 | //Fix for code alignment, CRM-3058 | |
be2fb01f | 175 | $contactTypes = ['Contact', 'Individual', 'Household', 'Organization']; |
6a488035 TO |
176 | $this->assign('contactTypes', json_encode($contactTypes)); |
177 | ||
be2fb01f CW |
178 | $sel1 = ["" => ts("- select -")] + CRM_Core_SelectValues::customGroupExtends(); |
179 | $sel2 = []; | |
6a488035 TO |
180 | $activityType = CRM_Core_PseudoConstant::activityType(FALSE, TRUE, FALSE, 'label', TRUE); |
181 | ||
353ffa53 TO |
182 | $eventType = CRM_Core_OptionGroup::values('event_type'); |
183 | $grantType = CRM_Core_OptionGroup::values('grant_type'); | |
184 | $campaignTypes = CRM_Campaign_PseudoConstant::campaignType(); | |
185 | $membershipType = CRM_Member_BAO_MembershipType::getMembershipTypes(FALSE); | |
6a488035 | 186 | $participantRole = CRM_Core_OptionGroup::values('participant_role'); |
6a488035 TO |
187 | |
188 | ksort($sel1); | |
189 | asort($activityType); | |
190 | asort($eventType); | |
191 | asort($grantType); | |
192 | asort($membershipType); | |
193 | asort($participantRole); | |
6a488035 TO |
194 | |
195 | $sel2['Event'] = $eventType; | |
196 | $sel2['Grant'] = $grantType; | |
197 | $sel2['Activity'] = $activityType; | |
198 | $sel2['Campaign'] = $campaignTypes; | |
199 | $sel2['Membership'] = $membershipType; | |
200 | $sel2['ParticipantRole'] = $participantRole; | |
201 | $sel2['ParticipantEventName'] = CRM_Event_PseudoConstant::event(NULL, FALSE, "( is_template IS NULL OR is_template != 1 )"); | |
202 | $sel2['ParticipantEventType'] = $eventType; | |
879db7b0 | 203 | $sel2['Contribution'] = CRM_Contribute_PseudoConstant::financialType(); |
a9c1ff43 | 204 | $sel2['Relationship'] = self::getRelationshipTypes(); |
6a488035 TO |
205 | |
206 | $sel2['Individual'] = CRM_Contact_BAO_ContactType::subTypePairs('Individual', FALSE, NULL); | |
207 | $sel2['Household'] = CRM_Contact_BAO_ContactType::subTypePairs('Household', FALSE, NULL); | |
208 | $sel2['Organization'] = CRM_Contact_BAO_ContactType::subTypePairs('Organization', FALSE, NULL); | |
209 | ||
210 | CRM_Core_BAO_CustomGroup::getExtendedObjectTypes($sel2); | |
211 | ||
212 | foreach ($sel2 as $main => $sub) { | |
213 | if (!empty($sel2[$main])) { | |
be2fb01f | 214 | $sel2[$main] = [ |
a9c1ff43 | 215 | '' => ts("- Any -"), |
be2fb01f | 216 | ] + $sel2[$main]; |
6a488035 TO |
217 | } |
218 | } | |
219 | ||
220 | $cSubTypes = CRM_Core_Component::contactSubTypes(); | |
221 | ||
222 | if (!empty($cSubTypes)) { | |
be2fb01f | 223 | $contactSubTypes = []; |
6a488035 TO |
224 | foreach ($cSubTypes as $key => $value) { |
225 | $contactSubTypes[$key] = $key; | |
226 | } | |
be2fb01f | 227 | $sel2['Contact'] = [ |
b6b6d6cc | 228 | "" => ("- Any -"), |
be2fb01f | 229 | ] + $contactSubTypes; |
6a488035 TO |
230 | } |
231 | else { | |
232 | if (!isset($this->_id)) { | |
233 | $formName = 'document.forms.' . $this->_name; | |
234 | ||
235 | $js = "<script type='text/javascript'>\n"; | |
236 | $js .= "{$formName}['extends_1'].style.display = 'none';\n"; | |
237 | $js .= "</script>"; | |
238 | $this->assign('initHideBlocks', $js); | |
239 | } | |
240 | } | |
241 | ||
242 | $sel = &$this->add('hierselect', | |
243 | 'extends', | |
244 | ts('Used For'), | |
be2fb01f | 245 | [ |
6a488035 | 246 | 'name' => 'extends[0]', |
21dfd5f5 | 247 | 'style' => 'vertical-align: top;', |
be2fb01f | 248 | ], |
6a488035 TO |
249 | TRUE |
250 | ); | |
be2fb01f | 251 | $sel->setOptions([$sel1, $sel2]); |
6a488035 TO |
252 | if (is_a($sel->_elements[1], 'HTML_QuickForm_select')) { |
253 | // make second selector a multi-select - | |
254 | $sel->_elements[1]->setMultiple(TRUE); | |
255 | $sel->_elements[1]->setSize(5); | |
256 | } | |
257 | if ($this->_action == CRM_Core_Action::UPDATE) { | |
9c1bc317 | 258 | $subName = $this->_defaults['extends_entity_column_id'] ?? NULL; |
6a488035 TO |
259 | if ($this->_defaults['extends'] == 'Participant') { |
260 | if ($subName == 1) { | |
261 | $this->_defaults['extends'] = 'ParticipantRole'; | |
262 | } | |
263 | elseif ($subName == 2) { | |
264 | $this->_defaults['extends'] = 'ParticipantEventName'; | |
265 | } | |
266 | elseif ($subName == 3) { | |
267 | $this->_defaults['extends'] = 'ParticipantEventType'; | |
268 | } | |
269 | } | |
270 | ||
271 | //allow to edit settings if custom set is empty CRM-5258 | |
272 | $this->_isGroupEmpty = CRM_Core_BAO_CustomGroup::isGroupEmpty($this->_id); | |
273 | if (!$this->_isGroupEmpty) { | |
274 | if (!empty($this->_subtypes)) { | |
275 | // we want to allow adding / updating subtypes for this case, | |
276 | // and therefore freeze the first selector only. | |
277 | $sel->_elements[0]->freeze(); | |
278 | } | |
279 | else { | |
280 | // freeze both the selectors | |
281 | $sel->freeze(); | |
282 | } | |
283 | } | |
284 | $this->assign('isCustomGroupEmpty', $this->_isGroupEmpty); | |
285 | $this->assign('gid', $this->_id); | |
286 | } | |
287 | $this->assign('defaultSubtypes', json_encode($this->_subtypes)); | |
288 | ||
289 | // help text | |
5d51a2f9 CW |
290 | $this->add('wysiwyg', 'help_pre', ts('Pre-form Help'), $attributes['help_pre']); |
291 | $this->add('wysiwyg', 'help_post', ts('Post-form Help'), $attributes['help_post']); | |
6a488035 TO |
292 | |
293 | // weight | |
f20d2b0d | 294 | $this->add('number', 'weight', ts('Order'), $attributes['weight'], TRUE); |
6a488035 TO |
295 | $this->addRule('weight', ts('is a numeric field'), 'numeric'); |
296 | ||
297 | // display style | |
298 | $this->add('select', 'style', ts('Display Style'), CRM_Core_SelectValues::customGroupStyle()); | |
299 | ||
2af06525 CW |
300 | $this->add('text', 'icon', ts('Tab icon'), ['class' => 'crm-icon-picker', 'allowClear' => TRUE]); |
301 | ||
6a488035 | 302 | // is this set collapsed or expanded ? |
cbaf6620 | 303 | $this->addElement('advcheckbox', 'collapse_display', ts('Collapse this set on initial display')); |
6a488035 TO |
304 | |
305 | // is this set collapsed or expanded ? in advanced search | |
cbaf6620 | 306 | $this->addElement('advcheckbox', 'collapse_adv_display', ts('Collapse this set in Advanced Search')); |
6a488035 TO |
307 | |
308 | // is this set active ? | |
cbaf6620 | 309 | $this->addElement('advcheckbox', 'is_active', ts('Is this Custom Data Set active?')); |
6a488035 | 310 | |
460931ca JP |
311 | //Is this set visible on public pages? |
312 | $this->addElement('advcheckbox', 'is_public', ts('Is this Custom Data Set public?')); | |
313 | ||
6a488035 | 314 | // does this set have multiple record? |
cbaf6620 | 315 | $multiple = $this->addElement('advcheckbox', 'is_multiple', |
d9aa50a7 | 316 | ts('Does this Custom Field Set allow multiple records?'), NULL); |
6a488035 TO |
317 | |
318 | // $min_multiple = $this->add('text', 'min_multiple', ts('Minimum number of multiple records'), $attributes['min_multiple'] ); | |
319 | // $this->addRule('min_multiple', ts('is a numeric field') , 'numeric'); | |
320 | ||
321 | $max_multiple = $this->add('text', 'max_multiple', ts('Maximum number of multiple records'), $attributes['max_multiple']); | |
322 | $this->addRule('max_multiple', ts('is a numeric field'), 'numeric'); | |
323 | ||
324 | //allow to edit settings if custom set is empty CRM-5258 | |
325 | $this->assign('isGroupEmpty', $this->_isGroupEmpty); | |
326 | if (!$this->_isGroupEmpty) { | |
327 | $multiple->freeze(); | |
328 | //$min_multiple->freeze(); | |
329 | $max_multiple->freeze(); | |
330 | } | |
331 | ||
332 | $this->assign('showStyle', FALSE); | |
333 | $this->assign('showMultiple', FALSE); | |
be2fb01f CW |
334 | $buttons = [ |
335 | [ | |
6a488035 TO |
336 | 'type' => 'next', |
337 | 'name' => ts('Save'), | |
338 | 'spacing' => ' ', | |
339 | 'isDefault' => TRUE, | |
be2fb01f CW |
340 | ], |
341 | [ | |
6a488035 TO |
342 | 'type' => 'cancel', |
343 | 'name' => ts('Cancel'), | |
be2fb01f CW |
344 | ], |
345 | ]; | |
6a488035 | 346 | if (!$this->_isGroupEmpty && !empty($this->_subtypes)) { |
b01812e5 | 347 | $buttons[0]['class'] = 'crm-warnDataLoss'; |
6a488035 TO |
348 | } |
349 | $this->addButtons($buttons); | |
350 | ||
b6b6d6cc | 351 | // TODO: Is this condition ever true? Can this code be removed? |
6a488035 TO |
352 | if ($this->_action & CRM_Core_Action::VIEW) { |
353 | $this->freeze(); | |
cbd83dde AH |
354 | $this->addElement('xbutton', 'done', ts('Done'), [ |
355 | 'type' => 'button', | |
356 | 'onclick' => "location.href='civicrm/admin/custom/group?reset=1&action=browse'", | |
357 | ]); | |
6a488035 TO |
358 | } |
359 | } | |
360 | ||
361 | /** | |
c490a46a | 362 | * Set default values for the form. Note that in edit/view mode |
6a488035 TO |
363 | * the default values are retrieved from the database |
364 | * | |
6a488035 | 365 | * |
a6c01b45 CW |
366 | * @return array |
367 | * array of default values | |
6a488035 | 368 | */ |
00be9182 | 369 | public function setDefaultValues() { |
6a488035 TO |
370 | $defaults = &$this->_defaults; |
371 | $this->assign('showMaxMultiple', TRUE); | |
372 | if ($this->_action == CRM_Core_Action::ADD) { | |
373 | $defaults['weight'] = CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_CustomGroup'); | |
374 | ||
375 | $defaults['is_multiple'] = $defaults['min_multiple'] = 0; | |
460931ca | 376 | $defaults['is_active'] = $defaults['is_public'] = $defaults['collapse_display'] = 1; |
6a488035 TO |
377 | $defaults['style'] = 'Inline'; |
378 | } | |
a7488080 | 379 | elseif (empty($defaults['max_multiple']) && !$this->_isGroupEmpty) { |
6a488035 TO |
380 | $this->assign('showMaxMultiple', FALSE); |
381 | } | |
382 | ||
b05a0fb6 | 383 | if (($this->_action & CRM_Core_Action::UPDATE) && !empty($defaults['is_multiple'])) { |
5a205b89 PJ |
384 | $defaults['collapse_display'] = 0; |
385 | } | |
386 | ||
6a488035 TO |
387 | if (isset($defaults['extends'])) { |
388 | $extends = $defaults['extends']; | |
389 | unset($defaults['extends']); | |
390 | ||
391 | $defaults['extends'][0] = $extends; | |
392 | ||
393 | if (!empty($this->_subtypes)) { | |
394 | $defaults['extends'][1] = $this->_subtypes; | |
395 | } | |
396 | else { | |
be2fb01f | 397 | $defaults['extends'][1] = [0 => '']; |
6a488035 TO |
398 | } |
399 | ||
6a488035 | 400 | if ($extends == 'Relationship' && !empty($this->_subtypes)) { |
be2fb01f | 401 | $relationshipDefaults = []; |
6a488035 TO |
402 | foreach ($defaults['extends'][1] as $donCare => $rel_type_id) { |
403 | $relationshipDefaults[] = $rel_type_id; | |
404 | } | |
405 | ||
406 | $defaults['extends'][1] = $relationshipDefaults; | |
407 | } | |
408 | } | |
409 | ||
410 | return $defaults; | |
411 | } | |
412 | ||
413 | /** | |
fe482240 | 414 | * Process the form. |
6a488035 | 415 | * |
6a488035 TO |
416 | * |
417 | * @return void | |
6a488035 TO |
418 | */ |
419 | public function postProcess() { | |
420 | // get the submitted form values. | |
421 | $params = $this->controller->exportValues('Group'); | |
422 | $params['overrideFKConstraint'] = 0; | |
423 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
424 | $params['id'] = $this->_id; | |
425 | if ($this->_defaults['extends'][0] != $params['extends'][0]) { | |
426 | $params['overrideFKConstraint'] = 1; | |
427 | } | |
428 | ||
429 | if (!empty($this->_subtypes)) { | |
be2fb01f | 430 | $subtypesToBeRemoved = []; |
00aca323 | 431 | $subtypesToPreserve = $params['extends'][1]; |
5737ecff | 432 | // Don't remove any value if group is extended to -any- subtype |
b27a1338 | 433 | if (!empty($subtypesToPreserve[0])) { |
5737ecff | 434 | $subtypesToBeRemoved = array_diff($this->_subtypes, array_intersect($this->_subtypes, $subtypesToPreserve)); |
435 | } | |
00aca323 | 436 | CRM_Contact_BAO_ContactType::deleteCustomRowsOfSubtype($this->_id, $subtypesToBeRemoved, $subtypesToPreserve); |
6a488035 TO |
437 | } |
438 | } | |
439 | elseif ($this->_action & CRM_Core_Action::ADD) { | |
440 | //new custom set , so lets set the created_id | |
441 | $session = CRM_Core_Session::singleton(); | |
442 | $params['created_id'] = $session->get('userID'); | |
443 | $params['created_date'] = date('YmdHis'); | |
444 | } | |
445 | ||
446 | $group = CRM_Core_BAO_CustomGroup::create($params); | |
447 | ||
448 | // reset the cache | |
9cdf85c1 | 449 | Civi::cache('fields')->flush(); |
5e601882 SL |
450 | // reset ACL and system caches. |
451 | CRM_Core_BAO_Cache::resetCaches(); | |
6a488035 TO |
452 | |
453 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
be2fb01f | 454 | CRM_Core_Session::setStatus(ts('Your custom field set \'%1 \' has been saved.', [1 => $group->title]), ts('Saved'), 'success'); |
6a488035 TO |
455 | } |
456 | else { | |
704f21c0 CW |
457 | // Jump directly to adding a field if popups are disabled |
458 | $action = CRM_Core_Resources::singleton()->ajaxPopupsEnabled ? '' : '/add'; | |
6f231148 | 459 | $url = CRM_Utils_System::url("civicrm/admin/custom/group/field$action", 'reset=1&new=1&gid=' . $group->id . '&action=' . ($action ? 'add' : 'browse')); |
6a488035 | 460 | CRM_Core_Session::setStatus(ts("Your custom field set '%1' has been added. You can add custom fields now.", |
be2fb01f | 461 | [1 => $group->title] |
353ffa53 | 462 | ), ts('Saved'), 'success'); |
6a488035 TO |
463 | $session = CRM_Core_Session::singleton(); |
464 | $session->replaceUserContext($url); | |
465 | } | |
466 | ||
467 | // prompt Drupal Views users to update $db_prefix in settings.php, if necessary | |
468 | global $db_prefix; | |
469 | $config = CRM_Core_Config::singleton(); | |
470 | if (is_array($db_prefix) && $config->userSystem->is_drupal && module_exists('views')) { | |
471 | // get table_name for each custom group | |
be2fb01f | 472 | $tables = []; |
353ffa53 | 473 | $sql = "SELECT table_name FROM civicrm_custom_group WHERE is_active = 1"; |
6a488035 TO |
474 | $result = CRM_Core_DAO::executeQuery($sql); |
475 | while ($result->fetch()) { | |
476 | $tables[$result->table_name] = $result->table_name; | |
477 | } | |
478 | ||
479 | // find out which tables are missing from the $db_prefix array | |
480 | $missingTableNames = array_diff_key($tables, $db_prefix); | |
481 | ||
482 | if (!empty($missingTableNames)) { | |
483 | CRM_Core_Session::setStatus(ts("To ensure that all of your custom data groups are available to Views, you may need to add the following key(s) to the db_prefix array in your settings.php file: '%1'.", | |
be2fb01f | 484 | [1 => implode(', ', $missingTableNames)] |
353ffa53 | 485 | ), ts('Note'), 'info'); |
6a488035 TO |
486 | } |
487 | } | |
488 | } | |
489 | ||
e0ef6999 | 490 | /** |
a9c1ff43 | 491 | * Return a formatted list of relationship labels. |
e0ef6999 | 492 | * |
a6c01b45 | 493 | * @return array |
a9c1ff43 | 494 | * Array (int $id => string $label). |
e0ef6999 | 495 | */ |
a9c1ff43 TO |
496 | public static function getRelationshipTypes() { |
497 | // Note: We include inactive reltypes because we don't want to break custom-data | |
498 | // UI when a reltype is disabled. | |
499 | return CRM_Core_DAO::executeQuery(' | |
500 | SELECT | |
501 | id, | |
502 | (CASE 1 | |
503 | WHEN label_a_b is not null AND label_b_a is not null AND label_a_b != label_b_a | |
504 | THEN concat(label_a_b, \' / \', label_b_a) | |
505 | WHEN label_a_b is not null | |
506 | THEN label_a_b | |
507 | WHEN label_b_a is not null | |
508 | THEN label_b_a | |
509 | ELSE concat("RelType #", id) | |
510 | END) as label | |
511 | FROM civicrm_relationship_type | |
512 | ' | |
513 | )->fetchMap('id', 'label'); | |
6a488035 | 514 | } |
96025800 | 515 | |
6a488035 | 516 | } |