VotedataHandler::getCount()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 4
nop 1
dl 0
loc 14
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace XoopsModules\Soapbox;
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 http://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2 or later}
18
 * @package
19
 * @since
20
 * @author         XOOPS Development Team, Jan Pedersen (Mithrandir)
21
 */
22
23
use XoopsModules\Soapbox;
24
25
// defined('XOOPS_ROOT_PATH') || die('Restricted access');
26
//require_once XOOPS_ROOT_PATH . '/modules/soapbox/include/cleantags.php';
27
28
/**
29
 * Class VotedataHandler
30
 */
31
class VotedataHandler extends \XoopsPersistableObjectHandler
32
{
33
    /**
34
     * create a new entry
35
     *
36
     * @param  bool $isNew flag the new objects as "new"?
37
     * @return object Votedata
38
     */
39
    public function create($isNew = true)
40
    {
41
        $entry = new Votedata();
42
        if ($isNew) {
43
            $entry->setNew();
44
        }
45
46
        return $entry;
47
    }
48
49
    /**
50
     * retrieve a entry
51
     *
52
     * @param  mixed|null $id
53
     * @param  null       $fields
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $fields is correct as it would always require null to be passed?
Loading history...
54
     * @return mixed      reference to the <a href='psi_element://Entry'>Entry</a> object, FALSE if failed
55
     *                           object, FALSE if failed
56
     *                           object, FALSE if failed
57
     * @internal param int $ratingid ratingid of the entry
58
     */
59
    public function get($id = null, $fields = null)
60
    {
61
        $ret = false;
62
        if ((int)$id > 0) {
63
            $sql = 'SELECT * FROM ' . $this->db->prefix('sbvotedata') . " WHERE ratingid = '$id'";
64
            if (!$result = $this->db->query($sql)) {
65
                return $ret;
66
            }
67
            $numrows = $this->db->getRowsNum($result);
68
            if (1 === $numrows) {
69
                $entry = new Votedata();
70
                $entry->assignVars($this->db->fetchArray($result));
71
72
                return $entry;
73
            }
74
        }
75
76
        return $ret;
77
    }
78
79
    /**
80
     * retrieve entrys from the database
81
     *
82
     * @param  \CriteriaElement $criteria  {@link CriteriaElement} conditions to be match
83
     * @param  bool             $id_as_key use the ratingid as key for the array?
84
     * @param  bool             $as_object
85
     * @return array           array of <a href='psi_element://Votedata'>Votedata</a> objects
86
     *                                     objects
87
     */
88
    public function &getObjects(\CriteriaElement $criteria = null, $id_as_key = false, $as_object = true)
89
    {
90
        $ret   = [];
91
        $limit = $start = 0;
92
        $sql   = 'SELECT * FROM ' . $this->db->prefix('sbvotedata');
93
        if (isset($criteria) && $criteria instanceof \CriteriaElement) {
94
            $sql .= ' ' . $criteria->renderWhere();
0 ignored issues
show
Bug introduced by
The method renderWhere() does not exist on CriteriaElement. Did you maybe mean render()? ( Ignorable by Annotation )

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

94
            $sql .= ' ' . $criteria->/** @scrutinizer ignore-call */ renderWhere();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
95
            if ('' !== $criteria->getSort()) {
96
                $sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder();
97
            }
98
            $limit = $criteria->getLimit();
99
            $start = $criteria->getStart();
100
        }
101
        $result = $this->db->query($sql, $limit, $start);
102
        if (!$result) {
103
            return $ret;
104
        }
105
        while (false !== ($myrow = $this->db->fetchArray($result))) {
106
            $entry = new Votedata();
107
            $entry->assignVars($myrow);
108
            if (!$id_as_key) {
109
                $ret[] = $entry;
110
            } else {
111
                $ret[$myrow['ratingid']] = $entry;
112
            }
113
            unset($entry);
114
        }
115
116
        return $ret;
117
    }
118
119
    /**
120
     * insert a new entry in the database
121
     *
122
     * @param \XoopsObject $entry        reference to the {@link Votedata}
123
     *                                   object
124
     * @param  bool        $force
125
     * @return bool               FALSE if failed, TRUE if already present and unchanged or successful
126
     */
127
    public function insert(\XoopsObject $entry, $force = false)
128
    {
129
        if (mb_strtolower(get_class($entry)) !== mb_strtolower(Votedata::class)) {
130
            return false;
131
        }
132
        if (!$entry->isDirty()) {
133
            return true;
134
        }
135
        if (!$entry->cleanVars()) {
136
            return false;
137
        }
138
        foreach ($entry->cleanVars as $k => $v) {
139
            ${$k} = $v;
140
        }
141
        // RMV-NOTIFY
142
        if ($entry->isNew()) {
143
            $ratingid = $this->db->genId($this->db->prefix('sbvotedata') . '_ratingid_seq');
144
            $sql      = sprintf('INSERT INTO `%s` (ratingid, lid, ratinguser, rating, ratinghostname, ratingtimestamp) VALUES (%u, %u, %u, %u, %s, %u)', $this->db->prefix('sbvotedata'), $ratingid, $lid, $ratinguser, $rating, $this->db->quoteString($ratinghostname), $ratingtimestamp);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $ratingtimestamp seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $ratinguser does not exist. Did you maybe mean $ratingid?
Loading history...
Comprehensibility Best Practice introduced by
The variable $ratinghostname seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $rating does not exist. Did you maybe mean $ratingid?
Loading history...
Comprehensibility Best Practice introduced by
The variable $lid seems to be never defined.
Loading history...
145
        } else {
146
            $sql = sprintf('UPDATE `%s` SET lid = %u, ratinguser = %u, rating = %u, ratinghostname = %s, ratingtimestamp = %uratingtimestamp WHERE ratingid = %u', $this->db->prefix('sbvotedata'), $ratinguser, $rating, $this->db->quoteString($ratinghostname), $ratingtimestamp, $ratingid);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $ratingid seems to be never defined.
Loading history...
147
        }
148
        if ($force) {
149
            $result = $this->db->queryF($sql);
150
        } else {
151
            $result = $this->db->query($sql);
152
        }
153
        if (!$result) {
154
            return false;
155
        }
156
        if (empty($ratingid)) {
157
            $ratingid = $this->db->getInsertId();
158
        }
159
        $entry->assignVar('ratingid', $ratingid);
160
161
        return true;
162
    }
163
164
    /**
165
     * delete a entry from the database
166
     *
167
     * @param \XoopsObject $entry reference to the entry to delete
168
     * @param  bool        $force
169
     * @return bool        FALSE if failed.
170
     */
171
    public function delete(\XoopsObject $entry, $force = false)
172
    {
173
        if (mb_strtolower(get_class($entry)) !== mb_strtolower(Votedata::class)) {
174
            return false;
175
        }
176
        $sql = sprintf('DELETE FROM `%s` WHERE ratingid = %u', $this->db->prefix('sbvotedata'), $entry->getVar('ratingid'));
0 ignored issues
show
Bug introduced by
It seems like $entry->getVar('ratingid') can also be of type array and array; however, parameter $args of sprintf() 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

176
        $sql = sprintf('DELETE FROM `%s` WHERE ratingid = %u', $this->db->prefix('sbvotedata'), /** @scrutinizer ignore-type */ $entry->getVar('ratingid'));
Loading history...
177
        if ($force) {
178
            $result = $this->db->queryF($sql);
179
        } else {
180
            $result = $this->db->query($sql);
181
        }
182
        if (!$result) {
183
            return false;
184
        }
185
186
        return true;
187
    }
188
189
    /**
190
     * delete entry from the database
191
     *
192
     * @param  object $criteria {@link CriteriaElement} conditions to be match
193
     * @param  bool   $force
194
     * @return bool   FALSE if failed.
195
     */
196
    public function deleteEntrys($criteria = null, $force = false)
197
    {
198
        $sql = sprintf('DELETE FROM `%s` ', $this->db->prefix('sbvotedata'));
199
        if (isset($criteria) && $criteria instanceof \CriteriaElement) {
200
            $sql .= ' ' . $criteria->renderWhere();
201
        }
202
        if ($force) {
203
            $result = $this->db->queryF($sql);
204
        } else {
205
            $result = $this->db->query($sql);
206
        }
207
        if (!$result) {
208
            return false;
209
        }
210
211
        return true;
212
    }
213
214
    /**
215
     * count entrys matching a condition
216
     *
217
     * @param  \CriteriaElement $criteria {@link CriteriaElement} to match
218
     * @return int             count of entrys
219
     */
220
    public function getCount(\CriteriaElement $criteria = null)
221
    {
222
        $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('sbvotedata');
223
224
        if (isset($criteria) && $criteria instanceof \CriteriaElement) {
225
            $sql .= ' ' . $criteria->renderWhere();
226
        }
227
        $result = $this->db->query($sql);
228
        if (!$result) {
229
            return 0;
230
        }
231
        list($count) = $this->db->fetchRow($result);
232
233
        return $count;
234
    }
235
236
    /**
237
     * updates a single field in a Votedata record
238
     *
239
     * @param  object $entry      reference to the {@link Votedata} object
240
     * @param  string $fieldName  name of the field to update
241
     * @param  string $fieldValue updated value for the field
242
     * @param  bool   $force
243
     * @return bool   TRUE if success or unchanged, FALSE on failure
244
     */
245
    public function updateByField($entry, $fieldName, $fieldValue, $force = false)
246
    {
247
        if (mb_strtolower(get_class($entry)) !== mb_strtolower(Votedata::class)) {
248
            return false;
249
        }
250
        $entry->setVar($fieldName, $fieldValue);
251
252
        return $this->insert($entry, $force);
253
    }
254
}
255