Merge pull request #19746 from eileenmcnaughton/mem_2
[civicrm-core.git] / ext / afform / core / Civi / Api4 / Action / Afform / Get.php
CommitLineData
11e724f0
TO
1<?php
2
3namespace Civi\Api4\Action\Afform;
4
9c84a124
CW
5use Civi\Api4\CustomField;
6use Civi\Api4\CustomGroup;
7
11e724f0 8/**
28b4ace4 9 * @inheritDoc
11e724f0
TO
10 * @package Civi\Api4\Action\Afform
11 */
28b4ace4 12class Get extends \Civi\Api4\Generic\BasicGetAction {
11e724f0 13
28b4ace4 14 use \Civi\Api4\Utils\AfformFormatTrait;
11e724f0
TO
15
16 public function getRecords() {
17 /** @var \CRM_Afform_AfformScanner $scanner */
18 $scanner = \Civi::service('afform_scanner');
9c84a124
CW
19 $getComputed = $this->_isFieldSelected('has_local') || $this->_isFieldSelected('has_base');
20 $getLayout = $this->_isFieldSelected('layout');
21
edf837b4
CW
22 // This helps optimize lookups by file/module/directive name
23 $toGet = array_filter([
24 'name' => $this->_itemsToGet('name'),
25 'module_name' => $this->_itemsToGet('module_name'),
26 'directive_name' => $this->_itemsToGet('directive_name'),
27 ]);
11e724f0 28
edf837b4 29 $names = $toGet['name'] ?? array_keys($scanner->findFilePaths());
25f2b36b 30
9c84a124 31 $values = $this->getAutoGenerated($names, $toGet, $getLayout);
11e724f0 32
48be26b0
CW
33 if ($this->checkPermissions) {
34 $names = array_filter($names, [$this, 'checkPermission']);
35 }
36
11e724f0 37 foreach ($names as $name) {
25f2b36b 38 $info = [
edf837b4 39 'name' => $name,
25f2b36b
CW
40 'module_name' => _afform_angular_module_name($name, 'camel'),
41 'directive_name' => _afform_angular_module_name($name, 'dash'),
42 ];
edf837b4
CW
43 foreach ($toGet as $key => $get) {
44 if (!in_array($info[$key], $get)) {
45 continue;
46 }
25f2b36b 47 }
11e724f0 48 $record = $scanner->getMeta($name);
9c84a124
CW
49 if (!$record && !isset($values[$name])) {
50 continue;
51 }
25f2b36b 52 $values[$name] = array_merge($values[$name] ?? [], $record ?? [], $info);
9c84a124
CW
53 if ($getComputed) {
54 $scanner->addComputedFields($values[$name]);
1aebf5b0 55 }
9c84a124
CW
56 if ($getLayout) {
57 $values[$name]['layout'] = $scanner->getLayout($name) ?? $values[$name]['layout'] ?? '';
11e724f0 58 }
11e724f0
TO
59 }
60
9c84a124
CW
61 if ($getLayout && $this->layoutFormat !== 'html') {
62 foreach ($values as $name => $record) {
63 $values[$name]['layout'] = $this->convertHtmlToOutput($record['layout']);
64 }
65 }
66
67 return $values;
68 }
69
48be26b0
CW
70 /**
71 * Assert that a form is authorized.
72 *
73 * @return bool
74 */
75 protected function checkPermission($name) {
76 return \CRM_Core_Permission::check("@afform:$name");
77 }
78
9c84a124
CW
79 /**
80 * Generates afform blocks from custom field sets.
81 *
82 * @param $names
83 * @param $toGet
84 * @param $getLayout
85 * @return array
86 * @throws \API_Exception
87 */
88 protected function getAutoGenerated(&$names, $toGet, $getLayout) {
89 $values = $groupNames = [];
edf837b4
CW
90 foreach ($toGet['name'] ?? [] as $name) {
91 if (strpos($name, 'afjoinCustom_') === 0 && strlen($name) > 13) {
92 $groupNames[] = substr($name, 13);
9c84a124
CW
93 }
94 }
c3273f01
CW
95 // Early return if this api call is fetching afforms by name and those names are not custom-related
96 if ((!empty($toGet['name']) && !$groupNames)
edf837b4
CW
97 || (!empty($toGet['module_name']) && !strstr(implode(' ', $toGet['module_name']), 'afjoinCustom'))
98 || (!empty($toGet['directive_name']) && !strstr(implode(' ', $toGet['directive_name']), 'afjoin-custom'))
99 ) {
9c84a124
CW
100 return $values;
101 }
102 $customApi = CustomGroup::get()
103 ->setCheckPermissions(FALSE)
6c108cab 104 ->setSelect(['name', 'title', 'help_pre', 'help_post', 'extends', 'max_multiple'])
9c84a124
CW
105 ->addWhere('is_multiple', '=', 1)
106 ->addWhere('is_active', '=', 1);
107 if ($groupNames) {
108 $customApi->addWhere('name', 'IN', $groupNames);
109 }
110 if ($getLayout) {
111 $customApi->addSelect('help_pre')->addSelect('help_post');
112 $customApi->addChain('fields', CustomField::get()
113 ->setCheckPermissions(FALSE)
114 ->addSelect('name')
115 ->addWhere('custom_group_id', '=', '$id')
116 ->addWhere('is_active', '=', 1)
117 ->addOrderBy('weight', 'ASC')
118 );
119 }
120 foreach ($customApi->execute() as $custom) {
edf837b4 121 $name = 'afjoinCustom_' . $custom['name'];
9c84a124
CW
122 if (!in_array($name, $names)) {
123 $names[] = $name;
124 }
125 $item = [
126 'name' => $name,
b1335297 127 'type' => 'block',
9c84a124
CW
128 'requires' => [],
129 'title' => ts('%1 block (default)', [1 => $custom['title']]),
130 'description' => '',
1887d8bd 131 'is_dashlet' => FALSE,
9c84a124
CW
132 'is_public' => FALSE,
133 'permission' => 'access CiviCRM',
344e8290 134 'join' => 'Custom_' . $custom['name'],
b1335297 135 'block' => $custom['extends'],
6c108cab 136 'repeat' => $custom['max_multiple'] ?: TRUE,
9c84a124
CW
137 'has_base' => TRUE,
138 ];
139 if ($getLayout) {
140 $item['layout'] = ($custom['help_pre'] ? '<div class="af-markup">' . $custom['help_pre'] . "</div>\n" : '');
141 foreach ($custom['fields'] as $field) {
142 $item['layout'] .= "<af-field name=\"{$field['name']}\" />\n";
143 }
144 $item['layout'] .= ($custom['help_post'] ? '<div class="af-markup">' . $custom['help_post'] . "</div>\n" : '');
145 }
146 $values[$name] = $item;
147 }
11e724f0
TO
148 return $values;
149 }
150
151}