Commit b8410d5d authored by jaapjansma's avatar jaapjansma
Browse files

Added Contact Sub Type field.

parent 6ffdb6d6
Version 1.19 (not yet released)
Version 1.19
============
* Added Contact Sub Type field.
Version 1.18
============
......
<?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 \CRM_FormProcessor_ExtensionUtil as E;
class ContactSubTypeType extends AbstractType implements OptionListInterface {
private $options = [];
private $normalizedOptions = [];
private $denormalizedOptions = [];
private $optionsLoaded = FALSE;
/**
* Get the configuration specification
*
* @return \Civi\FormProcessor\Config\SpecificationBag
*/
public function getConfigurationSpecification() {
return new SpecificationBag([
new Specification('multiple', 'Boolean', E::ts('Multiple'), FALSE, 0, NULL, [
0 => E::ts('Single value'),
1 => E::ts('Multiple values'),
]),
]);
}
/**
* Returns true when this field is a multiple field.
*
* @return bool
*/
public function isMultiple() {
return $this->configuration->get('multiple') ? TRUE : FALSE;
}
public function validateValue($value, $allValues = []) {
$multiple = $this->configuration->get('multiple') ? TRUE : FALSE;
$options = $this->getOptions($allValues);
// Correct array values when field is not multiple.
// this could be caused for example by a drupal webform with
// a checkbox field. The value is submitted as an array, altough it contains
// only one value.
if (!$multiple && is_array($value) && count($value) == 1) {
$value = reset($value);
}
if ($multiple && is_array($value)) {
foreach ($value as $valueItem) {
if (\CRM_Utils_Type::validate($valueItem, 'String', FALSE) === NULL) {
return FALSE;
}
if (!isset($options[$valueItem])) {
return FALSE;
}
}
}
elseif (!is_array($value) && $value) {
if (\CRM_Utils_Type::validate($value, 'String', FALSE) === NULL) {
return FALSE;
}
if (!isset($options[$value])) {
return FALSE;
}
}
else {
return FALSE;
}
return TRUE;
}
/**
* Returns the type number from CRM_Utils_Type
*/
public function getCrmType() {
return \CRM_Utils_Type::T_STRING;
}
/**
* Normalize the input value.
*
* @param $value
*
* @return mixed
*/
public function normalizeValue($value) {
$this->loadOptions();
$multiple = $this->configuration->get('multiple') ? TRUE : FALSE;
// Correct array values when field is not multiple.
// this could be caused for example by a drupal webform with
// a checkbox field. The value is submitted as an array, altough it contains
// only one value.
if (!$multiple && is_array($value) && count($value) == 1) {
$value = reset($value);
}
if ($multiple && is_array($value)) {
$return = [];
foreach ($value as $item) {
$return[] = $this->normalizedOptions[$item];
}
return $return;
}
elseif (!is_array($value)) {
return $this->normalizedOptions[$value];
}
else {
return NULL;
}
}
/**
* Denormalize the input value.
*
* @param $value
*
* @return mixed
*/
public function denormalizeValue($value) {
$this->loadOptions();
$multiple = $this->configuration->get('multiple') ? TRUE : FALSE;
if ($multiple && is_array($value)) {
$return = [];
foreach ($value as $item) {
$return[] = $this->denormalizedOptions[$item];
}
return $return;
}
elseif (!is_array($value) && strlen($value)) {
return $this->denormalizedOptions[$value];
}
else {
return NULL;
}
}
public function getOptions($params) {
$this->loadOptions();
return $this->options;
}
public function loadOptions() {
if ($this->optionsLoaded) {
return;
}
$contactTypesApi = civicrm_api3('ContactType', 'get', ['is_active' => 1, 'options' => ['limit' => 0]]);
foreach ($contactTypesApi['values'] as $key => $contactType) {
if (empty($contactType['parent_id'])) {
continue;
}
$this->options[$contactType['name']] = $contactType['label'];
$this->normalizedOptions[$contactType['name']] = $contactType['name'];
$this->denormalizedOptions[$contactType['name']] = $contactType['name'];
}
$this->optionsLoaded = TRUE;
}
}
......@@ -27,6 +27,7 @@ class Factory {
$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 ContactSubTypeType('ContactSubType', E::ts('Contact Sub Type')));
$this->addType(new CustomOptionListType('CustomOptionListType', E::ts('Custom Options')));
$this->addType(new CountryType('Country', E::ts('Country')));
$this->addType(new CountryIsoCodeType('CountryIsoCode', E::ts('Country ISO Code')));
......
......@@ -16,8 +16,8 @@
<url desc="Licensing">http://www.gnu.org/licenses/agpl-3.0.html</url>
<url desc="Action-Provider Extension">https://lab.civicrm.org/extensions/action-provider</url>
</urls>
<releaseDate>2020-11-23</releaseDate>
<version>1.19-dev</version>
<releaseDate>2020-12-09</releaseDate>
<version>1.19</version>
<develStage>stable</develStage>
<compatibility>
<ver>4.7</ver>
......
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