diff --git a/Civi/DataProcessor/Factory.php b/Civi/DataProcessor/Factory.php index d84bb73735770eddaa96857ee64bea95efc664cb..7969a282f5ecce07f24d7b9a6549d1e872d40579 100644 --- a/Civi/DataProcessor/Factory.php +++ b/Civi/DataProcessor/Factory.php @@ -147,6 +147,7 @@ class Factory { $this->addFilter('contact_in_group_filter', new Definition('Civi\DataProcessor\FilterHandler\ContactInGroupFilter'), E::ts('Contact in Group filter')); $this->addFilter('contact_with_tag_filter', new Definition('Civi\DataProcessor\FilterHandler\ContactWithTagFilter'), E::ts('Contact has Tag filter')); $this->addFilter('contact_has_membership', new Definition('Civi\DataProcessor\FilterHandler\ContactHasMembershipFilter'), E::ts('Contact has Membership filter')); + $this->addFilter('worldregion_filter', new Definition('Civi\DataProcessor\FilterHandler\WorldRegionFilter'), E::ts('World Region Filter')); $this->addFilter('contact_type_filter', new Definition('Civi\DataProcessor\FilterHandler\ContactTypeFilter'), E::ts('Contact Type filter')); $this->addFilter('permission_to_view_contact', new Definition('Civi\DataProcessor\FilterHandler\PermissionToViewContactFilter'), E::ts('Permission to view contact')); $this->addFilter('case_role_filter', new Definition('Civi\DataProcessor\FilterHandler\CaseRoleFilter'), E::ts('Contact has role on case filter')); @@ -156,6 +157,7 @@ class Factory { $this->addOutputHandler('raw', new Definition('Civi\DataProcessor\FieldOutputHandler\RawFieldOutputHandler'), E::ts('Raw field value')); $this->addOutputHandler('markup', new Definition('Civi\DataProcessor\FieldOutputHandler\MarkupFieldOutputHandler'), E::ts('Markup/Html field value')); $this->addOutputHandler('formatted_address', new Definition('Civi\DataProcessor\FieldOutputHandler\FormattedAddressFieldOutputHandler'), E::ts('Formatted Address')); + $this->addOutputHandler('worldregion', new Definition('Civi\DataProcessor\FieldOutputHandler\WorldRegionFieldOutputHandler'), E::ts('Worldregion')); $this->addOutputHandler('number', new Definition('Civi\DataProcessor\FieldOutputHandler\NumberFieldOutputHandler'), E::ts('Formatted Number field value')); $this->addOutputHandler('date', new Definition('Civi\DataProcessor\FieldOutputHandler\DateFieldOutputHandler'), E::ts('Date field value')); $this->addOutputHandler('age', new Definition('Civi\DataProcessor\FieldOutputHandler\AgeFieldOutputHandler'), E::ts('Age field value')); diff --git a/Civi/DataProcessor/FieldOutputHandler/WorldRegionFieldOutputHandler.php b/Civi/DataProcessor/FieldOutputHandler/WorldRegionFieldOutputHandler.php new file mode 100644 index 0000000000000000000000000000000000000000..f14f71beecf758cf68e181a3a5bb651296132584 --- /dev/null +++ b/Civi/DataProcessor/FieldOutputHandler/WorldRegionFieldOutputHandler.php @@ -0,0 +1,54 @@ +<?php +/** + * @author Klaas Eikelboom <klaas.eikelboom@civicoop.org + * @license AGPL-3.0 + */ + +namespace Civi\DataProcessor\FieldOutputHandler; + +use CRM_Dataprocessor_ExtensionUtil as E; + +class WorldRegionFieldOutputHandler extends AbstractSimpleFieldOutputHandler { + + /** + * Returns the label of the field for selecting a field. + * + * This could be override in a child class. + * + * @return string + */ + protected function getFieldTitle() { + return E::ts('Country Field'); + } + + /** + * Returns the data type of this field + * + * @return String + */ + protected function getType() { + return 'String'; + } + + /** + * Returns the formatted value + * + * @param $rawRecord + * @param $formattedRecord + * + * @return \Civi\DataProcessor\FieldOutputHandler\FieldOutput + */ + public function formatField($rawRecord, $formattedRecord) { + $sql = 'SELECT wr.name FROM civicrm_worldregion wr + JOIN civicrm_country c on (c.region_id = wr.id) + WHERE c.id = %1'; + $countryId = $rawRecord[$this->inputFieldSpec->alias]; + + $regionName = \CRM_Core_DAO::singleValueQuery($sql, [ 1 => [$countryId,'Integer']]); + $formattedValue = new HTMLFieldOutput(); + $formattedValue->setHtmlOutput($regionName); + return $formattedValue; + } + + +} diff --git a/Civi/DataProcessor/FilterHandler/WorldRegionFilter.php b/Civi/DataProcessor/FilterHandler/WorldRegionFilter.php new file mode 100644 index 0000000000000000000000000000000000000000..0bc7fcff38da958092c17e287283404a7707c9de --- /dev/null +++ b/Civi/DataProcessor/FilterHandler/WorldRegionFilter.php @@ -0,0 +1,199 @@ +<?php +/** + * @author Klaas Eikelboom <klaas.eikelboom@civicoop.org> + * @license AGPL-3.0 + */ + +namespace Civi\DataProcessor\FilterHandler; + +use Civi\DataProcessor\DataFlow\SqlDataFlow; +use Civi\DataProcessor\Exception\InvalidConfigurationException; +use CRM_Dataprocessor_ExtensionUtil as E; + +class WorldRegionFilter extends AbstractFieldFilterHandler { + + /** + * Initialize the filter + * + * @throws \Civi\DataProcessor\Exception\DataSourceNotFoundException + * @throws \Civi\DataProcessor\Exception\InvalidConfigurationException + * @throws \Civi\DataProcessor\Exception\FieldNotFoundException + */ + protected function doInitialization() { + if (!isset($this->configuration['datasource']) || !isset($this->configuration['field'])) { + throw new InvalidConfigurationException(E::ts("Filter %1 requires a field to filter on. None given.", array(1=>$this->title))); + } + $this->initializeField($this->configuration['datasource'], $this->configuration['field']); + } + + /** + * @param array $filter + * The filter settings + * @return mixed + */ + public function setFilter($filter) { + $this->resetFilter(); + $dataFlow = $this->dataSource->ensureField($this->inputFieldSpecification); + $region_ids = $filter['value']; + if (!is_array($region_ids)) { + $region_ids = explode(",", $region_ids); + if (!is_array($region_ids)) { + $region_ids = [$region_ids]; + } + } + $countryTableAlias = 'civicrm_country_'.$this->inputFieldSpecification->alias; + $countryFilters = array( + new SqlDataFlow\SimpleWhereClause($countryTableAlias, 'region_id', 'IN', $region_ids), + ); + if ($dataFlow && $dataFlow instanceof SqlDataFlow) { + $this->whereClause = new SqlDataFlow\InTableWhereClause( + 'id', + 'civicrm_country', + $countryTableAlias, + $countryFilters, + $dataFlow->getName(), + $this->inputFieldSpecification->name, + $filter['op'] + ); + + $dataFlow->addWhereClause($this->whereClause); + } + } + + /** + * Returns true when this filter has additional configuration + * + * @return bool + */ + public function hasConfiguration() { + return true; + } + + /** + * When this filter type has additional configuration you can add + * the fields on the form with this function. + * + * @param \CRM_Core_Form $form + * @param array $filter + */ + public function buildConfigurationForm(\CRM_Core_Form $form, $filter=array()) { + $fieldSelect = \CRM_Dataprocessor_Utils_DataSourceFields::getAvailableFilterFieldsInDataSources($filter['data_processor_id']); + + $form->add('select', 'country_id_field', E::ts('Country Field'), $fieldSelect, true, array( + 'style' => 'min-width:250px', + 'class' => 'crm-select2 huge data-processor-field-for-name', + 'placeholder' => E::ts('- select -'), + )); + + if (isset($filter['configuration'])) { + $configuration = $filter['configuration']; + $defaults = array(); + if (isset($configuration['field']) && isset($configuration['datasource'])) { + $defaults['country_id_field'] = $configuration['datasource'] . '::' . $configuration['field']; + } + $form->setDefaults($defaults); + } + } + + /** + * When this filter type has configuration specify the template file name + * for the configuration form. + * + * @return false|string + */ + public function getConfigurationTemplateFileName() { + return "CRM/Dataprocessor/Form/Filter/Configuration/WorldRegionFilter.tpl"; + } + + /** + * Process the submitted values and create a configuration array + * + * @param $submittedValues + * @return array + */ + public function processConfiguration($submittedValues) { + list($datasource, $field) = explode('::', $submittedValues['country_id_field'], 2); + $configuration['field'] = $field; + $configuration['datasource'] = $datasource; + return $configuration; + } + + /** + * Add the elements to the filter form. + * + * @param \CRM_Core_Form $form + * @param array $defaultFilterValue + * @param string $size + * Possible values: full or compact + * @return array + * Return variables belonging to this filter. + */ + public function addToFilterForm(\CRM_Core_Form $form, $defaultFilterValue, $size='full') { + $fieldSpec = $this->getFieldSpecification(); + $alias = $fieldSpec->alias; + $operations = $this->getOperatorOptions($fieldSpec); + + $title = $fieldSpec->title; + if ($this->isRequired()) { + $title .= ' <span class="crm-marker">*</span>'; + } + + $sizeClass = 'huge'; + $minWidth = 'min-width: 250px;'; + if ($size =='compact') { + $sizeClass = 'medium'; + $minWidth = ''; + } + + $form->add('select', "{$fieldSpec->alias}_op", E::ts('Operator:'), $operations, true, [ + 'style' => $minWidth, + 'class' => 'crm-select2 '.$sizeClass, + 'multiple' => false, + 'placeholder' => E::ts('- select -'), + ]); + $form->add('select', "{$fieldSpec->alias}_value", E::ts('World Regions:'), $this->worldRegions(), true, [ + 'placeholder' => E::ts('Select worldregion'), + 'class' => 'crm-select2 '.$sizeClass, + 'multiple' => true, + 'select' => ['minimumInputLength' => 0], + ]); + + if (isset($defaultFilterValue['op'])) { + $defaults[$alias . '_op'] = $defaultFilterValue['op']; + } else { + $defaults[$alias . '_op'] = key($operations); + } + if (isset($defaultFilterValue['value'])) { + $defaults[$alias.'_value'] = $defaultFilterValue['value']; + } + + if (count($defaults)) { + $form->setDefaults($defaults); + } + + $filter['type'] = $fieldSpec->type; + $filter['alias'] = $fieldSpec->alias; + $filter['title'] = $title; + $filter['size'] = $size; + + return $filter; + } + + protected function worldRegions(){ + $regions = []; + $dao = \CRM_Core_DAO::executeQuery('select id, name from civicrm_worldregion order by name'); + while($dao->fetch()){ + $regions[$dao->id] = $dao->name; + } + return $regions; + } + + protected function getOperatorOptions(\Civi\DataProcessor\DataSpecification\FieldSpecification $fieldSpec) { + return array( + 'IN' => E::ts('Address in Worldregions'), + 'NOT IN' => E::ts('Address outside Worldregions'), + ); + } + + +} diff --git a/templates/CRM/Dataprocessor/Form/Filter/Configuration/WorldRegionFilter.tpl b/templates/CRM/Dataprocessor/Form/Filter/Configuration/WorldRegionFilter.tpl new file mode 100644 index 0000000000000000000000000000000000000000..50d82042a5c268d3cbe778a1def44627748328d4 --- /dev/null +++ b/templates/CRM/Dataprocessor/Form/Filter/Configuration/WorldRegionFilter.tpl @@ -0,0 +1,7 @@ +{crmScope extensionKey='dataprocessor'} +<div class="crm-section"> + <div class="label">{$form.country_id_field.label}</div> + <div class="content">{$form.country_id_field.html}</div> + <div class="clear"></div> +</div> +{/crmScope}