update_tdmcreate_v10()   B
last analyzed

Complexity

Conditions 9
Paths 42

Size

Total Lines 44
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 9
eloc 28
nc 42
nop 1
dl 0
loc 44
rs 8.0555
c 1
b 0
f 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
 * Tdmcreate module for xoops
14
 *
15
 * @param mixed      $module
16
 * @param null|mixed $prev_version
17
 * @package        Tdmcreate
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_tdmcreate(&$module, $prev_version = null)
33
{
34
    $ret = null;
35
    if ($prev_version < 10) {
36
        $ret = update_tdmcreate_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 = tdmcreate_check_db($module);
40
41
    //check upload directory
42
	include_once __DIR__ . '/install.php';
43
    $ret = xoops_module_install_tdmcreate($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_tdmcreate_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 tdmcreate_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 tdmcreate_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
    /*
119
    // Example: update table (add new field)
120
    $table   = $GLOBALS['xoopsDB']->prefix('tdmcreate_images');
121
    $field   = 'img_exif';
122
    $check   = $GLOBALS['xoopsDB']->queryF('SHOW COLUMNS FROM `' . $table . "` LIKE '" . $field . "'");
123
    $numRows = $GLOBALS['xoopsDB']->getRowsNum($check);
124
    if (!$numRows) {
125
        $sql = "ALTER TABLE `$table` ADD `$field` TEXT NULL AFTER `img_state`;";
126
        if (!$result = $GLOBALS['xoopsDB']->queryF($sql)) {
127
            xoops_error($GLOBALS['xoopsDB']->error() . '<br>' . $sql);
128
            $module->setErrors("Error when adding '$field' to table '$table'.");
129
            $ret = false;
130
        }
131
    }
132
133
    // Example: create new table
134
    $table   = $GLOBALS['xoopsDB']->prefix('tdmcreate_categories');
135
    $check   = $GLOBALS['xoopsDB']->queryF("SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='$table'");
136
    $numRows = $GLOBALS['xoopsDB']->getRowsNum($check);
137
    if (!$numRows) {
138
        // create new table 'tdmcreate_categories'
139
        $sql = "CREATE TABLE `$table` (
140
                  `cat_id`        INT(8) UNSIGNED NOT NULL AUTO_INCREMENT,
141
                  `cat_text`      VARCHAR(100)    NOT NULL DEFAULT '',
142
                  `cat_date`      INT(8)          NOT NULL DEFAULT '0',
143
                  `cat_submitter` INT(8)          NOT NULL DEFAULT '0',
144
                  PRIMARY KEY (`cat_id`)
145
                ) ENGINE=InnoDB;";
146
        if (!$result = $GLOBALS['xoopsDB']->queryF($sql)) {
147
            xoops_error($GLOBALS['xoopsDB']->error() . '<br>' . $sql);
148
            $module->setErrors("Error when creating table '$table'.");
149
            $ret = false;
150
        }
151
    }
152
    */
153
    return $ret;
154
}
155