b_news_randomnews_edit()   B
last analyzed

Complexity

Conditions 7
Paths 32

Size

Total Lines 47
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 32
c 0
b 0
f 0
dl 0
loc 47
rs 8.4746
cc 7
nc 32
nop 1
1
<?php declare(strict_types=1);
2
/*
3
 * You may not change or alter any portion of this comment or credits
4
 * of supporting developers from this source code or any supporting source code
5
 * which is considered copyrighted (c) material of the original comment or credit authors.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
/**
13
 * @copyright      {@link https://xoops.org/ XOOPS Project}
14
 * @license        {@link https://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2 or later}
15
 * @author         XOOPS Development Team
16
 */
17
18
use XoopsModules\News;
19
use XoopsModules\News\Helper;
20
use XoopsModules\News\NewsStory;
21
22
// require_once XOOPS_ROOT_PATH . '/modules/news/class/class.newsstory.php';
23
24
/**
25
 * @param $options
26
 *
27
 * @return array|string
28
 */
29
function b_news_randomnews_show($options)
30
{
31
    /** @var Helper $helper */
32
    if (!class_exists(Helper::class)) {
33
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type array|string.
Loading history...
34
    }
35
36
    $helper = Helper::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $helper is dead and can be removed.
Loading history...
37
38
    $myts          = \MyTextSanitizer::getInstance();
39
    $block         = [];
40
    $block['sort'] = $options[0];
41
42
    $tmpstory   = new NewsStory();
43
    $restricted = News\Utility::getModuleOption('restrictindex');
44
    $dateformat = News\Utility::getModuleOption('dateformat');
45
    $infotips   = News\Utility::getModuleOption('infotips');
46
    if ('' == $dateformat) {
47
        $dateformat = 's';
48
    }
49
    if (0 == $options[4]) {
50
        $stories = $tmpstory->getRandomNews((int)$options[1], 0, $restricted, 0, 1, $options[0]);
51
    } else {
52
        $topics  = array_slice($options, 4);
53
        $stories = $tmpstory->getRandomNews((int)$options[1], 0, $restricted, $topics, 1, $options[0]);
0 ignored issues
show
Bug introduced by
$topics of type array is incompatible with the type integer expected by parameter $topic of XoopsModules\News\NewsStory::getRandomNews(). ( Ignorable by Annotation )

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

53
        $stories = $tmpstory->getRandomNews((int)$options[1], 0, $restricted, /** @scrutinizer ignore-type */ $topics, 1, $options[0]);
Loading history...
54
    }
55
    unset($tmpstory);
56
    if (0 == count($stories)) {
57
        return '';
58
    }
59
    foreach ($stories as $story) {
60
        $news  = [];
61
        $title = $story->title();
62
        if (mb_strlen($title) > $options[2]) {
63
            $title = xoops_substr($title, 0, $options[2] + 3);
64
        }
65
        $news['title']       = $title;
66
        $news['id']          = $story->storyid();
67
        $news['date']        = formatTimestamp($story->published(), $dateformat);
68
        $news['hits']        = $story->counter();
69
        $news['rating']      = $story->rating();
70
        $news['votes']       = $story->votes();
71
        $news['author']      = sprintf('%s %s', _POSTEDBY, $story->uname());
72
        $news['topic_title'] = $story->topic_title();
73
        $news['topic_color'] = '#' . $myts->displayTarea($story->topic_color);
74
        $news['picture']     = XOOPS_URL . '/uploads/news/image/' . $story->picture();
75
        $news['pictureinfo'] = $story->pictureinfo();
76
77
        if ($options[3] > 0) {
78
            $html             = 1 == $story->nohtml() ? 0 : 1;
79
            $news['teaser']   = News\Utility::truncateTagSafe($myts->displayTarea($story->hometext, $html), $options[3] + 3);
80
            $news['infotips'] = ' title="' . $story->title() . '"';
81
        } else {
82
            $news['teaser'] = '';
83
            if ($infotips > 0) {
84
                $news['infotips'] = ' title="' . News\Utility::makeInfotips($story->hometext()) . '"';
85
            } else {
86
                $news['infotips'] = ' title="' . $story->title() . '"';
87
            }
88
        }
89
        $block['stories'][] = $news;
90
    }
91
    $block['lang_read_more'] = _MB_READMORE;
92
93
    return $block;
94
}
95
96
/**
97
 * @param $options
98
 *
99
 * @return string
100
 */
101
function b_news_randomnews_edit($options)
102
{
103
    global $xoopsDB;
104
    $form = _MB_NEWS_ORDER . "&nbsp;<select name='options[]'>";
105
    $form .= "<option value='published'";
106
    if ('published' === $options[0]) {
107
        $form .= ' selected';
108
    }
109
    $form .= '>' . _MB_NEWS_DATE . "</option>\n";
110
111
    $form .= "<option value='counter'";
112
    if ('counter' === $options[0]) {
113
        $form .= ' selected';
114
    }
115
    $form .= '>' . _MB_NEWS_HITS . '</option>';
116
117
    $form .= "<option value='rating'";
118
    if ('rating' === $options[0]) {
119
        $form .= ' selected';
120
    }
121
    $form .= '>' . _MB_NEWS_RATE . '</option>';
122
123
    $form .= "</select>\n";
124
    $form .= '&nbsp;' . _MB_NEWS_DISP . "&nbsp;<input type='text' name='options[]' value='" . $options[1] . "'>&nbsp;" . _MB_NEWS_ARTCLS;
125
    $form .= '&nbsp;<br><br>' . _MB_NEWS_CHARS . "&nbsp;<input type='text' name='options[]' value='" . $options[2] . "'>&nbsp;" . _MB_NEWS_LENGTH . '<br><br>';
126
127
    $form .= _MB_NEWS_TEASER . " <input type='text' name='options[]' value='" . $options[3] . "'>" . _MB_NEWS_LENGTH;
128
    $form .= '<br><br>' . _MB_SPOTLIGHT_TOPIC . "<br><select id='options[4]' name='options[]' multiple='multiple'>";
129
130
    // require_once XOOPS_ROOT_PATH . '/modules/news/class/xoopsstory.php';
131
    $xt                    = new \XoopsModules\News\XoopsTopic($xoopsDB->prefix('news_topics'));
132
    $alltopics             = $xt->getTopicsList();
133
    $alltopics[0]['title'] = _MB_SPOTLIGHT_ALL_TOPICS;
134
    ksort($alltopics);
135
    $size = count($options);
136
    foreach ($alltopics as $topicid => $topic) {
137
        $sel = '';
138
        for ($i = 4; $i < $size; ++$i) {
139
            if ($options[$i] == $topicid) {
140
                $sel = ' selected';
141
            }
142
        }
143
        $form .= "<option value='$topicid'$sel>" . $topic['title'] . '</option>';
144
    }
145
    $form .= '</select><br>';
146
147
    return $form;
148
}
149
150
/**
151
 * @param $options
152
 */
153
function b_news_randomnews_onthefly($options): void
154
{
155
    $options = explode('|', $options);
156
    $block   = b_news_randomnews_show($options);
157
158
    $tpl = new \XoopsTpl();
159
    $tpl->assign('block', $block);
160
    $tpl->display('db:news_block_moderate.tpl');
161
}
162