FormSelectImage   A
last analyzed

Complexity

Total Complexity 30

Size/Duplication

Total Lines 185
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 95
dl 0
loc 185
rs 10
c 0
b 0
f 0
wmc 30

7 Methods

Rating   Name   Duplication   Size   Complexity  
B render() 0 58 10
A addOptGroupArray() 0 5 3
A getOptGroupsID() 0 3 1
A __construct() 0 4 1
A getOptGroups() 0 3 1
C getImageList() 0 47 13
A addOptGroup() 0 3 1
1
<?php
2
3
namespace XoopsModules\Mastopgo2;
4
5
### =============================================================
6
### Mastop InfoDigital - Paixão por Internet
7
### =============================================================
8
### Classe para Colocar as imagens da biblioteca em um Select
9
### =============================================================
10
### Developer: Fernando Santos (topet05), [email protected]
11
### Copyright: Mastop InfoDigital © 2003-2007
12
### -------------------------------------------------------------
13
### www.mastop.com.br
14
### =============================================================
15
###
16
### =============================================================
17
18
// defined('XOOPS_ROOT_PATH') || die('Restricted access');
19
20
require_once XOOPS_ROOT_PATH . '/class/xoopsform/formselect.php';
21
22
/**
23
 * Class FormSelectImage
24
 */
25
class FormSelectImage extends \XoopsFormSelect
26
{
27
    /**
28
     * OptGroup
29
     * @var array
30
     * @access    private
31
     */
32
    public $_optgroups   = [];
33
    public $_optgroupsID = [];
34
35
    /**
36
     * Construtor
37
     *
38
     * @param    string $caption
39
     * @param    string $name
40
     * @param mixed     $value Valor pré-selecionado (ou array de valores).
41
     * @param string    $cat   Nome da Categoria da biblioteca. Se vazio ou não definido, retorna todas as bibliotecas que o cara pode acessar.
42
     * @internal param int $size Número de Linhas. "1" dá um Select List normal de 1 opção.
43
     */
44
    public function __construct($caption, $name, $value = null, $cat = null)
45
    {
46
        parent::__construct($caption, $name, $value);
47
        $this->addOptGroupArray($this->getImageList($cat));
0 ignored issues
show
Bug introduced by
It seems like $cat can also be of type string; however, parameter $cat of XoopsModules\Mastopgo2\F...ctImage::getImageList() does only seem to accept null, 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

47
        $this->addOptGroupArray($this->getImageList(/** @scrutinizer ignore-type */ $cat));
Loading history...
48
    }
49
50
    /**
51
     * Adiciona um Optgroup
52
     *
53
     * @param array|string $value opções do Grupo
54
     * @param string       $name  Nome do Grupo de Opções
55
     */
56
    public function addOptGroup($value = [], $name = '&nbsp;')
57
    {
58
        $this->_optgroups[$name] = $value;
59
    }
60
61
    /**
62
     * Adiciona múltiplos Optgroups
63
     *
64
     * @param array $options Array com nome->opções
65
     */
66
    public function addOptGroupArray($options)
67
    {
68
        if (is_array($options)) {
0 ignored issues
show
introduced by
The condition is_array($options) is always true.
Loading history...
69
            foreach ($options as $k => $v) {
70
                $this->addOptGroup($v, $k);
71
            }
72
        }
73
    }
74
75
    /**
76
     * @param null $cat
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $cat is correct as it would always require null to be passed?
Loading history...
77
     *
78
     * @return array
79
     */
80
    public function getImageList($cat = null)
81
    {
82
        global $xoopsUser;
83
        $ret = [];
84
        if (!is_object($xoopsUser)) {
85
            $group = [XOOPS_GROUP_ANONYMOUS];
86
        } else {
87
            $group = $xoopsUser->getGroups();
88
        }
89
        $imgcatHandler = xoops_getHandler('imagecategory');
90
        $catlist       = $imgcatHandler->getList($group, 'imgcat_read', 1);
0 ignored issues
show
Bug introduced by
The method getList() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of XoopsObjectHandler such as XoopsModuleHandler or XoopsImageHandler or XoopsRankHandler or XoopsCommentHandler or XoopsTplsetHandler or XoopsAvatarHandler or XoopsBlockHandler or XoopsImagesetHandler or XoopsPersistableObjectHandler or XoopsImagecategoryHandler. ( Ignorable by Annotation )

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

90
        /** @scrutinizer ignore-call */ 
91
        $catlist       = $imgcatHandler->getList($group, 'imgcat_read', 1);
Loading history...
91
        if ($catlist && is_array($cat)) {
0 ignored issues
show
introduced by
The condition is_array($cat) is always false.
Loading history...
92
            foreach ($catlist as $k => $v) {
93
                if (!in_array($k, $cat)) {
94
                    unset($catlist[$k]);
95
                }
96
            }
97
        } elseif (is_int($cat)) {
0 ignored issues
show
introduced by
The condition is_int($cat) is always false.
Loading history...
98
            $catlist = array_key_exists($cat, $catlist) ? [$cat => $catlist[$cat]] : [];
99
        }
100
        $imageHandler = xoops_getHandler('image');
101
        foreach ($catlist as $k => $v) {
102
            $this->_optgroupsID[$v] = $k;
103
            $criteria               = new \CriteriaCompo(new \Criteria('imgcat_id', $k));
104
            $criteria->add(new \Criteria('image_display', 1));
105
            $total = $imageHandler->getCount($criteria);
0 ignored issues
show
Bug introduced by
The method getCount() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of said class. However, the method does not exist in XoopsGroupHandler or XoopsConfigCategoryHandler or XoopsRankHandler or XoopsConfigOptionHandler or XoopsBlockHandler or XoopsImagesetHandler. Are you sure you never get one of those? ( Ignorable by Annotation )

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

105
            /** @scrutinizer ignore-call */ 
106
            $total = $imageHandler->getCount($criteria);
Loading history...
106
            if ($total > 0) {
107
                $imgcat    = $imgcatHandler->get($k);
108
                $storetype = $imgcat->getVar('imgcat_storetype');
109
                if ('db' === $storetype) {
110
                    $images = $imageHandler->getObjects($criteria, false, true);
0 ignored issues
show
Bug introduced by
The method getObjects() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of said class. However, the method does not exist in XoopsRankHandler or XoUserHandler. Are you sure you never get one of those? ( Ignorable by Annotation )

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

110
                    /** @scrutinizer ignore-call */ 
111
                    $images = $imageHandler->getObjects($criteria, false, true);
Loading history...
111
                } else {
112
                    $images = $imageHandler->getObjects($criteria, false, false);
113
                }
114
                foreach ($images as $i) {
115
                    if ('db' === $storetype) {
116
                        $ret[$v]['/image.php?id=' . $i->getVar('image_id')] = $i->getVar('image_nicename');
117
                    } else {
118
                        $ret[$v]['/uploads/' . $i->getVar('image_name')] = $i->getVar('image_nicename');
119
                    }
120
                }
121
            } else {
122
                $ret[$v] = '';
123
            }
124
        }
125
126
        return $ret;
127
    }
128
129
    /**
130
     * Pega todos os Optgroups
131
     *
132
     * @return array Array com nome->opções
133
     */
134
    public function getOptGroups()
135
    {
136
        return $this->_optgroups;
137
    }
138
139
    /**
140
     * Pega todos os IDs dos Optgroups
141
     *
142
     * @return    array   Array com nome->ids
143
     */
144
    public function getOptGroupsID()
145
    {
146
        return $this->_optgroupsID;
147
    }
148
149
    /**
150
     * @return string
151
     */
152
    public function render()
153
    {
154
        global $xoopsUser;
155
        if (!is_object($xoopsUser)) {
156
            $group = [XOOPS_GROUP_ANONYMOUS];
157
        } else {
158
            $group = &$xoopsUser->getGroups();
159
        }
160
        $imgcatHandler = xoops_getHandler('imagecategory');
161
        $catlist       = $imgcatHandler->getList($group, 'imgcat_write', 1);
162
        $catlist_total = count($catlist);
163
        $optIds        = $this->getOptGroupsID();
164
        $ret           = "<select onchange='if(this.options[this.selectedIndex].value != \"\"){ document.getElementById(\""
165
                         . $this->getName()
166
                         . '_img").src="'
167
                         . XOOPS_URL
168
                         . '"+this.options[this.selectedIndex].value;} else {document.getElementById("'
169
                         . $this->getName()
170
                         . '_img").src="'
171
                         . XOOPS_URL
172
                         . '/modules/'
173
                         . MGO_MOD_DIR
174
                         . "/assets/images/spager.gif\";}'  size='"
175
                         . $this->getSize()
176
                         . "'"
177
                         . $this->getExtra()
178
                         . '';
179
        if (false !== $this->isMultiple()) {
180
            $ret .= " name='" . $this->getName() . "[]' id='" . $this->getName() . "[]' multiple='multiple'>\n";
181
        } else {
182
            $ret .= " name='" . $this->getName() . "' id='" . $this->getName() . "'>\n";
183
        }
184
        $ret .= "<option value=''>" . _SELECT . "</option>\n";
185
        foreach ($this->getOptGroups() as $nome => $valores) {
186
            $ret .= '\n<optgroup id="img_cat_' . $optIds[$nome] . '" label="' . $nome . '">';
187
            if (is_array($valores)) {
188
                foreach ($valores as $value => $name) {
189
                    $ret .= "<option value='" . htmlspecialchars($value, ENT_QUOTES) . "'";
190
                    if (count($this->getValue()) > 0 && in_array($value, $this->getValue())) {
191
                        $ret    .= ' selected';
192
                        $imagem = $value;
193
                    }
194
                    $ret .= '>' . $name . "</option>\n";
195
                }
196
            }
197
            $ret .= '</optgroup>\n';
198
        }
199
        $browse_url = __DIR__ . '/formimage_browse.php';
200
        $browse_url = str_replace(XOOPS_ROOT_PATH, XOOPS_URL, $browse_url);
201
        $ret        .= '</select>';
202
        $ret        .= ($catlist_total > 0) ? " <input type='button' value='"
203
                                              . _ADDIMAGE
204
                                              . "' onclick=\"window.open('$browse_url?target="
205
                                              . $this->getName()
206
                                              . "','MastopFormImage','resizable=yes,width=500,height=470,left='+(screen.availWidth/2-200)+',top='+(screen.availHeight/2-200)+'');return false;\">" : '';
207
        $ret        .= "<br><img id='" . $this->getName() . "_img' src='" . (!empty($imagem) ? XOOPS_URL . $imagem : XOOPS_URL . '/modules/' . MGO_MOD_DIR . '/assets/images/spacer.gif') . "'>";
208
209
        return $ret;
210
    }
211
}
212