Passed
Branch master (4067a3)
by Michael
03:34
created

class/BaseObjectHandler.php (1 issue)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace XoopsModules\Publisher;
6
7
/*
8
 You may not change or alter any portion of this comment or credits
9
 of supporting developers from this source code or any supporting source code
10
 which is considered copyrighted (c) material of the original comment or credit authors.
11
12
 This program is distributed in the hope that it will be useful,
13
 but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15
 */
16
17
/**
18
 *  Publisher class
19
 *
20
 * @copyright       The XUUPS Project http://sourceforge.net/projects/xuups/
21
 * @license         http://www.fsf.org/copyleft/gpl.html GNU public license
22
 * @since           1.0
23
 * @author          trabis <[email protected]>
24
 * @author          The SmartFactory <www.smartfactory.ca>
25
 */
26
27
use XoopsModules\Publisher;
28
29
require_once \dirname(__DIR__) . '/include/common.php';
30
31
/**
32
 * BaseObjectHandler class
33
 *
34
 * @copyright       The XUUPS Project http://sourceforge.net/projects/xuups/
35
 * @license         http://www.fsf.org/copyleft/gpl.html GNU public license
36
 * @since           1.0
37
 * @author          trabis <[email protected]>
38
 * @author          Nazar Aziz <[email protected]>
39
 */
40
class BaseObjectHandler extends \XoopsPersistableObjectHandler
41
{
42
    /**
43
     * Database connection
44
     *
45
     * @var \XoopsDatabase
46
     */
47
    //mb    public $_db; //mb it is already declared in XoopsObjectHandler
48
49
    /**
50
     * Autoincrementing DB fieldname
51
     *
52
     * @var string
53
     */
54
    protected $idfield          = 'id';
55
    public    $helper           = null;
56
    public    $publisherIsAdmin = null;
57
58
    /**
59
     * @param \XoopsDatabase|null $db
60
     */
61
    public function init(\XoopsDatabase $db = null)
62
    {
63
        $this->db = $db;
64
    }
65
66
    /**
67
     * DB Table Name
68
     *
69
     * @var string
70
     */
71
    protected $dbtable = 'publisher_mimetypes';
72
73
    /**
74
     * create a new  object
75
     *
76
     * @param bool $isNew
77
     * @return \XoopsObject
78
     */
79
    public function create($isNew = true)
80
    {
81
        return new $this->className();
82
    }
83
84
    /**
85
     * retrieve an object from the database, based on. use in child classes
86
     *
87
     * @param int|null   $id ID
88
     *
89
     * @param array|null $fields
90
     * @return mixed object if id exists, false if not
91
     */
92
    public function get($id = null, $fields = null)
93
    {
94
        $id = (int)$id;
95
        if ($id > 0) {
96
            $sql = $this->selectQuery(new \Criteria($this->idfield, $id));
97
            if (!$result = $this->db->query($sql)) {
98
                return false;
99
            }
100
            $numrows = $this->db->getRowsNum($result);
101
            if (1 == $numrows) {
102
                $obj = new $this->className($this->db->fetchArray($result));
103
104
                return $obj;
105
            }
106
        }
107
108
        return false;
109
    }
110
111
    /**
112
     * retrieve objects from the database
113
     *
114
     * @param \Criteria|\CriteriaElement|null $criteria conditions to be met
115
     * @param bool                            $idAsKey  Should the department ID be used as array key
116
     *
117
     * @param bool                            $asObject
118
     * @return array array of objects
119
     */
120
    public function &getObjects($criteria = null, $idAsKey = false, $asObject = true) //&getObjects($criteria = null, $idAsKey = false)
121
    {
122
        $ret   = [];
123
        $limit = $start = 0;
124
        $sql   = $this->selectQuery($criteria);
125
        $id    = $this->idfield;
126
        if (null !== $criteria) {
127
            $limit = $criteria->getLimit();
128
            $start = $criteria->getStart();
129
        }
130
        $result = $this->db->query($sql, $limit, $start);
131
        // if no records from db, return empty array
132
        if (!$result) {
133
            return $ret;
134
        }
135
        // Add each returned record to the result array
136
        while (false !== ($myrow = $this->db->fetchArray($result))) {
137
            $obj = new $this->className($myrow);
138
            if ($idAsKey) {
139
                $ret[$obj->getVar($id)] = $obj;
140
            } else {
141
                $ret[] = $obj;
142
            }
143
            unset($obj);
144
        }
145
146
        return $ret;
147
    }
148
149
    /**
150
     * @param \XoopsObject $obj
151
     * @param bool         $force
152
     *
153
     * @return bool
154
     */
155
    public function insert(\XoopsObject $obj, $force = false)// insert($obj, $force = false)
156
    {
157
        // Make sure object is of correct type
158
        if (0 != \strcasecmp($this->className, \get_class($obj))) {
159
            return false;
160
        }
161
        // Make sure object needs to be stored in DB
162
        if (!$obj->isDirty()) {
163
            return true;
164
        }
165
        // Make sure object fields are filled with valid values
166
        if (!$obj->cleanVars()) {
167
            return false;
168
        }
169
        // Create query for DB update
170
        if ($obj->isNew()) {
171
            // Determine next auto-gen ID for table
172
            $this->db->genId($this->db->prefix($this->dbtable) . '_uid_seq');
173
            $sql = $this->insertQuery($obj);
174
        } else {
175
            $sql = $this->updateQuery($obj);
176
        }
177
        // Update DB
178
        if ($force) {
179
            $result = $this->db->queryF($sql);
180
        } else {
181
            $result = $this->db->query($sql);
182
        }
183
        if (!$result) {
184
            $obj->setErrors('The query returned an error. ' . $this->db->error());
185
186
            return false;
187
        }
188
        //Make sure auto-gen ID is stored correctly in object
189
        if ($obj->isNew()) {
190
            $obj->assignVar($this->idfield, $this->db->getInsertId());
191
        }
192
193
        return true;
194
    }
195
196
    /**
197
     * Create a "select" SQL query
198
     *
199
     * @param \Criteria|null $criteria {@link \Criteria} to match
200
     *
201
     * @return string SQL query
202
     */
203
    private function selectQuery(\Criteria $criteria = null)
204
    {
205
        $sql = \sprintf('SELECT * FROM `%s`', $this->db->prefix($this->dbtable));
206
        if (null !== $criteria && $criteria instanceof \Criteria) {
207
            $sql .= ' ' . $criteria->renderWhere();
208
            if ('' != $criteria->getSort()) {
209
                $sql .= ' ORDER BY ' . $criteria->getSort() . '
210
                    ' . $criteria->getOrder();
211
            }
212
        }
213
214
        return $sql;
215
    }
216
217
    /**
218
     * count objects matching a criteria
219
     *
220
     * @param \CriteriaElement|null $criteria {@link CriteriaElement}                                                  to match
221
     *
222
     * @return int count of objects
223
     */
224
    public function getCount(\CriteriaElement $criteria = null) //getCount($criteria = null)
225
    {
226
        $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix($this->dbtable);
227
        if (null !== $criteria && $criteria instanceof \CriteriaElement) {
228
            $sql .= ' ' . $criteria->renderWhere();
0 ignored issues
show
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

228
            $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...
229
        }
230
        if (!$result = $this->db->query($sql)) {
231
            return 0;
232
        }
233
        [$count] = $this->db->fetchRow($result);
234
235
        return $count;
236
    }
237
238
    /**
239
     * delete object based on id
240
     *
241
     * @param \XoopsObject $obj   {@link XoopsObject}
242
     *                            to delete
243
     * @param bool         $force override XOOPS delete protection
244
     *
245
     * @return bool deletion successful?
246
     */
247
    public function delete(\XoopsObject $obj, $force = false) //delete($obj, $force = false)
248
    {
249
        if (0 != \strcasecmp($this->className, \get_class($obj))) {
250
            return false;
251
        }
252
        $sql = $this->deleteQuery($obj);
253
        if ($force) {
254
            $result = $this->db->queryF($sql);
255
        } else {
256
            $result = $this->db->query($sql);
257
        }
258
        if (!$result) {
259
            return false;
260
        }
261
262
        return true;
263
    }
264
265
    /**
266
     * delete department matching a set of conditions
267
     *
268
     * @param \CriteriaElement|null $criteria {@link CriteriaElement}
269
     *
270
     * @param bool                  $force
271
     * @param bool                  $asObject
272
     * @return bool FALSE if deletion failed
273
     */
274
    public function deleteAll(\CriteriaElement $criteria = null, $force = true, $asObject = false) //deleteAll($criteria = null)
275
    {
276
        $sql = 'DELETE FROM ' . $this->db->prefix($this->dbtable);
277
        if (null !== $criteria && $criteria instanceof \CriteriaElement) {
278
            $sql .= ' ' . $criteria->renderWhere();
279
        }
280
        if (!$result = $this->db->query($sql)) {
281
            return false;
282
        }
283
284
        return true;
285
    }
286
287
    /**
288
     * Assign a value to 1 field for tickets matching a set of conditions
289
     *
290
     * @param string                $fieldname
291
     * @param string                $fieldvalue
292
     * @param \Criteria|\CriteriaCompo|null $criteria
293
     *
294
     * @param bool                  $force
295
     * @return bool FALSE if update failed
296
     */
297
    public function updateAll($fieldname, $fieldvalue, $criteria = null, $force = false) //updateAll($fieldname, $fieldvalue, $criteria = null)
298
    {
299
        $setClause = \is_numeric($fieldvalue) ? $fieldname . ' = ' . $fieldvalue : $fieldname . ' = ' . $this->db->quoteString($fieldvalue);
300
        $sql       = 'UPDATE ' . $this->db->prefix($this->dbtable) . ' SET ' . $setClause;
301
        if (null !== $criteria && $criteria instanceof \CriteriaElement) {
302
            $sql .= ' ' . $criteria->renderWhere();
303
        }
304
        if (!$result = $this->db->query($sql)) {
305
            return false;
306
        }
307
308
        return true;
309
    }
310
311
    /**
312
     * @param $obj
313
     *
314
     * @return bool
315
     */
316
    protected function insertQuery($obj)
317
    {
318
        return false;
319
    }
320
321
    /**
322
     * @param $obj
323
     *
324
     * @return bool|string
325
     */
326
    protected function updateQuery($obj)
327
    {
328
        return false;
329
    }
330
331
    /**
332
     * @param $obj
333
     *
334
     * @return bool
335
     */
336
    protected function deleteQuery($obj)
337
    {
338
        return false;
339
    }
340
341
    /**
342
     * Singleton - prevent multiple instances of this class
343
     *
344
     *
345
     * @param \XoopsDatabase|null $db
346
     * @return \XoopsObject {@link pagesCategoryHandler}
347
     */
348
    public function getInstance(\XoopsDatabase $db = null)
349
    {
350
        static $instance;
351
        if (null === $instance) {
352
            $className = $this->className . 'Handler';
353
            $instance  = new $className($db);
354
        }
355
356
        return $instance;
357
    }
358
}
359