Passed
Push — master ( 646a8a...8b445d )
by Michael
14:01 queued 12s
created

tableExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 2
b 0
f 0
1
<?php declare(strict_types=1);
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
/**
13
 * @copyright    XOOPS Project (https://xoops.org)
14
 * @license      GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
15
 * @author       XOOPS Development Team
16
 */
17
18
use Xmf\Database\Tables;
19
use XoopsModules\Waiting\Common\Configurator;
20
use XoopsModules\Waiting\Helper;
21
use XoopsModules\Waiting\Utility;
22
23
if ((!defined('XOOPS_ROOT_PATH')) || !($GLOBALS['xoopsUser'] instanceof \XoopsUser)
24
    || !$GLOBALS['xoopsUser']->isAdmin()) {
25
    exit('Restricted access' . PHP_EOL);
26
}
27
28
/**
29
 * Prepares system prior to attempting to install module
30
 * @param \XoopsModule $module {@link XoopsModule}
31
 *
32
 * @return bool true if ready to install, false if not
33
 */
34
function xoops_module_pre_update_waiting(\XoopsModule $module)
35
{
36
    $moduleDirName = \basename(\dirname(__DIR__));
0 ignored issues
show
Unused Code introduced by
The assignment to $moduleDirName is dead and can be removed.
Loading history...
37
    /** @var \XoopsModules\Waiting\Helper $helper */
38
    /** @var \XoopsModules\Waiting\Utility $utility */
39
    $helper  = Helper::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $helper is dead and can be removed.
Loading history...
40
    $utility = new Utility();
41
42
    $xoopsSuccess = $utility::checkVerXoops($module);
43
    $phpSuccess   = $utility::checkVerPhp($module);
44
45
    return $xoopsSuccess && $phpSuccess;
46
}
47
48
/**
49
 * Performs tasks required during update of the module
50
 * @param \XoopsModule $module {@link XoopsModule}
51
 * @param null         $previousVersion
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $previousVersion is correct as it would always require null to be passed?
Loading history...
52
 *
53
 * @return bool true if update successful, false if not
54
 */
55
function xoops_module_update_waiting(\XoopsModule $module, $previousVersion = null)
56
{
57
    $moduleDirName      = \basename(\dirname(__DIR__));
58
    $moduleDirNameUpper = \mb_strtoupper($moduleDirName);
0 ignored issues
show
Unused Code introduced by
The assignment to $moduleDirNameUpper is dead and can be removed.
Loading history...
59
60
    /** @var \XoopsModules\Waiting\Helper $helper */ /** @var \XoopsModules\Waiting\Utility $utility */
61
    /** @var \XoopsModules\Waiting\Common\Configurator $configurator */
62
    $helper       = Helper::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $helper is dead and can be removed.
Loading history...
63
    $utility      = new Utility();
64
    $configurator = new Configurator();
65
66
    if ($previousVersion < 240) {
67
        //rename column EXAMPLE
68
        $tables     = new Tables();
69
        $table      = 'waitingx_categories';
70
        $column     = 'ordre';
71
        $newName    = 'order';
72
        $attributes = "INT(5) NOT NULL DEFAULT '0'";
73
        if ($tables->useTable($table)) {
74
            $tables->alterColumn($table, $column, $attributes, $newName);
75
            if (!$tables->executeQueue()) {
76
                echo '<br>' . _AM_WAITING_UPGRADEFAILED0 . ' ' . $tables->getLastError();
77
            }
78
        }
79
80
        //delete old HTML templates
81
        if (count($configurator->templateFolders) > 0) {
82
            foreach ($configurator->templateFolders as $folder) {
83
                $templateFolder = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $folder);
84
                if (is_dir($templateFolder)) {
85
                    $templateList = array_diff(scandir($templateFolder, SCANDIR_SORT_NONE), ['..', '.']);
86
                    foreach ($templateList as $k => $v) {
87
                        $fileInfo = new \SplFileInfo($templateFolder . $v);
88
                        if ('html' === $fileInfo->getExtension() && 'index.html' !== $fileInfo->getFilename()) {
89
                            if (is_file($templateFolder . $v)) {
90
                                unlink($templateFolder . $v);
91
                            }
92
                        }
93
                    }
94
                }
95
            }
96
        }
97
98
        //  ---  DELETE OLD FILES ---------------
99
        if (count($configurator->oldFiles) > 0) {
100
            //    foreach (array_keys($GLOBALS['uploadFolders']) as $i) {
101
            foreach (array_keys($configurator->oldFiles) as $i) {
102
                $tempFile = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $configurator->oldFiles[$i]);
103
                if (is_file($tempFile)) {
104
                    unlink($tempFile);
105
                }
106
            }
107
        }
108
109
        //  ---  DELETE OLD FOLDERS ---------------
110
        xoops_load('XoopsFile');
111
        if (count($configurator->oldFolders) > 0) {
112
            //    foreach (array_keys($GLOBALS['uploadFolders']) as $i) {
113
            foreach (array_keys($configurator->oldFolders) as $i) {
114
                $tempFolder = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $configurator->oldFolders[$i]);
115
                /** @var XoopsObjectHandler $folderHandler */
116
                $folderHandler = XoopsFile::getHandler('folder', $tempFolder);
117
                $folderHandler->delete($tempFolder);
118
            }
119
        }
120
121
        //  ---  CREATE FOLDERS ---------------
122
        if (count($configurator->uploadFolders) > 0) {
123
            //    foreach (array_keys($GLOBALS['uploadFolders']) as $i) {
124
            foreach (array_keys($configurator->uploadFolders) as $i) {
125
                $utility::createFolder($configurator->uploadFolders[$i]);
126
            }
127
        }
128
129
        //  ---  COPY blank.png FILES ---------------
130
        if (count($configurator->copyBlankFiles) > 0) {
131
            $file = \dirname(__DIR__) . '/assets/images/blank.png';
132
            foreach (array_keys($configurator->copyBlankFiles) as $i) {
133
                $dest = $configurator->copyBlankFiles[$i] . '/blank.png';
134
                $utility::copyFile($file, $dest);
135
            }
136
        }
137
138
        //delete .html entries from the tpl table
139
        $sql = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix('tplfile') . " WHERE `tpl_module` = '" . $module->getVar('dirname', 'n') . '\' AND `tpl_file` LIKE \'%.html%\'';
140
        $GLOBALS['xoopsDB']->queryF($sql);
141
142
        /** @var \XoopsGroupPermHandler $grouppermHandler */
143
        $grouppermHandler = xoops_getHandler('groupperm');
144
145
        return $grouppermHandler->deleteByModule($module->getVar('mid'), 'item_read');
146
    }
147
148
    return true;
149
}
150