3 require_once 'afform_gui.civix.php';
4 use CRM_AfformGui_ExtensionUtil
as E
;
7 * Implements hook_civicrm_config().
9 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_config
11 function afform_gui_civicrm_config(&$config) {
12 _afform_gui_civix_civicrm_config($config);
16 * Implements hook_civicrm_xmlMenu().
18 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_xmlMenu
20 function afform_gui_civicrm_xmlMenu(&$files) {
21 _afform_gui_civix_civicrm_xmlMenu($files);
25 * Implements hook_civicrm_install().
27 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_install
29 function afform_gui_civicrm_install() {
30 _afform_gui_civix_civicrm_install();
34 * Implements hook_civicrm_postInstall().
36 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_postInstall
38 function afform_gui_civicrm_postInstall() {
39 _afform_gui_civix_civicrm_postInstall();
43 * Implements hook_civicrm_uninstall().
45 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_uninstall
47 function afform_gui_civicrm_uninstall() {
48 _afform_gui_civix_civicrm_uninstall();
52 * Implements hook_civicrm_enable().
54 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_enable
56 function afform_gui_civicrm_enable() {
57 _afform_gui_civix_civicrm_enable();
61 * Implements hook_civicrm_disable().
63 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_disable
65 function afform_gui_civicrm_disable() {
66 _afform_gui_civix_civicrm_disable();
70 * Implements hook_civicrm_upgrade().
72 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_upgrade
74 function afform_gui_civicrm_upgrade($op, CRM_Queue_Queue
$queue = NULL) {
75 return _afform_gui_civix_civicrm_upgrade($op, $queue);
79 * Implements hook_civicrm_managed().
81 * Generate a list of entities to create/deactivate/delete when this module
82 * is installed, disabled, uninstalled.
84 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_managed
86 function afform_gui_civicrm_managed(&$entities) {
87 _afform_gui_civix_civicrm_managed($entities);
91 * Implements hook_civicrm_caseTypes().
93 * Generate a list of case-types.
95 * Note: This hook only runs in CiviCRM 4.4+.
97 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_caseTypes
99 function afform_gui_civicrm_caseTypes(&$caseTypes) {
100 _afform_gui_civix_civicrm_caseTypes($caseTypes);
104 * Implements hook_civicrm_angularModules().
106 * Generate a list of Angular modules.
108 * Note: This hook only runs in CiviCRM 4.5+. It may
109 * use features only available in v4.6+.
111 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_angularModules
113 function afform_gui_civicrm_angularModules(&$angularModules) {
114 _afform_gui_civix_civicrm_angularModules($angularModules);
118 * Implements hook_civicrm_alterSettingsFolders().
120 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_alterSettingsFolders
122 function afform_gui_civicrm_alterSettingsFolders(&$metaDataFolders = NULL) {
123 _afform_gui_civix_civicrm_alterSettingsFolders($metaDataFolders);
127 * Implements hook_civicrm_entityTypes().
129 * Declare entity types provided by this module.
131 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_entityTypes
133 function afform_gui_civicrm_entityTypes(&$entityTypes) {
134 _afform_gui_civix_civicrm_entityTypes($entityTypes);
138 * Implements hook_civicrm_themes().
140 function afform_gui_civicrm_themes(&$themes) {
141 _afform_gui_civix_civicrm_themes($themes);
145 * Implements hook_civicrm_pageRun().
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'));
154 * Implements hook_civicrm_buildAsset().
156 * Loads metadata to send to the gui editor.
158 * FIXME: This is a prototype and should get broken out into separate callbacks with hooks, events, etc.
160 function afform_gui_civicrm_buildAsset($asset, $params, &$mimeType, &$content) {
161 if ($asset !== 'af-gui-vars.js') {
165 $entityWhitelist = $data = [];
167 // First scan the entityDefaults directory for our list of supported entities
168 // FIXME: Need a way to load this from other extensions too
169 foreach (glob(__DIR__
. '/ang/afGuiEditor/entityDefaults/*.json') as $file) {
171 preg_match('/([-a-z_A-Z0-9]*).json/', $file, $matches);
172 $entityWhitelist[] = $entity = $matches[1];
173 // No json_decode, the files are not strict json and will go through angular.$parse clientside
174 $data['defaults'][$entity] = trim(CRM_Utils_JS
::stripComments(file_get_contents($file)));
177 // Load main entities
178 $data['entities'] = (array) Civi\Api4\Entity
::get()
179 ->setCheckPermissions(FALSE)
180 ->setSelect(['name', 'description'])
181 ->addWhere('name', 'IN', $entityWhitelist)
185 $data['blocks'] = [];
186 $blockData = \Civi\Api4\Afform
::get()
187 ->setCheckPermissions(FALSE)
188 ->addWhere('block', 'IS NOT NULL')
189 ->setSelect(['name', 'title', 'block', 'join', 'layout', 'repeat'])
190 ->setFormatWhitespace(TRUE)
191 ->setLayoutFormat('shallow')
193 foreach ($blockData as $block) {
194 if (!empty($block['join']) && !in_array($block['join'], $entityWhitelist)) {
195 $entityWhitelist[] = $block['join'];
197 $data['blocks'][_afform_angular_module_name($block['name'], 'dash')] = $block;
200 // Todo: scan for other elements
201 $data['elements'] = [
203 'title' => ts('Container'),
206 'class' => 'af-container',
211 'title' => ts('Text box'),
214 'class' => 'af-text',
216 ['#text' => ts('Enter text')],
221 'title' => ts('Rich content'),
224 'class' => 'af-markup',
229 'title' => ts('Button'),
232 'class' => 'af-button btn-primary',
233 'crm-icon' => 'fa-check',
234 'ng-click' => 'afform.submit()',
236 ['#text' => ts('Enter text')],
243 'checkPermissions' => FALSE,
244 'includeCustom' => TRUE,
245 'loadOptions' => TRUE,
246 'action' => 'create',
247 'select' => ['name', 'title', 'input_type', 'input_attrs', 'required', 'options', 'help_pre', 'help_post', 'serialize', 'data_type'],
248 'where' => [['input_type', 'IS NOT NULL']],
251 // Get fields for main entities + joined entities
252 foreach (array_unique($entityWhitelist) as $entityName) {
253 $data['fields'][$entityName] = (array) civicrm_api4($entityName, 'getFields', $getFieldParams, 'name');
255 // TODO: Teach the api to return options in this format
256 foreach ($data['fields'][$entityName] as $name => $field) {
257 if (!empty($field['options'])) {
258 $data['fields'][$entityName][$name]['options'] = CRM_Utils_Array
::makeNonAssociative($field['options'], 'key', 'label');
261 unset($data['fields'][$entityName][$name]['options']);
266 // Now adjust the field metadata
267 // FIXME: This should probably be a callback event or something to allow extensions to tweak the metadata for their entities
268 $data['fields']['Contact']['contact_type']['required_data'] = TRUE;
270 // Scan for input types
271 // FIXME: Need a way to load this from other extensions too
272 foreach (glob(__DIR__
. '/ang/afGuiEditor/inputType/*.html') as $file) {
274 preg_match('/([-a-z_A-Z0-9]*).html/', $file, $matches);
275 $data['inputType'][$matches[1]] = $matches[1];
279 'default' => ts('Default'),
280 'primary' => ts('Primary'),
281 'success' => ts('Success'),
282 'info' => ts('Info'),
283 'warning' => ts('Warning'),
284 'danger' => ts('Danger'),
287 $data['permissions'] = [];
288 foreach (CRM_Core_Permission
::basicPermissions(TRUE, TRUE) as $name => $perm) {
289 $data['permissions'][] = [
292 'description' => $perm[1] ??
NULL,
296 $mimeType = 'text/javascript';
297 $content = "CRM.afformAdminData=" . json_encode($data, JSON_UNESCAPED_SLASHES
) . ';';