Commit 1d9b0764 authored by ErikHommel's avatar ErikHommel
Browse files

locally tested action to process multi petitions

parent 34edfe88
<?php
namespace Civi\Aivlspecificactions\Actions;
use \Civi\ActionProvider\Action\AbstractAction;
use Civi\ActionProvider\Exception\ExecutionException;
use \Civi\ActionProvider\Exception\InvalidParameterException;
use Civi\ActionProvider\Parameter\OptionGroupByNameSpecification;
use \Civi\ActionProvider\Parameter\ParameterBagInterface;
use \Civi\ActionProvider\Parameter\SpecificationBag;
use \Civi\ActionProvider\Parameter\Specification;
use Civi\Core\Lock\NullLock;
use Civi\FormProcessor\API\Exception;
use CRM_Aivlspecificactions_ExtensionUtil as E;
/**
* Class MultiPetition - process 1 or more petition signatures in 1 go
*
* @package Civi\Aivlspecificactions\Actions
* @author Erik Hommel (CiviCooP) <erik.hommel@civicoop.org>
* @date 16 Sep 2021
* @license AGPL-3.0
*/
class MultiPetition extends AbstractAction {
/**
* @return SpecificationBag
*/
public function getParameterSpecification() {
$specs = new SpecificationBag();
$specs->addSpecification(new Specification('petition_ids', 'Integer', E::ts('Petition(s)'), FALSE, NULL, NULL, [], TRUE));
$specs->addSpecification(new Specification('contact_id', 'Integer', E::ts('Contact ID'), TRUE, NULL));
$specs->addSpecification(new Specification('more_information', 'Integer', E::ts('More Information'), FALSE, 0));
return $specs;
}
/**
* @return SpecificationBag
*/
public function getConfigurationSpecification() {
return new SpecificationBag();
}
/**
* Do the actual action - process multiple petition signatures
*
* @param ParameterBagInterface $parameterBag
* @param ParameterBagInterface $output
* @throws ExecutionException
*/
public function doAction(ParameterBagInterface $parameterBag, ParameterBagInterface $output) {
$petitionIds = $parameterBag->getParameter('petition_ids');
if ($petitionIds) {
$contactId = (int) $parameterBag->getParameter('contact_id');
$moreInformation = $parameterBag->getParameter('more_information');
foreach ($petitionIds as $petitionId) {
try {
civicrm_api3('FormProcessor', 'petition_form', [
'petition_flag' => 1,
'campaign_id' => (int) $petitionId,
'contact_id' => $contactId,
'more_information' => $moreInformation,
]);
}
catch (\CiviCRM_API3_Exception $ex) {
\Civi::log()->error(E::ts("Could not process petition signature in ") . __METHOD__
. E::ts(" with petition ID: ") . $petitionId . E::ts(", contact ID: ") . $contactId
. E::ts(" and more information: ") . $moreInformation);
}
}
}
}
/**
* Returns the specification of the output parameters of this action.
*
* This function could be overriden by child classes.
*
* @return SpecificationBag
*/
public function getOutputSpecification() {
return new SpecificationBag();
}
}
......@@ -30,6 +30,8 @@ class CompilerPass implements CompilerPassInterface {
['SetEmailPrefs', 'Civi\Aivlspecificactions\Actions\SetEmailPrefs', E::ts('Set AIVL Email Preferences'), []]);
$actionProviderDefinition->addMethodCall('addAction',
['UpdateCallAssignment', 'Civi\Aivlspecificactions\Actions\UpdateCallAssignment', E::ts('Update AIVL TM Call Assignment'), []]);
$actionProviderDefinition->addMethodCall('addAction',
['MultiPetition', 'Civi\Aivlspecificactions\Actions\MultiPetition', E::ts('Proces multiple AIVL petitions'), []]);
}
if ($container->hasDefinition('form_processor_type_factory')) {
$typeFactoryDefinition = $container->getDefinition('form_processor_type_factory');
......
......@@ -18,10 +18,6 @@ use \CRM_Aivlspecificactions_ExtensionUtil as E;
class MultiPetition extends AbstractType implements OptionListInterface {
protected $normalizedOptions;
protected $denormalizedOptions;
protected $options;
public function __construct() {
......@@ -90,19 +86,6 @@ class MultiPetition extends AbstractType implements OptionListInterface {
* @throws \CRM_Core_Exception
*/
public function validateValue($value, $allValues = []) {
$options = $this->getOptions($allValues);
$type = 'Integer';
if (!is_array($value)) {
if (\CRM_Utils_Type::validate($value, $type, FALSE) === NULL) {
return FALSE;
}
if (!isset($options[$value])) {
return FALSE;
}
}
else {
return FALSE;
}
return TRUE;
}
......@@ -113,50 +96,16 @@ class MultiPetition extends AbstractType implements OptionListInterface {
return \CRM_Utils_Type::T_INT;
}
/**
* Normalize the input value.
*
* @param $value
*
* @return mixed
*/
public function normalizeValue($value) {
$this->loadOptions();
if (isset($this->normalizedOptions[$value])) {
return $this->normalizedOptions[$value];
}
else {
return NULL;
}
}
/**
* Denormalize the input value.
*
* @param $value
*
* @return mixed
*/
public function denormalizeValue($value) {
$this->loadOptions();
if (isset($this->denormalizedOptions[$value])) {
return $this->denormalizedOptions[$value];
}
else {
return NULL;
}
}
/**
* @param array $params
* @return array
*/
public function getOptions($params) {
$this->loadOptions();
$selectedCampaigns = $this->configuration->get('petition_id');
foreach ($this->options as $campaignId => $campaignTitle) {
if (!in_array($campaignId, $selectedCampaigns)) {
unset($this->options[$campaignId]);
$selectedPetitions = $this->configuration->get('petition_id');
foreach ($this->options as $petitionId => $petitionTitle) {
if (!in_array($petitionId, $selectedPetitions)) {
unset($this->options[$petitionId]);
}
}
return $this->options;
......@@ -166,16 +115,13 @@ class MultiPetition extends AbstractType implements OptionListInterface {
*
*/
protected function loadOptions() {
if ($this->options != NULL && $this->normalizedOptions != NULL && !$this->denormalizedOptions != NULL) {
if ($this->options != NULL) {
return;
}
$petitions = $this->getPetitions();
$this->normalizedOptions = [];
$this->options = [];
foreach ($petitions as $petitionId => $petitionTitle) {
$this->options[$petitionId] = $petitionTitle;
$this->normalizedOptions[$petitionId] = $petitionId;
$this->denormalizedOptions[$petitionId] = $petitionId;
}
}
......@@ -185,8 +131,6 @@ class MultiPetition extends AbstractType implements OptionListInterface {
*/
public function setConfiguration(ConfigurationBag $configuration) {
parent::setConfiguration($configuration);
$this->normalizedOptions = NULL;
$this->denormalizedOptions = NULL;
$this->options = NULL;
return $this;
}
......@@ -196,8 +140,6 @@ class MultiPetition extends AbstractType implements OptionListInterface {
*/
public function setDefaults() {
parent::setDefaults();
$this->normalizedOptions = NULL;
$this->denormalizedOptions = NULL;
$this->options = NULL;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment