wgevents_update_subcats()   A
last analyzed

Complexity

Conditions 5
Paths 2

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 12
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 19
rs 9.5555
1
<?php
2
3
/*
4
 You may not change or alter any portion of this comment or credits
5
 of supporting developers from this source code or any supporting source code
6
 which is considered copyrighted (c) material of the original comment or credit authors.
7
8
 This program is distributed in the hope that it will be useful,
9
 but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
*/
12
13
/**
14
 * Wgevents module for xoops
15
 *
16
 * @param mixed      $module
17
 * @param null|mixed $prev_version
18
 * @package        Wgevents
19
 * @since          1.0
20
 * @min_xoops      2.5.11
21
 * @author         Wedega - Email:<[email protected]> - Website:<https://wedega.com>
22
 * @version        $Id: 1.0 update.php 1 Mon 2018-03-19 10:04:53Z XOOPS Project (www.xoops.org) $
23
 * @copyright      module for xoops
24
 * @license        GPL 2.0 or later
25
 */
26
27
use XoopsModules\Wgevents\Common\ {
28
    Configurator,
29
    Migrate,
30
    MigrateHelper
31
};
32
33
/**
34
 * @param      $module
35
 * @param null $prev_version
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $prev_version is correct as it would always require null to be passed?
Loading history...
36
 *
37
 * @return bool|null
38
 */
39
function xoops_module_update_wgevents($module, $prev_version = null)
0 ignored issues
show
Unused Code introduced by
The parameter $prev_version is not used and could be removed. ( Ignorable by Annotation )

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

39
function xoops_module_update_wgevents($module, /** @scrutinizer ignore-unused */ $prev_version = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
40
{
41
    $moduleDirName = $module->dirname();
42
43
    //check preload folder and remove replace index.php by index.html if exist
44
    if (\is_file(\XOOPS_ROOT_PATH . '/modules/' . $moduleDirName . '/preloads/index.php')) {
45
        //delete olf file
46
        \unlink(\XOOPS_ROOT_PATH . '/modules/' . $moduleDirName . '/preloads/index.php');
47
        //create html file
48
        $myfile = \fopen(\XOOPS_ROOT_PATH . '/modules/' . $moduleDirName . '/preloads/index.html', 'wb');
49
        \fwrite($myfile, '<script>history.go(-1);</script>');
50
        \fclose($myfile);
51
    }
52
53
    //wgevents_check_db($module);
54
    
55
    // update DB corresponding to sql/mysql.sql
56
    $configurator = new Configurator();
57
    $migrate = new Migrate($configurator);
58
    //$migrate->saveCurrentSchema();
59
60
    $fileSql = \XOOPS_ROOT_PATH . '/modules/' . $moduleDirName . '/sql/mysql.sql';
61
    // ToDo: add function setDefinitionFile to .\class\libraries\vendor\xoops\xmf\src\Database\Migrate.php
62
    // Todo: once we are using setDefinitionFile this part has to be adapted
63
    //$fileYaml = \XOOPS_ROOT_PATH . '/modules/' . $moduleDirName . '/sql/update_' . $moduleDirName . '_migrate.yml';
64
    //try {
65
    //$migrate->setDefinitionFile('update_' . $moduleDirName);
66
    //} catch (\Exception $e) {
67
    // as long as this is not done default file has to be created
68
    $moduleVersionOld = $module->getInfo('version');
69
    $moduleVersionNew = \str_replace(['.', '-'], '_', $moduleVersionOld);
70
    $fileYaml = \XOOPS_ROOT_PATH . '/modules/' . $moduleDirName . "/sql/{$moduleDirName}_{$moduleVersionNew}_migrate.yml";
71
    //}
72
73
    // create a schema file based on sql/mysql.sql
74
    $migratehelper = new MigrateHelper($fileSql, $fileYaml);
75
    if (!$migratehelper->createSchemaFromSqlfile()) {
76
        \xoops_error('Error: creation schema file failed!');
77
        return false;
78
    }
79
80
    //create copy for XOOPS 2.5.11 Beta 1 and older versions
81
    $fileYaml2 = \XOOPS_ROOT_PATH . '/modules/' . $moduleDirName . "/sql/{$moduleDirName}_{$moduleVersionOld}_migrate.yml";
82
    \copy($fileYaml, $fileYaml2);
83
84
    // run standard procedure for db migration
85
    $migrate->getTargetDefinitions();
86
    $migrate->synchronizeSchema();
87
88
    //check upload directory
89
    require_once __DIR__ . '/install.php';
90
    xoops_module_install_wgevents($module);
91
92
    $moduleVersion = (int)\str_replace(['.', '-'], '', $module->getInfo('version'));
93
    if ($moduleVersion < 104) {
94
        wgevents_update_fee($module);
95
    }
96
    if ($moduleVersion < 105) {
97
        wgevents_update_subcats($module);
98
    }
99
100
    $errors = $module->getErrors();
101
    if (!empty($errors)) {
102
        \print_r($errors);
103
    }
104
105
    return true;
106
107
}
108
109
/**
110
 * @param $module
111
 *
112
 * @return bool
113
 */
114
function wgevents_update_fee($module)
115
{
116
    // transform float value into json
117
    $table   = $GLOBALS['xoopsDB']->prefix('wgevents_event');
118
    $result  = $GLOBALS['xoopsDB']->queryF('SELECT * FROM `' . $table . '`');
119
    $numRows = $GLOBALS['xoopsDB']->getRowsNum($result);
120
    if ($numRows > 0) {
121
        while ($row = $GLOBALS['xoopsDB']->fetchArray($result)) {
122
            $newValue = \json_encode([[$row['fee'], '']]);
123
            $sql = 'UPDATE `' . $table . "` SET `fee` = '" . $newValue . "' WHERE `" . $table . '`.`id` = ' . $row['id'];
124
            if (!$GLOBALS['xoopsDB']->queryF($sql)) {
125
                xoops_error($GLOBALS['xoopsDB']->error() . '<br>' . $sql);
126
                $module->setErrors("Error when updating 'fee' in table '$table'.");
127
            }
128
        }
129
    }
130
    return true;
131
}
132
/**
133
 * @param $module
134
 *
135
 * @return bool
136
 */
137
function wgevents_update_subcats($module)
138
{
139
    // fix wrong default value
140
    $table   = $GLOBALS['xoopsDB']->prefix('wgevents_event');
141
    $result  = $GLOBALS['xoopsDB']->queryF('SELECT * FROM `' . $table . '`');
142
    $numRows = $GLOBALS['xoopsDB']->getRowsNum($result);
143
    if ($numRows > 0) {
144
        while ($row = $GLOBALS['xoopsDB']->fetchArray($result)) {
145
            if ("'" === (string)$row['subcats']) {
146
                $newValue = \serialize([]);
147
                $sql = 'UPDATE `' . $table . "` SET `subcats` = '" . $newValue . "' WHERE `" . $table . '`.`id` = ' . $row['id'];
148
                if (!$GLOBALS['xoopsDB']->queryF($sql)) {
149
                    xoops_error($GLOBALS['xoopsDB']->error() . '<br>' . $sql);
150
                    $module->setErrors("Error when updating 'fee' in table '$table'.");
151
                }
152
            }
153
        }
154
    }
155
    return true;
156
}
157
158
/**
159
 * @param $module
160
 *
161
 * @return bool
162
 */
163
function wgevents_check_db($module)
0 ignored issues
show
Unused Code introduced by
The parameter $module is not used and could be removed. ( Ignorable by Annotation )

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

163
function wgevents_check_db(/** @scrutinizer ignore-unused */ $module)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
164
{
165
    //insert here code for database check
166
167
    /*
168
    // Example: update table (add new field)
169
    $table   = $GLOBALS['xoopsDB']->prefix('wgevents_images');
170
    $field   = 'img_exif';
171
    $check   = $GLOBALS['xoopsDB']->queryF('SHOW COLUMNS FROM `' . $table . "` LIKE '" . $field . "'");
172
    $numRows = $GLOBALS['xoopsDB']->getRowsNum($check);
173
    if (!$numRows) {
174
        $sql = "ALTER TABLE `$table` ADD `$field` TEXT NULL AFTER `img_state`;";
175
        if (!$result = $GLOBALS['xoopsDB']->queryF($sql)) {
176
            xoops_error($GLOBALS['xoopsDB']->error() . '<br>' . $sql);
177
            $module->setErrors("Error when adding '$field' to table '$table'.");
178
            $ret = false;
179
        }
180
    }
181
182
    // Example: create new table
183
    $table   = $GLOBALS['xoopsDB']->prefix('wgevents_categories');
184
    $check   = $GLOBALS['xoopsDB']->queryF("SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='$table'");
185
    $numRows = $GLOBALS['xoopsDB']->getRowsNum($check);
186
    if (!$numRows) {
187
        // create new table 'wgevents_categories'
188
        $sql = "CREATE TABLE `$table` (
189
                  `id`        INT(8) UNSIGNED NOT NULL AUTO_INCREMENT,
190
                  `text`      VARCHAR(100)    NOT NULL DEFAULT '',
191
                  `date`      INT(8)          NOT NULL DEFAULT '0',
192
                  `submitter` INT(8)          NOT NULL DEFAULT '0',
193
                  PRIMARY KEY (`id`)
194
                ) ENGINE=InnoDB;";
195
        if (!$result = $GLOBALS['xoopsDB']->queryF($sql)) {
196
            xoops_error($GLOBALS['xoopsDB']->error() . '<br>' . $sql);
197
            $module->setErrors("Error when creating table '$table'.");
198
            $ret = false;
199
        }
200
    }
201
    */
202
    return true;
203
}
204