Completed
Pull Request — master (#489)
by Richard
10:39
created

PublisherItemForm   F

Complexity

Total Complexity 66

Size/Duplication

Total Lines 717
Duplicated Lines 2.79 %

Coupling/Cohesion

Components 1
Dependencies 27
Metric Value
wmc 66
lcom 1
cbo 27
dl 20
loc 717
rs 1.0434

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setCheckPermissions() 0 4 1
B __construct() 0 56 6
F buildMainTab() 0 119 13
D buildEditors() 20 61 13
B buildTSOptions() 0 25 6
B buildFilesTab() 0 82 6
C buildImagesTab() 0 188 8
B buildOthersTab() 0 45 5
A isGranted() 0 9 3
B hasTab() 0 16 5

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like PublisherItemForm often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use PublisherItemForm, and based on these observations, apply Extract Interface, too.

1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
use Xoops\Core\Kernel\Criteria;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Criteria.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
13
use Xoops\Core\Kernel\CriteriaCompo;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, CriteriaCompo.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
14
use Xoops\Form\ContainerInterface;
15
use Xoops\Form\Tab;
16
use Xoops\Form\TabTray;
17
18
include_once dirname(dirname(__DIR__)) . '/include/common.php';
19
20
$publisher = Publisher::getInstance();
21
22
/**
23
 *  Publisher form class
24
 *
25
 * @category  PublisherItemForm
26
 * @package   Publisher
27
 * @author    trabis <[email protected]>
28
 * @copyright 2011-2015 The XUUPS Project (http://sourceforge.net/projects/xuups/)
29
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
30
 * @link      http://xoops.org
31
 */
32
class PublisherItemForm extends Xoops\Form\SimpleForm
33
{
34
    private $checkperm = true;
35
36
    private $tabs = array(
37
        _CO_PUBLISHER_TAB_MAIN  => 'mainTab', _CO_PUBLISHER_TAB_IMAGES => 'imagesTab',
38
        _CO_PUBLISHER_TAB_FILES => 'filesTab', _CO_PUBLISHER_TAB_OTHERS => 'othersTab'
39
    );
40
41
    private $mainTab = array(
0 ignored issues
show
Unused Code introduced by
The property $mainTab is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
42
        _PUBLISHER_SUBTITLE, _PUBLISHER_ITEM_SHORT_URL, _PUBLISHER_ITEM_TAG, _PUBLISHER_SUMMARY, _PUBLISHER_DOHTML,
43
        _PUBLISHER_DOSMILEY, _PUBLISHER_DOXCODE, _PUBLISHER_DOIMAGE, _PUBLISHER_DOLINEBREAK, _PUBLISHER_DATESUB,
44
        _PUBLISHER_STATUS, _PUBLISHER_AUTHOR_ALIAS, _PUBLISHER_NOTIFY, _PUBLISHER_AVAILABLE_PAGE_WRAP, _PUBLISHER_UID
45
    );
46
47
    private $imagesTab = array(
0 ignored issues
show
Unused Code introduced by
The property $imagesTab is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
48
        _PUBLISHER_IMAGE_ITEM
49
    );
50
51
    private $filesTab = array(
0 ignored issues
show
Unused Code introduced by
The property $filesTab is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
52
        _PUBLISHER_ITEM_UPLOAD_FILE
53
    );
54
55
    private $othersTab = array(
0 ignored issues
show
Unused Code introduced by
The property $othersTab is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
56
        _PUBLISHER_ITEM_META_KEYWORDS, _PUBLISHER_ITEM_META_DESCRIPTION, _PUBLISHER_WEIGHT, _PUBLISHER_ALLOWCOMMENTS
57
    );
58
59
    /**
60
     * __construct
61
     *
62
     * @param PublisherItem $obj source object for form variables
63
     */
64
    public function __construct(PublisherItem $obj)
65
    {
66
        $xoops = Xoops::getInstance();
67
68
        parent::__construct('title', 'form', $xoops->getEnv('PHP_SELF'));
0 ignored issues
show
Bug introduced by
It seems like $xoops->getEnv('PHP_SELF') targeting Xoops::getEnv() can also be of type boolean; however, Xoops\Form\Form::__construct() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
69
        $this->setExtra('enctype="multipart/form-data"');
70
71
        $tabTray = new TabTray('', 'uniqueid');
72
73
        $mainTab = new Tab(_CO_PUBLISHER_TAB_MAIN, 'maintab');
74
        $this->buildMainTab($obj, $mainTab);
75
        $tabTray->addElement($mainTab);
76
77
        if ($xoops->isActiveModule('images') && $this->hasTab(_CO_PUBLISHER_TAB_IMAGES)) {
78
            $imagesTab = new Tab(_CO_PUBLISHER_TAB_IMAGES, 'imagestab');
79
            $this->buildImagesTab($obj, $imagesTab);
80
            $tabTray->addElement($imagesTab);
81
        }
82
83
        if ($this->hasTab(_CO_PUBLISHER_TAB_FILES)) {
84
            $filesTab = new Tab(_CO_PUBLISHER_TAB_FILES, 'filestab');
85
            $this->buildFilesTab($obj, $filesTab);
86
            $tabTray->addElement($filesTab);
87
        }
88
89
        if ($this->hasTab(_CO_PUBLISHER_TAB_OTHERS)) {
90
            $othersTab = new Tab(_CO_PUBLISHER_TAB_OTHERS, 'otherstab');
91
            $this->buildOthersTab($obj, $othersTab);
92
            $tabTray->addElement($othersTab);
93
        }
94
        $this->addElement($tabTray);
95
96
        //COMMON TO ALL TABS
97
98
        $buttonTray = new Xoops\Form\ElementTray('', '');
99
100
        if (!$obj->isNew()) {
101
            $buttonTray->addElement(new Xoops\Form\Button('', 'additem', XoopsLocale::A_SUBMIT, 'submit')); //orclone
102
103
        } else {
104
            $buttonTray->addElement(new Xoops\Form\Button('', 'additem', _CO_PUBLISHER_CREATE, 'submit'));
105
            $buttonTray->addElement(new Xoops\Form\Button('', '', _CO_PUBLISHER_CLEAR, 'reset'));
106
        }
107
108
        $buttonTray->addElement(new Xoops\Form\Button('', 'preview', _CO_PUBLISHER_PREVIEW, 'submit'));
109
110
        $buttonCancel = new Xoops\Form\Button('', '', _CO_PUBLISHER_CANCEL, 'button');
111
        $buttonCancel->set('onclick', 'history.go(-1);');
112
        $buttonTray->addElement($buttonCancel);
113
114
        $this->addElement($buttonTray);
115
116
        $hidden = new Xoops\Form\Hidden('itemid', $obj->getVar('itemid'));
117
        $this->addElement($hidden);
118
        unset($hidden);
119
    }
120
121
    /**
122
     * Build the main tab
123
     *
124
     * @param PublisherItem      $obj     data source
125
     * @param ContainerInterface $mainTab add elements to this tab/form
126
     *
127
     * @return void
128
     */
129
    private function buildMainTab(PublisherItem $obj, ContainerInterface $mainTab)
130
    {
131
        $xoops = Xoops::getInstance();
132
        $publisher = Publisher::getInstance();
133
134
        // Category
135
        $category_select = new Xoops\Form\Select(_CO_PUBLISHER_CATEGORY, 'categoryid', $obj->getVar('categoryid', 'e'));
136
        $category_select->setDescription(_CO_PUBLISHER_CATEGORY_DSC);
137
        $category_select->addOptionArray($publisher->getCategoryHandler()->getCategoriesForSubmit());
138
        $mainTab->addElement($category_select);
139
140
        // ITEM TITLE
141
        $mainTab->addElement(
142
            new Xoops\Form\Text(_CO_PUBLISHER_TITLE, 'title', 50, 255, $obj->getVar('title', 'e')),
143
            true
144
        );
145
146
        // SUBTITLE
147
        if ($this->isGranted(_PUBLISHER_SUBTITLE)) {
148
            $mainTab->addElement(
149
                new Xoops\Form\Text(_CO_PUBLISHER_SUBTITLE, 'subtitle', 50, 255, $obj->getVar('subtitle', 'e'))
150
            );
151
        }
152
153
        // SHORT URL
154
        if ($this->isGranted(_PUBLISHER_ITEM_SHORT_URL)) {
155
            $text_short_url = new Xoops\Form\Text(
156
                _CO_PUBLISHER_ITEM_SHORT_URL,
157
                'item_short_url',
158
                50,
159
                255,
160
                $obj->getVar('short_url', 'e')
161
            );
162
            $text_short_url->setDescription(_CO_PUBLISHER_ITEM_SHORT_URL_DSC);
163
            $mainTab->addElement($text_short_url);
164
        }
165
166
        // TAGS
167
        if ($xoops->isActiveModule('tag') && $this->isGranted(_PUBLISHER_ITEM_TAG)) {
168
            include_once $xoops->path('modules/tag/include/formtag.php');
169
            $text_tags = new Tag('item_tag', 60, 255, $obj->getVar('item_tag', 'e'), 0);
170
            $mainTab->addElement($text_tags);
171
        }
172
173
        $this->buildEditors($obj, $mainTab);
174
        $this->buildTSOptions($obj, $mainTab);
175
176
        // Available pages to wrap
177
        if ($this->isGranted(_PUBLISHER_AVAILABLE_PAGE_WRAP)) {
178
            $wrap_pages = XoopsLists::getHtmlListAsArray(PublisherUtils::getUploadDir(true, 'content'));
179
            $available_wrap_pages_text = array();
180
            foreach ($wrap_pages as $page) {
181
                $available_wrap_pages_text[] = "<span onclick='publisherPageWrap(\"body\", \"[pagewrap=$page] \");'"
182
                    . " onmouseover='style.cursor=\"pointer\"'>$page</span>";
183
            }
184
            $available_wrap_pages = new Xoops\Form\Label(
185
                _CO_PUBLISHER_AVAILABLE_PAGE_WRAP,
186
                implode(', ', $available_wrap_pages_text)
187
            );
188
            $available_wrap_pages->setDescription(_CO_PUBLISHER_AVAILABLE_PAGE_WRAP_DSC);
189
            $mainTab->addElement($available_wrap_pages);
190
        }
191
192
        // Uid
193
        /*  We need to retrieve the users manually because for some reason, on the frxoops.org server,
194
         the method users::getobjects encounters a memory error
195
         */
196
        // Trabis : well, maybe is because you are getting 6000 objects into memory , no??? LOL
197
        if ($this->isGranted(_PUBLISHER_UID)) {
198
            $uid_select =
199
                new Xoops\Form\SelectUser(_CO_PUBLISHER_UID, 'uid', true, array($obj->getVar('uid', 'e')), 1, false);
200
            $uid_select->setDescription(_CO_PUBLISHER_UID_DSC);
201
            $mainTab->addElement($uid_select);
202
        }
203
204
        // Author Alias
205
        if ($this->isGranted(_PUBLISHER_AUTHOR_ALIAS)) {
206
            $element = new Xoops\Form\Text(
207
                _CO_PUBLISHER_AUTHOR_ALIAS,
208
                'author_alias',
209
                50,
210
                255,
211
                $obj->getVar('author_alias', 'e')
212
            );
213
            $element->setDescription(_CO_PUBLISHER_AUTHOR_ALIAS_DSC);
214
            $mainTab->addElement($element);
215
            unset($element);
216
        }
217
218
        // STATUS
219
        if ($this->isGranted(_PUBLISHER_STATUS)) {
220
            $options = array(
221
                _PUBLISHER_STATUS_PUBLISHED => _CO_PUBLISHER_PUBLISHED,
222
                _PUBLISHER_STATUS_OFFLINE   => _CO_PUBLISHER_OFFLINE,
223
                _PUBLISHER_STATUS_SUBMITTED => _CO_PUBLISHER_SUBMITTED,
224
                _PUBLISHER_STATUS_REJECTED  => _CO_PUBLISHER_REJECTED
225
            );
226
            $status_select = new Xoops\Form\Select(_CO_PUBLISHER_STATUS, 'status', $obj->getVar('status'));
227
            $status_select->addOptionArray($options);
228
            $status_select->setDescription(_CO_PUBLISHER_STATUS_DSC);
229
            $mainTab->addElement($status_select);
230
            unset($status_select);
231
        }
232
233
        // Datesub
234
        if ($this->isGranted(_PUBLISHER_DATESUB)) {
235
            $datesub = ($obj->getVar('datesub') == 0) ? time() : $obj->getVar('datesub');
236
            $datesub_datetime = new Xoops\Form\DateTime(_CO_PUBLISHER_DATESUB, 'datesub', $datesub);
237
            $datesub_datetime->setDescription(_CO_PUBLISHER_DATESUB_DSC);
238
            $mainTab->addElement($datesub_datetime);
239
        }
240
241
        // NOTIFY ON PUBLISH
242
        if ($this->isGranted(_PUBLISHER_NOTIFY)) {
243
            $notify_radio = new Xoops\Form\RadioYesNo(_CO_PUBLISHER_NOTIFY, 'notify', $obj->getVar('notifypub'));
244
            $mainTab->addElement($notify_radio);
245
        }
246
247
    }
248
249
    /**
250
     * Build the summary and body editors
251
     *
252
     * @param PublisherItem      $obj     data source
253
     * @param ContainerInterface $mainTab add elements to this tab/form
254
     *
255
     * @return void
256
     */
257
    private function buildEditors(PublisherItem $obj, ContainerInterface $mainTab)
0 ignored issues
show
Coding Style introduced by
buildEditors uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
258
    {
259
        $xoops = Xoops::getInstance();
260
        $publisher = Publisher::getInstance();
261
262
        // SELECT EDITOR
263
        $allowed_editors = PublisherUtils::getEditors($publisher->getPermissionHandler()->getGrantedItems('editors'));
264
265
        $nohtml = false;
266
        if (count($allowed_editors) == 1) {
267
            $editor = $allowed_editors[0];
268 View Code Duplication
        } else {
269
            if (count($allowed_editors) > 0) {
270
                $editor = @$_POST['editor'];
271
                if (!empty($editor)) {
272
                    PublisherUtils::setCookieVar('publisher_editor', $editor);
273
                } else {
274
                    $editor = PublisherUtils::getCookieVar('publisher_editor');
275
                    if (empty($editor) && $xoops->isUser()) {
276
                        $editor = $xoops->user->getVar('publisher_editor'); // Need set through user profile
277
                    }
278
                }
279
                $editor = (empty($editor) || !in_array($editor, $allowed_editors))
280
                    ? $publisher->getConfig('submit_editor') : $editor;
281
282
                $form_editor = new Xoops\Form\SelectEditor($this, 'editor', $editor, $nohtml, $allowed_editors);
283
                $mainTab->addElement($form_editor);
284
            } else {
285
                $editor = $publisher->getConfig('submit_editor');
286
            }
287
        }
288
289
        $editor_configs = array();
290
        $editor_configs["rows"] = !$publisher->getConfig('submit_editor_rows')
291
            ? 35 : $publisher->getConfig('submit_editor_rows');
292
        $editor_configs["cols"] = !$publisher->getConfig('submit_editor_cols')
293
            ? 60 : $publisher->getConfig('submit_editor_cols');
294
        $editor_configs["width"] = !$publisher->getConfig('submit_editor_width')
295
            ? "100%" : $publisher->getConfig('submit_editor_width');
296
        $editor_configs["height"] = !$publisher->getConfig('submit_editor_height')
297
            ? "400px" : $publisher->getConfig('submit_editor_height');
298
299
        // SUMMARY
300
        if ($this->isGranted(_PUBLISHER_SUMMARY)) {
301
            // Description
302
            $editor_configs["name"] = "summary";
303
            $editor_configs["value"] = $obj->getVar('summary', 'e');
304
            $summary_text =
305
                new Xoops\Form\Editor(_CO_PUBLISHER_SUMMARY, $editor, $editor_configs, $nohtml, $onfailure = null);
306
            $summary_text->setDescription(_CO_PUBLISHER_SUMMARY_DSC);
307
            $mainTab->addElement($summary_text);
308
        }
309
310
        // BODY
311
        $editor_configs["name"] = "body";
312
        $editor_configs["value"] = $obj->getVar('body', 'e');
313
        $body_text = new Xoops\Form\Editor(_CO_PUBLISHER_BODY, $editor, $editor_configs, $nohtml, $onfailure = null);
314
        $body_text->setDescription(_CO_PUBLISHER_BODY_DSC);
315
        $mainTab->addElement($body_text);
316
317
    }
318
319
    /**
320
     * Build the option selectors for Text\Sanitizer display processing
321
     *
322
     * @param PublisherItem      $obj     data source
323
     * @param ContainerInterface $mainTab add elements to this tab/form
324
     *
325
     * @return void
326
     */
327
    private function buildTSOptions(PublisherItem $obj, ContainerInterface $mainTab)
328
    {
329
        // VARIOUS OPTIONS
330
        if ($this->isGranted(_PUBLISHER_DOHTML)) {
331
            $html_radio = new Xoops\Form\RadioYesNo(_CO_PUBLISHER_DOHTML, 'dohtml', $obj->getVar('dohtml'));
332
            $mainTab->addElement($html_radio);
333
        }
334
        if ($this->isGranted(_PUBLISHER_DOSMILEY)) {
335
            $smiley_radio = new Xoops\Form\RadioYesNo(_CO_PUBLISHER_DOSMILEY, 'dosmiley', $obj->getVar('dosmiley'));
336
            $mainTab->addElement($smiley_radio);
337
        }
338
        if ($this->isGranted(_PUBLISHER_DOXCODE)) {
339
            $xcode_radio = new Xoops\Form\RadioYesNo(_CO_PUBLISHER_DOXCODE, 'doxcode', $obj->getVar('doxcode'));
340
            $mainTab->addElement($xcode_radio);
341
        }
342
        if ($this->isGranted(_PUBLISHER_DOIMAGE)) {
343
            $image_radio = new Xoops\Form\RadioYesNo(_CO_PUBLISHER_DOIMAGE, 'doimage', $obj->getVar('doimage'));
344
            $mainTab->addElement($image_radio);
345
        }
346
        if ($this->isGranted(_PUBLISHER_DOLINEBREAK)) {
347
            $linebreak_radio =
348
                new Xoops\Form\RadioYesNo(_CO_PUBLISHER_DOLINEBREAK, 'dolinebreak', $obj->getVar('dobr'));
349
            $mainTab->addElement($linebreak_radio);
350
        }
351
    }
352
353
354
    /**
355
     * Build the files tab
356
     *
357
     * @param PublisherItem      $obj      data source
358
     * @param ContainerInterface $filesTab add elements to this tab/form
359
     *
360
     * @return void
361
     */
362
    private function buildFilesTab(PublisherItem $obj, ContainerInterface $filesTab)
363
    {
364
        $publisher = Publisher::getInstance();
365
366
        // File upload UPLOAD
367
        if ($this->isGranted(_PUBLISHER_ITEM_UPLOAD_FILE)) {
368
            // NAME
369
            $name_text = new Xoops\Form\Text(_CO_PUBLISHER_FILENAME, 'item_file_name', 50, 255, '');
370
            $name_text->setDescription(_CO_PUBLISHER_FILE_NAME_DSC);
371
            $filesTab->addElement($name_text);
372
            unset($name_text);
373
374
            // DESCRIPTION
375
            $description_text =
376
                new Xoops\Form\TextArea(_CO_PUBLISHER_FILE_DESCRIPTION, 'item_file_description', '');
377
            $description_text->setDescription(_CO_PUBLISHER_FILE_DESCRIPTION_DSC);
378
            $filesTab->addElement($description_text);
379
            unset($description_text);
380
381
            //1 - active
382
            $status_select = new Xoops\Form\RadioYesNo(_CO_PUBLISHER_FILE_STATUS, 'item_file_status', 1);
383
            $status_select->setDescription(_CO_PUBLISHER_FILE_STATUS_DSC);
384
            $filesTab->addElement($status_select);
385
            unset($status_select);
386
387
            $file_box = new Xoops\Form\File(_CO_PUBLISHER_ITEM_UPLOAD_FILE, "item_upload_file");
388
            $file_box->setDescription(_CO_PUBLISHER_ITEM_UPLOAD_FILE_DSC);
389
            $file_box->set('size', 50);
390
            $filesTab->addElement($file_box);
391
            unset($file_box);
392
393
            if (!$obj->isNew()) {
394
                $filesObj = $publisher->getFileHandler()->getAllFiles($obj->getVar('itemid'));
395
                if (count($filesObj) > 0) {
396
                    $table = '';
397
                    $table .= "<table width='100%' cellspacing=1 cellpadding=3 border=0 class = outer>";
398
                    $table .= "<tr>";
399
                    $table .= "<td width='50' class='bg3' align='center'><strong>ID</strong></td>";
400
                    $table .= "<td width='150' class='bg3' align='left'><strong>"
401
                        . _AM_PUBLISHER_FILENAME . "</strong></td>";
402
                    $table .= "<td class='bg3' align='left'><strong>"
403
                        . _AM_PUBLISHER_DESCRIPTION . "</strong></td>";
404
                    $table .= "<td width='60' class='bg3' align='center'><strong>"
405
                        . _AM_PUBLISHER_HITS . "</strong></td>";
406
                    $table .= "<td width='100' class='bg3' align='center'><strong>"
407
                        . _AM_PUBLISHER_UPLOADED_DATE . "</strong></td>";
408
                    $table .= "<td width='60' class='bg3' align='center'><strong>"
409
                        . _AM_PUBLISHER_ACTION . "</strong></td>";
410
                    $table .= "</tr>";
411
412
                    /* @var $fileObj PublisherFile */
413
                    foreach ($filesObj as $fileObj) {
414
                        $modify = "<a href='file.php?op=mod&fileid=" . $fileObj->getVar('fileid')
415
                            . "'><img src='" . PUBLISHER_URL . "/images/links/edit.gif' title='"
416
                            . _CO_PUBLISHER_EDITFILE . "' alt='" . _CO_PUBLISHER_EDITFILE . "' /></a>";
417
                        $delete = "<a href='file.php?op=del&fileid=" . $fileObj->getVar('fileid')
418
                            . "'><img src='" . PUBLISHER_URL . "/images/links/delete.png' title='"
419
                            . _CO_PUBLISHER_DELETEFILE . "' alt='" . _CO_PUBLISHER_DELETEFILE . "'/></a>";
420
                        if ($fileObj->getVar('status') == 0) {
421
                            $notVisible = "<img src='" . PUBLISHER_URL . "/images/no.gif'/>";
422
                        } else {
423
                            $notVisible = '';
424
                        }
425
                        $table .= "<tr>";
426
                        $table .= "<td class='head' align='center'>" . $fileObj->getVar('fileid') . "</td>";
427
                        $table .= "<td class='odd' align='left'>" . $notVisible . $fileObj->getFileLink() . "</td>";
428
                        $table .= "<td class='even' align='left'>" . $fileObj->getVar('description') . "</td>";
429
                        $table .= "<td class='even' align='center'>" . $fileObj->getVar('counter') . "";
430
                        $table .= "<td class='even' align='center'>" . $fileObj->datesub() . "</td>";
431
                        $table .= "<td class='even' align='center'> {$modify} {$delete} </td>";
432
                        $table .= "</tr>";
433
                    }
434
                    $table .= "</table>";
435
436
                    $files_box = new Xoops\Form\Label(_CO_PUBLISHER_FILES_LINKED, $table);
437
                    $filesTab->addElement($files_box);
438
                    unset($files_box, $filesObj, $fileObj);
439
                }
440
            }
441
        }
442
443
    }
444
445
    /**
446
     * Build the images tab
447
     *
448
     * @param PublisherItem      $obj       data source
449
     * @param ContainerInterface $imagesTab add elements to this tab/form
450
     *
451
     * @return void
452
     */
453
    private function buildImagesTab(PublisherItem $obj, ContainerInterface $imagesTab)
454
    {
455
        $xoops = Xoops::getInstance();
456
        $group = $xoops->getUserGroups();
457
458
        // IMAGE
459
        if ($this->isGranted(_PUBLISHER_IMAGE_ITEM)) {
460
            $imgcat_handler = Images::getInstance()->getHandlerCategories();
461
            $image_handler = Images::getInstance()->getHandlerImages();
462
463
            $objimages = $obj->getImages();
464
            $mainarray = is_object($objimages['main']) ? array($objimages['main']) : array();
465
            $mergedimages = array_merge($mainarray, $objimages['others']);
466
            $objimage_array = array();
467
            /* @var $imageObj ImagesImage */
468
            foreach ($mergedimages as $imageObj) {
469
                $objimage_array[$imageObj->getVar('image_name')] = $imageObj->getVar('image_nicename');
470
            }
471
472
            $catlist = $imgcat_handler->getListByPermission($group, 'imgcat_read', 1);
473
            $catids = array_keys($catlist);
474
475
            $imageObjs = array();
476
            if (!empty($catids)) {
477
                $criteria = new CriteriaCompo(new Criteria('imgcat_id', '(' . implode(',', $catids) . ')', 'IN'));
478
                $criteria->add(new Criteria('image_display', 1));
479
                $criteria->setSort('image_nicename');
480
                $criteria->setOrder('ASC');
481
                $imageObjs = $image_handler->getObjects($criteria, true);
0 ignored issues
show
Bug introduced by
The method getObjects does only exist in XoopsPersistableObjectHandler, but not in XoopsObjectHandler.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
482
                unset($criteria);
483
            }
484
            $image_array = array();
485
            foreach ($imageObjs as $imageObj) {
486
                $image_array[$imageObj->getVar('image_name')] = $imageObj->getVar('image_nicename');
487
            }
488
489
            $image_array = array_diff($image_array, $objimage_array);
490
491
            $image_select = new Xoops\Form\Select('', 'image_notused', '', 5);
492
            $image_select->addOptionArray($image_array);
493
            $image_select->set(
494
                'onchange',
495
                'showImgSelected("image_display", "image_notused", "uploads/", "", "'
496
                . \XoopsBaseConfig::get('url') . '")'
497
            );
498
            unset($image_array);
499
500
            $image_select2 = new Xoops\Form\Select('', 'image_item', '', 5, true);
501
            $image_select2->addOptionArray($objimage_array);
502
            $image_select2->set(
503
                'onchange',
504
                'publisher_updateSelectOption("image_item", "image_featured"), '
505
                . 'showImgSelected("image_display", "image_item", "uploads/", "", "'
506
                . \XoopsBaseConfig::get('url') . '");'
507
            );
508
509
            $buttonadd = new Xoops\Form\Button('', 'buttonadd', _CO_PUBLISHER_ADD);
510
            $buttonadd->set(
511
                'onclick',
512
                'publisher_appendSelectOption("image_notused", "image_item"), '
513
                . 'publisher_updateSelectOption("image_item", "image_featured");'
514
            );
515
516
            $buttonremove = new Xoops\Form\Button('', 'buttonremove', _CO_PUBLISHER_REMOVE);
517
            $buttonremove->set(
518
                'onclick',
519
                'publisher_appendSelectOption("image_item", "image_notused"), '
520
                . 'publisher_updateSelectOption("image_item", "image_featured");'
521
            );
522
523
            $opentable = new Xoops\Form\Label('', "<table><tr><td>");
524
            $addcol = new Xoops\Form\Label('', "</td><td>");
525
            $addbreak = new Xoops\Form\Label('', "<br />");
526
            $closetable = new Xoops\Form\Label('', "</td></tr></table>");
527
528
            $xoops->theme()->addScript(PUBLISHER_URL . '/js/ajaxupload.3.9.js');
529
            //todo, find replacement for error class
530
            $js_data = new Xoops\Form\Label('', '
531
<script type= "text/javascript">/*<![CDATA[*/
532
$(document).ready(function(){
533
    var button = $("#publisher_upload_button"), interval;
534
    new AjaxUpload(button,{
535
        action: "' . PUBLISHER_URL . '/include/ajax_upload.php", // I disabled uploads in this example for security reasons
536
        responseType: "text/html",
537
        name: "publisher_upload_file",
538
        onSubmit : function(file, ext){
539
            // change button text, when user selects file
540
            $("#publisher_upload_message").html(" ");
541
            button.html("<img src=\'' . PUBLISHER_URL . '/images/loadingbar.gif\'/>"); this.setData({
542
                "image_nicename": $("#image_nicename").val(),
543
                "imgcat_id" : $("#imgcat_id").val()
544
            });
545
            // If you want to allow uploading only 1 file at time,
546
            // you can disable upload button
547
            this.disable();
548
            interval = window.setInterval(function(){
549
            }, 200);
550
        },
551
        onComplete: function(file, response){
552
            button.text("' . _CO_PUBLISHER_IMAGE_UPLOAD_NEW . '");
553
            window.clearInterval(interval);
554
            // enable upload button
555
            this.enable();
556
            // add file to the list
557
            var result = eval(response);
558
            if (result[0] == "success") {
559
                 $("#image_item").append("<option value=\'" + result[1] + "\' selected=\'selected\'>" + result[2] + "</option>");
560
                 publisher_updateSelectOption(\'image_item\', \'image_featured\');
561
                 showImgSelected(\'image_display\', \'image_item\', \'uploads/\', \'\', \'' . \XoopsBaseConfig::get('url') . '\')
562
            } else {
563
                 $("#publisher_upload_message").html("<div class=\'errorMsg\'>" + result[1] + "</div>");
564
            }
565
        }
566
    });
567
});
568
/*]]>*/</script>
569
');
570
            $messages = new Xoops\Form\Label('', "<div id='publisher_upload_message'></div>");
571
            $button = new Xoops\Form\Label(
572
                '',
573
                "<div id='publisher_upload_button'>" . _CO_PUBLISHER_IMAGE_UPLOAD_NEW . "</div>"
574
            );
575
            $nicename = new Xoops\Form\Text('', 'image_nicename', 30, 30, _CO_PUBLISHER_IMAGE_NICENAME);
576
577
            $catlist = $imgcat_handler->getListByPermission($group, 'imgcat_read', 1);
578
            $imagecat = new Xoops\Form\Select('', 'imgcat_id', '', 1);
579
            $imagecat->addOptionArray($catlist);
580
581
            $image_upload_tray = new Xoops\Form\ElementTray(_CO_PUBLISHER_IMAGE_UPLOAD, '');
582
            $image_upload_tray->addElement($js_data);
583
            $image_upload_tray->addElement($messages);
584
            $image_upload_tray->addElement($opentable);
585
586
            $image_upload_tray->addElement($imagecat);
587
588
            $image_upload_tray->addElement($addbreak);
589
590
            $image_upload_tray->addElement($nicename);
591
592
            $image_upload_tray->addElement($addbreak);
593
594
            $image_upload_tray->addElement($button);
595
596
            $image_upload_tray->addElement($closetable);
597
            $imagesTab->addElement($image_upload_tray);
598
599
            $image_tray = new Xoops\Form\ElementTray(_CO_PUBLISHER_IMAGE_ITEMS, '');
600
            $image_tray->addElement($opentable);
601
602
            $image_tray->addElement($image_select);
603
            $image_tray->addElement($addbreak);
604
            $image_tray->addElement($buttonadd);
605
606
            $image_tray->addElement($addcol);
607
608
            $image_tray->addElement($image_select2);
609
            $image_tray->addElement($addbreak);
610
            $image_tray->addElement($buttonremove);
611
612
            $image_tray->addElement($closetable);
613
            $image_tray->setDescription(_CO_PUBLISHER_IMAGE_ITEMS_DSC);
614
            $imagesTab->addElement($image_tray);
615
616
            $imagename = is_object($objimages['main']) ? $objimages['main']->getVar('image_name') : '';
617
            $imageforpath = ($imagename != '') ? $imagename : 'blank.gif';
618
619
            $image_select3 = new Xoops\Form\Select(_CO_PUBLISHER_IMAGE_ITEM, 'image_featured', $imagename, 1);
620
            $image_select3->addOptionArray($objimage_array);
621
            $image_select3->set(
622
                'onchange',
623
                'showImgSelected("image_display", "image_featured", "uploads/", "", "'
624
                . \XoopsBaseConfig::get('url') . '");'
625
            );
626
            $image_select3->setDescription(_CO_PUBLISHER_IMAGE_ITEM_DSC);
627
            $imagesTab->addElement($image_select3);
628
629
            $imgTag = new Xoops\Html\Img([
630
                'src' => $xoops->url('uploads/' . $imageforpath),
631
                'width' => 500,
632
                'name' => 'image_display',
633
                'id' => 'image_display',
634
                'alt' => '',
635
636
            ]);
637
            $image_preview = new Xoops\Form\Label(_CO_PUBLISHER_IMAGE_PREVIEW, $imgTag->render());
638
            $imagesTab->addElement($image_preview);
639
        }
640
    }
641
642
    /**
643
     * Build the others tab
644
     *
645
     * @param PublisherItem      $obj       data source
646
     * @param ContainerInterface $othersTab add elements to this tab/form
647
     *
648
     * @return void
649
     */
650
    private function buildOthersTab(PublisherItem $obj, ContainerInterface $othersTab)
651
    {
652
        // Meta Keywords
653
        if ($this->isGranted(_PUBLISHER_ITEM_META_KEYWORDS)) {
654
            $text_meta_keywords = new Xoops\Form\TextArea(
655
                _CO_PUBLISHER_ITEM_META_KEYWORDS,
656
                'item_meta_keywords',
657
                $obj->getVar('meta_keywords', 'e'),
658
                7,
659
                60
660
            );
661
            $text_meta_keywords->setDescription(_CO_PUBLISHER_ITEM_META_KEYWORDS_DSC);
662
            $othersTab ->addElement($text_meta_keywords);
663
        }
664
665
        // Meta Description
666
        if ($this->isGranted(_PUBLISHER_ITEM_META_DESCRIPTION)) {
667
            $text_meta_description = new Xoops\Form\TextArea(
668
                _CO_PUBLISHER_ITEM_META_DESCRIPTION,
669
                'item_meta_description',
670
                $obj->getVar('meta_description', 'e'),
671
                7,
672
                60
673
            );
674
            $text_meta_description->setDescription(_CO_PUBLISHER_ITEM_META_DESCRIPTION_DSC);
675
            $othersTab ->addElement($text_meta_description);
676
        }
677
678
        // COMMENTS
679
        if ($this->isGranted(_PUBLISHER_ALLOWCOMMENTS)) {
680
            $addcomments_radio = new Xoops\Form\RadioYesNo(
681
                _CO_PUBLISHER_ALLOWCOMMENTS,
682
                'allowcomments',
683
                $obj->getVar('cancomment')
684
            );
685
            $othersTab ->addElement($addcomments_radio);
686
        }
687
688
        // WEIGHT
689
        if ($this->isGranted(_PUBLISHER_WEIGHT)) {
690
            $othersTab ->addElement(
691
                new Xoops\Form\Text(_CO_PUBLISHER_WEIGHT, 'weight', 5, 5, $obj->getVar('weight'))
692
            );
693
        }
694
    }
695
696
    /**
697
     * setCheckPermissions
698
     *
699
     * @param boolean $checkperm true to check permissions, false to ignore permissions
700
     *
701
     * @return void
702
     */
703
    public function setCheckPermissions($checkperm)
704
    {
705
        $this->checkperm = (bool)$checkperm;
706
    }
707
708
    /**
709
     * isGranted
710
     *
711
     * @param int $item permission item to check
712
     *
713
     * @return bool true if permission is granted, false if not
714
     */
715
    private function isGranted($item)
716
    {
717
        $publisher = Publisher::getInstance();
718
        $ret = false;
719
        if (!$this->checkperm || $publisher->getPermissionHandler()->isGranted('form_view', $item)) {
720
            $ret = true;
721
        }
722
        return $ret;
723
    }
724
725
    /**
726
     * hasTab
727
     *
728
     * @param string $tab tab name
729
     *
730
     * @return bool true if form has tab named $tab
731
     */
732
    private function hasTab($tab)
733
    {
734
        if (!isset($tab) || !isset($this->tabs[$tab])) {
735
            return false;
736
        }
737
738
        $tabRef = $this->tabs[$tab];
739
        $items = $this->$tabRef;
740
        foreach ($items as $item) {
741
            if ($this->isGranted($item)) {
742
                return true;
743
            }
744
        }
745
746
        return false;
747
    }
748
}
749