Passed
Push — master ( ae895e...eec3d3 )
by Michael
10:44
created

tableExists()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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