Passed
Pull Request — master (#18)
by Michael
02:01
created

Updater   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 186
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 62
dl 0
loc 186
rs 10
c 0
b 0
f 0
wmc 15

3 Methods

Rating   Name   Duplication   Size   Complexity  
B createUpdateTable() 0 42 9
A checkInfoTemplates() 0 9 2
A checkInfoTable() 0 107 4
1
<?php namespace XoopsModules\Mymenus;
2
3
/**
4
 * Class Updater
5
 * @package XoopsModules\Mymenus
6
 */
7
class Updater
8
{
9
10
    // =========================================================================================
11
    // This class updates any existing table of a < 1.50 version to the format used
12
    // in the release of Mymenus 1.51
13
    // =========================================================================================
14
15
    /**
16
     * @param $module
17
     *
18
     * @return bool
19
     */
20
    public static function checkInfoTemplates(\XoopsObject $module)
0 ignored issues
show
Bug introduced by
The type XoopsObject was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
    {
22
        $err = true;
23
        if (!file_exists(XOOPS_ROOT_PATH . '/modules/' . $module->getInfo('dirname') . '/templates/blocks/' . $module->getInfo('dirname') . '_block.tpl')) {
0 ignored issues
show
Bug introduced by
The constant XoopsModules\Mymenus\XOOPS_ROOT_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
24
            $module->setErrors('Template ' . $module->getInfo('dirname') . '_block.tpl not exists!');
25
            $err = false;
26
        }
27
28
        return $err;
29
    }
30
31
    /**
32
     * @param $module
33
     *
34
     * @return bool
35
     */
36
    public static function checkInfoTable(\XoopsObject $module)
37
    {
38
        //    global $xoopsDB;
39
        $err = true;
0 ignored issues
show
Unused Code introduced by
The assignment to $err is dead and can be removed.
Loading history...
40
41
        $tables_menus = [
42
            'id'    => 'int(5) NOT NULL auto_increment',
43
            'title' => "varchar(255) NOT NULL default ''",
44
            'css'   => "varchar(255) NOT NULL default ''"
45
        ];
46
47
        $tables_links = [
48
            'id'        => 'int(5) NOT NULL auto_increment',
49
            'pid'       => "int(5) NOT NULL default '0'",
50
            'mid'       => "int(5) NOT NULL default '0'",
51
            'title'     => "varchar(150) NOT NULL default ''",
52
            'alt_title' => "varchar(255) NOT NULL default ''",
53
            'visible'   => "tinyint(1) NOT NULL default '0'",
54
            'link'      => 'varchar(255) default NULL',
55
            'weight'    => "tinyint(4) NOT NULL default '0'",
56
            'target'    => 'varchar(10) default NULL',
57
            'groups'    => 'text default NULL',
58
            'hooks'     => 'text default NULL',
59
            'image'     => 'varchar(255) default NULL',
60
            'css'       => 'varchar(255) default NULL'
61
        ];
62
63
        /*
64
65
        // CREATE or ALTER 'mymenus_menus' table
66
        if (!InfoTableExists($GLOBALS['xoopsDB']->prefix($module->getInfo("dirname")) . '_menus')) {
67
            $sql = "CREATE TABLE " . $GLOBALS['xoopsDB']->prefix($module->getInfo("dirname")) . "_menus (";
68
            foreach ($tables_menus as $s => $w) {
69
                $sql .= " " . $s . " " . $w . ",";
70
            }
71
            $sql .= " PRIMARY KEY (id)); ";
72
            echo $sql;
73
            $result = $GLOBALS['xoopsDB']->queryF($sql);
74
            if (!$result) {
75
                $module->setErrors("Can't create Table " . $GLOBALS['xoopsDB']->prefix($module->getInfo("dirname")) . '_menus');
76
                return false;
77
            } else {
78
                $sql    = "INSERT INTO " . $GLOBALS['xoopsDB']->prefix($module->getInfo("dirname")) . "_menus (id,title) VALUES (1,'Default')";
79
                $result = $GLOBALS['xoopsDB']->queryF($sql);
80
            }
81
        } else {
82
            foreach ($tables_menus as $s => $w) {
83
                if (!InfoColumnExists($GLOBALS['xoopsDB']->prefix($module->getInfo("dirname")) . '_menus', $s)) {
84
                    $sql    = "ALTER TABLE " . $GLOBALS['xoopsDB']->prefix($module->getInfo("dirname")) . "_menus ADD " . $s . " " . $w . ";";
85
                    $result = $GLOBALS['xoopsDB']->queryF($sql);
86
                } else {
87
                    $sql    = "ALTER TABLE " . $GLOBALS['xoopsDB']->prefix($module->getInfo("dirname")) . "_menus CHANGE " . $s . " " . $s . " " . $w . ";";
88
                    $result = $GLOBALS['xoopsDB']->queryF($sql);
89
                }
90
            }
91
        }
92
    */
93
94
        self::createUpdateTable($tables_menus, '_menus', $module);
95
96
        // RENAME TABLE 'mymenus_menu' TO 'mymenus_links'
97
        if (!InfoTableExists($GLOBALS['xoopsDB']->prefix($module->getInfo('dirname')) . '_links')) {
98
            if (InfoTableExists($GLOBALS['xoopsDB']->prefix($module->getInfo('dirname')) . '_menu')) {
99
                $sql    = 'RENAME TABLE ' . $GLOBALS['xoopsDB']->prefix($module->getInfo('dirname')) . '_menu TO ' . $GLOBALS['xoopsDB']->prefix($module->getInfo('dirname')) . '_links;';
100
                $result = $GLOBALS['xoopsDB']->queryF($sql);
101
                if (!$result) {
102
                    $module->setErrors("Can't rename Table " . $GLOBALS['xoopsDB']->prefix($module->getInfo('dirname')) . '_menu');
103
104
                    return false;
105
                }
106
            }
107
        }
108
109
        /*
110
      //---------------------------
111
          // CREATE or ALTER 'mymenus_links' table
112
          if (!InfoTableExists($GLOBALS['xoopsDB']->prefix($module->getInfo("dirname")) . "_links")) {
113
              $sql = "CREATE TABLE " . $GLOBALS['xoopsDB']->prefix($module->getInfo("dirname")) . "_links ( ";
114
              foreach ($tables_links as $c => $w) {
115
                  $sql .= " " . $c . " " . $w . ",";
116
              }
117
              $sql .= "  PRIMARY KEY  (id) ) ;";
118
              $result = $GLOBALS['xoopsDB']->queryF($sql);
119
              if (!$result) {
120
                  $module->setErrors("Can't create Table " . $GLOBALS['xoopsDB']->prefix($module->getInfo("dirname")) . "_links");
121
                  $sql    = 'DROP TABLE ' . $GLOBALS['xoopsDB']->prefix($module->getInfo("dirname")) . '_menus';
122
                  $result = $GLOBALS['xoopsDB']->queryF($sql);
123
                  return false;
124
              }
125
          } else {
126
              foreach ($tables_links as $s => $w) {
127
                  if (!InfoColumnExists($GLOBALS['xoopsDB']->prefix($module->getInfo("dirname")) . '_links', $s)) {
128
                      $sql    = "ALTER TABLE " . $GLOBALS['xoopsDB']->prefix($module->getInfo("dirname")) . "_links ADD " . $s . " " . $w . ";";
129
                      $result = $GLOBALS['xoopsDB']->queryF($sql);
130
                  } else {
131
                      $sql    = "ALTER TABLE " . $GLOBALS['xoopsDB']->prefix($module->getInfo("dirname")) . "_links CHANGE " . $s . " " . $s . " " . $w . ";";
132
                      $result = $GLOBALS['xoopsDB']->queryF($sql);
133
                  }
134
              }
135
          }
136
137
          //--------------------------
138
      */
139
140
        self::createUpdateTable($tables_links, '_links', $module);
141
142
        return true;
143
    }
144
145
    /**
146
     * @param array       $table
147
     * @param string      $tablename
148
     * @param \XoopsObject $module
149
     * @return bool|null
150
     */
151
    public static function createUpdateTable($table, $tablename, \XoopsObject $module)
152
    {
153
        if (!InfoTableExists($GLOBALS['xoopsDB']->prefix($module->getInfo('dirname')) . $tablename)) {
154
            $sql = 'CREATE TABLE ' . $GLOBALS['xoopsDB']->prefix($module->getInfo('dirname')) . $tablename . ' (';
155
            foreach ($table as $s => $w) {
156
                $sql .= ' ' . $s . ' ' . $w . ',';
157
            }
158
            $sql .= ' PRIMARY KEY (id)); ';
159
            //    echo $sql;
160
            $result = $GLOBALS['xoopsDB']->queryF($sql);
161
            if (!$result) {
162
                $module->setErrors("Can't create Table " . $GLOBALS['xoopsDB']->prefix($module->getInfo('dirname')) . $tablename);
163
164
                if ('_menu' === $tablename) {
165
                    return false;
166
                }
167
168
                if ('_links' === $tablename) {
169
                    $sql    = 'DROP TABLE ' . $GLOBALS['xoopsDB']->prefix($module->getInfo('dirname')) . '_menus';
170
                    $result = $GLOBALS['xoopsDB']->queryF($sql);
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
171
172
                    return false;
173
                }
174
            } else {
175
                if ('_menu' === $tablename) {
176
                    $sql    = 'INSERT INTO ' . $GLOBALS['xoopsDB']->prefix($module->getInfo('dirname')) . $tablename . " (id,title) VALUES (1,'Default')";
177
                    $result = $GLOBALS['xoopsDB']->queryF($sql);
178
                }
179
            }
180
        } else {
181
            foreach ($table as $s => $w) {
182
                if (!InfoColumnExists($GLOBALS['xoopsDB']->prefix($module->getInfo('dirname')) . $tablename, $s)) {
183
                    $sql    = 'ALTER TABLE ' . $GLOBALS['xoopsDB']->prefix($module->getInfo('dirname')) . $tablename . ' ADD ' . $s . ' ' . $w . ';';
184
                    $result = $GLOBALS['xoopsDB']->queryF($sql);
185
                } else {
186
                    $sql    = 'ALTER TABLE ' . $GLOBALS['xoopsDB']->prefix($module->getInfo('dirname')) . $tablename . ' CHANGE ' . $s . ' ' . $s . ' ' . $w . ';';
187
                    $result = $GLOBALS['xoopsDB']->queryF($sql);
188
                }
189
            }
190
        }
191
192
        return null;
193
    }
194
}
195