Commit a0a665ee authored by jaapjansma's avatar jaapjansma
Browse files

First commit

parents
<?php
/**
* @author Jaap Jansma <jaap.jansma@civicoop.org>
* @license AGPL-3.0
*/
class CRM_Dataprocessor_BAO_DataProcessor extends CRM_Dataprocessor_DAO_DataProcessor {
/**
* Function to get values
*
* @return array $result found rows with data
* @access public
* @static
*/
public static function getValues($params) {
$result = array();
$dataProcessor = new CRM_Dataprocessor_DAO_DataProcessor();
if (!empty($params)) {
$fields = self::fields();
foreach ($params as $key => $value) {
if (isset($fields[$key])) {
$dataProcessor->$key = $value;
}
}
}
$dataProcessor->find();
while ($dataProcessor->fetch()) {
$row = array();
self::storeValues($dataProcessor, $row);
$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');
}
if (!empty($params['id'])) {
CRM_Utils_Hook::pre('edit', 'DataProcessor', $params['id'], $params);
}
else {
CRM_Utils_Hook::pre('create', 'DataProcessor', NULL, $params);
}
$dataProcessor = new CRM_Dataprocessor_DAO_DataProcessor();
$fields = self::fields();
foreach ($params as $key => $value) {
if (isset($fields[$key])) {
$dataProcessor->$key = $value;
}
}
if (empty($dataProcessor->name)) {
$dataProcessor->name = self::buildNameFromTitle($dataProcessor->title);
}
if (empty($dataProcessor->type)) {
$dataProcessor->type = 'default';
}
if (isset($dataProcessor->configuration) && is_array($dataProcessor->configuration)) {
$dataProcessor->configuration = json_encode($dataProcessor->configuration);
}
if (isset($dataProcessor->storage_configuration) && is_array($dataProcessor->storage_configuration)) {
$dataProcessor->storage_configuration = json_encode($dataProcessor->storage_configuration);
}
$dataProcessor->save();
self::storeValues($dataProcessor, $result);
if (!empty($params['id'])) {
CRM_Utils_Hook::post('edit', 'DataProcessor', $dataProcessor->id, $dataProcessor);
}
else {
CRM_Utils_Hook::post('create', 'DataProcessor', $dataProcessor->id, $dataProcessor);
}
return $result;
}
/**
* Function to delete a FormProcessorInstance 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');
}
CRM_Utils_Hook::pre('delete', 'DataProcessor', $id, CRM_Core_DAO::$_nullArray);
$dataProcessor = new CRM_Dataprocessor_DAO_DataProcessor();
$dataProcessor->id = $id;
$dataProcessor->delete();
CRM_Utils_Hook::post('delete', 'DataProcessor', $id, CRM_Core_DAO::$_nullArray);
return;
}
/**
* Function to disable a data processor
*
* @param int $id
* @throws Exception when id is empty
* @access public
* @static
*/
public static function disable($id) {
if (empty($id)) {
throw new Exception('id can not be empty when attempting to disable a data processor');
}
$dataProcessor = new CRM_Dataprocessor_BAO_DataProcessor();
$dataProcessor->id = $id;
$dataProcessor->find(true);
self::add(array('id' => $dataProcessor->id, 'is_active' => 0));
}
/**
* Function to enable a data processor
*
* @param int $id
* @throws Exception when id is empty
* @access public
* @static
*/
public static function enable($id) {
if (empty($id)) {
throw new Exception('id can not be empty when attempting to enable a data processor');
}
$dataProcessor = new CRM_Dataprocessor_BAO_DataProcessor();
$dataProcessor->id = $id;
$dataProcessor->find(true);
self::add(array('id' => $dataProcessor->id, 'is_active' => 1));
}
/**
* 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 $id optional
* @return bool
* @static
*/
public static function isNameValid($name, $id=null) {
$invalidNames = array('getactions', 'getfields', 'get', 'create', 'delete');
if (in_array(strtolower($name), $invalidNames)) {
return false;
}
$sql = "SELECT COUNT(*) FROM `civicrm_data_processor` WHERE `name` = %1";
$params[1] = array($name, 'String');
if ($id) {
$sql .= " AND `id` != %2";
$params[2] = array($id, 'Integer');
}
$count = CRM_Core_DAO::singleValueQuery($sql, $params);
return ($count > 0) ? false : true;
}
public static function getDataProcessorByOutputTypeAndName($output_type, $name) {
$sql = "
SELECT civicrm_data_processor.*
FROM civicrm_data_processor
INNER JOIN civicrm_data_processor_output ON civicrm_data_processor.id = civicrm_data_processor_output.data_processor_id
WHERE is_active = 1 AND civicrm_data_processor.name = %1 AND civicrm_data_processor_output.type = %2
";
$params[1] = array($name, 'String');
$params[2] = array($output_type, 'String');
$dao = CRM_Dataprocessor_BAO_DataProcessor::executeQuery($sql, $params, TRUE, 'CRM_Dataprocessor_BAO_DataProcessor');
if ($dao->N != 1) {
throw new \Exception('Could not find Data Processor');
}
$dao->fetch();
return $dao->getDataProcessor();
}
public function getDataProcessor() {
$factory = \Civi::service('data_processor_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']);
$join = null;
if ($sourceDao['join_type']) {
$join = $factory->getJoinByName($sourceDao['join_type']);
$join->initialize($sourceDao['join_configuration'], $this->id);
}
$dataProcessor->addDataSource($source, $join);
}
return $dataProcessor;
}
/**
* Returns the id of the data processor.
*
* @param string $dataProcessorName
* The name of the data processor.
* @return int
* The id of the data processor.
*/
public static function getId($dataProcessorName) {
$sql = "SELECT `id` FROM `civicrm_data_processor` WHERE `name` = %1";
$params[1] = array($dataProcessorName, 'String');
$id = CRM_Core_DAO::singleValueQuery($sql, $params);
return $id;
}
/**
* Returns the status of the data processor.
* @See CRM_Dataprocessor_DAO_DataProcessor for possible values.
*
* @param string $dataProcessorName
* The name of the data processor.
* @return int
* The status of the data processor.
*/
public static function getStatus($dataProcessorName) {
$sql = "SELECT `status` FROM `civicrm_data_processor` WHERE `name` = %1";
$params[1] = array($dataProcessorName, 'String');
$status = CRM_Core_DAO::singleValueQuery($sql, $params);
return $status;
}
/**
* Updates the status and source file of the data processor.
* @See CRM_Dataprocessor_DAO_DataProcessor for possible status values.
*
* @param string $dataProcessorName
* The name of the data processor.
* @param int $status
* The status value.
* @param string $source_file
* The source file. Leave empty when status is IN_DATABASE.
*/
public static function setStatusAndSourceFile($dataProcessorName, $status, $source_file) {
$sql = "UPDATE `civicrm_data_processor` SET `status` = %2, `source_file` = %3 WHERE `name` = %1";
$params[1] = array($dataProcessorName, 'String');
$params[2] = array($status, 'Integer');
$params[3] = array($source_file, 'String');
CRM_Core_DAO::executeQuery($sql, $params);
}
/**
* Exports a data processor
*
* Returns the array with the whole configuration.
*
* @param $id
* @return array
*/
public static function export($id) {
$dataProcessors = self::getValues(array('id' => $id));
if (!isset($dataProcessors[$id])) {
return array();
}
$dataProcessor = $dataProcessors[$id];
unset($dataProcessor['id']);
unset($dataProcessor['status']);
unset($dataProcessor['source_file']);
$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']);
}
$dataProcessor['outputs'] = CRM_Dataprocessor_BAO_Output::getValues(array('data_processor_id' => $id));
foreach($dataProcessor['outputs'] as $i => $output) {
unset($dataProcessor['outputs'][$i]['id']);
}
return $dataProcessor;
}
/**
* Revert a data processor to the state in code.
*/
public static function revert($data_processor_id) {
$dao = \CRM_Core_DAO::executeQuery("SELECT status, source_file FROM civicrm_data_processor WHERE id = %1", array(1=>array($data_processor_id, 'Integer')));
if (!$dao->fetch()) {
return false;
}
if ($dao->status != CRM_Dataprocessor_DAO_DataProcessor::STATUS_OVERRIDDEN) {
return false;
}
$key = substr($dao->source_file, 0, stripos($dao->source_file, "/"));
$extension = civicrm_api3('Extension', 'getsingle', array('key' => $key));
$filename = $extension['path'].substr($dao->source_file, stripos($dao->source_file, "/"));
$data = file_get_contents($filename);
$data = json_decode($data, true);
CRM_Dataprocessor_Utils_Importer::importDataProcessor($data, $dao->source_file, $data_processor_id);
return true;
}
}
\ No newline at end of file
<?php
/**
* @author Jaap Jansma <jaap.jansma@civicoop.org>
* @license AGPL-3.0
*/
class CRM_Dataprocessor_BAO_Output extends CRM_Dataprocessor_DAO_Output {
/**
* Function to get values
*
* @return array $result found rows with data
* @access public
* @static
*/
public static function getValues($params) {
$factory = \Civi::service('data_processor_factory');
$types = $factory->getOutputs();
$result = array();
$output = new CRM_Dataprocessor_DAO_Output();
if (!empty($params)) {
$fields = self::fields();
foreach ($params as $key => $value) {
if (isset($fields[$key])) {
$output->$key = $value;
}
}
}
$output->find();
while ($output->fetch()) {
$row = array();
self::storeValues($output, $row);
if (isset($types[$row['type']])) {
$row['type_name'] = $types[$row['type']];
} else {
$row['type_name'] = '';
}
$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 output');
}
if (!empty($params['id'])) {
CRM_Utils_Hook::pre('edit', 'DataProcessorOutput', $params['id'], $params);
}
else {
CRM_Utils_Hook::pre('create', 'DataProcessorOutput', NULL, $params);
}
$output = new CRM_Dataprocessor_DAO_Output();
$fields = self::fields();
foreach ($params as $key => $value) {
if (isset($fields[$key])) {
$output->$key = $value;
}
}
if (!isset($output->configuration)) {
$output->configuration = array();
}
if (is_array($output->configuration)) {
$output->configuration = json_encode($output->configuration);
}
$output->save();
self::storeValues($output, $result);
if (!empty($params['id'])) {
CRM_Utils_Hook::post('edit', 'DataProcessorOutput', $output->id, $output);
}
else {
CRM_Utils_Hook::post('create', 'DataProcessorOutput', $output->id, $output);
}
return $result;
}
/**
* Function to delete a Data Processor Output 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 output');
}
CRM_Utils_Hook::pre('delete', 'DataProcessorOutput', $id, CRM_Core_DAO::$_nullArray);
$output = new CRM_Dataprocessor_DAO_Output();
$output->id = $id;
$output->delete();
CRM_Utils_Hook::post('delete', 'DataProcessorOutput', $id, CRM_Core_DAO::$_nullArray);
return;
}
/**
* Function to delete a Data Processor Output 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 output');
}
$output = new CRM_Dataprocessor_DAO_Output();
$output->data_processor_id = $id;
$output->find(FALSE);
while ($output->fetch()) {
self::deleteWithId($output->id);
}
}
}
\ No newline at end of file
<?php
/**
* @author Jaap Jansma <jaap.jansma@civicoop.org>
* @license AGPL-3.0
*/
class CRM_Dataprocessor_BAO_Source extends CRM_Dataprocessor_DAO_Source {
/**
* Function to get values
*
* @return array $result found rows with data
* @access public
* @static
*/
public static function getValues($params) {
$factory = \Civi::service('data_processor_factory');
$types = $factory->getDataSources();
$result = array();
$source = new CRM_Dataprocessor_DAO_Source();
if (!empty($params)) {
$fields = self::fields();
foreach ($params as $key => $value) {
if (isset($fields[$key])) {
$source->$key = $value;
}
}
}
$source->find();
while ($source->fetch()) {
$row = array();
self::storeValues($source, $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 source');
}
if (!empty($params['id'])) {
CRM_Utils_Hook::pre('edit', 'DataProcessorSource', $params['id'], $params);
}
else {
CRM_Utils_Hook::pre('create', 'DataProcessorSource', NULL, $params);
}
$source = new CRM_Dataprocessor_DAO_Source();
$fields = self::fields();
foreach ($params as $key => $value) {
if (isset($fields[$key])) {
$source->$key = $value;
}
}
if (isset($source->configuration) && is_array($source->configuration)) {
$source->configuration = json_encode($source->configuration);
}
if (isset($source->join_configuration) && is_array($source->join_configuration)) {
$source->join_configuration = json_encode($source->join_configuration);
}
$source->save();
self::storeValues($source, $result);
if (!empty($params['id'])) {
CRM_Utils_Hook::post('edit', 'DataProcessorSource', $source->id, $source);
}
else {
CRM_Utils_Hook::post('create', 'DataProcessorSource', $source->id, $source);
}
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_source` 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` != %2";
$params[3] = array($id, 'Integer');
}
$count = CRM_Core_DAO::singleValueQuery($sql, $params);
return ($count > 0) ? false : true;
}
/**
* Function to delete a Data Processor Source with id
*
* @param int $id
* @throws Exception when $id is empty