Commit 62bc7244 authored by jaapjansma's avatar jaapjansma
Browse files

Added Yes/No OptionList type

parent f6a7c1af
......@@ -10,6 +10,7 @@ Version 1.3
* Added a type with a list of weekdays
* Added field type for time
* Added field type for formatted decimal numbers
* Added field type for Yes/No values as an option list
Version 1.2
===========
......
......@@ -25,6 +25,7 @@
$this->addType(new DateType(E::ts('Date')));
$this->addType(new TimeType(E::ts('Time')));
$this->addType(new GenericType('Boolean', E::ts('Yes/No')));
$this->addType(new YesNoOptionListType('YesNoOptionList', E::ts('Yes/No as Option List')));
$this->addType(new OptionGroupType('OptionGroup', E::ts('Option Group')));
$this->addType(new CountryType('Country', E::ts('Country')));
$this->addType(new MailingGroupType('MailingGroup', E::ts('Mailing Group')));
......
<?php
/**
* @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\Specification;
use \Civi\FormProcessor\Config\SpecificationBag;
use \Civi\FormProcessor\Type\AbstractType;
use \Civi\FormProcessor\Type\OptionListInterface;
use \CRM_FormProcessor_ExtensionUtil as E;
class YesNoOptionListType extends AbstractType implements OptionListInterface {
private $options;
private $optionsLoaded = false;
/**
* Get the configuration specification
*
* @return SpecificationBag
*/
public function getConfigurationSpecification() {
return new SpecificationBag(array(
new Specification('yes_label', 'String', E::ts('Yes label'), true, E::ts('Yes'), null, null, false),
new Specification('no_label', 'String', E::ts('No label'), true, E::ts('No'), null, null, false),
));
}
public function validateValue($value, $allValues=array()) {
return true;
}
/**
* Returns the type number from CRM_Utils_Type
*/
public function getCrmType() {
return \CRM_Utils_Type::T_BOOLEAN;
}
public function getOptions($params) {
$this->loadOptions();
return $this->options;
}
/**
* Normalize the input value.
*
* @param $value
*
* @return mixed
*/
public function normalizeValue($value) {
return $value ? '1': '0';
}
/**
* Denormalize the input value.
*
* @param $value
*
* @return mixed
*/
public function denormalizeValue($value) {
return $value ? '1': '0';
}
private function loadOptions() {
if ($this->optionsLoaded) {
return;
}
$this->options = array();
$yes_label = $this->configuration->get('yes_label') ? $this->configuration->get('yes_label') : E::ts('Yes');
$no_label = $this->configuration->get('no_label') ? $this->configuration->get('no_label') : E::ts('No');
$options = array(
0 => $no_label,
1 => $yes_label
);
$this->optionsLoaded = true;
return $options;
}
}
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