diff --git a/CRM/FormProcessor/BAO/FormProcessorInstance.php b/CRM/FormProcessor/BAO/FormProcessorInstance.php index cdf59a60843f3f005d2a5a4c8840ca558f3a1c09..1e9ddb2cfb009c1ecd1fb5205572379d0c046b65 100644 --- a/CRM/FormProcessor/BAO/FormProcessorInstance.php +++ b/CRM/FormProcessor/BAO/FormProcessorInstance.php @@ -135,6 +135,10 @@ * @static */ public static function isNameValid($name, $id=null) { + $invalidNames = array('getactions', 'getfields'); + if (in_array(strtolower($name), $invalidNames)) { + return false; + } $sql = "SELECT COUNT(*) FROM `civicrm_form_processor_instance` WHERE `name` = %1"; $params[1] = array($name, 'String'); if ($id) { diff --git a/Civi/FormProcessor/API/Provider.php b/Civi/FormProcessor/API/Provider.php index 95260d84cb21ac6e8e1b4cf82eb855a05df43080..beb6fb03e859f431b48ef9a6f9b189d2aef2e015 100644 --- a/Civi/FormProcessor/API/Provider.php +++ b/Civi/FormProcessor/API/Provider.php @@ -7,6 +7,7 @@ namespace Civi\FormProcessor\API; use Civi\API\Event\ResolveEvent; + use Civi\API\Event\RespondEvent; use Civi\API\Events; use Civi\API\Provider\ProviderInterface as API_ProviderInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface; @@ -26,13 +27,70 @@ * @return array */ public static function getSubscribedEvents() { + // Some remarks on the solution to implement the getFields method. + // We would like to have implemented it as a normal api call. Meaning + // it would been processed in the invoke method. + // + // However the reflection provider has a stop propegation so we cannot use + // getfields here unles we are earlier then the reflection provider. + // We should then use a weight lower than Events::W_EARLY and do a + // stop propegation in our event. But setting an early flag is not a neat way to do stuff. + // So instead we use the Respond event and check in the respond event whether the action is getFields and + // if so do our getfields stuff there. return array( - Events::RESOLVE => array('onApiResolve'), - ); + Events::RESOLVE => array('onApiResolve'), + Events::RESPOND => array('onGetFieldsRepsonse'), // we use this method to add our field definition to the getFields action. + ); } public function onApiResolve(ResolveEvent $event) { - $event->setApiProvider($this); + $event->setApiProvider($this); + } + + /** + * Event listener on the ResponddEvent to handle the getfields actions. + * So the fields defined by the user are availble in the api explorer for example. + */ + public function onGetFieldsRepsonse(RespondEvent $event) { + $apiRequest = $event->getApiRequest(); + $params = $apiRequest['params']; + $result = $event->getResponse(); + + // First check whether the entity is formprocess and the action is getfields. + // If not return this function. + if (strtolower($apiRequest['entity']) != 'formprocessor' || strtolower($apiRequest['action']) != 'getfields') { + return; + } + // Now check whether the action param is set. With the action param we can find the form processor. + if (isset($params['action'])) { + $actionProvider = form_processor_get_action_provider(); + + // Find the form processor + $formProcessors = \CRM_FormProcessor_BAO_FormProcessorInstance::getValues(array('name' => $params['action'])); + if (count($formProcessors) != 1) { + return; + } + $formProcessor = reset($formProcessors); + + // Process all inputs of the formprocessor. + foreach($formProcessor['inputs'] as $input) { + $field = array( + 'name' => $input['name'], + 'title' => $input['name'], + 'description' => '', + 'type' => $input['type']->getCrmType(), + 'api.required' => $input['is_required'], + 'api.default' => $input['default_value'], + 'api.aliases' => array(), + 'entity' => 'FormProcessor', + ); + if ($input['type']->getCrmType()) { + $result['values'][$input['name']] = $field; + } + } + $result['count'] = count($result['values']); + $event->setResponse($result); + } } /** @@ -107,9 +165,13 @@ * API entity. * @return array<string> */ - public function getActionNames($version, $entity) { + public function getActionNames($version, $entity) { + if (strtolower($entity) != 'formprocessor') { + return array(); + } $params['is_active'] = 1; $form_processors = \CRM_FormProcessor_BAO_FormProcessorInstance::getValues($params); + $actions[] = 'getfields'; foreach($form_processors as $form_processor) { $actions[] = $form_processor['name']; } diff --git a/Civi/FormProcessor/Type/AbstractType.php b/Civi/FormProcessor/Type/AbstractType.php index 7949a481f0369746f8d70db602edfa70d5a0aa96..2b771f2c726b63036f5c89c347974516fb7adc39 100644 --- a/Civi/FormProcessor/Type/AbstractType.php +++ b/Civi/FormProcessor/Type/AbstractType.php @@ -65,6 +65,13 @@ return $this->name; } + /** + * Returns the type number from CRM_Utils_Type + */ + public function getCrmType() { + return false; + } + /** * Returns the label of the type. * diff --git a/Civi/FormProcessor/Type/GenericType.php b/Civi/FormProcessor/Type/GenericType.php index bd277dacb462fe453f2b2b5ba47abb43f9418512..680fcc37e5d036340744bb37144712af6150f9ab 100644 --- a/Civi/FormProcessor/Type/GenericType.php +++ b/Civi/FormProcessor/Type/GenericType.php @@ -18,4 +18,16 @@ return true; } + /** + * Returns the type number from CRM_Utils_Type + */ + public function getCrmType() { + $coreTypes = \CRM_Utils_Type::getValidTypes(); + if (isset($coreTypes[$this->name])) { + return $coreTypes[$this->name]; + } + return false; + } + + } diff --git a/api/v3/FormProcessorAction/Create.php b/api/v3/FormProcessorAction/Create.php index 7fb16e988f854f1713d7bf6d7ca276535336d457..b280fce88aa8d818e82177b8cde29c77d4450647 100644 --- a/api/v3/FormProcessorAction/Create.php +++ b/api/v3/FormProcessorAction/Create.php @@ -14,38 +14,38 @@ function _civicrm_api3_form_processor_action_create_spec(&$spec) { $spec['id'] = array( 'title' => E::ts('ID'), 'type' => CRM_Utils_Type::T_INT, - 'api_required' => false + 'api.required' => false ); $spec['form_processor_instance_id'] = array( 'title' => E::ts('Form Processor Instance ID'), 'type' => CRM_Utils_Type::T_INT, - 'api_required' => true, + 'api.required' => true, 'FKApiName' => 'FormProcessorInstance', ); $spec['weight'] = array( 'title' => E::ts('Weight'), 'type' => CRM_Utils_Type::T_INT, - 'api_required' => true, + 'api.required' => true, ); $spec['title'] = array( 'title' => E::ts('Title'), 'type' => CRM_Utils_Type::T_STRING, - 'api_required' => true + 'api.required' => true ); $spec['type'] = array( 'title' => E::ts('Type'), 'type' => CRM_Utils_Type::T_STRING, - 'api_required' => true + 'api.required' => true ); $spec['configuration'] = array( 'title' => E::ts('Configuration'), 'type' => CRM_Utils_Type::T_TEXT, - 'api_required' => false + 'api.required' => false ); $spec['mapping'] = array( 'title' => E::ts('Mapping'), 'type' => CRM_Utils_Type::T_TEXT, - 'api_required' => false + 'api.required' => false ); } diff --git a/api/v3/FormProcessorAction/Delete.php b/api/v3/FormProcessorAction/Delete.php index d420884aa4b8b917a52b5993b15988690a7b3a5f..8e2f9058d078a90ac81c66ef5b0fc90cc73b48a8 100644 --- a/api/v3/FormProcessorAction/Delete.php +++ b/api/v3/FormProcessorAction/Delete.php @@ -14,7 +14,7 @@ function _civicrm_api3_form_processor_action_Delete_spec(&$spec) { $spec['id'] = array( 'title' => E::ts('ID'), 'type' => CRM_Utils_Type::T_INT, - 'api_required' => true + 'api.required' => true ); } diff --git a/api/v3/FormProcessorInput/Create.php b/api/v3/FormProcessorInput/Create.php index 920445c53eb849b0d30144054a3a878fe57b9961..e4fe5e2d838d8738ef0f7f22cd9898830378e6ce 100644 --- a/api/v3/FormProcessorInput/Create.php +++ b/api/v3/FormProcessorInput/Create.php @@ -14,39 +14,39 @@ function _civicrm_api3_form_processor_input_create_spec(&$spec) { $spec['id'] = array( 'title' => E::ts('ID'), 'type' => CRM_Utils_Type::T_INT, - 'api_required' => false + 'api.required' => false ); $spec['form_processor_instance_id'] = array( 'title' => E::ts('Form Processor Instance ID'), 'type' => CRM_Utils_Type::T_INT, - 'api_required' => true, + 'api.required' => true, 'FKApiName' => 'FormProcessorInstance', ); $spec['name'] = array( 'title' => E::ts('Name'), 'type' => CRM_Utils_Type::T_STRING, - 'api_required' => true + 'api.required' => true ); $spec['type'] = array( 'title' => E::ts('Type'), 'type' => CRM_Utils_Type::T_STRING, - 'api_required' => true + 'api.required' => true ); $spec['is_required'] = array( 'title' => E::ts('Is required'), 'type' => CRM_Utils_Type::T_BOOLEAN, - 'api_required' => false, + 'api.required' => false, 'api.default' => false, ); $spec['default_value'] = array( 'title' => E::ts('Default Value'), 'type' => CRM_Utils_Type::T_STRING, - 'api_required' => false + 'api.required' => false ); $spec['configuration'] = array( 'title' => E::ts('Configuration'), 'type' => CRM_Utils_Type::T_TEXT, - 'api_required' => false + 'api.required' => false ); } diff --git a/api/v3/FormProcessorInput/Delete.php b/api/v3/FormProcessorInput/Delete.php index 86503957996227552c7f8070eca6bfbbf452a727..2a8fa3c41cfb40566d43f47de1eeb6b30ca831d6 100644 --- a/api/v3/FormProcessorInput/Delete.php +++ b/api/v3/FormProcessorInput/Delete.php @@ -14,7 +14,7 @@ function _civicrm_api3_form_processor_input_Delete_spec(&$spec) { $spec['id'] = array( 'title' => E::ts('ID'), 'type' => CRM_Utils_Type::T_INT, - 'api_required' => true + 'api.required' => true ); } diff --git a/api/v3/FormProcessorInputValidation/Create.php b/api/v3/FormProcessorInputValidation/Create.php index 646babbf916693dac5e132bd834463c3b34a09be..a3b7587af807ef61620f5fec4407a96d03b23b24 100644 --- a/api/v3/FormProcessorInputValidation/Create.php +++ b/api/v3/FormProcessorInputValidation/Create.php @@ -14,23 +14,23 @@ function _civicrm_api3_form_processor_input_validation_create_spec(&$spec) { $spec['id'] = array( 'title' => E::ts('ID'), 'type' => CRM_Utils_Type::T_INT, - 'api_required' => false + 'api.required' => false ); $spec['form_processor_input_id'] = array( 'title' => E::ts('Form Processor Input ID'), 'type' => CRM_Utils_Type::T_INT, - 'api_required' => true, + 'api.required' => true, 'FKApiName' => 'FormProcessorInput', ); $spec['validator'] = array( 'title' => E::ts('Validator'), 'type' => CRM_Utils_Type::T_STRING, - 'api_required' => true + 'api.required' => true ); $spec['configuration'] = array( 'title' => E::ts('Configuration'), 'type' => CRM_Utils_Type::T_TEXT, - 'api_required' => false + 'api.required' => false ); } diff --git a/api/v3/FormProcessorInputValidation/Delete.php b/api/v3/FormProcessorInputValidation/Delete.php index 546a75e0e83450dfcd8515dcdd7b14eced6618d8..7ecc8809ae79a5eab8b0ab02d961344b0a31ca27 100644 --- a/api/v3/FormProcessorInputValidation/Delete.php +++ b/api/v3/FormProcessorInputValidation/Delete.php @@ -14,7 +14,7 @@ function _civicrm_api3_form_processor_input_validation_Delete_spec(&$spec) { $spec['id'] = array( 'title' => E::ts('ID'), 'type' => CRM_Utils_Type::T_INT, - 'api_required' => true + 'api.required' => true ); } diff --git a/api/v3/FormProcessorInstance/Create.php b/api/v3/FormProcessorInstance/Create.php index 0be79ee3d0782e156903cd80328546cfe0e7ed9e..2167e42b4b0879675b381c5d932a196a2bbf3825 100644 --- a/api/v3/FormProcessorInstance/Create.php +++ b/api/v3/FormProcessorInstance/Create.php @@ -14,28 +14,28 @@ function _civicrm_api3_form_processor_instance_create_spec(&$spec) { $spec['id'] = array( 'title' => E::ts('ID'), 'type' => CRM_Utils_Type::T_INT, - 'api_required' => false + 'api.required' => false ); $spec['name'] = array( 'title' => E::ts('Name'), 'type' => CRM_Utils_Type::T_STRING, - 'api_required' => true + 'api.required' => true ); $spec['title'] = array( 'title' => E::ts('Title'), 'type' => CRM_Utils_Type::T_STRING, - 'api_required' => true + 'api.required' => true ); $spec['is_active'] = array( 'title' => E::ts('Is active'), 'type' => CRM_Utils_Type::T_BOOLEAN, - 'api_required' => true, + 'api.required' => true, 'api.default' => true, ); $spec['description'] = array( 'title' => E::ts('Description'), 'type' => CRM_Utils_Type::T_TEXT, - 'api_required' => true + 'api.required' => true ); } diff --git a/api/v3/FormProcessorInstance/Delete.php b/api/v3/FormProcessorInstance/Delete.php index 5b45e17da69e87a3b62ebf14947c4cc640190287..04beb93a16c968954e741d031a03f23cc5043961 100644 --- a/api/v3/FormProcessorInstance/Delete.php +++ b/api/v3/FormProcessorInstance/Delete.php @@ -14,7 +14,7 @@ function _civicrm_api3_form_processor_instance_Delete_spec(&$spec) { $spec['id'] = array( 'title' => E::ts('ID'), 'type' => CRM_Utils_Type::T_INT, - 'api_required' => true + 'api.required' => true ); } diff --git a/api/v3/FormProcessorInstance/Validatename.php b/api/v3/FormProcessorInstance/Validatename.php index 69b6a5c4b624c25998f05bd3910d30c7ec815f47..d0487051484dc81025b0fae7c0d4e8d69ca0ac0b 100644 --- a/api/v3/FormProcessorInstance/Validatename.php +++ b/api/v3/FormProcessorInstance/Validatename.php @@ -31,11 +31,11 @@ function _civicrm_api3_form_processor_instance_validatename_spec(&$spec) { $spec['id'] = array( 'title' => E::ts('ID'), 'type' => CRM_Utils_Type::T_INT, - 'api_required' => false + 'api.required' => false ); $spec['name'] = array( 'title' => E::ts('Name'), 'type' => CRM_Utils_Type::T_STRING, - 'api_required' => true + 'api.required' => true ); } \ No newline at end of file