Repository: ampache/ampache (language: php)

Issue:
Catalog Filter choices are not saved at catalog creation
## Description
Catalog selections are not saved during catalog filter creation
 
### Describe the bug
When a catalog filter is first created, the admin is presented with a check-box list of catalogs which should be included in this new filter.  After saving, the filter has zero catalogs assigned, and the admin must edit the filter to put the appropriate catalogs in the filter.

### To reproduce

1. Go to "Admin -> Catalogs -> Add catalog filter
2. Enter a new catalog filter name and select any number of catalogs to be included
3. Hit "Add Filter", then "Continue"
4. In the displayed filter list, see the new filter has a 0 (zero) in the Catalogs field

At this point, the admin must edit the filter and put the catalogs back.

### Expected behavior
I expect the catalog creation to work by saving the selected catalogs at creation time.

### Screenshots
![image](https://user-images.githubusercontent.com/8419000/181777395-e061216e-e4ef-4965-b3be-58f657220efc.png)
Screenshot of the filter being created.

![image](https://user-images.githubusercontent.com/8419000/181777558-1b7e918d-dc1f-4190-8584-f5e00dc79583.png)
After creation, the filter has no catalogs assigned, even though they were selected during filter creation.

## Environment

* Ampache version: develop branch

### Client type

* Web

## Settings

<!-- Either put a description of your settings, or paste the contents of `conf/ampache.cfg.php` after removing sensitive information (server host, database connection, etc.). If posting full config file, make sure to put triple back-ticks ``` at the top and bottom to make it into a code block.

It may also make sense to post PHP settings depending on the situation.  -->

## Logs
Nothing out of the ordinary, the errors listed are known.
~~~
2022-07-29T14:07:53+00:00 [ampache] (Ampache\Module\System\Dba) -> Database connection...
2022-07-29T14:07:53+00:00 [ampache] (Ampache\Module\System\Dba) -> Database connection...
2022-07-29T14:07:53+00:00 [ampache] (log.lib) -> [Error] Return type of Gettext\Translations::offsetSet($index, $value) should either be compatible with ArrayObject::offsetSet(mixed $key, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in file /var/www/ampache/vendor/gettext/gettext/src/Translations.php(218)
2022-07-29T14:07:53+00:00 [ampache] (log.lib) -> [Error] Constant FILTER_SANITIZE_STRING is deprecated in file /var/www/ampache/src/Repository/Model/User.php(1328)
2022-07-29T14:07:53+00:00 [admin] (Ampache\Module\System\Session) -> 1ql4qk8a45ehgg77vr0bfler2c has been extended to Fri, 29 Jul 2022 15:07:53 +0000 extension length 3600
2022-07-29T14:07:53+00:00 [admin] (Ampache\Module\Application\ApplicationRunner) -> Found handler "Ampache\Module\Application\Admin\Filter\AddFilterAction" for action "add_filter"
2022-07-29T14:07:53+00:00 [admin] (Ampache\Module\System\Core) -> Verified SID 2d07de131173f2e27e3564e33cdd5d14 for post form add_filter
2022-07-29T14:07:53+00:00 [ampache] (log.lib) -> [Error] Constant FILTER_SANITIZE_STRING is deprecated in file /var/www/ampache/src/Module/System/Core.php(127)
2022-07-29T14:07:53+00:00 [ampache] (log.lib) -> [Error] Constant FILTER_SANITIZE_STRING is deprecated in file /var/www/ampache/src/Module/System/Core.php(127)
2022-07-29T14:07:53+00:00 [admin] (Ampache\Module\System\Session) -> Session created: 19325f6d4436f9ae3dba3f47b4ac8192
2022-07-29T14:07:53+00:00 [ampache] (log.lib) -> [Error] Constant FILTER_SANITIZE_STRING is deprecated in file /var/www/ampache/src/Module/System/Core.php(127)
2022-07-29T14:07:53+00:00 [ampache] (log.lib) -> [Error] Constant FILTER_SANITIZE_STRING is deprecated in file /var/www/ampache/src/Module/Application/Admin/Filter/AddFilterAction.php(65)
2022-07-29T14:07:53+00:00 [ampache] (log.lib) -> [Error] Constant FILTER_SANITIZE_STRING is deprecated in file /var/www/ampache/src/Module/Application/Admin/Filter/AddFilterAction.php(89)
2022-07-29T14:07:53+00:00 [ampache] (log.lib) -> [Error] Constant FILTER_SANITIZE_STRING is deprecated in file /var/www/ampache/src/Module/Application/Admin/Filter/AddFilterAction.php(89)
2022-07-29T14:07:53+00:00 [ampache] (log.lib) -> [Error] Constant FILTER_SANITIZE_STRING is deprecated in file /var/www/ampache/src/Module/Application/Admin/Filter/AddFilterAction.php(89)
2022-07-29T14:07:53+00:00 [ampache] (log.lib) -> [Error] Constant FILTER_SANITIZE_STRING is deprecated in file /var/www/ampache/src/Module/Application/Admin/Filter/AddFilterAction.php(89)
2022-07-29T14:07:53+00:00 [ampache] (log.lib) -> [Error] Constant FILTER_SANITIZE_STRING is deprecated in file /var/www/ampache/src/Module/Application/Admin/Filter/AddFilterAction.php(89)
2022-07-29T14:07:53+00:00 [ampache] (log.lib) -> [Error] Constant FILTER_SANITIZE_STRING is deprecated in file /var/www/ampache/src/Module/Application/Admin/Filter/AddFilterAction.php(89)
2022-07-29T14:07:53+00:00 [ampache] (log.lib) -> [Error] Constant FILTER_SANITIZE_STRING is deprecated in file /var/www/ampache/src/Module/Application/Admin/Filter/AddFilterAction.php(89)
2022-07-29T14:07:53+00:00 [admin] (Ampache\Module\System\Core) -> Registered post form confirmation with SID 243248d37f560e73d2c986b2c1bac2d1 and expiration 1659107273 (3600 seconds from now)
2022-07-29T14:07:53+00:00 [admin] (Ampache\Module\System\Session) -> Writing to 1ql4qk8a45ehgg77vr0bfler2c with expiration 1659107273
~~~


Discussion / hints:
Here's some debug output I've added to src/Repository/Model/Catalog.php : add_catalog_filter_group()

~~~
022-07-29T17:32:01+00:00 [admin] (Ampache\Repository\Model\Catalog) -> catalogs Array
2022-07-29T17:32:01+00:00 [admin] (Ampache\Repository\Model\Catalog) -> (
2022-07-29T17:32:01+00:00 [admin] (Ampache\Repository\Model\Catalog) ->     [Choral] => 0
2022-07-29T17:32:01+00:00 [admin] (Ampache\Repository\Model\Catalog) ->     [Extras] => 0
2022-07-29T17:32:01+00:00 [admin] (Ampache\Repository\Model\Catalog) ->     [Folk] => 0
2022-07-29T17:32:01+00:00 [admin] (Ampache\Repository\Model\Catalog) ->     [Kids] => 0
2022-07-29T17:32:01+00:00 [admin] (Ampache\Repository\Model\Catalog) ->     [Podcasts] => 0
2022-07-29T17:32:01+00:00 [admin] (Ampache\Repository\Model\Catalog) ->     [RESTRICTED] => 0
2022-07-29T17:32:01+00:00 [admin] (Ampache\Repository\Model\Catalog) ->     [TEST] => 0
2022-07-29T17:32:01+00:00 [admin] (Ampache\Repository\Model\Catalog) -> )
2022-07-29T17:32:01+00:00 [admin] (Ampache\Repository\Model\Catalog) -> 
2022-07-29T17:32:01+00:00 [admin] (Ampache\Repository\Model\Catalog) -> catalog SQL INSERT INTO `catalog_filter_group_map` (`group_id`, `catalog_id`, `enabled`) VALUES (11, 1, 0),(11, 2, 0),(11, 3, 0),(11, 4, 0),(11, 5, 0),(11, 6, 0),(11, 7, 0)
~~~
So it looks to me that the "enabled" checkboxes are not getting passed down to the handler.

Fix patch:
diff --git a/public/templates/show_add_filter.inc.php b/public/templates/show_add_filter.inc.php
index 06a477ebfc..fb7ed0c816 100644
--- a/public/templates/show_add_filter.inc.php
+++ b/public/templates/show_add_filter.inc.php
@@ -52,7 +52,7 @@
     $catalogs = Catalog::get_catalogs();
     foreach ($catalogs as $catalog_id) {
         $cn = Catalog::get_catalog_name($catalog_id);
-        echo "<tr><td>$cn</td>" . '<td><input type="checkbox" name="' . $catalog_id . '" value="1"></td></tr>';
+        echo "<tr><td>$cn</td>" . '<td><input type="checkbox" name="catalog_' . $catalog_id . '" value="1"></td></tr>';
     }
     ?>
     </table>
diff --git a/public/templates/show_add_user.inc.php b/public/templates/show_add_user.inc.php
index 7a206ae865..4ae312c4e2 100644
--- a/public/templates/show_add_user.inc.php
+++ b/public/templates/show_add_user.inc.php
@@ -89,7 +89,7 @@
     $options = array();
     foreach ($filters as $filter) {
         $selected = "";
-        if ($filter['id'] == 1) {
+        if ($filter['id'] == 0) {
             $selected = ' selected = "selected" ';
         }
         $options[] = '<option value="' . $filter['id'] . '" ' . $selected . '>' . $filter['name'] . '</option>';
diff --git a/src/Module/Application/Admin/Filter/AddFilterAction.php b/src/Module/Application/Admin/Filter/AddFilterAction.php
index e45816c189..b70d024117 100644
--- a/src/Module/Application/Admin/Filter/AddFilterAction.php
+++ b/src/Module/Application/Admin/Filter/AddFilterAction.php
@@ -86,7 +86,7 @@ protected function handle(ServerRequestInterface $request): ?ResponseInterface
         $catalog_array = array();
         foreach ($catalogs as $catalog) {
             $catalog_name                 = Catalog::get_catalog_name($catalog);
-            $catalog_array[$catalog_name] = (int) scrub_in(filter_input(INPUT_POST, $catalog_name, FILTER_SANITIZE_STRING, FILTER_FLAG_NO_ENCODE_QUOTES));
+            $catalog_array[$catalog_name] = (int)filter_input(INPUT_POST, 'catalog_' . $catalog, FILTER_SANITIZE_NUMBER_INT);
         }
 
         // Attempt to create the filter
@@ -97,7 +97,6 @@ protected function handle(ServerRequestInterface $request): ?ResponseInterface
 
         $this->ui->showConfirmation(
             T_('New Filter Added'),
-            // HINT: %1 Username, %2 Access (Guest, User, Admin)
             sprintf(T_('%1$s has been created'), $filtername),
             sprintf('%s/admin/filter.php', $this->configContainer->getWebPath())
         );


Test patch: