Get.php 3.99 KB
Newer Older
1
2
3
4
<?php

namespace Civi\Api4\Action\Afform;

5
6
7
use Civi\Api4\CustomField;
use Civi\Api4\CustomGroup;

8
/**
colemanw's avatar
colemanw committed
9
 * @inheritDoc
10
11
 * @package Civi\Api4\Action\Afform
 */
colemanw's avatar
colemanw committed
12
class Get extends \Civi\Api4\Generic\BasicGetAction {
13

colemanw's avatar
colemanw committed
14
  use \Civi\Api4\Utils\AfformFormatTrait;
15
16
17
18

  public function getRecords() {
    /** @var \CRM_Afform_AfformScanner $scanner */
    $scanner = \Civi::service('afform_scanner');
19
20
21
22
23
    $getComputed = $this->_isFieldSelected('has_local') || $this->_isFieldSelected('has_base');
    $getLayout = $this->_isFieldSelected('layout');

    $toGet = $this->_itemsToGet('name');
    $names = $toGet ?? array_keys($scanner->findFilePaths());
24

25
26
27
28
29
30
    // Optimize lookups by module/directive name
    if (!$toGet) {
      $moduleNames = $this->_itemsToGet('module_name');
      $directiveNames = $this->_itemsToGet('directive_name');
    }

31
    $values = $this->getAutoGenerated($names, $toGet, $getLayout);
32
33

    foreach ($names as $name) {
34
35
36
37
38
39
40
      $info = [
        'module_name' => _afform_angular_module_name($name, 'camel'),
        'directive_name' => _afform_angular_module_name($name, 'dash'),
      ];
      if (($moduleNames && !in_array($info['module_name'], $moduleNames)) || ($directiveNames && !in_array($info['directive_name'], $directiveNames))) {
        continue;
      }
41
      $record = $scanner->getMeta($name);
42
43
44
      if (!$record && !isset($values[$name])) {
        continue;
      }
45
      $values[$name] = array_merge($values[$name] ?? [], $record ?? [], $info);
46
47
      if ($getComputed) {
        $scanner->addComputedFields($values[$name]);
48
      }
49
50
      if ($getLayout) {
        $values[$name]['layout'] = $scanner->getLayout($name) ?? $values[$name]['layout'] ?? '';
51
52
53
      }
    }

54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
    if ($getLayout && $this->layoutFormat !== 'html') {
      foreach ($values as $name => $record) {
        $values[$name]['layout'] = $this->convertHtmlToOutput($record['layout']);
      }
    }

    return $values;
  }

  /**
   * Generates afform blocks from custom field sets.
   *
   * @param $names
   * @param $toGet
   * @param $getLayout
   * @return array
   * @throws \API_Exception
   */
  protected function getAutoGenerated(&$names, $toGet, $getLayout) {
    $values = $groupNames = [];
    foreach ($toGet ?? [] as $name) {
      if (strpos($name, 'blockCustom_') === 0 && strlen($name) > 12) {
        $groupNames[] = substr($name, 12);
      }
    }
    if ($toGet && !$groupNames) {
      return $values;
    }
    $customApi = CustomGroup::get()
      ->setCheckPermissions(FALSE)
      ->setSelect(['name', 'title', 'help_pre', 'help_post'])
      ->addWhere('is_multiple', '=', 1)
      ->addWhere('is_active', '=', 1);
    if ($groupNames) {
      $customApi->addWhere('name', 'IN', $groupNames);
    }
    if ($getLayout) {
      $customApi->addSelect('help_pre')->addSelect('help_post');
      $customApi->addChain('fields', CustomField::get()
        ->setCheckPermissions(FALSE)
        ->addSelect('name')
        ->addWhere('custom_group_id', '=', '$id')
        ->addWhere('is_active', '=', 1)
        ->addOrderBy('weight', 'ASC')
      );
    }
    foreach ($customApi->execute() as $custom) {
      $name = 'blockCustom_' . $custom['name'];
      if (!in_array($name, $names)) {
        $names[] = $name;
      }
      $item = [
        'name' => $name,
        'requires' => [],
        'title' => ts('%1 block (default)', [1 => $custom['title']]),
        'description' => '',
        'is_public' => FALSE,
        'permission' => 'access CiviCRM',
112
        'join' => 'Custom_' . $custom['name'],
113
        'extends' => 'Contact',
114
        'repeat' => TRUE,
115
116
117
118
119
120
121
122
123
124
125
        'has_base' => TRUE,
      ];
      if ($getLayout) {
        $item['layout'] = ($custom['help_pre'] ? '<div class="af-markup">' . $custom['help_pre'] . "</div>\n" : '');
        foreach ($custom['fields'] as $field) {
          $item['layout'] .= "<af-field name=\"{$field['name']}\" />\n";
        }
        $item['layout'] .= ($custom['help_post'] ? '<div class="af-markup">' . $custom['help_post'] . "</div>\n" : '');
      }
      $values[$name] = $item;
    }
126
127
128
129
    return $values;
  }

}