3 namespace Civi\Api4\Action\Afform
;
5 use Civi\Api4\CustomField
;
6 use Civi\Api4\CustomGroup
;
7 use CRM_Afform_ExtensionUtil
as E
;
11 * @package Civi\Api4\Action\Afform
13 class Get
extends \Civi\Api4\Generic\BasicGetAction
{
15 use \Civi\Api4\Utils\AfformFormatTrait
;
17 public function getRecords() {
18 /** @var \CRM_Afform_AfformScanner $scanner */
19 $scanner = \Civi
::service('afform_scanner');
20 $getComputed = $this->_isFieldSelected('has_local') ||
$this->_isFieldSelected('has_base');
21 $getLayout = $this->_isFieldSelected('layout');
23 // This helps optimize lookups by file/module/directive name
24 $toGet = array_filter([
25 'name' => $this->_itemsToGet('name'),
26 'module_name' => $this->_itemsToGet('module_name'),
27 'directive_name' => $this->_itemsToGet('directive_name'),
30 $names = $toGet['name'] ??
array_keys($scanner->findFilePaths());
32 $values = $this->getAutoGenerated($names, $toGet, $getLayout);
34 if ($this->checkPermissions
) {
35 $names = array_filter($names, [$this, 'checkPermission']);
38 foreach ($names as $name) {
41 'module_name' => _afform_angular_module_name($name, 'camel'),
42 'directive_name' => _afform_angular_module_name($name, 'dash'),
44 foreach ($toGet as $key => $get) {
45 if (!in_array($info[$key], $get)) {
49 $record = $scanner->getMeta($name);
50 if (!$record && !isset($values[$name])) {
53 $values[$name] = array_merge($values[$name] ??
[], $record ??
[], $info);
55 $scanner->addComputedFields($values[$name]);
58 // Autogenerated layouts will already be in values but can be overridden; scanner takes priority
59 $values[$name]['layout'] = $scanner->getLayout($name) ??
$values[$name]['layout'] ??
'';
63 if ($getLayout && $this->layoutFormat
!== 'html') {
64 foreach ($values as $name => $record) {
65 $values[$name]['layout'] = $this->convertHtmlToOutput($record['layout']);
73 * Assert that a form is authorized.
77 protected function checkPermission($name) {
78 return \CRM_Core_Permission
::check("@afform:$name");
82 * Generates afform blocks from custom field sets.
88 * @throws \API_Exception
90 protected function getAutoGenerated(&$names, $toGet, $getLayout) {
91 $values = $groupNames = [];
92 foreach ($toGet['name'] ??
[] as $name) {
93 if (strpos($name, 'afblockCustom_') === 0 && strlen($name) > 13) {
94 $groupNames[] = substr($name, 14);
97 // Early return if this api call is fetching afforms by name and those names are not custom-related
98 if ((!empty($toGet['name']) && !$groupNames)
99 ||
(!empty($toGet['module_name']) && !strstr(implode(' ', $toGet['module_name']), 'afblockCustom'))
100 ||
(!empty($toGet['directive_name']) && !strstr(implode(' ', $toGet['directive_name']), 'afblock-custom'))
104 $customApi = CustomGroup
::get()
105 ->setCheckPermissions(FALSE)
106 ->addSelect('name', 'title', 'help_pre', 'help_post', 'extends', 'max_multiple')
107 ->addWhere('is_multiple', '=', 1)
108 ->addWhere('is_active', '=', 1);
110 $customApi->addWhere('name', 'IN', $groupNames);
113 $customApi->addSelect('help_pre', 'help_post');
114 $customApi->addChain('fields', CustomField
::get(FALSE)
116 ->addWhere('custom_group_id', '=', '$id')
117 ->addWhere('is_active', '=', 1)
118 ->addOrderBy('weight', 'ASC')
121 foreach ($customApi->execute() as $custom) {
122 $name = 'afblockCustom_' . $custom['name'];
123 if (!in_array($name, $names)) {
130 'title' => E
::ts('%1 block', [1 => $custom['title']]),
132 'is_dashlet' => FALSE,
133 'is_public' => FALSE,
135 'permission' => 'access CiviCRM',
136 'join_entity' => 'Custom_' . $custom['name'],
137 'entity_type' => $custom['extends'],
138 'repeat' => $custom['max_multiple'] ?
: TRUE,
142 $item['layout'] = ($custom['help_pre'] ?
'<div class="af-markup">' . $custom['help_pre'] . "</div>\n" : '');
143 foreach ($custom['fields'] as $field) {
144 $item['layout'] .= "<af-field name=\"{$field['name']}\" />\n";
146 $item['layout'] .= ($custom['help_post'] ?
'<div class="af-markup">' . $custom['help_post'] . "</div>\n" : '');
148 $values[$name] = $item;