xoops_module_update_mymodule()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 4
nop 2
dl 0
loc 19
rs 9.9332
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
/**
13
 * Mymodule module for xoops
14
 *
15
 * @param mixed      $module
16
 * @param null|mixed $prev_version
17
 * @package        Mymodule
18
 * @since          1.0
19
 * @min_xoops      2.5.9
20
 * @author         Wedega - Email:<[email protected]> - Website:<https://wedega.com>
21
 * @version        $Id: 1.0 update.php 1 Mon 2018-03-19 10:04:53Z XOOPS Project (www.xoops.org) $
22
 * @copyright      module for xoops
23
 * @license        GPL 2.0 or later
24
 */
25
26
/**
27
 * @param      $module
28
 * @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...
29
 *
30
 * @return bool|null
31
 */
32
function xoops_module_update_mymodule(&$module, $prev_version = null)
33
{
34
    $ret = null;
35
    if ($prev_version < 10) {
36
        $ret = update_mymodule_v10($module);
0 ignored issues
show
Unused Code introduced by
The assignment to $ret is dead and can be removed.
Loading history...
37
    }
38
39
    $ret = mymodule_check_db($module);
40
41
    //check upload directory
42
	include_once __DIR__ . '/install.php';
43
    $ret = xoops_module_install_mymodule($module);
44
45
    $errors = $module->getErrors();
46
    if (!empty($errors)) {
47
        print_r($errors);
48
    }
49
50
    return $ret;
51
52
}
53
54
// irmtfan bug fix: solve templates duplicate issue
55
/**
56
 * @param $module
57
 *
58
 * @return bool
59
 */
60
function update_mymodule_v10($module)
61
{
62
    global $xoopsDB;
63
    $result = $xoopsDB->query(
64
        'SELECT t1.tpl_id FROM ' . $xoopsDB->prefix('tplfile') . ' t1, ' . $xoopsDB->prefix('tplfile') . ' t2 WHERE t1.tpl_refid = t2.tpl_refid AND t1.tpl_module = t2.tpl_module AND t1.tpl_tplset=t2.tpl_tplset AND t1.tpl_file = t2.tpl_file AND t1.tpl_type = t2.tpl_type AND t1.tpl_id > t2.tpl_id'
65
    );
66
    $tplids = [];
67
    while (false !== (list($tplid) = $xoopsDB->fetchRow($result))) {
68
        $tplids[] = $tplid;
69
    }
70
    if (count($tplids) > 0) {
71
        $tplfileHandler  = xoops_getHandler('tplfile');
72
        $duplicate_files = $tplfileHandler->getObjects(new \Criteria('tpl_id', '(' . implode(',', $tplids) . ')', 'IN'));
0 ignored issues
show
Bug introduced by
The method getObjects() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of said class. However, the method does not exist in XoopsRankHandler or XoUserHandler. Are you sure you never get one of those? ( Ignorable by Annotation )

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

72
        /** @scrutinizer ignore-call */ 
73
        $duplicate_files = $tplfileHandler->getObjects(new \Criteria('tpl_id', '(' . implode(',', $tplids) . ')', 'IN'));
Loading history...
73
74
        if (count($duplicate_files) > 0) {
75
            foreach (array_keys($duplicate_files) as $i) {
76
                $tplfileHandler->delete($duplicate_files[$i]);
77
            }
78
        }
79
    }
80
    $sql = 'SHOW INDEX FROM ' . $xoopsDB->prefix('tplfile') . " WHERE KEY_NAME = 'tpl_refid_module_set_file_type'";
81
    if (!$result = $xoopsDB->queryF($sql)) {
82
        xoops_error($xoopsDB->error() . '<br>' . $sql);
83
84
        return false;
85
    }
86
    $ret = [];
87
    while (false !== ($myrow = $xoopsDB->fetchArray($result))) {
88
        $ret[] = $myrow;
89
    }
90
    if (!empty($ret)) {
91
        $module->setErrors("'tpl_refid_module_set_file_type' unique index is exist. Note: check 'tplfile' table to be sure this index is UNIQUE because XOOPS CORE need it.");
92
93
        return true;
94
    }
95
    $sql = 'ALTER TABLE ' . $xoopsDB->prefix('tplfile') . ' ADD UNIQUE tpl_refid_module_set_file_type ( tpl_refid, tpl_module, tpl_tplset, tpl_file, tpl_type )';
96
    if (!$result = $xoopsDB->queryF($sql)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
97
        xoops_error($xoopsDB->error() . '<br>' . $sql);
98
        $module->setErrors("'tpl_refid_module_set_file_type' unique index is not added to 'tplfile' table. Warning: do not use XOOPS until you add this unique index.");
99
100
        return false;
101
    }
102
103
    return true;
104
}
105
106
// irmtfan bug fix: solve templates duplicate issue
107
108
/**
109
 * @param $module
110
 *
111
 * @return bool
112
 */
113
function mymodule_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

113
function mymodule_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...
114
{
115
    $ret = true;
116
	//insert here code for database check
117
118
    return $ret;
119
}
120