Completed
Push — master ( e9acbc...ae1831 )
by Gino
06:34 queued 02:43
created

IncludeUpdate::render()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 14
rs 9.4285
c 1
b 1
f 0
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: IncludeUpdate.php 12258 2014-01-02 09:33:29Z timgno $
23
 */
24
25
/**
26
 * Class IncludeUpdate.
27
 */
28
class IncludeUpdate extends TDMCreateFile
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
29
{
30
    /*
31
    *  @public function constructor
32
    *  @param null
33
    */
34
    /**
35
     *
36
     */
37
    public function __construct()
38
    {
39
        parent::__construct();
40
    }
41
42
    /*
43
    *  @static function getInstance
44
    *  @param null
45
    */
46
    /**
47
     * @return IncludeUpdate
48
     */
49
    public static function getInstance()
50
    {
51
        static $instance = false;
52
        if (!$instance) {
53
            $instance = new self();
54
        }
55
56
        return $instance;
57
    }
58
59
    /*
60
    *  @public function write
61
    *  @param string $module
62
    *  @param string $filename
63
    */
64
    /**
65
     * @param $module
66
     * @param $filename
67
     */
68
    public function write($module, $filename)
69
    {
70
        $this->setModule($module);
71
        $this->setFileName($filename);
72
    }
73
74
    /*
75
    *  @private function getIncludeUpdateModule
76
    *  @param string $moduleDirname
77
    *  @param mixed $moduleVersion
78
    */
79
    /**
80
     * @param $moduleDirname
81
     * @param $moduleVersion
82
     *
83
     * @return string
84
     */
85
    private function getIncludeUpdateModule($moduleDirname, $moduleVersion)
86
    {
87
        $ret = <<<EOT
88
/**
89
 * @param      \$module
90
 * @param null \$prev_version
91
 *
92
 * @return bool|null
93
 */
94
function xoops_module_update_{$moduleDirname}(&\$module, \$prev_version = null)
95
{
96
    // irmtfan bug fix: solve templates duplicate issue
97
    \$ret = null;
98
    if (\$prev_version < {$moduleVersion}) {
99
        \$ret = update_{$moduleDirname}_v{$moduleVersion}(\$module);
100
    }
101
    \$errors = \$module->getErrors();
102
    if (!empty(\$errors)) {
103
        print_r(\$errors);
104
    }
105
106
    return \$ret;
107
    // irmtfan bug fix: solve templates duplicate issue
108
}
109
EOT;
110
111
        return $ret;
112
    }
113
114
    /*
115
    *  @private function getIncludeUpdateVersion
116
    *  @param string $moduleDirname
117
    *  @param mixed $moduleVersion
118
    */
119
    /**
120
     * @param $moduleDirname
121
     * @param $moduleVersion
122
     *
123
     * @return string
124
     */
125
    private function getIncludeUpdateVersion($moduleDirname, $moduleVersion)
126
    {
127
        $ret = <<<EOT
128
// irmtfan bug fix: solve templates duplicate issue
129
/**
130
 * @param \$module
131
 *
132
 * @return bool
133
 */
134
function update_{$moduleDirname}_v{$moduleVersion}(&\$module)
135
{
136
    global \$xoopsDB;
137
    \$result = \$xoopsDB->query(
138
        "SELECT t1.tpl_id FROM " . \$xoopsDB->prefix('tplfile') . " t1, " . \$xoopsDB->prefix('tplfile')
139
        . " 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"
140
    );
141
    \$tplids = array();
142
    while (list(\$tplid) = \$xoopsDB->fetchRow(\$result)) {
143
        \$tplids[] = \$tplid;
144
    }
145
    if (count(\$tplids) > 0) {
146
        \$tplfile_handler = xoops_getHandler('tplfile');
147
        \$duplicate_files = \$tplfile_handler->getObjects(
148
            new Criteria('tpl_id', "(" . implode(',', \$tplids) . ")", "IN")
149
        );
150
151
        if (count(\$duplicate_files) > 0) {
152
            foreach (array_keys(\$duplicate_files) as \$i) {
153
                \$tplfile_handler->delete(\$duplicate_files[\$i]);
154
            }
155
        }
156
    }
157
    \$sql = "SHOW INDEX FROM " . \$xoopsDB->prefix('tplfile') . " WHERE KEY_NAME = 'tpl_refid_module_set_file_type'";
158
    if (!\$result = \$xoopsDB->queryF(\$sql)) {
159
        xoops_error(\$this->db->error() . '<br />' . \$sql);
160
161
        return false;
162
    }
163
    \$ret = array();
164
    while (\$myrow = \$xoopsDB->fetchArray(\$result)) {
165
        \$ret[] = \$myrow;
166
    }
167
    if (!empty(\$ret)) {
168
        \$module->setErrors(
169
            "'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."
170
        );
171
172
        return true;
173
    }
174
    \$sql = "ALTER TABLE " . \$xoopsDB->prefix('tplfile')
175
        . " ADD UNIQUE tpl_refid_module_set_file_type ( tpl_refid, tpl_module, tpl_tplset, tpl_file, tpl_type )";
176
    if (!\$result = \$xoopsDB->queryF(\$sql)) {
177
        xoops_error(\$xoopsDB->error() . '<br />' . \$sql);
178
        \$module->setErrors(
179
            "'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."
180
        );
181
182
        return false;
183
    }
184
185
    return true;
186
}
187
// irmtfan bug fix: solve templates duplicate issue
188
EOT;
189
190
        return $ret;
191
    }
192
193
    /*
194
    *  @public function render
195
    *  @param null
196
    */
197
    /**
198
     * @return bool|string
199
     */
200
    public function render()
201
    {
202
        $module = $this->getModule();
203
        $filename = $this->getFileName();
204
        $moduleDirname = $module->getVar('mod_dirname');
205
        $moduleVersion = str_replace('.', '', $module->getVar('mod_version'));
206
        $content = $this->getHeaderFilesComments($module, $filename);
207
        $content .= $this->getIncludeUpdateModule($moduleDirname, $moduleVersion);
208
        $content .= $this->getIncludeUpdateVersion($moduleDirname, $moduleVersion);
209
        //
210
        $this->create($moduleDirname, 'include', $filename, $content, _AM_TDMCREATE_FILE_CREATED, _AM_TDMCREATE_FILE_NOTCREATED);
211
212
        return $this->renderFile();
213
    }
214
}
215