Commit | Line | Data |
---|---|---|
11e724f0 TO |
1 | <?php |
2 | ||
3 | namespace Civi\Api4\Action\Afform; | |
4 | ||
9c84a124 CW |
5 | use Civi\Api4\CustomField; |
6 | use Civi\Api4\CustomGroup; | |
7 | ||
11e724f0 | 8 | /** |
28b4ace4 | 9 | * @inheritDoc |
11e724f0 TO |
10 | * @package Civi\Api4\Action\Afform |
11 | */ | |
28b4ace4 | 12 | class Get extends \Civi\Api4\Generic\BasicGetAction { |
11e724f0 | 13 | |
28b4ace4 | 14 | use \Civi\Api4\Utils\AfformFormatTrait; |
11e724f0 TO |
15 | |
16 | public function getRecords() { | |
17 | /** @var \CRM_Afform_AfformScanner $scanner */ | |
18 | $scanner = \Civi::service('afform_scanner'); | |
9c84a124 CW |
19 | $getComputed = $this->_isFieldSelected('has_local') || $this->_isFieldSelected('has_base'); |
20 | $getLayout = $this->_isFieldSelected('layout'); | |
21 | ||
edf837b4 CW |
22 | // This helps optimize lookups by file/module/directive name |
23 | $toGet = array_filter([ | |
24 | 'name' => $this->_itemsToGet('name'), | |
25 | 'module_name' => $this->_itemsToGet('module_name'), | |
26 | 'directive_name' => $this->_itemsToGet('directive_name'), | |
27 | ]); | |
11e724f0 | 28 | |
edf837b4 | 29 | $names = $toGet['name'] ?? array_keys($scanner->findFilePaths()); |
25f2b36b | 30 | |
9c84a124 | 31 | $values = $this->getAutoGenerated($names, $toGet, $getLayout); |
11e724f0 | 32 | |
11e724f0 | 33 | foreach ($names as $name) { |
25f2b36b | 34 | $info = [ |
edf837b4 | 35 | 'name' => $name, |
25f2b36b CW |
36 | 'module_name' => _afform_angular_module_name($name, 'camel'), |
37 | 'directive_name' => _afform_angular_module_name($name, 'dash'), | |
38 | ]; | |
edf837b4 CW |
39 | foreach ($toGet as $key => $get) { |
40 | if (!in_array($info[$key], $get)) { | |
41 | continue; | |
42 | } | |
25f2b36b | 43 | } |
11e724f0 | 44 | $record = $scanner->getMeta($name); |
9c84a124 CW |
45 | if (!$record && !isset($values[$name])) { |
46 | continue; | |
47 | } | |
25f2b36b | 48 | $values[$name] = array_merge($values[$name] ?? [], $record ?? [], $info); |
9c84a124 CW |
49 | if ($getComputed) { |
50 | $scanner->addComputedFields($values[$name]); | |
1aebf5b0 | 51 | } |
9c84a124 CW |
52 | if ($getLayout) { |
53 | $values[$name]['layout'] = $scanner->getLayout($name) ?? $values[$name]['layout'] ?? ''; | |
11e724f0 | 54 | } |
11e724f0 TO |
55 | } |
56 | ||
9c84a124 CW |
57 | if ($getLayout && $this->layoutFormat !== 'html') { |
58 | foreach ($values as $name => $record) { | |
59 | $values[$name]['layout'] = $this->convertHtmlToOutput($record['layout']); | |
60 | } | |
61 | } | |
62 | ||
63 | return $values; | |
64 | } | |
65 | ||
66 | /** | |
67 | * Generates afform blocks from custom field sets. | |
68 | * | |
69 | * @param $names | |
70 | * @param $toGet | |
71 | * @param $getLayout | |
72 | * @return array | |
73 | * @throws \API_Exception | |
74 | */ | |
75 | protected function getAutoGenerated(&$names, $toGet, $getLayout) { | |
76 | $values = $groupNames = []; | |
edf837b4 CW |
77 | foreach ($toGet['name'] ?? [] as $name) { |
78 | if (strpos($name, 'afjoinCustom_') === 0 && strlen($name) > 13) { | |
79 | $groupNames[] = substr($name, 13); | |
9c84a124 CW |
80 | } |
81 | } | |
c3273f01 CW |
82 | // Early return if this api call is fetching afforms by name and those names are not custom-related |
83 | if ((!empty($toGet['name']) && !$groupNames) | |
edf837b4 CW |
84 | || (!empty($toGet['module_name']) && !strstr(implode(' ', $toGet['module_name']), 'afjoinCustom')) |
85 | || (!empty($toGet['directive_name']) && !strstr(implode(' ', $toGet['directive_name']), 'afjoin-custom')) | |
86 | ) { | |
9c84a124 CW |
87 | return $values; |
88 | } | |
89 | $customApi = CustomGroup::get() | |
90 | ->setCheckPermissions(FALSE) | |
91 | ->setSelect(['name', 'title', 'help_pre', 'help_post']) | |
92 | ->addWhere('is_multiple', '=', 1) | |
93 | ->addWhere('is_active', '=', 1); | |
94 | if ($groupNames) { | |
95 | $customApi->addWhere('name', 'IN', $groupNames); | |
96 | } | |
97 | if ($getLayout) { | |
98 | $customApi->addSelect('help_pre')->addSelect('help_post'); | |
99 | $customApi->addChain('fields', CustomField::get() | |
100 | ->setCheckPermissions(FALSE) | |
101 | ->addSelect('name') | |
102 | ->addWhere('custom_group_id', '=', '$id') | |
103 | ->addWhere('is_active', '=', 1) | |
104 | ->addOrderBy('weight', 'ASC') | |
105 | ); | |
106 | } | |
107 | foreach ($customApi->execute() as $custom) { | |
edf837b4 | 108 | $name = 'afjoinCustom_' . $custom['name']; |
9c84a124 CW |
109 | if (!in_array($name, $names)) { |
110 | $names[] = $name; | |
111 | } | |
112 | $item = [ | |
113 | 'name' => $name, | |
114 | 'requires' => [], | |
115 | 'title' => ts('%1 block (default)', [1 => $custom['title']]), | |
116 | 'description' => '', | |
1887d8bd | 117 | 'is_dashlet' => FALSE, |
9c84a124 CW |
118 | 'is_public' => FALSE, |
119 | 'permission' => 'access CiviCRM', | |
344e8290 | 120 | 'join' => 'Custom_' . $custom['name'], |
9c84a124 | 121 | 'extends' => 'Contact', |
344e8290 | 122 | 'repeat' => TRUE, |
9c84a124 CW |
123 | 'has_base' => TRUE, |
124 | ]; | |
125 | if ($getLayout) { | |
126 | $item['layout'] = ($custom['help_pre'] ? '<div class="af-markup">' . $custom['help_pre'] . "</div>\n" : ''); | |
127 | foreach ($custom['fields'] as $field) { | |
128 | $item['layout'] .= "<af-field name=\"{$field['name']}\" />\n"; | |
129 | } | |
130 | $item['layout'] .= ($custom['help_post'] ? '<div class="af-markup">' . $custom['help_post'] . "</div>\n" : ''); | |
131 | } | |
132 | $values[$name] = $item; | |
133 | } | |
11e724f0 TO |
134 | return $values; |
135 | } | |
136 | ||
137 | } |