Commit 759b9641 authored by jaapjansma's avatar jaapjansma

added group and updated join

parent 44a4d897
Pipeline #37 failed with stages
in 2 seconds
......@@ -226,22 +226,16 @@ class CRM_Dataprocessor_BAO_DataProcessor extends CRM_Dataprocessor_DAO_DataProc
$dataProcessor = $factory->getDataProcessorTypeByName($this->type);
$sources = CRM_Dataprocessor_BAO_Source::getValues(array('data_processor_id' => $this->id));
foreach($sources as $sourceDao) {
$source = $factory->getDataSourceByName($sourceDao['type']);
$source->setSourceName($sourceDao['name']);
$source->setSourceTitle($sourceDao['title']);
$source->initialize($sourceDao['configuration']);
$join = null;
if ($sourceDao['join_type']) {
$join = $factory->getJoinByName($sourceDao['join_type']);
$join->initialize($sourceDao['join_configuration'], $this->id);
}
$dataProcessor->addDataSource($source, $join);
CRM_Dataprocessor_BAO_Source::getSourceClass($sourceDao, $dataProcessor);
}
$aggregationFields = CRM_Dataprocessor_BAO_DataProcessor::getAvailableAggregationFields($this->dataProcessorId);
$aggregationFields = CRM_Dataprocessor_BAO_DataProcessor::getAvailableAggregationFields($this->id);
if (is_string($this->aggregation)) {
$this->aggregation = json_decode($this->aggregation, true);
}
if (!is_array($this->aggregation)) {
$this->aggregation = array();
}
foreach($this->aggregation as $alias) {
$dataSource = $dataProcessor->getDataSourceByName($aggregationFields[$alias]->dataSource->getSourceName());
if ($dataSource) {
......@@ -268,7 +262,6 @@ class CRM_Dataprocessor_BAO_DataProcessor extends CRM_Dataprocessor_DAO_DataProc
$dataProcessor->addOutputFieldHandlers($outputHandler);
}
}
return $dataProcessor;
}
......@@ -280,16 +273,7 @@ class CRM_Dataprocessor_BAO_DataProcessor extends CRM_Dataprocessor_DAO_DataProc
$dataProcessor = $factory->getDataProcessorTypeByName($dao->type);
$sources = CRM_Dataprocessor_BAO_Source::getValues(array('data_processor_id' => $dao->id));
foreach($sources as $sourceDao) {
$source = $factory->getDataSourceByName($sourceDao['type']);
$source->setSourceName($sourceDao['name']);
$source->setSourceTitle($sourceDao['title']);
$source->initialize($sourceDao['configuration']);
$join = null;
if ($sourceDao['join_type']) {
$join = $factory->getJoinByName($sourceDao['join_type']);
$join->initialize($sourceDao['join_configuration'], $dao->id);
}
$dataProcessor->addDataSource($source, $join);
CRM_Dataprocessor_BAO_Source::getSourceClass($sourceDao, $dataProcessor);
}
return $dataProcessor->getAvailableOutputHandlers();
......@@ -303,16 +287,7 @@ class CRM_Dataprocessor_BAO_DataProcessor extends CRM_Dataprocessor_DAO_DataProc
$dataProcessor = $factory->getDataProcessorTypeByName($dao->type);
$sources = CRM_Dataprocessor_BAO_Source::getValues(array('data_processor_id' => $dao->id));
foreach($sources as $sourceDao) {
$source = $factory->getDataSourceByName($sourceDao['type']);
$source->setSourceName($sourceDao['name']);
$source->setSourceTitle($sourceDao['title']);
$source->initialize($sourceDao['configuration']);
$join = null;
if ($sourceDao['join_type']) {
$join = $factory->getJoinByName($sourceDao['join_type']);
$join->initialize($sourceDao['join_configuration'], $dao->id);
}
$dataProcessor->addDataSource($source, $join);
CRM_Dataprocessor_BAO_Source::getSourceClass($sourceDao, $dataProcessor);
}
return $dataProcessor->getAvailableFilterHandlers();
......@@ -320,13 +295,14 @@ class CRM_Dataprocessor_BAO_DataProcessor extends CRM_Dataprocessor_DAO_DataProc
public static function getAvailableAggregationFields($data_processor_id) {
$availableAggregationFields = array();
$dao = new CRM_Dataprocessor_BAO_DataProcessor();
$dao->id = $data_processor_id;
$dao->find(true);
$factory = dataprocessor_get_factory();
$sources = CRM_Dataprocessor_BAO_Source::getValues(array('data_processor_id' => $data_processor_id));
$dataProcessor = $factory->getDataProcessorTypeByName($dao->type);
$sources = CRM_Dataprocessor_BAO_Source::getValues(array('data_processor_id' => $dao->id));
foreach($sources as $sourceDao) {
$source = $factory->getDataSourceByName($sourceDao['type']);
$source->setSourceName($sourceDao['name']);
$source->setSourceTitle($sourceDao['title']);
$source->initialize($sourceDao['configuration']);
$source = CRM_Dataprocessor_BAO_Source::getSourceClass($sourceDao, $dataProcessor);
$availableAggregationFields = array_merge($availableAggregationFields, $source->getAvailableAggregationFields());
}
......
......@@ -180,4 +180,30 @@ class CRM_Dataprocessor_BAO_Source extends CRM_Dataprocessor_DAO_Source {
}
}
/**
* @param $source
* @param \Civi\DataProcessor\ProcessorType\AbstractProcessorType $dataProcessor
* @return \Civi\DataProcessor\Source\SourceInterface
*/
public static function getSourceClass($source, \Civi\DataProcessor\ProcessorType\AbstractProcessorType $dataProcessor) {
$factory = dataprocessor_get_factory();
$sourceClass = $factory->getDataSourceByName($source['type']);
$sourceClass->setSourceName($source['name']);
$sourceClass->setSourceTitle($source['title']);
$sourceClass->setConfiguration($source['configuration']);
$sourceClass->setDataProcessor($dataProcessor);
$join = null;
if ($source['join_type']) {
$join = $factory->getJoinByName($source['join_type']);
$join->setConfiguration($source['join_configuration']);
$join->setDataProcessor($dataProcessor);
}
$dataProcessor->addDataSource($sourceClass, $join);
if ($join) {
$join->initialize();
$sourceClass->setJoin($join);
}
return $sourceClass;
}
}
\ No newline at end of file
......@@ -110,7 +110,7 @@ class CRM_Dataprocessor_Form_Join_Simple extends CRM_Core_Form {
$sourceClass->initialize($source['configuration'], $source['name']);
$sourceFields = $sourceClass->getAvailableFields()->getFields();
foreach($sourceFields as $sourceField) {
$fields[$source['name'].'.'.$sourceField->name] = $source['title'] . '::'.$sourceField->name;
$fields[$source['name'] . '.' . $sourceField->name] = $source['title'] . ' :: ' . $sourceField->title;
}
if ($source['id'] == $this->source_id) {
......
......@@ -16,7 +16,7 @@ class DataFlowDescription {
/**
* @var \Civi\DataProcessor\DataFlow\MultipleDataFlows\JoinSpecification
*/
protected $joinSpecification = null;
protected $joinSpecification = array();
public function __construct($datFlow, $joinSpecification = null) {
$this->dataFlow = $datFlow;
......
......@@ -6,6 +6,9 @@
namespace Civi\DataProcessor\DataFlow\MultipleDataFlows;
use Civi\DataProcessor\DataFlow\AbstractDataFlow;
use Civi\DataProcessor\ProcessorType\AbstractProcessorType;
interface JoinInterface{
/**
......@@ -19,13 +22,34 @@ interface JoinInterface{
*/
public function isJoinable($left_record, $right_record);
/**
* Returns true when this join is compatible with this data flow
*
* @param \Civi\DataProcessor\DataFlow\AbstractDataFlow $
* @return bool
*/
public function worksWithDataFlow(AbstractDataFlow $dataFlow);
/**
* Initialize the join
*
* @return void
*/
public function initialize();
/**
* @param AbstractProcessorType $dataProcessor
*
* @return \Civi\DataProcessor\Source\SourceInterface
*/
public function setDataProcessor(AbstractProcessorType $dataProcessor);
/**
* @param array $configuration
* @param int $data_processor_id
*
* @return \Civi\DataProcessor\DataFlow\MultipleDataFlows\JoinInterface
* @return \Civi\DataProcessor\Source\SourceInterface
*/
public function initialize($configuration, $data_processor_id);
public function setConfiguration($configuration);
/**
* Returns the URL for the configuration form of the join specification
......
......@@ -6,11 +6,16 @@
namespace Civi\DataProcessor\DataFlow\MultipleDataFlows;
use Civi\DataProcessor\DataFlow\AbstractDataFlow;
use Civi\DataProcessor\DataFlow\CombinedDataFlow\CombinedSqlDataFlow;
use Civi\DataProcessor\DataFlow\SqlDataFlow;
use Civi\DataProcessor\DataFlow\SqlTableDataFlow;
use Civi\DataProcessor\ProcessorType\AbstractProcessorType;
class SimpleJoin implements JoinInterface, SqlJoinInterface {
private $isInitialized = false;
/**
* @var string
* The name of the left field
......@@ -35,12 +40,27 @@ class SimpleJoin implements JoinInterface, SqlJoinInterface {
*/
private $right_prefix;
/**
* @var String
*/
private $right_table;
/**
* @var String
*/
private $left_table;
/**
* @var String
* The join type, e.g. INNER, LEFT, OUT etc..
*/
private $type = "INNER";
/**
* @var AbstractProcessorType
*/
private $dataProcessor;
public function __construct($left_prefix = null, $left_field = null, $right_prefix = null, $right_field = null, $type = "INNER") {
$this->left_prefix = $left_prefix;
$this->left_field = $left_field;
......@@ -58,11 +78,10 @@ class SimpleJoin implements JoinInterface, SqlJoinInterface {
/**
* @param array $configuration
* @param int $data_processor_id
*
* @return \Civi\DataProcessor\DataFlow\MultipleDataFlows\JoinInterface
*/
public function initialize($configuration, $data_processor_id) {
public function setConfiguration($configuration) {
if (isset($configuration['left_field'])) {
$this->left_field = $configuration['left_field'];
}
......@@ -78,6 +97,65 @@ class SimpleJoin implements JoinInterface, SqlJoinInterface {
if (isset($configuration['type'])) {
$this->type = $configuration['type'];
}
return $this;
}
/**
* @param AbstractProcessorType $dataProcessor
* @return \Civi\DataProcessor\DataFlow\MultipleDataFlows\JoinInterface
* @throws \Exception
*/
public function setDataProcessor(AbstractProcessorType $dataProcessor) {
$this->dataProcessor = $dataProcessor;
}
/**
* Returns true when this join is compatible with this data flow
*
* @param \Civi\DataProcessor\DataFlow\AbstractDataFlow $
* @return bool
*/
public function worksWithDataFlow(AbstractDataFlow $dataFlow) {
if (!$dataFlow instanceof SqlDataFlow) {
return false;
}
$this->initialize();
if ($dataFlow->getTableAlias() == $this->left_table) {
return true;
}
if ($dataFlow->getTableAlias() == $this->right_table) {
return true;
}
return false;
}
public function initialize() {
if ($this->isInitialized) {
return $this;
}
if ($this->left_prefix && $this->left_field) {
$this->left_table = $this->left_prefix;
$left_source = $this->dataProcessor->getDataSourceByName($this->left_prefix);
if ($left_source) {
$leftTable = $left_source->ensureField($this->left_field);
if ($leftTable && $leftTable instanceof SqlTableDataFlow) {
$this->left_table = $leftTable->getTableAlias();
}
}
}
if ($this->right_prefix && $this->right_field) {
$this->right_table = $this->right_prefix;
$right_source = $this->dataProcessor->getDataSourceByName($this->right_prefix);
if ($right_source) {
$rightTable = $right_source->ensureField($this->right_field);
if ($rightTable && $rightTable instanceof SqlTableDataFlow) {
$this->right_table = $rightTable->getTableAlias();
}
}
}
$this->isInitialized = true;
return $this;
}
/**
......@@ -112,9 +190,10 @@ class SimpleJoin implements JoinInterface, SqlJoinInterface {
* @return string
*/
public function getJoinClause(DataFlowDescription $sourceDataFlowDescription) {
$this->initialize();
$joinClause = "";
if ($sourceDataFlowDescription->getJoinSpecification()) {
$joinClause = "ON `{$this->left_prefix}`.`{$this->left_field}` = `{$this->right_prefix}`.`{$this->right_field}`";
$joinClause = "ON `{$this->left_table}`.`{$this->left_field}` = `{$this->right_table}`.`{$this->right_field}`";
}
if ($sourceDataFlowDescription->getDataFlow() instanceof SqlTableDataFlow) {
$table = $sourceDataFlowDescription->getDataFlow()->getTable();
......
......@@ -76,6 +76,7 @@ class Factory {
$this->addDataProcessorType('default', 'Civi\DataProcessor\ProcessorType\DefaultProcessorType', E::ts('Default'));
$this->addDataSource('contact', 'Civi\DataProcessor\Source\ContactSource', E::ts('Contact'));
$this->addDataSource('group', 'Civi\DataProcessor\Source\GroupSource', E::ts('Group'));
$this->addDataSource('email', 'Civi\DataProcessor\Source\EmailSource', E::ts('E-mail'));
$this->addDataSource('contribution', 'Civi\DataProcessor\Source\ContributionSource', E::ts('Contribution'));
$this->addDataSource('relationship', 'Civi\DataProcessor\Source\RelationshipSource', E::ts('Relationship'));
......
......@@ -163,7 +163,7 @@ class Api implements OutputInterface, API_ProviderInterface, EventSubscriberInte
} else {
$options = _civicrm_api3_get_options_from_params($apiRequest['params']);
if (isset($options['limit'])) {
if (isset($options['limit']) && $options['limit'] > 0) {
$dataProcessor->getDataFlow()->setLimit($options['limit']);
}
if (isset($options['offset'])) {
......
......@@ -32,17 +32,4 @@ class EmailSource extends AbstractCivicrmEntitySource {
return 'civicrm_email';
}
/**
* @return \Civi\DataProcessor\DataSpecification\DataSpecification
* @throws \Exception
*/
public function getAvailableFilterFields() {
if (!$this->availableFilterFields) {
$this->availableFilterFields = new DataSpecification();
$this->loadFields($this->availableFilterFields, array());
$this->loadCustomGroupsAndFields($this->availableFilterFields, true);
}
return $this->availableFilterFields;
}
}
\ No newline at end of file
<?php
/**
* @author Jaap Jansma <jaap.jansma@civicoop.org>
* @license AGPL-3.0
*/
namespace Civi\DataProcessor\Source;
use Civi\DataProcessor\DataFlow\SqlTableDataFlow;
use Civi\DataProcessor\DataSpecification\DataSpecification;
use Civi\DataProcessor\DataSpecification\FieldSpecification;
use CRM_Dataprocessor_ExtensionUtil as E;
class GroupSource extends AbstractCivicrmEntitySource {
/**
* Returns the entity name
*
* @return String
*/
protected function getEntity() {
return 'Group';
}
/**
* Returns the table name of this entity
*
* @return String
*/
protected function getTable() {
return 'civicrm_group';
}
}
\ No newline at end of file
......@@ -7,7 +7,9 @@
namespace Civi\DataProcessor\Source;
use Civi\DataProcessor\DataFlow\AbstractDataFlow;
use Civi\DataProcessor\DataFlow\MultipleDataFlows\JoinInterface;
use Civi\DataProcessor\DataSpecification\FieldSpecification;
use Civi\DataProcessor\ProcessorType\AbstractProcessorType;
interface SourceInterface {
......@@ -20,13 +22,34 @@ interface SourceInterface {
/**
* Initialize this data source.
* Initialize the join
*
* @return void
*/
public function initialize();
/**
* Sets the join specification to connect this source to other data sources.
*
* @param \Civi\DataProcessor\DataFlow\MultipleDataFlows\JoinInterface $join
*
* @return \Civi\DataProcessor\Source\SourceInterface
*/
public function setJoin(JoinInterface $join);
/**
* @param AbstractProcessorType $dataProcessor
*
* @return \Civi\DataProcessor\Source\SourceInterface
*/
public function setDataProcessor(AbstractProcessorType $dataProcessor);
/**
* @param array $configuration
*
* @return \Civi\DataProcessor\Source\SourceInterface
*/
public function initialize($configuration);
public function setConfiguration($configuration);
/**
* @return \Civi\DataProcessor\DataSpecification\DataSpecification
......@@ -50,6 +73,15 @@ interface SourceInterface {
*/
public function getConfigurationUrl();
/**
* Ensure that filter field is accesible in the query
*
* @param String $fieldName
* @return \Civi\DataProcessor\DataFlow\MultipleDataFlows\DataFlowDescription
* @throws \Exception
*/
public function ensureField($fieldName);
/**
* Ensures a field is in the data source
*
......
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