Commit 34edfe88 authored by ErikHommel's avatar ErikHommel
Browse files

fix issue 7746 - multi petition input type

parent 8688a70f
......@@ -5,6 +5,7 @@ namespace Civi\Aivlspecificactions;
* Compiler Class for action provider
*/
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
......@@ -30,5 +31,10 @@ class CompilerPass implements CompilerPassInterface {
$actionProviderDefinition->addMethodCall('addAction',
['UpdateCallAssignment', 'Civi\Aivlspecificactions\Actions\UpdateCallAssignment', E::ts('Update AIVL TM Call Assignment'), []]);
}
if ($container->hasDefinition('form_processor_type_factory')) {
$typeFactoryDefinition = $container->getDefinition('form_processor_type_factory');
$typeFactoryDefinition->addMethodCall('addType', [new Definition('Civi\Aivlspecificactions\FormProcessor\Type\MultiPetition')]);
}
}
}
<?php
/**
* @author Erik Hommel (CiviCooP) <erik.hommel@civicoop.org>
* @date 16 Sep 2021
* @license http://www.gnu.org/licenses/agpl-3.0.html
*/
namespace Civi\Aivlspecificactions\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 \CRM_Aivlspecificactions_ExtensionUtil as E;
class MultiPetition extends AbstractType implements OptionListInterface {
protected $normalizedOptions;
protected $denormalizedOptions;
protected $options;
public function __construct() {
parent::__construct('aivl_multi_petition', E::ts('Petition(s)'));
}
/**
* Get the configuration specification
*
* @return SpecificationBag
*/
public function getConfigurationSpecification() {
$petitions = [];
$apiPetitions = $this->getPetitions();
foreach ($apiPetitions as $petitionId => $petitionTitle) {
$petitions[$petitionId] = $petitionTitle;
}
return new SpecificationBag([
new Specification('petition_id', 'Integer', E::ts('Petition(s)'), TRUE, NULL, NULL, $petitions, TRUE),
]);
}
/**
* Method to get the active petitions with api3 or api4
*
* @return array
*/
private function getPetitions() {
$result = [];
if (function_exists('civicrm_api4')) {
try {
$petitions = \Civi\Api4\Campaign::get()
->addSelect('id', 'title')
->addWhere('is_active', '=', TRUE)
->addWhere('campaign_type_id', '=', \Civi::service('aivlgeneric')->getPetitionCampaignTypeId())
->execute();
foreach ($petitions as $petition) {
$result[$petition['id']] = $petition['title'];
}
}
catch (\API_Exception $ex) {
}
}
else {
try {
$petitions = civicrm_api3('Campaign', 'get', [
'return' => ["id", "title"],
'is_active' => 1,
'campaign_type_id' => \Civi::service('aivlgeneric')->getPetitionCampaignTypeId(),
'options' => ['limit' => 0],
]);
foreach ($petitions['values'] as $petitionId => $petition) {
$result[$petitionId] = $petition['title'];
}
}
catch (\CiviCRM_API3_Exception $ex) {
}
}
return $result;
}
/**
* @param mixed $value
* @param array $allValues
* @return bool
* @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;
}
/**
* @return false|int
*/
public function getCrmType() {
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]);
}
}
return $this->options;
}
/**
*
*/
protected function loadOptions() {
if ($this->options != NULL && $this->normalizedOptions != NULL && !$this->denormalizedOptions != 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;
}
}
/**
* @param ConfigurationBag $configuration
* @return $this|MultiPetition
*/
public function setConfiguration(ConfigurationBag $configuration) {
parent::setConfiguration($configuration);
$this->normalizedOptions = NULL;
$this->denormalizedOptions = NULL;
$this->options = NULL;
return $this;
}
/**
* Sets the default values of this action
*/
public function setDefaults() {
parent::setDefaults();
$this->normalizedOptions = NULL;
$this->denormalizedOptions = NULL;
$this->options = NULL;
}
/**
* Returns true when this field is a multiple field.
*
* @return bool
*/
public function isMultiple() {
return TRUE;
}
}
......@@ -14,8 +14,8 @@
<url desc="Support">https://civicoop.org</url>
<url desc="Licensing">http://www.gnu.org/licenses/agpl-3.0.html</url>
</urls>
<releaseDate>2021-06-17</releaseDate>
<version>1.16</version>
<releaseDate>2021-09-16</releaseDate>
<version>1.17</version>
<develStage>stable</develStage>
<compatibility>
<ver>4.7</ver>
......
Supports Markdown
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