super-powers/src/e0f6ddbe-2a35-4537-942c-faf.../code.power

199 lines
4.2 KiB
Plaintext

/**
* All areas/views/tables with their field details
*
* @var array
* @since 3.2.0
**/
protected array $tables;
/**
* Get any value from a item/field/column of an area/view/table
* Example: $this->get('table_name', 'field_name', 'value_key');
* Get an item/field/column of an area/view/table
* Example: $this->get('table_name', 'field_name');
* Get all items/fields/columns of an area/view/table
* Example: $this->get('table_name');
* Get all areas/views/tables with all their item/field/column details
* Example: $this->get('All');
*
* @param string $table The table
* @param string|null $field The field
* @param string|null $key The value key
*
* @return mixed
* @since 3.2.0
*/
public function get(string $table, ?string $field = null, ?string $key = null)
{
// return the item/field/column of an area/view/table
if (is_string($field) && is_string($key))
{
// return the value of a item/field/column of an area/view/table
if (isset($this->tables[$table][$field][$key]))
{
return $this->tables[$table][$field][$key];
}
return null;
}
// return the item/field/column of an area/view/table
elseif (is_string($field))
{
if (isset($this->tables[$table][$field]))
{
return $this->tables[$table][$field];
}
return null;
}
// return an area/view/table
elseif ($table !== 'All')
{
if (isset($this->tables[$table]))
{
return $this->tables[$table];
}
return null;
}
// return all
return $this->tables;
}
/**
* Get title field from an area/view/table
*
* @param string $table The area
*
* @return ?array
* @since 3.2.0
*/
public function title(string $table): ?array
{
// return the title item/field/column of an area/view/table
if (($table = $this->get($table)) !== null)
{
foreach ($table as $item)
{
if ($item['title'])
{
return $item;
}
}
}
// none found
return null;
}
/**
* Get title field name
*
* @param string $table The area
*
* @return string
* @since 3.2.0
*/
public function titleName(string $table): string
{
// return the title name of an area/view/table
if (($field = $this->title($table)) !== null)
{
return $field['name'];
}
// none found default to ID
return 'id';
}
/**
* Get all tables
*
* @return array
* @since 3.2.0
*/
public function tables(): array
{
// return all areas/views/tables
return array_keys($this->tables);
}
/**
* Check if a table (and field) exist
*
* @param string $table The area
* @param string|null $field The area
*
* @return bool
* @since 3.2.0
*/
public function exist(string $table, ?string $field = null): bool
{
if (isset($this->tables[$table]))
{
// if we have a field
if (is_string($field))
{
if (isset($this->tables[$table][$field]))
{
return true;
}
}
else
{
return true;
}
}
return false;
}
/**
* Get all fields of an area/view/table
*
* @param string $table The area
* @param bool $default Add the default fields
*
* @return array|null On success an array of fields
* @since 3.2.0
*/
public function fields(string $table, bool $default = false): ?array
{
// return all fields of an area/view/table
if (($table = $this->get($table)) !== null)
{
if ($default)
{
return $this->addDefault(array_keys($table));
}
else
{
return array_keys($table);
}
}
// none found
return null;
}
/**
* Add the default fields
*
* @param array $fields The table dynamic fields
*
* @return array Fields (with defaults added)
* @since 3.2.0
*/
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';
return $fields;
}