SavedSearchHandler::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
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
 * SavedSearchHandler class
29
 *
30
 * SavedSearch Handler for SavedSearch class
31
 *
32
 * @author  Eric Juden <[email protected]> &
33
 */
34
class SavedSearchHandler extends BaseObjectHandler
35
{
36
    /**
37
     * Name of child class
38
     *
39
     * @var string
40
     */
41
    public $classname = SavedSearch::class;
42
    /**
43
     * DB table name
44
     *
45
     * @var string
46
     */
47
    public $dbtable = 'xhelp_saved_searches';
48
49
    private const TABLE = 'xhelp_saved_searches';
50
    private const ENTITY = SavedSearch::class;
51
    private const ENTITYNAME = 'SavedSearch';
52
    private const KEYNAME = 'id';
53
    private const IDENTIFIER = 'name';
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, NAME, search, pagenav_vars, hasCustFields) VALUES (%u, %d, %s, %s, %s, %u)', $this->db->prefix($this->dbtable), $id, $uid, $this->db->quoteString($name), $this->db->quoteString($search), $this->db->quoteString($pagenav_vars), $hasCustFields);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $pagenav_vars 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 $name 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 $hasCustFields seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $search 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, NAME = %s, search = %s, pagenav_vars = %s, hasCustFields = %u WHERE id = %u', $this->db->prefix($this->dbtable), $uid, $this->db->quoteString($name), $this->db->quoteString($search), $this->db->quoteString($pagenav_vars), $hasCustFields, $id);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $hasCustFields seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $search 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 $pagenav_vars 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 $name 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
    /**
113
     * @param int  $uid
114
     * @param bool $has_global
115
     * @return array
116
     */
117
    public function getByUid(int $uid, bool $has_global = false): array
118
    {
119
        $uid = $uid;
120
        if ($has_global) {
121
            $criteria = new \CriteriaCompo(new \Criteria('uid', $uid), 'OR');
122
            $criteria->add(new \Criteria('uid', \XHELP_GLOBAL_UID), 'OR');
123
        } else {
124
            $criteria = new \Criteria('uid', $uid);
125
        }
126
        $criteria->setOrder('ASC');
127
        $criteria->setSort('name');
128
        $ret = $this->getObjects($criteria);
129
130
        return $ret;
131
    }
132
133
    /**
134
     * @param \CriteriaElement|\CriteriaCompo $criteria
135
     * @return string
136
     */
137
    public function createSQL($criteria): string
138
    {
139
        $sql = $this->selectQuery($criteria);
140
141
        return $sql;
142
    }
143
144
    /**
145
     * delete department matching a set of conditions
146
     *
147
     * @param \CriteriaElement|\CriteriaCompo|null $criteria {@link \CriteriaElement}
148
     * @return bool   FALSE if deletion failed
149
     */
150
    public function deleteAll(\CriteriaElement $criteria = null, $force = true, $asObject = false): bool
151
    {
152
        $sql = 'DELETE FROM ' . $this->db->prefix($this->dbtable);
153
        if (($criteria instanceof \CriteriaCompo) || ($criteria instanceof \Criteria)) {
154
            $sql .= ' ' . $criteria->renderWhere();
155
        }
156
        if (!$result = $this->db->query($sql)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
157
            return false;
158
        }
159
160
        return true;
161
    }
162
}
163