Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
126 / 126
100.00% covered (success)
100.00%
12 / 12
CRAP
100.00% covered (success)
100.00%
1 / 1
News
100.00% covered (success)
100.00%
126 / 126
100.00% covered (success)
100.00%
12 / 12
23
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getAll
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
1 / 1
4
 getLatestData
100.00% covered (success)
100.00%
31 / 31
100.00% covered (success)
100.00%
1 / 1
4
 getLatestDataPaginated
100.00% covered (success)
100.00%
34 / 34
100.00% covered (success)
100.00%
1 / 1
2
 countLatestData
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getHeader
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
2
 get
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
1 / 1
4
 create
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 update
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 delete
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 activate
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 deactivate
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * The News class for phpMyFAQ news.
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public License,
7 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
8 * obtain one at https://mozilla.org/MPL/2.0/.
9 *
10 * @package   phpMyFAQ
11 * @author    Thorsten Rinne <thorsten@phpmyfaq.de>
12 * @author    Matteo Scaramuccia <matteo@scaramuccia.com>
13 * @copyright 2006-2026 phpMyFAQ Team
14 * @license   https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
15 * @link      https://www.phpmyfaq.de
16 * @since     2006-06-25
17 */
18
19declare(strict_types=1);
20
21namespace phpMyFAQ;
22
23use Exception;
24use phpMyFAQ\Entity\NewsMessage;
25use phpMyFAQ\Link\Util\TitleSlugifier;
26use phpMyFAQ\News\NewsRepository;
27use phpMyFAQ\News\NewsRepositoryInterface;
28use stdClass;
29
30/**
31 * Class News
32 *
33 * @package phpMyFAQ
34 */
35readonly class News
36{
37    private NewsRepositoryInterface $newsRepository;
38
39    /**
40     * Constructor.
41     */
42    public function __construct(
43        private Configuration $configuration,
44        ?NewsRepositoryInterface $newsRepository = null,
45    ) {
46        $this->newsRepository = $newsRepository ?? new NewsRepository($this->configuration);
47    }
48
49    /**
50     * Returns the current news as an array.
51     *
52     * @param bool $showArchive Show archived news
53     * @param bool $active Show active news
54     * @return stdClass[]
55     * @throws Exception
56     */
57    public function getAll(bool $showArchive = false, bool $active = true): array
58    {
59        $output = [];
60        $date = new Date($this->configuration);
61        $language = $this->configuration->getLanguage()->getLanguage();
62        $limit = null;
63        if (!$showArchive && $active) {
64            $limit = (int) $this->configuration->get(item: 'records.numberOfShownNewsEntries');
65        }
66
67        foreach ($this->newsRepository->getLatest($language, $active, $limit) as $row) {
68            $entry = new stdClass();
69            $entry->url = sprintf(
70                '%snews/%d/%s/%s.html',
71                $this->configuration->getDefaultUrl(),
72                (int) $row->id,
73                (string) $row->lang,
74                TitleSlugifier::slug((string) $row->header),
75            );
76            $entry->header = $row->header;
77            $entry->content = strip_tags((string) $row->artikel);
78            $entry->date = $date->format((string) $row->datum);
79            $output[] = $entry;
80        }
81
82        return $output;
83    }
84
85    /**
86     * Return the latest news data.
87     *
88     * @param bool $showArchive Show archived news
89     * @param bool $active Show active news
90     * @param bool $forceConfLimit Force to limit in configuration
91     * @return array<int, array>
92     */
93    public function getLatestData(bool $showArchive = false, bool $active = true, bool $forceConfLimit = false): array
94    {
95        $news = [];
96        $language = $this->configuration->getLanguage()->getLanguage();
97        $configuredLimit = (int) $this->configuration->get(item: 'records.numberOfShownNewsEntries');
98        $limit = null;
99        if ($configuredLimit > 0 && !$showArchive) {
100            $limit = $configuredLimit;
101        }
102
103        foreach ($this->newsRepository->getLatest($language, $active, $limit) as $row) {
104            $url = sprintf(
105                '%snews/%d/%s/%s.html',
106                $this->configuration->getDefaultUrl(),
107                (int) $row->id,
108                (string) $row->lang,
109                TitleSlugifier::slug((string) $row->header),
110            );
111            $item = [
112                'id' => (int) $row->id,
113                'lang' => $row->lang,
114                'date' => Date::createIsoDate((string) $row->datum, DATE_ATOM),
115                'header' => $row->header,
116                'content' => $row->artikel,
117                'authorName' => $row->author_name,
118                'authorEmail' => $row->author_email,
119                'active' => 'y' === $row->active,
120                'allowComments' => 'y' === $row->comment,
121                'link' => $row->link,
122                'linkTitle' => $row->linktitel,
123                'target' => $row->target,
124                'url' => $url,
125            ];
126            $news[] = $item;
127        }
128
129        return $news;
130    }
131
132    /**
133     * Fetches paginated news data with sorting support for API.
134     *
135     * @param bool $active Filter by active status
136     * @param int $limit Number of items per page
137     * @param int $offset Starting offset
138     * @param string $sortField Field to sort by
139     * @param string $sortOrder Sort direction (ASC, DESC)
140     * @return array
141     */
142    public function getLatestDataPaginated(
143        bool $active = true,
144        int $limit = 25,
145        int $offset = 0,
146        string $sortField = 'datum',
147        string $sortOrder = 'DESC',
148    ): array {
149        $news = [];
150        $language = $this->configuration->getLanguage()->getLanguage();
151
152        foreach ($this->newsRepository->getLatestPaginated(
153            $language,
154            $active,
155            $limit,
156            $offset,
157            $sortField,
158            $sortOrder,
159        ) as $row) {
160            $url = sprintf(
161                '%snews/%d/%s/%s.html',
162                $this->configuration->getDefaultUrl(),
163                (int) $row->id,
164                (string) $row->lang,
165                TitleSlugifier::slug((string) $row->header),
166            );
167            $item = [
168                'id' => (int) $row->id,
169                'lang' => $row->lang,
170                'date' => Date::createIsoDate((string) $row->datum, DATE_ATOM),
171                'header' => $row->header,
172                'content' => $row->artikel,
173                'authorName' => $row->author_name,
174                'authorEmail' => $row->author_email,
175                'active' => 'y' === $row->active,
176                'allowComments' => 'y' === $row->comment,
177                'link' => $row->link,
178                'linkTitle' => $row->linktitel,
179                'target' => $row->target,
180                'url' => $url,
181            ];
182            $news[] = $item;
183        }
184
185        return $news;
186    }
187
188    /**
189     * Counts total news entries for current language.
190     *
191     * @param bool $active Filter by active status
192     * @return int Total count
193     */
194    public function countLatestData(bool $active = true): int
195    {
196        $language = $this->configuration->getLanguage()->getLanguage();
197        return $this->newsRepository->countLatest($language, $active);
198    }
199
200    /**
201     * Fetches all news headers.
202     */
203    public function getHeader(): array
204    {
205        $headers = [];
206        $language = $this->configuration->getLanguage()->getLanguage();
207        foreach ($this->newsRepository->getHeaders($language) as $header) {
208            $headers[$header->id] = [
209                'id' => $header->id,
210                'lang' => $header->lang,
211                'header' => $header->header,
212                'date' => Date::createIsoDate((string) $header->datum),
213                'active' => $header->active,
214            ];
215        }
216
217        return $headers;
218    }
219
220    /**
221     * Fetches a news entry identified by its ID.
222     *
223     * @param int  $newsId ID of news
224     * @param bool $admin Is admin
225     * @return array<string, mixed>
226     */
227    public function get(int $newsId, bool $admin = false): array
228    {
229        $row = $this->newsRepository->getById($newsId, $this->configuration->getLanguage()->getLanguage());
230        if (!$row) {
231            return [];
232        }
233
234        $content = $row->artikel;
235        $active = 'y' === $row->active;
236        $allowComments = 'y' === $row->comment;
237        if (!$admin && !$active) {
238            $content = Translation::get(key: 'err_inactiveNews');
239        }
240
241        return [
242            'id' => $row->id,
243            'lang' => $row->lang,
244            'date' => Date::createIsoDate((string) $row->datum),
245            'header' => $row->header,
246            'content' => $content,
247            'authorName' => $row->author_name,
248            'authorEmail' => $row->author_email,
249            'active' => $active,
250            'allowComments' => $allowComments,
251            'link' => $row->link,
252            'linkTitle' => $row->linktitel,
253            'target' => $row->target,
254        ];
255    }
256
257    /**
258     * Adds a new news entry.
259     *
260     * @param NewsMessage $newsMessage NewsMessage object with news data
261     */
262    public function create(NewsMessage $newsMessage): bool
263    {
264        return $this->newsRepository->insert($newsMessage);
265    }
266
267    /**
268     * Updates a new news entry identified by its ID.
269     *
270     * @param NewsMessage $newsMessage NewsMessage object with news data
271     */
272    public function update(NewsMessage $newsMessage): bool
273    {
274        return $this->newsRepository->update($newsMessage);
275    }
276
277    /**
278     * Deletes a news entry identified by its ID.
279     *
280     * @param int $newsId News ID
281     * @todo   check if there are comments attached to the deleted news
282     */
283    public function delete(int $newsId): bool
284    {
285        return $this->newsRepository->delete($newsId, $this->configuration->getLanguage()->getLanguage());
286    }
287
288    /**
289     * Activates/Deactivates a news message
290     *
291     * @param int $newsId News ID
292     */
293    public function activate(int $newsId): bool
294    {
295        return $this->newsRepository->activate($newsId, status: true);
296    }
297
298    public function deactivate(int $newsId): bool
299    {
300        return $this->newsRepository->activate($newsId, status: false);
301    }
302}