Skip to content

Commit

Permalink
update brwose to use catalogquery
Browse files Browse the repository at this point in the history
* allow getting catalogs from gather type array (rename gather_types to gather_type)
* add enabled and user filters
  • Loading branch information
lachlan-00 committed May 14, 2024
1 parent d8103da commit b728e93
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 35 deletions.
57 changes: 24 additions & 33 deletions src/Module/Api/Method/BrowseMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,23 @@ public static function browse(array $input, User $user): bool
return false;
}

$browse = Api::getBrowse();
if ($object_type === 'root') {
// catalog root
$objects = User::get_user_catalogs($user->id, 'music');
$output_type = 'catalog';
$child_type = $output_type;
$gather_types = array('music');
if (AmpConfig::get('podcast')) {
$objects = array_merge($objects, User::get_user_catalogs($user->id, 'podcast'));
$gather_types[] = 'podcast';
}
if (AmpConfig::get('video')) {
// 'clip', 'tvshow', 'movie', 'personal_video'
$objects = array_merge($objects, User::get_user_catalogs($user->id, 'clip'));
$objects = array_merge($objects, User::get_user_catalogs($user->id, 'tvshow'));
$objects = array_merge($objects, User::get_user_catalogs($user->id, 'movie'));
$objects = array_merge($objects, User::get_user_catalogs($user->id, 'personal_video'));
$gather_types = array_merge($gather_types, array('clip', 'tvshow', 'movie', 'personal_video'));
}
$child_type = 'catalog';
$results = Catalog::get_name_array($objects, 'catalog', 'name');

$browse->reset_filters();
$browse->set_type($output_type);
$browse->set_filter('gather_types', $gather_types);
$browse->set_filter('user', $user->getId());
} elseif ($object_type === 'catalog') {
// artist/podcasts/videos
if (!Api::check_parameter($input, array('filter'), self::ACTION)) {
Expand All @@ -105,29 +107,26 @@ public static function browse(array $input, User $user): bool

return false;
}
$browse = Api::getBrowse();
$browse->reset_filters();

Api::set_filter('add', $input['add'] ?? '', $browse);
Api::set_filter('update', $input['update'] ?? '', $browse);
$browse->reset_filters();
switch ((string)$catalog->gather_types) {
case 'clip':
case 'tvshow':
case 'movie':
case 'personal_video':
$output_type = 'video';
$browse->set_type('video');
$browse->set_filter('gather_types', 'video');
$browse->set_filter('gather_type', 'video');
break;
case 'music':
$output_type = 'artist';
$browse->set_type('album_artist');
$browse->set_filter('gather_types', 'music');
$browse->set_filter('gather_type', 'music');
break;
case 'podcast':
$output_type = 'podcast';
$browse->set_type('podcast');
$browse->set_filter('gather_types', 'podcast');
$browse->set_filter('gather_type', 'podcast');
break;
default:
/* HINT: Requested object string/id/type ("album", "myusername", "some song title", 1298376) */
Expand All @@ -138,13 +137,6 @@ public static function browse(array $input, User $user): bool
$child_type = $output_type;
$browse->set_sort('name', 'ASC');
$browse->set_filter('catalog', $catalog->id);
$objects = $browse->get_objects();
if (empty($objects)) {
Api::empty('browse', $input['api_format']);

return false;
}
$results = Catalog::get_name_array($objects, $output_type, 'name');
} else {
if (!Api::check_parameter($input, array('filter', 'catalog'), self::ACTION)) {
return false;
Expand Down Expand Up @@ -172,9 +164,8 @@ public static function browse(array $input, User $user): bool

return false;
}
$browse = Api::getBrowse();
$browse->reset_filters();

$browse->reset_filters();
// for sub objects you want to browse their children
switch ($object_type) {
case 'artist':
Expand All @@ -201,19 +192,19 @@ public static function browse(array $input, User $user): bool
$child_type = $output_type;
$browse->set_sort('name', 'ASC');
$browse->set_filter('catalog', $catalog->id);
}

Api::set_filter('add', $input['add'] ?? '', $browse);
Api::set_filter('update', $input['update'] ?? '', $browse);
Api::set_filter('add', $input['add'] ?? '', $browse);
Api::set_filter('update', $input['update'] ?? '', $browse);

$objects = $browse->get_objects();
if (empty($objects)) {
Api::empty('browse', $input['api_format']);
$objects = $browse->get_objects();
if (empty($objects)) {
Api::empty('browse', $input['api_format']);

return false;
}
$results = Catalog::get_name_array($objects, $output_type, 'name');
return false;
}

$results = Catalog::get_name_array($objects, $output_type, 'name');
ob_end_clean();
switch ($input['api_format']) {
case 'json':
Expand Down
17 changes: 15 additions & 2 deletions src/Module/Database/Query/CatalogQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,16 @@
namespace Ampache\Module\Database\Query;

use Ampache\Module\System\Dba;
use Ampache\Repository\Model\Catalog;
use Ampache\Repository\Model\Query;

final class CatalogQuery implements QueryInterface
{
public const FILTERS = array(
'gather_types'
'enabled',
'gather_type',
'gather_types',
'user'
);

/** @var string[] $sorts */
Expand Down Expand Up @@ -97,9 +101,18 @@ public function get_sql_filter($query, $filter, $value): string
{
$filter_sql = '';
switch ($filter) {
case 'gather_types':
case 'enabled':
$filter_sql = " `catalog`.`enabled` = '" . (int)$value . "' AND ";
break;
case 'gather_type':
$filter_sql = " `catalog`.`gather_types` = '" . Dba::escape($value) . "' AND ";
break;
case 'gather_types':
$filter_sql = " `catalog`.`gather_types` IN (" . implode(',', $value) . ") AND ";
break;
case 'user':
$filter_sql = " `catalog`.`id` IN (" . implode(',', Catalog::get_catalogs('', $value, true)) . ") AND ";
break;
}

return $filter_sql;
Expand Down
1 change: 1 addition & 0 deletions src/Repository/Model/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ public function set_filter($key, mixed $value): bool
case 'album':
case 'disk':
case 'hidden':
case 'gather_type':
case 'gather_types':
$this->_state['filter'][$key] = $value;
break;
Expand Down

0 comments on commit b728e93

Please sign in to comment.