Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 46
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Feedback
0.00% covered (danger)
0.00%
0 / 46
0.00% covered (danger)
0.00%
0 / 3
110
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getFeedbackByFilter
0.00% covered (danger)
0.00%
0 / 39
0.00% covered (danger)
0.00%
0 / 1
56
 deleteByIdArray
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3/**
4 * Class Feedback
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Moravian Library 2022.
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  Db_Table
25 * @author   Josef Moravec <moravec@mzk.cz>
26 * @license  https://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org Main Site
28 */
29
30declare(strict_types=1);
31
32namespace VuFind\Db\Table;
33
34use Laminas\Db\Adapter\Adapter;
35use Laminas\Paginator\Paginator;
36use VuFind\Db\Row\RowGateway;
37
38use function intval;
39
40/**
41 * Class Feedback
42 *
43 * @category VuFind
44 * @package  Db_Table
45 * @author   Josef Moravec <moravec@mzk.cz>
46 * @license  https://opensource.org/licenses/gpl-2.0.php GNU General Public License
47 * @link     https://vufind.org Main Site
48 */
49class Feedback extends Gateway
50{
51    /**
52     * Constructor
53     *
54     * @param Adapter         $adapter Database adapter
55     * @param PluginManager   $tm      Table manager
56     * @param array           $cfg     Laminas configuration
57     * @param RowGateway|null $rowObj  Row prototype object (null for default)
58     * @param string          $table   Name of database table to interface with
59     */
60    public function __construct(
61        Adapter $adapter,
62        PluginManager $tm,
63        $cfg,
64        ?RowGateway $rowObj = null,
65        $table = 'feedback'
66    ) {
67        parent::__construct($adapter, $tm, $cfg, $rowObj, $table);
68    }
69
70    /**
71     * Get feedback by filter
72     *
73     * @param string|null $formName Form name
74     * @param string|null $siteUrl  Site URL
75     * @param string|null $status   Current status
76     * @param string|null $page     Current page
77     * @param int         $limit    Limit per page
78     *
79     * @return Paginator
80     */
81    public function getFeedbackByFilter(
82        ?string $formName = null,
83        ?string $siteUrl = null,
84        ?string $status = null,
85        ?string $page = null,
86        int $limit = 20
87    ): Paginator {
88        $sql = $this->getSql();
89        $select = $sql->select()->columns(
90            [
91                '*',
92                'user_name' => new \Laminas\Db\Sql\Expression(
93                    "CONCAT_WS(' ', u.firstname, u.lastname)"
94                ),
95                'manager_name' => new \Laminas\Db\Sql\Expression(
96                    "CONCAT_WS(' ', m.firstname, m.lastname)"
97                ),
98            ]
99        );
100        if (null !== $formName) {
101            $select->where->equalTo('form_name', $formName);
102        }
103        if (null !== $siteUrl) {
104            $select->where->equalTo('site_url', $siteUrl);
105        }
106        if (null !== $status) {
107            $select->where->equalTo('status', $status);
108        }
109        $select->join(
110            ['u' => 'user'],
111            'u.id = feedback.user_id',
112            [],
113            $select::JOIN_LEFT
114        )->join(
115            ['m' => 'user'],
116            'm.id = feedback.updated_by',
117            [],
118            $select::JOIN_LEFT
119        )->order('created DESC');
120
121        $page = null === $page ? null : intval($page);
122        if (null !== $page) {
123            $select->limit($limit);
124            $select->offset($limit * ($page - 1));
125        }
126        $adapter = new \Laminas\Paginator\Adapter\LaminasDb\DbSelect($select, $sql);
127        $paginator = new \Laminas\Paginator\Paginator($adapter);
128        $paginator->setItemCountPerPage($limit);
129        if (null !== $page) {
130            $paginator->setCurrentPageNumber($page);
131        }
132        return $paginator;
133    }
134
135    /**
136     * Delete feedback by ids
137     *
138     * @param array $ids IDs
139     *
140     * @return int Count of deleted rows
141     */
142    public function deleteByIdArray(array $ids): int
143    {
144        // Do nothing if we have no IDs to delete!
145        if (empty($ids)) {
146            return 0;
147        }
148        $callback = function ($select) use ($ids) {
149            $select->where->in('id', $ids);
150        };
151        return $this->delete($callback);
152    }
153}