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_install().
69 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_install
71 function afform_civicrm_install() {
72 _afform_civix_civicrm_install();
76 * Implements hook_civicrm_postInstall().
78 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_postInstall
80 function afform_civicrm_postInstall() {
81 _afform_civix_civicrm_postInstall();
85 * Implements hook_civicrm_uninstall().
87 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_uninstall
89 function afform_civicrm_uninstall() {
90 _afform_civix_civicrm_uninstall();
94 * Implements hook_civicrm_enable().
96 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_enable
98 function afform_civicrm_enable() {
99 _afform_civix_civicrm_enable();
103 * Implements hook_civicrm_disable().
105 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_disable
107 function afform_civicrm_disable() {
108 _afform_civix_civicrm_disable();
112 * Implements hook_civicrm_upgrade().
114 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_upgrade
116 function afform_civicrm_upgrade($op, CRM_Queue_Queue
$queue = NULL) {
117 return _afform_civix_civicrm_upgrade($op, $queue);
121 * Implements hook_civicrm_managed().
123 * Generate a list of entities to create/deactivate/delete when this module
124 * is installed, disabled, uninstalled.
126 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_managed
128 function afform_civicrm_managed(&$entities) {
129 /** @var \CRM_Afform_AfformScanner $scanner */
130 if (\Civi
::container()->has('afform_scanner')) {
131 $scanner = \Civi
::service('afform_scanner');
134 // This might happen at oddballs points - e.g. while you're in the middle of re-enabling the ext.
135 // This AfformScanner instance only lives during this method call, and it feeds off the regular cache.
136 $scanner = new CRM_Afform_AfformScanner();
139 foreach ($scanner->getMetas() as $afform) {
140 if (empty($afform['is_dashlet']) ||
empty($afform['name'])) {
144 'module' => E
::LONG_NAME
,
145 'name' => 'afform_dashlet_' . $afform['name'],
146 'entity' => 'Dashboard',
147 'update' => 'always',
148 // ideal cleanup policy might be to (a) deactivate if used and (b) remove if unused
149 'cleanup' => 'always',
153 // Q: Should we loop through all domains?
154 'domain_id' => 'current_domain',
156 'name' => $afform['name'],
157 'label' => $afform['title'] ?? E
::ts('(Untitled)'),
158 'directive' => _afform_angular_module_name($afform['name'], 'dash'),
159 'permission' => "@afform:" . $afform['name'],
168 * Implements hook_civicrm_tabset().
170 * Adds afforms as contact summary tabs.
172 function afform_civicrm_tabset($tabsetName, &$tabs, $context) {
173 if ($tabsetName !== 'civicrm/contact/view') {
176 $scanner = \Civi
::service('afform_scanner');
178 foreach ($scanner->getMetas() as $afform) {
179 if (!empty($afform['contact_summary']) && $afform['contact_summary'] === 'tab') {
180 $module = _afform_angular_module_name($afform['name']);
182 'id' => $afform['name'],
183 'title' => $afform['title'],
184 'weight' => $weight++
,
185 'icon' => 'crm-i fa-list-alt',
187 'template' => 'afform/contactSummary/AfformTab.tpl',
189 'directive' => _afform_angular_module_name($afform['name'], 'dash'),
191 // If this is the real contact summary page (and not a callback from ContactLayoutEditor), load module.
192 if (empty($context['caller'])) {
193 Civi
::service('angularjs.loader')->addModules($module);
200 * Implements hook_civicrm_pageRun().
202 * Adds afforms as contact summary blocks.
204 function afform_civicrm_pageRun(&$page) {
205 if (get_class($page) !== 'CRM_Contact_Page_View_Summary') {
208 $scanner = \Civi
::service('afform_scanner');
209 $cid = $page->get('cid');
211 foreach ($scanner->getMetas() as $afform) {
212 if (!empty($afform['contact_summary']) && $afform['contact_summary'] === 'block') {
213 $module = _afform_angular_module_name($afform['name']);
216 'directive' => _afform_angular_module_name($afform['name'], 'dash'),
218 $content = CRM_Core_Smarty
::singleton()->fetchWith('afform/contactSummary/AfformBlock.tpl', ['contactId' => $cid, 'block' => $block]);
219 CRM_Core_Region
::instance("contact-basic-info-$side")->add([
220 'markup' => '<div class="crm-summary-block">' . $content . '</div>',
223 Civi
::service('angularjs.loader')->addModules($module);
224 $side = $side === 'left' ?
'right' : 'left';
230 * Implements hook_civicrm_contactSummaryBlocks().
232 * @link https://github.com/civicrm/org.civicrm.contactlayout
234 function afform_civicrm_contactSummaryBlocks(&$blocks) {
235 $afforms = \Civi\Api4\Afform
::get(FALSE)
236 ->setSelect(['name', 'title', 'directive_name', 'module_name', 'type', 'type:icon', 'type:label'])
237 ->addWhere('contact_summary', '=', 'block')
239 foreach ($afforms as $index => $afform) {
240 // Create a group per afform type
242 "afform_{$afform['type']}" => [
243 'title' => $afform['type:label'],
244 'icon' => $afform['type:icon'],
248 $blocks["afform_{$afform['type']}"]['blocks'][$afform['name']] = [
249 'title' => $afform['title'],
250 'tpl_file' => 'afform/contactSummary/AfformBlock.tpl',
251 'module' => $afform['module_name'],
252 'directive' => $afform['directive_name'],
254 $afform['type:label'],
256 'edit' => 'civicrm/admin/afform#/edit/' . $afform['name'],
257 'system_default' => [0, $index %
2],
263 * Implements hook_civicrm_angularModules().
265 * Generate a list of Afform Angular modules.
267 function afform_civicrm_angularModules(&$angularModules) {
268 $afforms = \Civi\Api4\Afform
::get(FALSE)
269 ->setSelect(['name', 'requires', 'module_name', 'directive_name'])
272 foreach ($afforms as $afform) {
273 $angularModules[$afform['module_name']] = [
274 'ext' => E
::LONG_NAME
,
275 'js' => ['assetBuilder://afform.js?name=' . urlencode($afform['name'])],
276 'requires' => $afform['requires'],
278 'partialsCallback' => '_afform_get_partials',
279 '_afform' => $afform['name'],
280 // TODO: Allow afforms to declare their own theming requirements
281 'bundles' => ['bootstrap3'],
283 $afform['directive_name'] => 'E',
290 * Callback to retrieve partials for a given afform/angular module.
292 * @see afform_civicrm_angularModules
294 * @param string $moduleName
296 * @param array $module
297 * The module definition.
299 * Array(string $filename => string $html).
300 * @throws API_Exception
302 function _afform_get_partials($moduleName, $module) {
303 $afform = civicrm_api4('Afform', 'get', [
304 'where' => [['name', '=', $module['_afform']]],
305 'select' => ['layout'],
306 'layoutFormat' => 'html',
307 'checkPermissions' => FALSE,
310 "~/$moduleName/$moduleName.aff.html" => $afform['layout'],
315 * Implements hook_civicrm_entityTypes().
317 * Declare entity types provided by this module.
319 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_entityTypes
321 function afform_civicrm_entityTypes(&$entityTypes) {
322 _afform_civix_civicrm_entityTypes($entityTypes);
326 * Implements hook_civicrm_buildAsset().
328 function afform_civicrm_buildAsset($asset, $params, &$mimeType, &$content) {
329 if ($asset !== 'afform.js') {
333 if (empty($params['name'])) {
334 throw new RuntimeException("Missing required parameter: afform.js?name=NAME");
337 $moduleName = _afform_angular_module_name($params['name'], 'camel');
338 $formMetaData = (array) civicrm_api4('Afform', 'get', [
339 'checkPermissions' => FALSE,
340 'select' => ['redirect', 'name', 'title'],
341 'where' => [['name', '=', $params['name']]],
343 $smarty = CRM_Core_Smarty
::singleton();
344 $smarty->assign('afform', [
345 'camel' => $moduleName,
346 'meta' => $formMetaData,
347 'templateUrl' => "~/$moduleName/$moduleName.aff.html",
349 $mimeType = 'text/javascript';
350 $content = $smarty->fetch('afform/AfformAngularModule.tpl');
354 * Implements hook_civicrm_alterMenu().
356 function afform_civicrm_alterMenu(&$items) {
357 if (Civi
::container()->has('afform_scanner')) {
358 $scanner = Civi
::service('afform_scanner');
361 // During installation...
362 $scanner = new CRM_Afform_AfformScanner();
364 foreach ($scanner->getMetas() as $name => $meta) {
365 if (!empty($meta['server_route'])) {
366 $items[$meta['server_route']] = [
367 'page_callback' => 'CRM_Afform_Page_AfformBase',
368 'page_arguments' => 'afform=' . urlencode($name),
369 'access_arguments' => [["@afform:$name"], 'and'],
370 'is_public' => $meta['is_public'],
377 * Implements hook_civicrm_permission_check().
379 * This extends the list of permissions available in `CRM_Core_Permission:check()`
380 * by introducing virtual-permissions named `@afform:myForm`. The evaluation
381 * of these virtual-permissions is dependent on the settings for `myForm`.
382 * `myForm` may be exposed/integrated through multiple subsystems (routing,
383 * nav-menu, API, etc), and the use of virtual-permissions makes easy to enforce
384 * consistent permissions across any relevant subsystems.
386 * @see CRM_Utils_Hook::permission_check()
388 function afform_civicrm_permission_check($permission, &$granted, $contactId) {
389 if ($permission[0] !== '@') {
390 // Micro-optimization - this function may get hit a lot.
394 if (preg_match('/^@afform:(.*)/', $permission, $m)) {
397 $afform = \Civi\Api4\Afform
::get()
398 ->setCheckPermissions(FALSE)
399 ->addWhere('name', '=', $name)
400 ->setSelect(['permission'])
404 $granted = CRM_Core_Permission
::check($afform['permission'], $contactId);
410 * Implements hook_civicrm_permissionList().
412 * @see CRM_Utils_Hook::permissionList()
414 function afform_civicrm_permissionList(&$permissions) {
415 $scanner = Civi
::service('afform_scanner');
416 foreach ($scanner->getMetas() as $name => $meta) {
417 $permissions['@afform:' . $name] = [
419 'title' => E
::ts('Afform: Inherit permission of %1', [
427 * Clear any local/in-memory caches based on afform data.
429 function _afform_clear() {
430 $container = \Civi
::container();
431 $container->get('afform_scanner')->clear();
432 $container->get('angular')->clear();
436 * @param string $fileBaseName
438 * @param string $format
441 * Ex: 'FooBar' or 'foo-bar'.
444 function _afform_angular_module_name($fileBaseName, $format = 'camel') {
448 foreach (preg_split('/[-_ ]/', $fileBaseName, NULL, PREG_SPLIT_NO_EMPTY
) as $shortNamePart) {
449 $camelCase .= ucfirst($shortNamePart);
451 return strtolower($camelCase[0]) . substr($camelCase, 1);
454 return strtolower(implode('-', preg_split('/[-_ ]|(?=[A-Z])/', $fileBaseName, NULL, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE
)));
457 throw new \
Exception("Unrecognized format");
462 * Implements hook_civicrm_alterApiRoutePermissions().
464 * @see CRM_Utils_Hook::alterApiRoutePermissions
466 function afform_civicrm_alterApiRoutePermissions(&$permissions, $entity, $action) {
467 if ($entity == 'Afform') {
468 // These actions should be accessible to anonymous users; permissions are checked internally
469 $allowedActions = ['prefill', 'submit', 'submitFile', 'getOptions'];
470 if (in_array($action, $allowedActions, TRUE)) {
471 $permissions = CRM_Core_Permission
::ALWAYS_ALLOW_PERMISSION
;
477 * Implements hook_civicrm_preProcess().
479 * Wordpress only: Adds Afforms to the shortcode dialog (when editing pages/posts).
481 function afform_civicrm_preProcess($formName, &$form) {
482 if ($formName === 'CRM_Core_Form_ShortCode') {
483 $form->components
['afform'] = [
484 'label' => E
::ts('Form Builder'),
487 'entity' => 'Afform',
488 'select' => ['minimumInputLength' => 0],
490 'params' => ['type' => ['IN' => ['form', 'search']]],
498 * Implements hook_civicrm_pre().
500 function afform_civicrm_pre($op, $entity, $id, &$params) {
501 // When deleting a searchDisplay, also delete any Afforms the display is embedded within
502 if ($entity === 'SearchDisplay' && $op === 'delete') {
503 $display = \Civi\Api4\SearchDisplay
::get(FALSE)
504 ->addSelect('saved_search_id.name', 'name')
505 ->addWhere('id', '=', $id)
506 ->execute()->first();
507 \Civi\Api4\Afform
::revert(FALSE)
508 ->addWhere('search_displays', 'CONTAINS', $display['saved_search_id.name'] . ".{$display['name']}")
511 // When deleting a savedSearch, delete any Afforms which use the default display
512 elseif ($entity === 'SavedSearch' && $op === 'delete') {
513 $search = \Civi\Api4\SavedSearch
::get(FALSE)
515 ->addWhere('id', '=', $id)
516 ->execute()->first();
517 \Civi\Api4\Afform
::revert(FALSE)
518 ->addWhere('search_displays', 'CONTAINS', $search['name'])
524 * Implements hook_civicrm_referenceCounts().
526 function afform_civicrm_referenceCounts($dao, &$counts) {
527 // Count afforms which contain a search display
528 if (is_a($dao, 'CRM_Search_DAO_SearchDisplay') && $dao->id
) {
529 if (empty($dao->saved_search_id
) ||
empty($dao->name
)) {
532 $search = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_SavedSearch', $dao->saved_search_id
);
533 $afforms = \Civi\Api4\Afform
::get(FALSE)
535 ->addWhere('search_displays', 'CONTAINS', "$search.$dao->name")
537 if ($afforms->count()) {
541 'count' => $afforms->count(),
545 // Count afforms which contain any displays from a SavedSearch (including the default display)
546 elseif (is_a($dao, 'CRM_Contact_DAO_SavedSearch') && $dao->id
) {
547 if (empty($dao->name
)) {
551 ['search_displays', 'CONTAINS', $dao->name
],
554 $displays = civicrm_api4('SearchDisplay', 'get', [
555 'where' => [['saved_search_id', '=', $dao->id
]],
558 foreach ($displays as $displayName) {
559 $clauses[] = ['search_displays', 'CONTAINS', $dao->name
. '.' . $displayName];
562 catch (Exception
$e) {
563 // In case SearchKit is not installed, the api call would fail
565 $afforms = \Civi\Api4\Afform
::get(FALSE)
567 ->addClause('OR', $clauses)
569 if ($afforms->count()) {
573 'count' => $afforms->count(),
579 // Wordpress only: Register callback for rendering shortcodes
580 if (function_exists('add_filter')) {
581 add_filter('civicrm_shortcode_get_markup', 'afform_shortcode_content', 10, 4);
585 * Wordpress only: Render Afform content for shortcodes.
587 * @param string $content
590 * Shortcode attributes.
592 * Existing shortcode arguments.
593 * @param string $context
594 * How many shortcodes are present on the page: 'single' or 'multiple'.
598 function afform_shortcode_content($content, $atts, $args, $context) {
599 if ($atts['component'] === 'afform') {
600 $afform = civicrm_api4('Afform', 'get', [
601 'select' => ['directive_name', 'module_name'],
602 'where' => [['name', '=', $atts['name']]],
605 Civi
::service('angularjs.loader')->addModules($afform['module_name']);
607 <div class='crm-container' id='bootstrap-theme'>
608 <crm-angular-js modules='{$afform['module_name']}'>
609 <{$afform['directive_name']}></{$afform['directive_name']}>