Afform.php 3.56 KB
Newer Older
totten's avatar
totten committed
1
2
3
4
<?php

namespace Civi\Api4;

5
6
use Civi\Api4\Generic\AbstractEntity;
use Civi\Api4\Generic\BasicBatchAction;
colemanw's avatar
colemanw committed
7
use Civi\Api4\Generic\BasicGetFieldsAction;
totten's avatar
totten committed
8
9

/**
10
 * User-configurable forms
totten's avatar
totten committed
11
12
 * @package Civi\Api4
 */
13
class Afform extends AbstractEntity {
14
15

  /**
16
   * @return \Civi\Api4\Action\Afform\Get
17
18
   */
  public static function get() {
19
    return new \Civi\Api4\Action\Afform\Get('Afform', __FUNCTION__);
20
21
  }

colemanw's avatar
colemanw committed
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
  /**
   * @return \Civi\Api4\Action\Afform\Create
   */
  public static function create() {
    return new \Civi\Api4\Action\Afform\Create('Afform', __FUNCTION__);
  }

  /**
   * @return \Civi\Api4\Action\Afform\Update
   */
  public static function update() {
    return new \Civi\Api4\Action\Afform\Update('Afform', __FUNCTION__, 'name');
  }

  /**
   * @return \Civi\Api4\Action\Afform\Save
   */
  public static function save() {
    return new \Civi\Api4\Action\Afform\Save('Afform', __FUNCTION__, 'name');
  }

  /**
   * @return \Civi\Api4\Action\Afform\Prefill
   */
  public static function prefill() {
    return new \Civi\Api4\Action\Afform\Prefill('Afform', __FUNCTION__);
  }

  /**
   * @return \Civi\Api4\Action\Afform\Submit
   */
  public static function submit() {
    return new \Civi\Api4\Action\Afform\Submit('Afform', __FUNCTION__);
  }

57
  /**
58
   * @return \Civi\Api4\Generic\BasicBatchAction
59
60
   */
  public static function revert() {
61
62
63
64
    return new BasicBatchAction('Afform', __FUNCTION__, ['name'], function($item, BasicBatchAction $action) {
      $scanner = \Civi::service('afform_scanner');
      $files = [
        \CRM_Afform_AfformScanner::METADATA_FILE,
65
        \CRM_Afform_AfformScanner::LAYOUT_FILE,
66
67
68
69
70
71
72
73
74
75
76
77
      ];

      foreach ($files as $file) {
        $metaPath = $scanner->createSiteLocalPath($item['name'], $file);
        if (file_exists($metaPath)) {
          if (!@unlink($metaPath)) {
            throw new \API_Exception("Failed to remove afform overrides in $file");
          }
        }
      }

      // We may have changed list of files covered by the cache.
78
      _afform_clear();
79
80
81
82
83
84

      // FIXME if `server_route` changes, then flush the menu cache.
      // FIXME if asset-caching is enabled, then flush the asset cache

      return $item;
    });
85
86
  }

colemanw's avatar
colemanw committed
87
  public static function getFields() {
colemanw's avatar
colemanw committed
88
    return new BasicGetFieldsAction('Afform', __FUNCTION__, function($self) {
89
      $fields = [
colemanw's avatar
colemanw committed
90
91
92
93
94
95
        [
          'name' => 'name',
        ],
        [
          'name' => 'requires',
        ],
96
97
98
99
        [
          'name' => 'block',
        ],
        [
100
          'name' => 'join',
101
        ],
colemanw's avatar
colemanw committed
102
103
        [
          'name' => 'title',
colemanw's avatar
colemanw committed
104
          'required' => $self->getAction() === 'create',
colemanw's avatar
colemanw committed
105
106
107
108
109
110
111
112
        ],
        [
          'name' => 'description',
        ],
        [
          'name' => 'is_public',
          'data_type' => 'Boolean',
        ],
113
        [
114
115
          'name' => 'repeat',
          'data_type' => 'Mixed',
116
        ],
colemanw's avatar
colemanw committed
117
118
119
        [
          'name' => 'server_route',
        ],
120
121
122
        [
          'name' => 'permission',
        ],
colemanw's avatar
colemanw committed
123
124
125
126
        [
          'name' => 'layout',
        ],
      ];
127
128

      if ($self->getAction() === 'get') {
129
130
131
132
133
134
        $fields[] = [
          'name' => 'module_name',
        ];
        $fields[] = [
          'name' => 'directive_name',
        ];
135
136
137
138
        $fields[] = [
          'name' => 'has_local',
        ];
        $fields[] = [
139
          'name' => 'has_base',
140
141
142
143
        ];
      }

      return $fields;
colemanw's avatar
colemanw committed
144
145
146
    });
  }

147
148
149
150
151
152
153
  /**
   * @return array
   */
  public static function permissions() {
    return [
      "meta" => ["access CiviCRM"],
      "default" => ["administer CiviCRM"],
colemanw's avatar
colemanw committed
154
155
      'prefill' => [],
      'submit' => [],
156
157
    ];
  }
totten's avatar
totten committed
158
159

}