Commit 32fd18b1 authored by jaapjansma's avatar jaapjansma
Browse files

Added input type for Tags.

parent e842c5d3
Version 1.23 (not yet released)
Version 1.23
============
* Added input type for Tags.
Version 1.22
============
......
......@@ -33,6 +33,7 @@ class Factory {
$this->addType(new CountryIsoCodeType('CountryIsoCode', E::ts('Country ISO Code')));
$this->addType(new StateProvinceType('StateProvince', E::ts('State/Province')));
$this->addType(new MailingGroupType('MailingGroup', E::ts('Mailing Group')));
$this->addType(new TagType('Tag', E::ts('Tags')));
$this->addType(new ParentMailingGroupType('ParentMailingGroup', E::ts('Parent Mailing Group')));
$this->addType(new ParticipantStatusType('ParticipantStatusType', E::ts('Participant Status')));
$this->addType(new CampaignType('CampaignType', E::ts('Campaign')));
......
<?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 TagType extends AbstractType implements OptionListInterface {
/**
* Returns true when this field is a multiple field.
*
* @return bool
*/
public function isMultiple() {
return $this->configuration->get('multiple') == 1 ? TRUE : FALSE;
}
/**
* Get the configuration specification
*
* @return SpecificationBag
*/
public function getConfigurationSpecification() {
$tagsParams['used_for'] = 'civicrm_contact';
$tagsParams['options']['limit'] = 0;
$tagApi = civicrm_api3('Tag', 'get', $tagsParams);
$tags = [];
$tagSets = [];
foreach ($tagApi['values'] as $tag) {
if (isset($tag['is_tagset']) && $tag['is_tagset']) {
$tagSets[$tag['name']] = $tag['name'];
} else {
$tags[$tag['name']] = $tag['name'];
}
}
return new SpecificationBag([
new Specification('multiple', 'Boolean', E::ts('Multiple'), TRUE, 0, NULL, [
0 => E::ts('Single value'),
1 => E::ts('Multiple values'),
]),
new Specification('tagsets', 'String', E::ts("Limit to tagset"), false, null, null, $tagSets, true),
new Specification('tags', 'String', E::ts("Limit to tags"), false, null, null, $tags, true),
]);
}
public function validateValue($value, $allValues = []) {
$multiple = $this->configuration->get('multiple') ? TRUE : FALSE;
$groups = $this->getOptions($allValues);
if ($multiple && is_array($value)) {
foreach ($value as $valueItem) {
if (\CRM_Utils_Type::validate($valueItem, 'Integer', FALSE) === NULL) {
return FALSE;
}
if (!isset($groups[$valueItem])) {
return FALSE;
}
}
}
elseif (!is_array($value) && $value) {
if (\CRM_Utils_Type::validate($value, 'Integer', FALSE) === NULL) {
return FALSE;
}
if (!isset($groups[$value])) {
return FALSE;
}
}
else {
return FALSE;
}
return TRUE;
}
/**
* Returns the type number from CRM_Utils_Type
*/
public function getCrmType() {
return \CRM_Utils_Type::T_INT;
}
/**
* Normalize the input value.
*
* @param $value
*
* @return mixed
*/
public function normalizeValue($value) {
$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);
}
return $value;
}
/**
* Denormalize the input value.
*
* @param $value
*
* @return mixed
*/
public function denormalizeValue($value) {
$multiple = $this->configuration->get('multiple') ? TRUE : FALSE;
if ($multiple && !is_array($value)) {
$value = [$value];
}
return $value;
}
public function getOptions($params) {
static $return;
if ($return) {
return $return;
}
$tagsParams['used_for'] = 'civicrm_contact';
$tagsParams['is_tagset'] = '0';
if (is_array($this->configuration->get('tagsets')) && count($this->configuration->get('tagsets'))) {
$tagsParams['parent_id'] = ['IN' => $this->configuration->get('tagsets')];
} elseif (strlen($this->configuration->get('tagsets'))) {
$tagsParams['parent_id'] = $this->configuration->get('tagsets');
}
$tagsParams['options']['limit'] = 0;
$tags = civicrm_api3('Tag', 'get', $tagsParams);
$return = [];
foreach ($tags['values'] as $tag) {
if (is_array($this->configuration->get('tags')) && count($this->configuration->get('tags'))) {
if (in_array($tag['name'], $this->configuration->get('tags'))) {
$return[$tag['id']] = $tag['name'];
}
} else {
$return[$tag['id']] = $tag['name'];
}
}
return $return;
}
}
<div ng-if="specification.type == 'specification'">
<div ng-if="!noLabel" class="label">
<label crm-ui-for="specification.name" crm-depth="1" crm-ui-force-required="specification.required">{{specification.title}}</label>
</div>
<div ng-attr-class="{{!noLabel ? 'content' : ''}}" ng-if="specification.options">
<div crm-ui-field="uiField" ng-if="specification.options">
<select
class="huge"
crm-ui-select="{allowClear: true, minimumInputLength: 0}"
crm-ui-id="{{specification.name}}"
name="{{specification.name}}"
ng-if="!specification.multiple"
ng-model="configuration[specification.name]"
ng-required="specification.required"
>
<option ng-repeat="(key,value) in specification.options" value="{{key}}">{{value}}</option>
</select>
<select
class="huge"
crm-ui-select="{allowClear: true, minimumInputLength: 0}"
crm-ui-id="{{specification.name}}"
name="{{specification.name}}"
ng-if="specification.multiple"
ng-model="configuration[specification.name]"
ng-required="specification.required"
multiple="multiple"
>
<option ng-repeat="(key,value) in specification.options" value="{{key}}">{{value}}</option>
</select>
</div>
<div ng-attr-class="{{!noLabel ? 'content' : ''}}" ng-if="specification.fk_entity">
<div crm-ui-field="uiField" ng-if="specification.fk_entity">
<input class="huge"
crm-entityref="{entity: specification.fk_entity, select: {allowClear: true, placeholder: specification.title, minimumInputLength:0}}"
crm-entityref="{entity: specification.fk_entity, select: {allowClear: true, placeholder: specification.title, minimumInputLength:0, multiple: specification.multiple}}"
name="{{specification.name}}"
crm-ui-id="{{specification.name}}"
ng-model="configuration[specification.name]"
ng-required="spec.required"
ng-required="specification.required"
/>
</div>
<div ng-attr-class="{{!noLabel ? 'content' : ''}}" ng-if="!specification.options && !specification.fk_entity">
<div crm-ui-field="uiField" ng-if="!specification.options && !specification.fk_entity">
<input
type="text"
name="{{specification.name}}"
......@@ -39,10 +48,7 @@
</div>
<div ng-if="specification.type == 'fields'">
<div ng-if="!noLabel" class="label">
<label crm-ui-for="specification.name" crm-depth="1" crm-ui-force-required="specification.required">{{specification.title}}</label>
</div>
<div ng-attr-class="{{!noLabel ? 'content' : ''}}">
<div crm-ui-field="uiField">
<select
name="{{specification.name}}"
crm-ui-select="{allowClear: true, minimumInputLength: 0}"
......
......@@ -20,14 +20,19 @@
var compiledContents;
return function(scope, iElement, iAttr) {
scope.ts = CRM.ts(null);
if (scope.noLabel === undefined) {
scope.noLabel = false;
}
scope.uiField = {
name: scope.specification.name,
'name': scope.specification.name,
};
if (!scope.noLabel) {
scope.uiField['title'] = scope.specification.title;
scope.uiField['required'] = scope.specification.required;
scope.uiField.title = scope.specification.title;
}
if (scope.specification.required) {
scope.uiField.required = true;
}
scope.addItemToCollection = function addItemToCollection(specificationBag) {
var item_config = angular.copy(specificationBag.default_configuration);
......
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