Completed
Push — master ( 55b92e...48ecb4 )
by Michael
01:50
created

InstructionPage::getForm()   D

Complexity

Conditions 8
Paths 128

Size

Total Lines 117
Code Lines 68

Duplication

Lines 6
Ratio 5.13 %

Importance

Changes 0
Metric Value
cc 8
eloc 68
nc 128
nop 2
dl 6
loc 117
rs 4.606
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
89
90
91 View Code Duplication
        if (InstructionUtility::checkVerXoops($module, '2.5.9')) {
0 ignored issues
show
Bug introduced by
The variable $module 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...
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...
92
            $mytree_select = $mytree->makeSelectElement('pid', 'title', '--', $this->getVar('pid'), true, 0, '', _AM_INSTRUCTION_PPAGEC);
93
            $form->addElement($mytree_select);
94
        } else {
95
            $form->addElement(new XoopsFormLabel(_AM_INSTRUCTION_PPAGEC, $mytree->makeSelBox('pid', 'title', '--', $this->getVar('pid'), true)));
96
        }
97
98
99
        // Вес
100
        $form->addElement(new XoopsFormText(_AM_INSTRUCTION_WEIGHTC, 'weight', 5, 5, $this->getVar('weight')), true);
101
        // Основной текст
102
        $form->addElement(InstructionUtility::getWysiwygForm(_AM_INSTRUCTION_HOMETEXTC, 'hometext', $this->getVar('hometext', 'e')), true);
103
        // Сноска
104
        $form_footnote = new XoopsFormTextArea(_AM_INSTRUCTION_FOOTNOTEC, 'footnote', $this->getVar('footnote', 'e'));
105
        $form_footnote->setDescription(_AM_INSTRUCTION_FOOTNOTE_DSC);
106
        $form->addElement($form_footnote, false);
107
        unset($form_footnote);
108
        // Статус
109
        $form->addElement(new XoopsFormRadioYN(_AM_INSTRUCTION_ACTIVEC, 'status', $this->getVar('status')), false);
110
        // Тип страницы
111
        $form_type = new XoopsFormSelect(_AM_INSTR_PAGETYPEC, 'type', $this->getVar('type'));
112
        $form_type->setDescription(_AM_INSTR_PAGETYPEC_DESC);
113
        $form_type->addOptionArray($pagetypes);
114
        $form->addElement($form_type, false);
115
        // Мета-теги ключевых слов
116
        $form->addElement(new XoopsFormText(_AM_INSTRUCTION_METAKEYWORDSC, 'keywords', 50, 255, $this->getVar('keywords')), false);
117
        // Мета-теги описания
118
        $form->addElement(new XoopsFormText(_AM_INSTRUCTION_METADESCRIPTIONC, 'description', 50, 255, $this->getVar('description')), false);
119
120
        // Настройки
121
        $option_tray = new XoopsFormElementTray(_OPTIONS, '<br>');
122
        // HTML
123
        $html_checkbox = new XoopsFormCheckBox('', 'dohtml', $this->getVar('dohtml'));
124
        $html_checkbox->addOption(1, _AM_INSTR_DOHTML);
125
        $option_tray->addElement($html_checkbox);
126
        // Смайлы
127
        $smiley_checkbox = new XoopsFormCheckBox('', 'dosmiley', $this->getVar('dosmiley'));
128
        $smiley_checkbox->addOption(1, _AM_INSTR_DOSMILEY);
129
        $option_tray->addElement($smiley_checkbox);
130
        // ББ коды
131
        $xcode_checkbox = new XoopsFormCheckBox('', 'doxcode', $this->getVar('doxcode'));
132
        $xcode_checkbox->addOption(1, _AM_INSTR_DOXCODE);
133
        $option_tray->addElement($xcode_checkbox);
134
        //
135
        $br_checkbox = new XoopsFormCheckBox('', 'dobr', $this->getVar('dobr'));
136
        $br_checkbox->addOption(1, _AM_INSTR_DOAUTOWRAP);
137
        $option_tray->addElement($br_checkbox);
138
        //
139
        $form->addElement($option_tray);
140
141
        // Если мы редактируем страницу
142
        if (!$this->isNew()) {
143
            $form->addElement(new XoopsFormHidden('pageid', $this->getVar('pageid')));
144
        } else {
145
            $form->addElement(new XoopsFormHidden('pageid', 0));
146
        }
147
        // ID инструкции
148
        if ($instrid) {
149
            $form->addElement(new XoopsFormHidden('instrid', $instrid));
150
        } else {
151
            $form->addElement(new XoopsFormHidden('instrid', 0));
152
        }
153
        //
154
        $form->addElement(new XoopsFormHidden('op', 'savepage'));
155
        // Кнопка
156
        $button_tray = new XoopsFormElementTray('', '');
157
        $button_tray->addElement(new XoopsFormButton('', 'submit', _SUBMIT, 'submit'));
158
        $save_btn = new XoopsFormButton('', 'cancel', _AM_INSTR_SAVEFORM);
159
        $save_btn->setExtra('onclick="instrSavePage();"');
160
        $button_tray->addElement($save_btn);
161
        $form->addElement($button_tray);
162
163
        return $form;
164
    }
165
166
    //
167
    public function getInstrid()
168
    {
169
        // Возвращаем ID инструкции
170
        return $this->getVar('instrid');
171
    }
172
}
173
174
class InstructionPageHandler extends XoopsPersistableObjectHandler
175
{
176
    public function __construct($db)
177
    {
178
        parent::__construct($db, 'instruction_page', 'InstructionPage', 'pageid', 'title');
179
    }
180
181
    /**
182
     * Generate function for update user post
183
     *
184
     * @ Update user post count after send approve content
185
     * @ Update user post count after change status content
186
     * @ Update user post count after delete content
187
     */
188
    public function updateposts($uid, $status, $action)
189
    {
190
        //
191
        switch ($action) {
192
            // Добавление страницы
193 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...
194
                if ($uid && $status) {
195
                    $user          = new xoopsUser($uid);
196
                    $memberHandler = xoops_getHandler('member');
197
                    // Добавялем +1 к комментам
198
                    $memberHandler->updateUserByField($user, 'posts', $user->getVar('posts') + 1);
199
                }
200
                break;
201
            // Удаление страницы
202 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...
203
                if ($uid && $status) {
204
                    $user          = new xoopsUser($uid);
205
                    $memberHandler = xoops_getHandler('member');
206
                    // Декримент комментов
207
                    //$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...
208
                    // Сохраняем
209
                    $memberHandler->updateUserByField($user, 'posts', $user->getVar('posts') - 1);
210
                }
211
                break;
212
213
            case 'status':
214
                if ($uid) {
215
                    $user          = new xoopsUser($uid);
216
                    $memberHandler = xoops_getHandler('member');
217
                    if ($status) {
218
                        $memberHandler->updateUserByField($user, 'posts', $user->getVar('posts') - 1);
219
                    } else {
220
                        $memberHandler->updateUserByField($user, 'posts', $user->getVar('posts') + 1);
221
                    }
222
                }
223
                break;
224
        }
225
    }
226
}
227