Commit f4ee6b6e authored by jaapjansma's avatar jaapjansma

Created a container with context awareness

parent a617a5b8
......@@ -61,7 +61,7 @@ abstract class AbstractAction {
*/
public function getName() {
$className = get_class($this);
return str_replace('\\', '_', $className);
return $className;
}
/**
......
......@@ -32,7 +32,7 @@ class AddToGroup extends AbstractAction {
*/
public function getConfigurationSpecification() {
return new SpecificationBag(array(
new Specification('group_id', 'Integer', E::ts('Select group(s)'), true, null, 'Group', null, TRUE),
new Specification('group_id', 'Integer', E::ts('Select group'), true, null, 'Group', null, FALSE),
));
}
......
<?php
namespace Civi\ActionProvider;
use \Civi\ActionProvider\Provider;
class Container {
/**
* @var Provider
*/
protected $defaultProvider;
protected $providerContexts = array();
private static $instance;
protected function __construct() {
$this->defaultProvider = Provider::getInstance();
}
/**
* @return Container
*/
public static function getInstance() {
if (!self::$instance) {
self::$instance = new Container();
}
return self::$instance;
}
/**
* return Provider
*/
public function getDefaultProvider() {
return $this->defaultProvider;
}
/**
* Returns the provider object the name of the context.
*
* @param string $context
* @return Provider
*/
public function getProviderByContext($context) {
if (isset($this->providerContexts[$context])) {
return $this->providerContexts;
}
return $this->defaultProvider;
}
/**
* Adds a Provider for a certain context.
*
* A context could be the name of the extension etc... *
* The name of the context is defined by other parts of the system.
* This way one add a specific context from with an extenion.
*
* @param string $context
* @param Provider $provider
* @return Container
*/
public function addContextProvider($context, Provider $provider) {
$this->providerContexts[$context] = $provider;
return $this;
}
}
......@@ -95,7 +95,7 @@ class AddConfigToQuickForm {
public static function getSubmittedConfiguration(\CRM_Core_Form $form, AbstractAction $action) {
$prefix = $action->getName();
$submitted_configuration = array();
$submittedValues = $form->get('_submitValues');
$submittedValues = $form->getVar('_submitValues');
foreach($action->getConfigurationSpecification() as $config_field) {
if (isset($submittedValues[$prefix.$config_field->getName()])) {
$submitted_configuration[$config_field->getName()] = $submittedValues[$prefix.$config_field->getName()];
......
......@@ -2,6 +2,22 @@
require_once 'action_provider.civix.php';
use CRM_ActionProvider_ExtensionUtil as E;
use Symfony\Component\DependencyInjection\Definition;
/**
* Implements hook_civicrm_container()
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_container/
*/
function action_provider_civicrm_container($container) {
if (!$container->has('action_provider')) {
// Only add our container when it does not exists.
// This way other extensions might override the container method.
$actionProviderDefinition = new Definition('Civi\ActionProvider\Container');
$actionProviderDefinition->setFactory(array('Civi\ActionProvider\Container','getInstance'));
$container->setDefinition('action_provider', $actionProviderDefinition);
}
}
/**
* Implements hook_civicrm_config().
......
<?xml version="1.0"?>
<extension key="action-provider" type="module">
<file>action_provider</file>
<name>Actin Provider</name>
<name>Action Provider</name>
<description>This is a base extension providing an abstract action interface to be used in other extensions.</description>
<license>AGPL-3.0</license>
<maintainer>
......
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