Major upgrade #3

Merged
Llewellyn merged 11 commits from dev into master 2023-08-01 07:42:01 +00:00
15 changed files with 588 additions and 45 deletions

View File

@ -15,6 +15,8 @@ class Load << (F,LightGreen) >> #Green {
+ items(array $select, array $tables, ...) : ?array
+ row(array $select, array $tables, ...) : ?array
+ item(array $select, array $tables, ...) : ?object
+ max(string $field, array $tables, ...) : ?int
+ count(array $tables, array $filter) : ?int
+ value(array $select, array $tables, ...) : mixed
# many(array $select, array $tables, ...) : bool
# one(array $select, array $tables, ...) : bool
@ -75,6 +77,25 @@ note left of Load::item
?array $order = null
end note
note right of Load::max
Get the max value based on a filtered result from a given table
since: 3.2.0
return: ?int
arguments:
string $field
array $tables
array $filter
end note
note left of Load::count
Count the number of items based on filter result from a given table
since: 3.2.0
return: ?int
end note
note right of Load::value
Load one value from a row

View File

@ -144,6 +144,71 @@ final class Load extends Database implements LoadInterface
return null;
}
/**
* Get the max value based on a filtered result from a given table
*
* @param string $field The field key
* @param string $tables The tables
* @param array $filter The filter keys
*
* @return int|null
* @since 3.2.0
**/
public function max($field, array $tables, array $filter): ?int
{
// only do check if we have the table set
if (isset($tables['a']))
{
// get the query
$query = $this->query(["all" => "MAX(`$field`)"], $tables, $filter);
// Load the max number
$this->db->setQuery($query);
$this->db->execute();
// check if we have values
if ($this->db->getNumRows())
{
return (int) $this->db->loadResult();
}
}
// data does not exist
return null;
}
/**
* Count the number of items based on filter result from a given table
*
* @param string $tables The table
* @param array $filter The filter keys
*
* @return int|null
* @since 3.2.0
**/
public function count(array $tables, array $filter): ?int
{
// only do check if we have the table set
if (isset($tables['a']))
{
// get the query
$query = $this->query(["all" => 'COUNT(*)'], $tables, $filter);
// Load the max number
$this->db->setQuery($query);
$this->db->execute();
// check if we have values
if ($this->db->getNumRows())
{
return (int) $this->db->loadResult();
}
}
// data does not exist
return null;
}
/**
* Load one value from a row
*

View File

@ -118,6 +118,71 @@
return null;
}
/**
* Get the max value based on a filtered result from a given table
*
* @param string $field The field key
* @param string $tables The tables
* @param array $filter The filter keys
*
* @return int|null
* @since 3.2.0
**/
public function max($field, array $tables, array $filter): ?int
{
// only do check if we have the table set
if (isset($tables['a']))
{
// get the query
$query = $this->query(["all" => "MAX(`$field`)"], $tables, $filter);
// Load the max number
$this->db->setQuery($query);
$this->db->execute();
// check if we have values
if ($this->db->getNumRows())
{
return (int) $this->db->loadResult();
}
}
// data does not exist
return null;
}
/**
* Count the number of items based on filter result from a given table
*
* @param string $tables The table
* @param array $filter The filter keys
*
* @return int|null
* @since 3.2.0
**/
public function count(array $tables, array $filter): ?int
{
// only do check if we have the table set
if (isset($tables['a']))
{
// get the query
$query = $this->query(["all" => 'COUNT(*)'], $tables, $filter);
// Load the max number
$this->db->setQuery($query);
$this->db->execute();
// check if we have values
if ($this->db->getNumRows())
{
return (int) $this->db->loadResult();
}
}
// data does not exist
return null;
}
/**
* Load one value from a row
*

View File

@ -15,6 +15,8 @@ interface LoadInterface #Lavender {
+ items(array $select, array $tables, ...) : ?array
+ row(array $select, array $tables, ...) : ?array
+ item(array $select, array $tables, ...) : ?object
+ max(string $field, array $tables, ...) : ?int
+ count(array $tables, array $filter) : ?int
+ value(array $select, array $tables, ...) : mixed
}
@ -72,6 +74,25 @@ note right of LoadInterface::item
?array $order = null
end note
note right of LoadInterface::max
Get the max value based on a filtered result from a given table
since: 3.2.0
return: ?int
arguments:
string $field
array $tables
array $filter
end note
note right of LoadInterface::count
Count the number of items based on filter result from a given table
since: 3.2.0
return: ?int
end note
note right of LoadInterface::value
Load one value from a row

View File

@ -75,6 +75,29 @@ interface LoadInterface
**/
public function item(array $select, array $tables, ?array $where = null, ?array $order = null): ?object;
/**
* Get the max value based on a filtered result from a given table
*
* @param string $field The field key
* @param string $tables The table
* @param array $filter The filter keys
*
* @return int|null
* @since 3.2.0
**/
public function max($field, array $tables, array $filter): ?int;
/**
* Count the number of items based on filter result from a given table
*
* @param string $tables The table
* @param array $filter The filter keys
*
* @return int|null
* @since 3.2.0
**/
public function count(array $tables, array $filter): ?int;
/**
* Load one value from a row
*
@ -86,7 +109,6 @@ interface LoadInterface
* @return mixed
* @since 3.2.0
**/
public function value(array $select, array $tables, ?array $where = null, ?array $order = null);
public function value(array $select, array $tables, ?array $where = null, ?array $order = null);
}

View File

@ -54,6 +54,29 @@
**/
public function item(array $select, array $tables, ?array $where = null, ?array $order = null): ?object;
/**
* Get the max value based on a filtered result from a given table
*
* @param string $field The field key
* @param string $tables The table
* @param array $filter The filter keys
*
* @return int|null
* @since 3.2.0
**/
public function max($field, array $tables, array $filter): ?int;
/**
* Count the number of items based on filter result from a given table
*
* @param string $tables The table
* @param array $filter The filter keys
*
* @return int|null
* @since 3.2.0
**/
public function count(array $tables, array $filter): ?int;
/**
* Load one value from a row
*
@ -65,4 +88,4 @@
* @return mixed
* @since 3.2.0
**/
public function value(array $select, array $tables, ?array $where = null, ?array $order = null);
public function value(array $select, array $tables, ?array $where = null, ?array $order = null);

View File

@ -204,7 +204,7 @@ final class Insert extends Database implements InsertInterface
$add_version = true;
}
if (!isset($columns['version']))
if (!isset($columns['published']))
{
$columns['published'] = ' (o_O) ';
$add_published = true;

View File

@ -177,7 +177,7 @@
$add_version = true;
}
if (!isset($columns['version']))
if (!isset($columns['published']))
{
$columns['published'] = ' (o_O) ';
$add_published = true;

View File

@ -15,9 +15,9 @@ abstract Model #Orange {
# Table $table
+ __construct(Table $table)
+ {abstract} value(mixed $value, string $field, ...) : mixed
+ item(object $item, ?string $table = null) : ?object
+ item(?object $item, ?string $table = null) : ?object
+ items(?array $items = null, ?string $table = null) : ?array
+ row(array $item, ?string $table = null) : ?array
+ row(?array $item, ?string $table = null) : ?array
+ rows(?array $items = null, ?string $table = null) : ?array
+ last(?string $table = null) : ?int
# getTableFields(string $table, bool $default = false) : ?array

View File

@ -69,14 +69,20 @@ abstract class Model
* Model the values of an item
* Example: $this->item(Object, 'table_name');
*
* @param object $item The item object
* @param object|null $item The item object
* @param string|null $table The table
*
* @return object|null
* @since 3.2.0
*/
public function item(object $item, ?string $table = null): ?object
public function item(?object $item, ?string $table = null): ?object
{
// we must have an object
if (empty($item))
{
return null;
}
// set the table name
if (empty($table))
{
@ -172,14 +178,20 @@ abstract class Model
* Model the values of an row
* Example: $this->item(Array, 'table_name');
*
* @param array $item The item array
* @param array|null $item The item array
* @param string|null $table The table
*
* @return array|null
* @since 3.2.0
*/
public function row(array $item, ?string $table = null): ?array
public function row(?array $item, ?string $table = null): ?array
{
// we must have an array
if (empty($item))
{
return null;
}
// set the table name
if (empty($table))
{

View File

@ -43,14 +43,20 @@
* Model the values of an item
* Example: $this->item(Object, 'table_name');
*
* @param object $item The item object
* @param object|null $item The item object
* @param string|null $table The table
*
* @return object|null
* @since 3.2.0
*/
public function item(object $item, ?string $table = null): ?object
public function item(?object $item, ?string $table = null): ?object
{
// we must have an object
if (empty($item))
{
return null;
}
// set the table name
if (empty($table))
{
@ -146,14 +152,20 @@
* Model the values of an row
* Example: $this->item(Array, 'table_name');
*
* @param array $item The item array
* @param array|null $item The item array
* @param string|null $table The table
*
* @return array|null
* @since 3.2.0
*/
public function row(array $item, ?string $table = null): ?array
public function row(?array $item, ?string $table = null): ?array
{
// we must have an array
if (empty($item))
{
return null;
}
// set the table name
if (empty($table))
{

View File

@ -132,7 +132,7 @@ abstract class FileHelper
elseif (!self::$curlError)
{
// set the notice
Factory::getApplication()->enqueueMessage(Text::_('COM_COMPONENTBUILDER_HTWOCURL_NOT_FOUNDHTWOPPLEASE_SETUP_CURL_ON_YOUR_SYSTEM_OR_BCOMPONENTBUILDERB_WILL_NOT_FUNCTION_CORRECTLYP'), 'Error');
Factory::getApplication()->enqueueMessage(Text::_('COM_GETBIBLE_HTWOCURL_NOT_FOUNDHTWOPPLEASE_SETUP_CURL_ON_YOUR_SYSTEM_OR_BGETBIBLEB_WILL_NOT_FUNCTION_CORRECTLYP'), 'Error');
// load this notice only once
self::$curlError = true;
}

View File

@ -19,6 +19,9 @@ abstract BaseTable #Orange {
+ exist(string $table, ?string $field = null) : bool
+ fields(string $table, bool $default = false) : ?array
# addDefault(array $fields) : array
# isDefault(string $field) : bool
# getDefault(string $field) : ?array
# getDefaultKey(string $field, string $key) : ?string
}
note right of BaseTable::get
@ -40,7 +43,7 @@ Example: $this->get('All');
?string $key = null
end note
note right of BaseTable::title
note left of BaseTable::title
Get title field from an area/view/table
since: 3.2.0
@ -54,7 +57,7 @@ note right of BaseTable::titleName
return: string
end note
note right of BaseTable::tables
note left of BaseTable::tables
Get all tables
since: 3.2.0
@ -68,7 +71,7 @@ note right of BaseTable::exist
return: bool
end note
note right of BaseTable::fields
note left of BaseTable::fields
Get all fields of an area/view/table
since: 3.2.0
@ -81,6 +84,27 @@ note right of BaseTable::addDefault
since: 3.2.0
return: array
end note
note left of BaseTable::isDefault
Check if the field is a default field
since: 3.2.0
return: bool
end note
note right of BaseTable::getDefault
Get a default field
since: 3.2.0
return: ?array
end note
note left of BaseTable::getDefaultKey
Get a default field property
since: 3.2.0
return: ?string
end note
@enduml
```

View File

@ -30,6 +30,97 @@ abstract class BaseTable implements Tableinterface
**/
protected array $tables;
/**
* All default fields
*
* @var array
* @since 3.2.0
**/
protected array $defaults = [
'id' => [
'order' => -1,
'name' => 'id',
'label' => 'ID',
'type' => 'text',
'title' => false,
'list' => NULL,
'store' => NULL,
'tab_name' => NULL
],
'ordering' => [
'name' => 'ordering',
'label' => 'Ordering',
'type' => 'number',
'title' => false,
'list' => NULL,
'store' => NULL,
'tab_name' => NULL
],
'published' => [
'name' => 'published',
'label' => 'Status',
'type' => 'list',
'title' => false,
'list' => NULL,
'store' => NULL,
'tab_name' => NULL
],
'modified_by' => [
'name' => 'modified_by',
'label' => 'Modified by',
'type' => 'user',
'title' => false,
'list' => NULL,
'store' => NULL,
'tab_name' => NULL
],
'modified' => [
'name' => 'modified',
'label' => 'Modified',
'type' => 'calendar',
'title' => false,
'list' => NULL,
'store' => NULL,
'tab_name' => NULL
],
'created_by' => [
'name' => 'created_by',
'label' => 'Created by',
'type' => 'user',
'title' => false,
'list' => NULL,
'store' => NULL,
'tab_name' => NULL
],
'created' => [
'name' => 'created',
'label' => 'Created',
'type' => 'calendar',
'title' => false,
'list' => NULL,
'store' => NULL,
'tab_name' => NULL
],
'hits' => [
'name' => 'hits',
'label' => 'Hits',
'type' => 'number',
'title' => false,
'list' => NULL,
'store' => NULL,
'tab_name' => NULL
],
'version' => [
'name' => 'version',
'label' => 'Version',
'type' => 'text',
'title' => false,
'list' => NULL,
'store' => NULL,
'tab_name' => NULL
]
];
/**
* Get any value from a item/field/column of an area/view/table
* Example: $this->get('table_name', 'field_name', 'value_key');
@ -57,7 +148,8 @@ abstract class BaseTable implements Tableinterface
{
return $this->tables[$table][$field][$key];
}
return null;
return $this->getDefaultKey($field, $key);
}
// return the item/field/column of an area/view/table
elseif (is_string($field))
@ -66,7 +158,8 @@ abstract class BaseTable implements Tableinterface
{
return $this->tables[$table][$field];
}
return null;
return $this->getDefault($field);
}
// return an area/view/table
elseif ($table !== 'All')
@ -167,7 +260,7 @@ abstract class BaseTable implements Tableinterface
}
}
return false;
return $this->isDefault($field);
}
/**
@ -201,7 +294,7 @@ abstract class BaseTable implements Tableinterface
/**
* Add the default fields
*
* @param array $fields The table dynamic fields
* @param array $fields The table dynamic fields
*
* @return array Fields (with defaults added)
* @since 3.2.0
@ -209,17 +302,63 @@ abstract class BaseTable implements Tableinterface
protected function addDefault(array $fields): array
{
// add default fields
array_unshift($fields, 'id');
$fields[] = 'ordering';
$fields[] = 'published';
$fields[] = 'modified_by';
$fields[] = 'modified';
$fields[] = 'created_by';
$fields[] = 'created';
$fields[] = 'hits';
$fields[] = 'version';
foreach ($this->defaults as $default)
{
// used just for loading the fields
$order = $default['order'] ?? 1;
unset($default['order']);
if ($order < 0)
{
array_unshift($fields, $default['name']);
}
else
{
$fields[] = $default['name'];
}
}
return $fields;
}
/**
* Check if the field is a default field
*
* @param string $field The field to check
*
* @return bool True if a default field
* @since 3.2.0
*/
protected function isDefault(string $field): bool
{
return isset($this->defaults[$field]);
}
/**
* Get a default field
*
* @param string $field The field to check
*
* @return array|null True if a default field
* @since 3.2.0
*/
protected function getDefault(string $field): ?array
{
return $this->defaults[$field] ?? null;
}
/**
* Get a default field property
*
* @param string $field The field to check
* @param string $key The field key/property to check
*
* @return string|null String value if a default field property exist
* @since 3.2.0
*/
protected function getDefaultKey(string $field, string $key): ?string
{
return $this->defaults[$field][$key] ?? null;
}
}

View File

@ -6,6 +6,97 @@
**/
protected array $tables;
/**
* All default fields
*
* @var array
* @since 3.2.0
**/
protected array $defaults = [
'id' => [
'order' => -1,
'name' => 'id',
'label' => 'ID',
'type' => 'text',
'title' => false,
'list' => NULL,
'store' => NULL,
'tab_name' => NULL
],
'ordering' => [
'name' => 'ordering',
'label' => 'Ordering',
'type' => 'number',
'title' => false,
'list' => NULL,
'store' => NULL,
'tab_name' => NULL
],
'published' => [
'name' => 'published',
'label' => 'Status',
'type' => 'list',
'title' => false,
'list' => NULL,
'store' => NULL,
'tab_name' => NULL
],
'modified_by' => [
'name' => 'modified_by',
'label' => 'Modified by',
'type' => 'user',
'title' => false,
'list' => NULL,
'store' => NULL,
'tab_name' => NULL
],
'modified' => [
'name' => 'modified',
'label' => 'Modified',
'type' => 'calendar',
'title' => false,
'list' => NULL,
'store' => NULL,
'tab_name' => NULL
],
'created_by' => [
'name' => 'created_by',
'label' => 'Created by',
'type' => 'user',
'title' => false,
'list' => NULL,
'store' => NULL,
'tab_name' => NULL
],
'created' => [
'name' => 'created',
'label' => 'Created',
'type' => 'calendar',
'title' => false,
'list' => NULL,
'store' => NULL,
'tab_name' => NULL
],
'hits' => [
'name' => 'hits',
'label' => 'Hits',
'type' => 'number',
'title' => false,
'list' => NULL,
'store' => NULL,
'tab_name' => NULL
],
'version' => [
'name' => 'version',
'label' => 'Version',
'type' => 'text',
'title' => false,
'list' => NULL,
'store' => NULL,
'tab_name' => NULL
]
];
/**
* Get any value from a item/field/column of an area/view/table
* Example: $this->get('table_name', 'field_name', 'value_key');
@ -33,7 +124,8 @@
{
return $this->tables[$table][$field][$key];
}
return null;
return $this->getDefaultKey($field, $key);
}
// return the item/field/column of an area/view/table
elseif (is_string($field))
@ -42,7 +134,8 @@
{
return $this->tables[$table][$field];
}
return null;
return $this->getDefault($field);
}
// return an area/view/table
elseif ($table !== 'All')
@ -143,7 +236,7 @@
}
}
return false;
return $this->isDefault($field);
}
/**
@ -177,7 +270,7 @@
/**
* Add the default fields
*
* @param array $fields The table dynamic fields
* @param array $fields The table dynamic fields
*
* @return array Fields (with defaults added)
* @since 3.2.0
@ -185,15 +278,61 @@
protected function addDefault(array $fields): array
{
// add default fields
array_unshift($fields, 'id');
$fields[] = 'ordering';
$fields[] = 'published';
$fields[] = 'modified_by';
$fields[] = 'modified';
$fields[] = 'created_by';
$fields[] = 'created';
$fields[] = 'hits';
$fields[] = 'version';
foreach ($this->defaults as $default)
{
// used just for loading the fields
$order = $default['order'] ?? 1;
unset($default['order']);
if ($order < 0)
{
array_unshift($fields, $default['name']);
}
else
{
$fields[] = $default['name'];
}
}
return $fields;
}
/**
* Check if the field is a default field
*
* @param string $field The field to check
*
* @return bool True if a default field
* @since 3.2.0
*/
protected function isDefault(string $field): bool
{
return isset($this->defaults[$field]);
}
/**
* Get a default field
*
* @param string $field The field to check
*
* @return array|null True if a default field
* @since 3.2.0
*/
protected function getDefault(string $field): ?array
{
return $this->defaults[$field] ?? null;
}
/**
* Get a default field property
*
* @param string $field The field to check
* @param string $key The field key/property to check
*
* @return string|null String value if a default field property exist
* @since 3.2.0
*/
protected function getDefaultKey(string $field, string $key): ?string
{
return $this->defaults[$field][$key] ?? null;
}