Passed
Push — master ( 0f0242...f1016d )
by Goffy
05:12
created

LanguageMain::getLanguageMain()   F

Complexity

Conditions 12
Paths 408

Size

Total Lines 119
Code Lines 102

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 102
nc 408
nop 2
dl 0
loc 119
rs 2.8977
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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
namespace XoopsModules\Modulebuilder\Files\Language;
4
5
use XoopsModules\Modulebuilder;
6
use XoopsModules\Modulebuilder\Files;
7
8
/*
9
 You may not change or alter any portion of this comment or credits
10
 of supporting developers from this source code or any supporting source code
11
 which is considered copyrighted (c) material of the original comment or credit authors.
12
13
 This program is distributed in the hope that it will be useful,
14
 but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
 */
17
/**
18
 * modulebuilder module.
19
 *
20
 * @copyright       XOOPS Project (https://xoops.org)
21
 * @license         GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
22
 *
23
 * @since           2.5.0
24
 *
25
 * @author          Txmod Xoops http://www.txmodxoops.org
26
 *
27
 */
28
29
/**
30
 * Class LanguageMain.
31
 */
32
class LanguageMain extends Files\CreateFile
33
{
34
    /**
35
     * @var mixed
36
     */
37
    private $ld = null;
38
39
    /**
40
     * @var mixed
41
     */
42
    private $pc = null;
43
44
    /**
45
     * @public function constructor
46
     * @param null
47
     */
48
    public function __construct()
49
    {
50
        parent::__construct();
51
        $this->ld = LanguageDefines::getInstance();
52
        $this->pc = Modulebuilder\Files\CreatePhpCode::getInstance();
53
    }
54
55
    /**
56
     * @static function getInstance
57
     * @param null
58
     * @return LanguageMain
59
     */
60
    public static function getInstance()
61
    {
62
        static $instance = false;
63
        if (!$instance) {
64
            $instance = new self();
65
        }
66
67
        return $instance;
68
    }
69
70
    /**
71
     * @public function write
72
     * @param string $module
73
     * @param mixed  $tables
74
     * @param string $filename
75
     */
76
    public function write($module, $tables, $filename)
77
    {
78
        $this->setModule($module);
79
        $this->setFileName($filename);
80
        $this->setTables($tables);
81
    }
82
83
    /**
84
     * @private function getLanguageMain
85
     * @param string $module
86
     * @param string $language
87
     *
88
     * @return string
89
     */
90
    private function getLanguageMain($module, $language)
91
    {
92
        /** @var \XoopsModules\Modulebuilder\Utility $utility */
93
        $utility = new \XoopsModules\Modulebuilder\Utility();
94
95
        $moduleName = $module->getVar('mod_name');
96
        $tables     = $this->getTables();
97
        $ret        = $this->ld->getBlankLine();
98
        $ret        .= $this->pc->getPhpCodeIncludeDir('__DIR__', 'admin', true);
99
        $ret        .= $this->ld->getBlankLine();
100
        $ret        .= $this->ld->getAboveHeadDefines('Main');
101
        $ret        .= $this->ld->getDefine($language, 'INDEX', 'Home');
102
        $ret        .= $this->ld->getDefine($language, 'TITLE', (string)$module->getVar('mod_name'));
103
        $ret        .= $this->ld->getDefine($language, 'DESC', (string)$module->getVar('mod_description'));
104
        $ret        .= $this->ld->getDefine(
105
            $language,
106
            'INDEX_DESC',
107
            "Welcome to the homepage of your new module {$moduleName}!<br>
108
As you can see, you have created a page with a list of links at the top to navigate between the pages of your module. This description is only visible on the homepage of this module, the other pages you will see the content you created when you built this module with the module ModuleBuilder, and after creating new content in admin of this module. In order to expand this module with other resources, just add the code you need to extend the functionality of the same. The files are grouped by type, from the header to the footer to see how divided the source code.<br><br>If you see this message, it is because you have not created content for this module. Once you have created any type of content, you will not see this message.<br><br>If you liked the module ModuleBuilder and thanks to the long process for giving the opportunity to the new module to be created in a moment, consider making a donation to keep the module ModuleBuilder and make a donation using this button <a href='http://www.txmodxoops.org/modules/xdonations/index.php' title='Donation To Txmod Xoops'><img src='https://www.paypal.com/en_US/i/btn/btn_donate_LG.gif' alt='Button Donations' /></a><br>Thanks!<br><br>Use the link below to go to the admin and create content.",
109
            true
110
        );
111
        $ret        .= $this->ld->getDefine($language, 'NO_PDF_LIBRARY', 'Libraries TCPDF not there yet, upload them in root/Frameworks');
112
        $ret        .= $this->ld->getDefine($language, 'NO', 'No');
113
        $ret        .= $this->ld->getDefine($language, 'DETAILS', 'Show details');
114
        $ret        .= $this->ld->getDefine($language, 'BROKEN', 'Notify broken');
115
        $ret        .= $this->ld->getAboveHeadDefines('Contents');
116
        $ucfTableName     = '';
117
        $ucfTableSoleName = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $ucfTableSoleName is dead and can be removed.
Loading history...
118
        $stuTableSoleName = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $stuTableSoleName is dead and can be removed.
Loading history...
119
        $tableSoleName    = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $tableSoleName is dead and can be removed.
Loading history...
120
        $tableSubmit      = 0;
121
        $tableBroken      = 0;
122
        $tableRate        = 0;
123
        $tablePrint       = 0;
124
        foreach (\array_keys($tables) as $i) {
125
            if (1 == $tables[$i]->getVar('table_user')) {
126
                $tableName = $tables[$i]->getVar('table_name');
127
                $tableSoleName = $tables[$i]->getVar('table_solename');
128
                if (1 === (int)$tables[$i]->getVar('table_submit')) {
129
                    $tableSubmit = 1;
130
                }
131
                if (1 === (int)$tables[$i]->getVar('table_broken')) {
132
                    $tableBroken = 1;
133
                }
134
                if (1 === (int)$tables[$i]->getVar('table_rate')) {
135
                    $tableRate = 1;
136
                }
137
                if (1 === (int)$tables[$i]->getVar('table_print')) {
138
                    $tablePrint = 1;
139
                }
140
                $stuTableName = \mb_strtoupper($tableName);
141
                $stuTableSoleName = \mb_strtoupper($tableSoleName);
142
                $ucfTableName = $utility::UcFirstAndToLower($tableName);
143
                $ucfTableSoleName = $utility::UcFirstAndToLower($tableSoleName);
144
                $ret .= $this->ld->getAboveDefines($ucfTableSoleName);
145
                $ret .= $this->ld->getDefine($language, $stuTableSoleName, $ucfTableSoleName);
146
                $ret .= $this->ld->getDefine($language, $stuTableName, $ucfTableName);
147
                $ret .= $this->ld->getDefine($language, "{$stuTableName}_TITLE", "{$ucfTableName} title");
148
                $ret .= $this->ld->getDefine($language, "{$stuTableName}_DESC", "{$ucfTableName} description");
149
                $ret .= $this->ld->getDefine($language, "{$stuTableName}_LIST", "List of {$ucfTableName}");
150
                $ret .= $this->ld->getAboveDefines("Caption of {$ucfTableSoleName}");
151
                $fields = $this->getTableFields($tables[$i]->getVar('table_mid'), $tables[$i]->getVar('table_id'));
152
                foreach (\array_keys($fields) as $f) {
153
                    $fieldName = $fields[$f]->getVar('field_name');
154
                    $rpFieldName = $this->getRightString($fieldName);
155
                    $fieldNameDesc = \ucfirst($rpFieldName);
156
                    $ret .= $this->ld->getDefine($language, $stuTableSoleName . '_' . $rpFieldName, $fieldNameDesc);
157
                }
158
            }
159
        }
160
        $ret .= $this->ld->getDefine($language, 'INDEX_THEREARE', "There are %s {$ucfTableName}");
161
        $ret .= $this->ld->getDefine($language, 'INDEX_LATEST_LIST', "Last {$module->getVar('mod_name')}");
162
        if (1 === $tableSubmit) {
163
            $ret .= $this->ld->getAboveDefines('Submit');
164
            $ret .= $this->ld->getDefine($language, 'SUBMIT', 'Submit');
165
            $ret .= $this->ld->getAboveDefines('Form');
166
            $ret .= $this->ld->getDefine($language, 'FORM_OK', 'Successfully saved');
167
            $ret .= $this->ld->getDefine($language, 'FORM_DELETE_OK', 'Successfully deleted');
168
            $ret .= $this->ld->getDefine($language, 'FORM_SURE_DELETE', "Are you sure to delete: <b><span style='color : Red;'>%s </span></b>", true);
169
            $ret .= $this->ld->getDefine($language, 'FORM_SURE_RENEW', "Are you sure to update: <b><span style='color : Red;'>%s </span></b>", true);
170
            if (1 === $tableBroken) {
171
                $ret .= $this->ld->getDefine($language, 'FORM_SURE_BROKEN', "Are you sure to notify as broken: <b><span style='color : Red;'>%s </span></b>", true);
172
            }
173
            $ret .= $this->ld->getDefine($language, 'INVALID_PARAM', "Invalid parameter", true);
174
        }
175
        if (1 === $tableRate) {
176
            $ret .= $this->ld->getAboveHeadDefines('Ratings');
177
            $ret .= $this->ld->getDefine($language, 'RATING_CURRENT_1', 'Rating: %c / %m (%t rating totally)');
178
            $ret .= $this->ld->getDefine($language, 'RATING_CURRENT_X', 'Rating: %c / %m (%t ratings totally)');
179
            $ret .= $this->ld->getDefine($language, 'RATING_CURRENT_SHORT_1', '%c (%t rating)');
180
            $ret .= $this->ld->getDefine($language, 'RATING_CURRENT_SHORT_X', '%c (%t ratings)');
181
            $ret .= $this->ld->getDefine($language, 'RATING1', '1 of 5');
182
            $ret .= $this->ld->getDefine($language, 'RATING2', '2 of 5');
183
            $ret .= $this->ld->getDefine($language, 'RATING3', '3 of 5');
184
            $ret .= $this->ld->getDefine($language, 'RATING4', '4 of 5');
185
            $ret .= $this->ld->getDefine($language, 'RATING5', '5 of 5');
186
            $ret .= $this->ld->getDefine($language, 'RATING_10_1', '1 of 10');
187
            $ret .= $this->ld->getDefine($language, 'RATING_10_2', '2 of 10');
188
            $ret .= $this->ld->getDefine($language, 'RATING_10_3', '3 of 10');
189
            $ret .= $this->ld->getDefine($language, 'RATING_10_4', '4 of 10');
190
            $ret .= $this->ld->getDefine($language, 'RATING_10_5', '5 of 10');
191
            $ret .= $this->ld->getDefine($language, 'RATING_10_6', '6 of 10');
192
            $ret .= $this->ld->getDefine($language, 'RATING_10_7', '7 of 10');
193
            $ret .= $this->ld->getDefine($language, 'RATING_10_8', '8 of 10');
194
            $ret .= $this->ld->getDefine($language, 'RATING_10_9', '9 of 10');
195
            $ret .= $this->ld->getDefine($language, 'RATING_10_10', '10 of 10');
196
            $ret .= $this->ld->getDefine($language, 'RATING_VOTE_BAD', 'Invalid vote');
197
            $ret .= $this->ld->getDefine($language, 'RATING_VOTE_ALREADY', 'You have already voted');
198
            $ret .= $this->ld->getDefine($language, 'RATING_VOTE_THANKS', 'Thank you for rating');
199
            $ret .= $this->ld->getDefine($language, 'RATING_NOPERM', "Sorry, you don't have permission to rate items", true);
200
            $ret .= $this->ld->getDefine($language, 'RATING_LIKE', 'Like');
201
            $ret .= $this->ld->getDefine($language, 'RATING_DISLIKE', 'Dislike');
202
            $ret .= $this->ld->getDefine($language, 'RATING_ERROR1', 'Error: update base table failed!');
203
        }
204
        if (1 === $tablePrint) {
205
            $ret .= $this->ld->getAboveHeadDefines('Print');
206
            $ret .= $this->ld->getDefine($language, 'PRINT', 'Print');
207
        }
208
        return $ret;
209
    }
210
211
    /**
212
     * @private function getLanguageMainFooter
213
     * @param string $language
214
     *
215
     * @return string
216
     */
217
    private function getLanguageMainFooter($language)
218
    {
219
        $ret = $this->ld->getAboveDefines('Admin link');
220
        $ret .= $this->ld->getDefine($language, 'ADMIN', 'Admin');
221
        $ret .= $this->ld->getBelowDefines('End');
222
        $ret .= $this->ld->getBlankLine();
223
224
        return $ret;
225
    }
226
227
    /**
228
     * @public function render
229
     * @param null
230
     * @return bool|string
231
     */
232
    public function render()
233
    {
234
        $module        = $this->getModule();
235
        $filename      = $this->getFileName();
236
        $moduleDirname = $module->getVar('mod_dirname');
237
        $language      = $this->getLanguage($moduleDirname, 'MA');
238
        $content       = $this->getHeaderFilesComments($module);
239
        $content       .= $this->getLanguageMain($module, $language);
240
        $content       .= $this->getLanguageMainFooter($language);
241
242
        $this->create($moduleDirname, 'language/' . $GLOBALS['xoopsConfig']['language'], $filename, $content, _AM_MODULEBUILDER_FILE_CREATED, _AM_MODULEBUILDER_FILE_NOTCREATED);
243
244
        return $this->renderFile();
245
    }
246
}
247