Completed
Push — master ( 358680...e9acbc )
by Gino
04:15
created

update.php ➔ update_tdmcreate_v191()   C

Complexity

Conditions 9
Paths 42

Size

Total Lines 53
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 9
eloc 34
c 2
b 0
f 0
nc 42
nop 1
dl 0
loc 53
rs 6.8963

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
 * tdmcreate module.
14
 *
15
 * @copyright       The XOOPS Project http://sourceforge.net/projects/xoops/
16
 * @license         GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
17
 *
18
 * @since           2.5.0
19
 *
20
 * @author          Txmod Xoops http://www.txmodxoops.org
21
 *
22
 * @version         $Id: update.php 12258 2014-01-02 09:33:29Z timgno $
23
 */
24
25
/**
26
 * @param      $module
27
 * @param null $prev_version
28
 *
29
 * @return bool|null
30
 */
31
function xoops_module_update_tdmcreate(&$module, $prev_version = null)
32
{
33
    // irmtfan bug fix: solve templates duplicate issue
34
    $ret = null;
35
    if ($prev_version < 191) {
36
        $ret = update_system_v191($module);
37
    }
38
    $errors = $module->getErrors();
39
    if (!empty($errors)) {
40
        print_r($errors);
41
    }
42
43
    return $ret;
44
    // irmtfan bug fix: solve templates duplicate issue
45
}
46
47
// irmtfan bug fix: solve templates duplicate issue
48
/**
49
 * @param $module
50
 *
51
 * @return bool
52
 */
53
function update_tdmcreate_v191(&$module)
54
{
55
    global $xoopsDB;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
56
    $result = $xoopsDB->query(
57
        'SELECT t1.tpl_id FROM '.$xoopsDB->prefix('tplfile').' t1, '.$xoopsDB->prefix('tplfile')
58
        .' 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'
59
    );
60
    $tplids = array();
61
    while (list($tplid) = $xoopsDB->fetchRow($result)) {
62
        $tplids[] = $tplid;
63
    }
64
    if (count($tplids) > 0) {
65
        $tplfile_handler = &xoops_getHandler('tplfile');
66
        $duplicate_files = $tplfile_handler->getObjects(
67
            new Criteria('tpl_id', '('.implode(',', $tplids).')', 'IN')
68
        );
69
70
        if (count($duplicate_files) > 0) {
71
            foreach (array_keys($duplicate_files) as $i) {
72
                $tplfile_handler->delete($duplicate_files[$i]);
73
            }
74
        }
75
    }
76
    $sql = 'SHOW INDEX FROM '.$xoopsDB->prefix('tplfile')." WHERE KEY_NAME = 'tpl_refid_module_set_file_type'";
77
    if (!$result = $xoopsDB->queryF($sql)) {
78
        xoops_error($this->db->error().'<br />'.$sql);
0 ignored issues
show
Bug introduced by
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
79
80
        return false;
81
    }
82
    $ret = array();
83
    while ($myrow = $xoopsDB->fetchArray($result)) {
84
        $ret[] = $myrow;
85
    }
86
    if (!empty($ret)) {
87
        $module->setErrors(
88
            "'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."
89
        );
90
91
        return true;
92
    }
93
    $sql = 'ALTER TABLE '.$xoopsDB->prefix('tplfile')
94
        .' ADD UNIQUE tpl_refid_module_set_file_type ( tpl_refid, tpl_module, tpl_tplset, tpl_file, tpl_type )';
95
    if (!$result = $xoopsDB->queryF($sql)) {
96
        xoops_error($xoopsDB->error().'<br />'.$sql);
97
        $module->setErrors(
98
            "'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
101
        return false;
102
    }
103
104
    return true;
105
}
106
// irmtfan bug fix: solve templates duplicate issue
107
108