ColumnsHandler   A
last analyzed

Complexity

Total Complexity 32

Size/Duplication

Total Lines 201
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 87
dl 0
loc 201
rs 9.84
c 0
b 0
f 0
wmc 32

7 Methods

Rating   Name   Duplication   Size   Complexity  
A updateByField() 0 8 2
A get() 0 18 4
B getObjects() 0 30 7
B insert() 0 37 9
A create() 0 8 2
A getCount() 0 13 4
A delete() 0 16 4
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
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 ColumnsHandler
30
 */
31
class ColumnsHandler extends \XoopsPersistableObjectHandler
32
{
33
    public $totalarts_AllPermcheck;
34
35
    /**
36
     * create a new category
37
     *
38
     * @param  bool $isNew flag the new objects as "new"?
39
     * @return object Columns
40
     */
41
    public function create($isNew = true)
42
    {
43
        $sbcolumn = new Columns();
44
        if ($isNew) {
45
            $sbcolumn->setNew();
46
        }
47
48
        return $sbcolumn;
49
    }
50
51
    /**
52
     * retrieve a category
53
     *
54
     * @param  mixed|null $id
55
     * @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...
56
     * @return mixed      reference to the <a href='psi_element://Columns'>Columns</a> object, FALSE if failed
57
     *                           object, FALSE if failed
58
     *                           object, FALSE if failed
59
     * @internal param int $columnID columnID of the category
60
     */
61
    public function get($id = null, $fields = null) //&get($id)
62
    {
63
        $ret = false;
64
        if ((int)$id > 0) {
65
            $sql = 'SELECT * FROM ' . $this->db->prefix('sbcolumns') . " WHERE columnID = '$id'";
66
            if (!$result = $this->db->query($sql)) {
67
                return $ret;
68
            }
69
            $numrows = $this->db->getRowsNum($result);
70
            if (1 === $numrows) {
71
                $sbcolumn = new Columns();
72
                $sbcolumn->assignVars($this->db->fetchArray($result));
73
74
                return $sbcolumn;
75
            }
76
        }
77
78
        return $ret;
79
    }
80
81
    /**
82
     * retrieve categorys from the database
83
     *
84
     * @param  \CriteriaElement $criteria  {@link CriteriaElement} conditions to be match
85
     * @param  bool             $id_as_key use the columnID as key for the array?
86
     * @param  bool             $as_object
87
     * @return array           array of <a href='psi_element://Columns'>Columns</a> objects
88
     *                                     objects
89
     */
90
    public function &getObjects(\CriteriaElement $criteria = null, $id_as_key = false, $as_object = true) //&getObjects($criteria = null, $id_as_key = false)
91
    {
92
        $ret   = [];
93
        $limit = $start = 0;
94
        $sql   = 'SELECT * FROM ' . $this->db->prefix('sbcolumns');
95
        if (isset($criteria) && $criteria instanceof \CriteriaElement) {
96
            $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

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

181
        $sql = sprintf('DELETE FROM `%s` WHERE columnID = %u', $this->db->prefix('sbcolumns'), /** @scrutinizer ignore-type */ $sbcolumn->getVar('columnID'));
Loading history...
182
        if ($force) {
183
            $result = $this->db->queryF($sql);
184
        } else {
185
            $result = $this->db->query($sql);
186
        }
187
        if (!$result) {
188
            return false;
189
        }
190
191
        return true;
192
    }
193
194
    /**
195
     * count categorys matching a condition
196
     *
197
     * @param  \CriteriaElement $criteria {@link CriteriaElement} to match
198
     * @return int             count of categorys
199
     */
200
    public function getCount(\CriteriaElement $criteria = null)//getCount($criteria = null)
201
    {
202
        $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('sbcolumns');
203
        if (isset($criteria) && $criteria instanceof \CriteriaElement) {
204
            $sql .= ' ' . $criteria->renderWhere();
205
        }
206
        $result = $this->db->query($sql);
207
        if (!$result) {
208
            return 0;
209
        }
210
        list($count) = $this->db->fetchRow($result);
211
212
        return $count;
213
    }
214
215
    /**
216
     * updates a single field in a Column record
217
     *
218
     * @param  object $entry      reference to the {@link Columns} object
219
     * @param  string $fieldName  name of the field to update
220
     * @param  string $fieldValue updated value for the field
221
     * @param  bool   $force
222
     * @return bool   TRUE if success or unchanged, FALSE on failure
223
     */
224
    public function updateByField($entry, $fieldName, $fieldValue, $force = false)
225
    {
226
        if (mb_strtolower(get_class($entry)) !== mb_strtolower(Columns::class)) {
227
            return false;
228
        }
229
        $entry->setVar($fieldName, $fieldValue);
230
231
        return $this->insert($entry, $force);
232
    }
233
}
234