3 namespace Civi\Api4\Action\Afform
;
5 use Civi\Afform\FormDataModel
;
6 use Civi\Api4\Generic\Result
;
9 * Shared functionality for form submission processing.
10 * @package Civi\Api4\Action\Afform
12 abstract class AbstractProcessor
extends \Civi\Api4\Generic\AbstractAction
{
22 * Arguments present when loading the form
30 * @var \Civi\Afform\FormDataModel
31 * List of entities declared by this form.
33 protected $_formDataModel;
36 * @param \Civi\Api4\Generic\Result $result
37 * @throws \API_Exception
39 public function _run(Result
$result) {
40 // This will throw an exception if the form doesn't exist
41 $this->_afform
= (array) civicrm_api4('Afform', 'get', ['checkPermissions' => FALSE, 'where' => [['name', '=', $this->name
]]], 0);
42 $this->_formDataModel
= new FormDataModel($this->_afform
['layout']);
43 $this->checkPermissions();
44 $this->validateArgs();
45 $result->exchangeArray($this->processForm());
49 * Strip out arguments that are not allowed on this form
51 protected function validateArgs() {
52 $rawArgs = $this->args
;
53 $entities = $this->_formDataModel
->getEntities();
55 foreach ($rawArgs as $arg => $val) {
56 if (!empty($entities[$arg]['url-autofill'])) {
57 $this->args
[$arg] = $val;
63 * Assert that the current form submission is authorized.
65 * @throws \Civi\API\Exception\UnauthorizedException
67 protected function checkPermissions() {
68 if ($this->getCheckPermissions()) {
69 if (!\CRM_Core_Permission
::check("@afform:" . $this->_afform
['name'])) {
70 throw new \Civi\API\Exception\
UnauthorizedException("Authorization failed: Cannot process form " . $this->_afform
['name']);
78 abstract protected function processForm();
81 * @param $mainEntityName
82 * @param $joinEntityName
83 * @param $mainEntityId
85 * @throws \API_Exception
87 protected static function getJoinWhereClause($mainEntityName, $joinEntityName, $mainEntityId) {
89 if (self
::fieldExists($joinEntityName, 'entity_id')) {
90 $params[] = ['entity_id', '=', $mainEntityId];
91 if (self
::fieldExists($joinEntityName, 'entity_table')) {
92 $params[] = ['entity_table', '=', 'civicrm_' . _civicrm_api_get_entity_name_from_camel($mainEntityName)];
96 $mainEntityField = _civicrm_api_get_entity_name_from_camel($mainEntityName) . '_id';
97 $params[] = [$mainEntityField, '=', $mainEntityId];
103 * Check if a field exists for a given entity
108 * @throws \API_Exception
110 public static function fieldExists($entityName, $fieldName) {
111 if (empty(\Civi
::$statics[__CLASS__
][__FUNCTION__
][$entityName])) {
112 $fields = civicrm_api4($entityName, 'getFields', [
113 'checkPermissions' => FALSE,
114 'action' => 'create',
115 'select' => ['name'],
116 'includeCustom' => FALSE,
118 \Civi
::$statics[__CLASS__
][__FUNCTION__
][$entityName] = $fields->column('name');
120 return in_array($fieldName, \Civi
::$statics[__CLASS__
][__FUNCTION__
][$entityName]);