Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
90.00% covered (success)
90.00%
9 / 10
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
RecentPostsCell
90.00% covered (success)
90.00%
9 / 10
0.00% covered (danger)
0.00%
0 / 1
3.01
0.00% covered (danger)
0.00%
0 / 1
 display
90.00% covered (success)
90.00%
9 / 10
0.00% covered (danger)
0.00%
0 / 1
3.01
1<?php
2declare(strict_types=1);
3
4namespace DefaultTheme\View\Cell;
5
6use Cake\View\Cell;
7
8/**
9 * RecentPosts cell
10 *
11 * Displays recent articles in sidebar or other locations
12 */
13class RecentPostsCell extends Cell
14{
15    /**
16     * List of valid options that can be passed into this
17     * cell's constructor.
18     *
19     * @var array<string>
20     */
21    protected array $_validCellOptions = ['cacheKey', 'title', 'excludeIds'];
22
23    /**
24     * Default display method
25     *
26     * Fetches and displays recent articles
27     *
28     * @param string|null $cacheKey Optional cache key for performance
29     * @param string|null $title Optional custom title for the section
30     * @param array $excludeIds Array of article IDs to exclude
31     * @return void
32     */
33    public function display(?string $cacheKey = null, ?string $title = null, array $excludeIds = []): void
34    {
35        $conditions = [];
36        if (!empty($excludeIds)) {
37            $conditions['Articles.id NOT IN'] = $excludeIds;
38        }
39
40        $articlesTable = $this->fetchTable('Articles');
41
42        // Generate cache key if not provided
43        if ($cacheKey === null) {
44            $lang = $this->request->getParam('lang', 'en');
45            $cacheKey = 'recent_' . $lang . '_';
46        }
47
48        $articles = $articlesTable->getRecentArticles($cacheKey, $conditions);
49
50        $this->set('articles', $articles);
51        $this->set('title', $title ?? __('Recent posts'));
52    }
53}