Commit | Line | Data |
---|---|---|
6bc5e930 TO |
1 | <?php |
2 | ||
3 | require_once 'afform_gui.civix.php'; | |
4 | use 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 | */ | |
11 | function 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 | */ | |
20 | function 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 | */ | |
29 | function 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 | */ | |
38 | function 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 | */ | |
47 | function 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 | */ | |
56 | function 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 | */ | |
65 | function 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 | */ | |
74 | function 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 | */ | |
86 | function 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 | */ | |
99 | function 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 | */ | |
113 | function 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 | */ | |
122 | function 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 | */ | |
133 | function afform_gui_civicrm_entityTypes(&$entityTypes) { | |
134 | _afform_gui_civix_civicrm_entityTypes($entityTypes); | |
135 | } | |
136 | ||
7620445e TO |
137 | /** |
138 | * Implements hook_civicrm_themes(). | |
139 | */ | |
140 | function afform_gui_civicrm_themes(&$themes) { | |
141 | _afform_gui_civix_civicrm_themes($themes); | |
142 | } | |
143 | ||
6bc5e930 | 144 | /** |
c820e08c CW |
145 | * Implements hook_civicrm_pageRun(). |
146 | */ | |
147 | function 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 CW |
156 | * Loads metadata to send to the gui editor. |
157 | */ | |
158 | function afform_gui_civicrm_buildAsset($asset, $params, &$mimeType, &$content) { | |
159 | if ($asset !== 'af-gui-vars.js') { | |
160 | return; | |
161 | } | |
162 | ||
1b745abc CW |
163 | $entityWhitelist = $data = []; |
164 | ||
165 | // First scan the entityDefaults directory for our list of supported entities | |
166 | // FIXME: Need a way to load this from other extensions too | |
167 | foreach (glob(__DIR__ . '/ang/afGuiEditor/entityDefaults/*.json') as $file) { | |
168 | $matches = []; | |
169 | preg_match('/([-a-z_A-Z0-9]*).json/', $file, $matches); | |
170 | $entityWhitelist[] = $entity = $matches[1]; | |
171 | // No json_decode, the files are not strict json and will go through angular.$parse clientside | |
172 | $data['defaults'][$entity] = trim(CRM_Utils_JS::stripComments(file_get_contents($file))); | |
c820e08c CW |
173 | } |
174 | ||
1b745abc | 175 | $data['entities'] = (array) Civi\Api4\Entity::get() |
c820e08c | 176 | ->setCheckPermissions(FALSE) |
1b745abc CW |
177 | ->setSelect(['name', 'description']) |
178 | ->addWhere('name', 'IN', $entityWhitelist) | |
c820e08c CW |
179 | ->execute(); |
180 | ||
1b745abc CW |
181 | foreach ($entityWhitelist as $entityName) { |
182 | $api = 'Civi\\Api4\\' . $entityName; | |
183 | $data['fields'][$entityName] = (array) $api::getFields() | |
184 | ->setCheckPermissions(FALSE) | |
185 | ->setIncludeCustom(TRUE) | |
186 | ->setLoadOptions(TRUE) | |
187 | ->setAction('create') | |
b4def6e9 | 188 | ->setSelect(['name', 'title', 'input_type', 'input_attrs', 'required', 'options', 'help_pre', 'help_post', 'serialize', 'data_type']) |
1b745abc CW |
189 | ->addWhere('input_type', 'IS NOT NULL') |
190 | ->execute() | |
191 | ->indexBy('name'); | |
b4def6e9 CW |
192 | |
193 | // TODO: Teach the api to return options in this format | |
194 | foreach ($data['fields'][$entityName] as $name => $field) { | |
195 | if (!empty($field['options'])) { | |
196 | $data['fields'][$entityName][$name]['options'] = CRM_Utils_Array::makeNonAssociative($field['options'], 'key', 'label'); | |
197 | } | |
198 | else { | |
199 | unset($data['fields'][$entityName][$name]['options']); | |
200 | } | |
201 | } | |
1b745abc CW |
202 | } |
203 | ||
204 | // Now adjust the field metadata | |
205 | // FIXME: This should probably be a callback event or something to allow extensions to tweak the metadata for their entities | |
206 | $data['fields']['Contact']['contact_type']['required_data'] = TRUE; | |
a064e90d | 207 | |
4bd44053 CW |
208 | // Scan for input types |
209 | // FIXME: Need a way to load this from other extensions too | |
b4def6e9 | 210 | foreach (glob(__DIR__ . '/ang/afGuiEditor/inputType/*.html') as $file) { |
4bd44053 CW |
211 | $matches = []; |
212 | preg_match('/([-a-z_A-Z0-9]*).html/', $file, $matches); | |
b4def6e9 | 213 | $data['inputType'][$matches[1]] = $matches[1]; |
4bd44053 CW |
214 | } |
215 | ||
51ec7d5a CW |
216 | $data['styles'] = [ |
217 | 'default' => ts('Default'), | |
218 | 'primary' => ts('Primary'), | |
219 | 'success' => ts('Success'), | |
220 | 'info' => ts('Info'), | |
221 | 'warning' => ts('Warning'), | |
222 | 'danger' => ts('Danger'), | |
223 | ]; | |
224 | ||
c820e08c | 225 | $mimeType = 'text/javascript'; |
1b745abc | 226 | $content = "CRM.afformAdminData=" . json_encode($data, JSON_UNESCAPED_SLASHES) . ';'; |
c820e08c | 227 | } |