Skip to content

Commit

Permalink
metadata code abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
brookgagnon committed Jul 22, 2024
1 parent 05494d2 commit 2665353
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 22 deletions.
46 changes: 42 additions & 4 deletions classes/base/metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,47 @@

namespace OB\Classes\Base;

abstract class Metadata
class Metadata
{
public static string $sqlType;
public static string $constraintTable;
public static string $constraintColumn;
protected $name;
protected $description;
protected $type;
protected $settings;

public function __construct($name, $description, $type, $settings = [])
{
$this->name = $name;
$this->description = $description;
$this->type = $type;
$this->settings = $settings;
$this->db = \OBFDB::get_instance();

// letters, numbers, underscores only for name
if (!preg_match('/^[a-zA-Z0-9_]+$/', $this->name)) {
throw new \Exception('Invalid metadata name');
}
}

/**
* Return a string or array of strings to be included in the SELECT part of a query.
*/
public function querySelect()
{
$default = $this->settings->default;
if (is_array($default)) {
$default = implode(',', $default);
}

return 'COALESCE(media.metadata_' . $this->name . ', "' . $this->db->escape($default) . '") as metadata_' . $this->name;
}

/**
* Return a string or array of strings to be included in the SET part of a query.
* This is used for adding and inserting items that use metadata.
* TODO: This is not yet used by media_model.
*/
public function querySet($value)
{
return 'metadata_' . $this->name . ' = "' . $this->db->escape($value) . '"';
}
}
1 change: 0 additions & 1 deletion classes/metadata/text.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@

class Text extends \OB\Classes\Base\Metadata
{
public static string $sqlType = 'VARCHAR(255)';
}
23 changes: 7 additions & 16 deletions models/media_model.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,30 +219,21 @@ public function get_init_what($args = [])
$this->db->what('media.artist', 'artist');
$this->db->what('media.album', 'album');
$this->db->what('media.year', 'year');

$this->db->what('media.type', 'type');
$this->db->what('media.format', 'format');

$this->db->what('media.category_id', 'category_id');
$this->db->what('media_categories.name', 'category_name');

$this->db->what('media.country', 'country');
$this->db->what('countries.name', 'country_name');

$this->db->what('media.language', 'language');
$this->db->what('languages.ref_name', 'language_name');

$this->db->what('media.is_approved', 'is_approved');

$this->db->what('media.genre_id', 'genre_id');
$this->db->what('media_genres.name', 'genre_name');

$this->db->what('media.comments', 'comments');

$this->db->what('media.filename', 'filename');
$this->db->what('media.file_hash', 'file_hash');
$this->db->what('media.file_location', 'file_location');

$this->db->what('media.is_copyright_owner', 'is_copyright_owner');
$this->db->what('media.duration', 'duration');
$this->db->what('media.owner_id', 'owner_id');
Expand All @@ -251,16 +242,16 @@ public function get_init_what($args = [])
$this->db->what('media.is_archived', 'is_archived');
$this->db->what('media.status', 'status');
$this->db->what('media.dynamic_select', 'dynamic_select');

$this->db->what('users.display_name', 'owner_name');

foreach ($args['metadata_fields'] as $metadata_field) {
$default = $metadata_field['settings']->default ?? null;
if (is_array($default)) {
$default = implode(',', $default);
$query_select = $metadata_field->querySelect();
if (!is_array($query_select)) {
$query_select = [$query_select];
}
foreach ($query_select as $select) {
$this->db->what($select, null, false);
}

$this->db->what('COALESCE(media.metadata_' . $metadata_field['name'] . ',"' . $this->db->escape($default) . '")', 'metadata_' . $metadata_field['name'], false);
}
}

Expand All @@ -286,7 +277,7 @@ public function get_init($args = [])
{
$metadata_fields = $this->models->mediametadata('get_all');

$this('get_init_what', ['metadata_fields' => $metadata_fields]);
$this('get_init_what', ['metadata_fields' => $this->models->mediametadata('get_all_objects')]);
$this('get_init_join');
}

Expand Down
25 changes: 24 additions & 1 deletion models/mediametadata_model.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,28 @@
*/
class MediaMetadataModel extends OBFModel
{
/**
* Get all metadata columns as metadata objects.
*/
public function get_all_objects()
{
$columns = $this->get_all();

$objects = [];
foreach ($columns as $column) {
$class = 'OB\Classes\Metadata\\' . ucfirst($column['type']);

// no class? use base class.
if (!class_exists($class)) {
$class = 'OB\Classes\Base\Metadata';
}

$objects[] = new $class($column['name'], $column['description'], $column['type'], $column['settings']);
}

return $objects;
}

/**
* Get all metadata columns.
*
Expand All @@ -35,7 +57,7 @@ public function get_all()
{
$this->db->orderby('order_id');
$fields = $this->db->get('media_metadata');
if (! $fields) {
if (!$fields) {
return [];
}

Expand All @@ -50,6 +72,7 @@ public function get_all()
$field['settings']->all = $this('tag_search', ['id' => $field['id'],'search' => '']);
}
}

return $fields;
}

Expand Down

0 comments on commit 2665353

Please sign in to comment.