Added comments and cleaned code

This commit is contained in:
Paolo
2019-12-17 16:57:02 +01:00
parent 719f2d7314
commit e509f7acd1
7 changed files with 70 additions and 61 deletions
+2 -2
View File
@@ -4,7 +4,7 @@ if (! defined('BASEPATH')) exit('No direct script access allowed');
/**
* This controller operates between (interface) the JS (GUI) and the FilterWidgetLib (back-end)
* Provides data to the ajax get calls about the filter
* Provides data to the ajax get calls about the filter widget
* Accepts ajax post calls to change the filter data
* This controller works with JSON calls on the HTTP GET or POST and the output is always JSON
* NOTE: extends the FHC_Controller instead of the Auth_Controller because the FilterWidget has its
@@ -12,7 +12,7 @@ if (! defined('BASEPATH')) exit('No direct script access allowed');
*/
class Filters extends FHC_Controller
{
const FILTER_UNIQUE_ID = 'filterUniqueId';
const FILTER_UNIQUE_ID = 'filterUniqueId'; // Name of the filter widget unique id
/**
* Calls the parent's constructor and loads the FilterWidgetLib
+2 -2
View File
@@ -4,7 +4,7 @@ if (! defined('BASEPATH')) exit('No direct script access allowed');
/**
* This controller operates between (interface) the JS (GUI) and the tablewidgetlib (back-end)
* Provides data to the ajax get calls about the filter
* Provides data to the ajax get calls about the table widget
* Accepts ajax post calls to change the filter data
* This controller works with JSON calls on the HTTP GET or POST and the output is always JSON
* NOTE: extends the FHC_Controller instead of the Auth_Controller because the TableWidget has its
@@ -12,7 +12,7 @@ if (! defined('BASEPATH')) exit('No direct script access allowed');
*/
class Tables extends FHC_Controller
{
const TABLE_UNIQUE_ID = 'tableUniqueId';
const TABLE_UNIQUE_ID = 'tableUniqueId'; // Name of the table widget unique id
/**
* Calls the parent's constructor and loads the tablewidgetlib
+13 -8
View File
@@ -3,14 +3,19 @@
if (! defined('BASEPATH')) exit('No direct script access allowed');
/**
* This controller...
* This controller operates between (interface) the JS (GUI) and the UDFLib (back-end)
* Provides data to the ajax get calls about the UDF widget
* Accepts ajax post calls to save UDFs
* This controller works with JSON calls on the HTTP GET or POST and the output is always JSON
* NOTE: extends the FHC_Controller instead of the Auth_Controller because the UDFWidget has its
* own permissions check
*/
class UDF extends FHC_Controller
{
const UDF_UNIQUE_ID = 'udfUniqueId';
const UDF_UNIQUE_ID = 'udfUniqueId'; // Name of the udf widget unique id
/**
* Calls the parent's constructor and loads the
* Calls the parent's constructor and loads the UDFLib
*/
public function __construct()
{
@@ -22,7 +27,7 @@ class UDF extends FHC_Controller
// Loads the UDFLib with HTTP GET/POST parameters
$this->_loadUDFLib();
// Checks if the caller is allow to read this data
// Checks if the caller is allow to use this UDF widget
$this->_isAllowed();
}
@@ -30,7 +35,7 @@ class UDF extends FHC_Controller
// Public methods
/**
* Retrieves data about the current filter from the session and will be written on the output in JSON format
* Save data about the current UDFs and the result will be written on the output in JSON format
*/
public function saveUDFs()
{
@@ -59,7 +64,7 @@ class UDF extends FHC_Controller
// Private methods
/**
* Checks if the user is allowed to use this filter
* Checks if the user is allowed to use this UDFWidget
*/
private function _isAllowed()
{
@@ -70,7 +75,7 @@ class UDF extends FHC_Controller
}
/**
* Loads the tablewidgetlib with the UDF_UNIQUE_ID parameter
* Loads the UDFLib with the UDF_UNIQUE_ID parameter
* If the parameter UDF_UNIQUE_ID is not given then the execution of the controller is terminated and
* an error message is printed
*/
@@ -89,7 +94,7 @@ class UDF extends FHC_Controller
$udfUniqueId = $this->input->post(self::UDF_UNIQUE_ID); // is retrieved from the HTTP POST
}
// Loads the tablewidgetlib that contains all the used logic
// Loads the UDFLib that contains all the used logic
$this->load->library('UDFLib');
$this->udflib->setUDFUniqueId($udfUniqueId);
+1 -1
View File
@@ -61,7 +61,7 @@ class DB_Model extends CI_Model
// Public methods
/**
* This method provides a way to setup a database model without declaring one
* This method provides a way to setup a database model without declaring one that extends this class
*/
public function setup($schema, $table, $primaryKey, $hasSequence = true)
{
+45 -41
View File
@@ -3,19 +3,20 @@
if (! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Library to manage UDF
* Library to manage UDFs
*/
class UDFLib
{
const UDF_UNIQUE_ID = 'udfUniqueId';
const UDF_UNIQUE_ID = 'udfUniqueId'; // Name of the UDF widget unique id
const SESSION_NAME = 'FHC_UDF_WIDGET';
const SESSION_NAME = 'FHC_UDF_WIDGET'; // Name of the session area used for UDFs
const WIDGET_NAME = 'UDFWidget';
const SCHEMA_ARG_NAME = 'schema';
const TABLE_ARG_NAME = 'table';
const FIELD_ARG_NAME = 'field';
const UDFS_ARG_NAME = 'udfs';
// Parameters names
const WIDGET_NAME = 'UDFWidget'; // UDFWidget name
const SCHEMA_ARG_NAME = 'schema'; // Schema parameter name
const TABLE_ARG_NAME = 'table'; // Table parameter name
const FIELD_ARG_NAME = 'field'; // Field parameter name
const UDFS_ARG_NAME = 'udfs'; // UDFs parameter name
// UDF json schema attributes
const NAME = 'name'; // UDF name attribute
@@ -29,7 +30,7 @@ class UDFLib
// ...to specify permissions that are needed to use this TableWidget
const REQUIRED_PERMISSIONS_PARAMETER = 'requiredPermissions';
// ...
// ...to specify the primary key name and value
const PRIMARY_KEY_NAME = 'primaryKeyName';
const PRIMARY_KEY_VALUE = 'primaryKeyValue';
@@ -61,10 +62,10 @@ class UDFLib
private $_ci; // Code igniter instance
private $_udfUniqueId; //
private $_udfUniqueId; // Property that contains the UDF widget unique id
/**
* Loads fhc helper
* Gets CI instance
*/
public function __construct()
{
@@ -400,7 +401,7 @@ class UDFLib
}
/**
* Return an unique string that identify this filter widget
* Return an unique string that identify this UDF widget
* NOTE: The default value is the URI where the FilterWidget is called
* If the fhc_controller_id is present then is also used
*/
@@ -420,7 +421,7 @@ class UDFLib
}
/**
* Wrapper method to the session helper funtions to retrieve the whole session for this filter
* Wrapper method to the session helper funtions to retrieve the whole session for this UDF widget
*/
public function getSession()
{
@@ -428,7 +429,7 @@ class UDFLib
}
/**
* Wrapper method to the session helper funtions to retrieve one element from the session of this filter
* Wrapper method to the session helper funtions to retrieve one element from the session of this UDF widget
*/
public function getSessionElement($name)
{
@@ -443,7 +444,7 @@ class UDFLib
}
/**
* Wrapper method to the session helper funtions to set the whole session for this filter
* Wrapper method to the session helper funtions to set the whole session for this UDF widget
*/
public function setSession($data)
{
@@ -451,7 +452,7 @@ class UDFLib
}
/**
* Wrapper method to the session helper funtions to set one element in the session for this filter
* Wrapper method to the session helper funtions to set one element in the session for this UDF widget
*/
public function setSessionElement($name, $value)
{
@@ -470,31 +471,52 @@ class UDFLib
// Read the all session for this udf widget
$session = $this->getSession();
// If session is empty then return an error
if ($session == null) return error('No UDFWidget loaded');
// Workaround
// Workaround to load CI
$this->_ci->load->model('system/UDF_model', 'UDFModel');
//
// Initialize a new DB_Model
$dbModel = new DB_Model();
// Setup the new dbModel object with...
$dbModel->setup(
$session[self::SCHEMA_ARG_NAME], //
$session[self::TABLE_ARG_NAME], //
$session[self::PRIMARY_KEY_NAME] //
$session[self::SCHEMA_ARG_NAME], // ... schema...
$session[self::TABLE_ARG_NAME], // ...table...
$session[self::PRIMARY_KEY_NAME] // ...and primary key name
);
// Returns the result of the database update operation to save UDFs
return $dbModel->update(
array($session[self::PRIMARY_KEY_NAME] => $session[self::PRIMARY_KEY_VALUE]),
(array)$udfs
);
}
/**
* 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 UDFWidget.
* If the parameter requiredPermissions is NOT given or is not present in the session,
* then NO one is allow to use this UDFWidget
* 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);
}
// -------------------------------------------------------------------------------------------------
// Private methods
/**
*
* Print the block for UDFs
*/
private function _printStartUDFBlock($widgetData)
{
@@ -508,31 +530,13 @@ class UDFLib
}
/**
*
* Print the end of the UDFs block
*/
private function _printEndUDFBlock()
{
echo '</div>'."\n";
}
/**
* 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);
}
/**
* Move UDFs from $data to $UDFs
*/
+1 -1
View File
@@ -177,7 +177,7 @@
// Tinymce JS
if ($tinymce === true) generateJSsInclude('vendor/tinymce/tinymce/tinymce.min.js');
// Tinymce JS
// User Defined Fields
if ($udfs === true) generateJSsInclude('public/js/UDFWidget.js');
// SB Admin 2 template JS
+6 -6
View File
@@ -6,12 +6,12 @@
*/
class UDFWidget extends HTMLWidget
{
private $_requiredPermissions; //
private $_requiredPermissions; // The required permissions to use this UDF widget
private $_schema;
private $_table;
private $_primaryKeyName;
private $_primaryKeyValue;
private $_schema; // Schema name
private $_table; // Table name
private $_primaryKeyName; // Primary key name
private $_primaryKeyValue; // Primary key value
/**
* Initialize the UDFWidget and starts the execution of the logic
@@ -22,7 +22,7 @@ class UDFWidget extends HTMLWidget
$this->load->library('UDFLib'); // Loads the UDFLib that contains all the used logic
$this->udflib->setUDFUniqueIdByParams($args);
$this->udflib->setUDFUniqueIdByParams($args); // sets the unique id for this UDF
$this->_initUDFWidget($args); // checks parameters and initialize properties