Merge pull request #18286 from sunilpawar/ui_30
[civicrm-core.git] / ext / afform / gui / afform_gui.php
CommitLineData
6bc5e930
TO
1<?php
2
3require_once 'afform_gui.civix.php';
4use CRM_AfformGui_ExtensionUtil as E;
5
6/**
7 * Implements hook_civicrm_config().
8 *
9 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_config
10 */
11function afform_gui_civicrm_config(&$config) {
12 _afform_gui_civix_civicrm_config($config);
13}
14
15/**
16 * Implements hook_civicrm_xmlMenu().
17 *
18 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_xmlMenu
19 */
20function afform_gui_civicrm_xmlMenu(&$files) {
21 _afform_gui_civix_civicrm_xmlMenu($files);
22}
23
24/**
25 * Implements hook_civicrm_install().
26 *
27 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_install
28 */
29function afform_gui_civicrm_install() {
30 _afform_gui_civix_civicrm_install();
31}
32
33/**
34 * Implements hook_civicrm_postInstall().
35 *
36 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_postInstall
37 */
38function afform_gui_civicrm_postInstall() {
39 _afform_gui_civix_civicrm_postInstall();
40}
41
42/**
43 * Implements hook_civicrm_uninstall().
44 *
45 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_uninstall
46 */
47function afform_gui_civicrm_uninstall() {
48 _afform_gui_civix_civicrm_uninstall();
49}
50
51/**
52 * Implements hook_civicrm_enable().
53 *
54 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_enable
55 */
56function afform_gui_civicrm_enable() {
57 _afform_gui_civix_civicrm_enable();
58}
59
60/**
61 * Implements hook_civicrm_disable().
62 *
63 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_disable
64 */
65function afform_gui_civicrm_disable() {
66 _afform_gui_civix_civicrm_disable();
67}
68
69/**
70 * Implements hook_civicrm_upgrade().
71 *
72 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_upgrade
73 */
74function afform_gui_civicrm_upgrade($op, CRM_Queue_Queue $queue = NULL) {
75 return _afform_gui_civix_civicrm_upgrade($op, $queue);
76}
77
78/**
79 * Implements hook_civicrm_managed().
80 *
81 * Generate a list of entities to create/deactivate/delete when this module
82 * is installed, disabled, uninstalled.
83 *
84 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_managed
85 */
86function afform_gui_civicrm_managed(&$entities) {
87 _afform_gui_civix_civicrm_managed($entities);
88}
89
90/**
91 * Implements hook_civicrm_caseTypes().
92 *
93 * Generate a list of case-types.
94 *
95 * Note: This hook only runs in CiviCRM 4.4+.
96 *
97 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_caseTypes
98 */
99function afform_gui_civicrm_caseTypes(&$caseTypes) {
100 _afform_gui_civix_civicrm_caseTypes($caseTypes);
101}
102
103/**
104 * Implements hook_civicrm_angularModules().
105 *
106 * Generate a list of Angular modules.
107 *
108 * Note: This hook only runs in CiviCRM 4.5+. It may
109 * use features only available in v4.6+.
110 *
111 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_angularModules
112 */
113function afform_gui_civicrm_angularModules(&$angularModules) {
114 _afform_gui_civix_civicrm_angularModules($angularModules);
115}
116
117/**
118 * Implements hook_civicrm_alterSettingsFolders().
119 *
120 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_alterSettingsFolders
121 */
122function afform_gui_civicrm_alterSettingsFolders(&$metaDataFolders = NULL) {
123 _afform_gui_civix_civicrm_alterSettingsFolders($metaDataFolders);
124}
125
126/**
127 * Implements hook_civicrm_entityTypes().
128 *
129 * Declare entity types provided by this module.
130 *
131 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_entityTypes
132 */
133function afform_gui_civicrm_entityTypes(&$entityTypes) {
134 _afform_gui_civix_civicrm_entityTypes($entityTypes);
135}
136
7620445e
TO
137/**
138 * Implements hook_civicrm_themes().
139 */
140function afform_gui_civicrm_themes(&$themes) {
141 _afform_gui_civix_civicrm_themes($themes);
142}
143
6bc5e930 144/**
c820e08c
CW
145 * Implements hook_civicrm_pageRun().
146 */
147function afform_gui_civicrm_pageRun(&$page) {
148 if (get_class($page) == 'CRM_Afform_Page_AfformBase' && $page->get('afModule') == 'afGuiAdmin') {
149 Civi::resources()->addScriptUrl(Civi::service('asset_builder')->getUrl('af-gui-vars.js'));
150 }
151}
6bc5e930
TO
152
153/**
c820e08c 154 * Implements hook_civicrm_buildAsset().
6bc5e930 155 *
c820e08c 156 * Loads metadata to send to the gui editor.
e1aca853
CW
157 *
158 * FIXME: This is a prototype and should get broken out into separate callbacks with hooks, events, etc.
c820e08c
CW
159 */
160function afform_gui_civicrm_buildAsset($asset, $params, &$mimeType, &$content) {
161 if ($asset !== 'af-gui-vars.js') {
162 return;
163 }
164
0406c8f9
CW
165 $getFieldParams = [
166 'checkPermissions' => FALSE,
167 'includeCustom' => TRUE,
168 'loadOptions' => TRUE,
169 'action' => 'create',
170 'select' => ['name', 'title', 'input_type', 'input_attrs', 'required', 'options', 'help_pre', 'help_post', 'serialize', 'data_type'],
171 'where' => [['input_type', 'IS NOT NULL']],
172 ];
1b745abc 173
0406c8f9
CW
174 $data = [
175 'entities' => [
176 'Contact' => [
177 'entity' => 'Contact',
178 'label' => ts('Contact'),
179 'fields' => (array) civicrm_api4('Contact', 'getFields', $getFieldParams, 'name'),
180 ],
181 ],
182 'blocks' => [],
183 ];
c820e08c 184
0406c8f9
CW
185 $contactTypes = CRM_Contact_BAO_ContactType::basicTypeInfo();
186
187 // Scan all extensions for our list of supported entities
188 foreach (CRM_Extension_System::singleton()->getMapper()->getActiveModuleFiles() as $ext) {
189 $dir = CRM_Utils_File::addTrailingSlash(dirname($ext['filePath'])) . 'afformEntities';
190 if (is_dir($dir)) {
191 foreach (glob($dir . '/*.php') as $file) {
192 $entity = include $file;
193 // Skip disabled contact types
194 if (!empty($entity['contact_type']) && !isset($contactTypes[$entity['contact_type']])) {
195 continue;
196 }
197 if (!empty($entity['contact_type'])) {
198 $entity['label'] = $contactTypes[$entity['contact_type']]['label'];
199 }
200 // For Contact pseudo-entities (Individual, Organization, Household)
201 $values = array_intersect_key($entity, ['contact_type' => NULL]);
202 $afformEntity = $entity['contact_type'] ?? $entity['entity'];
203 $entity['fields'] = (array) civicrm_api4($entity['entity'], 'getFields', $getFieldParams + ['values' => $values], 'name');
204 $data['entities'][$afformEntity] = $entity;
205 }
206 }
207 }
c820e08c 208
edf837b4 209 // Load fields from afform blocks with joins
e1aca853
CW
210 $blockData = \Civi\Api4\Afform::get()
211 ->setCheckPermissions(FALSE)
9633741a
CW
212 ->addWhere('join', 'IS NOT NULL')
213 ->setSelect(['join'])
e1aca853
CW
214 ->execute();
215 foreach ($blockData as $block) {
9633741a 216 if (!isset($data['entities'][$block['join']]['fields'])) {
0406c8f9
CW
217 $data['entities'][$block['join']]['entity'] = $block['join'];
218 // Normally you shouldn't pass variables to ts() but very common strings like "Email" should already exist
219 $data['entities'][$block['join']]['label'] = ts($block['join']);
220 $data['entities'][$block['join']]['fields'] = (array) civicrm_api4($block['join'], 'getFields', $getFieldParams, 'name');
344e8290 221 }
e1aca853
CW
222 }
223
edf837b4 224 // Todo: add method for extensions to define other elements
e1aca853
CW
225 $data['elements'] = [
226 'container' => [
227 'title' => ts('Container'),
228 'element' => [
229 '#tag' => 'div',
230 'class' => 'af-container',
231 '#children' => [],
232 ],
233 ],
234 'text' => [
235 'title' => ts('Text box'),
236 'element' => [
237 '#tag' => 'p',
238 'class' => 'af-text',
239 '#children' => [
240 ['#text' => ts('Enter text')],
241 ],
242 ],
243 ],
244 'markup' => [
245 'title' => ts('Rich content'),
246 'element' => [
247 '#tag' => 'div',
248 'class' => 'af-markup',
249 '#markup' => FALSE,
250 ],
251 ],
9633741a
CW
252 'submit' => [
253 'title' => ts('Submit Button'),
e1aca853
CW
254 'element' => [
255 '#tag' => 'button',
256 'class' => 'af-button btn-primary',
257 'crm-icon' => 'fa-check',
258 'ng-click' => 'afform.submit()',
259 '#children' => [
9633741a 260 ['#text' => ts('Submit')],
e1aca853
CW
261 ],
262 ],
263 ],
e9dc7ca5
CW
264 'fieldset' => [
265 'title' => ts('Fieldset'),
266 'element' => [
267 '#tag' => 'fieldset',
268 'af-fieldset' => NULL,
269 '#children' => [
270 [
271 '#tag' => 'legend',
272 'class' => 'af-text',
273 '#children' => [
274 [
275 '#text' => ts('Enter title'),
276 ],
277 ],
278 ],
279 ],
280 ],
281 ],
e1aca853
CW
282 ];
283
0406c8f9
CW
284 // Reformat options
285 // TODO: Teach the api to return options in this format
286 foreach ($data['entities'] as $entityName => $entity) {
287 foreach ($entity['fields'] as $name => $field) {
b4def6e9 288 if (!empty($field['options'])) {
0406c8f9 289 $data['entities'][$entityName]['fields'][$name]['options'] = CRM_Utils_Array::makeNonAssociative($field['options'], 'key', 'label');
b4def6e9
CW
290 }
291 else {
0406c8f9 292 unset($data['entities'][$entityName]['fields'][$name]['options']);
b4def6e9
CW
293 }
294 }
1b745abc
CW
295 }
296
4bd44053
CW
297 // Scan for input types
298 // FIXME: Need a way to load this from other extensions too
b4def6e9 299 foreach (glob(__DIR__ . '/ang/afGuiEditor/inputType/*.html') as $file) {
4bd44053
CW
300 $matches = [];
301 preg_match('/([-a-z_A-Z0-9]*).html/', $file, $matches);
b4def6e9 302 $data['inputType'][$matches[1]] = $matches[1];
4bd44053
CW
303 }
304
51ec7d5a
CW
305 $data['styles'] = [
306 'default' => ts('Default'),
307 'primary' => ts('Primary'),
308 'success' => ts('Success'),
309 'info' => ts('Info'),
310 'warning' => ts('Warning'),
311 'danger' => ts('Danger'),
312 ];
313
afc0cd72
CW
314 $data['permissions'] = [];
315 foreach (CRM_Core_Permission::basicPermissions(TRUE, TRUE) as $name => $perm) {
316 $data['permissions'][] = [
317 'id' => $name,
318 'text' => $perm[0],
319 'description' => $perm[1] ?? NULL,
320 ];
321 }
322
c820e08c 323 $mimeType = 'text/javascript';
1b745abc 324 $content = "CRM.afformAdminData=" . json_encode($data, JSON_UNESCAPED_SLASHES) . ';';
c820e08c 325}