<?php

namespace {
/*
 * This file is part of Contao.
 *
 * (c) Leo Feyer
 *
 * @license LGPL-3.0-or-later
 */
\Contao\System::loadLanguageFile('tl_image_size');
$GLOBALS['TL_DCA']['tl_image_size_item'] = array(
    // Config
    'config' => array('dataContainer' => \Contao\DC_Table::class, 'ptable' => 'tl_image_size', 'enableVersioning' => \true, 'onload_callback' => array(array('tl_image_size_item', 'checkPermission')), 'sql' => array('keys' => array('id' => 'primary', 'pid' => 'index'))),
    // List
    'list' => array('sorting' => array('mode' => 4, 'fields' => array('sorting'), 'panelLayout' => 'limit', 'headerFields' => array('name', 'tstamp', 'width', 'height', 'resizeMode', 'zoom'), 'child_record_callback' => array('tl_image_size_item', 'listImageSizeItem')), 'global_operations' => array('all' => array('href' => 'act=select', 'class' => 'header_edit_all', 'attributes' => 'onclick="Backend.getScrollOffset()" accesskey="e"')), 'operations' => array('edit' => array('href' => 'act=edit', 'icon' => 'edit.svg'), 'copy' => array('href' => 'act=paste&amp;mode=copy', 'icon' => 'copy.svg', 'attributes' => 'onclick="Backend.getScrollOffset()"'), 'cut' => array('href' => 'act=paste&amp;mode=cut', 'icon' => 'cut.svg', 'attributes' => 'onclick="Backend.getScrollOffset()"'), 'delete' => array('href' => 'act=delete', 'icon' => 'delete.svg', 'attributes' => 'onclick="if(!confirm(\'' . $GLOBALS['TL_LANG']['MSC']['deleteConfirm'] . '\'))return false;Backend.getScrollOffset()"'), 'toggle' => array('icon' => 'visible.svg', 'attributes' => 'onclick="Backend.getScrollOffset();return AjaxRequest.toggleVisibility(this,%s)"', 'button_callback' => array('tl_image_size_item', 'toggleIcon')), 'show' => array('href' => 'act=show', 'icon' => 'show.svg'))),
    // Palettes
    'palettes' => array('default' => '{title_legend},media,width,height,resizeMode,zoom;{source_legend},densities,sizes;{visibility_legend:hide},invisible'),
    // Fields
    'fields' => array('id' => array('sql' => "int(10) unsigned NOT NULL auto_increment"), 'pid' => array('foreignKey' => 'tl_image_size.name', 'sql' => "int(10) unsigned NOT NULL default 0", 'relation' => array('type' => 'belongsTo', 'load' => 'lazy')), 'sorting' => array('sql' => "int(10) unsigned NOT NULL default 0"), 'tstamp' => array('sql' => "int(10) unsigned NOT NULL default 0"), 'media' => array('inputType' => 'text', 'exclude' => \true, 'eval' => array('maxlength' => 255, 'tl_class' => 'long', 'decodeEntities' => \true), 'sql' => "varchar(255) NOT NULL default ''"), 'densities' => array('label' => &$GLOBALS['TL_LANG']['tl_image_size']['densities'], 'inputType' => 'text', 'explanation' => 'imageSizeDensities', 'exclude' => \true, 'eval' => array('helpwizard' => \true, 'maxlength' => 255, 'tl_class' => 'w50'), 'sql' => "varchar(255) NOT NULL default ''"), 'sizes' => array('label' => &$GLOBALS['TL_LANG']['tl_image_size']['sizes'], 'inputType' => 'text', 'explanation' => 'imageSizeDensities', 'exclude' => \true, 'eval' => array('helpwizard' => \true, 'maxlength' => 255, 'tl_class' => 'clr', 'decodeEntities' => \true), 'sql' => "varchar(255) NOT NULL default ''"), 'width' => array('label' => &$GLOBALS['TL_LANG']['tl_image_size']['width'], 'inputType' => 'text', 'exclude' => \true, 'eval' => array('rgxp' => 'digit', 'nospace' => \true, 'tl_class' => 'clr w50'), 'sql' => "int(10) NULL"), 'height' => array('label' => &$GLOBALS['TL_LANG']['tl_image_size']['height'], 'inputType' => 'text', 'exclude' => \true, 'eval' => array('rgxp' => 'digit', 'nospace' => \true, 'tl_class' => 'w50'), 'sql' => "int(10) NULL"), 'resizeMode' => array('label' => &$GLOBALS['TL_LANG']['tl_image_size']['resizeMode'], 'inputType' => 'select', 'options' => array('proportional', 'box', 'crop'), 'reference' => &$GLOBALS['TL_LANG']['tl_image_size'], 'exclude' => \true, 'eval' => array('helpwizard' => \true, 'tl_class' => 'clr w50'), 'sql' => "varchar(255) NOT NULL default ''"), 'zoom' => array('label' => &$GLOBALS['TL_LANG']['tl_image_size']['zoom'], 'inputType' => 'text', 'exclude' => \true, 'eval' => array('rgxp' => 'prcnt', 'nospace' => \true, 'tl_class' => 'w50'), 'sql' => "int(10) NULL"), 'invisible' => array('exclude' => \true, 'filter' => \true, 'inputType' => 'checkbox', 'sql' => "char(1) NOT NULL default ''")),
);
/**
 * Provide miscellaneous methods that are used by the data configuration array.
 *
 * @author Leo Feyer <https://github.com/leofeyer>
 */
class tl_image_size_item extends \Contao\Backend
{
    /**
     * Import the back end user object
     */
    public function __construct()
    {
        parent::__construct();
        $this->import('Contao\\BackendUser', 'User');
    }
    /**
     * Check permissions to edit the table
     *
     * @throws Contao\CoreBundle\Exception\AccessDeniedException
     */
    public function checkPermission()
    {
        if ($this->User->isAdmin) {
            return;
        }
        if (!$this->User->hasAccess('image_sizes', 'themes')) {
            throw new \Contao\CoreBundle\Exception\AccessDeniedException('Not enough permissions to access the image sizes module.');
        }
    }
    /**
     * List an image size item
     *
     * @param array $row
     *
     * @return string
     */
    public function listImageSizeItem($row)
    {
        $html = '<div class="tl_content_left">';
        $html .= $row['media'];
        if ($row['width'] || $row['height']) {
            $html .= ' <span style="padding-left:3px">' . $row['width'] . 'x' . $row['height'] . '</span>';
        }
        if ($row['zoom']) {
            $html .= ' <span style="color:#999;padding-left:3px">(' . $row['zoom'] . '%)</span>';
        }
        $html .= "</div>\n";
        return $html;
    }
    /**
     * Return the "toggle visibility" button
     *
     * @param array  $row
     * @param string $href
     * @param string $label
     * @param string $title
     * @param string $icon
     * @param string $attributes
     *
     * @return string
     */
    public function toggleIcon($row, $href, $label, $title, $icon, $attributes)
    {
        if (\Contao\Input::get('tid')) {
            $this->toggleVisibility(\Contao\Input::get('tid'), \Contao\Input::get('state') == 1, \func_num_args() <= 12 ? \null : \func_get_arg(12));
            $this->redirect($this->getReferer());
        }
        // Check permissions AFTER checking the tid, so hacking attempts are logged
        if (!$this->User->hasAccess('tl_image_size_item::invisible', 'alexf')) {
            return '';
        }
        $href .= '&amp;tid=' . $row['id'] . '&amp;state=' . $row['invisible'];
        if ($row['invisible']) {
            $icon = 'invisible.svg';
        }
        return '<a href="' . $this->addToUrl($href) . '" title="' . \Contao\StringUtil::specialchars($title) . '"' . $attributes . '>' . \Contao\Image::getHtml($icon, $label, 'data-state="' . ($row['invisible'] ? 0 : 1) . '"') . '</a> ';
    }
    /**
     * Toggle the visibility of a format definition
     *
     * @param integer              $intId
     * @param boolean              $blnVisible
     * @param Contao\DataContainer $dc
     */
    public function toggleVisibility($intId, $blnVisible, \Contao\DataContainer $dc = \null)
    {
        // Set the ID and action
        \Contao\Input::setGet('id', $intId);
        \Contao\Input::setGet('act', 'toggle');
        if ($dc) {
            $dc->id = $intId;
            // see #8043
        }
        // Trigger the onload_callback
        if (\is_array($GLOBALS['TL_DCA']['tl_image_size_item']['config']['onload_callback'])) {
            foreach ($GLOBALS['TL_DCA']['tl_image_size_item']['config']['onload_callback'] as $callback) {
                if (\is_array($callback)) {
                    $this->import($callback[0]);
                    $this->{$callback[0]}->{$callback[1]}($dc);
                } elseif (\is_callable($callback)) {
                    $callback($dc);
                }
            }
        }
        // Check the field access
        if (!$this->User->hasAccess('tl_image_size_item::invisible', 'alexf')) {
            throw new \Contao\CoreBundle\Exception\AccessDeniedException('Not enough permissions to publish/unpublish image size item ID ' . $intId . '.');
        }
        $objRow = $this->Database->prepare("SELECT * FROM tl_image_size_item WHERE id=?")->limit(1)->execute($intId);
        if ($objRow->numRows < 1) {
            throw new \Contao\CoreBundle\Exception\AccessDeniedException('Invalid image size item ID ' . $intId . '.');
        }
        // Set the current record
        if ($dc) {
            $dc->activeRecord = $objRow;
        }
        $objVersions = new \Contao\Versions('tl_image_size_item', $intId);
        $objVersions->initialize();
        // Reverse the logic (image sizes have invisible=1)
        $blnVisible = !$blnVisible;
        // Trigger the save_callback
        if (\is_array($GLOBALS['TL_DCA']['tl_image_size_item']['fields']['invisible']['save_callback'])) {
            foreach ($GLOBALS['TL_DCA']['tl_image_size_item']['fields']['invisible']['save_callback'] as $callback) {
                if (\is_array($callback)) {
                    $this->import($callback[0]);
                    $blnVisible = $this->{$callback[0]}->{$callback[1]}($blnVisible, $dc);
                } elseif (\is_callable($callback)) {
                    $blnVisible = $callback($blnVisible, $dc);
                }
            }
        }
        $time = \time();
        // Update the database
        $this->Database->prepare("UPDATE tl_image_size_item SET tstamp={$time}, invisible='" . ($blnVisible ? '1' : '') . "' WHERE id=?")->execute($intId);
        if ($dc) {
            $dc->activeRecord->tstamp = $time;
            $dc->activeRecord->invisible = $blnVisible ? '1' : '';
        }
        // Trigger the onsubmit_callback
        if (\is_array($GLOBALS['TL_DCA']['tl_image_size_item']['config']['onsubmit_callback'])) {
            foreach ($GLOBALS['TL_DCA']['tl_image_size_item']['config']['onsubmit_callback'] as $callback) {
                if (\is_array($callback)) {
                    $this->import($callback[0]);
                    $this->{$callback[0]}->{$callback[1]}($dc);
                } elseif (\is_callable($callback)) {
                    $callback($dc);
                }
            }
        }
        $objVersions->create();
        if ($dc) {
            $dc->invalidateCacheTags();
        }
    }
}
}
