3 require_once 'afform.civix.php';
4 use CRM_Afform_ExtensionUtil
as E
;
7 * Filter the content of $params to only have supported afform fields.
12 function _afform_fields_filter($params) {
14 $fields = \Civi\Api4\Afform
::getfields(FALSE)->setAction('create')->execute()->indexBy('name');
15 foreach ($fields as $fieldName => $field) {
16 if (isset($params[$fieldName])) {
17 $result[$fieldName] = $params[$fieldName];
19 if ($field['data_type'] === 'Boolean' && !is_bool($params[$fieldName])) {
20 $result[$fieldName] = CRM_Utils_String
::strtobool($params[$fieldName]);
28 * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
30 function afform_civicrm_container($container) {
31 $container->addResource(new \Symfony\Component\Config\
Resource\
FileResource(__FILE__
));
32 $container->setDefinition('afform_scanner', new \Symfony\Component\DependencyInjection\
Definition(
33 'CRM_Afform_AfformScanner',
39 * Implements hook_civicrm_config().
41 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_config
43 function afform_civicrm_config(&$config) {
44 _afform_civix_civicrm_config($config);
46 if (isset(Civi
::$statics[__FUNCTION__
])) {
49 Civi
::$statics[__FUNCTION__
] = 1;
51 $dispatcher = Civi
::dispatcher();
52 $dispatcher->addListener('civi.afform.submit', ['\Civi\Api4\Action\Afform\Submit', 'processGenericEntity'], 0);
53 $dispatcher->addListener('civi.afform.submit', ['\Civi\Api4\Action\Afform\Submit', 'preprocessContact'], 10);
54 $dispatcher->addListener('hook_civicrm_angularModules', ['\Civi\Afform\AngularDependencyMapper', 'autoReq'], -1000);
55 $dispatcher->addListener('hook_civicrm_alterAngular', ['\Civi\Afform\AfformMetadataInjector', 'preprocess']);
56 $dispatcher->addListener('hook_civicrm_check', ['\Civi\Afform\StatusChecks', 'hook_civicrm_check']);
58 // Register support for email tokens
59 if (CRM_Extension_System
::singleton()->getMapper()->isActiveModule('authx')) {
60 $dispatcher->addListener('hook_civicrm_alterMailContent', ['\Civi\Afform\Tokens', 'applyCkeditorWorkaround']);
61 $dispatcher->addListener('hook_civicrm_tokens', ['\Civi\Afform\Tokens', 'hook_civicrm_tokens']);
62 $dispatcher->addListener('hook_civicrm_tokenValues', ['\Civi\Afform\Tokens', 'hook_civicrm_tokenValues']);
67 * Implements hook_civicrm_xmlMenu().
69 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_xmlMenu
71 function afform_civicrm_xmlMenu(&$files) {
72 _afform_civix_civicrm_xmlMenu($files);
76 * Implements hook_civicrm_install().
78 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_install
80 function afform_civicrm_install() {
81 _afform_civix_civicrm_install();
85 * Implements hook_civicrm_postInstall().
87 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_postInstall
89 function afform_civicrm_postInstall() {
90 _afform_civix_civicrm_postInstall();
94 * Implements hook_civicrm_uninstall().
96 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_uninstall
98 function afform_civicrm_uninstall() {
99 _afform_civix_civicrm_uninstall();
103 * Implements hook_civicrm_enable().
105 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_enable
107 function afform_civicrm_enable() {
108 _afform_civix_civicrm_enable();
112 * Implements hook_civicrm_disable().
114 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_disable
116 function afform_civicrm_disable() {
117 _afform_civix_civicrm_disable();
121 * Implements hook_civicrm_upgrade().
123 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_upgrade
125 function afform_civicrm_upgrade($op, CRM_Queue_Queue
$queue = NULL) {
126 return _afform_civix_civicrm_upgrade($op, $queue);
130 * Implements hook_civicrm_managed().
132 * Generate a list of entities to create/deactivate/delete when this module
133 * is installed, disabled, uninstalled.
135 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_managed
137 function afform_civicrm_managed(&$entities) {
138 _afform_civix_civicrm_managed($entities);
140 /** @var \CRM_Afform_AfformScanner $scanner */
141 if (\Civi
::container()->has('afform_scanner')) {
142 $scanner = \Civi
::service('afform_scanner');
145 // This might happen at oddballs points - e.g. while you're in the middle of re-enabling the ext.
146 // This AfformScanner instance only lives during this method call, and it feeds off the regular cache.
147 $scanner = new CRM_Afform_AfformScanner();
150 foreach ($scanner->getMetas() as $afform) {
151 if (empty($afform['is_dashlet']) ||
empty($afform['name'])) {
155 'module' => E
::LONG_NAME
,
156 'name' => 'afform_dashlet_' . $afform['name'],
157 'entity' => 'Dashboard',
158 'update' => 'always',
159 // ideal cleanup policy might be to (a) deactivate if used and (b) remove if unused
160 'cleanup' => 'always',
163 // Q: Should we loop through all domains?
164 'domain_id' => CRM_Core_BAO_Domain
::getDomain()->id
,
166 'name' => $afform['name'],
167 'label' => $afform['title'] ?? E
::ts('(Untitled)'),
168 'directive' => _afform_angular_module_name($afform['name'], 'dash'),
169 'permission' => "@afform:" . $afform['name'],
176 * Implements hook_civicrm_tabset().
178 * Adds afforms as contact summary tabs.
180 function afform_civicrm_tabset($tabsetName, &$tabs, $context) {
181 if ($tabsetName !== 'civicrm/contact/view') {
184 $scanner = \Civi
::service('afform_scanner');
186 foreach ($scanner->getMetas() as $afform) {
187 if (!empty($afform['contact_summary']) && $afform['contact_summary'] === 'tab') {
188 $module = _afform_angular_module_name($afform['name']);
190 'id' => $afform['name'],
191 'title' => $afform['title'],
192 'weight' => $weight++
,
193 'icon' => 'crm-i fa-list-alt',
195 'template' => 'afform/contactSummary/AfformTab.tpl',
197 'directive' => _afform_angular_module_name($afform['name'], 'dash'),
199 // If this is the real contact summary page (and not a callback from ContactLayoutEditor), load module.
200 if (empty($context['caller'])) {
201 Civi
::service('angularjs.loader')->addModules($module);
208 * Implements hook_civicrm_pageRun().
210 * Adds afforms as contact summary blocks.
212 function afform_civicrm_pageRun(&$page) {
213 if (get_class($page) !== 'CRM_Contact_Page_View_Summary') {
216 $scanner = \Civi
::service('afform_scanner');
217 $cid = $page->get('cid');
219 foreach ($scanner->getMetas() as $afform) {
220 if (!empty($afform['contact_summary']) && $afform['contact_summary'] === 'block') {
221 $module = _afform_angular_module_name($afform['name']);
224 'directive' => _afform_angular_module_name($afform['name'], 'dash'),
226 $content = CRM_Core_Smarty
::singleton()->fetchWith('afform/contactSummary/AfformBlock.tpl', ['contactId' => $cid, 'block' => $block]);
227 CRM_Core_Region
::instance("contact-basic-info-$side")->add([
228 'markup' => '<div class="crm-summary-block">' . $content . '</div>',
231 Civi
::service('angularjs.loader')->addModules($module);
232 $side = $side === 'left' ?
'right' : 'left';
238 * Implements hook_civicrm_contactSummaryBlocks().
240 * @link https://github.com/civicrm/org.civicrm.contactlayout
242 function afform_civicrm_contactSummaryBlocks(&$blocks) {
243 $afforms = \Civi\Api4\Afform
::get(FALSE)
244 ->setSelect(['name', 'title', 'directive_name', 'module_name', 'type', 'type:icon', 'type:label'])
245 ->addWhere('contact_summary', '=', 'block')
247 foreach ($afforms as $index => $afform) {
248 // Create a group per afform type
250 "afform_{$afform['type']}" => [
251 'title' => $afform['type:label'],
252 'icon' => $afform['type:icon'],
256 $blocks["afform_{$afform['type']}"]['blocks'][$afform['name']] = [
257 'title' => $afform['title'],
258 'tpl_file' => 'afform/contactSummary/AfformBlock.tpl',
259 'module' => $afform['module_name'],
260 'directive' => $afform['directive_name'],
262 $afform['type:label'],
264 'edit' => 'civicrm/admin/afform#/edit/' . $afform['name'],
265 'system_default' => [0, $index %
2],
271 * Implements hook_civicrm_angularModules().
273 * Generate a list of Afform Angular modules.
275 function afform_civicrm_angularModules(&$angularModules) {
276 _afform_civix_civicrm_angularModules($angularModules);
278 $afforms = \Civi\Api4\Afform
::get(FALSE)
279 ->setSelect(['name', 'requires', 'module_name', 'directive_name'])
282 foreach ($afforms as $afform) {
283 $angularModules[$afform['module_name']] = [
284 'ext' => E
::LONG_NAME
,
285 'js' => ['assetBuilder://afform.js?name=' . urlencode($afform['name'])],
286 'requires' => $afform['requires'],
288 'partialsCallback' => '_afform_get_partials',
289 '_afform' => $afform['name'],
290 // TODO: Allow afforms to declare their own theming requirements
291 'bundles' => ['bootstrap3'],
293 $afform['directive_name'] => 'E',
300 * Callback to retrieve partials for a given afform/angular module.
302 * @see afform_civicrm_angularModules
304 * @param string $moduleName
306 * @param array $module
307 * The module definition.
309 * Array(string $filename => string $html).
310 * @throws API_Exception
312 function _afform_get_partials($moduleName, $module) {
313 $afform = civicrm_api4('Afform', 'get', [
314 'where' => [['name', '=', $module['_afform']]],
315 'select' => ['layout'],
316 'layoutFormat' => 'html',
317 'checkPermissions' => FALSE,
320 "~/$moduleName/$moduleName.aff.html" => $afform['layout'],
325 * Implements hook_civicrm_alterSettingsFolders().
327 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_alterSettingsFolders
329 function afform_civicrm_alterSettingsFolders(&$metaDataFolders = NULL) {
330 _afform_civix_civicrm_alterSettingsFolders($metaDataFolders);
334 * Implements hook_civicrm_entityTypes().
336 * Declare entity types provided by this module.
338 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_entityTypes
340 function afform_civicrm_entityTypes(&$entityTypes) {
341 _afform_civix_civicrm_entityTypes($entityTypes);
345 * Implements hook_civicrm_themes().
347 function afform_civicrm_themes(&$themes) {
348 _afform_civix_civicrm_themes($themes);
352 * Implements hook_civicrm_buildAsset().
354 function afform_civicrm_buildAsset($asset, $params, &$mimeType, &$content) {
355 if ($asset !== 'afform.js') {
359 if (empty($params['name'])) {
360 throw new RuntimeException("Missing required parameter: afform.js?name=NAME");
363 $moduleName = _afform_angular_module_name($params['name'], 'camel');
364 $formMetaData = (array) civicrm_api4('Afform', 'get', [
365 'checkPermissions' => FALSE,
366 'select' => ['redirect', 'name'],
367 'where' => [['name', '=', $params['name']]],
369 $smarty = CRM_Core_Smarty
::singleton();
370 $smarty->assign('afform', [
371 'camel' => $moduleName,
372 'meta' => $formMetaData,
373 'templateUrl' => "~/$moduleName/$moduleName.aff.html",
375 $mimeType = 'text/javascript';
376 $content = $smarty->fetch('afform/AfformAngularModule.tpl');
380 * Implements hook_civicrm_alterMenu().
382 function afform_civicrm_alterMenu(&$items) {
383 if (Civi
::container()->has('afform_scanner')) {
384 $scanner = Civi
::service('afform_scanner');
387 // During installation...
388 $scanner = new CRM_Afform_AfformScanner();
390 foreach ($scanner->getMetas() as $name => $meta) {
391 if (!empty($meta['server_route'])) {
392 $items[$meta['server_route']] = [
393 'page_callback' => 'CRM_Afform_Page_AfformBase',
394 'page_arguments' => 'afform=' . urlencode($name),
395 'title' => $meta['title'] ??
'',
396 'access_arguments' => [["@afform:$name"], 'and'],
397 'is_public' => $meta['is_public'],
404 * Implements hook_civicrm_permission_check().
406 * This extends the list of permissions available in `CRM_Core_Permission:check()`
407 * by introducing virtual-permissions named `@afform:myForm`. The evaluation
408 * of these virtual-permissions is dependent on the settings for `myForm`.
409 * `myForm` may be exposed/integrated through multiple subsystems (routing,
410 * nav-menu, API, etc), and the use of virtual-permissions makes easy to enforce
411 * consistent permissions across any relevant subsystems.
413 * @see CRM_Utils_Hook::permission_check()
415 function afform_civicrm_permission_check($permission, &$granted, $contactId) {
416 if ($permission[0] !== '@') {
417 // Micro-optimization - this function may get hit a lot.
421 if (preg_match('/^@afform:(.*)/', $permission, $m)) {
424 $afform = \Civi\Api4\Afform
::get()
425 ->setCheckPermissions(FALSE)
426 ->addWhere('name', '=', $name)
427 ->setSelect(['permission'])
431 $granted = CRM_Core_Permission
::check($afform['permission'], $contactId);
437 * Implements hook_civicrm_permissionList().
439 * @see CRM_Utils_Hook::permissionList()
441 function afform_civicrm_permissionList(&$permissions) {
442 $scanner = Civi
::service('afform_scanner');
443 foreach ($scanner->getMetas() as $name => $meta) {
444 $permissions['@afform:' . $name] = [
446 'title' => E
::ts('Afform: Inherit permission of %1', [
454 * Clear any local/in-memory caches based on afform data.
456 function _afform_clear() {
457 $container = \Civi
::container();
458 $container->get('afform_scanner')->clear();
459 $container->get('angular')->clear();
463 * @param string $fileBaseName
465 * @param string $format
468 * Ex: 'FooBar' or 'foo-bar'.
471 function _afform_angular_module_name($fileBaseName, $format = 'camel') {
475 foreach (preg_split('/[-_ ]/', $fileBaseName, NULL, PREG_SPLIT_NO_EMPTY
) as $shortNamePart) {
476 $camelCase .= ucfirst($shortNamePart);
478 return strtolower($camelCase[0]) . substr($camelCase, 1);
481 return strtolower(implode('-', preg_split('/[-_ ]|(?=[A-Z])/', $fileBaseName, NULL, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE
)));
484 throw new \
Exception("Unrecognized format");
489 * Implements hook_civicrm_alterApiRoutePermissions().
491 * @see CRM_Utils_Hook::alterApiRoutePermissions
493 function afform_civicrm_alterApiRoutePermissions(&$permissions, $entity, $action) {
494 if ($entity == 'Afform') {
495 // These actions should be accessible to anonymous users; permissions are checked internally
496 $allowedActions = ['prefill', 'submit', 'submitFile', 'getOptions'];
497 if (in_array($action, $allowedActions, TRUE)) {
498 $permissions = CRM_Core_Permission
::ALWAYS_ALLOW_PERMISSION
;
504 * Implements hook_civicrm_preProcess().
506 * Wordpress only: Adds Afforms to the shortcode dialog (when editing pages/posts).
508 function afform_civicrm_preProcess($formName, &$form) {
509 if ($formName === 'CRM_Core_Form_ShortCode') {
510 $form->components
['afform'] = [
511 'label' => E
::ts('Form Builder'),
514 'entity' => 'Afform',
515 'select' => ['minimumInputLength' => 0],
517 'params' => ['type' => ['IN' => ['form', 'search']]],
525 * Implements hook_civicrm_pre().
527 function afform_civicrm_pre($op, $entity, $id, &$params) {
528 // When deleting a searchDisplay, also delete any Afforms the display is embedded within
529 if ($entity === 'SearchDisplay' && $op === 'delete') {
530 $display = \Civi\Api4\SearchDisplay
::get(FALSE)
531 ->addSelect('saved_search_id.name', 'name')
532 ->addWhere('id', '=', $id)
533 ->execute()->first();
534 \Civi\Api4\Afform
::revert(FALSE)
535 ->addWhere('search_displays', 'CONTAINS', $display['saved_search_id.name'] . ".{$display['name']}")
538 // When deleting a savedSearch, delete any Afforms which use the default display
539 if ($entity === 'SearchDisplay' && $op === 'delete') {
540 $search = \Civi\Api4\SavedSearch
::get(FALSE)
542 ->addWhere('id', '=', $id)
543 ->execute()->first();
544 \Civi\Api4\Afform
::revert(FALSE)
545 ->addWhere('search_displays', 'CONTAINS', $search['name'])
551 * Implements hook_civicrm_referenceCounts().
553 function afform_civicrm_referenceCounts($dao, &$counts) {
554 // Count afforms which contain a search display
555 if (is_a($dao, 'CRM_Search_DAO_SearchDisplay') && $dao->id
) {
556 if (empty($dao->saved_search_id
) ||
empty($dao->name
)) {
559 $search = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_SavedSearch', $dao->saved_search_id
);
560 $afforms = \Civi\Api4\Afform
::get(FALSE)
562 ->addWhere('search_displays', 'CONTAINS', "$search.$dao->name")
564 if ($afforms->count()) {
568 'count' => $afforms->count(),
572 // Count afforms which contain any displays from a SavedSearch (including the default display)
573 elseif (is_a($dao, 'CRM_Contact_DAO_SavedSearch') && $dao->id
) {
574 if (empty($dao->name
)) {
578 ['search_displays', 'CONTAINS', $dao->name
],
581 $displays = civicrm_api4('SearchDisplay', 'get', [
582 'where' => [['saved_search_id', '=', $dao->id
]],
585 foreach ($displays as $displayName) {
586 $clauses[] = ['search_displays', 'CONTAINS', $dao->name
. '.' . $displayName];
589 catch (Exception
$e) {
590 // In case SearchKit is not installed, the api call would fail
592 $afforms = \Civi\Api4\Afform
::get(FALSE)
594 ->addClause('OR', $clauses)
596 if ($afforms->count()) {
600 'count' => $afforms->count(),
606 // Wordpress only: Register callback for rendering shortcodes
607 if (function_exists('add_filter')) {
608 add_filter('civicrm_shortcode_get_markup', 'afform_shortcode_content', 10, 4);
612 * Wordpress only: Render Afform content for shortcodes.
614 * @param string $content
617 * Shortcode attributes.
619 * Existing shortcode arguments.
620 * @param string $context
621 * How many shortcodes are present on the page: 'single' or 'multiple'.
625 function afform_shortcode_content($content, $atts, $args, $context) {
626 if ($atts['component'] === 'afform') {
627 $afform = civicrm_api4('Afform', 'get', [
628 'select' => ['directive_name', 'module_name'],
629 'where' => [['name', '=', $atts['name']]],
632 Civi
::service('angularjs.loader')->addModules($afform['module_name']);
634 <div class='crm-container' id='bootstrap-theme'>
635 <crm-angular-js modules='{$afform['module_name']}'>
636 <{$afform['directive_name']}></{$afform['directive_name']}>