TicketListHandler   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 201
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 85
c 0
b 0
f 0
dl 0
loc 201
rs 10
wmc 20

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getListsByUser() 0 9 1
A changeWeight() 0 24 5
A deleteQuery() 0 5 1
A getObject() 0 24 4
A __construct() 0 5 1
A createNewWeight() 0 13 2
A insertQuery() 0 11 2
A createStaffGlobalLists() 0 22 2
A updateQuery() 0 11 2
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Xhelp;
4
5
/*
6
 * You may not change or alter any portion of this comment or credits
7
 * of supporting developers from this source code or any supporting source code
8
 * which is considered copyrighted (c) material of the original comment or credit authors.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
 */
14
15
/**
16
 * @copyright    {@link https://xoops.org/ XOOPS Project}
17
 * @license      {@link https://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2 or later}
18
 * @author       Eric Juden <[email protected]>
19
 * @author       XOOPS Development Team
20
 */
21
22
if (!\defined('XHELP_CLASS_PATH')) {
23
    exit();
24
}
25
// require_once XHELP_CLASS_PATH . '/BaseObjectHandler.php';
26
27
/**
28
 * TicketListHandler class
29
 *
30
 * TicketList Handler for TicketList class
31
 *
32
 * @author  Eric Juden <[email protected]> &
33
 */
34
class TicketListHandler extends BaseObjectHandler
35
{
36
    /**
37
     * Name of child class
38
     *
39
     * @var string
40
     */
41
    public $classname = TicketList::class;
42
    /**
43
     * DB table name
44
     *
45
     * @var string
46
     */
47
    public $dbtable = 'xhelp_ticket_lists';
48
49
    private const TABLE = 'xhelp_ticket_lists';
50
    private const ENTITY = TicketList::class;
51
    private const ENTITYNAME = 'TicketList';
52
    private const KEYNAME = 'id';
53
    private const IDENTIFIER = 'uid';
54
55
    /**
56
     * Constructor
57
     *
58
     * @param \XoopsMySQLDatabase|null $db reference to a xoopsDB object
59
     */
60
    public function __construct(\XoopsMySQLDatabase $db = null)
61
    {
62
        $this->init($db);
63
        $this->helper = Helper::getInstance();
64
        parent::__construct($db, static::TABLE, static::ENTITY, static::KEYNAME, static::IDENTIFIER);
65
    }
66
67
    /**
68
     * @param \XoopsObject $object
69
     * @return string
70
     */
71
    public function insertQuery(\XoopsObject $object): string
72
    {
73
        //TODO mb replace with individual variables
74
        // Copy all object vars into local variables
75
        foreach ($object->cleanVars as $k => $v) {
76
            ${$k} = $v;
77
        }
78
79
        $sql = \sprintf('INSERT INTO `%s` (id, uid, searchid, weight) VALUES (%u, %d, %u, %u)', $this->db->prefix($this->dbtable), $id, $uid, $searchid, $weight);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $id seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $uid seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $searchid seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $weight seems to be never defined.
Loading history...
80
81
        return $sql;
82
    }
83
84
    /**
85
     * @param \XoopsObject $object
86
     * @return string
87
     */
88
    public function updateQuery(\XoopsObject $object): string
89
    {
90
        //TODO mb replace with individual variables
91
        // Copy all object vars into local variables
92
        foreach ($object->cleanVars as $k => $v) {
93
            ${$k} = $v;
94
        }
95
96
        $sql = \sprintf('UPDATE `%s` SET uid = %d, searchid = %u, weight = %u WHERE id = %u', $this->db->prefix($this->dbtable), $uid, $searchid, $weight, $id);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $uid seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $id seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $searchid seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $weight seems to be never defined.
Loading history...
97
98
        return $sql;
99
    }
100
101
    /**
102
     * @param \XoopsObject $object
103
     * @return string
104
     */
105
    public function deleteQuery(\XoopsObject $object): string
106
    {
107
        $sql = \sprintf('DELETE FROM `%s` WHERE id = %u', $this->db->prefix($this->dbtable), $object->getVar('id'));
0 ignored issues
show
Bug introduced by
It seems like $object->getVar('id') can also be of type array and array; however, parameter $values of sprintf() does only seem to accept double|integer|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

107
        $sql = \sprintf('DELETE FROM `%s` WHERE id = %u', $this->db->prefix($this->dbtable), /** @scrutinizer ignore-type */ $object->getVar('id'));
Loading history...
108
109
        return $sql;
110
    }
111
112
    // Weight of last ticketList(from staff) and +1
113
114
    /**
115
     * @param int $uid
116
     * @return int
117
     */
118
    public function createNewWeight(int $uid): int
119
    {
120
        $uid = $uid;
121
122
        $criteria = new \CriteriaCompo(new \Criteria('uid', $uid), 'OR');
123
        $criteria->add(new \Criteria('uid', \XHELP_GLOBAL_UID), 'OR');
124
        $criteria->setSort('weight');
125
        $criteria->setOrder('desc');
126
        $criteria->setLimit(1);
127
        $ticketList = $this->getObjects($criteria);
128
        $weight     = (\is_object($ticketList[0]) ? $ticketList[0]->getVar('weight') : 0);
129
130
        return $weight + 1;
131
    }
132
133
    /**
134
     * @param int  $listID
135
     * @param bool $up
136
     * @return bool
137
     */
138
    public function changeWeight(int $listID, bool $up = true): ?bool
139
    {
140
        $listID           = $listID;
141
        $ticketList       = $this->get($listID);     // Get ticketList being changed
142
        $origTicketWeight = $ticketList->getVar('weight');
143
        $criteria         = new \Criteria('weight', $origTicketWeight, ($up ? '<' : '>'));
0 ignored issues
show
Bug introduced by
It seems like $origTicketWeight can also be of type array and array; however, parameter $value of Criteria::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

143
        $criteria         = new \Criteria('weight', /** @scrutinizer ignore-type */ $origTicketWeight, ($up ? '<' : '>'));
Loading history...
144
        $criteria->setSort('weight');
145
        $criteria->setOrder($up ? 'DESC' : 'ASC');
146
        $criteria->setLimit(1);
147
148
        /** @var \XoopsModules\Xhelp\TicketList $changeTicketList */
149
        $changeTicketList = $this->getObject($criteria);               // Get ticketList being changed with
150
        $newTicketWeight  = $changeTicketList->getVar('weight');
151
152
        $ticketList->setVar('weight', $newTicketWeight);
153
        if ($this->insert($ticketList, true)) {      // If first one succeeds, change 2nd number
154
            $changeTicketList->setVar('weight', $origTicketWeight);
155
            if (!$this->insert($changeTicketList, true)) {
156
                return false;
157
            }
158
        } else {
159
            return false;
160
        }
161
        return true;
162
    }
163
164
    /**
165
     * @param \CriteriaElement|\CriteriaCompo|null $criteria
166
     * @return array|bool
167
     */
168
    public function getObject($criteria = null)
169
    {
170
        $ret   = [];
171
        $limit = $start = 0;
172
        $sql   = $this->selectQuery($criteria);
173
        $id    = $this->idfield;
0 ignored issues
show
Unused Code introduced by
The assignment to $id is dead and can be removed.
Loading history...
174
175
        if (null !== $criteria) {
176
            $limit = $criteria->getLimit();
177
            $start = $criteria->getStart();
178
        }
179
        $result = $this->db->query($sql, $limit, $start);
180
        // If no records from db, return empty array
181
        if (!$result) {
182
            return $ret;
183
        }
184
        $numrows = $this->db->getRowsNum($result);
185
        if (1 == $numrows) {
186
            $object = new $this->classname($this->db->fetchArray($result));
187
188
            return $object;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $object returns the type object which is incompatible with the documented return type array|boolean.
Loading history...
189
        }
190
191
        return false;
192
    }
193
194
    /**
195
     * @param int $uid
196
     * @return array
197
     */
198
    public function &getListsByUser(int $uid): array
199
    {
200
        $uid      = $uid;
201
        $criteria = new \CriteriaCompo(new \Criteria('uid', $uid), 'OR');
202
        $criteria->add(new \Criteria('uid', \XHELP_GLOBAL_UID), 'OR');
203
        $criteria->setSort('weight');
204
        $ret = $this->getObjects($criteria);
205
206
        return $ret;
207
    }
208
209
    /**
210
     * @param int $uid
211
     * @return bool
212
     */
213
    public function createStaffGlobalLists(int $uid): bool
214
    {
215
        $ret            = false;
216
        $hSavedSearches = $this->helper->getHandler('SavedSearch');
217
        $uid            = $uid;
218
219
        $criteria = new \Criteria('uid', \XHELP_GLOBAL_UID);
220
        $criteria->setSort('id');
221
        $criteria->setOrder('ASC');
222
        $globalSearches = $hSavedSearches->getObjects($criteria, true);
223
        $i              = 1;
224
        foreach ($globalSearches as $search) {
225
            /** @var \XoopsModules\Xhelp\TicketList $list */
226
            $list = $this->create();
227
            $list->setVar('uid', $uid);
228
            $list->setVar('searchid', $search->getVar('id'));
229
            $list->setVar('weight', $i);
230
            $ret = $this->insert($list, true);
231
            ++$i;
232
        }
233
234
        return $ret;
235
    }
236
}
237