Added controller UDF to read UDFs definitions

UDFs update&insert second version
This commit is contained in:
Paolo
2017-06-09 15:45:33 +02:00
parent 30c31211aa
commit 294c65a9bf
2 changed files with 102 additions and 51 deletions
@@ -0,0 +1,56 @@
<?php
/**
* FH-Complete
*
* @package FHC-API
* @author FHC-Team
* @copyright Copyright (c) 2016, fhcomplete.org
* @license GPLv3
* @link http://fhcomplete.org
* @since Version 1.0
* @filesource
*/
// ------------------------------------------------------------------------
if (!defined('BASEPATH')) exit('No direct script access allowed');
class UDF extends APIv1_Controller
{
/**
* UDF API constructor.
*/
public function __construct()
{
parent::__construct();
// Load model UDF_model
$this->load->model('system/UDF_model', 'UDFModel');
}
/**
* @return void
*/
public function getUDF()
{
$schema = $this->get('schema');
$table = $this->get('table');
if (isset($schema) || isset($table))
{
$result = $this->UDFModel->loadWhere(
array(
'schema' => $schema,
'table' => $table
)
);
$this->response($result, REST_Controller::HTTP_OK);
}
else
{
$result = $this->UDFModel->load();
$this->response($result, REST_Controller::HTTP_OK);
}
}
}
+46 -51
View File
@@ -97,7 +97,7 @@ class DB_Model extends FHC_Model
// Checks rights
if ($isEntitled = $this->_isEntitled(PermissionLib::REPLACE_RIGHT)) return $isEntitled;
// DB-REPLACE
if ($this->db->replace($this->dbTable, $data))
return success($this->db->insert_id());
@@ -105,51 +105,6 @@ class DB_Model extends FHC_Model
return error($this->db->error(), FHC_DB_ERROR);
}
/**
* Manage UDFs on update
*/
private function _manageUDFUpdate($id, &$data)
{
// Checks if this table has udf
if ($this->_hasUDF())
{
$dbVersionArray = explode('.', $this->db->version());
if ($dbVersionArray[0] == 9 && $dbVersionArray[1] <= 5)
{
$this->addSelect(DB_Model::UDF_FIELD_NAME);
$result = $this->load($id);
if (hasData($result))
{
// Get udf values from $data & clean udf values from $data
// Must be performed here because the load method populates UDFs too
$this->_popUDF($data);
$jsonb = (array)$result->retval[0];
if (count($jsonb) > 0)
{
foreach($this->UDFs as $key => $val)
{
if (isset($jsonb[$key]))
{
$jsonb[$key] = $val;
}
}
$jsonEncodedUDFs = json_encode($jsonb);
if ($jsonEncodedUDFs !== false)
{
$data[DB_Model::UDF_FIELD_NAME] = $jsonEncodedUDFs;
}
}
}
}
else if ($dbVersionArray[0] == 9 && $dbVersionArray[1] > 5)
{
// TODO
}
}
}
/**
* Update Data in DB-Table
*
@@ -171,11 +126,6 @@ class DB_Model extends FHC_Model
// UDFs
$this->_manageUDFUpdate($id, $data);
var_dump($this->UDFs);
var_dump($data);
exit;
// DB-UPDATE
// Check for composite Primary Key
if (is_array($id))
@@ -864,6 +814,51 @@ class DB_Model extends FHC_Model
}
}
/**
* Manage UDFs on update
*/
private function _manageUDFUpdate($id, &$data)
{
// Checks if this table has udf
if ($this->_hasUDF())
{
$dbVersionArray = explode('.', $this->db->version());
if ($dbVersionArray[0] == 9 && $dbVersionArray[1] <= 4) // If postgresql version is <= 9.4
{
$this->addSelect(DB_Model::UDF_FIELD_NAME);
$result = $this->load($id);
if (hasData($result))
{
// Get udf values from $data & clean udf values from $data
// Must be performed here because the load method populates UDFs too
$this->_popUDF($data);
$jsonb = (array)$result->retval[0]; // convert the result to an array
if (count($jsonb) > 0) // if UDFs are present
{
foreach($this->UDFs as $key => $val)
{
if (isset($jsonb[$key]))
{
$jsonb[$key] = $val;
}
}
$jsonEncodedUDFs = json_encode($jsonb); // encode to json
if ($jsonEncodedUDFs !== false)
{
$data[DB_Model::UDF_FIELD_NAME] = $jsonEncodedUDFs;
}
}
}
}
else if ($dbVersionArray[0] == 9 && $dbVersionArray[1] > 4) // If postgresql version is > 9.4
{
// TODO: use jsonb_set
}
}
}
/**
* Checks if the caller is entitled to perform this operation with this right
*/