mirror of
https://github.com/FH-Complete/FHC-Core.git
synced 2026-07-19 16:02:15 +00:00
Merge branch 'feature-4026/TabulatorWidget' into feature-3994/Digitaler_Lehrauftrag
This commit is contained in:
@@ -1,361 +0,0 @@
|
||||
<?php
|
||||
|
||||
if (! defined('BASEPATH')) exit('No direct script access allowed');
|
||||
|
||||
/**
|
||||
* Library used to call a method of a model or a library
|
||||
*/
|
||||
class CallerLib
|
||||
{
|
||||
const RESOURCE_PARAMETER = 'resource';
|
||||
const FUNCTION_PARAMETER = 'function';
|
||||
const REG_SPLIT_EXPR = '/\//';
|
||||
const LIB_PREFIX = 'Lib';
|
||||
const LIB_FILE_EXTENSION = '.php';
|
||||
const LIBS_PATH = 'libraries';
|
||||
const MODEL_PREFIX = '_model';
|
||||
|
||||
// Black list of resources that are no allowed to be used
|
||||
private static $RESOURCES_BLACK_LIST = array(
|
||||
'CallerLib', // disabled self loading
|
||||
'LogLib', // hardly usefull and virtually dangerous
|
||||
'MigrationLib', // virtually dangerous, DB manipulation
|
||||
'FilesystemLib', // virtually dangerous, direct access to file system
|
||||
'PermissionLib', // usefull?
|
||||
'PersonLogLib'
|
||||
);
|
||||
|
||||
private $_ci; // CI instance
|
||||
|
||||
/**
|
||||
* Library initialization
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->_ci =& get_instance(); // Gets CI instance
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper method for _call
|
||||
*/
|
||||
public function callLibrary($callParameters)
|
||||
{
|
||||
return $this->_call($callParameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper method for _call
|
||||
*/
|
||||
public function callModel($callParameters)
|
||||
{
|
||||
return $this->_call($callParameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* Everything starts here...
|
||||
*/
|
||||
private function _call($callParameters)
|
||||
{
|
||||
$result = null;
|
||||
$parameters = $this->_getParameters($callParameters);
|
||||
$validation = $this->_validateCall($parameters);
|
||||
|
||||
// If the validation was passed
|
||||
if (isSuccess($validation))
|
||||
{
|
||||
$loaded = null;
|
||||
// If the given resource is a model
|
||||
if (strpos($parameters->resourceName, CallerLib::MODEL_PREFIX) !== false)
|
||||
{
|
||||
// Try to load the model
|
||||
$result = $this->_loadModel($parameters->resourcePath, $parameters->resourceName);
|
||||
if (isSuccess($result))
|
||||
{
|
||||
$loaded = $result->retval;
|
||||
}
|
||||
}
|
||||
// If the given resource is a library
|
||||
elseif (strpos($parameters->resourceName, CallerLib::LIB_PREFIX) !== false)
|
||||
{
|
||||
// Check if the resource is already loaded, it works only with libraries and drivers
|
||||
$isLoaded = $this->_ci->load->is_loaded($parameters->resourceName);
|
||||
// If not loaded then load it
|
||||
if ($isLoaded === false)
|
||||
{
|
||||
// Try to load the library
|
||||
$result = $this->_loadLibrary($parameters->resourcePath, $parameters->resourceName);
|
||||
if (isSuccess($result))
|
||||
{
|
||||
$loaded = $result->retval;
|
||||
}
|
||||
}
|
||||
// If it is already loaded $isLoaded contains the instance of the library
|
||||
else
|
||||
{
|
||||
$loaded = $isLoaded;
|
||||
}
|
||||
}
|
||||
// Wrong selection!
|
||||
else
|
||||
{
|
||||
$result = error('Neither a lib nor model: '.$parameters->resourcePath.$parameters->resourceName);
|
||||
}
|
||||
|
||||
// If the resource was found and loaded
|
||||
if (!is_null($loaded))
|
||||
{
|
||||
$result = $this->_callThis($parameters->resourceName, $parameters->function, $parameters->parameters);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Resource not loaded
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$result = $validation;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the parameters from the http call
|
||||
* Search for parameters <RESOURCE_PARAMETER> and <FUNCTION_PARAMETER>
|
||||
* <RESOURCE_PARAMETER> is the name of the model or of the library
|
||||
* <FUNCTION_PARAMETER> is the name of the method present in the model/library
|
||||
* All the others parameters will be given to the method in the same order that
|
||||
* they are present in the HTTP call
|
||||
* EX:
|
||||
* URL: ../system/CallerLibrary/Call?resource=<resource>&function=<method>&<par1>=<val1>&<par2>=<val2>&<par3>=<val3>
|
||||
* will call <resource>.<method>(par1, par2, par3)
|
||||
*/
|
||||
private function _getParameters($parametersArray)
|
||||
{
|
||||
$parameters = new stdClass();
|
||||
$parameters->parameters = array();
|
||||
$count = 0;
|
||||
|
||||
foreach ($parametersArray as $parameterName => $parameterValue)
|
||||
{
|
||||
// The name of the resource, path included
|
||||
if ($parameterName == CallerLib::RESOURCE_PARAMETER)
|
||||
{
|
||||
// Separates the resource path from the resource name
|
||||
$splittedResource = preg_split(CallerLib::REG_SPLIT_EXPR, $parameterValue);
|
||||
$parameters->resourceName = $splittedResource[count($splittedResource) - 1];
|
||||
$parameters->resourcePath = str_replace($parameters->resourceName, '', $parameterValue);
|
||||
}
|
||||
// The name of the function
|
||||
elseif ($parameterName == CallerLib::FUNCTION_PARAMETER)
|
||||
{
|
||||
$parameters->function = $parameterValue;
|
||||
}
|
||||
// It is assumed that all other parameters are the parameters to be passed to the function
|
||||
// They will be passed to the function in the same order in which they are passed to
|
||||
// this controller
|
||||
else
|
||||
{
|
||||
$parameters->parameters[$count++] = $parameterValue;
|
||||
}
|
||||
}
|
||||
|
||||
return $parameters;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate the given parameters
|
||||
*/
|
||||
private function _validateCall($parameters)
|
||||
{
|
||||
if (!is_object($parameters))
|
||||
{
|
||||
return error('Parameter is not an object');
|
||||
}
|
||||
if (!isset($parameters->resourcePath))
|
||||
{
|
||||
return error('Resource path is not specified');
|
||||
}
|
||||
if (!isset($parameters->resourceName))
|
||||
{
|
||||
return error('Resource name is not specified');
|
||||
}
|
||||
if (!isset($parameters->function))
|
||||
{
|
||||
return error('Function is not specified');
|
||||
}
|
||||
if (!is_array($parameters->parameters))
|
||||
{
|
||||
return error('Parameters are not specified');
|
||||
}
|
||||
if (in_array($parameters->resourceName, CallerLib::$RESOURCES_BLACK_LIST))
|
||||
{
|
||||
return error('You are trying to access to unauthorized resources');
|
||||
}
|
||||
|
||||
return success('Input data are valid');
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads a model using the given path and name
|
||||
*
|
||||
* NOTE: the models automatically handle the permissions
|
||||
*/
|
||||
private function _loadModel($resourcePath, $resourceName)
|
||||
{
|
||||
$loaded = null;
|
||||
$result = null;
|
||||
|
||||
try
|
||||
{
|
||||
$loaded = $this->_ci->load->model($resourcePath.$resourceName);
|
||||
}
|
||||
catch (Exception $e)
|
||||
{
|
||||
// Errors while loading the model
|
||||
$result = error('Errors while loading the model: '.$e->getMessage());
|
||||
}
|
||||
|
||||
if (!is_null($loaded))
|
||||
{
|
||||
$result = success($loaded);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads a library using the given path and name
|
||||
*
|
||||
* The method 'library' of the class CI_Loader provided by CI has some limitations,
|
||||
* so to be able to check errors was used a workaround.
|
||||
* It consists in:
|
||||
* - Checking if the file (identified by parameters $resourcePath and $resourceName) exists
|
||||
* - If exists it will be loaded using the method 'file' from CI_Loader
|
||||
* - Checks if the loaded file contains a class identified by parameter $resourceName
|
||||
*
|
||||
* If one of the previous tests fails, it will be returned a null value
|
||||
*/
|
||||
private function _loadLibrary($resourcePath, $resourceName)
|
||||
{
|
||||
$loaded = null;
|
||||
|
||||
try
|
||||
{
|
||||
// Gets all the configured resources paths
|
||||
$packagePaths = $this->_ci->load->get_package_paths();
|
||||
// Looking for a file in every paths with the same name of the resource
|
||||
$found = null;
|
||||
for ($i = 0; $i < count($packagePaths) && is_null($found); $i++)
|
||||
{
|
||||
$file = $packagePaths[$i].CallerLib::LIBS_PATH.DIRECTORY_SEPARATOR.
|
||||
$resourcePath.$resourceName.CallerLib::LIB_FILE_EXTENSION;
|
||||
if (file_exists($file))
|
||||
{
|
||||
$found = $file;
|
||||
}
|
||||
}
|
||||
|
||||
// If the file was found
|
||||
if (!is_null($found))
|
||||
{
|
||||
// Load the file
|
||||
$loaded = $this->_ci->load->file($found);
|
||||
// If the resource is not present inside the file
|
||||
if (!class_exists($resourceName))
|
||||
{
|
||||
$loaded = null;
|
||||
// Same phrase error as load->model() provided by CI
|
||||
$result = error($found.' exists, but doesn\'t declare class '.$resourceName);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$loaded = null;
|
||||
// Same phrase error as load->model() provided by CI
|
||||
$result = error('Unable to load the requested class: '.$resourceName);
|
||||
}
|
||||
}
|
||||
catch (Exception $e)
|
||||
{
|
||||
// Errors while loading the library
|
||||
$result = error('Errors while loading the library: '.$e->getMessage());
|
||||
}
|
||||
|
||||
if (!is_null($loaded))
|
||||
{
|
||||
$result = success($loaded);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls a method of a class with the given parameters and returns its result
|
||||
*
|
||||
* @param string $resourceName identifies the class name
|
||||
* @param string $function identifies the method name
|
||||
* @param array $parameters contains the parameters to be passed to the method
|
||||
*/
|
||||
private function _callThis($resourceName, $function, $parameters)
|
||||
{
|
||||
$result = null;
|
||||
|
||||
try
|
||||
{
|
||||
// Get informations about the function
|
||||
$reflectionMethod = new ReflectionMethod($resourceName, $function);
|
||||
// If the number of given parameters is greater or equal to the number of
|
||||
// parameters required by the function
|
||||
if (count($parameters) >= $reflectionMethod->getNumberOfRequiredParameters())
|
||||
{
|
||||
// If the function is static
|
||||
if ($reflectionMethod->isStatic() === true)
|
||||
{
|
||||
$classMethod = $resourceName.'::'.$function;
|
||||
}
|
||||
// If the function is not static
|
||||
else
|
||||
{
|
||||
$classMethod = array(new $resourceName(), $function);
|
||||
}
|
||||
|
||||
// If the resource's function is callable
|
||||
if (is_callable($classMethod))
|
||||
{
|
||||
// Call resource->function()
|
||||
// @ was applied to prevent really ugly and unmanageable errors
|
||||
$resultCall = @call_user_func_array($classMethod, $parameters);
|
||||
// If errors occurred while running it
|
||||
// NOTE: if the called function via call_user_func_array returns a boolean set as false,
|
||||
// it will be recognized like a running error. A little bit tricky ;)
|
||||
if ($resultCall === false)
|
||||
{
|
||||
$result = error('Error running '.$resourceName.'->'.$function.'()');
|
||||
}
|
||||
// Returns the result of resource->function()
|
||||
else
|
||||
{
|
||||
$result = success($resultCall);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$result = error($resourceName.'->'.$function.'() is not callable!');
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$result = error(
|
||||
'Number of required parameters: '.$reflectionMethod->getNumberOfRequiredParameters().'. Given: '.count($parameters)
|
||||
);
|
||||
}
|
||||
}
|
||||
catch (Exception $e)
|
||||
{
|
||||
$result = error($e->getMessage());
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
@@ -532,6 +532,14 @@ class FilterWidgetLib
|
||||
return $applyFilters;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reloads dataset by setting session variable to true
|
||||
*/
|
||||
public function reloadDataset()
|
||||
{
|
||||
$this->setSessionElement(self::SESSION_RELOAD_DATASET, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a filter (SQL where clause) to be applied to the current filter
|
||||
*/
|
||||
@@ -795,32 +803,7 @@ class FilterWidgetLib
|
||||
$filterUniqueId = $this->_ci->router->directory.$this->_ci->router->class.'/'.$this->_ci->router->method;
|
||||
}
|
||||
|
||||
if ($params != null
|
||||
&& is_array($params)
|
||||
&& (isset($params[self::APP_PARAMETER]) || isset($params[self::DATASET_NAME_PARAMETER]) || isset($params[self::FILTER_ID])))
|
||||
{
|
||||
$app = '';
|
||||
$dataset = '';
|
||||
$filterid = '';
|
||||
|
||||
if (isset($params[self::APP_PARAMETER])) $app = $params[self::APP_PARAMETER];
|
||||
if (isset($params[self::DATASET_NAME_PARAMETER])) $dataset = $params[self::DATASET_NAME_PARAMETER];
|
||||
if (isset($params[self::FILTER_ID])) $filterid = $params[self::FILTER_ID];
|
||||
|
||||
$filterUniqueId .= '/'.$app.':'.$dataset.':'.$filterid;
|
||||
}
|
||||
|
||||
// If the FHC_CONTROLLER_ID parameter is present in the HTTP GET
|
||||
if (isset($_GET[self::FHC_CONTROLLER_ID]))
|
||||
{
|
||||
$filterUniqueId .= '/'.$this->_ci->input->get(self::FHC_CONTROLLER_ID); // then use it
|
||||
}
|
||||
elseif (isset($_POST[self::FHC_CONTROLLER_ID])) // else if the FHC_CONTROLLER_ID parameter is present in the HTTP POST
|
||||
{
|
||||
$filterUniqueId .= '/'.$this->_ci->input->post(self::FHC_CONTROLLER_ID); // then use it
|
||||
}
|
||||
|
||||
$this->_filterUniqueId = $filterUniqueId;
|
||||
$this->setFilterUniqueId($filterUniqueId);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,531 +0,0 @@
|
||||
<?php
|
||||
|
||||
if (! defined('BASEPATH')) exit('No direct script access allowed');
|
||||
|
||||
/**
|
||||
* Format class
|
||||
* Help convert between various formats such as XML, JSON, CSV, etc.
|
||||
*
|
||||
* @author Phil Sturgeon, Chris Kacerguis, @softwarespot
|
||||
* @license http://www.dbad-license.org/
|
||||
*/
|
||||
class Format {
|
||||
|
||||
/**
|
||||
* Array output format
|
||||
*/
|
||||
const ARRAY_FORMAT = 'array';
|
||||
|
||||
/**
|
||||
* Comma Separated Value (CSV) output format
|
||||
*/
|
||||
const CSV_FORMAT = 'csv';
|
||||
|
||||
/**
|
||||
* Json output format
|
||||
*/
|
||||
const JSON_FORMAT = 'json';
|
||||
|
||||
/**
|
||||
* HTML output format
|
||||
*/
|
||||
const HTML_FORMAT = 'html';
|
||||
|
||||
/**
|
||||
* PHP output format
|
||||
*/
|
||||
const PHP_FORMAT = 'php';
|
||||
|
||||
/**
|
||||
* Serialized output format
|
||||
*/
|
||||
const SERIALIZED_FORMAT = 'serialized';
|
||||
|
||||
/**
|
||||
* XML output format
|
||||
*/
|
||||
const XML_FORMAT = 'xml';
|
||||
|
||||
/**
|
||||
* Default format of this class
|
||||
*/
|
||||
const DEFAULT_FORMAT = self::JSON_FORMAT; // Couldn't be DEFAULT, as this is a keyword
|
||||
|
||||
/**
|
||||
* CodeIgniter instance
|
||||
*
|
||||
* @var object
|
||||
*/
|
||||
private $_CI;
|
||||
|
||||
/**
|
||||
* Data to parse
|
||||
*
|
||||
* @var mixed
|
||||
*/
|
||||
protected $_data = [];
|
||||
|
||||
/**
|
||||
* Type to convert from
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_from_type = NULL;
|
||||
|
||||
/**
|
||||
* DO NOT CALL THIS DIRECTLY, USE factory()
|
||||
*
|
||||
* @param NULL $data
|
||||
* @param NULL $from_type
|
||||
* @throws Exception
|
||||
*/
|
||||
|
||||
public function __construct($data = NULL, $from_type = NULL)
|
||||
{
|
||||
// Get the CodeIgniter reference
|
||||
$this->_CI = &get_instance();
|
||||
|
||||
// Load the inflector helper
|
||||
$this->_CI->load->helper('inflector');
|
||||
|
||||
// If the provided data is already formatted we should probably convert it to an array
|
||||
if ($from_type !== NULL)
|
||||
{
|
||||
if (method_exists($this, '_from_' . $from_type))
|
||||
{
|
||||
$data = call_user_func([$this, '_from_' . $from_type], $data);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception('Format class does not support conversion from "' . $from_type . '".');
|
||||
}
|
||||
}
|
||||
|
||||
// Set the member variable to the data passed
|
||||
$this->_data = $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an instance of the format class
|
||||
* e.g: echo $this->format->factory(['foo' => 'bar'])->to_csv();
|
||||
*
|
||||
* @param mixed $data Data to convert/parse
|
||||
* @param string $from_type Type to convert from e.g. json, csv, html
|
||||
*
|
||||
* @return object Instance of the format class
|
||||
*/
|
||||
public function factory($data, $from_type = NULL)
|
||||
{
|
||||
// $class = __CLASS__;
|
||||
// return new $class();
|
||||
|
||||
return new static($data, $from_type);
|
||||
}
|
||||
|
||||
// FORMATTING OUTPUT ---------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Format data as an array
|
||||
*
|
||||
* @param mixed|NULL $data Optional data to pass, so as to override the data passed
|
||||
* to the constructor
|
||||
* @return array Data parsed as an array; otherwise, an empty array
|
||||
*/
|
||||
public function to_array($data = NULL)
|
||||
{
|
||||
// If no data is passed as a parameter, then use the data passed
|
||||
// via the constructor
|
||||
if ($data === NULL && func_num_args() === 0)
|
||||
{
|
||||
$data = $this->_data;
|
||||
}
|
||||
|
||||
// Cast as an array if not already
|
||||
if (is_array($data) === FALSE)
|
||||
{
|
||||
$data = (array) $data;
|
||||
}
|
||||
|
||||
$array = [];
|
||||
foreach ((array) $data as $key => $value)
|
||||
{
|
||||
if (is_object($value) === TRUE || is_array($value) === TRUE)
|
||||
{
|
||||
$array[$key] = $this->to_array($value);
|
||||
}
|
||||
else
|
||||
{
|
||||
$array[$key] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
return $array;
|
||||
}
|
||||
|
||||
/**
|
||||
* Format data as XML
|
||||
*
|
||||
* @param mixed|NULL $data Optional data to pass, so as to override the data passed
|
||||
* to the constructor
|
||||
* @param NULL $structure
|
||||
* @param string $basenode
|
||||
* @return mixed
|
||||
*/
|
||||
public function to_xml($data = NULL, $structure = NULL, $basenode = 'xml')
|
||||
{
|
||||
if ($data === NULL && func_num_args() === 0)
|
||||
{
|
||||
$data = $this->_data;
|
||||
}
|
||||
|
||||
// turn off compatibility mode as simple xml throws a wobbly if you don't.
|
||||
if (ini_get('zend.ze1_compatibility_mode') == 1)
|
||||
{
|
||||
ini_set('zend.ze1_compatibility_mode', 0);
|
||||
}
|
||||
|
||||
if ($structure === NULL)
|
||||
{
|
||||
$structure = simplexml_load_string("<?xml version='1.0' encoding='utf-8'?><$basenode />");
|
||||
}
|
||||
|
||||
// Force it to be something useful
|
||||
if (is_array($data) === FALSE && is_object($data) === FALSE)
|
||||
{
|
||||
$data = (array) $data;
|
||||
}
|
||||
|
||||
foreach ($data as $key => $value)
|
||||
{
|
||||
|
||||
//change false/true to 0/1
|
||||
if (is_bool($value))
|
||||
{
|
||||
$value = (int) $value;
|
||||
}
|
||||
|
||||
// no numeric keys in our xml please!
|
||||
if (is_numeric($key))
|
||||
{
|
||||
// make string key...
|
||||
$key = (singular($basenode) != $basenode) ? singular($basenode) : 'item';
|
||||
}
|
||||
|
||||
// replace anything not alpha numeric
|
||||
$key = preg_replace('/[^a-z_\-0-9]/i', '', $key);
|
||||
|
||||
if ($key === '_attributes' && (is_array($value) || is_object($value)))
|
||||
{
|
||||
$attributes = $value;
|
||||
if (is_object($attributes))
|
||||
{
|
||||
$attributes = get_object_vars($attributes);
|
||||
}
|
||||
|
||||
foreach ($attributes as $attribute_name => $attribute_value)
|
||||
{
|
||||
$structure->addAttribute($attribute_name, $attribute_value);
|
||||
}
|
||||
}
|
||||
// if there is another array found recursively call this function
|
||||
elseif (is_array($value) || is_object($value))
|
||||
{
|
||||
$node = $structure->addChild($key);
|
||||
|
||||
// recursive call.
|
||||
$this->to_xml($value, $node, $key);
|
||||
}
|
||||
else
|
||||
{
|
||||
// add single node.
|
||||
$value = htmlspecialchars(html_entity_decode($value, ENT_QUOTES, 'UTF-8'), ENT_QUOTES, 'UTF-8');
|
||||
|
||||
$structure->addChild($key, $value);
|
||||
}
|
||||
}
|
||||
|
||||
return $structure->asXML();
|
||||
}
|
||||
|
||||
/**
|
||||
* Format data as HTML
|
||||
*
|
||||
* @param mixed|NULL $data Optional data to pass, so as to override the data passed
|
||||
* to the constructor
|
||||
* @return mixed
|
||||
*/
|
||||
public function to_html($data = NULL)
|
||||
{
|
||||
// If no data is passed as a parameter, then use the data passed
|
||||
// via the constructor
|
||||
if ($data === NULL && func_num_args() === 0)
|
||||
{
|
||||
$data = $this->_data;
|
||||
}
|
||||
|
||||
// Cast as an array if not already
|
||||
if (is_array($data) === FALSE)
|
||||
{
|
||||
$data = (array) $data;
|
||||
}
|
||||
|
||||
// Check if it's a multi-dimensional array
|
||||
if (isset($data[0]) && count($data) !== count($data, COUNT_RECURSIVE))
|
||||
{
|
||||
// Multi-dimensional array
|
||||
$headings = array_keys($data[0]);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Single array
|
||||
$headings = array_keys($data);
|
||||
$data = [$data];
|
||||
}
|
||||
|
||||
// Load the table library
|
||||
$this->_CI->load->library('table');
|
||||
|
||||
$this->_CI->table->set_heading($headings);
|
||||
|
||||
foreach ($data as $row)
|
||||
{
|
||||
// Suppressing the "array to string conversion" notice
|
||||
// Keep the "evil" @ here
|
||||
$row = @array_map('strval', $row);
|
||||
|
||||
$this->_CI->table->add_row($row);
|
||||
}
|
||||
|
||||
return $this->_CI->table->generate();
|
||||
}
|
||||
|
||||
/**
|
||||
* @link http://www.metashock.de/2014/02/create-csv-file-in-memory-php/
|
||||
* @param mixed|NULL $data Optional data to pass, so as to override the data passed
|
||||
* to the constructor
|
||||
* @param string $delimiter The optional delimiter parameter sets the field
|
||||
* delimiter (one character only). NULL will use the default value (,)
|
||||
* @param string $enclosure The optional enclosure parameter sets the field
|
||||
* enclosure (one character only). NULL will use the default value (")
|
||||
* @return string A csv string
|
||||
*/
|
||||
public function to_csv($data = NULL, $delimiter = ',', $enclosure = '"')
|
||||
{
|
||||
// Use a threshold of 1 MB (1024 * 1024)
|
||||
$handle = fopen('php://temp/maxmemory:1048576', 'w');
|
||||
if ($handle === FALSE)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// If no data is passed as a parameter, then use the data passed
|
||||
// via the constructor
|
||||
if ($data === NULL && func_num_args() === 0)
|
||||
{
|
||||
$data = $this->_data;
|
||||
}
|
||||
|
||||
// If NULL, then set as the default delimiter
|
||||
if ($delimiter === NULL)
|
||||
{
|
||||
$delimiter = ',';
|
||||
}
|
||||
|
||||
// If NULL, then set as the default enclosure
|
||||
if ($enclosure === NULL)
|
||||
{
|
||||
$enclosure = '"';
|
||||
}
|
||||
|
||||
// Cast as an array if not already
|
||||
if (is_array($data) === FALSE)
|
||||
{
|
||||
$data = (array) $data;
|
||||
}
|
||||
|
||||
// Check if it's a multi-dimensional array
|
||||
if (isset($data[0]) && count($data) !== count($data, COUNT_RECURSIVE))
|
||||
{
|
||||
// Multi-dimensional array
|
||||
$headings = array_keys($data[0]);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Single array
|
||||
$headings = array_keys($data);
|
||||
$data = [$data];
|
||||
}
|
||||
|
||||
// Apply the headings
|
||||
fputcsv($handle, $headings, $delimiter, $enclosure);
|
||||
|
||||
foreach ($data as $record)
|
||||
{
|
||||
// If the record is not an array, then break. This is because the 2nd param of
|
||||
// fputcsv() should be an array
|
||||
if (is_array($record) === FALSE)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
// Suppressing the "array to string conversion" notice.
|
||||
// Keep the "evil" @ here.
|
||||
$record = @ array_map('strval', $record);
|
||||
|
||||
// Returns the length of the string written or FALSE
|
||||
fputcsv($handle, $record, $delimiter, $enclosure);
|
||||
}
|
||||
|
||||
// Reset the file pointer
|
||||
rewind($handle);
|
||||
|
||||
// Retrieve the csv contents
|
||||
$csv = stream_get_contents($handle);
|
||||
|
||||
// Close the handle
|
||||
fclose($handle);
|
||||
|
||||
return $csv;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode data as json
|
||||
*
|
||||
* @param mixed|NULL $data Optional data to pass, so as to override the data passed
|
||||
* to the constructor
|
||||
* @return string Json representation of a value
|
||||
*/
|
||||
public function to_json($data = NULL)
|
||||
{
|
||||
// If no data is passed as a parameter, then use the data passed
|
||||
// via the constructor
|
||||
if ($data === NULL && func_num_args() === 0)
|
||||
{
|
||||
$data = $this->_data;
|
||||
}
|
||||
|
||||
// Get the callback parameter (if set)
|
||||
$callback = $this->_CI->input->get('callback');
|
||||
|
||||
if (empty($callback) === TRUE)
|
||||
{
|
||||
return json_encode($data);
|
||||
}
|
||||
|
||||
// We only honour a jsonp callback which are valid javascript identifiers
|
||||
elseif (preg_match('/^[a-z_\$][a-z0-9\$_]*(\.[a-z_\$][a-z0-9\$_]*)*$/i', $callback))
|
||||
{
|
||||
// Return the data as encoded json with a callback
|
||||
return $callback . '(' . json_encode($data) . ');';
|
||||
}
|
||||
|
||||
// An invalid jsonp callback function provided.
|
||||
// Though I don't believe this should be hardcoded here
|
||||
$data['warning'] = 'INVALID JSONP CALLBACK: ' . $callback;
|
||||
|
||||
return json_encode($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode data as a serialized array
|
||||
*
|
||||
* @param mixed|NULL $data Optional data to pass, so as to override the data passed
|
||||
* to the constructor
|
||||
* @return string Serialized data
|
||||
*/
|
||||
public function to_serialized($data = NULL)
|
||||
{
|
||||
// If no data is passed as a parameter, then use the data passed
|
||||
// via the constructor
|
||||
if ($data === NULL && func_num_args() === 0)
|
||||
{
|
||||
$data = $this->_data;
|
||||
}
|
||||
|
||||
return serialize($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Format data using a PHP structure
|
||||
*
|
||||
* @param mixed|NULL $data Optional data to pass, so as to override the data passed
|
||||
* to the constructor
|
||||
* @return mixed String representation of a variable
|
||||
*/
|
||||
public function to_php($data = NULL)
|
||||
{
|
||||
// If no data is passed as a parameter, then use the data passed
|
||||
// via the constructor
|
||||
if ($data === NULL && func_num_args() === 0)
|
||||
{
|
||||
$data = $this->_data;
|
||||
}
|
||||
|
||||
return var_export($data, TRUE);
|
||||
}
|
||||
|
||||
// INTERNAL FUNCTIONS
|
||||
|
||||
/**
|
||||
* @param $data XML string
|
||||
* @return SimpleXMLElement XML element object; otherwise, empty array
|
||||
*/
|
||||
protected function _from_xml($data)
|
||||
{
|
||||
return $data ? (array) simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA) : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $data CSV string
|
||||
* @param string $delimiter The optional delimiter parameter sets the field
|
||||
* delimiter (one character only). NULL will use the default value (,)
|
||||
* @param string $enclosure The optional enclosure parameter sets the field
|
||||
* enclosure (one character only). NULL will use the default value (")
|
||||
* @return array A multi-dimensional array with the outer array being the number of rows
|
||||
* and the inner arrays the individual fields
|
||||
*/
|
||||
protected function _from_csv($data, $delimiter = ',', $enclosure = '"')
|
||||
{
|
||||
// If NULL, then set as the default delimiter
|
||||
if ($delimiter === NULL)
|
||||
{
|
||||
$delimiter = ',';
|
||||
}
|
||||
|
||||
// If NULL, then set as the default enclosure
|
||||
if ($enclosure === NULL)
|
||||
{
|
||||
$enclosure = '"';
|
||||
}
|
||||
|
||||
return str_getcsv($data, $delimiter, $enclosure);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $data Encoded json string
|
||||
* @return mixed Decoded json string with leading and trailing whitespace removed
|
||||
*/
|
||||
protected function _from_json($data)
|
||||
{
|
||||
return json_decode(trim($data));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string Data to unserialized
|
||||
* @return mixed Unserialized data
|
||||
*/
|
||||
protected function _from_serialize($data)
|
||||
{
|
||||
return unserialize(trim($data));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $data Data to trim leading and trailing whitespace
|
||||
* @return string Data with leading and trailing whitespace removed
|
||||
*/
|
||||
protected function _from_php($data)
|
||||
{
|
||||
return trim($data);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,102 +1,252 @@
|
||||
<?php
|
||||
|
||||
if (! defined('BASEPATH')) exit('No direct script access allowed');
|
||||
if (!defined('BASEPATH')) exit('No direct script access allowed');
|
||||
|
||||
/**
|
||||
* Library usefull for logging!
|
||||
* This library can log using CodeIgniter log system (file system) or to database
|
||||
*/
|
||||
class LogLib
|
||||
{
|
||||
const DEBUG = 'debug';
|
||||
const ERROR = 'error';
|
||||
// Log levels
|
||||
const INFO = 'info';
|
||||
const DEBUG = 'debug';
|
||||
const WARNING = 'warning';
|
||||
const ERROR = 'error';
|
||||
|
||||
// Default debug trace levels
|
||||
const CLASS_INDEX = 3;
|
||||
const FUNCTION_INDEX = 3;
|
||||
const LINE_INDEX = 2;
|
||||
|
||||
const DB_EXECUTE_USER = 'LogLib'; // Default execute user
|
||||
|
||||
// Caller data names
|
||||
const CLASS_NAME = 'className';
|
||||
const FUNCTION_NAME = 'functionName';
|
||||
const CODE_LINE = 'codeLine';
|
||||
|
||||
// To format the log message prefix when logging to file system
|
||||
const CALLER_PREFIX = '[';
|
||||
const CALLER_POSTFIX = ']';
|
||||
const CLASS_POSTFIX = '->';
|
||||
const LINE_SEPARATOR = ':';
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------
|
||||
// Public methods
|
||||
// CodeIgniter configuration log entry name and log debug value
|
||||
const CI_LOG_THRESHOLD_NAME = 'log_threshold';
|
||||
const CI_LOG_THRESHOLD_DEBUG = 2;
|
||||
|
||||
// LogLib parameters names
|
||||
const P_NAME_CLASS_INDEX = 'classIndex';
|
||||
const P_NAME_FUNCTION_INDEX = 'functionIndex';
|
||||
const P_NAME_LINE_INDEX = 'lineIndex';
|
||||
const P_NAME_DB_LOG_TYPE = 'dbLogType';
|
||||
const P_NAME_DB_EXECUTE_USER = 'dbExecuteUser';
|
||||
|
||||
// Properties used to retrieve caller data
|
||||
private $_classIndex;
|
||||
private $_functionIndex;
|
||||
private $_lineIndex;
|
||||
|
||||
// Properties used when logging to database
|
||||
private $_dbLogType;
|
||||
private $_dbExecuteUser;
|
||||
|
||||
/**
|
||||
* logDebug
|
||||
* Set properties to a default value or overwrites them with the given parameters
|
||||
*/
|
||||
public function __construct($params = null)
|
||||
{
|
||||
// Properties default values
|
||||
$this->_classIndex = self::CLASS_INDEX;
|
||||
$this->_functionIndex = self::FUNCTION_INDEX;
|
||||
$this->_lineIndex = self::LINE_INDEX;
|
||||
$this->_dbLogType = null;
|
||||
$this->_dbExecuteUser = self::DB_EXECUTE_USER;
|
||||
|
||||
// If parameters are given then overwrite the default values
|
||||
if (!isEmptyArray($params))
|
||||
{
|
||||
if (isset($params[self::P_NAME_CLASS_INDEX])) $this->_classIndex = $params[self::P_NAME_CLASS_INDEX];
|
||||
if (isset($params[self::P_NAME_FUNCTION_INDEX])) $this->_functionIndex = $params[self::P_NAME_FUNCTION_INDEX];
|
||||
if (isset($params[self::P_NAME_LINE_INDEX])) $this->_lineIndex = $params[self::P_NAME_LINE_INDEX];
|
||||
if (isset($params[self::P_NAME_DB_LOG_TYPE])) $this->_dbLogType = $params[self::P_NAME_DB_LOG_TYPE];
|
||||
if (isset($params[self::P_NAME_DB_EXECUTE_USER])) $this->_dbExecuteUser = $params[self::P_NAME_DB_EXECUTE_USER];
|
||||
}
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------
|
||||
// Public methods based on CodeIgniter log system
|
||||
|
||||
/**
|
||||
* Writes a debug log to CodeIgniter log
|
||||
*/
|
||||
public function logDebug($message)
|
||||
{
|
||||
$this->_log(LogLib::DEBUG, $message);
|
||||
$this->_log(self::DEBUG, $message);
|
||||
}
|
||||
|
||||
/**
|
||||
* logInfo
|
||||
* Writes an info log to CodeIgniter log
|
||||
*/
|
||||
public function logInfo($message)
|
||||
{
|
||||
$this->_log(LogLib::INFO, $message);
|
||||
$this->_log(self::INFO, $message);
|
||||
}
|
||||
|
||||
/**
|
||||
* logError
|
||||
* Writes an error log to CodeIgniter log
|
||||
*/
|
||||
public function logError($message)
|
||||
{
|
||||
$this->_log(LogLib::ERROR, $message);
|
||||
$this->_log(self::ERROR, $message);
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------
|
||||
// Public methods based on database
|
||||
|
||||
/**
|
||||
* Writes an info log to database
|
||||
*/
|
||||
public function logInfoDB($requestId, $data)
|
||||
{
|
||||
$this->_logDB(self::INFO, $requestId, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes a debug log to database
|
||||
*/
|
||||
public function logDebugDB($requestId, $data)
|
||||
{
|
||||
$this->_logDB(self::DEBUG, $requestId, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes an warning log to database
|
||||
*/
|
||||
public function logWarningDB($requestId, $data)
|
||||
{
|
||||
$this->_logDB(self::WARNING, $requestId, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes an error log to database
|
||||
*/
|
||||
public function logErrorDB($requestId, $data)
|
||||
{
|
||||
$this->_logDB(self::ERROR, $requestId, $data);
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------
|
||||
// Private methods
|
||||
|
||||
/**
|
||||
* log
|
||||
* Writes using CodeIgniter log system (file system)
|
||||
*/
|
||||
private function _log($level, $message)
|
||||
{
|
||||
log_message($level, $this->_getCaller().$message);
|
||||
log_message($level, $this->_getPrefix($this->_getCaller()).$message);
|
||||
}
|
||||
|
||||
/**
|
||||
* _getCaller
|
||||
* Writes logs to database
|
||||
*/
|
||||
private function _logDB($level, $requestId, $data)
|
||||
{
|
||||
// If the _dbLogType parameter was not given when this library was loaded
|
||||
// NOTE: this message will be displayed only to the developer AND stops the execution
|
||||
if ($this->_dbLogType == null)
|
||||
{
|
||||
show_error('To log to database you need to specify the "'.self::P_NAME_DB_LOG_TYPE.'" parameter when the LogLib is loaded');
|
||||
}
|
||||
|
||||
$ci =& get_instance(); // get code igniter instance
|
||||
|
||||
// If only debug log is enabed then is possible to write a debug log, otherwise...
|
||||
if ($level == self::DEBUG && $ci->config->item(self::CI_LOG_THRESHOLD_NAME) != self::CI_LOG_THRESHOLD_DEBUG)
|
||||
{
|
||||
// ...do nothing
|
||||
}
|
||||
else
|
||||
{
|
||||
// Loads WebservicelogModel
|
||||
$ci->load->model('system/Webservicelog_model', 'WebservicelogModel');
|
||||
|
||||
// Get caller data
|
||||
$callerData = $this->_getCaller();
|
||||
|
||||
// Writes a log to database
|
||||
$ci->WebservicelogModel->insert(array(
|
||||
'webservicetyp_kurzbz' => $this->_dbLogType,
|
||||
'request_id' => $requestId,
|
||||
'beschreibung' => $this->_getDatabaseDescription($callerData),
|
||||
'request_data' => $data,
|
||||
'execute_user' => $this->_dbExecuteUser,
|
||||
'execute_time' => 'NOW()' // current time
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves caller's data
|
||||
*/
|
||||
private function _getCaller()
|
||||
{
|
||||
$classIndex = 3;
|
||||
$functionIndex = 3;
|
||||
$lineIndex = 2;
|
||||
$class = '';
|
||||
$function = '';
|
||||
$line = '';
|
||||
$backtrace_arr = debug_backtrace();
|
||||
if (isset($backtrace_arr[$classIndex]['class']) && $backtrace_arr[$classIndex]['class'] != '')
|
||||
|
||||
if (isset($backtrace_arr[$this->_classIndex]['class']) && $backtrace_arr[$this->_classIndex]['class'] != '')
|
||||
{
|
||||
$class = $backtrace_arr[$classIndex]['class'];
|
||||
$class = $backtrace_arr[$this->_classIndex]['class'];
|
||||
}
|
||||
|
||||
if (isset($backtrace_arr[$functionIndex]['function']) && $backtrace_arr[$functionIndex]['function'] != '')
|
||||
if (isset($backtrace_arr[$this->_functionIndex]['function']) && $backtrace_arr[$this->_functionIndex]['function'] != '')
|
||||
{
|
||||
$function = $backtrace_arr[$functionIndex]['function'];
|
||||
$function = $backtrace_arr[$this->_functionIndex]['function'];
|
||||
}
|
||||
|
||||
if (isset($backtrace_arr[$lineIndex]['line']) && $backtrace_arr[$lineIndex]['line'] != '')
|
||||
if (isset($backtrace_arr[$this->_lineIndex]['line']) && $backtrace_arr[$this->_lineIndex]['line'] != '')
|
||||
{
|
||||
$line = $backtrace_arr[$lineIndex]['line'];
|
||||
$line = $backtrace_arr[$this->_lineIndex]['line'];
|
||||
}
|
||||
|
||||
return $this->_format($class, $function, $line);
|
||||
return array(
|
||||
self::CLASS_NAME => $class,
|
||||
self::FUNCTION_NAME => $function,
|
||||
self::CODE_LINE => $line
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* format
|
||||
* Formats the log message prefix (file system based)
|
||||
*/
|
||||
private function _format($class, $function, $line)
|
||||
private function _getPrefix($callerData)
|
||||
{
|
||||
$formatted = LogLib::CALLER_PREFIX;
|
||||
$formatted = self::CALLER_PREFIX;
|
||||
|
||||
if (!is_null($class) && $class != '')
|
||||
if (!isEmptyString($callerData[self::CLASS_NAME]))
|
||||
{
|
||||
$formatted .= $class.LogLib::CLASS_POSTFIX;
|
||||
$formatted .= $callerData[self::CLASS_NAME].self::CLASS_POSTFIX;
|
||||
}
|
||||
|
||||
$formatted .= $function.LogLib::LINE_SEPARATOR.$line.LogLib::CALLER_POSTFIX.' ';
|
||||
$formatted .= $callerData[self::FUNCTION_NAME].self::LINE_SEPARATOR.$callerData[self::CODE_LINE].self::CALLER_POSTFIX.' ';
|
||||
|
||||
return $formatted;
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats the database description for a log
|
||||
*/
|
||||
private function _getDatabaseDescription($callerData)
|
||||
{
|
||||
$formatted = $callerData[self::FUNCTION_NAME].self::LINE_SEPARATOR.$callerData[self::CODE_LINE];
|
||||
|
||||
if (!isEmptyString($callerData[self::CLASS_NAME]))
|
||||
{
|
||||
$formatted = $callerData[self::CLASS_NAME].self::CLASS_POSTFIX.$formatted;
|
||||
}
|
||||
|
||||
return $formatted;
|
||||
}
|
||||
|
||||
@@ -1,466 +0,0 @@
|
||||
<?php
|
||||
|
||||
if (! defined("BASEPATH")) exit("No direct script access allowed");
|
||||
|
||||
/**
|
||||
* Utility class to be used in the database migration process
|
||||
*/
|
||||
class MigrationLib extends CI_Migration
|
||||
{
|
||||
/**
|
||||
* Object initialization
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
// Loads EPrintfLib
|
||||
$this->load->library('EPrintfLib');
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a column exists in a table and schema
|
||||
*/
|
||||
private function columnExists($name, $schema, $table)
|
||||
{
|
||||
$query = sprintf("SELECT %s FROM %s.%s LIMIT 1", $name, $schema, $table);
|
||||
|
||||
if (@$this->db->simple_query($query))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Print an info about the starting of method up
|
||||
*/
|
||||
protected function startUP()
|
||||
{
|
||||
$this->eprintflib->printInfo(
|
||||
sprintf("%s Start method up of class %s %s", EPrintfLib::SEPARATOR, get_called_class(), EPrintfLib::SEPARATOR)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Print an info about the ending of method up
|
||||
*/
|
||||
protected function endUP()
|
||||
{
|
||||
$this->eprintflib->printInfo(
|
||||
sprintf("%s End method up of class %s %s", EPrintfLib::SEPARATOR, get_called_class(), EPrintfLib::SEPARATOR)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Print an info about the starting of method down
|
||||
*/
|
||||
protected function startDown()
|
||||
{
|
||||
$this->eprintflib->printInfo(
|
||||
sprintf("%s Start method down of class %s %s", EPrintfLib::SEPARATOR, get_called_class(), EPrintfLib::SEPARATOR)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Print an info about the ending of method down
|
||||
*/
|
||||
protected function endDown()
|
||||
{
|
||||
$this->eprintflib->printInfo(
|
||||
sprintf("%s End method down of class %s %s", EPrintfLib::SEPARATOR, get_called_class(), EPrintfLib::SEPARATOR)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a column, with attributes, to a table and schema
|
||||
*/
|
||||
protected function addColumn($schema, $table, $fields)
|
||||
{
|
||||
foreach ($fields as $name => $definition)
|
||||
{
|
||||
if (!$this->columnExists($name, $schema, $table))
|
||||
{
|
||||
if ($this->dbforge->add_column($schema.'.'.$table, array($name => $definition)))
|
||||
{
|
||||
$this->eprintflib->printMessage(sprintf("Column %s.%s.%s of type %s added", $schema, $table, $name, $definition["type"]));
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->eprintflib->printError(sprintf("Error while adding column %s.%s.%s of type %s", $schema, $table, $name, $definition["type"]));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->eprintflib->printInfo(sprintf("Column %s.%s.%s already exists", $schema, $table, $name));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Modifies a column, and its attributes, of a table and schema
|
||||
*/
|
||||
protected function modifyColumn($schema, $table, $fields)
|
||||
{
|
||||
foreach ($fields as $name => $definition)
|
||||
{
|
||||
if ($this->columnExists($name, $schema, $table))
|
||||
{
|
||||
if ($this->dbforge->modify_column($schema.'.'.$table, array($name => $definition)))
|
||||
{
|
||||
$this->eprintflib->printMessage(sprintf("Column %s.%s.%s has been modified", $schema, $table, $name));
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->eprintflib->printError(sprintf("Error while modifying column %s.%s.%s", $schema, $table, $name));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->eprintflib->printInfo(sprintf("Column %s.%s.%s doesn't exist", $schema, $table, $name));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Drops a column from a table and schema
|
||||
*/
|
||||
protected function dropColumn($schema, $table, $field)
|
||||
{
|
||||
if ($this->columnExists($field, $schema, $table))
|
||||
{
|
||||
if ($this->dbforge->drop_column($schema.'.'.$table, $field))
|
||||
{
|
||||
$this->eprintflib->printMessage(sprintf("Column %s.%s.%s has been dropped", $schema, $table, $field));
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->eprintflib->printError(sprintf("Error while dropping column %s.%s.%s", $schema, $table, $field));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->eprintflib->printInfo(sprintf("Column %s.%s.%s doesn't exist", $schema, $table, $field));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a column as primary key of a table and schema
|
||||
*/
|
||||
protected function addPrimaryKey($schema, $table, $name, $fields)
|
||||
{
|
||||
$stringFields = null;
|
||||
|
||||
if (is_array($fields))
|
||||
{
|
||||
if (count($fields) > 0)
|
||||
{
|
||||
$stringFields = "";
|
||||
for ($i = 0; $i < count($fields); $i++)
|
||||
{
|
||||
$stringFields .= $fields[$i];
|
||||
if ($i != count($fields) - 1)
|
||||
{
|
||||
$stringFields .= ", ";
|
||||
}
|
||||
}
|
||||
$query = sprintf("ALTER TABLE %s.%s ADD CONSTRAINT %s PRIMARY KEY (%s)", $schema, $table, $name, $stringFields);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$query = sprintf("ALTER TABLE %s.%s ADD CONSTRAINT %s PRIMARY KEY (%s)", $schema, $table, $name, $fields);
|
||||
}
|
||||
|
||||
if (@$this->db->simple_query($query))
|
||||
{
|
||||
$this->eprintflib->printMessage(sprintf("Added primary key %s on table %s.%s", $name, $schema, $table));
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->eprintflib->printError(sprintf("Adding primary key %s on table %s.%s", $name, $schema, $table));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a column as foreign key of a table and schema
|
||||
*/
|
||||
protected function addForeingKey($schema, $table, $name, $field, $schemaDest, $tableDest, $fieldDest, $attributes)
|
||||
{
|
||||
$query = sprintf(
|
||||
"ALTER TABLE %s.%s ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s.%s (%s) %s",
|
||||
$schema,
|
||||
$table,
|
||||
$name,
|
||||
$field,
|
||||
$schemaDest,
|
||||
$tableDest,
|
||||
$fieldDest,
|
||||
$attributes
|
||||
);
|
||||
|
||||
if (@$this->db->simple_query($query))
|
||||
{
|
||||
$this->eprintflib->printMessage(sprintf("Added foreign key %s on table %s.%s", $name, $schema, $table));
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->eprintflib->printError(sprintf("Adding foreign key %s on table %s.%s", $name, $schema, $table));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a column as unique key of a table and schema
|
||||
*/
|
||||
protected function addUniqueKey($schema, $table, $name, $fields)
|
||||
{
|
||||
$stringFields = null;
|
||||
|
||||
if (is_array($fields))
|
||||
{
|
||||
if (count($fields) > 0)
|
||||
{
|
||||
$stringFields = "";
|
||||
for ($i = 0; $i < count($fields); $i++)
|
||||
{
|
||||
$stringFields .= $fields[$i];
|
||||
if ($i != count($fields) - 1)
|
||||
{
|
||||
$stringFields .= ", ";
|
||||
}
|
||||
}
|
||||
$query = sprintf("CREATE UNIQUE INDEX %s ON %s.%s (%s)", $name, $schema, $table, $stringFields);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$query = sprintf("CREATE UNIQUE INDEX %s ON %s.%s (%s)", $name, $schema, $table, $fields);
|
||||
}
|
||||
|
||||
if (@$this->db->simple_query($query))
|
||||
{
|
||||
$this->eprintflib->printMessage(sprintf("Added unique key %s on table %s.%s", $name, $schema, $table));
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->eprintflib->printError(sprintf("Adding unique key %s on table %s.%s", $name, $schema, $table));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Grants permissions to a user on a table and schema
|
||||
*/
|
||||
protected function grantTable($permissions, $schema, $table, $user)
|
||||
{
|
||||
$stringPermission = null;
|
||||
|
||||
if (is_array($permissions))
|
||||
{
|
||||
if (count($permissions) > 0)
|
||||
{
|
||||
$stringPermission = "";
|
||||
for ($i = 0; $i < count($permissions); $i++)
|
||||
{
|
||||
$stringPermission .= $permissions[$i];
|
||||
if ($i != count($permissions) - 1)
|
||||
{
|
||||
$stringPermission .= ", ";
|
||||
}
|
||||
}
|
||||
$query = sprintf("GRANT %s ON TABLE %s.%s TO %s", $stringPermission, $schema, $table, $user);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$query = sprintf("GRANT %s ON TABLE %s.%s TO %s", $permissions, $schema, $table, $user);
|
||||
}
|
||||
|
||||
if (@$this->db->simple_query($query))
|
||||
{
|
||||
$this->eprintflib->printMessage(
|
||||
sprintf(
|
||||
"Granted permissions %s on table %s.%s to user %s",
|
||||
is_null($stringPermission) ? $permissions : $stringPermission,
|
||||
$schema,
|
||||
$table,
|
||||
$user
|
||||
)
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->eprintflib->printError(
|
||||
sprintf(
|
||||
"Granting permissions %s on table %s.%s to user %s",
|
||||
is_null($stringPermission) ? $permissions : $stringPermission,
|
||||
$schema,
|
||||
$table,
|
||||
$user
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a table in a schema with columns
|
||||
*/
|
||||
protected function createTable($schema, $table, $fields)
|
||||
{
|
||||
$this->dbforge->add_field($fields);
|
||||
|
||||
if ($this->dbforge->create_table($schema.'.'.$table, true))
|
||||
{
|
||||
$this->eprintflib->printMessage(sprintf("Table %s.%s created or existing", $schema, $table));
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->eprintflib->printError(sprintf("Creating table %s.%s", $schema, $table));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Drops a table from a schema
|
||||
*/
|
||||
protected function dropTable($schema, $table)
|
||||
{
|
||||
if ($this->dbforge->drop_table($schema.".".$table))
|
||||
{
|
||||
$this->eprintflib->printMessage(sprintf("Table %s.%s has been dropped", $schema, $table));
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->eprintflib->printError(sprintf("Dropping table %s.%s", $schema, $table));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes a sequence with the max value of a column
|
||||
*/
|
||||
protected function initializeSequence($schemaSrc, $sequence, $schemaDst, $table, $field)
|
||||
{
|
||||
$query = sprintf("SELECT SETVAL('%s.%s', (SELECT MAX(%s) FROM %s.%s))", $schemaSrc, $sequence, $field, $schemaDst, $table);
|
||||
|
||||
if (@$this->db->simple_query($query))
|
||||
{
|
||||
$this->eprintflib->printMessage(sprintf("Sequence %s.%s has been initialized", $schemaSrc, $sequence));
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->eprintflib->printError(sprintf("Initializing sequence %s.%s", $schemaSrc, $sequence));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add comment to a column
|
||||
*/
|
||||
protected function addCommentToColumn($schema, $table, $field, $comment)
|
||||
{
|
||||
$query = sprintf("COMMENT ON COLUMN %s.%s.%s IS ?", $schema, $table, $field);
|
||||
|
||||
if (@$this->db->query($query, array($comment)))
|
||||
{
|
||||
$this->eprintflib->printMessage(sprintf("Comment added to %s.%s.%s", $schema, $table, $field));
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->eprintflib->printError(sprintf("Error while adding comment to %s.%s.%s", $schema, $table, $field));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add comment to a table
|
||||
*/
|
||||
protected function addCommentToTable($schema, $table, $comment)
|
||||
{
|
||||
$query = sprintf("COMMENT ON TABLE %s.%s IS ?", $schema, $table, $field);
|
||||
|
||||
if (@$this->db->query($query, array($comment)))
|
||||
{
|
||||
$this->eprintflib->printMessage(sprintf("Comment added to %s.%s", $schema, $table));
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->eprintflib->printError(sprintf("Error while adding comment to %s.%s", $schema, $table));
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Grants permissions to a user on a sequence
|
||||
*/
|
||||
protected function grantSequence($permissions, $schema, $sequence, $user)
|
||||
{
|
||||
$stringPermission = null;
|
||||
|
||||
if (is_array($permissions))
|
||||
{
|
||||
if (count($permissions) > 0)
|
||||
{
|
||||
$stringPermission = "";
|
||||
for ($i = 0; $i < count($permissions); $i++)
|
||||
{
|
||||
$stringPermission .= $permissions[$i];
|
||||
if ($i != count($permissions) - 1)
|
||||
{
|
||||
$stringPermission .= ", ";
|
||||
}
|
||||
}
|
||||
$query = sprintf("GRANT %s ON SEQUENCE %s.%s TO %s", $stringPermission, $schema, $sequence, $user);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$query = sprintf("GRANT %s ON SEQUENCE %s.%s TO %s", $permissions, $schema, $sequence, $user);
|
||||
}
|
||||
|
||||
if (@$this->db->simple_query($query))
|
||||
{
|
||||
$this->eprintflib->printMessage(
|
||||
sprintf(
|
||||
"Granted permissions %s on sequence %s.%s to user %s",
|
||||
is_null($stringPermission) ? $permissions : $stringPermission,
|
||||
$schema,
|
||||
$sequence,
|
||||
$user
|
||||
)
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->eprintflib->printError(
|
||||
sprintf(
|
||||
"Granting permissions %s on sequence %s.%s to user %s",
|
||||
is_null($stringPermission) ? $permissions : $stringPermission,
|
||||
$schema,
|
||||
$sequence,
|
||||
$user
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes the given query
|
||||
*/
|
||||
protected function execQuery($query)
|
||||
{
|
||||
if (! @$this->db->simple_query($query))
|
||||
{
|
||||
$error = $this->db->error();
|
||||
|
||||
if (is_array($error) && isset($error["message"]))
|
||||
{
|
||||
$this->eprintflib->printError($error["message"]);
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->eprintflib->printError("Error while executing a query");
|
||||
}
|
||||
}
|
||||
|
||||
$this->eprintflib->printInfo(
|
||||
"Query correctly executed: ".
|
||||
substr(preg_replace("/\s+/", " ", trim($query)), 0, EPrintfLib::PRINT_QUERY_LEN).
|
||||
(strlen($query) > EPrintfLib::PRINT_QUERY_LEN ? "..." : "")
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ if (! defined('BASEPATH')) exit('No direct script access allowed');
|
||||
class PersonLogLib
|
||||
{
|
||||
const PARKED_LOGNAME = 'Parked';
|
||||
const ONHOLD_LOGNAME = 'Onhold';
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
@@ -91,26 +92,20 @@ class PersonLogLib
|
||||
*/
|
||||
public function park($person_id, $date, $taetigkeit_kurzbz, $app = 'core', $oe_kurzbz = null, $user = null)
|
||||
{
|
||||
$logdata = array(
|
||||
$onhold = $this->getOnHoldDate($person_id);
|
||||
|
||||
if (hasData($onhold))
|
||||
return error("Person already on hold");
|
||||
|
||||
$logjson = array(
|
||||
'name' => self::PARKED_LOGNAME
|
||||
);
|
||||
|
||||
$data = array(
|
||||
'person_id' => $person_id,
|
||||
'zeitpunkt' => $date,
|
||||
'taetigkeit_kurzbz' => $taetigkeit_kurzbz,
|
||||
'app' => $app,
|
||||
'oe_kurzbz' => $oe_kurzbz,
|
||||
'logtype_kurzbz' => 'Processstate',
|
||||
'logdata' => json_encode($logdata),
|
||||
'insertvon' => $user
|
||||
);
|
||||
|
||||
return $this->ci->PersonLogModel->insert($data);
|
||||
return $this->_savePsLog($person_id, $date, $taetigkeit_kurzbz, $logjson, $app, $oe_kurzbz, $user);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unparks a person, i.e. removes all log entries in the future
|
||||
* Unparks a person, i.e. removes all log entries in the future with logname for parking
|
||||
* @param $person_id
|
||||
* @return array with deleted logids
|
||||
*/
|
||||
@@ -131,17 +126,9 @@ class PersonLogLib
|
||||
{
|
||||
$deleted[] = $log->log_id;
|
||||
}
|
||||
else
|
||||
{
|
||||
return $delresult;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return $result;
|
||||
}
|
||||
|
||||
return success($deleted);
|
||||
}
|
||||
@@ -172,4 +159,111 @@ class PersonLogLib
|
||||
|
||||
return $parkeddate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets person on hold, i.e. marks a person so no actions are expected for the person (e.g. as a prestudent).
|
||||
* Done by adding a logentry with a special name. can be undone only manually by clicking button.
|
||||
* @param $person_id
|
||||
* @param $date
|
||||
* @param $taetigkeit_kurzbz
|
||||
* @param string $app
|
||||
* @param null $oe_kurzbz
|
||||
* @param null $user
|
||||
* @return array
|
||||
*/
|
||||
public function setOnHold($person_id, $date, $taetigkeit_kurzbz, $app = 'core', $oe_kurzbz = null, $user = null)
|
||||
{
|
||||
$parked = $this->getParkedDate($person_id);
|
||||
|
||||
if (hasData($parked))
|
||||
return error("Person already parked");
|
||||
|
||||
$logjson = array(
|
||||
'name' => self::ONHOLD_LOGNAME
|
||||
);
|
||||
|
||||
return $this->_savePsLog($person_id, $date, $taetigkeit_kurzbz, $logjson, $app, $oe_kurzbz, $user);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes on hold status, i.e. removes all log entries with logname for on hold
|
||||
* @param $person_id
|
||||
* @return array
|
||||
*/
|
||||
public function removeOnHold($person_id)
|
||||
{
|
||||
$deleted = array();
|
||||
|
||||
$result = $this->ci->PersonLogModel->filterLog($person_id);
|
||||
if (hasData($result))
|
||||
{
|
||||
foreach ($result->retval as $log)
|
||||
{
|
||||
$logdata = json_decode($log->logdata);
|
||||
if (isset($logdata->name) && $logdata->name === self::ONHOLD_LOGNAME)
|
||||
{
|
||||
$delresult = $this->ci->PersonLogModel->deleteLog($log->log_id);
|
||||
if (isSuccess($delresult))
|
||||
{
|
||||
$deleted[] = $log->log_id;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return success($deleted);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets date until which a person is on hold
|
||||
* @param $person_id
|
||||
* @return the date if person is on hold, null otherwise
|
||||
*/
|
||||
public function getOnHoldDate($person_id)
|
||||
{
|
||||
$result = $this->ci->PersonLogModel->filterLog($person_id);
|
||||
|
||||
$onholddate = null;
|
||||
|
||||
if (hasData($result))
|
||||
{
|
||||
foreach ($result->retval as $log)
|
||||
{
|
||||
$logdata = json_decode($log->logdata);
|
||||
if (isset($logdata->name) && $logdata->name === self::ONHOLD_LOGNAME)
|
||||
{
|
||||
$onholddate = $log->zeitpunkt;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $onholddate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves a processstate log with specified parameters, including a specified log date.
|
||||
* @param $person_id
|
||||
* @param $date
|
||||
* @param $taetigkeit_kurzbz
|
||||
* @param $logjson
|
||||
* @param string $app
|
||||
* @param null $oe_kurzbz
|
||||
* @param null $user
|
||||
* @return mixed
|
||||
*/
|
||||
private function _savePsLog($person_id, $date, $taetigkeit_kurzbz, $logjson, $app = 'core', $oe_kurzbz = null, $user = null)
|
||||
{
|
||||
$data = array(
|
||||
'person_id' => $person_id,
|
||||
'zeitpunkt' => $date,
|
||||
'taetigkeit_kurzbz' => $taetigkeit_kurzbz,
|
||||
'app' => $app,
|
||||
'oe_kurzbz' => $oe_kurzbz,
|
||||
'logtype_kurzbz' => 'Processstate',
|
||||
'logdata' => json_encode($logjson),
|
||||
'insertvon' => $user
|
||||
);
|
||||
|
||||
return $this->ci->PersonLogModel->insert($data);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,206 @@
|
||||
<?php
|
||||
|
||||
if (! defined('BASEPATH')) exit('No direct script access allowed');
|
||||
|
||||
/**
|
||||
* TableWidget logic
|
||||
*/
|
||||
class TableWidgetLib
|
||||
{
|
||||
const TABLE_UNIQUE_ID = 'tableUniqueId'; // TableWidget unique id
|
||||
|
||||
// Session parameters names
|
||||
const SESSION_NAME = 'FHC_TABLE_WIDGET'; // Table session name
|
||||
const SESSION_FIELDS = 'fields';
|
||||
const SESSION_COLUMNS_ALIASES = 'columnsAliases';
|
||||
const SESSION_ADDITIONAL_COLUMNS = 'additionalColumns';
|
||||
const SESSION_CHECKBOXES = 'checkboxes';
|
||||
const SESSION_METADATA = 'datasetMetadata';
|
||||
const SESSION_DATASET = 'dataset';
|
||||
const SESSION_ROW_NUMBER = 'rowNumber';
|
||||
const SESSION_RELOAD_DATASET = 'reloadDataset';
|
||||
const SESSION_DATASET_REPRESENTATION = 'datasetRepresentation';
|
||||
const SESSION_DATASET_REP_OPTIONS = 'datasetRepresentationOptions';
|
||||
const SESSION_DATASET_REP_FIELDS_DEFS = 'datasetRepresentationFieldsDefinitions';
|
||||
|
||||
// Alias for the dynamic table used to retrieve the dataset
|
||||
const DATASET_TABLE_ALIAS = 'datasetTableWidget';
|
||||
|
||||
// Parameters names...
|
||||
|
||||
// ...to reload the dataset
|
||||
const DATASET_RELOAD_PARAMETER = 'reloadDataset';
|
||||
|
||||
// ...to specify permissions that are needed to use this TableWidget
|
||||
const REQUIRED_PERMISSIONS_PARAMETER = 'requiredPermissions';
|
||||
|
||||
// ...stament to retrieve the dataset
|
||||
const QUERY_PARAMETER = 'query';
|
||||
|
||||
// ...to specify more columns or aliases for them
|
||||
const ADDITIONAL_COLUMNS = 'additionalColumns';
|
||||
const CHECKBOXES = 'checkboxes';
|
||||
const COLUMNS_ALIASES = 'columnsAliases';
|
||||
|
||||
// ...to format/mark records of a dataset
|
||||
const FORMAT_ROW = 'formatRow';
|
||||
const MARK_ROW = 'markRow';
|
||||
|
||||
// ...to specify how to represent the dataset (ex: tablesorter, pivotUI, ...)
|
||||
const DATASET_REPRESENTATION = 'datasetRepresentation';
|
||||
const DATASET_REP_OPTIONS = 'datasetRepOptions';
|
||||
const DATASET_REP_FIELDS_DEFS = 'datasetRepFieldsDefs';
|
||||
|
||||
// Different dataset representations
|
||||
const DATASET_REP_TABLESORTER = 'tablesorter';
|
||||
const DATASET_REP_PIVOTUI = 'pivotUI';
|
||||
const DATASET_REP_TABULATOR = 'tabulator';
|
||||
|
||||
const PERMISSION_TABLE_METHOD = 'TableWidget'; // Name for fake method to be checked by the PermissionLib
|
||||
const PERMISSION_TYPE = 'rw';
|
||||
|
||||
private $_ci; // Code igniter instance
|
||||
private $_tableUniqueId; // unique id for this table widget
|
||||
|
||||
/**
|
||||
* Gets the CI instance and loads message helper
|
||||
*/
|
||||
public function __construct($params = null)
|
||||
{
|
||||
$this->_ci =& get_instance(); // get code igniter instance
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------------------------------------------
|
||||
// Public methods
|
||||
|
||||
/**
|
||||
* Checks if at least one of the permissions given as parameter (requiredPermissions) belongs
|
||||
* to the authenticated user, if confirmed then is allowed to use this FilterWidget.
|
||||
* If the parameter requiredPermissions is NOT given or is not present in the session,
|
||||
* then NO one is allow to use this FilterWidget
|
||||
* Wrapper method to permissionlib->hasAtLeastOne
|
||||
*/
|
||||
public function isAllowed($requiredPermissions = null)
|
||||
{
|
||||
$this->_ci->load->library('PermissionLib'); // Load permission library
|
||||
|
||||
// Gets the required permissions from the session if they are not provided as parameter
|
||||
$rq = $requiredPermissions;
|
||||
if ($rq == null) $rq = $this->getSessionElement(self::REQUIRED_PERMISSIONS_PARAMETER);
|
||||
|
||||
return $this->_ci->permissionlib->hasAtLeastOne($rq, self::PERMISSION_TABLE_METHOD, self::PERMISSION_TYPE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper method to the session helper funtions to retrieve the whole session for this filter
|
||||
*/
|
||||
public function getSession()
|
||||
{
|
||||
return getSessionElement(self::SESSION_NAME, $this->_tableUniqueId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper method to the session helper funtions to retrieve one element from the session of this filter
|
||||
*/
|
||||
public function getSessionElement($name)
|
||||
{
|
||||
$session = getSessionElement(self::SESSION_NAME, $this->_tableUniqueId);
|
||||
|
||||
if (isset($session[$name]))
|
||||
{
|
||||
return $session[$name];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper method to the session helper funtions to set the whole session for this filter
|
||||
*/
|
||||
public function setSession($data)
|
||||
{
|
||||
setSessionElement(self::SESSION_NAME, $this->_tableUniqueId, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper method to the session helper funtions to set one element in the session for this filter
|
||||
*/
|
||||
public function setSessionElement($name, $value)
|
||||
{
|
||||
$session = getSessionElement(self::SESSION_NAME, $this->_tableUniqueId);
|
||||
|
||||
$session[$name] = $value;
|
||||
|
||||
setSessionElement(self::SESSION_NAME, $this->_tableUniqueId, $session); // stores the single value
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate the query to retrieve the dataset for a filter
|
||||
*/
|
||||
public function generateDatasetQuery($query)
|
||||
{
|
||||
return 'SELECT * FROM ('.$query.') '.self::DATASET_TABLE_ALIAS;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the dataset from the DB
|
||||
*/
|
||||
public function getDataset($datasetQuery)
|
||||
{
|
||||
$dataset = null;
|
||||
|
||||
if ($datasetQuery != null)
|
||||
{
|
||||
$this->_ci->load->model('system/Filters_model', 'FiltersModel');
|
||||
|
||||
// Execute the given SQL statement suppressing error messages
|
||||
$dataset = @$this->_ci->FiltersModel->execReadOnlyQuery($datasetQuery);
|
||||
}
|
||||
|
||||
return $dataset;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves metadata from the last executed query
|
||||
*/
|
||||
public function getExecutedQueryMetaData()
|
||||
{
|
||||
return $this->_ci->FiltersModel->getExecutedQueryMetaData();
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the list of fields from the last executed query
|
||||
*/
|
||||
public function getExecutedQueryListFields()
|
||||
{
|
||||
return $this->_ci->FiltersModel->getExecutedQueryListFields();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an unique string that identify this filter widget
|
||||
* NOTE: The default value is the URI where the FilterWidget is called
|
||||
* If the fhc_controller_id is present then is also used
|
||||
*/
|
||||
public function setTableUniqueIdByParams($params)
|
||||
{
|
||||
if ($params != null
|
||||
&& is_array($params)
|
||||
&& isset($params[self::TABLE_UNIQUE_ID])
|
||||
&& !isEmptyString($params[self::TABLE_UNIQUE_ID]))
|
||||
{
|
||||
$tableUniqueId = $this->_ci->router->directory.$this->_ci->router->class.'/'.
|
||||
$this->_ci->router->method.'/'.
|
||||
$params[self::TABLE_UNIQUE_ID];
|
||||
|
||||
$this->setTableUniqueId($tableUniqueId);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the _tableUniqueId property
|
||||
*/
|
||||
public function setTableUniqueId($tableUniqueId)
|
||||
{
|
||||
$this->_tableUniqueId = $tableUniqueId;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
<?php
|
||||
|
||||
if (! defined('BASEPATH')) exit('No direct script access allowed');
|
||||
|
||||
/**
|
||||
* Class VariableLib
|
||||
* Provides functionality for managing uservariables for currently logged in user.
|
||||
* Preloads variables for a user, so variables can be retrieved from views without getting uid again.
|
||||
*/
|
||||
class VariableLib
|
||||
{
|
||||
private $_variables; // Contains the retrieved variables
|
||||
|
||||
/**
|
||||
* VariableLib constructor.
|
||||
* Loads variable of logged in user.
|
||||
* @param $loggeduid
|
||||
*/
|
||||
public function __construct($loggeduid)
|
||||
{
|
||||
$this->_ci =& get_instance();
|
||||
|
||||
$this->_variables = null;
|
||||
|
||||
$this->_ci->load->model('system/Variable_model', 'VariableModel');
|
||||
$this->_ci->load->model('organisation/studiensemester_model', 'StudiensemesterModel');
|
||||
|
||||
if (isset($loggeduid['uid']) && !isEmptyString($loggeduid['uid']))
|
||||
$this->_setVariables($loggeduid['uid']);
|
||||
else
|
||||
{
|
||||
show_error('uid of logged user not passed!');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an already loaded user variable by variable name.
|
||||
* @param $name
|
||||
* @return mixed|null
|
||||
*/
|
||||
public function getVar($name)
|
||||
{
|
||||
return isset($this->_variables[$name]) ? $this->_variables[$name] : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes variables having Studiensemester as value. Sets variable value to next or previous Semester.
|
||||
* @param $uid variable is set for this user
|
||||
* @param $name variable name
|
||||
* @param $change if positive, variable value is set to next semester, negative - previous semester
|
||||
* @return array if change was successfull, uid and variable name. Infotext otherwise.
|
||||
*/
|
||||
public function changeStudiensemesterVar($uid, $name, $change)
|
||||
{
|
||||
$result = error('error when setting variable!');
|
||||
$notchangedtext = "Studiensemester variable not changed.";
|
||||
|
||||
if (!isEmptyString($uid) && !isEmptyString($name) && is_numeric($change))
|
||||
{
|
||||
$change = (int) $change;
|
||||
$varres = $this->_ci->VariableModel->getVariables($uid, array($name));
|
||||
|
||||
if (isSuccess($varres))
|
||||
{
|
||||
if (hasData($varres))
|
||||
{
|
||||
$currStudiensemester = getData($varres);
|
||||
|
||||
if ($change === 0)
|
||||
{
|
||||
$result = success($notchangedtext);
|
||||
}
|
||||
else
|
||||
{
|
||||
if ($change > 0)
|
||||
{
|
||||
$changedsem = $this->_ci->StudiensemesterModel->getNextFrom($currStudiensemester[$name]);
|
||||
}
|
||||
elseif ($change < 0)
|
||||
{
|
||||
$changedsem = $this->_ci->StudiensemesterModel->getPreviousFrom($currStudiensemester[$name]);
|
||||
}
|
||||
|
||||
if (hasData($changedsem))
|
||||
{
|
||||
$changedsem = getData($changedsem);
|
||||
|
||||
$result = $this->_ci->VariableModel->setVariable($uid, $name, $changedsem[0]->studiensemester_kurzbz);
|
||||
//update property
|
||||
$this->_setVariable($uid, $name);
|
||||
}
|
||||
else
|
||||
{
|
||||
$result = success($notchangedtext);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* "Refreshes" variable value with given name by retrieving current value from db and saving it.
|
||||
* @param $uid
|
||||
* @param $name
|
||||
*/
|
||||
private function _setVariable($uid, $name)
|
||||
{
|
||||
$variable = $this->_ci->VariableModel->getVariables($uid, array($name));
|
||||
|
||||
if (hasData($variable))
|
||||
{
|
||||
$variable = getData($variable);
|
||||
$this->_variables[$name] = $variable[$name];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* "Refreshes" all variable values by retrieving current values from db and saving them.
|
||||
* @param $uid
|
||||
*/
|
||||
private function _setVariables($uid)
|
||||
{
|
||||
$variables = $this->_ci->VariableModel->getVariables($uid);
|
||||
if (hasData($variables))
|
||||
{
|
||||
$this->_variables = getData($variables);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user