Sort permission list by title. Make it easier to skim.
[civicrm-core.git] / ext / afform / admin / Civi / AfformAdmin / AfformAdminMeta.php
CommitLineData
2e2aaaea 1<?php
490565d0
CW
2
3namespace Civi\AfformAdmin;
4
2e2aaaea
CW
5use CRM_AfformAdmin_ExtensionUtil as E;
6
490565d0 7class AfformAdminMeta {
2e2aaaea 8
9e58ceed
CW
9 /**
10 * @return array
11 */
12 public static function getAdminSettings() {
3d371a03
CW
13 $afformTypes = (array) \Civi\Api4\OptionValue::get(FALSE)
14 ->addSelect('name', 'label', 'icon')
15 ->addWhere('is_active', '=', TRUE)
16 ->addWhere('option_group_id:name', '=', 'afform_type')
17 ->addOrderBy('weight', 'ASC')
18 ->execute();
19 // Pluralize tabs (too bad option groups only store a single label)
20 $plurals = [
21 'form' => ts('Custom Forms'),
22 'search' => ts('Search Displays'),
23 'block' => ts('Field Blocks'),
24 'system' => ts('System Forms'),
25 ];
26 foreach ($afformTypes as $index => $type) {
27 $afformTypes[$index]['plural'] = $plurals[$type['name']] ?? \CRM_Utils_String::pluralize($type['label']);
28 }
9e58ceed 29 return [
3d371a03 30 'afform_type' => $afformTypes,
9e58ceed
CW
31 ];
32 }
33
2e2aaaea 34 /**
490565d0
CW
35 * @param $entityName
36 * @return array|void
37 */
38 public static function getAfformEntity($entityName) {
39 // Optimization: look here before scanning every other extension
40 global $civicrm_root;
41 $fileName = \CRM_Utils_File::addTrailingSlash($civicrm_root) . "ext/afform/admin/afformEntities/$entityName.php";
42 if (is_file($fileName)) {
43 return include $fileName;
44 }
45 foreach (\CRM_Extension_System::singleton()->getMapper()->getActiveModuleFiles() as $ext) {
46 $fileName = \CRM_Utils_File::addTrailingSlash(dirname($ext['filePath'])) . "afformEntities/$entityName.php";
47 if (is_file($fileName)) {
48 return include $fileName;
49 }
50 }
51 }
52
53 /**
54 * @param $entityName
9e58ceed 55 * @return array
2e2aaaea 56 */
490565d0
CW
57 public static function getApiEntity($entityName) {
58 if (in_array($entityName, ['Individual', 'Household', 'Organization'])) {
59 $contactTypes = \CRM_Contact_BAO_ContactType::basicTypeInfo();
60 return [
61 'entity' => 'Contact',
62 'label' => $contactTypes[$entityName]['label'],
63 ];
64 }
b1335297
CW
65 $info = \Civi\Api4\Entity::get(FALSE)
66 ->addWhere('name', '=', $entityName)
67 ->addSelect('title', 'icon')
68 ->execute()->first();
490565d0
CW
69 return [
70 'entity' => $entityName,
71 'label' => $info['title'],
72 'icon' => $info['icon'],
73 ];
74 }
75
76 /**
77 * @param $entityName
78 * @param array $params
79 * @return array
80 */
81 public static function getFields($entityName, $params = []) {
82 $params += [
2e2aaaea
CW
83 'checkPermissions' => FALSE,
84 'includeCustom' => TRUE,
1a0ba8a6 85 'loadOptions' => ['id', 'label'],
2e2aaaea
CW
86 'action' => 'create',
87 'select' => ['name', 'label', 'input_type', 'input_attrs', 'required', 'options', 'help_pre', 'help_post', 'serialize', 'data_type'],
88 'where' => [['input_type', 'IS NOT NULL']],
89 ];
490565d0
CW
90 if (in_array($entityName, ['Individual', 'Household', 'Organization'])) {
91 $params['values']['contact_type'] = $entityName;
92 $entityName = 'Contact';
93 }
94 if ($entityName === 'Address') {
95 // The stateProvince option list is waaay too long unless country limits are set
96 if (!\Civi::settings()->get('provinceLimit')) {
97 // If no province limit, restrict it to the default country, or if there's no default, pick one to avoid breaking the UI
98 $params['values']['country_id'] = \Civi::settings()->get('defaultContactCountry') ?: 1228;
99 }
100 $params['values']['state_province_id'] = \Civi::settings()->get('defaultContactStateProvince');
101 }
102 return (array) civicrm_api4($entityName, 'getFields', $params, 'name');
103 }
2e2aaaea 104
490565d0
CW
105 /**
106 * Loads metadata for the gui editor.
107 *
108 * @return array
109 */
110 public static function getGuiSettings() {
2e2aaaea
CW
111 $data = [
112 'entities' => [
490565d0 113 'Contact' => self::getApiEntity('Contact'),
2e2aaaea 114 ],
2e2aaaea
CW
115 ];
116
490565d0 117 $contactTypes = \CRM_Contact_BAO_ContactType::basicTypeInfo();
2e2aaaea 118
9c7a4423 119 // Scan all extensions for entities & input types
490565d0
CW
120 foreach (\CRM_Extension_System::singleton()->getMapper()->getActiveModuleFiles() as $ext) {
121 $dir = \CRM_Utils_File::addTrailingSlash(dirname($ext['filePath']));
2e2aaaea 122 if (is_dir($dir)) {
9c7a4423
CW
123 // Scan for entities
124 foreach (glob($dir . 'afformEntities/*.php') as $file) {
2e2aaaea 125 $entity = include $file;
490565d0
CW
126 $afformEntity = basename($file, '.php');
127 // Contact pseudo-entities (Individual, Organization, Household) get special treatment,
128 // notably their fields are pre-loaded since they are both commonly-used and nonstandard
2e2aaaea 129 if (!empty($entity['contact_type'])) {
490565d0
CW
130 // Skip disabled contact types
131 if (!isset($contactTypes[$entity['contact_type']])) {
132 continue;
133 }
2e2aaaea
CW
134 $entity['label'] = $contactTypes[$entity['contact_type']]['label'];
135 }
490565d0
CW
136 elseif (empty($entity['label']) || empty($entity['icon'])) {
137 $entity += self::getApiEntity($entity['entity']);
138 }
2e2aaaea
CW
139 $data['entities'][$afformEntity] = $entity;
140 }
9c7a4423
CW
141 // Scan for input types
142 foreach (glob($dir . 'ang/afGuiEditor/inputType/*.html') as $file) {
490565d0
CW
143 $name = basename($file, '.html');
144 $data['inputType'][$name] = $name;
9c7a4423 145 }
2e2aaaea
CW
146 }
147 }
148
2e2aaaea
CW
149 // Todo: add method for extensions to define other elements
150 $data['elements'] = [
151 'container' => [
152 'title' => E::ts('Container'),
153 'element' => [
154 '#tag' => 'div',
155 'class' => 'af-container',
156 '#children' => [],
157 ],
158 ],
159 'text' => [
160 'title' => E::ts('Text box'),
161 'element' => [
162 '#tag' => 'p',
163 'class' => 'af-text',
164 '#children' => [
165 ['#text' => E::ts('Enter text')],
166 ],
167 ],
168 ],
169 'markup' => [
170 'title' => E::ts('Rich content'),
171 'element' => [
172 '#tag' => 'div',
173 'class' => 'af-markup',
174 '#markup' => FALSE,
175 ],
176 ],
177 'submit' => [
178 'title' => E::ts('Submit Button'),
179 'element' => [
180 '#tag' => 'button',
181 'class' => 'af-button btn-primary',
182 'crm-icon' => 'fa-check',
183 'ng-click' => 'afform.submit()',
184 '#children' => [
185 ['#text' => E::ts('Submit')],
186 ],
187 ],
188 ],
189 'fieldset' => [
190 'title' => E::ts('Fieldset'),
191 'element' => [
192 '#tag' => 'fieldset',
193 'af-fieldset' => NULL,
194 '#children' => [
195 [
196 '#tag' => 'legend',
197 'class' => 'af-text',
198 '#children' => [
199 [
200 '#text' => E::ts('Enter title'),
201 ],
202 ],
203 ],
204 ],
205 ],
206 ],
207 ];
208
2e2aaaea
CW
209 $data['styles'] = [
210 'default' => E::ts('Default'),
211 'primary' => E::ts('Primary'),
212 'success' => E::ts('Success'),
213 'info' => E::ts('Info'),
214 'warning' => E::ts('Warning'),
215 'danger' => E::ts('Danger'),
216 ];
217
218 $data['permissions'] = [];
f1de39ec
TO
219 $perms = \Civi\Api4\Permission::get()
220 ->addWhere('group', 'IN', ['afformGeneric', 'const', 'civicrm', 'cms'])
221 ->addWhere('is_active', '=', 1)
7efb95e7 222 ->setOrderBy(['title' => 'ASC'])
f1de39ec
TO
223 ->execute();
224 foreach ($perms as $perm) {
2e2aaaea 225 $data['permissions'][] = [
f1de39ec
TO
226 'id' => $perm['name'],
227 'text' => $perm['title'],
228 'description' => $perm['description'] ?? NULL,
2e2aaaea
CW
229 ];
230 }
231
232 return $data;
233 }
234
235}