GenreHandler::insert()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 12
rs 10
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Songlist;
4
5
require_once \dirname(__DIR__) . '/include/songlist.object.php';
6
// require_once \dirname(__DIR__) . '/include/songlist.form.php';
7
use  XoopsModules\Songlist\Form\FormController;
8
9
/**
10
 * Class GenreHandler
11
 */
12
class GenreHandler extends \XoopsPersistableObjectHandler
13
{
14
    /**
15
     * GenreHandler constructor.
16
     * @param \XoopsDatabase $db
17
     */
18
    public function __construct(\XoopsDatabase $db)
19
    {
20
        parent::__construct($db, 'songlist_genre', Genre::class, 'gid', 'name');
21
    }
22
23
    /**
24
     * @return array
25
     */
26
    public function filterFields(): array
27
    {
28
        return ['gid', 'name', 'artists', 'albums', 'songs', 'hits', 'rank', 'votes', 'created', 'updated'];
29
    }
30
31
    /**
32
     * @param $filter
33
     * @return \CriteriaCompo
34
     */
35
    public function getFilterCriteria($filter): \CriteriaCompo
36
    {
37
        $parts    = \explode('|', $filter);
38
        $criteria = new \CriteriaCompo();
39
        foreach ($parts as $part) {
40
            $var = \explode(',', $part);
41
            if (!empty($var[1]) && !\is_numeric($var[0])) {
42
                $object = $this->create();
43
                if (\XOBJ_DTYPE_TXTBOX == $object->vars[$var[0]]['data_type']
44
                    || \XOBJ_DTYPE_TXTAREA == $object->vars[$var[0]]['data_type']) {
45
                    $criteria->add(new \Criteria('`' . $var[0] . '`', '%' . $var[1] . '%', ($var[2] ?? 'LIKE')));
46
                } elseif (in_array($object->vars[$var[0]]['data_type'], [XOBJ_DTYPE_INT, XOBJ_DTYPE_DECIMAL, XOBJ_DTYPE_FLOAT])) {
47
                    $criteria->add(new \Criteria('`' . $var[0] . '`', $var[1], ($var[2] ?? '=')));
48
                } elseif (\XOBJ_DTYPE_ENUM == $object->vars[$var[0]]['data_type']) {
49
                    $criteria->add(new \Criteria('`' . $var[0] . '`', $var[1], ($var[2] ?? '=')));
50
                } elseif (\XOBJ_DTYPE_ARRAY == $object->vars[$var[0]]['data_type']) {
51
                    $criteria->add(new \Criteria('`' . $var[0] . '`', '%"' . $var[1] . '";%', ($var[2] ?? 'LIKE')));
52
                }
53
            } elseif (!empty($var[1]) && \is_numeric($var[0])) {
54
                $criteria->add(new \Criteria($var[0], $var[1]));
55
            }
56
        }
57
58
        return $criteria;
59
    }
60
61
    /**
62
     * @param        $filter
63
     * @param        $field
64
     * @param string $sort
65
     * @param string $op
66
     * @param string $fct
67
     * @return string
68
     */
69
    public function getFilterForm($filter, $field, $sort = 'created', $op = 'dashboard', $fct = 'list'): string
70
    {
71
        $ele = Utility::getFilterElement($filter, $field, $sort, $op, $fct);
72
        if (\is_object($ele)) {
73
            return $ele->render();
74
        }
75
76
        return '&nbsp;';
77
    }
78
79
    /**
80
     * @param bool $force
81
     * @return bool|mixed
82
     */
83
    public function insert(\XoopsObject $obj, $force = true)
84
    {
85
        if ($obj->isNew()) {
86
            $obj->setVar('created', \time());
87
        } else {
88
            $obj->setVar('updated', \time());
89
        }
90
        if ('' == $obj->getVar('name')) {
91
            return false;
92
        }
93
94
        return parent::insert($obj, $force);
95
    }
96
97
    public $_objects = ['object' => [], 'array' => []];
98
99
    /**
100
     * @param null $id
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $id is correct as it would always require null to be passed?
Loading history...
101
     * @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...
102
     * @return \XoopsObject
103
     */
104
    public function get($id = null, $fields = null): \XoopsObject//get($id, $fields = '*')
105
    {
106
        $fields = $fields ?: '*';
0 ignored issues
show
introduced by
$fields is of type null, thus it always evaluated to false.
Loading history...
107
        if (!isset($this->_objects['object'][$id])) {
108
            $this->_objects['object'][$id] = parent::get($id, $fields);
0 ignored issues
show
Bug introduced by
$fields of type string is incompatible with the type array expected by parameter $fields of XoopsPersistableObjectHandler::get(). ( Ignorable by Annotation )

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

108
            $this->_objects['object'][$id] = parent::get($id, /** @scrutinizer ignore-type */ $fields);
Loading history...
109
            if (!isset($GLOBALS['songlistAdmin']) && \is_object($this->_objects['object'][$id])) {
110
                $sql = 'UPDATE `' . $this->table . '` set hits=hits+1 where `' . $this->keyName . '` = ' . $this->_objects['object'][$id]->getVar($this->keyName);
111
                $GLOBALS['xoopsDB']->queryF($sql);
112
            }
113
        }
114
115
        return $this->_objects['object'][$id];
116
    }
117
118
    /**
119
     * @param \CriteriaElement|\CriteriaCompo $criteria
120
     * @param bool $id_as_key
121
     * @param bool $as_object
122
     * @return array
123
     */
124
    public function &getObjects($criteria = null, $id_as_key = false, $as_object = true): array
125
    {
126
        $ret = parent::getObjects($criteria, $id_as_key, $as_object);
127
128
        /*if (!isset($GLOBALS['songlistAdmin'])) {
129
            $id = [];
130
            foreach($ret as $data) {
131
                if ($as_object==true) {
132
                    if (!in_array($data->getVar($this->keyName), array_keys($this->_objects['object']))) {
133
                        $this->_objects['object'][$data->getVar($this->keyName)] = $data;
134
                        $id[$data->getVar($this->keyName)] = $data->getVar($this->keyName);
135
                    }
136
                } else {
137
                    if (!in_array($data[$this->keyName], array_keys($this->_objects['array']))) {
138
                        $this->_objects['array'][$data[$this->keyName]] = $data;
139
                        $id[$data[$this->keyName]] = $data[$this->keyName];;
140
                    }
141
                }
142
            }
143
        }
144
        if (!isset($GLOBALS['songlistAdmin'])&&count($id)>0) {
145
            $sql = 'UPDATE `'.$this->table.'` set hits=hits+1 where `'.$this->keyName.'` IN ('.implode(',', $id).')';
146
            $GLOBALS['xoopsDB']->queryF($sql);
147
        }*/
148
149
        return $ret;
150
    }
151
152
    /**
153
     * @return string
154
     */
155
    public function getURL(): string
156
    {
157
        global $file, $op, $fct, $id, $value, $gid, $cid, $start, $limit;
158
        if ($GLOBALS['songlistModuleConfig']['htaccess']) {
159
            return XOOPS_URL . '/' . $GLOBALS['songlistModuleConfig']['baseurl'] . '/' . $file . '/' . $start . '-' . $op . '-' . $fct . '-' . $id . '-' . \urlencode($value) . '-' . $gid . '-' . $cid . $GLOBALS['songlistModuleConfig']['endofurl'];
160
        }
161
162
        return XOOPS_URL . '/modules/songlist/' . $file . '.php?op=' . $op . '&fct=' . $fct . '&id=' . $id . '&value=' . \urlencode($value ?? '') . '&gid=' . $gid . '&cid=' . $cid . '&start=' . $start;
163
    }
164
165
    /**
166
     * @param int $limit
167
     * @return array
168
     */
169
    public function getTop($limit = 1): array
170
    {
171
        $sql     = 'SELECT * FROM `' . $this->table . '` WHERE `rank`>=0 ORDER BY (`rank`/`votes`) DESC LIMIT ' . $limit;
172
        $results = $GLOBALS['xoopsDB']->queryF($sql);
173
        $ret     = [];
174
        $i       = 0;
175
        while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($results))) {
176
            $ret[$i] = $this->create();
177
            $ret[$i]->assignVars($row);
178
            ++$i;
179
        }
180
181
        return $ret;
182
    }
183
184
    /**
185
     * @param \XoopsObject $object
186
     * @param bool         $force
187
     * @return bool
188
     */
189
    public function delete(\XoopsObject $object, $force = true): bool
190
    {
191
        parent::delete($object, $force);
192
        $sql = 'UPDATE ' . $GLOBALS['xoopsDB']->prefix('songlist_songs') . ' SET `gid` = 0 WHERE `gid` = ' . $object->getVar('gid');
193
194
        return $GLOBALS['xoopsDB']->queryF($sql);
195
    }
196
}
197