Merge pull request #20199 from kartik1000/altering_recaptcha_form
[civicrm-core.git] / ext / afform / core / afform.php
CommitLineData
66aa0f5e
TO
1<?php
2
3require_once 'afform.civix.php';
4use CRM_Afform_ExtensionUtil as E;
fb388832 5use Civi\Api4\Action\Afform\Submit;
66aa0f5e 6
bc3b7c5b
TO
7/**
8 * Filter the content of $params to only have supported afform fields.
9 *
10 * @param array $params
11 * @return array
12 */
13function _afform_fields_filter($params) {
5591cfbf 14 $result = [];
67d666c6 15 $fields = \Civi\Api4\Afform::getfields(FALSE)->setAction('create')->execute()->indexBy('name');
50868e8d
CW
16 foreach ($fields as $fieldName => $field) {
17 if (isset($params[$fieldName])) {
18 $result[$fieldName] = $params[$fieldName];
d1ec770c 19
e38db494
CW
20 if ($field['data_type'] === 'Boolean' && !is_bool($params[$fieldName])) {
21 $result[$fieldName] = CRM_Utils_String::strtobool($params[$fieldName]);
d1ec770c
TO
22 }
23 }
bc3b7c5b
TO
24 }
25 return $result;
26}
27
8f4a0ee9 28/**
5591cfbf 29 * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
8f4a0ee9
TO
30 */
31function afform_civicrm_container($container) {
77dccccb 32 $container->addResource(new \Symfony\Component\Config\Resource\FileResource(__FILE__));
8f4a0ee9
TO
33 $container->setDefinition('afform_scanner', new \Symfony\Component\DependencyInjection\Definition(
34 'CRM_Afform_AfformScanner',
5591cfbf 35 []
b53fe171 36 ))->setPublic(TRUE);
8f4a0ee9
TO
37}
38
66aa0f5e
TO
39/**
40 * Implements hook_civicrm_config().
41 *
42 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_config
43 */
44function afform_civicrm_config(&$config) {
45 _afform_civix_civicrm_config($config);
77dccccb
TO
46
47 if (isset(Civi::$statics[__FUNCTION__])) {
48 return;
49 }
50 Civi::$statics[__FUNCTION__] = 1;
51
b7edd04e
TO
52 $dispatcher = Civi::dispatcher();
53 $dispatcher->addListener(Submit::EVENT_NAME, [Submit::class, 'processContacts'], 500);
54 $dispatcher->addListener(Submit::EVENT_NAME, [Submit::class, 'processGenericEntity'], -1000);
55 $dispatcher->addListener('hook_civicrm_angularModules', ['\Civi\Afform\AngularDependencyMapper', 'autoReq'], -1000);
56 $dispatcher->addListener('hook_civicrm_alterAngular', ['\Civi\Afform\AfformMetadataInjector', 'preprocess']);
bdceaf62 57 $dispatcher->addListener('hook_civicrm_check', ['\Civi\Afform\StatusChecks', 'hook_civicrm_check']);
b7edd04e
TO
58
59 // Register support for email tokens
bdceaf62
TO
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']);
64 }
66aa0f5e
TO
65}
66
67/**
68 * Implements hook_civicrm_xmlMenu().
69 *
70 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_xmlMenu
71 */
72function afform_civicrm_xmlMenu(&$files) {
73 _afform_civix_civicrm_xmlMenu($files);
74}
75
76/**
77 * Implements hook_civicrm_install().
78 *
79 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_install
80 */
81function afform_civicrm_install() {
82 _afform_civix_civicrm_install();
83}
84
85/**
86 * Implements hook_civicrm_postInstall().
87 *
88 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_postInstall
89 */
90function afform_civicrm_postInstall() {
91 _afform_civix_civicrm_postInstall();
92}
93
94/**
95 * Implements hook_civicrm_uninstall().
96 *
97 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_uninstall
98 */
99function afform_civicrm_uninstall() {
100 _afform_civix_civicrm_uninstall();
101}
102
103/**
104 * Implements hook_civicrm_enable().
105 *
106 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_enable
107 */
108function afform_civicrm_enable() {
109 _afform_civix_civicrm_enable();
110}
111
112/**
113 * Implements hook_civicrm_disable().
114 *
115 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_disable
116 */
117function afform_civicrm_disable() {
118 _afform_civix_civicrm_disable();
119}
120
121/**
122 * Implements hook_civicrm_upgrade().
123 *
124 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_upgrade
125 */
126function afform_civicrm_upgrade($op, CRM_Queue_Queue $queue = NULL) {
127 return _afform_civix_civicrm_upgrade($op, $queue);
128}
129
130/**
131 * Implements hook_civicrm_managed().
132 *
133 * Generate a list of entities to create/deactivate/delete when this module
134 * is installed, disabled, uninstalled.
135 *
136 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_managed
137 */
138function afform_civicrm_managed(&$entities) {
139 _afform_civix_civicrm_managed($entities);
521b232a
TO
140
141 /** @var \CRM_Afform_AfformScanner $scanner */
142 if (\Civi::container()->has('afform_scanner')) {
143 $scanner = \Civi::service('afform_scanner');
144 }
145 else {
146 // This might happen at oddballs points - e.g. while you're in the middle of re-enabling the ext.
147 // This AfformScanner instance only lives during this method call, and it feeds off the regular cache.
148 $scanner = new CRM_Afform_AfformScanner();
149 }
150
151 foreach ($scanner->getMetas() as $afform) {
152 if (empty($afform['is_dashlet']) || empty($afform['name'])) {
153 continue;
154 }
155 $entities[] = [
156 'module' => E::LONG_NAME,
157 'name' => 'afform_dashlet_' . $afform['name'],
158 'entity' => 'Dashboard',
159 'update' => 'always',
160 // ideal cleanup policy might be to (a) deactivate if used and (b) remove if unused
161 'cleanup' => 'always',
162 'params' => [
163 'version' => 3,
164 // Q: Should we loop through all domains?
165 'domain_id' => CRM_Core_BAO_Domain::getDomain()->id,
166 'is_active' => TRUE,
167 'name' => $afform['name'],
67d666c6 168 'label' => $afform['title'] ?? E::ts('(Untitled)'),
521b232a
TO
169 'directive' => _afform_angular_module_name($afform['name'], 'dash'),
170 'permission' => "@afform:" . $afform['name'],
171 ],
172 ];
173 }
66aa0f5e
TO
174}
175
176/**
c63f20d3 177 * Implements hook_civicrm_tabset().
66aa0f5e 178 *
c63f20d3
CW
179 * Adds afforms as contact summary tabs.
180 */
181function afform_civicrm_tabset($tabsetName, &$tabs, $context) {
182 if ($tabsetName !== 'civicrm/contact/view') {
183 return;
184 }
185 $scanner = \Civi::service('afform_scanner');
186 $weight = 111;
187 foreach ($scanner->getMetas() as $afform) {
188 if (!empty($afform['contact_summary']) && $afform['contact_summary'] === 'tab') {
189 $module = _afform_angular_module_name($afform['name']);
190 $tabs[] = [
191 'id' => $afform['name'],
192 'title' => $afform['title'],
193 'weight' => $weight++,
194 'icon' => 'crm-i fa-list-alt',
195 'is_active' => TRUE,
196 'template' => 'afform/contactSummary/AfformTab.tpl',
197 'module' => $module,
198 'directive' => _afform_angular_module_name($afform['name'], 'dash'),
199 ];
200 // If this is the real contact summary page (and not a callback from ContactLayoutEditor), load module.
201 if (empty($context['caller'])) {
202 Civi::service('angularjs.loader')->addModules($module);
203 }
204 }
205 }
206}
207
208/**
209 * Implements hook_civicrm_pageRun().
66aa0f5e 210 *
c63f20d3
CW
211 * Adds afforms as contact summary blocks.
212 */
213function afform_civicrm_pageRun(&$page) {
214 if (get_class($page) !== 'CRM_Contact_Page_View_Summary') {
215 return;
216 }
217 $scanner = \Civi::service('afform_scanner');
218 $cid = $page->get('cid');
219 $side = 'left';
220 foreach ($scanner->getMetas() as $afform) {
221 if (!empty($afform['contact_summary']) && $afform['contact_summary'] === 'block') {
222 $module = _afform_angular_module_name($afform['name']);
223 $block = [
224 'module' => $module,
225 'directive' => _afform_angular_module_name($afform['name'], 'dash'),
226 ];
227 $content = CRM_Core_Smarty::singleton()->fetchWith('afform/contactSummary/AfformBlock.tpl', ['contactId' => $cid, 'block' => $block]);
228 CRM_Core_Region::instance("contact-basic-info-$side")->add([
229 'markup' => '<div class="crm-summary-block">' . $content . '</div>',
230 'weight' => 1,
231 ]);
232 Civi::service('angularjs.loader')->addModules($module);
233 $side = $side === 'left' ? 'right' : 'left';
234 }
235 }
236}
237
238/**
239 * Implements hook_civicrm_contactSummaryBlocks().
66aa0f5e 240 *
c63f20d3 241 * @link https://github.com/civicrm/org.civicrm.contactlayout
66aa0f5e 242 */
c63f20d3 243function afform_civicrm_contactSummaryBlocks(&$blocks) {
4f664e9c
CW
244 $afforms = \Civi\Api4\Afform::get(FALSE)
245 ->setSelect(['name', 'title', 'directive_name', 'module_name', 'type', 'type:icon', 'type:label'])
246 ->addWhere('contact_summary', '=', 'block')
247 ->execute();
248 foreach ($afforms as $afform) {
249 // Create a group per afform type
250 $blocks += [
251 "afform_{$afform['type']}" => [
252 'title' => $afform['type:label'],
253 'icon' => $afform['type:icon'],
254 'blocks' => [],
255 ],
256 ];
257 $blocks["afform_{$afform['type']}"]['blocks'][$afform['name']] = [
258 'title' => $afform['title'],
259 'tpl_file' => 'afform/contactSummary/AfformBlock.tpl',
260 'module' => $afform['module_name'],
261 'directive' => $afform['directive_name'],
262 'sample' => [
263 $afform['type:label'],
264 ],
265 'edit' => 'civicrm/admin/afform#/edit/' . $afform['name'],
266 ];
c63f20d3 267 }
66aa0f5e
TO
268}
269
270/**
271 * Implements hook_civicrm_angularModules().
272 *
e1aca853 273 * Generate a list of Afform Angular modules.
66aa0f5e
TO
274 */
275function afform_civicrm_angularModules(&$angularModules) {
276 _afform_civix_civicrm_angularModules($angularModules);
bb56ac78 277
5e04a2d4 278 $afforms = \Civi\Api4\Afform::get(FALSE)
e38db494 279 ->setSelect(['name', 'requires', 'module_name', 'directive_name'])
2d4bfef1
CW
280 ->execute();
281
282 foreach ($afforms as $afform) {
e38db494 283 $angularModules[$afform['module_name']] = [
bb56ac78 284 'ext' => E::LONG_NAME,
2d4bfef1
CW
285 'js' => ['assetBuilder://afform.js?name=' . urlencode($afform['name'])],
286 'requires' => $afform['requires'],
bb56ac78 287 'basePages' => [],
aa6abb77 288 'partialsCallback' => '_afform_get_partials',
2d4bfef1 289 '_afform' => $afform['name'],
292054ac
CW
290 // TODO: Allow afforms to declare their own theming requirements
291 'bundles' => ['bootstrap3'],
77dccccb 292 'exports' => [
04417491 293 $afform['directive_name'] => 'E',
77dccccb 294 ],
bb56ac78 295 ];
bb56ac78 296 }
66aa0f5e
TO
297}
298
aa6abb77 299/**
2d4bfef1
CW
300 * Callback to retrieve partials for a given afform/angular module.
301 *
302 * @see afform_civicrm_angularModules
aa6abb77
TO
303 *
304 * @param string $moduleName
305 * The module name.
306 * @param array $module
307 * The module definition.
308 * @return array
309 * Array(string $filename => string $html).
2d4bfef1 310 * @throws API_Exception
aa6abb77
TO
311 */
312function _afform_get_partials($moduleName, $module) {
2d4bfef1
CW
313 $afform = civicrm_api4('Afform', 'get', [
314 'where' => [['name', '=', $module['_afform']]],
315 'select' => ['layout'],
316 'layoutFormat' => 'html',
317 'checkPermissions' => FALSE,
318 ], 0);
aa6abb77 319 return [
2d4bfef1 320 "~/$moduleName/$moduleName.aff.html" => $afform['layout'],
aa6abb77
TO
321 ];
322}
323
66aa0f5e
TO
324/**
325 * Implements hook_civicrm_alterSettingsFolders().
326 *
327 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_alterSettingsFolders
328 */
329function afform_civicrm_alterSettingsFolders(&$metaDataFolders = NULL) {
330 _afform_civix_civicrm_alterSettingsFolders($metaDataFolders);
331}
332
333/**
334 * Implements hook_civicrm_entityTypes().
335 *
336 * Declare entity types provided by this module.
337 *
338 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_entityTypes
339 */
340function afform_civicrm_entityTypes(&$entityTypes) {
341 _afform_civix_civicrm_entityTypes($entityTypes);
342}
343
98f4a7cb
TO
344/**
345 * Implements hook_civicrm_themes().
346 */
347function afform_civicrm_themes(&$themes) {
348 _afform_civix_civicrm_themes($themes);
349}
350
bb56ac78
TO
351/**
352 * Implements hook_civicrm_buildAsset().
353 */
354function afform_civicrm_buildAsset($asset, $params, &$mimeType, &$content) {
355 if ($asset !== 'afform.js') {
356 return;
357 }
358
359 if (empty($params['name'])) {
360 throw new RuntimeException("Missing required parameter: afform.js?name=NAME");
361 }
362
2d4bfef1 363 $moduleName = _afform_angular_module_name($params['name'], 'camel');
b6e13973
SL
364 $formMetaData = (array) civicrm_api4('Afform', 'get', [
365 'checkPermissions' => FALSE,
366 'select' => ['redirect', 'name'],
367 'where' => [['name', '=', $params['name']]],
368 ], 0);
bb56ac78
TO
369 $smarty = CRM_Core_Smarty::singleton();
370 $smarty->assign('afform', [
aa6abb77 371 'camel' => $moduleName,
b6e13973 372 'meta' => $formMetaData,
aa6abb77 373 'templateUrl' => "~/$moduleName/$moduleName.aff.html",
bb56ac78
TO
374 ]);
375 $mimeType = 'text/javascript';
9ec944f2 376 $content = $smarty->fetch('afform/AfformAngularModule.tpl');
bb56ac78
TO
377}
378
8775c48a
TO
379/**
380 * Implements hook_civicrm_alterMenu().
381 */
382function afform_civicrm_alterMenu(&$items) {
8f4a0ee9
TO
383 if (Civi::container()->has('afform_scanner')) {
384 $scanner = Civi::service('afform_scanner');
385 }
386 else {
387 // During installation...
388 $scanner = new CRM_Afform_AfformScanner();
389 }
8775c48a
TO
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),
13bdd6d2 395 'title' => $meta['title'] ?? '',
f16b2aee 396 'access_arguments' => [["@afform:$name"], 'and'],
254f01f0 397 'is_public' => $meta['is_public'],
8775c48a
TO
398 ];
399 }
400 }
f16b2aee
TO
401}
402
403/**
404 * Implements hook_civicrm_permission_check().
405 *
586344a7
TO
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.
412 *
f16b2aee
TO
413 * @see CRM_Utils_Hook::permission_check()
414 */
415function afform_civicrm_permission_check($permission, &$granted, $contactId) {
14b26ac5 416 if ($permission[0] !== '@') {
f16b2aee
TO
417 // Micro-optimization - this function may get hit a lot.
418 return;
419 }
420
421 if (preg_match('/^@afform:(.*)/', $permission, $m)) {
422 $name = $m[1];
423
2d4bfef1
CW
424 $afform = \Civi\Api4\Afform::get()
425 ->setCheckPermissions(FALSE)
426 ->addWhere('name', '=', $name)
427 ->setSelect(['permission'])
428 ->execute()
429 ->first();
430 if ($afform) {
431 $granted = CRM_Core_Permission::check($afform['permission'], $contactId);
432 }
f16b2aee 433 }
8775c48a
TO
434}
435
c4e6b413
TO
436/**
437 * Implements hook_civicrm_permissionList().
438 *
439 * @see CRM_Utils_Hook::permissionList()
440 */
441function afform_civicrm_permissionList(&$permissions) {
442 $scanner = Civi::service('afform_scanner');
443 foreach ($scanner->getMetas() as $name => $meta) {
444 $permissions['@afform:' . $name] = [
445 'group' => 'afform',
67d666c6 446 'title' => E::ts('Afform: Inherit permission of %1', [
c4e6b413
TO
447 1 => $name,
448 ]),
449 ];
450 }
451}
452
74f862e4
TO
453/**
454 * Clear any local/in-memory caches based on afform data.
455 */
456function _afform_clear() {
457 $container = \Civi::container();
458 $container->get('afform_scanner')->clear();
76b9562a 459 $container->get('angular')->clear();
74f862e4
TO
460}
461
bb56ac78 462/**
87dde5eb
TO
463 * @param string $fileBaseName
464 * Ex: foo-bar
841850b1
TO
465 * @param string $format
466 * 'camel' or 'dash'.
bb56ac78 467 * @return string
841850b1 468 * Ex: 'FooBar' or 'foo-bar'.
3cd5c38b 469 * @throws \Exception
bb56ac78 470 */
87dde5eb 471function _afform_angular_module_name($fileBaseName, $format = 'camel') {
841850b1
TO
472 switch ($format) {
473 case 'camel':
87dde5eb 474 $camelCase = '';
9c84a124 475 foreach (preg_split('/[-_ ]/', $fileBaseName, NULL, PREG_SPLIT_NO_EMPTY) as $shortNamePart) {
87dde5eb
TO
476 $camelCase .= ucfirst($shortNamePart);
477 }
14b26ac5 478 return strtolower($camelCase[0]) . substr($camelCase, 1);
841850b1
TO
479
480 case 'dash':
9c84a124 481 return strtolower(implode('-', preg_split('/[-_ ]|(?=[A-Z])/', $fileBaseName, NULL, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE)));
841850b1
TO
482
483 default:
484 throw new \Exception("Unrecognized format");
485 }
bb56ac78 486}
355881ac
SL
487
488/**
489 * Implements hook_civicrm_alterApiRoutePermissions().
490 *
491 * @see CRM_Utils_Hook::alterApiRoutePermissions
492 */
493function afform_civicrm_alterApiRoutePermissions(&$permissions, $entity, $action) {
f362c531
TO
494 if ($entity == 'Afform') {
495 if ($action == 'prefill' || $action == 'submit') {
496 $permissions = CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION;
497 }
355881ac
SL
498 }
499}
f228ad74
CW
500
501/**
502 * Implements hook_civicrm_preProcess().
503 *
504 * Wordpress only: Adds Afforms to the shortcode dialog (when editing pages/posts).
505 */
506function afform_civicrm_preProcess($formName, &$form) {
507 if ($formName === 'CRM_Core_Form_ShortCode') {
508 $form->components['afform'] = [
509 'label' => E::ts('Form Builder'),
510 'select' => [
511 'key' => 'name',
512 'entity' => 'Afform',
513 'select' => ['minimumInputLength' => 0],
514 'api' => [
515 'params' => ['type' => ['IN' => ['form', 'search']]],
516 ],
517 ],
518 ];
519 }
520}
521
522// Wordpress only: Register callback for rendering shortcodes
523if (function_exists('add_filter')) {
524 add_filter('civicrm_shortcode_get_markup', 'afform_shortcode_content', 10, 4);
525}
526
527/**
528 * Wordpress only: Render Afform content for shortcodes.
529 *
530 * @param string $content
531 * HTML Markup
532 * @param array $atts
533 * Shortcode attributes.
534 * @param array $args
535 * Existing shortcode arguments.
536 * @param string $context
537 * How many shortcodes are present on the page: 'single' or 'multiple'.
538 * @return string
539 * Modified markup.
540 */
541function afform_shortcode_content($content, $atts, $args, $context) {
542 if ($atts['component'] === 'afform') {
543 $afform = civicrm_api4('Afform', 'get', [
544 'select' => ['directive_name', 'module_name'],
545 'where' => [['name', '=', $atts['name']]],
546 ])->first();
547 if ($afform) {
548 Civi::service('angularjs.loader')->addModules($afform['module_name']);
549 $content = "
550 <div class='crm-container' id='bootstrap-theme'>
551 <crm-angular-js modules='{$afform['module_name']}'>
552 <{$afform['directive_name']}></{$afform['directive_name']}>
553 </crm-angular-js>
554 </div>";
555 }
556 }
557 return $content;
558}