Digest   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 40
c 1
b 0
f 0
dl 0
loc 89
rs 10
wmc 12

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setHtml() 0 3 1
A setSummary() 0 3 1
A cleanup() 0 10 1
A __construct() 0 7 1
A buildContent() 0 23 6
A addItem() 0 8 2
1
<?php
2
3
namespace XoopsModules\Newbb;
4
5
/**
6
 * NewBB 5.0x,  the forum module for XOOPS project
7
 *
8
 * @copyright      XOOPS Project (https://xoops.org)
9
 * @license        GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
10
 * @author         Taiwen Jiang (phppp or D.J.) <[email protected]>
11
 * @since          4.00
12
 * @package        module::newbb
13
 */
14
class Digest extends \XoopsObject
15
{
16
    public $digest_id;
17
    public $digest_time;
18
    public $digest_content;
19
20
    public $items;
21
    public $isHtml    = false;
22
    public $isSummary = true;
23
24
    public function __construct()
25
    {
26
        parent::__construct();
27
        $this->initVar('digest_id', \XOBJ_DTYPE_INT);
28
        $this->initVar('digest_time', \XOBJ_DTYPE_INT);
29
        $this->initVar('digest_content', \XOBJ_DTYPE_TXTAREA);
30
        $this->items = [];
31
    }
32
33
    public function setHtml()
34
    {
35
        $this->isHtml = true;
36
    }
37
38
    public function setSummary()
39
    {
40
        $this->isSummary = true;
41
    }
42
43
    /**
44
     * @param        $title
45
     * @param        $link
46
     * @param        $author
47
     * @param string $summary
48
     */
49
    public function addItem($title, $link, $author, $summary = '')
50
    {
51
        $title  = $this->cleanup($title);
52
        $author = $this->cleanup($author);
53
        if (!empty($summary)) {
54
            $summary = $this->cleanup($summary);
55
        }
56
        $this->items[] = ['title' => $title, 'link' => $link, 'author' => $author, 'summary' => $summary];
57
    }
58
59
    /**
60
     * @param $text
61
     * @return mixed|string
62
     */
63
    public function cleanup($text)
64
    {
65
        global $myts;
66
67
        $clean = \stripslashes($text);
68
        $clean = &$myts->displayTarea($clean, 1, 0, 1);
69
        $clean = \strip_tags($clean);
70
        $clean = \htmlspecialchars($clean, \ENT_QUOTES);
71
72
        return $clean;
73
    }
74
75
    /**
76
     * @param bool $isSummary
77
     * @param bool $isHtml
78
     * @return bool
79
     */
80
    public function buildContent($isSummary = true, $isHtml = false)
81
    {
82
        $digest_count = \count($this->items);
83
        $content      = '';
84
        if ($digest_count > 0) {
85
            $linebreak = $isHtml ? '<br>' : "\n";
86
            for ($i = 0; $i < $digest_count; ++$i) {
87
                if ($isHtml) {
88
                    $content .= ($i + 1) . '. <a href=' . $this->items[$i]['link'] . '>' . $this->items[$i]['title'] . '</a>';
89
                } else {
90
                    $content .= ($i + 1) . '. ' . $this->items[$i]['title'] . $linebreak . $this->items[$i]['link'];
91
                }
92
93
                $content .= $linebreak . $this->items[$i]['author'];
94
                if ($isSummary) {
95
                    $content .= $linebreak . $this->items[$i]['summary'];
96
                }
97
                $content .= $linebreak . $linebreak;
98
            }
99
        }
100
        $this->setVar('digest_content', $content);
101
102
        return true;
103
    }
104
}
105