Albums::getForm()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
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 Albums
11
 */
12
class Albums extends \XoopsObject
13
{
14
    /**
15
     * Albums constructor.
16
     * @param null $fid
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $fid is correct as it would always require null to be passed?
Loading history...
17
     */
18
    public function __construct($fid = null)
19
    {
20
        $this->initVar('abid', \XOBJ_DTYPE_INT, 0, false);
21
        $this->initVar('cid', \XOBJ_DTYPE_INT, 0, false);
22
        $this->initVar('aids', \XOBJ_DTYPE_ARRAY, [], false);
23
        $this->initVar('sids', \XOBJ_DTYPE_ARRAY, [], false);
24
        $this->initVar('title', \XOBJ_DTYPE_TXTBOX, null, false, 128);
25
        $this->initVar('image', \XOBJ_DTYPE_TXTBOX, null, false, 255);
26
        $this->initVar('path', \XOBJ_DTYPE_TXTBOX, null, false, 255);
27
        $this->initVar('artists', \XOBJ_DTYPE_INT, 0, false);
28
        $this->initVar('songs', \XOBJ_DTYPE_INT, 0, false);
29
        $this->initVar('hits', \XOBJ_DTYPE_INT, 0, false);
30
        $this->initVar('rank', \XOBJ_DTYPE_DECIMAL, 0, false);
31
        $this->initVar('votes', \XOBJ_DTYPE_INT, 0, false);
32
        $this->initVar('created', \XOBJ_DTYPE_INT, 0, false);
33
        $this->initVar('updated', \XOBJ_DTYPE_INT, 0, false);
34
    }
35
36
    /**
37
     * @param bool $as_array
38
     * @return array|string
39
     */
40
    public function getForm($as_array = false)
41
    {
42
        return FormController::getFormAlbums($this, $as_array);
43
    }
44
45
    /**
46
     * @param bool $extra
47
     * @return array
48
     */
49
    public function toArray($extra = true): array
50
    {
51
        $ret  = parent::toArray();
52
        $form = $this->getForm(true);
53
        foreach ($form as $key => $element) {
54
            $ret['form'][$key] = $element->render();
55
        }
56
        foreach (['created', 'updated'] as $key) {
57
            if ($this->getVar($key) > 0) {
58
                $ret['form'][$key] = \date(_DATESTRING, $this->getVar($key));
59
                $ret[$key]         = \date(_DATESTRING, $this->getVar($key));
60
            }
61
        }
62
        $ret['picture'] = $this->getImage('image', false);
63
        $ret['rank']    = \number_format(($this->getVar('rank') > 0 && $this->getVar('votes') > 0 ? $this->getVar('rank') / $this->getVar('votes') : 0), 2) . \_MI_SONGLIST_OFTEN;
64
        $ret['url']     = $this->getURL(true);
0 ignored issues
show
Unused Code introduced by
The call to XoopsModules\Songlist\Albums::getURL() has too many arguments starting with true. ( Ignorable by Annotation )

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

64
        /** @scrutinizer ignore-call */ 
65
        $ret['url']     = $this->getURL(true);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
65
66
        if (!$extra) {
67
            return $ret;
68
        }
69
70
        if (0 != $this->getVar('cid')) {
71
            $categoryHandler = \XoopsModules\Songlist\Helper::getInstance()->getHandler('Category');
72
            $category        = $categoryHandler->get($this->getVar('cid'));
73
            if (\is_object($category)) {
74
                $ret['category'] = $category->toArray(false);
0 ignored issues
show
Unused Code introduced by
The call to XoopsObject::toArray() has too many arguments starting with false. ( Ignorable by Annotation )

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

74
                /** @scrutinizer ignore-call */ 
75
                $ret['category'] = $category->toArray(false);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
75
            }
76
        }
77
78
        if (0 != \count($this->getVar('aids'))) {
0 ignored issues
show
Bug introduced by
It seems like $this->getVar('aids') can also be of type boolean and null and string; however, parameter $value of count() does only seem to accept Countable|array, 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

78
        if (0 != \count(/** @scrutinizer ignore-type */ $this->getVar('aids'))) {
Loading history...
79
            $artistsHandler = \XoopsModules\Songlist\Helper::getInstance()->getHandler('Artists');
80
            foreach ($this->getVar('aids') as $aid) {
81
                $artist = $artistsHandler->get($aid);
82
                if (\is_object($artist)) {
83
                    $ret['artists_array'][$aid] = $artist->toArray(false);
84
                }
85
            }
86
        }
87
88
        if (0 != \count($this->getVar('sids'))) {
89
            $songsHandler = \XoopsModules\Songlist\Helper::getInstance()->getHandler('Songs');
90
            $criteria     = new \Criteria('sid', '(' . \implode(',', $this->getVar('sids')) . ')', 'IN');
0 ignored issues
show
Bug introduced by
It seems like $this->getVar('sids') can also be of type boolean and null and string; however, parameter $pieces of implode() does only seem to accept array, 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

90
            $criteria     = new \Criteria('sid', '(' . \implode(',', /** @scrutinizer ignore-type */ $this->getVar('sids')) . ')', 'IN');
Loading history...
91
            $criteria->setSort('traxid');
92
            $criteria->setOrder('ASC');
93
            foreach ($songsHandler->getObjects($criteria, true) as $sid => $song) {
94
                if (\is_object($song)) {
95
                    $ret['songs_array'][$sid] = $song->toArray(false);
96
                }
97
            }
98
        }
99
100
        return $ret;
101
    }
102
103
    /**
104
     * @param string $field
105
     * @param bool   $local
106
     * @return bool|string
107
     */
108
    public function getImage($field = 'image', $local = false)
109
    {
110
        if ('' == $this->getVar($field)) {
111
            return false;
112
        }
113
        if (!\file_exists($GLOBALS['xoops']->path($this->getVar('path') . $this->getVar($field)))) {
114
            return false;
115
        }
116
        if (!$local) {
117
            return XOOPS_URL . '/' . \str_replace(DS, '/', $this->getVar('path')) . $this->getVar($field);
118
        }
119
120
        return XOOPS_ROOT_PATH . DS . $this->getVar('path') . $this->getVar($field);
121
    }
122
123
    /**
124
     * @return string
125
     */
126
    public function getURL(): string
127
    {
128
        global $file, $op, $fct, $id, $value, $gid, $vid, $vcid, $cid, $start, $limit;
129
        if ($GLOBALS['songlistModuleConfig']['htaccess']) {
130
            if (0 != $id) {
131
                $artistHandler = \XoopsModules\Songlist\Helper::getInstance()->getHandler('Albums');
132
                $artist        = $artistHandler->get($id);
133
                if (\is_object($artist) && !$artist->isNew()) {
134
                    return XOOPS_URL
135
                           . '/'
136
                           . $GLOBALS['songlistModuleConfig']['baseofurl']
137
                           . '/albums/'
138
                           . \urlencode(\str_replace([' ', \chr(9)], '-', $artist->getVar('title')))
139
                           . '/'
140
                           . $start
141
                           . '-'
142
                           . $id
143
                           . '-'
144
                           . $op
145
                           . '-'
146
                           . $fct
147
                           . '-'
148
                           . $gid
149
                           . '-'
150
                           . $cid
151
                           . '/'
152
                           . \urlencode($value)
153
                           . $GLOBALS['songlistModuleConfig']['endofurl'];
154
                }
155
156
                return XOOPS_URL . '/' . $GLOBALS['songlistModuleConfig']['baseofurl'] . '/albums/' . $start . '-' . $id . '-' . $op . '-' . $fct . '-' . $gid . '-' . $cid . '/' . \urlencode($value) . $GLOBALS['songlistModuleConfig']['endofurl'];
157
            }
158
159
            return XOOPS_URL . '/' . $GLOBALS['songlistModuleConfig']['baseofurl'] . '/albums/' . $start . '-' . $id . '-' . $op . '-' . $fct . '-' . $gid . '-' . $cid . '/' . \urlencode($value) . $GLOBALS['songlistModuleConfig']['endofurl'];
160
        }
161
162
        return XOOPS_URL . '/modules/songlist/albums.php?op=' . $op . '&fct=' . $fct . '&id=' . $id . '&value=' . \urlencode($value ?? '') . '&gid=' . $gid . '&vid=' . $vid . '&cid=' . $cid . '&start=' . $start;
163
    }
164
}
165