- New directory application/widgets/html/ for widgets to render HTML

elements
- All the widgets classes to render HTML elements moved from WidgetLib
to application/widgets/html/
- New directory application/widgets/udf/ for widgets to render UDF
- All the UDF widgets classes moved from WidgetLib to
application/widgets/udf/
- HTMLWidget is now the main class to render HTML widget
- UDFWidget is now the mail class to render UDF
- Removed UDFWidgetTpl
- Added function loadResource to fhc helper
- Changed method widget of WidgetLib (using loadResource)
- Moved UDFWidget method from WidgetLib to UDFLib
- Slimmed down class Widget present in WidgetLib
- Controller system/UDf now uses UDFLib
- View application/views/system/udf.php now uses UDFLib
- Updated udf and html widgets to adapt them to the new libs
This commit is contained in:
Paolo
2017-08-11 18:41:21 +02:00
parent d7bed89869
commit f126bca708
19 changed files with 748 additions and 652 deletions
+18 -535
View File
@@ -28,6 +28,9 @@ if (!defined("BASEPATH")) exit("No direct script access allowed");
class WidgetLib
{
const DIR_HTML_WIDGETS = 'html';
const HTML_WIDGET_NAME = 'HTMLWidget';
/* default values */
private $_template = 'template';
private $_parser = FALSE;
@@ -45,8 +48,15 @@ class WidgetLib
{
$this->_ci = & get_instance();
// set the default widget path with APPPATH
$this->_ci->load->helper('fhc');
// Set the default widget path with APPPATH
$this->_widget_path = APPPATH . 'widgets/';
// Loads widgets to render HTML elements
// NOTE: the first one to be loaded must be HTMLWidget
loadResource($this->_widget_path.WidgetLib::DIR_HTML_WIDGETS, WidgetLib::HTML_WIDGET_NAME);
loadResource($this->_widget_path.WidgetLib::DIR_HTML_WIDGETS);
if (!empty($config))
$this->initialize($config);
@@ -184,65 +194,15 @@ class WidgetLib
*/
public function widget($name, $data = array(), $htmlArgs = array())
{
$class = str_replace('.php', '', trim($name, '/'));
//
loadResource($this->_widget_path, $name, true);
// determine path and widget class name
$path = $this->_widget_path;
if (($last_slash = strrpos($class, '/')) !== FALSE) {
$path += substr($class, 0, $last_slash);
$class = substr($class, $last_slash + 1);
if (!class_exists($name))
{
show_error("Widget '" . $name . "' was not found.");
}
// new widget
if (!class_exists($class)) {
// try both lowercase and capitalized versions
foreach (array(ucfirst($class), strtolower($class)) as $class) {
if (file_exists($path . $class . '.php')) {
include_once ($path . $class . '.php');
// found the file, stop looking
break;
}
}
}
if (!class_exists($class)) {
show_error("Widget '" . $class . "' was not found.");
}
return new $class($class, $data, $htmlArgs);
}
/**
*
*/
public function UDFWidget($args, $htmlArgs = array())
{
if (!empty($args[UDFWidgetTpl::SCHEMA_ARG_NAME]) && !empty($args[UDFWidgetTpl::TABLE_ARG_NAME]))
{
//
if (empty($args[UDFWidgetTpl::FIELD_ARG_NAME]) && !isset($htmlArgs[Widget::EXTERNAL_BLOCK]))
{
$htmlArgs[Widget::EXTERNAL_BLOCK] = true;
}
return $this->widget(
UDFWidgetTpl::WIDGET_NAME,
$args,
$htmlArgs
);
}
else
{
if (empty($args[UDFWidgetTpl::SCHEMA_ARG_NAME]))
{
show_error(UDFWidgetTpl::SCHEMA_ARG_NAME.' parameter is missing!');
}
if (empty($args[UDFWidgetTpl::TABLE_ARG_NAME]))
{
show_error(UDFWidgetTpl::TABLE_ARG_NAME.' parameter is missing!');
}
}
return new $name($name, $data, $htmlArgs);
}
/**
@@ -635,35 +595,10 @@ class Partial
}
/**
* The mother of all widgets
* it represent a generic HTML element
* General widget element
*/
class Widget extends Partial
{
// The name of the array present in the data array given to the view that will render this widget
const HTML_ARG_NAME = 'HTML';
const HTML_DEFAULT_VALUE = ''; // Default value of the html element
const HTML_NAME = 'name'; // HTML name attribute
const HTML_ID = 'id'; // HTML id attribute
const EXTERNAL_BLOCK = 'externalBlock'; //
const EXTERNAL_START_BLOCK_HTML_TAG = '<div>'; //
const EXTERNAL_END_BLOCK_HTML_TAG = '</div>'; //
/**
* It gets also the htmlArgs array as parameter, it will be used to set the HTML properties
*/
public function __construct($name, $args = array(), $htmlArgs = array())
{
parent::__construct($name, $args);
// Initialising HTML properties
$this->_setHtmlProperties($htmlArgs);
// Loads helper message to manage returning messages
$this->load->helper('message');
}
/**
* (non-PHPdoc)
* @see Partial::content()
@@ -690,456 +625,4 @@ class Widget extends Partial
return parent::content();
}
/**
* Initialising html properties, such as the id and name attributes of the HTML element
*/
private function _setHtmlProperties($htmlArgs)
{
// If $htmlArgs wasn't already stored in $this->_args
if (!isset($this->_args[Widget::HTML_ARG_NAME]))
{
$this->_args[Widget::HTML_ARG_NAME] = array();
// Avoids that elements in a HTML page have the same name or id
$randomIdentifier = uniqid(rand(0, 1000));
$this->_args[Widget::HTML_ARG_NAME][Widget::HTML_ID] = $randomIdentifier;
$this->_args[Widget::HTML_ARG_NAME][Widget::HTML_NAME] = $randomIdentifier;
foreach($htmlArgs as $argName => $argValue)
{
$this->_args[Widget::HTML_ARG_NAME][$argName] = $argValue;
}
}
}
/**
*
*/
public static function printAttribute($htmlArgs, $attribute, $isValuePresent = true)
{
if ($attribute != null)
{
if (isset($htmlArgs[$attribute]))
{
if ($isValuePresent === true)
{
$value = $htmlArgs[$attribute];
if (is_bool($value))
{
$value = $value ? 'true' : 'false';
}
echo sprintf('%s="%s"', $attribute, $value);
}
else
{
echo $attribute;
}
}
}
}
/**
*
*/
public static function printStartBlock($htmlArgs)
{
if (isset($htmlArgs[Widget::EXTERNAL_BLOCK])
&& $htmlArgs[Widget::EXTERNAL_BLOCK] === true)
{
echo Widget::EXTERNAL_START_BLOCK_HTML_TAG;
}
}
/**
*
*/
public static function printEndBlock($htmlArgs)
{
if (isset($htmlArgs[Widget::EXTERNAL_BLOCK])
&& $htmlArgs[Widget::EXTERNAL_BLOCK] === true)
{
echo Widget::EXTERNAL_END_BLOCK_HTML_TAG;
}
}
}
/**
* It exends the Widget class to represent an HTML dropdown
*/
class DropdownWidget extends Widget
{
// The name of the element of the data array given to the view
// this element is an array of elements to be place inside the dropdown
const WIDGET_DATA_ELEMENTS_ARRAY_NAME = 'ELEMENTS_ARRAY';
// Name of the property that will be used to store the value attribute of the option tag
const ID_FIELD = 'id';
// Name of the property that will be used to store the value between the option tags
const DESCRIPTION_FIELD = 'description';
// The name of the element of the data array given to the view
// this element is used to tell what element of the dropdown is selected
const SELECTED_ELEMENT = 'selectedElement';
//
const HTML_DEFAULT_VALUE = 'null';
const SIZE = 'size'; //
const MULTIPLE = 'multiple'; //
/**
*
*/
public function __construct($name, $args = array(), $htmlArgs = array())
{
parent::__construct($name, $args, $htmlArgs);
//
if (!isset($this->_args[DropdownWidget::SELECTED_ELEMENT]))
{
$this->_args[DropdownWidget::SELECTED_ELEMENT] = DropdownWidget::HTML_DEFAULT_VALUE;
}
}
/**
*
*/
public function setMultiple()
{
$this->_args[Widget::HTML_ARG_NAME][DropdownWidget::MULTIPLE] = DropdownWidget::MULTIPLE;
$this->_args[Widget::HTML_ARG_NAME][Widget::HTML_NAME] .= '[]';
}
/**
*
*/
public function isMultipleDropdown()
{
$isMultipleDropdown = false;
if (isset($this->_args[Widget::HTML_ARG_NAME][DropdownWidget::MULTIPLE])
&& $this->_args[Widget::HTML_ARG_NAME][DropdownWidget::MULTIPLE] == DropdownWidget::MULTIPLE)
{
$isMultipleDropdown = true;
}
return $isMultipleDropdown;
}
/**
* Add the correct select to the model used to load a list of elemets for this dropdown
* @param model $model the model used to load elements
* @param string $idName the name of the field that will used to be the value of the option tag
* @param string $descriptionName the name of the field that will used to be displayed in the dropdown
*/
protected function addSelectToModel($model, $idName, $descriptionName)
{
$model->addSelect(
sprintf(
'%s AS %s, %s AS %s',
$idName,
DropdownWidget::ID_FIELD,
$descriptionName,
DropdownWidget::DESCRIPTION_FIELD
)
);
}
/**
* Set the array used to populate the dropdown
* @param array $elements list used to populate this dropdown
* @param boolean $emptyElement if an empty element must be added at the beginning of the dropdown
* @param string $stdDescription description of the empty element
* @param string $noDataDescription description if no data are found
* @param string $id value of the attribute value of the empty element
*/
protected function setElementsArray(
$elements, $emptyElement = false, $stdDescription = '' , $noDataDescription = 'No data found' , $id = DropdownWidget::HTML_DEFAULT_VALUE
)
{
$tmpElements = array();
if (isError($elements))
{
if (is_object($elements) && isset($elements->retval))
{
show_error($elements->retval);
}
else if (is_string($elements))
{
show_error($elements);
}
else
{
show_error('Generic error occurred');
}
}
else
{
if ($emptyElement === true && $this->isMultipleDropdown() == false)
{
$tmpElements = $this->addElementAtBeginning(
$elements,
$stdDescription,
$noDataDescription,
$id
);
}
else
{
$tmpElements = $elements->retval;
}
$this->_args[DropdownWidget::WIDGET_DATA_ELEMENTS_ARRAY_NAME] = $tmpElements;
}
}
/**
* Adds an element to the beginning of the array
*/
protected function addElementAtBeginning($elements, $stdDescription, $noDataDescription, $id)
{
$element = new stdClass();
$element->{DropdownWidget::ID_FIELD} = $id;
$element->{DropdownWidget::DESCRIPTION_FIELD} = $stdDescription;
if (!hasData($elements))
{
$element->{DropdownWidget::DESCRIPTION_FIELD} = $noDataDescription;
}
array_unshift($elements->retval, $element);
return $elements->retval;
}
/**
* Loads the dropdown view with all the elements to be displayed
*/
protected function loadDropDownView()
{
$this->view('widgets/dropdown', $this->_args);
}
}
/**
*
*/
class DropdownWidgetUDF extends DropdownWidget
{
/**
*
*/
public function render($parameters)
{
$tmpElements = array();
//
foreach($parameters as $parameter)
{
//
if ((is_array($parameter) && count($parameter) == 2)
|| (is_string($parameter) || is_numeric($parameter))
|| (is_object($parameter) && isset($parameter->{PARENT::ID_FIELD}) && isset($parameter->{PARENT::DESCRIPTION_FIELD})))
{
$element = new stdClass(); //
//
if (is_array($parameter) && count($parameter) == 2)
{
$element->{PARENT::ID_FIELD} = $parameter[0]; //
$element->{PARENT::DESCRIPTION_FIELD} = $parameter[1]; //
}
//
else if (is_string($parameter) || is_numeric($parameter))
{
$element->{PARENT::ID_FIELD} = $parameter; //
$element->{PARENT::DESCRIPTION_FIELD} = $parameter; //
}
//
else if (is_object($parameter) && isset($parameter->{PARENT::ID_FIELD}) && isset($parameter->{PARENT::DESCRIPTION_FIELD}))
{
$element->{PARENT::ID_FIELD} = $parameter->{PARENT::ID_FIELD}; //
$element->{PARENT::DESCRIPTION_FIELD} = $parameter->{PARENT::DESCRIPTION_FIELD}; //
}
array_push($tmpElements, $element); //
}
}
$this->setElementsArray(
success($tmpElements),
true,
$this->_args[Widget::HTML_ARG_NAME][UDFWidgetTpl::PLACEHOLDER],
'No data found for this UDF'
);
$this->loadDropDownView();
echo $this->content();
}
}
/**
* It exends the Widget class to represent an HTML textarea
*/
class TextareaWidget extends Widget
{
const TEXT = 'text'; //
const ROWS = 'rows'; //
const COLS = 'cols'; //
/**
*
*/
protected function setText($text)
{
$this->_args[TextareaWidget::TEXT] = $text;
}
/**
*
*/
protected function loadTextareaView()
{
$this->view('widgets/textarea', $this->_args);
}
}
/**
*
*/
class TextareaWidgetUDF extends TextareaWidget
{
/**
*
*/
public function render($parameters)
{
if ($parameters != null)
{
$this->setText($parameters);
}
else
{
$this->setText('');
}
$this->loadTextareaView();
echo $this->content();
}
}
/**
* It exends the Widget class to represent an HTML textarea
*/
class TextfieldWidget extends Widget
{
const VALUE = 'text'; //
const SIZE = 'size'; //
/**
*
*/
protected function setValue($value)
{
$this->_args[TextfieldWidget::VALUE] = $value;
}
/**
*
*/
protected function loadTextfieldView()
{
$this->view('widgets/textfield', $this->_args);
}
}
/**
*
*/
class TextfieldWidgetUDF extends TextfieldWidget
{
/**
*
*/
public function render($parameters)
{
if ($parameters != null)
{
$this->setValue($parameters);
}
else
{
$this->setValue('');
}
$this->loadTextfieldView();
echo $this->content();
}
}
/**
* It exends the Widget class to represent an HTML dropdown
*/
class CheckboxWidget extends Widget
{
// Name of the property that will be used to store the value attribute of the option tag
const VALUE_FIELD = 'value';
// Name of the property that will be used to store the value between the option tags
const DESCRIPTION_FIELD = 'description';
// Value of value attribute of the checkbox
const CHECKBOX_VALUE = 'true';
/**
*
*/
protected function loadCheckboxView()
{
$this->view('widgets/checkbox', $this->_args);
}
}
/**
*
*/
class CheckboxWidgetUDF extends CheckboxWidget
{
/**
*
*/
public function render()
{
$this->loadCheckboxView();
echo $this->content();
}
}
/**
* Defined here because these constants can be used
* in the views also
*/
abstract class UDFWidgetTpl extends Widget
{
const WIDGET_NAME = 'UDFWidget';
const SCHEMA_ARG_NAME = 'schema';
const TABLE_ARG_NAME = 'table';
const FIELD_ARG_NAME = 'field';
const UDFS_ARG_NAME = 'udfs';
// HTML components
const TITLE = 'description';
const LABEL = 'title';
const PLACEHOLDER = 'placeholder';
const DEFAULT_VALUE = 'defaultValue';
// Validation attributes
const REGEX = 'regex';
const REQUIRED = 'required';
const MAX_VALUE = 'max-value';
const MIN_VALUE = 'min-value';
const MAX_LENGTH = 'max-length';
const MIN_LENGTH = 'min-length';
}