newbb_list_topic_show()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 45
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 26
nc 4
nop 1
dl 0
loc 45
rs 9.504
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
/**
4
 * NewBB,  the forum module for XOOPS project
5
 *
6
 * @copyright      XOOPS Project (https://xoops.org)
7
 * @license        GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
8
 * @author         Taiwen Jiang (phppp or D.J.) <[email protected]>, irmtfan <[email protected]>
9
 * @author         The Persian Xoops Support Site <www.xoops.ir>
10
 * @since          4.3
11
 */
12
13
14
use XoopsModules\Newbb\{
15
    ForumHandler,
16
    Helper,
17
    TopicRenderer
18
};
19
20
/** @var Helper $helper */
21
/** @var ForumHandler $forumHandler */
22
if (defined('LIST_TOPIC_DEFINED')) {
23
    return;
24
}
25
define('LIST_TOPIC_DEFINED', true);
26
27
//require_once \dirname(__DIR__) . '/include/functions.ini.php';
28
//require_once \dirname(__DIR__) . '/class/TopicRenderer.php';
29
require_once \dirname(__DIR__) . '/footer.php'; // to include js/style files like validate function
30
31
xoops_loadLanguage('main', 'newbb');
32
33
require_once \dirname(__DIR__) . '/include/functions.config.php';
34
require_once \dirname(__DIR__) . '/include/functions.time.php';
35
require_once \dirname(__DIR__) . '/include/functions.session.php';
36
require_once \dirname(__DIR__) . '/include/functions.render.php';
37
require_once \dirname(__DIR__) . '/include/functions.user.php';
38
39
// options[0] - Status in WHERE claus: all(by default), sticky, digest,lock, poll, voted, viewed, replied, read, (UN_) , active, pending, deleted (admin) (It is  multi-select)
40
// options[1] - Uid in WHERE claus: uid of the topic poster : -1 - all users (by default)
41
// options[2] - Lastposter in WHERE claus: uid of the lastposter in topic : -1 - all users (by default)
42
// options[3] - Type in WHERE claus: topic type in the forum : 0 - none (by default)
43
// options[4] - Sort in ORDER claus: topic, forum, poster, replies, views, lastpost(by default), lastposttime, lastposter, lastpostmsgicon, ratings, votes, publish, digest, sticky, lock, poll, type (if exist), approve(admin mode)
44
// options[5] - Order in ORDER claus: Descending 0(by default), Ascending 1
45
// options[6] - NumberToDisplay: any positive integer - 5 by default
46
// options[7] - TimeDuration: negative for hours, positive for days, for instance, -5 for 5 hours and 5 for 5 days - 360 by default
47
// options[8] - DisplayMode: all fields in sort PLUS attachment, read, pagenav
48
// options[9] - Display Navigator: 1 (by default), 0 (No)
49
// options[10] - Title Length : 0 by default - no limit and show complete title
50
// options[11] - Post text Length: 0 - dont show post text - 200 by default
51
// options[12] - SelectedForumIDs: multi-select ngative values for categories and positive values for forums: null for all(by default)
52
53
/**
54
 * @param array $options
55
 *
56
 * @return (array|bool)[]
57
 *
58
 * @psalm-return array{headers: array, indexNav: bool}
59
 */
60
function newbb_list_topic_show(array $options ): array
61
{
62
    $newbbConfig = newbbLoadConfig(); // load all newbb configs
63
64
    $topicRenderer            = new TopicRenderer();
65
    $topicRenderer->userlevel = (int)$GLOBALS['xoopsUserIsAdmin'] ? 2 : (int)is_object($GLOBALS['xoopsUser']); // Vistitor's level: 0 - anonymous; 1 - user; 2 - moderator or admin
66
67
    $topicRenderer->force = true; // force against static vars for parse
68
69
    $topicRenderer->is_multiple = true; // is it for multiple forums
70
    $topicRenderer->config      = &$newbbConfig; // get all configs
71
    if (!empty($options[6])) {
72
        $topicRenderer->config['topics_per_page'] = (int)$options[6]; // number of topics (items) to display
73
    }
74
    $topicRenderer->config['topic_title_excerpt'] = (int)$options[10]; // topic title length 0 = dont excerpt
75
    $topicRenderer->config['post_excerpt']        = (int)$options[11]; // post text excerpt 0 = no post text
76
77
    $optionsStatus = explode(',', (string) $options[0]); // status in where claus
78
    $optionsForum  = explode(',', (string) $options[12]);
79
80
    // set and parse values:
81
    // forum: parse positive values to forum IDs and negative values to category IDs. value=0 => all valid forums
82
    $topicRenderer->setVars(
83
        [
84
            'status'     => $optionsStatus,
85
            'uid'        => $options[1],
86
            'lastposter' => $options[2],
87
            'type'       => $options[3],
88
            'sort'       => $options[4],
89
            'order'      => $options[5],
90
            'since'      => $options[7],
91
            'forum'      => $optionsForum,
92
        ]
93
    );
94
    $block = [];
95
    // headers to display in block
96
    $block['headers'] = $topicRenderer->getHeader($options[8]);
97
98
    // render a list of topics using all above criterias
99
    [$block['topics'], $block['sticky']] = $topicRenderer->renderTopics();
100
101
    // show index navigation
102
    $block['indexNav'] = !empty($options[9]);
103
104
    return $block;
105
}
106
107
/**
108
 * @param array $options
109
 * @return string
110
 */
111
function newbb_list_topic_edit(array $options ): string
112
{
113
    // require_once $GLOBALS['xoops']->path('class/blockform.php'); //reserve for 2.6
114
    xoops_load('XoopsFormLoader');
115
    // $form = new \XoopsBlockForm(); //reserve for 2.6
116
    $form = new \XoopsThemeForm(_MB_NEWBB_DISPLAYMODE_DESC, 'list_topic', '');
117
118
    $topicRenderer            = new TopicRenderer();
119
    $topicRenderer->userlevel = 2; // 2 - moderator or admin
120
121
    // status element
122
    $optionsStatus = explode(',', (string) $options[0]);
123
    $statusEle     = new \XoopsFormSelect(_MB_NEWBB_CRITERIA, 'options[0]', $optionsStatus, 5, true);
124
    $status        = $topicRenderer->getStatus($topicRenderer->userlevel); // get all public status + admin status (admin mode, pending deleted)
125
    $statusEle->addOptionArray($status);
126
    $statusEle->setExtra("onchange = \"validate('options[0][]','select', true)\""); // if user dont select any option it select "all"
127
    $statusEle->setDescription(_MB_NEWBB_CRITERIA_DESC);
128
129
    // topic_poster element
130
    $topicPosterRadioEle = new \XoopsFormRadio(_MB_NEWBB_AUTHOR, 'options[1]', $options[1]);
131
    $topicPosterRadioEle->addOption('-1', _MD_NEWBB_TOTALUSER);
132
    $topicPosterRadioEle->addOption((-1 !== $options[1]) ? $options[1] : 0, _SELECT); // if no user in selection box it select uid=0 anon users
133
    $topicPosterRadioEle->setExtra("onchange=\"var el=document.getElementById('options[1]'); el.disabled=(this.id == 'options[1]1'); if (!el.value) {el.value= this.value}\""); // if user dont select any option it select "all"
134
    $topicPosterSelectEle = new \XoopsFormSelectUser(_MB_NEWBB_AUTHOR, 'options[1]', true, explode(',', (string) $options[1]), 5, true); // show $limit = 200 users when no user is selected;
135
    $topicPosterEle       = new \XoopsFormLabel(_MB_NEWBB_AUTHOR, $topicPosterRadioEle->render() . $topicPosterSelectEle->render());
136
137
    // lastposter element
138
    $lastPosterRadioEle = new \XoopsFormRadio(_MD_NEWBB_POSTER, 'options[2]', $options[2]);
139
    $lastPosterRadioEle->addOption('-1', _MD_NEWBB_TOTALUSER);
140
    $lastPosterRadioEle->addOption((-1 !== $options[2]) ? $options[2] : 0, _SELECT); // if no user in selection box it select uid=1
141
    $lastPosterRadioEle->setExtra("onchange=\"var el=document.getElementById('options[2]'); el.disabled=(this.id == 'options[2]1'); if (!el.value) {el.value= this.value}\""); // if user dont select any option it select "all"
142
    $lastPosterSelectEle = new \XoopsFormSelectUser(_MD_NEWBB_POSTER, 'options[2]', true, explode(',', (string) $options[2]), 5, true); // show $limit = 200 users when no user is selected;
143
    $lastPosterEle       = new \XoopsFormLabel(_MD_NEWBB_POSTER, $lastPosterRadioEle->render() . $lastPosterSelectEle->render());
144
145
    // type element
146
    $types   = $topicRenderer->getTypes(); // get all available types in all forums
147
    $typeEle = new \XoopsFormSelect(_MD_NEWBB_TYPE, 'options[3]', $options[3]);
148
    $typeEle->addOption('0', _NONE);
149
    if (!empty($types)) {
150
        foreach ($types as $type_id => $type) {
151
            $typeEle->addOption($type_id, $type['type_name']);
152
        }
153
    }
154
155
    // sort element
156
    $sortEle = new \XoopsFormSelect(_MD_NEWBB_SORTBY, 'options[4]', $options[4]);
157
    $sortEle->setDescription(_MB_NEWBB_CRITERIA_SORT_DESC);
158
    $sorts = $topicRenderer->getSort('', 'title');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $sorts is correct as $topicRenderer->getSort('', 'title') targeting XoopsModules\Newbb\TopicRenderer::getSort() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
159
    $sortEle->addOptionArray($sorts);
160
161
    // order element
162
    $orderEle = new \XoopsFormSelect(_MB_NEWBB_CRITERIA_ORDER, 'options[5]', $options[5]);
163
    $orderEle->addOption('0', _DESCENDING);
164
    $orderEle->addOption('1', _ASCENDING);
165
166
    // number of topics to display element
167
    $numdispEle = new \XoopsFormText(_MB_NEWBB_DISPLAY, 'options[6]', 10, 255, (string)$options[6]);
168
169
    $timeEle = new \XoopsFormText(_MB_NEWBB_TIME, 'options[7]', 10, 255, $options[7]);
170
    $timeEle->setDescription(_MB_NEWBB_TIME_DESC);
171
172
    // mode disp element
173
    $options_headers = explode(',', (string) $options[8]);
174
    $modeEle         = new \XoopsFormCheckBox(_MB_NEWBB_DISPLAYMODE, 'options[8][]', $options_headers);
175
    $modeEle->setDescription(_MB_NEWBB_DISPLAYMODE_DESC);
176
    $modeEle->columns = 4;
177
    $disps            = $topicRenderer->getHeader();
178
    $modeEle->addOptionArray($disps);
179
    $modeEle->setExtra("onchange = \"validate('options[8][]','checkbox', true)\""); // prevent user select no option
180
    // Index navigation element
181
    $navEle = new \XoopsFormRadioYN(_MB_NEWBB_INDEXNAV, 'options[9]', $options[9]);
182
183
    // Topic title element
184
    $lengthEle = new \XoopsFormText(_MB_NEWBB_TITLE_LENGTH, 'options[10]', 10, 255, $options[10]);
185
    $lengthEle->setDescription(_MB_NEWBB_TITLE_LENGTH_DESC);
186
187
    // Post text element
188
    $postExcerptEle = new \XoopsFormText(_MB_NEWBB_POST_EXCERPT, 'options[11]', 10, 255, $options[11]);
189
    $postExcerptEle->setDescription(_MB_NEWBB_POST_EXCERPT_DESC);
190
191
    //  forum element
192
    $optionsForum = explode(',', (string) $options[12]);
193
    require_once \dirname(__DIR__) . '/include/functions.forum.php';
194
    $forumHandler = Helper::getInstance()->getHandler('Forum');
195
    assert($forumHandler instanceof ForumHandler);
196
    //get forum Ids by values. parse positive values to forum IDs and negative values to category IDs. value=0 => all valid forums
197
    // Get accessible forums
198
    $accessForums = $forumHandler->getIdsByValues(array_map('\intval', $optionsForum));
199
    $isAll        = (0 === count($optionsForum) || empty($optionsForum[0]));
200
    $forumSel     = "<select name=\"options[12][]\" multiple=\"multiple\" onchange = \"validate('options[12][]','select', true)\">"; // if user dont select any it select "0"
201
    $forumSel     .= '<option value="0" ';
202
    if ($isAll) {
203
        $forumSel     .= ' selected';
204
        $accessForums = null; // just select _ALL option
205
    }
206
    $forumSel .= '>' . _ALL . '</option>';
207
    $forumSel .= newbbForumSelectBox($accessForums, 'access', false); //$accessForums, $permission = "access", $delimitorCategory = false
208
    $forumSel .= '</select>';
209
    $forumEle = new \XoopsFormLabel(_MB_NEWBB_FORUMLIST, $forumSel);
210
211
    // add all elements to form
212
    $form->addElement($statusEle);
213
    $form->addElement($topicPosterEle);
214
    $form->addElement($lastPosterEle);
215
    $form->addElement($typeEle);
216
    $form->addElement($sortEle);
217
    $form->addElement($orderEle);
218
    $form->addElement($numdispEle);
219
    $form->addElement($timeEle);
220
    $form->addElement($modeEle, true); // required: user should select at least one otherwise it will select the first one
221
    $form->addElement($navEle);
222
    $form->addElement($lengthEle);
223
    $form->addElement($postExcerptEle);
224
    $form->addElement($forumEle);
225
226
    return $form->render();
227
}
228