Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
ListItemSelectionTrait
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
12
0.00% covered (danger)
0.00%
0 / 1
 getSelectedIds
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3/**
4 * List Item Selection
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Hebis Verbundzentrale 2024.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2,
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 *
23 * @category VuFind
24 * @package  Controller_Plugins
25 * @author   David Lahm <lahm@uni-frankfurt.de>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org Main Page
28 */
29
30namespace VuFind\Controller\Feature;
31
32use function in_array;
33
34/**
35 * List Item Selection
36 *
37 * @category VuFind
38 * @package  Controller_Plugins
39 * @author   David Lahm <lahm@uni-frankfurt.de>
40 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
41 * @link     https://vufind.org Main Page
42 */
43trait ListItemSelectionTrait
44{
45    /**
46     * Get selected ids
47     *
48     * @return array
49     */
50    protected function getSelectedIds()
51    {
52        // Values may be stored as a default state (checked_default), a list of IDs that do not
53        // match the default state (non_default_ids), and a list of all IDs (all_ids_global). If these
54        // values are found, we need to calculate the selected list from them.
55        $checkedDefault = $this->params()->fromPost('checked_default') !== null;
56        $nonDefaultIds = $this->params()->fromPost('non_default_ids');
57        $allIdsGlobal = $this->params()->fromPost('all_ids_global', '[]');
58        if ($nonDefaultIds !== null) {
59            $nonDefaultIds = json_decode($nonDefaultIds);
60            return array_values(array_filter(
61                json_decode($allIdsGlobal),
62                function ($id) use ($checkedDefault, $nonDefaultIds) {
63                    $nonDefaultId = in_array($id, $nonDefaultIds);
64                    return $checkedDefault xor $nonDefaultId;
65                }
66            ));
67        }
68        // If we got this far, values were passed in a simpler format: a list of checked IDs (ids),
69        // a list of all IDs on the current page (idsAll), and whether the whole page is
70        // selected (selectAll):
71        return null === $this->params()->fromPost('selectAll')
72            ? $this->params()->fromPost('ids', [])
73            : $this->params()->fromPost('idsAll', []);
74    }
75}