PagesItem::getTemplatesUserPagesItemPanel()   F
last analyzed

Complexity

Conditions 15
Paths 672

Size

Total Lines 108
Code Lines 86

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 15
eloc 86
c 1
b 0
f 0
nc 672
nop 8
dl 0
loc 108
rs 2.0698

How to fix   Long Method    Complexity    Many Parameters   

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:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace XoopsModules\Modulebuilder\Files\Templates\User\Defstyle;
4
5
use XoopsModules\Modulebuilder;
6
use XoopsModules\Modulebuilder\{
7
    Files,
8
    Constants
9
};
10
11
/*
12
 You may not change or alter any portion of this comment or credits
13
 of supporting developers from this source code or any supporting source code
14
 which is considered copyrighted (c) material of the original comment or credit authors.
15
16
 This program is distributed in the hope that it will be useful,
17
 but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19
 */
20
/**
21
 * modulebuilder module.
22
 *
23
 * @copyright       XOOPS Project (https://xoops.org)
24
 * @license         GNU GPL 2 (https://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
25
 *
26
 * @since           2.5.0
27
 *
28
 * @author          Txmod Xoops https://xoops.org 
29
 *                  Goffy https://myxoops.org
30
 *
31
 */
32
33
/**
34
 * class PagesItem.
35
 */
36
class PagesItem extends Files\CreateFile
37
{
38
    /**
39
     * @var mixed
40
     */
41
    private $hc = null;
42
43
    /**
44
     * @var mixed
45
     */
46
    private $sc = null;
47
48
    /**
49
     * @public function constructor
50
     * @param null
51
     */
52
    public function __construct()
53
    {
54
        parent::__construct();
55
        $this->hc = Modulebuilder\Files\CreateHtmlCode::getInstance();
56
        $this->sc = Modulebuilder\Files\CreateSmartyCode::getInstance();
57
    }
58
59
    /**
60
     * @static function getInstance
61
     * @param null
62
     * @return PagesItem
63
     */
64
    public static function getInstance()
65
    {
66
        static $instance = false;
67
        if (!$instance) {
68
            $instance = new self();
69
        }
70
71
        return $instance;
72
    }
73
74
    /**
75
     * @public function write
76
     * @param        $module
77
     * @param        $table
78
     * @param string $filename
79
     * @param        $tables
80
     */
81
    public function write($module, $table, $tables, $filename)
82
    {
83
        $this->setModule($module);
84
        $this->setTable($table);
85
        $this->setTables($tables);
86
        $this->setFileName($filename);
87
    }
88
89
    /**
90
     * @private function getTemplatesUserPagesItemPanel
91
     * @param string $moduleDirname
92
     * @param        $tableId
93
     * @param        $tableMid
94
     * @param        $tableName
95
     * @param        $tableSoleName
96
     * @param        $tableRate
97
     * @param        $tableBroken
98
     * @param        $language
99
     * @return string
100
     */
101
    private function getTemplatesUserPagesItemPanel($moduleDirname, $tableId, $tableMid, $tableName, $tableSoleName, $tableRate, $tableBroken, $language)
102
    {
103
        $fields  = $this->getTableFields($tableMid, $tableId);
104
        $ret     = '';
105
        $retNumb = '';
106
        $fieldId   = '';
107
        $ccFieldId = '';
108
        $keyDouble = '';
109
        foreach (\array_keys($fields) as $f) {
110
            if (0 == $f) {
111
                $fieldId = $fields[$f]->getVar('field_name');
112
                $ccFieldId = $this->getCamelCase($fieldId, false, true);
113
                $keyDouble = $this->sc->getSmartyDoubleVar($tableSoleName, $fieldId);
114
            }
115
            $fieldElement = $fields[$f]->getVar('field_element');
116
            if (1 == $fields[$f]->getVar('field_user')) {
117
                if (1 == $fields[$f]->getVar('field_thead')) {
118
                    switch ($fieldElement) {
119
                        default:
120
                        //case 2:
121
                            $fieldName   = $fields[$f]->getVar('field_name');
122
                            $rpFieldName = $this->getRightString($fieldName);
123
                            $doubleVar   = $this->sc->getSmartyDoubleVar($tableSoleName, $rpFieldName);
124
                            $retNumb     .= $this->hc->getHtmlHNumb($doubleVar, '3', 'panel-title', "\t");
125
                            break;
126
                    }
127
                }
128
            }
129
        }
130
        $ret     .= $this->hc->getHtmlI('', '', $ccFieldId . '_' . $keyDouble);
131
        $ret     .= $this->hc->getHtmlDiv($retNumb, 'panel-heading');
132
        $retElem = '';
133
        foreach (\array_keys($fields) as $f) {
134
            $fieldElement = $fields[$f]->getVar('field_element');
135
            if (1 == $fields[$f]->getVar('field_user')) {
136
                if (1 == $fields[$f]->getVar('field_tbody')) {
137
                    switch ($fieldElement) {
138
                        default:
139
                        //case 3:
140
                        //case 4:
141
                            $fieldName   = $fields[$f]->getVar('field_name');
142
                            $rpFieldName = $this->getRightString($fieldName);
143
                            $doubleVar   = $this->sc->getSmartyDoubleVar($tableSoleName, $rpFieldName);
144
                            $retElem     .= $this->hc->getHtmlSpan($doubleVar, 'col-sm-9 justify', "\t");
145
                            break;
146
                        case Constants::FIELD_ELE_IMAGELIST:
147
                            $fieldName   = $fields[$f]->getVar('field_name');
148
                            $rpFieldName = $this->getRightString($fieldName);
149
                            $singleVar   = $this->sc->getSmartySingleVar('xoops_icons32_url');
150
                            $doubleVar   = $this->sc->getSmartyDoubleVar($tableSoleName, $rpFieldName);
151
                            $img         = $this->hc->getHtmlImage($singleVar . '/' . $doubleVar, (string)$tableName);
152
                            $retElem     .= $this->hc->getHtmlSpan($img, 'col-sm-3', "\t");
153
                            unset($img);
154
                            break;
155
                        case Constants::FIELD_ELE_UPLOADIMAGE:
156
                            $fieldName   = $fields[$f]->getVar('field_name');
157
                            $rpFieldName = $this->getRightString($fieldName);
158
                            $singleVar   = $this->sc->getSmartySingleVar($moduleDirname . '_upload_url');
159
                            $doubleVar   = $this->sc->getSmartyDoubleVar($tableSoleName, $rpFieldName);
160
                            $img         = $this->hc->getHtmlImage($singleVar . "/images/{$tableName}/" . $doubleVar, (string)$tableName);
161
                            $retElem     .= $this->hc->getHtmlSpan($img, 'col-sm-3',"\t");
162
                            unset($img);
163
                            break;
164
                    }
165
                }
166
            }
167
        }
168
        $ret .= $this->hc->getHtmlDiv($retElem, 'panel-body');
169
170
        $retFoot   = '';
171
        foreach (\array_keys($fields) as $f) {
172
            if (1 == $fields[$f]->getVar('field_user')) {
173
                if (1 == $fields[$f]->getVar('field_tfoot')) {
174
                    $fieldName   = $fields[$f]->getVar('field_name');
175
                    $rpFieldName = $this->getRightString($fieldName);
176
                    $langConst   = \mb_strtoupper($tableSoleName) . '_' . \mb_strtoupper($rpFieldName);
177
                    $lang        = $this->sc->getSmartyConst($language, $langConst);
178
                    $doubleVar   = $this->sc->getSmartyDoubleVar($tableSoleName, $rpFieldName);
179
                    $retFoot     .= $this->hc->getHtmlSpan($lang . ': ' . $doubleVar, 'block-pie justify',"\t");
180
                }
181
            }
182
        }
183
184
        $anchors  = '';
185
        $lang     = $this->sc->getSmartyConst($language, \mb_strtoupper($tableName) . '_LIST');
186
        $contIf   =  $this->hc->getHtmlAnchor($tableName . '.php?op=list&amp;start=<{$start}>&amp;limit=<{$limit}>#' .$ccFieldId . '_' . $keyDouble, $lang, $lang, '', 'btn btn-success right', '', "\t\t\t", "\n");
187
        $lang     = $this->sc->getSmartyConst($language, 'DETAILS');
188
        $contElse =  $this->hc->getHtmlAnchor($tableName . ".php?op=show&amp;{$fieldId}=" . $keyDouble . '&amp;start=<{$start}>&amp;limit=<{$limit}>', $lang, $lang, '', 'btn btn-success right', '', "\t\t\t", "\n");
189
        $anchors .= $this->sc->getSmartyConditions('showItem', '', '', $contIf, $contElse, '', '', "\t\t");
190
        $lang     = $this->sc->getSmartyConst('', '_EDIT');
191
        $contIf   =  $this->hc->getHtmlAnchor($tableName . ".php?op=edit&amp;{$fieldId}=" . $keyDouble . '&amp;start=<{$start}>&amp;limit=<{$limit}>', $lang, $lang, '', 'btn btn-primary right', '', "\t\t\t", "\n");
192
        $lang     = $this->sc->getSmartyConst('', '_CLONE');
193
        $contIf   .=  $this->hc->getHtmlAnchor($tableName . ".php?op=clone&amp;{$fieldId}_source=" . $keyDouble, $lang, $lang, '', 'btn btn-primary right', '', "\t\t\t", "\n");
194
        $lang     = $this->sc->getSmartyConst('', '_DELETE');
195
        $contIf   .=  $this->hc->getHtmlAnchor($tableName . ".php?op=delete&amp;{$fieldId}=" . $keyDouble, $lang, $lang, '', 'btn btn-danger right', '', "\t\t\t", "\n");
196
        $anchors  .= $this->sc->getSmartyConditions('permEdit', '', '', $contIf, false, '', '', "\t\t");
197
        if (1 == $tableBroken) {
198
            $lang        = $this->sc->getSmartyConst($language, 'BROKEN');
199
            $anchors .=  $this->hc->getHtmlAnchor($tableName . ".php?op=broken&amp;{$fieldId}=" . $keyDouble . '&amp;start=<{$start}>&amp;limit=<{$limit}>', $lang, $lang, '', 'btn btn-warning right', '', "\t\t", "\n");
200
        }
201
        $retFoot     .= $this->hc->getHtmlDiv($anchors, 'col-sm-12 right',"\t", "\n");
202
        $ret .= $this->hc->getHtmlDiv($retFoot, 'panel-foot');
203
        if ($tableRate) {
204
            $rate = $this->sc->getSmartyIncludeFile($moduleDirname, 'rate', false, false, "\t", "\n", 'item=$' . $tableSoleName);
205
            $ret .= $this->sc->getSmartyConditions('rating', '', '', $rate, false);
206
        }
207
208
        return $ret;
209
    }
210
211
    /**
212
     * @public function render
213
     * @param null
214
     * @return bool|string
215
     */
216
    public function render()
217
    {
218
        $module = $this->getModule();
219
        $table  = $this->getTable();
220
        $moduleDirname = $module->getVar('mod_dirname');
221
        $filename      = $this->getFileName();
222
        $language      = $this->getLanguage($moduleDirname, 'MA', '', false);
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type string expected by parameter $addFq of XoopsModules\Modulebuild...eateFile::getLanguage(). ( Ignorable by Annotation )

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

222
        $language      = $this->getLanguage($moduleDirname, 'MA', '', /** @scrutinizer ignore-type */ false);
Loading history...
223
        $content       = '';
224
        $tableId         = $table->getVar('table_id');
225
        $tableMid        = $table->getVar('table_mid');
226
        $tableName       = $table->getVar('table_name');
227
        $tableSoleName   = $table->getVar('table_solename');
228
        $tableRate       = $table->getVar('table_rate');
229
        $tableBroken     = $table->getVar('table_broken');
230
        $tableCategory[] = $table->getVar('table_category');
0 ignored issues
show
Comprehensibility Best Practice introduced by
$tableCategory was never initialized. Although not strictly required by PHP, it is generally a good practice to add $tableCategory = array(); before regardless.
Loading history...
231
        if (\in_array(0, $tableCategory)) {
232
            $content .= $this->getTemplatesUserPagesItemPanel($moduleDirname, $tableId, $tableMid, $tableName, $tableSoleName, $tableRate, $tableBroken, $language);
233
        }
234
235
        $this->create($moduleDirname, 'templates', $filename, $content, \_AM_MODULEBUILDER_FILE_CREATED, \_AM_MODULEBUILDER_FILE_NOTCREATED);
236
237
        return $this->renderFile();
238
    }
239
}
240