Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
69.05% covered (warning)
69.05%
29 / 42
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
GalleryCell
69.05% covered (warning)
69.05%
29 / 42
0.00% covered (danger)
0.00%
0 / 1
4.47
0.00% covered (danger)
0.00%
0 / 1
 display
69.05% covered (warning)
69.05%
29 / 42
0.00% covered (danger)
0.00%
0 / 1
4.47
1<?php
2declare(strict_types=1);
3
4namespace App\View\Cell;
5
6use Cake\I18n\I18n;
7use Cake\Log\LogTrait;
8use Cake\View\Cell;
9use Exception;
10
11/**
12 * Gallery Cell
13 *
14 * Provides mini-controller functionality for rendering gallery placeholders.
15 * Handles data fetching and preparation for gallery display components.
16 *
17 * Following CakePHP Cell conventions, this class:
18 * - Fetches data using fetchTable() (like a controller)
19 * - Sets view variables using set() (like a controller)
20 * - Renders templates in templates/cell/Gallery/
21 */
22class GalleryCell extends Cell
23{
24    use LogTrait;
25
26    /**
27     * Display a gallery with proper translation support
28     *
29     * @param string $galleryId Gallery UUID
30     * @param string $theme Gallery display theme (default: 'default')
31     * @param string $title Gallery title override (optional)
32     * @return void Sets view variables for template rendering
33     */
34    public function display(string $galleryId, string $theme = 'default', string $title = ''): void
35    {
36        try {
37            // Get the ImageGalleries table and set current locale for translations
38            $galleriesTable = $this->fetchTable('ImageGalleries');
39            $currentLocale = I18n::getLocale();
40
41            // Debug: Log the current locale being used
42            $this->log(sprintf('GalleryCell: Using locale %s for gallery %s', $currentLocale, $galleryId), 'debug');
43
44            // Explicitly set the locale on the table for TranslateBehavior
45            $galleriesTable->setLocale($currentLocale);
46
47            // Generate locale-aware cache key from request (same logic as AppController)
48            $cacheKey = hash('xxh3', json_encode($this->request->getAttribute('params')));
49
50            // Fetch gallery data using the table's dedicated method
51            // For admin theme, don't require published status
52            $requirePublished = $theme !== 'admin';
53            $gallery = $galleriesTable->getGalleryForPlaceholder($galleryId, $requirePublished, $cacheKey);
54
55            if (!$gallery || empty($gallery->images)) {
56                // Set empty flag for template to handle gracefully
57                $this->set([
58                    'gallery' => null,
59                    'theme' => $theme,
60                    'title' => $title,
61                    'isEmpty' => true,
62                ]);
63
64                return;
65            }
66
67            // Use translated gallery name, not the title from placeholder
68            // This ensures translations work properly
69            $displayTitle = $gallery->name;
70
71            // Set data for template rendering
72            $this->set([
73                'gallery' => $gallery,
74                'images' => $gallery->images,
75                'theme' => $theme,
76                'title' => $displayTitle,
77                'description' => $gallery->description,
78                'isEmpty' => false,
79            ]);
80
81            $this->log(
82                sprintf('Successfully rendered gallery cell for ID: %s', $galleryId),
83                'debug',
84                ['group_name' => 'App\\View\\Cell\\GalleryCell'],
85            );
86        } catch (Exception $e) {
87            // Log error and set empty state for graceful degradation
88            $this->log(
89                sprintf('GalleryCell error for gallery %s: %s', $galleryId, $e->getMessage()),
90                'error',
91                ['group_name' => 'App\\View\\Cell\\GalleryCell'],
92            );
93
94            $this->set([
95                'gallery' => null,
96                'theme' => $theme,
97                'title' => $title,
98                'isEmpty' => true,
99                'error' => $e->getMessage(),
100            ]);
101        }
102    }
103}