Commit 34119dd7 authored by jaapjansma's avatar jaapjansma

Added try out functionality

parent a2c0f8d8
......@@ -3,6 +3,7 @@ Version 1.7 (not yet released)
* fixed notice in FormProcessor->invokeFormProcessor
* Fixed #9 Error in the default value
* Fixed #11 Errors in DateType validation
* Added try out functionality.
Version 1.6
===========
......
<?php
/**
* @author Jaap Jansma <jaap.jansma@civicoop.org>
* @license AGPL-3.0
*/
use CRM_FormProcessor_ExtensionUtil as E;
class CRM_FormProcessor_Form_RunFormProcessor extends CRM_Core_Form {
protected $defaultFields = [];
protected $fields = [];
protected $formProcessorName;
/**
* Preprocess form.
*
* This is called before buildForm. Any pre-processing that
* needs to be done for buildForm should be done here.
*
* This is a virtual function and should be redefined if needed.
*/
public function preProcess() {
parent::preProcess(); // TODO: Change the autogenerated stub
$this->formProcessorName = CRM_Utils_Request::retrieve('_qf_formProcessorName', 'String', $this, TRUE);
}
/**
* This virtual function is used to build the form.
*
* It replaces the buildForm associated with QuickForm_Page. This allows us
* to put preProcess in front of the actual form building routine
*/
public function buildQuickForm() {
$this->add('hidden', '_qf_formProcessorName');
$this->setDefaults(['_qf_formProcessorName' => $this->formProcessorName]);
$defaultFields = civicrm_api3('FormProcessorDefaults', 'getfields', ['api_action' => $this->formProcessorName]);
$this->defaultFields = $defaultFields['values'];
$defaultFieldNames = [];
foreach($this->defaultFields as $field) {
$attributes = [
'style' => 'min-width:250px',
'class' => 'huge'
];
if (isset($field['options']) && is_array($field['options'])) {
$attributes['class'] .= ' crm-select2';
$attributes['placeholder'] = E::ts('- select -');
if ($field['formprocessor.is_multiple']) {
$attributes['multiple'] = 'multiple';
}
$this->add('select', $this->formProcessorName . '_default_' . $field['name'], $field['title'], $field['options'], $field['api.required'], $attributes);
} else {
$this->add('text', $this->formProcessorName . '_default_' . $field['name'], $field['title'], $attributes, $field['api.required']);
}
$defaultFieldNames[] = $this->formProcessorName.'_default_'.$field['name'];
}
$this->assign('defaultFieldNames', $defaultFieldNames);
$fields = civicrm_api3('FormProcessor', 'getfields', ['api_action' => $this->formProcessorName]);
$this->fields = $fields['values'];
$fieldNames = [];
foreach($this->fields as $field) {
$attributes = [
'style' => 'min-width:250px',
'class' => 'huge'
];
if (isset($field['options']) && is_array($field['options'])) {
$attributes['class'] .= ' crm-select2';
$attributes['placeholder'] = E::ts('- select -');
if ($field['formprocessor.is_multiple']) {
$attributes['multiple'] = 'multiple';
}
$this->add('select', $field['name'], $field['title'], $field['options'], $field['api.required'], $attributes);
} else {
$this->add('text', $field['name'], $field['title'], $attributes, $field['api.required']);
}
$fieldNames[] = $field['name'];
}
$this->assign('fieldNames', $fieldNames);
$this->addButtons([
[
'type' => 'refresh',
'name' => E::ts('Load'),
'isDefault' => FALSE,
],
[
'type' => 'submit',
'name' => E::ts('Submit'),
'isDefault' => FALSE,
],
]);
if (isset($this->_submitValues['_qf_RunFormProcessor_refresh']) && $this->_submitValues['_qf_RunFormProcessor_refresh'] == E::ts('Load')) {
$this->loadDefaultData($this->formProcessorName);
}
}
protected function loadDefaultData($formProcessorName) {
$defaultParams = [];
foreach($this->defaultFields as $field) {
$submitFieldName = $formProcessorName.'_default_'.$field['name'];
if (isset($this->_submitValues[$submitFieldName])) {
$defaultParams[$field['name']] = $this->_submitValues[$submitFieldName];
}
}
$defaultParams['version'] = 3;
$default = civicrm_api('FormProcessorDefaults', $formProcessorName, $defaultParams);
if (!isset($default['is_error']) || !$default['is_error']) {
$this->setDefaults($default);
foreach($default as $k=>$v) {
$idx = $this->_elementIndex[$k];
$this->_elements[$idx]->setValue($v);
}
} else {
CRM_Core_Session::setStatus($default['error_message']);
$this->clearForm();
}
}
protected function clearForm() {
foreach($this->fields as $field) {
$idx = $this->_elementIndex[$field['name']];
$this->_elements[$idx]->setValue('');
}
}
/**
* Performs the server side validation.
*
* @return bool
* true if no error found
* @throws HTML_QuickForm_Error
* @since 1.0
*/
public function validate() {
$return = parent::validate(); // TODO: Change the autogenerated stub
if (isset($this->_submitValues['_qf_RunFormProcessor_refresh']) && $this->_submitValues['_qf_RunFormProcessor_refresh'] == E::ts('Load')) {
foreach($this->fields as $field) {
if (isset($this->_errors[$field['name']])) {
unset($this->_errors[$field['name']]);
}
}
} elseif (isset($this->_submitValues['_qf_RunFormProcessor_submit']) && $this->_submitValues['_qf_RunFormProcessor_submit'] == E::ts('Submit')) {
foreach($this->defaultFields as $field) {
$submitFieldName = $this->formProcessorName.'_default_'.$field['name'];
if (isset($this->_errors[$submitFieldName])) {
unset($this->_errors[$submitFieldName]);
}
}
}
return (0 == count($this->_errors));
}
/**
* Called after the form is validated.
*
* Any processing of form state etc should be done in this function.
* Typically all processing associated with a form should be done
* here and relevant state should be stored in the session
*
* This is a virtual function and should be redefined if needed
*/
public function postProcess() {
if (isset($this->_submitValues['_qf_RunFormProcessor_submit']) && $this->_submitValues['_qf_RunFormProcessor_submit'] == E::ts('Submit') && !count($this->_errors)) {
$params = [];
foreach($this->fields as $field) {
$submitFieldName = $field['name'];
if (isset($this->_submitValues[$submitFieldName])) {
$params[$field['name']] = $this->_submitValues[$submitFieldName];
}
}
$params['version'] = 3;
$result = civicrm_api('FormProcessor', $this->formProcessorName, $params);
$this->assign('result_json', json_encode($result, JSON_PRETTY_PRINT));
if (!isset($default['is_error']) || !$default['is_error']) {
$this->clearForm();
}
}
}
}
......@@ -100,7 +100,7 @@
}
$field = array(
'name' => $input['name'],
'title' => $input['name'],
'title' => $input['title'],
'description' => '',
'type' => $input['type']->getCrmType(),
'api.required' => $input['is_required'],
......@@ -109,6 +109,7 @@
);
if ($input['type'] instanceof OptionListInterface) {
$field['options'] = $input['type']->getOptions($apiParams);
$field['formprocessor.is_multiple'] = $input['type']->isMultiple(); // We add our own spec here, as an option for is multiple does not exists.
}
// Set a default value
if (isset($input['default_value']) && $input['default_value'] != '') {
......
......@@ -53,7 +53,7 @@ class FormProcessorDefaults extends FormProcessor implements API_ProviderInterfa
foreach ($formProcessor['default_data_inputs'] as $input) {
$field = array(
'name' => $input['name'],
'title' => $input['name'],
'title' => $input['title'],
'description' => '',
'type' => $input['type']->getCrmType(),
'api.required' => $input['is_required'],
......@@ -62,6 +62,7 @@ class FormProcessorDefaults extends FormProcessor implements API_ProviderInterfa
);
if ($input['type'] instanceof OptionListInterface) {
$field['options'] = $input['type']->getOptions($apiParams);
$field['formprocessor.is_multiple'] = $input['type']->isMultiple(); // We add our own spec here, as an option for is multiple does not exists.
}
// Set a default value
if (isset($input['default_value']) && $input['default_value'] != '') {
......
......@@ -115,7 +115,7 @@ class DataBag {
if ($input) {
return $this->inputData[$input->id];
}
} elseif ($splitted_alias[0] == 'action') {
} elseif ($splitted_alias[0] == 'action' && isset($this->actionData[$splitted_alias[1]][$splitted_alias[2]])) {
return $this->actionData[$splitted_alias[1]][$splitted_alias[2]];
}
return null;
......
......@@ -4,112 +4,113 @@
* @author Jaap Jansma (CiviCooP) <jaap.jansma@civicoop.org>
* @license http://www.gnu.org/licenses/agpl-3.0.html
*/
namespace Civi\FormProcessor\Type;
use \Civi\FormProcessor\Config\ConfigurationBag;
use \Civi\FormProcessor\Config\Specification;
use \Civi\FormProcessor\Config\SpecificationBag;
abstract class AbstractType implements \JsonSerializable {
/**
* @var string
*/
protected $name;
/**
* @var string
*/
protected $label;
/**
* @var ConfigurationBag
*/
protected $configuration;
/**
* @var string
*/
protected $default_value_description;
/**
* @var ConfigurationBag
*/
protected $defaultConfiguration;
public function __construct($name, $label) {
$this->name = $name;
$this->label = $label;
}
/**
* Returns whether the provided value is a valid
*
* @param mixed $value
namespace Civi\FormProcessor\Type;
use \Civi\FormProcessor\Config\ConfigurationBag;
use \Civi\FormProcessor\Config\Specification;
use \Civi\FormProcessor\Config\SpecificationBag;
abstract class AbstractType implements \JsonSerializable {
/**
* @var string
*/
protected $name;
/**
* @var string
*/
protected $label;
/**
* @var ConfigurationBag
*/
protected $configuration;
/**
* @var string
*/
protected $default_value_description;
/**
* @var ConfigurationBag
*/
protected $defaultConfiguration;
public function __construct($name, $label) {
$this->name = $name;
$this->label = $label;
}
/**
* Returns whether the provided value is a valid
*
* @param mixed $value
* @param array $allValues
* @return bool
*/
abstract public function validateValue($value, $allValues=array());
/**
* Returns the name of the type.
*
* @return string
*/
public function getName() {
return $this->name;
}
/**
* Returns the type number from CRM_Utils_Type
*/
public function getCrmType() {
return false;
}
/**
* Returns the label of the type.
*
* @return string
*/
public function getLabel() {
return $this->label;
}
/**
* Get the configuration specification
*
* @return SpecificationBag
*/
public function getConfigurationSpecification() {
return new SpecificationBag();
}
/**
* @return bool;
*/
public function validateConfiguration() {
if ($this->configuration === null) {
return false;
}
return SpecificationBag::validate($this->configuration, $this->getConfigurationSpecification());
}
/**
* @return ConfigurationBag
*/
public function getConfiguration() {
return $this->configuration;
}
/**
* @return ConfigurationBag
*/
public function getDefaultConfiguration() {
return $this->defaultConfiguration;
}
*
* @return bool
*/
abstract public function validateValue($value, $allValues = []);
/**
* Returns the name of the type.
*
* @return string
*/
public function getName() {
return $this->name;
}
/**
* Returns the type number from CRM_Utils_Type
*/
public function getCrmType() {
return FALSE;
}
/**
* Returns the label of the type.
*
* @return string
*/
public function getLabel() {
return $this->label;
}
/**
* Get the configuration specification
*
* @return SpecificationBag
*/
public function getConfigurationSpecification() {
return new SpecificationBag();
}
/**
* @return bool;
*/
public function validateConfiguration() {
if ($this->configuration === NULL) {
return FALSE;
}
return SpecificationBag::validate($this->configuration, $this->getConfigurationSpecification());
}
/**
* @return ConfigurationBag
*/
public function getConfiguration() {
return $this->configuration;
}
/**
* @return ConfigurationBag
*/
public function getDefaultConfiguration() {
return $this->defaultConfiguration;
}
/**
* Sets the default values of this action
*/
......@@ -117,68 +118,70 @@
$this->configuration = new ConfigurationBag();
$this->defaultConfiguration = new ConfigurationBag();
foreach($this->getConfigurationSpecification() as $spec) {
if ($spec->getDefaultValue() !== null) {
foreach ($this->getConfigurationSpecification() as $spec) {
if ($spec->getDefaultValue() !== NULL) {
$this->configuration->set($spec->getName(), $spec->getDefaultValue());
$this->defaultConfiguration->set($spec->getName(), $spec->getDefaultValue());
}
}
}
/**
* Normalize the input value.
*
* @param $value
*
* @return mixed
*/
/**
* Normalize the input value.
*
* @param $value
*
* @return mixed
*/
public function normalizeValue($value) {
return $value;
}
/**
* Denormalize the input value.
*
* @param $value
*
* @return mixed
*/
public function denormalizeValue($value) {
return $value;
}
/**
* @param ConfigurationBag $configuration
*/
public function setConfiguration(ConfigurationBag $configuration) {
$this->configuration = $configuration;
return $this;
}
/**
* Returns the default value
*
* @param $defaultValue
*
* @return mixed
*/
public function getDefaultValue($defaultValue) {
return $defaultValue;
/**
* Denormalize the input value.
*
* @param $value
*
* @return mixed
*/
public function denormalizeValue($value) {
return $value;
}
public function toArray() {
$return['name'] = $this->name;
$return['label'] = $this->label;
$return['default_value_description'] = $this->default_value_description;
$return['configuration_spec'] = $this->getConfigurationSpecification()->toArray();
$return['default_configuration'] = null;
/**
* @param ConfigurationBag $configuration
*/
public function setConfiguration(ConfigurationBag $configuration) {
$this->configuration = $configuration;
return $this;
}
/**
* Returns the default value
*
* @param $defaultValue
*
* @return mixed
*/
public function getDefaultValue($defaultValue) {
return $defaultValue;
}
public function toArray() {
$return['name'] = $this->name;
$return['label'] = $this->label;
$return['default_value_description'] = $this->default_value_description;
$return['configuration_spec'] = $this->getConfigurationSpecification()
->toArray();
$return['default_configuration'] = NULL;
if ($this->getDefaultConfiguration()) {
$return['default_configuration'] = $this->getDefaultConfiguration()->toArray();
$return['default_configuration'] = $this->getDefaultConfiguration()
->toArray();
}
return $return;
}
/**
}
/**
* Returns the data structure to serialize it as a json
*/
public function jsonSerialize() {
......@@ -189,5 +192,5 @@
}
return $return;
}
}
}
......@@ -5,40 +5,40 @@
* @license http://www.gnu.org/licenses/agpl-3.0.html
*/
namespace Civi\FormProcessor\Type;
namespace Civi\FormProcessor\Type;
use Civi\FormProcessor\Config\ConfigurationBag;
use \Civi\FormProcessor\Config\Specification;
use \Civi\FormProcessor\Config\SpecificationBag;
use \Civi\FormProcessor\Type\AbstractType;
use \Civi\FormProcessor\Type\OptionListInterface;
use Civi\FormProcessor\Config\ConfigurationBag;
use \Civi\FormProcessor\Config\Specification;
use \Civi\FormProcessor\Config\SpecificationBag;
use \Civi\FormProcessor\Type\AbstractType;
use \Civi\FormProcessor\Type\OptionListInterface;
use \CRM_FormProcessor_ExtensionUtil as E;
use \CRM_FormProcessor_ExtensionUtil as E;
class CampaignType extends AbstractType implements OptionListInterface {
class CampaignType extends AbstractType implements OptionListInterface {
protected $normalizedOptions;
protected $normalizedOptions;
protected $denormalizedOptions;
protected $denormalizedOptions;
protected $options;
protected $options;
/**
* Get the configuration specification
*
* @return SpecificationBag
*/
public function getConfigurationSpecification() {
return new SpecificationBag(array(
new Specification('value_field', 'String', E::ts('Value field'), true, 'id', null, array(
'id' => E::ts('ID'),
'name' => E::ts('Name of the campaign'),
)),
));
}
/**
* Get the configuration specification
*
* @return SpecificationBag
*/
public function getConfigurationSpecification() {
return new SpecificationBag([
new Specification('value_field', 'String', E::ts('Value field'), TRUE, 'id', NULL, [
'id' => E::ts('ID'),
'name' => E::ts('Name of the campaign'),
]),
]);
}
public function validateValue($value, $allValues=array()) {
public function validateValue($value, $allValues = []) {
$options = $this->getOptions($allValues);
$valueAttribute = $use_label = $this->configuration->get('value_field') ? $this->configuration->get('value_field') : 'id';
......@@ -55,10 +55,11 @@
if (!isset($options[$value])) {
return FALSE;
}
} else {
}
else {
return FALSE;
}
return true;
return TRUE;
}