Completed
Push — master ( 48ecb4...5fe85d )
by Michael
01:45
created

InstructionPage::getForm()   F

Complexity

Conditions 9
Paths 256

Size

Total Lines 120
Code Lines 73

Duplication

Lines 6
Ratio 5 %

Importance

Changes 0
Metric Value
cc 9
eloc 73
nc 256
nop 2
dl 6
loc 120
rs 3.1304
c 0
b 0
f 0

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
//if (!defined("XOOPS_ROOT_PATH")) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
4
//	die("XOOPS root path not defined");
5
//}
6
7
include_once $GLOBALS['xoops']->path('include/common.php');
8
9
class InstructionPage extends XoopsObject
10
{
11
    // constructor
12
    public function __construct()
13
    {
14
        //	$this->XoopsObject();
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
15
        $this->initVar('pageid', XOBJ_DTYPE_INT, null, false, 11);
16
        $this->initVar('pid', XOBJ_DTYPE_INT, 0, false, 11);
17
        $this->initVar('instrid', XOBJ_DTYPE_INT, 0, false, 11);
18
        $this->initVar('uid', XOBJ_DTYPE_INT, 0, false, 11);
19
        $this->initVar('title', XOBJ_DTYPE_TXTBOX, '', false, 255);
20
        $this->initVar('status', XOBJ_DTYPE_INT, 1, false, 1);
21
        $this->initVar('type', XOBJ_DTYPE_INT, 1, false, 1);
22
        $this->initVar('hometext', XOBJ_DTYPE_TXTAREA, null, false);
23
        $this->initVar('footnote', XOBJ_DTYPE_TXTAREA, '', false);
24
        $this->initVar('weight', XOBJ_DTYPE_INT, 0, false, 11);
25
        $this->initVar('keywords', XOBJ_DTYPE_TXTBOX, '', false, 255);
26
        $this->initVar('description', XOBJ_DTYPE_TXTBOX, '', false, 255);
27
        $this->initVar('comments', XOBJ_DTYPE_INT, 0, false, 11);
28
        $this->initVar('datecreated', XOBJ_DTYPE_INT, 0, false, 10);
29
        $this->initVar('dateupdated', XOBJ_DTYPE_INT, 0, false, 10);
30
        $this->initVar('dohtml', XOBJ_DTYPE_INT, 1, false, 1);
31
        $this->initVar('dosmiley', XOBJ_DTYPE_INT, 0, false, 1);
32
        $this->initVar('doxcode', XOBJ_DTYPE_INT, 1, false, 1);
33
        $this->initVar('dobr', XOBJ_DTYPE_INT, 0, false, 1);
34
    }
35
36
    public function InstructionPage()
37
    {
38
        $this->__construct();
39
    }
40
41
    public function get_new_enreg()
42
    {
43
        $new_enreg = $GLOBALS['xoopsDB']->getInsertId();
44
        return $new_enreg;
45
    }
46
47
    // Получаем форму
48
    public function getForm($action = false, $instrid = 0)
49
    {
50
        // Если нет $action
51
        if (false === $action) {
52
            $action = xoops_getenv('REQUEST_URI');
53
        }
54
55
        // Подключаем формы
56
        include_once $GLOBALS['xoops']->path('class/xoopsformloader.php');
57
        // Подключаем типы страниц
58
        $pagetypes = include $GLOBALS['xoops']->path('modules/instruction/include/pagetypes.inc.php');
59
60
        // Название формы
61
        $title = $this->isNew() ? sprintf(_AM_INSTRUCTION_FORMADDPAGE) : sprintf(_AM_INSTRUCTION_FORMEDITPAGE);
62
63
        // Форма
64
        $form = new XoopsThemeForm($title, 'instr_form_page', $action, 'post', true);
65
        // Название
66
        $form->addElement(new XoopsFormText(_AM_INSTRUCTION_TITLEC, 'title', 50, 255, $this->getVar('title')), true);
67
68
        // Родительская страница
69
        $inspageHandler = xoops_getModuleHandler('page', 'instruction');
70
        $criteria       = new CriteriaCompo();
71
        // ID инструкции в которой данная страница
72
        $instrid_page = $this->isNew() ? $instrid : $this->getVar('instrid');
73
        // Находим все страницы данной инструкции
74
        $criteria->add(new Criteria('instrid', $instrid_page, '='));
75
        // Если мы редактируем, то убрать текущую страницу из списка выбора родительской
76
        if (!$this->isNew()) {
77
            $criteria->add(new Criteria('pageid', $this->getVar('pageid'), '<>'));
78
        }
79
        $criteria->setSort('weight');
80
        $criteria->setOrder('ASC');
81
        $inspage_arr = $inspageHandler->getall($criteria);
82
        unset($criteria);
83
        // Подключаем трей
84
        include_once $GLOBALS['xoops']->path('class/tree.php');
85
        $mytree = new XoopsObjectTree($inspage_arr, 'pageid', 'pid');
86
87
        // $form->addElement(new XoopsFormLabel(_AM_INSTRUCTION_PPAGEC, $mytree->makeSelBox('pid', 'title', '--', $this->getVar('pid'), true)));
0 ignored issues
show
Unused Code Comprehensibility introduced by
69% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
88
        $moduleDirName = basename(__DIR__);
89
        if (false !== ($moduleHelper = Xmf\Module\Helper::getHelper($moduleDirName))) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
90
        } else {
91
            $moduleHelper = Xmf\Module\Helper::getHelper('system');
92
        }
93
        $module = $moduleHelper->getModule();
94
95 View Code Duplication
        if (InstructionUtility::checkVerXoops($module, '2.5.9')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
96
            $mytree_select = $mytree->makeSelectElement('pid', 'title', '--', $this->getVar('pid'), true, 0, '', _AM_INSTRUCTION_PPAGEC);
97
            $form->addElement($mytree_select);
98
        } else {
99
            $form->addElement(new XoopsFormLabel(_AM_INSTRUCTION_PPAGEC, $mytree->makeSelBox('pid', 'title', '--', $this->getVar('pid'), true)));
100
        }
101
102
        // Вес
103
        $form->addElement(new XoopsFormText(_AM_INSTRUCTION_WEIGHTC, 'weight', 5, 5, $this->getVar('weight')), true);
104
        // Основной текст
105
        $form->addElement(InstructionUtility::getWysiwygForm(_AM_INSTRUCTION_HOMETEXTC, 'hometext', $this->getVar('hometext', 'e')), true);
106
        // Сноска
107
        $form_footnote = new XoopsFormTextArea(_AM_INSTRUCTION_FOOTNOTEC, 'footnote', $this->getVar('footnote', 'e'));
108
        $form_footnote->setDescription(_AM_INSTRUCTION_FOOTNOTE_DSC);
109
        $form->addElement($form_footnote, false);
110
        unset($form_footnote);
111
        // Статус
112
        $form->addElement(new XoopsFormRadioYN(_AM_INSTRUCTION_ACTIVEC, 'status', $this->getVar('status')), false);
113
        // Тип страницы
114
        $form_type = new XoopsFormSelect(_AM_INSTR_PAGETYPEC, 'type', $this->getVar('type'));
115
        $form_type->setDescription(_AM_INSTR_PAGETYPEC_DESC);
116
        $form_type->addOptionArray($pagetypes);
117
        $form->addElement($form_type, false);
118
        // Мета-теги ключевых слов
119
        $form->addElement(new XoopsFormText(_AM_INSTRUCTION_METAKEYWORDSC, 'keywords', 50, 255, $this->getVar('keywords')), false);
120
        // Мета-теги описания
121
        $form->addElement(new XoopsFormText(_AM_INSTRUCTION_METADESCRIPTIONC, 'description', 50, 255, $this->getVar('description')), false);
122
123
        // Настройки
124
        $option_tray = new XoopsFormElementTray(_OPTIONS, '<br>');
125
        // HTML
126
        $html_checkbox = new XoopsFormCheckBox('', 'dohtml', $this->getVar('dohtml'));
127
        $html_checkbox->addOption(1, _AM_INSTR_DOHTML);
128
        $option_tray->addElement($html_checkbox);
129
        // Смайлы
130
        $smiley_checkbox = new XoopsFormCheckBox('', 'dosmiley', $this->getVar('dosmiley'));
131
        $smiley_checkbox->addOption(1, _AM_INSTR_DOSMILEY);
132
        $option_tray->addElement($smiley_checkbox);
133
        // ББ коды
134
        $xcode_checkbox = new XoopsFormCheckBox('', 'doxcode', $this->getVar('doxcode'));
135
        $xcode_checkbox->addOption(1, _AM_INSTR_DOXCODE);
136
        $option_tray->addElement($xcode_checkbox);
137
        //
138
        $br_checkbox = new XoopsFormCheckBox('', 'dobr', $this->getVar('dobr'));
139
        $br_checkbox->addOption(1, _AM_INSTR_DOAUTOWRAP);
140
        $option_tray->addElement($br_checkbox);
141
        //
142
        $form->addElement($option_tray);
143
144
        // Если мы редактируем страницу
145
        if (!$this->isNew()) {
146
            $form->addElement(new XoopsFormHidden('pageid', $this->getVar('pageid')));
147
        } else {
148
            $form->addElement(new XoopsFormHidden('pageid', 0));
149
        }
150
        // ID инструкции
151
        if ($instrid) {
152
            $form->addElement(new XoopsFormHidden('instrid', $instrid));
153
        } else {
154
            $form->addElement(new XoopsFormHidden('instrid', 0));
155
        }
156
        //
157
        $form->addElement(new XoopsFormHidden('op', 'savepage'));
158
        // Кнопка
159
        $button_tray = new XoopsFormElementTray('', '');
160
        $button_tray->addElement(new XoopsFormButton('', 'submit', _SUBMIT, 'submit'));
161
        $save_btn = new XoopsFormButton('', 'cancel', _AM_INSTR_SAVEFORM);
162
        $save_btn->setExtra('onclick="instrSavePage();"');
163
        $button_tray->addElement($save_btn);
164
        $form->addElement($button_tray);
165
166
        return $form;
167
    }
168
169
    //
170
    public function getInstrid()
171
    {
172
        // Возвращаем ID инструкции
173
        return $this->getVar('instrid');
174
    }
175
}
176
177
class InstructionPageHandler extends XoopsPersistableObjectHandler
178
{
179
    public function __construct($db)
180
    {
181
        parent::__construct($db, 'instruction_page', 'InstructionPage', 'pageid', 'title');
182
    }
183
184
    /**
185
     * Generate function for update user post
186
     *
187
     * @ Update user post count after send approve content
188
     * @ Update user post count after change status content
189
     * @ Update user post count after delete content
190
     */
191
    public function updateposts($uid, $status, $action)
192
    {
193
        //
194
        switch ($action) {
195
            // Добавление страницы
196 View Code Duplication
            case 'add':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
197
                if ($uid && $status) {
198
                    $user          = new xoopsUser($uid);
199
                    $memberHandler = xoops_getHandler('member');
200
                    // Добавялем +1 к комментам
201
                    $memberHandler->updateUserByField($user, 'posts', $user->getVar('posts') + 1);
202
                }
203
                break;
204
            // Удаление страницы
205 View Code Duplication
            case 'delete':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
206
                if ($uid && $status) {
207
                    $user          = new xoopsUser($uid);
208
                    $memberHandler = xoops_getHandler('member');
209
                    // Декримент комментов
210
                    //$user->setVar( 'posts', $user->getVar( 'posts' ) - 1 );
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
211
                    // Сохраняем
212
                    $memberHandler->updateUserByField($user, 'posts', $user->getVar('posts') - 1);
213
                }
214
                break;
215
216
            case 'status':
217
                if ($uid) {
218
                    $user          = new xoopsUser($uid);
219
                    $memberHandler = xoops_getHandler('member');
220
                    if ($status) {
221
                        $memberHandler->updateUserByField($user, 'posts', $user->getVar('posts') - 1);
222
                    } else {
223
                        $memberHandler->updateUserByField($user, 'posts', $user->getVar('posts') + 1);
224
                    }
225
                }
226
                break;
227
        }
228
    }
229
}
230