Commit 39dc291a authored by jaapjansma's avatar jaapjansma
Browse files

added field functionality

parent 15538438
......@@ -183,6 +183,14 @@ class CRM_Dataprocessor_BAO_DataProcessor extends CRM_Dataprocessor_DAO_DataProc
return ($count > 0) ? false : true;
}
/**
* Returns a configured data processor instance.
*
* @param String $output_type
* @param String $name
* @return \Civi\DataProcessor\ProcessorType\AbstractProcessorType
* @throws \Exception when no data processor is found.
*/
public static function getDataProcessorByOutputTypeAndName($output_type, $name) {
$sql = "
SELECT civicrm_data_processor.*
......@@ -200,13 +208,20 @@ class CRM_Dataprocessor_BAO_DataProcessor extends CRM_Dataprocessor_DAO_DataProc
return $dao->getDataProcessor();
}
/**
* Returns a configured data processor instance.
*
* @return \Civi\DataProcessor\ProcessorType\AbstractProcessorType
*/
public function getDataProcessor() {
$factory = \Civi::service('data_processor_factory');
$factory = dataprocessor_get_factory();
$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->initialize($sourceDao['configuration'], $sourceDao['name']);
$source->setSourceName($sourceDao['name']);
$source->setSourceTitle($sourceDao['title']);
$source->initialize($sourceDao['configuration']);
$join = null;
if ($sourceDao['join_type']) {
$join = $factory->getJoinByName($sourceDao['join_type']);
......@@ -214,9 +229,43 @@ class CRM_Dataprocessor_BAO_DataProcessor extends CRM_Dataprocessor_DAO_DataProc
}
$dataProcessor->addDataSource($source, $join);
}
$fields = CRM_Dataprocessor_BAO_Field::getValues(array('data_processor_id' => $this->id));
$outputHandlers = $dataProcessor->getAvailableOutputHandlers();
foreach($fields as $field) {
if (isset($outputHandlers[$field['type']])) {
$outputHandler = $outputHandlers[$field['type']];
$outputHandler->initialize($field['name'], $field['title'], $field['configuration']);
$dataProcessor->addOutputFieldHandlers($outputHandler);
}
}
return $dataProcessor;
}
public static function getAvailableOutputHandlers($data_processor_id) {
$dao = new CRM_Dataprocessor_BAO_DataProcessor();
$dao->id = $data_processor_id;
$dao->find(true);
$factory = dataprocessor_get_factory();
$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);
}
return $dataProcessor->getAvailableOutputHandlers();
}
/**
* Returns the id of the data processor.
*
......@@ -288,10 +337,17 @@ class CRM_Dataprocessor_BAO_DataProcessor extends CRM_Dataprocessor_DAO_DataProc
$dataProcessor['data_sources'] = CRM_Dataprocessor_BAO_Source::getValues(array('data_processor_id' => $id));
foreach($dataProcessor['data_sources'] as $i => $datasource) {
unset($dataProcessor['data_sources'][$i]['id']);
unset($dataProcessor['data_sources'][$i]['data_processor_id']);
}
$dataProcessor['fields'] = CRM_Dataprocessor_BAO_Field::getValues(array('data_processor_id' => $id));
foreach($dataProcessor['fields'] as $i => $field) {
unset($dataProcessor['fields'][$i]['id']);
unset($dataProcessor['fields'][$i]['data_processor_id']);
}
$dataProcessor['outputs'] = CRM_Dataprocessor_BAO_Output::getValues(array('data_processor_id' => $id));
foreach($dataProcessor['outputs'] as $i => $output) {
unset($dataProcessor['outputs'][$i]['id']);
unset($dataProcessor['outputs'][$i]['data_processor_id']);
}
return $dataProcessor;
}
......
<?php
/**
* @author Jaap Jansma <jaap.jansma@civicoop.org>
* @license AGPL-3.0
*/
class CRM_Dataprocessor_BAO_Field extends CRM_Dataprocessor_DAO_Field {
/**
* Function to get values
*
* @return array $result found rows with data
* @access public
* @static
*/
public static function getValues($params) {
$factory = dataprocessor_get_factory();
$types = $factory->getDataSources();
$result = array();
$field = new CRM_Dataprocessor_DAO_Field();
if (!empty($params)) {
$fields = self::fields();
foreach ($params as $key => $value) {
if (isset($fields[$key])) {
$field->$key = $value;
}
}
}
$field->find();
while ($field->fetch()) {
$row = array();
self::storeValues($field, $row);
if (isset($types[$row['type']])) {
$row['type_name'] = $types[$row['type']];
} else {
$row['type_name'] = '';
}
if (!empty($row['configuration'])) {
$row['configuration'] = json_decode($row['configuration'], true);
} else {
$row['configuration'] = array();
}
if (!empty($row['join_configuration'])) {
$row['join_configuration'] = json_decode($row['join_configuration'], true);
} else {
$row['join_configuration'] = array();
}
$result[$row['id']] = $row;
}
return $result;
}
/**
* Function to add or update a DataProcessor
*
* @param array $params
* @return array $result
* @access public
* @throws Exception when params is empty
* @static
*/
public static function add($params) {
$result = array();
if (empty($params)) {
throw new Exception('Params can not be empty when adding or updating a data processor field');
}
if (!empty($params['id'])) {
CRM_Utils_Hook::pre('edit', 'DataProcessorField', $params['id'], $params);
}
else {
CRM_Utils_Hook::pre('create', 'DataProcessorField', NULL, $params);
}
$field = new CRM_Dataprocessor_DAO_Field();
$fields = self::fields();
foreach ($params as $key => $value) {
if (isset($fields[$key])) {
$field->$key = $value;
}
}
if (isset($field->configuration) && is_array($field->configuration)) {
$field->configuration = json_encode($field->configuration);
}
$field->save();
self::storeValues($field, $result);
if (!empty($params['id'])) {
CRM_Utils_Hook::post('edit', 'DataProcessorField', $field->id, $field);
}
else {
CRM_Utils_Hook::post('create', 'DataProcessorField', $field->id, $field);
}
return $result;
}
/**
* Public function to generate name from title
*
* @param $label
* @return string
* @access public
* @static
*/
public static function buildNameFromTitle($title) {
return preg_replace('@[^a-z0-9_]+@','_', strtolower($title));
}
/**
* Returns whether the name is valid or not
*
* @param string $name
* @param int $data_procssor_id,
* @param int $id optional
* @return bool
* @static
*/
public static function isNameValid($name, $data_procssor_id, $id=null) {
$sql = "SELECT COUNT(*) FROM `civicrm_data_processor_field` WHERE `name` = %1 AND `data_processor_id` = %2";
$params[1] = array($name, 'String');
$params[2] = array($data_procssor_id, 'Integer');
if ($id) {
$sql .= " AND `id` != %3";
$params[3] = array($id, 'Integer');
}
$count = CRM_Core_DAO::singleValueQuery($sql, $params);
return ($count > 0) ? false : true;
}
/**
* Function to delete a Data Processor Field with id
*
* @param int $id
* @throws Exception when $id is empty
* @access public
* @static
*/
public static function deleteWithId($id) {
if (empty($id)) {
throw new Exception('id can not be empty when attempting to delete a data processor field');
}
CRM_Utils_Hook::pre('delete', 'DataProcessorField', $id, CRM_Core_DAO::$_nullArray);
$field = new CRM_Dataprocessor_DAO_Field();
$field->id = $id;
$field->delete();
CRM_Utils_Hook::post('delete', 'DataProcessorField', $id, CRM_Core_DAO::$_nullArray);
return;
}
/**
* Function to delete a Data Processor Field with id
*
* @param int $id
* @throws Exception when $id is empty
* @access public
* @static
*/
public static function deleteWithDataProcessorId($id) {
if (empty($id)) {
throw new Exception('id can not be empty when attempting to delete a data processor field');
}
$field = new CRM_Dataprocessor_DAO_Field();
$field->data_processor_id = $id;
$field->find(FALSE);
while ($field->fetch()) {
self::deleteWithId($field->id);
}
}
}
\ No newline at end of file
......@@ -14,7 +14,7 @@ class CRM_Dataprocessor_BAO_Output extends CRM_Dataprocessor_DAO_Output {
* @static
*/
public static function getValues($params) {
$factory = \Civi::service('data_processor_factory');
$factory = dataprocessor_get_factory();
$types = $factory->getOutputs();
$result = array();
......
......@@ -14,7 +14,7 @@ class CRM_Dataprocessor_BAO_Source extends CRM_Dataprocessor_DAO_Source {
* @static
*/
public static function getValues($params) {
$factory = \Civi::service('data_processor_factory');
$factory = dataprocessor_get_factory();
$types = $factory->getDataSources();
$result = array();
......@@ -128,7 +128,7 @@ class CRM_Dataprocessor_BAO_Source extends CRM_Dataprocessor_DAO_Source {
$params[1] = array($name, 'String');
$params[2] = array($data_procssor_id, 'Integer');
if ($id) {
$sql .= " AND `id` != %2";
$sql .= " AND `id` != %3";
$params[3] = array($id, 'Integer');
}
$count = CRM_Core_DAO::singleValueQuery($sql, $params);
......
<?php
use CRM_Dataprocessor_ExtensionUtil as E;
/**
* @author Jaap Jansma (CiviCooP) <jaap.jansma@civicoop.org>
* @license http://www.gnu.org/licenses/agpl-3.0.html
*/
class CRM_Dataprocessor_DAO_Field extends CRM_Core_DAO {
/**
* static instance to hold the field values
*
* @var array
* @static
*/
static $_fields = null;
static $_export = null;
/**
* empty definition for virtual function
*/
static function getTableName() {
return 'civicrm_data_processor_field';
}
/**
* returns all the column names of this table
*
* @access public
* @return array
*/
public static function &fields() {
if (!(self::$_fields)) {
self::$_fields = array(
'id' => array(
'name' => 'id',
'title' => E::ts('ID'),
'type' => CRM_Utils_Type::T_INT,
'required' => true
) ,
'data_processor_id' => array(
'name' => 'data_processor_id',
'title' => E::ts('Data Processor ID'),
'type' => CRM_Utils_Type::T_INT,
'required' => true,
'FKApiName' => 'DataProcessor',
),
'type' => array(
'name' => 'type',
'title' => E::ts('Type'),
'type' => CRM_Utils_Type::T_STRING,
'maxlength' => 80,
'required' => true,
),
'name' => array(
'name' => 'name',
'title' => E::ts('Name'),
'type' => CRM_Utils_Type::T_STRING,
'maxlength' => 128,
'required' => true
),
'title' => array(
'name' => 'title',
'title' => E::ts('Title'),
'type' => CRM_Utils_Type::T_STRING,
'maxlength' => 128,
'required' => true
),
'configuration' => array(
'name' => 'configuration',
'title' => E::ts('Configuration'),
'type' => CRM_Utils_Type::T_TEXT,
),
);
}
return self::$_fields;
}
/**
* Returns an array containing, for each field, the array key used for that
* field in self::$_fields.
*
* @access public
* @return array
*/
public static function &fieldKeys() {
if (!(self::$_fieldKeys)) {
self::$_fieldKeys = array(
'id' => 'id',
'data_processor_id' => 'data_processor_id',
'type' => 'type',
'name' => 'name',
'title' => 'title',
'configuration' => 'configuration',
);
}
return self::$_fieldKeys;
}
}
\ No newline at end of file
......@@ -44,16 +44,19 @@ class CRM_Dataprocessor_Form_DataProcessor extends CRM_Core_Form {
if ($this->dataProcessorId) {
$this->addSources();
$this->addFields();
$this->assign('outputs', CRM_Dataprocessor_BAO_Output::getValues(array('data_processor_id' => $this->dataProcessorId)));
$dataSourceAddUrl = CRM_Utils_System::url('civicrm/dataprocessor/form/source', 'reset=1&action=add&data_processor_id='.$this->dataProcessorId, TRUE);
$addFieldUrl = CRM_Utils_System::url('civicrm/dataprocessor/form/field', 'reset=1&action=add&data_processor_id='.$this->dataProcessorId, TRUE);
$outputAddUrl = CRM_Utils_System::url('civicrm/dataprocessor/form/output', 'reset=1&action=add&data_processor_id='.$this->dataProcessorId, TRUE);
$this->assign('addDataSourceUrl', $dataSourceAddUrl);
$this->assign('addFieldUrl', $addFieldUrl);
$this->assign('addOutputUrl', $outputAddUrl);
}
}
protected function addSources() {
$factory = \Civi::service('data_processor_factory');
$factory = dataprocessor_get_factory();
$sources = CRM_Dataprocessor_BAO_Source::getValues(array('data_processor_id' => $this->dataProcessorId));
foreach($sources as $idx => $source) {
$sources[$idx]['join_link'] = '';
......@@ -70,6 +73,16 @@ class CRM_Dataprocessor_Form_DataProcessor extends CRM_Core_Form {
$this->assign('sources', $sources);
}
protected function addFields() {
$factory = dataprocessor_get_factory();
$fields = CRM_Dataprocessor_BAO_Field::getValues(array('data_processor_id' => $this->dataProcessorId));
foreach($fields as $idx => $field) {
$fields[$idx]['configuration_link'] = '';
$fields[$idx]['aggregate'] = $field['aggregate'] ? E::ts('Aggregate field') : '';
}
$this->assign('fields', $fields);
}
public function buildQuickForm() {
$this->add('hidden', 'id');
if ($this->_action != CRM_Core_Action::DELETE) {
......
<?php
use CRM_Dataprocessor_ExtensionUtil as E;
/**
* Form controller class
*
* @see https://wiki.civicrm.org/confluence/display/CRMDOC/QuickForm+Reference
*/
class CRM_Dataprocessor_Form_Field extends CRM_Core_Form {
private $dataProcessorId;
private $id;
/**
* Function to perform processing before displaying form (overrides parent function)
*
* @access public
*/
function preProcess() {
$session = CRM_Core_Session::singleton();
$this->dataProcessorId = CRM_Utils_Request::retrieve('data_processor_id', 'Integer');
$this->assign('data_processor_id', $this->dataProcessorId);
$this->id = CRM_Utils_Request::retrieve('id', 'Integer');
$this->assign('id', $this->id);
if ($this->id) {
$field = CRM_Dataprocessor_BAO_Field::getValues(array('id' => $this->id));
$this->assign('field', $field[$this->id]);
}
$title = E::ts('Data Processor Field');
CRM_Utils_System::setTitle($title);
$url = CRM_Utils_System::url('civicrm/dataprocessor/form/edit', array('id' => $this->dataProcessorId, 'action' => 'update', 'reset' => 1));
$session->pushUserContext($url);
}
public function buildQuickForm() {
$this->add('hidden', 'data_processor_id');
$this->add('hidden', 'id');
if ($this->_action != CRM_Core_Action::DELETE) {
$this->add('text', 'name', E::ts('Name'), array('size' => CRM_Utils_Type::HUGE), FALSE);
$this->add('text', 'title', E::ts('Title'), array('size' => CRM_Utils_Type::HUGE), TRUE);
$outputHandlers = CRM_Dataprocessor_BAO_DataProcessor::getAvailableOutputHandlers($this->dataProcessorId);
$outputHandlersSelect = array(E::ts('- Select -'));
foreach($outputHandlers as $outputHandler) {
$outputHandlersSelect[$outputHandler->getName()] = $outputHandler->getTitle();
}
$this->add('select', 'type', E::ts('Select Field'), $outputHandlersSelect, true, array('class' => 'crm-select2 crm-huge40'));
$this->add('checkbox', 'aggregate', E::ts('Aggregate field'));
}
if ($this->_action == CRM_Core_Action::ADD) {
$this->addButtons(array(
array('type' => 'next', 'name' => E::ts('Next'), 'isDefault' => TRUE,),
array('type' => 'cancel', 'name' => E::ts('Cancel'))));
} elseif ($this->_action == CRM_Core_Action::DELETE) {
$this->addButtons(array(
array('type' => 'next', 'name' => E::ts('Delete'), 'isDefault' => TRUE,),
array('type' => 'cancel', 'name' => E::ts('Cancel'))));
} else {
$this->addButtons(array(
array('type' => 'next', 'name' => E::ts('Save'), 'isDefault' => TRUE,),
array('type' => 'cancel', 'name' => E::ts('Cancel'))));
}
parent::buildQuickForm();
}
function setDefaultValues() {
$defaults = [];
$defaults['data_processor_id'] = $this->dataProcessorId;
$defaults['id'] = $this->id;
$field = CRM_Dataprocessor_BAO_Field::getValues(array('id' => $this->id));
if (isset($field[$this->id]['type'])) {
$defaults['type'] = $field[$this->id]['type'];
}
if (isset($field[$this->id]['title'])) {
$defaults['title'] = $field[$this->id]['title'];
}
if (isset($field[$this->id]['name'])) {
$defaults['name'] = $field[$this->id]['name'];
}
if (isset($field[$this->id]['aggregate'])) {
$defaults['aggregate'] = $field[$this->id]['aggregate'];
}
return $defaults;
}
public function postProcess() {
$session = CRM_Core_Session::singleton();
$redirectUrl = $session->readUserContext();
if ($this->_action == CRM_Core_Action::DELETE) {
CRM_Dataprocessor_BAO_Field::deleteWithId($this->id);
$session->setStatus(E::ts('Field removed'), E::ts('Removed'), 'success');
CRM_Utils_System::redirect($redirectUrl);
}
$values = $this->exportValues();
if (!empty($values['name'])) {
$params['name'] = $values['name'];
} else {
$params['name'] = CRM_Dataprocessor_BAO_Field::buildNameFromTitle($values['title']);
}
$params['title'] = $values['title'];
$params['type'] = $values['type'];
$params['aggregate'] = isset($values['aggregate']) && !empty($values['aggregate']) ? 1 : 0;
if ($this->dataProcessorId) {
$params['data_processor_id'] = $this->dataProcessorId;
}
if ($this->id) {
$params['id'] = $this->id;
}
$result = CRM_Dataprocessor_BAO_Field::add($params);
CRM_Utils_System::redirect($redirectUrl);
parent::postProcess();
}
/**
* Function to add validation rules (overrides parent function)
*
* @access public
*/
function addRules() {
if ($this->_action != CRM_Core_Action::DELETE) {
$this->addFormRule(array(
'CRM_Dataprocessor_Form_Field',
'validateName'
));
}
}
/**
* Function to validate if rule label already exists
*
* @param array $fields
* @return array|bool
* @access static
*/
static function validateName($fields) {
/*
* if id not empty, edit mode. Check if changed before check if exists
*/
$id = false;
if (!empty($fields['id'])) {
$id = $fields['id'];
}
if (empty($fields['name'])) {