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('civi.afform.submit', ['\Civi\Api4\Action\Afform\Submit', 'processRelationships'], 1);
55 $dispatcher->addListener('hook_civicrm_angularModules', ['\Civi\Afform\AngularDependencyMapper', 'autoReq'], -1000);
56 $dispatcher->addListener('hook_civicrm_alterAngular', ['\Civi\Afform\AfformMetadataInjector', 'preprocess']);
57 $dispatcher->addListener('hook_civicrm_check', ['\Civi\Afform\StatusChecks', 'hook_civicrm_check']);
59 // Register support for email tokens
60 if (CRM_Extension_System
::singleton()->getMapper()->isActiveModule('authx')) {
61 $dispatcher->addListener('hook_civicrm_alterMailContent', ['\Civi\Afform\Tokens', 'applyCkeditorWorkaround']);
62 $dispatcher->addListener('hook_civicrm_tokens', ['\Civi\Afform\Tokens', 'hook_civicrm_tokens']);
63 $dispatcher->addListener('hook_civicrm_tokenValues', ['\Civi\Afform\Tokens', 'hook_civicrm_tokenValues']);
68 * Implements hook_civicrm_install().
70 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_install
72 function afform_civicrm_install() {
73 _afform_civix_civicrm_install();
77 * Implements hook_civicrm_postInstall().
79 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_postInstall
81 function afform_civicrm_postInstall() {
82 _afform_civix_civicrm_postInstall();
86 * Implements hook_civicrm_uninstall().
88 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_uninstall
90 function afform_civicrm_uninstall() {
91 _afform_civix_civicrm_uninstall();
95 * Implements hook_civicrm_enable().
97 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_enable
99 function afform_civicrm_enable() {
100 _afform_civix_civicrm_enable();
104 * Implements hook_civicrm_disable().
106 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_disable
108 function afform_civicrm_disable() {
109 _afform_civix_civicrm_disable();
113 * Implements hook_civicrm_upgrade().
115 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_upgrade
117 function afform_civicrm_upgrade($op, CRM_Queue_Queue
$queue = NULL) {
118 return _afform_civix_civicrm_upgrade($op, $queue);
122 * Implements hook_civicrm_managed().
124 * Generate a list of entities to create/deactivate/delete when this module
125 * is installed, disabled, uninstalled.
127 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_managed
129 function afform_civicrm_managed(&$entities, $modules) {
130 if ($modules && !in_array(E
::LONG_NAME
, $modules, TRUE)) {
133 /** @var \CRM_Afform_AfformScanner $scanner */
134 if (\Civi
::container()->has('afform_scanner')) {
135 $scanner = \Civi
::service('afform_scanner');
138 // This might happen at oddballs points - e.g. while you're in the middle of re-enabling the ext.
139 // This AfformScanner instance only lives during this method call, and it feeds off the regular cache.
140 $scanner = new CRM_Afform_AfformScanner();
143 foreach ($scanner->getMetas() as $afform) {
144 if (empty($afform['is_dashlet']) ||
empty($afform['name'])) {
148 'module' => E
::LONG_NAME
,
149 'name' => 'afform_dashlet_' . $afform['name'],
150 'entity' => 'Dashboard',
151 'update' => 'always',
152 // ideal cleanup policy might be to (a) deactivate if used and (b) remove if unused
153 'cleanup' => 'always',
157 // Q: Should we loop through all domains?
158 'domain_id' => 'current_domain',
160 'name' => $afform['name'],
161 'label' => $afform['title'] ?? E
::ts('(Untitled)'),
162 'directive' => _afform_angular_module_name($afform['name'], 'dash'),
163 'permission' => "@afform:" . $afform['name'],
172 * Implements hook_civicrm_tabset().
174 * Adds afforms as contact summary tabs.
176 function afform_civicrm_tabset($tabsetName, &$tabs, $context) {
177 if ($tabsetName !== 'civicrm/contact/view') {
180 $scanner = \Civi
::service('afform_scanner');
182 foreach ($scanner->getMetas() as $afform) {
183 if (!empty($afform['contact_summary']) && $afform['contact_summary'] === 'tab') {
184 $module = _afform_angular_module_name($afform['name']);
186 'id' => $afform['name'],
187 'title' => $afform['title'],
188 'weight' => $weight++
,
189 'icon' => 'crm-i fa-list-alt',
191 'template' => 'afform/contactSummary/AfformTab.tpl',
193 'directive' => _afform_angular_module_name($afform['name'], 'dash'),
195 // If this is the real contact summary page (and not a callback from ContactLayoutEditor), load module.
196 if (empty($context['caller'])) {
197 Civi
::service('angularjs.loader')->addModules($module);
204 * Implements hook_civicrm_pageRun().
206 * Adds afforms as contact summary blocks.
208 function afform_civicrm_pageRun(&$page) {
209 if (get_class($page) !== 'CRM_Contact_Page_View_Summary') {
212 $scanner = \Civi
::service('afform_scanner');
213 $cid = $page->get('cid');
215 foreach ($scanner->getMetas() as $afform) {
216 if (!empty($afform['contact_summary']) && $afform['contact_summary'] === 'block') {
217 $module = _afform_angular_module_name($afform['name']);
220 'directive' => _afform_angular_module_name($afform['name'], 'dash'),
222 $content = CRM_Core_Smarty
::singleton()->fetchWith('afform/contactSummary/AfformBlock.tpl', ['contactId' => $cid, 'block' => $block]);
223 CRM_Core_Region
::instance("contact-basic-info-$side")->add([
224 'markup' => '<div class="crm-summary-block">' . $content . '</div>',
227 Civi
::service('angularjs.loader')->addModules($module);
228 $side = $side === 'left' ?
'right' : 'left';
234 * Implements hook_civicrm_contactSummaryBlocks().
236 * @link https://github.com/civicrm/org.civicrm.contactlayout
238 function afform_civicrm_contactSummaryBlocks(&$blocks) {
239 $afforms = \Civi\Api4\Afform
::get(FALSE)
240 ->setSelect(['name', 'title', 'directive_name', 'module_name', 'type', 'type:icon', 'type:label'])
241 ->addWhere('contact_summary', '=', 'block')
243 foreach ($afforms as $index => $afform) {
244 // Create a group per afform type
246 "afform_{$afform['type']}" => [
247 'title' => $afform['type:label'],
248 'icon' => $afform['type:icon'],
252 $blocks["afform_{$afform['type']}"]['blocks'][$afform['name']] = [
253 'title' => $afform['title'],
254 'tpl_file' => 'afform/contactSummary/AfformBlock.tpl',
255 'module' => $afform['module_name'],
256 'directive' => $afform['directive_name'],
258 $afform['type:label'],
260 'edit' => 'civicrm/admin/afform#/edit/' . $afform['name'],
261 'system_default' => [0, $index %
2],
267 * Implements hook_civicrm_angularModules().
269 * Generate a list of Afform Angular modules.
271 function afform_civicrm_angularModules(&$angularModules) {
272 $afforms = \Civi\Api4\Afform
::get(FALSE)
273 ->setSelect(['name', 'requires', 'module_name', 'directive_name'])
276 foreach ($afforms as $afform) {
277 $angularModules[$afform['module_name']] = [
278 'ext' => E
::LONG_NAME
,
279 'js' => ['assetBuilder://afform.js?name=' . urlencode($afform['name'])],
280 'requires' => $afform['requires'],
282 'partialsCallback' => '_afform_get_partials',
283 '_afform' => $afform['name'],
284 // TODO: Allow afforms to declare their own theming requirements
285 'bundles' => ['bootstrap3'],
287 $afform['directive_name'] => 'E',
294 * Callback to retrieve partials for a given afform/angular module.
296 * @see afform_civicrm_angularModules
298 * @param string $moduleName
300 * @param array $module
301 * The module definition.
303 * Array(string $filename => string $html).
304 * @throws API_Exception
306 function _afform_get_partials($moduleName, $module) {
307 $afform = civicrm_api4('Afform', 'get', [
308 'where' => [['name', '=', $module['_afform']]],
309 'select' => ['layout'],
310 'layoutFormat' => 'html',
311 'checkPermissions' => FALSE,
314 "~/$moduleName/$moduleName.aff.html" => $afform['layout'],
319 * Implements hook_civicrm_entityTypes().
321 * Declare entity types provided by this module.
323 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_entityTypes
325 function afform_civicrm_entityTypes(&$entityTypes) {
326 _afform_civix_civicrm_entityTypes($entityTypes);
330 * Implements hook_civicrm_buildAsset().
332 function afform_civicrm_buildAsset($asset, $params, &$mimeType, &$content) {
333 if ($asset !== 'afform.js') {
337 if (empty($params['name'])) {
338 throw new RuntimeException("Missing required parameter: afform.js?name=NAME");
341 $moduleName = _afform_angular_module_name($params['name'], 'camel');
342 $formMetaData = (array) civicrm_api4('Afform', 'get', [
343 'checkPermissions' => FALSE,
344 'select' => ['redirect', 'name', 'title'],
345 'where' => [['name', '=', $params['name']]],
347 $smarty = CRM_Core_Smarty
::singleton();
348 $smarty->assign('afform', [
349 'camel' => $moduleName,
350 'meta' => $formMetaData,
351 'templateUrl' => "~/$moduleName/$moduleName.aff.html",
353 $mimeType = 'text/javascript';
354 $content = $smarty->fetch('afform/AfformAngularModule.tpl');
358 * Implements hook_civicrm_alterMenu().
360 function afform_civicrm_alterMenu(&$items) {
361 if (Civi
::container()->has('afform_scanner')) {
362 $scanner = Civi
::service('afform_scanner');
365 // During installation...
366 $scanner = new CRM_Afform_AfformScanner();
368 foreach ($scanner->getMetas() as $name => $meta) {
369 if (!empty($meta['server_route'])) {
370 $items[$meta['server_route']] = [
371 'page_callback' => 'CRM_Afform_Page_AfformBase',
372 'page_arguments' => 'afform=' . urlencode($name),
373 'access_arguments' => [["@afform:$name"], 'and'],
374 'is_public' => $meta['is_public'],
381 * Implements hook_civicrm_permission_check().
383 * This extends the list of permissions available in `CRM_Core_Permission:check()`
384 * by introducing virtual-permissions named `@afform:myForm`. The evaluation
385 * of these virtual-permissions is dependent on the settings for `myForm`.
386 * `myForm` may be exposed/integrated through multiple subsystems (routing,
387 * nav-menu, API, etc), and the use of virtual-permissions makes easy to enforce
388 * consistent permissions across any relevant subsystems.
390 * @see CRM_Utils_Hook::permission_check()
392 function afform_civicrm_permission_check($permission, &$granted, $contactId) {
393 if ($permission[0] !== '@') {
394 // Micro-optimization - this function may get hit a lot.
398 if (preg_match('/^@afform:(.*)/', $permission, $m)) {
401 $afform = \Civi\Api4\Afform
::get()
402 ->setCheckPermissions(FALSE)
403 ->addWhere('name', '=', $name)
404 ->setSelect(['permission'])
408 $granted = CRM_Core_Permission
::check($afform['permission'], $contactId);
414 * Implements hook_civicrm_permissionList().
416 * @see CRM_Utils_Hook::permissionList()
418 function afform_civicrm_permissionList(&$permissions) {
419 $scanner = Civi
::service('afform_scanner');
420 foreach ($scanner->getMetas() as $name => $meta) {
421 $permissions['@afform:' . $name] = [
423 'title' => E
::ts('Afform: Inherit permission of %1', [
431 * Clear any local/in-memory caches based on afform data.
433 function _afform_clear() {
434 $container = \Civi
::container();
435 $container->get('afform_scanner')->clear();
436 $container->get('angular')->clear();
440 * @param string $fileBaseName
442 * @param string $format
445 * Ex: 'FooBar' or 'foo-bar'.
448 function _afform_angular_module_name($fileBaseName, $format = 'camel') {
452 foreach (preg_split('/[-_ ]/', $fileBaseName, NULL, PREG_SPLIT_NO_EMPTY
) as $shortNamePart) {
453 $camelCase .= ucfirst($shortNamePart);
455 return strtolower($camelCase[0]) . substr($camelCase, 1);
458 return strtolower(implode('-', preg_split('/[-_ ]|(?=[A-Z])/', $fileBaseName, NULL, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE
)));
461 throw new \
Exception("Unrecognized format");
466 * Implements hook_civicrm_alterApiRoutePermissions().
468 * @see CRM_Utils_Hook::alterApiRoutePermissions
470 function afform_civicrm_alterApiRoutePermissions(&$permissions, $entity, $action) {
471 if ($entity == 'Afform') {
472 // These actions should be accessible to anonymous users; permissions are checked internally
473 $allowedActions = ['prefill', 'submit', 'submitFile', 'getOptions'];
474 if (in_array($action, $allowedActions, TRUE)) {
475 $permissions = CRM_Core_Permission
::ALWAYS_ALLOW_PERMISSION
;
481 * Implements hook_civicrm_preProcess().
483 * Wordpress only: Adds Afforms to the shortcode dialog (when editing pages/posts).
485 function afform_civicrm_preProcess($formName, &$form) {
486 if ($formName === 'CRM_Core_Form_ShortCode') {
487 $form->components
['afform'] = [
488 'label' => E
::ts('Form Builder'),
491 'entity' => 'Afform',
492 'select' => ['minimumInputLength' => 0],
494 'params' => ['type' => ['IN' => ['form', 'search']]],
502 * Implements hook_civicrm_pre().
504 function afform_civicrm_pre($op, $entity, $id, &$params) {
505 // When deleting a searchDisplay, also delete any Afforms the display is embedded within
506 if ($entity === 'SearchDisplay' && $op === 'delete') {
507 $display = \Civi\Api4\SearchDisplay
::get(FALSE)
508 ->addSelect('saved_search_id.name', 'name')
509 ->addWhere('id', '=', $id)
510 ->execute()->first();
511 \Civi\Api4\Afform
::revert(FALSE)
512 ->addWhere('search_displays', 'CONTAINS', $display['saved_search_id.name'] . ".{$display['name']}")
515 // When deleting a savedSearch, delete any Afforms which use the default display
516 elseif ($entity === 'SavedSearch' && $op === 'delete') {
517 $search = \Civi\Api4\SavedSearch
::get(FALSE)
519 ->addWhere('id', '=', $id)
520 ->execute()->first();
521 \Civi\Api4\Afform
::revert(FALSE)
522 ->addWhere('search_displays', 'CONTAINS', $search['name'])
528 * Implements hook_civicrm_referenceCounts().
530 function afform_civicrm_referenceCounts($dao, &$counts) {
531 // Count afforms which contain a search display
532 if (is_a($dao, 'CRM_Search_DAO_SearchDisplay') && $dao->id
) {
533 if (empty($dao->saved_search_id
) ||
empty($dao->name
)) {
536 $search = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_SavedSearch', $dao->saved_search_id
);
537 $afforms = \Civi\Api4\Afform
::get(FALSE)
539 ->addWhere('search_displays', 'CONTAINS', "$search.$dao->name")
541 if ($afforms->count()) {
545 'count' => $afforms->count(),
549 // Count afforms which contain any displays from a SavedSearch (including the default display)
550 elseif (is_a($dao, 'CRM_Contact_DAO_SavedSearch') && $dao->id
) {
551 if (empty($dao->name
)) {
555 ['search_displays', 'CONTAINS', $dao->name
],
558 $displays = civicrm_api4('SearchDisplay', 'get', [
559 'where' => [['saved_search_id', '=', $dao->id
]],
562 foreach ($displays as $displayName) {
563 $clauses[] = ['search_displays', 'CONTAINS', $dao->name
. '.' . $displayName];
566 catch (Exception
$e) {
567 // In case SearchKit is not installed, the api call would fail
569 $afforms = \Civi\Api4\Afform
::get(FALSE)
571 ->addClause('OR', $clauses)
573 if ($afforms->count()) {
577 'count' => $afforms->count(),
583 // Wordpress only: Register callback for rendering shortcodes
584 if (function_exists('add_filter')) {
585 add_filter('civicrm_shortcode_get_markup', 'afform_shortcode_content', 10, 4);
589 * Wordpress only: Render Afform content for shortcodes.
591 * @param string $content
594 * Shortcode attributes.
596 * Existing shortcode arguments.
597 * @param string $context
598 * How many shortcodes are present on the page: 'single' or 'multiple'.
602 function afform_shortcode_content($content, $atts, $args, $context) {
603 if ($atts['component'] === 'afform') {
604 $afform = civicrm_api4('Afform', 'get', [
605 'select' => ['directive_name', 'module_name'],
606 'where' => [['name', '=', $atts['name']]],
609 Civi
::service('angularjs.loader')->addModules($afform['module_name']);
611 <div class='crm-container' id='bootstrap-theme'>
612 <crm-angular-js modules='{$afform['module_name']}'>
613 <{$afform['directive_name']}></{$afform['directive_name']}>