Completed
Push — master ( 53cd45...f19045 )
by Michael
25s queued 19s
created

MenusMenuHandler::updateWeights()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 20
nc 2
nop 1
dl 0
loc 29
rs 9.6
c 0
b 0
f 0
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 Doctrine\DBAL\FetchMode;
13
use Xoops\Core\Database\Connection;
14
use Xoops\Core\FixedGroups;
15
use Xoops\Core\Kernel\XoopsObject;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, XoopsObject. Consider defining an alias.

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...
16
use Xoops\Core\Kernel\XoopsPersistableObjectHandler;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, XoopsPersistableObjectHandler. Consider defining an alias.

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...
17
18
/**
19
 * @copyright       XOOPS Project (http://xoops.org)
20
 * @license         GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
21
 * @package         Menus
22
 * @since           1.0
23
 * @author          trabis <[email protected]>
24
 * @version         $Id$
25
 */
26
27
class MenusMenu extends XoopsObject
28
{
29
    /**
30
     * constructor
31
     */
32
    public function __construct()
33
    {
34
        $this->initVar('id', XOBJ_DTYPE_INT);
35
        $this->initVar('pid', XOBJ_DTYPE_INT);
36
        $this->initVar('mid', XOBJ_DTYPE_INT);
37
        $this->initVar('title', XOBJ_DTYPE_TXTBOX, '');
38
        $this->initVar('alt_title', XOBJ_DTYPE_TXTBOX, '');
39
        $this->initVar('visible', XOBJ_DTYPE_INT, 1);
40
        $this->initVar('link', XOBJ_DTYPE_TXTBOX);
41
        $this->initVar('weight', XOBJ_DTYPE_INT, 255);
42
        $this->initVar('target', XOBJ_DTYPE_TXTBOX, '_self');
43
        $this->initVar('groups', XOBJ_DTYPE_ARRAY, serialize(array(FixedGroups::ANONYMOUS, FixedGroups::USERS)));
44
        $this->initVar('hooks', XOBJ_DTYPE_ARRAY, serialize(array()));
45
        $this->initVar('image', XOBJ_DTYPE_TXTBOX);
46
        $this->initVar('css', XOBJ_DTYPE_TXTBOX);
47
    }
48
}
49
50
class MenusMenuHandler extends XoopsPersistableObjectHandler
51
{
52
    /**
53
     * @param Connection $db
54
     */
55
    public function __construct(Connection $db = null)
56
    {
57
        parent::__construct($db, 'menus_menu', 'MenusMenu', 'id', 'title');
58
    }
59
60
    /**
61
     * @param MenusMenu $obj
62
     */
63
    public function updateWeights(MenusMenu $obj)
64
    {
65
        $sql = "UPDATE " . $this->table
66
        . " SET weight = weight+1"
67
        . " WHERE weight >= " . $obj->getVar('weight')
68
        . " AND id <> " . $obj->getVar('id')
69
        /*. " AND pid = " . $obj->getVar('pid')*/
70
        . " AND mid = " . $obj->getVar('mid')
71
        ;
72
        $originalForce = $this->db2->getForce();
73
        $this->db2->setForce(true);
74
        $this->db2->query($sql);
75
76
        $sql = "SELECT id FROM " . $this->table
77
        . " WHERE mid = " . $obj->getVar('mid')
78
        /*. " AND pid = " . $obj->getVar('pid')*/
79
        . " ORDER BY weight ASC"
80
        ;
81
        $result = $this->db2->query($sql);
82
        $i = 1;  //lets start at 1 please!
83
        while (false !== (list($id) = $result->fetch(FetchMode::NUMERIC))) {
84
            $sql = "UPDATE " . $this->table
85
            . " SET weight = {$i}"
86
            . " WHERE id = {$id}"
87
            ;
88
            $this->db2->query($sql);
89
            ++$i;
90
        }
91
        $this->db2->setForce($originalForce);
92
    }
93
}
94