Skip to content

Commit

Permalink
[#20] form processor integration
Browse files Browse the repository at this point in the history
  • Loading branch information
bjendres committed Oct 8, 2019
1 parent 3c9eb52 commit 3336581
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 1 deletion.
115 changes: 115 additions & 0 deletions Civi/I3val/ActionProvider/Action/RequestContactUpdate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php
/*-------------------------------------------------------+
| Ilja's Input Validation Extension |
| Amnesty International Vlaanderen |
| Copyright (C) 2017-2019 SYSTOPIA |
| Author: B. Endres (endres@systopia.de) |
| http://www.systopia.de/ |
+--------------------------------------------------------+
| This program is released as free software under the |
| Affero GPL license. You can redistribute it and/or |
| modify it under the terms of this license which you |
| can read by viewing the included agpl.txt or online |
| at www.gnu.org/licenses/agpl.html. Removal of this |
| copyright header is strictly prohibited without |
| written permission from the original author(s). |
+--------------------------------------------------------*/

namespace Civi\I3val\ActionProvider\Action;

use CRM_I3val_ExtensionUtil as E;

use \Civi\ActionProvider\Action\AbstractAction;
use \Civi\ActionProvider\Parameter\ParameterBagInterface;
use \Civi\ActionProvider\Parameter\Specification;
use \Civi\ActionProvider\Parameter\SpecificationBag;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class RequestContactUpdate extends AbstractAction implements CompilerPassInterface {

/**
* Register this one action: RequestContactUpdate
*/
public function process(ContainerBuilder $container) {
if (!$container->hasDefinition('action_provider')) {
return;
}
$typeFactoryDefinition = $container->getDefinition('action_provider');
$typeFactoryDefinition->addMethodCall('addAction', ['RequestContactUpdate', 'Civi\I3val\ActionProvider\Action\RequestContactUpdate', E::ts('Request Contact Update'), [
AbstractAction::SINGLE_CONTACT_ACTION_TAG,
AbstractAction::DATA_RETRIEVAL_TAG
]]);
}

/**
* Returns the specification of the configuration options for the actual action.
*
* @return SpecificationBag specs
*/
public function getConfigurationSpecification() {
// load activity types
$activity_types = ['' => E::ts("none")];
$types = \civicrm_api3('OptionValue', 'get', [
'option.limit' => 0,
'option_group_id' => 'activity_type',
'return' => 'value,label']);
foreach ($types['values'] as $type) {
$types[$type['value']] = $type['label'];
}

// specify config
return new SpecificationBag([
new Specification('activity_type_id', 'Integer', E::ts('Default Activity Type'), false, null, null, $activity_types, false),
]);
}

/**
* Returns the specification of the parameters of the actual action.
*
* @return SpecificationBag specs
*/
public function getParameterSpecification() {
$specs = [];
$specs[] = new Specification('activity_type_id', 'Integer', E::ts('Activity Type'), false, null, null, null, false);
$specs[] = new Specification('id', 'Integer', E::ts('Contact ID'), false, null, null, null, false);
$specs[] = new Specification('i3val_note', 'String', E::ts('Note'), false, null, null, null, false);
$specs[] = new Specification('i3val_schedule_date', 'String', E::ts('Requested Change Date'), false, null, null, null, false);
$specs[] = new Specification('i3val_parent_id', 'Integer', E::ts('Linked Activity ID'), false, null, null, null, false);

// calculate handler input fields
$config = \CRM_I3val_Configuration::getConfiguration();
$handlers = $config->getHandlersForEntity('Contact');
foreach ($handlers as $handler) {
/** @var $handler \CRM_I3val_ActivityHandler */
$fields = $handler->getField2Label();
foreach ($fields as $field_name => $field_label) {
$specs[] = new Specification($field_name, 'String', $field_label, false, null, null, null, false);
}
}

return new SpecificationBag($specs);
}

/**
* Run the action
*
* @param ParameterBagInterface $parameters
* The parameters to this action.
* @param ParameterBagInterface $output
* The parameters this action can send back
* @return void
*/
protected function doAction(ParameterBagInterface $parameters, ParameterBagInterface $output) {
$params = $parameters->toArray();
// override if necessary
foreach (['activity_type_id', 'i3val_note', 'i3val_schedule_date', 'i3val_parent_id'] as $field_name) {
if (empty($params[$field_name])) {
$params[$field_name] = $this->configuration->getParameter($field_name);
}
}

// execute
$result = \civicrm_api3('Contact', 'request_update', $params);
}
}
13 changes: 12 additions & 1 deletion i3val.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/*-------------------------------------------------------+
| Ilja's Input Validation Extension |
| Amnesty International Vlaanderen |
| Copyright (C) 2017 SYSTOPIA |
| Copyright (C) 2019 SYSTOPIA |
| Author: B. Endres (endres@systopia.de) |
| http://www.systopia.de/ |
+--------------------------------------------------------+
Expand All @@ -17,6 +17,17 @@

require_once 'i3val.civix.php';

use \Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* Implements hook_civicrm_container()
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_container/
*/
function i3val_civicrm_container(ContainerBuilder $container) {
$container->addCompilerPass(new Civi\I3val\ActionProvider\Action\RequestContactUpdate());
}

/**
* Implements hook_civicrm_buildForm()
* to inject JS code for UI manipulations
Expand Down
3 changes: 3 additions & 0 deletions info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
<ver>5.0</ver>
</compatibility>
<comments>Funded by Amnesty International Vlaanderen</comments>
<classloader>
<psr4 prefix="Civi\" path="Civi" />
</classloader>
<civix>
<namespace>CRM/I3val</namespace>
</civix>
Expand Down

0 comments on commit 3336581

Please sign in to comment.