<?php

// Ensure ng-helpers is loaded
if (!function_exists('Plugins\\formatBytes')) {
    $ngHelpersPath = __DIR__ . '/../../ng-helpers/ng-helpers.php';
    if (file_exists($ngHelpersPath)) {
        require_once $ngHelpersPath;
    }
}

// Import ng-helpers functions
use function Plugins\{cache_get, cache_put, formatBytes, logger, get_ip, notify, sanitize};

/*
 * Class PluginGallery
 * Description: PluginGallery
 */

class PluginGallery
{
    protected $lang = [];
    protected $params = [];

    protected $galleries = [];

    protected $pluginTitle;

    public function __construct()
    {
        global $lang;

        $this->lang = $lang;

        $this->loadParams();
        $this->loadGalleries();

        $this->pluginTitle = $this->params['seo_title'] ?: $this->lang['gallery:title'];
    }

    // Load all params of plugin gallery
    protected function loadParams()
    {
        global $PLUGINS;

        $this->params = $PLUGINS['config']['gallery'];
    }

    // Load info about galleries
    protected function loadGalleries()
    {
        global $mysql;

        if (($this->galleries = cacheRetrieveFile('galleries.dat', 86400, 'gallery')) === false) {
            $rows = $mysql->select('SELECT *, (SELECT count(*) FROM ' . prefix . '_images WHERE folder=' . prefix . '_gallery.name) AS count FROM ' . prefix . '_gallery WHERE if_active=1 ORDER BY position');
            foreach ($rows as $row) {
                $id = (int) $row['id'];
                $name = $folder = secure_html($row['name']);
                $icon = secure_html($row['icon']);
                $this->galleries[$name] = [
                    'id' => $id,
                    'name' => $name,
                    'title' => secure_html($row['title']),
                    'url' => generatePluginLink('gallery', 'gallery', ['id' => $id, 'name' => $name]),
                    'count' => $row['count'], // count images in gallery
                    'images_count' => $row['images_count'], // count images in gallery for display in page gallery
                    'description' => secure_html($row['description']),
                    'keywords' => secure_html($row['keywords']),
                    'position' => (int) $row['position'],
                    'skin' => secure_html($row['skin']),
                    'icon' => images_url . '/' . $folder . '/' . $icon,
                    'icon_thumb' => file_exists(images_dir . '/' . $folder . '/thumb/' . $icon)
                        ? images_url . '/' . $folder . '/thumb/' . $icon
                        : images_url . '/' . $folder . '/' . $icon,
                ];
            }
            cacheStoreFile('galleries.dat', serialize($this->galleries), 'gallery');
        } else {
            $this->galleries = unserialize($this->galleries);
        }
    }

    public function categoryAction()
    {
        global $twig, $mysql, $template;

        // Register skin assets
        if (function_exists('gallery_register_skin_assets')) {
            gallery_register_skin_assets(pluginGetVariable('gallery', 'skin'), 'category');
        }

        if ($this->params['cache']) {
            $cacheFileName = md5('gallery' . 'category') . '.txt';
            $cacheData = cacheRetrieveFile($cacheFileName, (int) $this->params['cache_expire'], 'gallery');
            if ($cacheData != false) {
                return $template['vars']['plugin_gallery_category'] = $cacheData;
            }
        }

        $tPath = locatePluginTemplates(
            ['category'],
            'gallery',
            0,
            pluginGetVariable('gallery', 'skin')
        );
        // Register skin assets
        if (function_exists('gallery_register_skin_assets')) {
            gallery_register_skin_assets(pluginGetVariable('gallery', 'skin'), 'page_index');
        }
        $tVars = [
            'plugin_title' => $this->pluginTitle,
            'url_tpl' => $tPath['url:category'],
            'url_main' => generatePluginLink('gallery', null),
            'galleries' => $this->galleries,
        ];
        $template['vars']['plugin_gallery_category'] = $twig->render($tPath['category'] . 'category.tpl', $tVars);

        if ($this->params['cache']) {
            cacheStoreFile($cacheFileName, $template['vars']['plugin_gallery_category'], 'gallery');
        }
    }

    public function widgetAction()
    {
        global $template, $twig, $mysql, $config;

        if (empty($this->params['widgets']) or ! is_array($widgets = $this->params['widgets'])) {
            return;
        }

        foreach ($widgets as $id => $widget) {
            if (! $widget['if_active']) {
                continue;
            }

            $widgetName = 'plugin_gallery_' . $widget['name'];

            if ($this->params['cache']) {
                $cacheFileName = md5($config['theme'] . $config['default_lang'] . $widget['skin'] . $widgetName) . '.txt';
                $cacheData = cacheRetrieveFile($cacheFileName, (int) $this->params['cache_expire'], 'gallery');
                if ($cacheData != false) {
                    // We got data from cache. Return it and stop
                    $template['vars'][$widgetName] = $cacheData;
                    continue;
                }
            }

            $where = " where folder <>''";
            if (! empty($widget['gallery'])) {
                $where = ' where folder=' . db_squote($widget['gallery']);
            }

            $limit = 'limit ' . $widget['images_count'];
            if ($widget['if_rand'] == 1) {
                $image_key = $mysql->select('select id from ' . prefix . '_images ' . $where);
                if (is_array($image_key) && count($image_key)) {
                    shuffle($image_key);
                    if ($limit) {
                        $image_key = array_slice($image_key, 0, $widget['images_count']);
                    }
                    $where .= ' and ';
                    $t_key_list = '';
                    foreach ($image_key as $img) {
                        if ($t_key_list) {
                            $t_key_list .= ', ';
                        }
                        $t_key_list .= $img['id'];
                    }
                    $where .= ' id in (' . $t_key_list . ')';
                }
            } elseif ($widget['if_rand'] == 2) {
                $where .= ' ORDER BY views desc';
            } elseif ($widget['if_rand'] == 3) {
                $where .= ' ORDER BY com desc';
            }

            $images = [];
            $rows = $mysql->select('SELECT id, name, folder, description, views, com FROM ' . prefix . '_images ' . $where . ' ' . $limit);
            foreach ($rows as $row) {
                $id = (int) $row['id'];
                $name = secure_html($row['name']);
                $folder = secure_html($row['folder']);
                $images[] = [
                    'id' => $id,
                    'title' => $name,
                    'com' => (int) $row['com'],
                    'views' => (int) $row['views'],
                    'description' => secure_html($row['description']),
                    'url' => generatePluginLink('gallery', 'image', ['gallery' => $folder, 'id' => $id, 'name' => $name]),
                    'src' => file_exists(images_dir . '/' . $folder . '/' . $name)
                        ? images_url . '/' . $folder . '/' . $name
                        : '',
                    'src_thumb' => file_exists(images_dir . '/' . $folder . '/thumb/' . $name)
                        ? images_url . '/' . $folder . '/thumb/' . $name
                        : images_url . '/' . $folder . '/' . $name,
                    'gallery_url' => $this->galleries[$row['folder']]['url'],
                    'gallery_title' => $this->galleries[$row['folder']]['title'],
                ];
            }

            $tPath = locatePluginTemplates(['widget'], 'gallery', 0, $widget['skin']);
            // Register skin assets
            if (function_exists('gallery_register_skin_assets')) {
                gallery_register_skin_assets($widget['skin'], 'widget');
            }
            $tVars = [
                'url_tpl' => $tPath['url:widget'],
                'url_main' => generatePluginLink('gallery', null),
                'images' => $images,
                'widget_title' => $widget['title'],
            ];

            $template['vars'][$widgetName] = $twig->render($tPath['widget'] . 'widget.tpl', $tVars);

            if ($this->params['cache']) {
                cacheStoreFile($cacheFileName, $template['vars'][$widgetName], 'gallery');
            }
        }
    }

    public function indexPageAction($params)
    {
        global $userROW, $template, $twig, $lang, $mysql, $TemplateCache, $SYSTEM_FLAGS;

        $page = isset($params['page']) ? abs(intval($params['page'])) : (isset($_REQUEST['page']) ? abs(intval($_REQUEST['page'])) : 1);

        $SYSTEM_FLAGS['info']['title']['group'] = $this->pluginTitle;
        $SYSTEM_FLAGS['info']['title']['item'] = $this->lang['gallery:page'] . ' ' . $page;
        if ($this->params['if_description'] and ! empty($this->params['seo_description'])) {
            $SYSTEM_FLAGS['meta']['description'] = $this->params['seo_description'];
        }
        if ($this->params['if_keywords'] and ! empty($this->params['seo_keywords'])) {
            $SYSTEM_FLAGS['meta']['keywords'] = $this->params['seo_keywords'];
        }
        $SYSTEM_FLAGS['info']['breadcrumbs'] = [['link' => generatePluginLink('gallery', null), 'text' => $this->pluginTitle]];

        if ($this->params['cache']) {
            $cacheFileName = md5('gallery' . 'mainblock' . $page) . '.txt';
            $cacheData = cacheRetrieveFile($cacheFileName, (int) $this->params['cache_expire'], 'gallery');
            if ($cacheData != false) {
                return $template['vars']['mainblock'] .= $cacheData;
            }
        }

        // Work with array galleries, below we [SLICE] array
        $galleries = $this->galleries;

        $pagesss = '';
        if ($this->params['galleries_count'] && is_array($galleries) && count($galleries) > 0) {
            $pagesCount = ceil(count($galleries) / $this->params['galleries_count']);
            if ($pagesCount > 1) {
                $galleries = array_slice($galleries, ($page - 1) * $this->params['galleries_count'], $this->params['galleries_count']);
                $paginationParams = ['pluginName' => 'gallery', 'pluginHandler' => '', 'params' => [], 'xparams' => [], 'paginator' => ['page', 0, false]];
                templateLoadVariables(true);
                $navigations = $TemplateCache['site']['#variables']['navigation'];
                $pagesss .= ($page > 1) ? str_replace('%page%', $this->lang['prev_page'], str_replace('%link%', generatePageLink($paginationParams, $page - 1), $navigations['prevlink'])) : '';
                $pagesss .= generatePagination($page, 1, $pagesCount, 10, $paginationParams, $navigations);
                $pagesss .= ($page < $pagesCount) ? str_replace('%page%', $this->lang['next_page'], str_replace('%link%', generatePageLink($paginationParams, $page + 1), $navigations['nextlink'])) : '';
            }
        }

        $tPath = locatePluginTemplates(
            ['page_index'],
            'gallery',
            0,
            pluginGetVariable('gallery', 'skin')
        );

        // Register skin assets
        if (function_exists('gallery_register_skin_assets')) {
            gallery_register_skin_assets(pluginGetVariable('gallery', 'skin'), 'page_index');
        }

        $tVars = [
            'plugin_title' => $this->pluginTitle,
            'url_tpl' => $tPath['url:page_index'],
            'url_main' => generatePluginLink('gallery', null),
            'galleries' => $galleries,
            'pagesss' => $pagesss,
        ];
        $template['vars']['mainblock'] .= $output = $twig->render($tPath['page_index'] . 'page_index.tpl', $tVars);

        if ($this->params['cache']) {
            cacheStoreFile($cacheFileName, $output, 'gallery');
        }
    }

    public function galleryPageAction($params)
    {
        global $userROW, $template, $twig, $lang, $mysql, $TemplateCache, $SYSTEM_FLAGS;

        $page = ! empty($params['page']) ? intval($params['page']) : (! empty($_REQUEST['page']) ? intval($_REQUEST['page']) : 1);
        if ($page < 1) {
            $page = 1;
        }
        $gallery['name'] = ! empty($params['name']) ? secure_html($params['name']) : (! empty($_REQUEST['name']) ? secure_html($_REQUEST['name']) : false);

        if (! $gallery['name']) {
            notify('danger', 'Не все параметры заданы');
            return false;
        }

        if (! is_array($gallery = $this->galleries[$gallery['name']])) {
            notify('danger', 'Не все параметры заданы');
            return false;
        }

        $SYSTEM_FLAGS['info']['title']['group'] = $this->pluginTitle . ' ' . $gallery['title'];
        $SYSTEM_FLAGS['info']['title']['item'] = $this->lang['gallery:page'] . ' ' . $page;
        if ($this->params['if_description']) {
            $SYSTEM_FLAGS['meta']['description'] = $gallery['description'];
        }
        if ($this->params['if_keywords']) {
            $SYSTEM_FLAGS['meta']['keywords'] = $gallery['keywords'];
        }
        $SYSTEM_FLAGS['info']['breadcrumbs'] = [
            ['link' => generatePluginLink('gallery', null), 'text' => $this->pluginTitle],
            ['link' => generatePluginLink('gallery', 'gallery', ['id' => $gallery['id'], 'name' => $gallery['name']]), 'text' => $gallery['title']],
        ];

        if ($this->params['cache']) {
            $cacheFileName = md5('gallery' . $gallery['id'] . $gallery['name'] . $page) . '.txt';
            $cacheData = cacheRetrieveFile($cacheFileName, (int) $this->params['cache_expire'], 'gallery');
            if ($cacheData != false) {
                return $template['vars']['mainblock'] .= $cacheData;
            }
        }

        $images = [];
        $limit = 'LIMIT ' . ($page - 1) * $gallery['images_count'] . ', ' . $gallery['images_count'];
        $rows = $mysql->select('SELECT id, name, description, folder, views, com,width,height FROM ' . prefix . '_images WHERE folder=' . db_squote($gallery['name']) . ' ORDER BY date asc, id asc ' . $limit);
        foreach ($rows as $row) {
            $id = (int) $row['id'];
            $name = secure_html($row['name']);
            $folder = secure_html($row['folder']);

            $images[] = [
                'id' => $id,
                'name' => $name,
                'com' => (int) $row['com'],
                'views' => (int) $row['views'],
                'width' => (int) $row['width'],
                'height' => (int) $row['height'],
                'size' => is_readable($fname = images_dir . '/' . $folder . '/' . $name) ? formatBytes(filesize($fname)) : '-',
                'description' => secure_html($row['description']),
                'url' => generatePluginLink('gallery', 'image', ['gallery' => $folder, 'id' => $id, 'name' => $name]),
                'src' => images_url . '/' . $folder . '/' . $name,
                'src_thumb' => file_exists(images_url . '/' . $folder . '/thumb/' . $name)
                    ? images_url . '/' . $folder . '/thumb/' . $name
                    : images_url . '/' . $folder . '/' . $name,
            ];
        }

        $pagesss = '';
        if ($gallery['images_count']) {
            $count = 0;
            if (is_array($pcnt = $mysql->record('select count(*) as cnt from ' . prefix . '_images where folder=' . db_squote($gallery['name'])))) {
                $count = $pcnt['cnt'];
            }
            $pagesCount = ceil($count / $gallery['images_count']);
            if ($pagesCount > 1) {
                $paginationParams = ['pluginName' => 'gallery', 'pluginHandler' => 'gallery', 'params' => ['id' => $gallery['id'], 'name' => $gallery['name']], 'xparams' => [], 'paginator' => ['page', 0, false]];
                templateLoadVariables(true);
                $navigations = $TemplateCache['site']['#variables']['navigation'];
                $pagesss .= ($page > 1) ? str_replace('%page%', $this->lang['prev_page'], str_replace('%link%', generatePageLink($paginationParams, $page - 1), $navigations['prevlink'])) : '';
                $pagesss .= generatePagination($page, 1, $pagesCount, 10, $paginationParams, $navigations);
                $pagesss .= ($page < $pagesCount) ? str_replace('%page%', $this->lang['next_page'], str_replace('%link%', generatePageLink($paginationParams, $page + 1), $navigations['nextlink'])) : '';
            }
        }

        $tPath = locatePluginTemplates(['page_gallery'], 'gallery', 0, $gallery['skin']);

        // Register skin assets
        if (function_exists('gallery_register_skin_assets')) {
            gallery_register_skin_assets($gallery['skin'], 'page_gallery');
        }

        $tVars = [
            'plugin_title' => $this->pluginTitle,
            'url_tpl' => $tPath['url:page_gallery'],
            'url_main' => generatePluginLink('gallery', null),
            'images' => $images,
            'gallery' => [
                'url' => generatePluginLink('gallery', 'gallery', ['id' => $gallery['id'], 'name' => $gallery['name']]),
                'title' => $gallery['title'],
                'description' => $gallery['description'],
                'keywords' => $gallery['keywords'],
            ],
            'pagesss' => $pagesss,
        ];
        $template['vars']['mainblock'] .= $output = $twig->render($tPath['page_gallery'] . 'page_gallery.tpl', $tVars);

        if ($this->params['cache']) {
            cacheStoreFile($cacheFileName, $output, 'gallery');
        }
    }

    public function imagePageAction($params)
    {
        global $userROW, $template, $tpl, $twig, $lang, $mysql, $TemplateCache, $SYSTEM_FLAGS;
        global $galleries;

        // Debug logging
        $debugFile = root . 'engine/cache/gallery_debug.txt';
        file_put_contents($debugFile, date('Y-m-d H:i:s') . " - imagePageAction called\n", FILE_APPEND);
        file_put_contents($debugFile, "Params: " . print_r($params, true) . "\n", FILE_APPEND);

        $imageName = ! empty($params['name']) ? secure_html($params['name']) : false;
        $galleryName = ! empty($params['gallery']) ? secure_html($params['gallery']) : false;

        file_put_contents($debugFile, "Image: $imageName, Gallery: $galleryName\n", FILE_APPEND);

        if (! $imageName or ! $galleryName) {
            file_put_contents($debugFile, "ERROR: Missing image or gallery name\n", FILE_APPEND);
            error404();
            return false;
        }

        // Try to get gallery from cache first
        if (isset($this->galleries[$galleryName]) && is_array($this->galleries[$galleryName])) {
            $gallery = $this->galleries[$galleryName];
            file_put_contents($debugFile, "Gallery found in cache: {$galleryName}\n", FILE_APPEND);
        } else {
            file_put_contents($debugFile, "Gallery NOT in cache, loading from DB: {$galleryName}\n", FILE_APPEND);
            // If not in cache, try to load from database
            $galleryRow = $mysql->record('SELECT * FROM ' . prefix . '_gallery WHERE name=' . db_squote($galleryName) . ' AND if_active=1 LIMIT 1');
            if (!is_array($galleryRow) || empty($galleryRow)) {
                file_put_contents($debugFile, "ERROR: Gallery not found in DB: {$galleryName}\n", FILE_APPEND);
                if (function_exists('Plugins\\logger')) {
                    logger('Gallery not found or inactive: ' . $galleryName, 'error', 'gallery.log');
                }
                error404();
                return false;
            }

            $gallery = [
                'id' => (int) $galleryRow['id'],
                'name' => secure_html($galleryRow['name']),
                'title' => secure_html($galleryRow['title']),
                'description' => secure_html($galleryRow['description']),
                'keywords' => secure_html($galleryRow['keywords']),
            ];

            file_put_contents($debugFile, "Gallery loaded from DB successfully\n", FILE_APPEND);
            if (function_exists('Plugins\\logger')) {
                logger('Gallery loaded from DB: ' . $galleryName, 'info', 'gallery.log');
            }
        }

        $SYSTEM_FLAGS['info']['title']['group'] = $this->pluginTitle . ' ' . $gallery['title'];
        $SYSTEM_FLAGS['info']['title']['item'] = $imageName;
        if ($this->params['if_keywords']) {
            $SYSTEM_FLAGS['meta']['keywords'] = $gallery['keywords'];
        }
        if ($this->params['if_description']) {
            $SYSTEM_FLAGS['meta']['description'] = $gallery['description'];
        }
        $SYSTEM_FLAGS['info']['breadcrumbs'] = [
            ['link' => generatePluginLink('gallery', null), 'text' => $this->pluginTitle],
            ['link' => generatePluginLink('gallery', 'gallery', ['id' => $gallery['id'], 'name' => $gallery['name']]), 'text' => $gallery['title']],
            ['link' => generatePluginLink('gallery', 'image', ['gallery' => $gallery['name'], 'name' => $imageName]), 'text' => $imageName],
        ];

        // Temporaly disabled cached
        if ($this->params['cache']) {
            //$havePerm = (is_array($userROW) and (($userROW['status'] == 1) or ($userROW['status'] == 2) or ($row['author_id'] == $userROW['id'])));
            $cacheFileName = md5('gallery' . $gallery['id'] . $gallery['name'] . $imageName) . '.txt';
            $cacheData = cacheRetrieveFile($cacheFileName, (int) $this->params['cache_expire'], 'gallery');
            if ($cacheData != false) {
                //return $template['vars']['mainblock'] .= $cacheData;
            }
        }

        $row = $mysql->record('SELECT * FROM ' . prefix . '_images WHERE folder=' . db_squote($gallery['name']) . ' AND name=' . db_squote($imageName) . ' ORDER BY date LIMIT 1');

        $debugFile = root . 'engine/cache/gallery_debug.txt';
        file_put_contents($debugFile, "SQL query executed for image\n", FILE_APPEND);
        file_put_contents($debugFile, "Result: " . print_r($row, true) . "\n", FILE_APPEND);

        if (!is_array($row) || empty($row)) {
            file_put_contents($debugFile, "ERROR: Image not found in DB\n", FILE_APPEND);
            if (function_exists('Plugins\\logger')) {
                logger('Image not found in DB: gallery=' . $gallery['name'] . ', name=' . $imageName, 'error', 'gallery.log');
            }
            error404();
            return false;
        }

        file_put_contents($debugFile, "SUCCESS: Image found in DB, ID=" . $row['id'] . "\n", FILE_APPEND);

        // Update views count AFTER confirming image exists
        $mysql->query('UPDATE ' . prefix . '_images SET views=views+1 WHERE folder=' . db_squote($gallery['name']) . ' AND name=' . db_squote($imageName));

        $row['id'] = (int) $row['id'];
        $row['com'] = (int) $row['com'];
        $row['views'] = (int) $row['views'];
        $row['width'] = (int) $row['width'];
        $row['height'] = (int) $row['height'];
        $row['name'] = secure_html($row['name']);
        $row['description'] = secure_html($row['description']);

        // Reload meta-description of page
        if ($this->params['if_description']) {
            $SYSTEM_FLAGS['meta']['description'] = $row['description'];
        }

        // Prepare date to generate output Prev and Next
        templateLoadVariables(true);
        $nav = $TemplateCache['site']['#variables']['navigation'];

        // Prev image, if isset
        $pimage = $mysql->select('SELECT name FROM ' . prefix . '_images WHERE folder=' . db_squote($gallery['name']) . ' AND id<' . db_squote($row['id']) . ' ORDER BY `id` desc limit 1');
        if (isset($pimage[0])) {
            $imageName = secure_html($pimage[0]['name']);
            $paginationParams = [
                'pluginName' => 'gallery',
                'pluginHandler' => 'image',
                'params' => ['id' => $gallery['id'], 'gallery' => $gallery['name'], 'name' => $imageName],
                'xparams' => [],
                'paginator' => ['page', 0, false],
            ];
            $prevlink = str_replace('%page%', $this->lang['gallery:prevlink'], str_replace('%link%', generatePageLink($paginationParams, 0), $nav['prevlink']));
        } else {
            $prevlink = '';
        }

        // Next image, if isset
        $nimage = $mysql->select('select name from ' . prefix . '_images where folder=' . db_squote($gallery['name']) . ' and id>' . db_squote($row['id']) . ' ORDER BY `id` asc limit 1');
        if (isset($nimage[0])) {
            $imageName = secure_html($nimage[0]['name']);
            $paginationParams = [
                'pluginName' => 'gallery',
                'pluginHandler' => 'image',
                'params' => ['galleryID' => $gallery['id'], 'gallery' => $gallery['name'], 'name' => $imageName],
                'xparams' => [],
                'paginator' => ['page', 0, false],
            ];
            $nextlink = str_replace('%page%', $this->lang['gallery:nextlink'], str_replace('%link%', generatePageLink($paginationParams, 0), $nav['nextlink']));
        } else {
            $nextlink = '';
        }

        // Комментарии
        $commentsOutput = '';
        if (getPluginStatusActive('comments')) {
            $commentsFile = root . 'plugins/comments/inc/comments.show.php';
            if (file_exists($commentsFile)) {
                include_once $commentsFile;

                if (function_exists('comments_show') && function_exists('comments_showform')) {
                    // Prepare params for call - module 'images' for gallery images
                    $callingCommentsParams = ['outprint' => true, 'total' => $row['com'], 'module' => 'images'];

                    // Capture output with buffering
                    ob_start();

                    // Show comments list
                    echo comments_show($row['id'], 0, 0, $callingCommentsParams);

                    echo "\n";

                    // Show comment form (check if user can add comments)
                    if (!pluginGetVariable('comments', 'regonly') || is_array($userROW)) {
                        // Call comments_showform with proper parameters
                        $formParams = ['module' => 'images', 'outprint' => true];
                        echo comments_showform($row['id'], $formParams);
                    } else {
                        echo '<div class="alert alert-info">Для добавления комментариев необходимо авторизоваться.</div>';
                    }

                    $commentsOutput = ob_get_clean();

                    // Log for debugging
                    if (function_exists('Plugins\logger')) {
                        \Plugins\logger('Comments output for image #' . $row['id'] . ', length: ' . strlen($commentsOutput), 'info', 'gallery.log');
                    }
                } else {
                    $commentsOutput = '<!-- Comments functions not found -->';
                }
            } else {
                $commentsOutput = '<!-- Comments file not found: ' . $commentsFile . ' -->';
            }
        } else {
            $commentsOutput = '<!-- Comments plugin not active -->';
        }

        $tPath = locatePluginTemplates(['page_image'], 'gallery', 0, $gallery['skin']);

        // Register skin assets
        if (function_exists('gallery_register_skin_assets')) {
            gallery_register_skin_assets($gallery['skin'], 'page_image');
        }

        $tVars = [
            'plugin_title' => $this->pluginTitle,
            'url_tpl' => $tPath['url:page_image'],
            'url_main' => generatePluginLink('gallery', null),
            'gallery' => [
                'url' => generatePluginLink('gallery', 'gallery', ['id' => $gallery['id'], 'name' => $gallery['name']]),
                'title' => $gallery['title'],
                'description' => $gallery['description'],
                'keywords' => $gallery['keywords'],
            ],
            'image' => [
                'name' => $row['name'],
                'src' => images_url . '/' . $gallery['name'] . '/' . $row['name'],
                'src_thumb' => images_url . '/' . $gallery['name'] . '/thumb/' . $row['name'],
                'description' => $row['description'],
                'date' => LangDate('j.m.Y - H:i', $row['date']),
                'dateStamp' => $row['date'],
                'com' => $row['com'],
                'views' => $row['views'],
                'width' => $row['width'],
                'height' => $row['height'],
                'size' => is_readable($fname = images_dir . '/' . $gallery['name'] . '/' . $row['name']) ? formatBytes(filesize($fname)) : '-',
            ],
            'nextlink' => $nextlink,
            'gallerylink' => str_replace('%page%', $gallery['title'], str_replace('%link%', generatePluginLink('gallery', 'gallery', ['name' => $gallery['name']]), $nav['link_page'])),
            'prevlink' => $prevlink,
            'plugin_comments' => $commentsOutput,
        ];

        $template['vars']['mainblock'] .= $output = $twig->render($tPath['page_image'] . 'page_image.tpl', $tVars);
        if ($this->params['cache']) {
            cacheStoreFile($cacheFileName, $output, 'gallery');
        }
    }
}
