diff --git a/src/e0f6ddbe-2a35-4537-942c-faff2ebd04f6/README.md b/src/e0f6ddbe-2a35-4537-942c-faff2ebd04f6/README.md index b97b282..a65522c 100644 --- a/src/e0f6ddbe-2a35-4537-942c-faff2ebd04f6/README.md +++ b/src/e0f6ddbe-2a35-4537-942c-faff2ebd04f6/README.md @@ -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 ``` diff --git a/src/e0f6ddbe-2a35-4537-942c-faff2ebd04f6/code.php b/src/e0f6ddbe-2a35-4537-942c-faff2ebd04f6/code.php index f9a2df7..468645b 100644 --- a/src/e0f6ddbe-2a35-4537-942c-faff2ebd04f6/code.php +++ b/src/e0f6ddbe-2a35-4537-942c-faff2ebd04f6/code.php @@ -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; } } diff --git a/src/e0f6ddbe-2a35-4537-942c-faff2ebd04f6/code.power b/src/e0f6ddbe-2a35-4537-942c-faff2ebd04f6/code.power index 64dc19c..7de1cb4 100644 --- a/src/e0f6ddbe-2a35-4537-942c-faff2ebd04f6/code.power +++ b/src/e0f6ddbe-2a35-4537-942c-faff2ebd04f6/code.power @@ -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; } \ No newline at end of file