<?php

namespace {
/*
 * This file is part of Contao.
 *
 * (c) Leo Feyer
 *
 * @license LGPL-3.0-or-later
 */
$this->loadDataContainer('tl_page');
$GLOBALS['TL_DCA']['tl_article'] = array(
    // Config
    'config' => array('dataContainer' => \Contao\DC_Table::class, 'ptable' => 'tl_page', 'ctable' => array('tl_content'), 'switchToEdit' => \true, 'enableVersioning' => \true, 'markAsCopy' => 'title', 'onload_callback' => array(array('tl_article', 'checkPermission'), array('tl_article', 'addCustomLayoutSectionReferences'), array('tl_page', 'addBreadcrumb')), 'sql' => array('keys' => array('id' => 'primary', 'alias' => 'index', 'pid,start,stop,published,sorting' => 'index'))),
    // List
    'list' => array('sorting' => array('mode' => 6, 'paste_button_callback' => array('tl_article', 'pasteArticle'), 'panelLayout' => 'filter;search'), 'label' => array('fields' => array('title', 'inColumn'), 'format' => '%s <span style="color:#999;padding-left:3px">[%s]</span>', 'label_callback' => array('tl_article', 'addIcon')), 'global_operations' => array('toggleNodes' => array('href' => '&amp;ptg=all', 'class' => 'header_toggle', 'showOnSelect' => \true), 'all' => array('href' => 'act=select', 'class' => 'header_edit_all', 'attributes' => 'onclick="Backend.getScrollOffset()" accesskey="e"')), 'operations' => array('edit' => array('href' => 'table=tl_content', 'icon' => 'edit.svg', 'button_callback' => array('tl_article', 'editArticle')), 'editheader' => array('href' => 'act=edit', 'icon' => 'header.svg', 'button_callback' => array('tl_article', 'editHeader')), 'copy' => array('href' => 'act=paste&amp;mode=copy', 'icon' => 'copy.svg', 'attributes' => 'onclick="Backend.getScrollOffset()"', 'button_callback' => array('tl_article', 'copyArticle')), 'cut' => array('href' => 'act=paste&amp;mode=cut', 'icon' => 'cut.svg', 'attributes' => 'onclick="Backend.getScrollOffset()"', 'button_callback' => array('tl_article', 'cutArticle')), 'delete' => array('href' => 'act=delete', 'icon' => 'delete.svg', 'attributes' => 'onclick="if(!confirm(\'' . $GLOBALS['TL_LANG']['MSC']['deleteConfirm'] . '\'))return false;Backend.getScrollOffset()"', 'button_callback' => array('tl_article', 'deleteArticle')), 'toggle' => array('icon' => 'visible.svg', 'attributes' => 'onclick="Backend.getScrollOffset();return AjaxRequest.toggleVisibility(this,%s)"', 'button_callback' => array('tl_article', 'toggleIcon'), 'showInHeader' => \true), 'show' => array('href' => 'act=show', 'icon' => 'show.svg'))),
    // Select
    'select' => array('buttons_callback' => array(array('tl_article', 'addAliasButton'))),
    // Palettes
    'palettes' => array('__selector__' => array('protected'), 'default' => '{title_legend},title,alias,author;{layout_legend},inColumn,keywords;{teaser_legend:hide},teaserCssID,showTeaser,teaser;{syndication_legend},printable;{template_legend:hide},customTpl;{protected_legend:hide},protected;{expert_legend:hide},guests,cssID;{publish_legend},published,start,stop'),
    // Subpalettes
    'subpalettes' => array('protected' => 'groups'),
    // Fields
    'fields' => array('id' => array('label' => array('ID'), 'search' => \true, 'sql' => "int(10) unsigned NOT NULL auto_increment"), 'pid' => array('foreignKey' => 'tl_page.title', '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"), 'title' => array('exclude' => \true, 'inputType' => 'text', 'search' => \true, 'eval' => array('mandatory' => \true, 'decodeEntities' => \true, 'maxlength' => 255, 'tl_class' => 'w50'), 'sql' => "varchar(255) NOT NULL default ''"), 'alias' => array('exclude' => \true, 'inputType' => 'text', 'search' => \true, 'eval' => array('rgxp' => 'alias', 'doNotCopy' => \true, 'maxlength' => 255, 'tl_class' => 'w50 clr'), 'save_callback' => array(array('tl_article', 'generateAlias')), 'sql' => "varchar(255) BINARY NOT NULL default ''"), 'author' => array('default' => \Contao\BackendUser::getInstance()->id, 'exclude' => \true, 'search' => \true, 'filter' => \true, 'inputType' => 'select', 'foreignKey' => 'tl_user.name', 'eval' => array('doNotCopy' => \true, 'mandatory' => \true, 'chosen' => \true, 'includeBlankOption' => \true, 'tl_class' => 'w50'), 'sql' => "int(10) unsigned NOT NULL default 0", 'relation' => array('type' => 'hasOne', 'load' => 'lazy')), 'inColumn' => array('exclude' => \true, 'filter' => \true, 'inputType' => 'select', 'options_callback' => array('tl_article', 'getActiveLayoutSections'), 'eval' => array('mandatory' => \true, 'tl_class' => 'w50'), 'reference' => &$GLOBALS['TL_LANG']['COLS'], 'sql' => "varchar(32) NOT NULL default 'main'"), 'keywords' => array('exclude' => \true, 'inputType' => 'textarea', 'search' => \true, 'eval' => array('style' => 'height:60px', 'decodeEntities' => \true, 'tl_class' => 'clr'), 'sql' => "text NULL"), 'showTeaser' => array('exclude' => \true, 'inputType' => 'checkbox', 'eval' => array('tl_class' => 'w50 m12'), 'sql' => "char(1) NOT NULL default ''"), 'teaserCssID' => array('exclude' => \true, 'inputType' => 'text', 'eval' => array('multiple' => \true, 'size' => 2, 'tl_class' => 'w50'), 'sql' => "varchar(255) NOT NULL default ''"), 'teaser' => array('exclude' => \true, 'inputType' => 'textarea', 'search' => \true, 'eval' => array('rte' => 'tinyMCE', 'tl_class' => 'clr'), 'sql' => "text NULL"), 'printable' => array('exclude' => \true, 'inputType' => 'checkbox', 'options' => array('print', 'facebook', 'twitter'), 'eval' => array('multiple' => \true), 'reference' => &$GLOBALS['TL_LANG']['tl_article'], 'sql' => "varchar(255) NOT NULL default ''"), 'customTpl' => array('exclude' => \true, 'inputType' => 'select', 'options_callback' => static function () {
        return \Contao\Controller::getTemplateGroup('mod_article_', array(), 'mod_article');
    }, 'eval' => array('chosen' => \true, 'tl_class' => 'w50'), 'sql' => "varchar(64) NOT NULL default ''"), 'protected' => array('exclude' => \true, 'filter' => \true, 'inputType' => 'checkbox', 'eval' => array('submitOnChange' => \true), 'sql' => "char(1) NOT NULL default ''"), 'groups' => array('exclude' => \true, 'filter' => \true, 'inputType' => 'checkbox', 'foreignKey' => 'tl_member_group.name', 'eval' => array('mandatory' => \true, 'multiple' => \true), 'sql' => "blob NULL", 'relation' => array('type' => 'hasMany', 'load' => 'lazy')), 'guests' => array('exclude' => \true, 'filter' => \true, 'inputType' => 'checkbox', 'eval' => array('tl_class' => 'w50'), 'sql' => "char(1) NOT NULL default ''"), 'cssID' => array('exclude' => \true, 'inputType' => 'text', 'eval' => array('multiple' => \true, 'size' => 2, 'tl_class' => 'w50 clr'), 'sql' => "varchar(255) NOT NULL default ''"), 'published' => array('exclude' => \true, 'filter' => \true, 'inputType' => 'checkbox', 'eval' => array('doNotCopy' => \true), 'sql' => "char(1) NOT NULL default ''"), 'start' => array('exclude' => \true, 'inputType' => 'text', 'eval' => array('rgxp' => 'datim', 'datepicker' => \true, 'tl_class' => 'w50 wizard'), 'sql' => "varchar(10) NOT NULL default ''"), 'stop' => array('exclude' => \true, 'inputType' => 'text', 'eval' => array('rgxp' => 'datim', 'datepicker' => \true, 'tl_class' => 'w50 wizard'), 'sql' => "varchar(10) NOT NULL default ''")),
);
/**
 * Provide miscellaneous methods that are used by the data configuration array.
 *
 * @author Leo Feyer <https://github.com/leofeyer>
 */
class tl_article extends \Contao\Backend
{
    /**
     * Import the back end user object
     */
    public function __construct()
    {
        parent::__construct();
        $this->import('Contao\\BackendUser', 'User');
    }
    /**
     * Check permissions to edit table tl_page
     *
     * @throws Contao\CoreBundle\Exception\AccessDeniedException
     */
    public function checkPermission()
    {
        if ($this->User->isAdmin) {
            return;
        }
        /** @var Symfony\Component\HttpFoundation\Session\SessionInterface $objSession */
        $objSession = \Contao\System::getContainer()->get('session');
        $session = $objSession->all();
        // Set the default page user and group
        $GLOBALS['TL_DCA']['tl_page']['fields']['cuser']['default'] = (int) \Contao\Config::get('defaultUser') ?: $this->User->id;
        $GLOBALS['TL_DCA']['tl_page']['fields']['cgroup']['default'] = (int) \Contao\Config::get('defaultGroup') ?: (int) $this->User->groups[0];
        // Restrict the page tree
        if (empty($this->User->pagemounts) || !\is_array($this->User->pagemounts)) {
            $root = array(0);
        } else {
            $root = $this->User->pagemounts;
        }
        $GLOBALS['TL_DCA']['tl_page']['list']['sorting']['root'] = $root;
        // Set allowed page IDs (edit multiple)
        if (\is_array($session['CURRENT']['IDS'])) {
            $edit_all = array();
            $delete_all = array();
            foreach ($session['CURRENT']['IDS'] as $id) {
                $objArticle = $this->Database->prepare("SELECT p.pid, p.includeChmod, p.chmod, p.cuser, p.cgroup FROM tl_article a, tl_page p WHERE a.id=? AND a.pid=p.id")->limit(1)->execute($id);
                if ($objArticle->numRows < 1) {
                    continue;
                }
                $row = $objArticle->row();
                if ($this->User->isAllowed(\Contao\BackendUser::CAN_EDIT_ARTICLES, $row)) {
                    $edit_all[] = $id;
                }
                if ($this->User->isAllowed(\Contao\BackendUser::CAN_DELETE_ARTICLES, $row)) {
                    $delete_all[] = $id;
                }
            }
            $session['CURRENT']['IDS'] = \Contao\Input::get('act') == 'deleteAll' ? $delete_all : $edit_all;
        }
        // Set allowed clipboard IDs
        if (isset($session['CLIPBOARD']['tl_article']) && \is_array($session['CLIPBOARD']['tl_article']['id'])) {
            $clipboard = array();
            foreach ($session['CLIPBOARD']['tl_article']['id'] as $id) {
                $objArticle = $this->Database->prepare("SELECT p.pid, p.includeChmod, p.chmod, p.cuser, p.cgroup FROM tl_article a, tl_page p WHERE a.id=? AND a.pid=p.id")->limit(1)->execute($id);
                if ($objArticle->numRows < 1) {
                    continue;
                }
                if ($this->User->isAllowed(\Contao\BackendUser::CAN_EDIT_ARTICLE_HIERARCHY, $objArticle->row())) {
                    $clipboard[] = $id;
                }
            }
            $session['CLIPBOARD']['tl_article']['id'] = $clipboard;
        }
        $permission = 0;
        // Overwrite the session
        $objSession->replace($session);
        // Check current action
        if (\Contao\Input::get('act') && \Contao\Input::get('act') != 'paste') {
            // Set ID of the article's page
            $objPage = $this->Database->prepare("SELECT pid FROM tl_article WHERE id=?")->limit(1)->execute(\Contao\Input::get('id'));
            $ids = $objPage->numRows ? array($objPage->pid) : array();
            // Set permission
            switch (\Contao\Input::get('act')) {
                case 'edit':
                case 'toggle':
                    $permission = \Contao\BackendUser::CAN_EDIT_ARTICLES;
                    break;
                case 'move':
                    $permission = \Contao\BackendUser::CAN_EDIT_ARTICLE_HIERARCHY;
                    $ids[] = \Contao\Input::get('sid');
                    break;
                // Do not insert articles into a website root page
                case 'create':
                case 'copy':
                case 'copyAll':
                case 'cut':
                case 'cutAll':
                    $permission = \Contao\BackendUser::CAN_EDIT_ARTICLE_HIERARCHY;
                    // Insert into a page
                    if (\Contao\Input::get('mode') == 2) {
                        $objParent = $this->Database->prepare("SELECT id, type FROM tl_page WHERE id=?")->limit(1)->execute(\Contao\Input::get('pid'));
                        $ids[] = \Contao\Input::get('pid');
                    } else {
                        $objParent = $this->Database->prepare("SELECT id, type FROM tl_page WHERE id=(SELECT pid FROM tl_article WHERE id=?)")->limit(1)->execute(\Contao\Input::get('pid'));
                        $ids[] = $objParent->id;
                    }
                    if ($objParent->numRows && $objParent->type == 'root') {
                        throw new \Contao\CoreBundle\Exception\AccessDeniedException('Attempt to insert an article into website root page ID ' . \Contao\Input::get('pid') . '.');
                    }
                    break;
                case 'delete':
                    $permission = \Contao\BackendUser::CAN_DELETE_ARTICLES;
                    break;
            }
            // Check user permissions
            $pagemounts = array();
            // Get all allowed pages for the current user
            foreach ($this->User->pagemounts as $root) {
                $pagemounts[] = array($root);
                $pagemounts[] = $this->Database->getChildRecords($root, 'tl_page');
            }
            if (!empty($pagemounts)) {
                $pagemounts = \array_merge(...$pagemounts);
            }
            $pagemounts = \array_unique($pagemounts);
            // Check each page
            foreach ($ids as $id) {
                if (!\in_array($id, $pagemounts)) {
                    throw new \Contao\CoreBundle\Exception\AccessDeniedException('Page ID ' . $id . ' is not mounted.');
                }
                if (\Contao\Input::get('act') == 'show') {
                    continue;
                }
                $objPage = \Contao\PageModel::findById($id);
                // Check whether the current user has permission for the current page
                if ($objPage !== \null && !$this->User->isAllowed($permission, $objPage->row())) {
                    throw new \Contao\CoreBundle\Exception\AccessDeniedException('Not enough permissions to ' . \Contao\Input::get('act') . ' ' . (\Contao\Input::get('id') ? 'article ID ' . \Contao\Input::get('id') : ' articles') . ' on page ID ' . $id . ' or to paste it/them into page ID ' . $id . '.');
                }
            }
        }
    }
    /**
     * Add an image to each page in the tree
     *
     * @param array  $row
     * @param string $label
     *
     * @return string
     */
    public function addIcon($row, $label)
    {
        $image = 'articles';
        $unpublished = $row['start'] && $row['start'] > \time() || $row['stop'] && $row['stop'] <= \time();
        if ($unpublished || !$row['published']) {
            $image .= '_';
        }
        return '<a href="contao/preview.php?page=' . $row['pid'] . '&amp;article=' . ($row['alias'] ?: $row['id']) . '" title="' . \Contao\StringUtil::specialchars($GLOBALS['TL_LANG']['MSC']['view']) . '" target="_blank">' . \Contao\Image::getHtml($image . '.svg', '', 'data-icon="' . ($unpublished ? $image : \rtrim($image, '_')) . '.svg" data-icon-disabled="' . \rtrim($image, '_') . '_.svg"') . '</a> ' . $label;
    }
    /**
     * Auto-generate an article alias if it has not been set yet
     *
     * @param mixed                $varValue
     * @param Contao\DataContainer $dc
     *
     * @return string
     *
     * @throws Exception
     */
    public function generateAlias($varValue, \Contao\DataContainer $dc)
    {
        $aliasExists = function (string $alias) use($dc) : bool {
            if (\in_array($alias, array('top', 'wrapper', 'header', 'container', 'main', 'left', 'right', 'footer'), \true)) {
                return \true;
            }
            return $this->Database->prepare("SELECT id FROM tl_article WHERE alias=? AND id!=?")->execute($alias, $dc->id)->numRows > 0;
        };
        // Generate an alias if there is none
        if (!$varValue) {
            $varValue = \Contao\System::getContainer()->get('contao.slug')->generate($dc->activeRecord->title, $dc->activeRecord->pid, $aliasExists);
        } elseif (\preg_match('/^[1-9]\\d*$/', $varValue)) {
            throw new \Exception(\sprintf($GLOBALS['TL_LANG']['ERR']['aliasNumeric'], $varValue));
        } elseif ($aliasExists($varValue)) {
            throw new \Exception(\sprintf($GLOBALS['TL_LANG']['ERR']['aliasExists'], $varValue));
        }
        return $varValue;
    }
    /**
     * Return all active layout sections as array
     *
     * @param Contao\DataContainer $dc
     *
     * @return array
     */
    public function getActiveLayoutSections(\Contao\DataContainer $dc)
    {
        // Show only active sections
        if ($dc->activeRecord->pid) {
            $arrSections = array();
            $objPage = \Contao\PageModel::findWithDetails($dc->activeRecord->pid);
            // Get the layout sections
            if ($objPage->layout) {
                $objLayout = \Contao\LayoutModel::findByPk($objPage->layout);
                if ($objLayout === \null) {
                    return array();
                }
                $arrModules = \Contao\StringUtil::deserialize($objLayout->modules);
                if (empty($arrModules) || !\is_array($arrModules)) {
                    return array();
                }
                // Find all sections with an article module (see #6094)
                foreach ($arrModules as $arrModule) {
                    if ($arrModule['mod'] == 0 && $arrModule['enable']) {
                        $arrSections[] = $arrModule['col'];
                    }
                }
            }
        } else {
            $arrSections = array('header', 'left', 'right', 'main', 'footer');
            $objLayout = $this->Database->query("SELECT sections FROM tl_layout WHERE sections!=''");
            while ($objLayout->next()) {
                $arrCustom = \Contao\StringUtil::deserialize($objLayout->sections);
                // Add the custom layout sections
                if (!empty($arrCustom) && \is_array($arrCustom)) {
                    foreach ($arrCustom as $v) {
                        if (!empty($v['id'])) {
                            $arrSections[] = $v['id'];
                        }
                    }
                }
            }
        }
        return \Contao\Backend::convertLayoutSectionIdsToAssociativeArray($arrSections);
    }
    /**
     * Return the edit article button
     *
     * @param array  $row
     * @param string $href
     * @param string $label
     * @param string $title
     * @param string $icon
     * @param string $attributes
     *
     * @return string
     */
    public function editArticle($row, $href, $label, $title, $icon, $attributes)
    {
        $objPage = \Contao\PageModel::findById($row['pid']);
        return $this->User->isAllowed(\Contao\BackendUser::CAN_EDIT_ARTICLES, $objPage->row()) ? '<a href="' . $this->addToUrl($href . '&amp;id=' . $row['id']) . '" title="' . \Contao\StringUtil::specialchars($title) . '"' . $attributes . '>' . \Contao\Image::getHtml($icon, $label) . '</a> ' : \Contao\Image::getHtml(\preg_replace('/\\.svg$/i', '_.svg', $icon)) . ' ';
    }
    /**
     * Return the edit header button
     *
     * @param array  $row
     * @param string $href
     * @param string $label
     * @param string $title
     * @param string $icon
     * @param string $attributes
     *
     * @return string
     */
    public function editHeader($row, $href, $label, $title, $icon, $attributes)
    {
        if (!$this->User->canEditFieldsOf('tl_article')) {
            return \Contao\Image::getHtml(\preg_replace('/\\.svg$/i', '_.svg', $icon)) . ' ';
        }
        $objPage = \Contao\PageModel::findById($row['pid']);
        return $this->User->isAllowed(\Contao\BackendUser::CAN_EDIT_ARTICLES, $objPage->row()) ? '<a href="' . $this->addToUrl($href . '&amp;id=' . $row['id']) . '" title="' . \Contao\StringUtil::specialchars($title) . '"' . $attributes . '>' . \Contao\Image::getHtml($icon, $label) . '</a> ' : \Contao\Image::getHtml(\preg_replace('/\\.svg$/i', '_.svg', $icon)) . ' ';
    }
    /**
     * Return the copy article button
     *
     * @param array  $row
     * @param string $href
     * @param string $label
     * @param string $title
     * @param string $icon
     * @param string $attributes
     * @param string $table
     *
     * @return string
     */
    public function copyArticle($row, $href, $label, $title, $icon, $attributes, $table)
    {
        if ($GLOBALS['TL_DCA'][$table]['config']['closed']) {
            return '';
        }
        $objPage = \Contao\PageModel::findById($row['pid']);
        return $this->User->isAllowed(\Contao\BackendUser::CAN_EDIT_ARTICLE_HIERARCHY, $objPage->row()) ? '<a href="' . $this->addToUrl($href . '&amp;id=' . $row['id']) . '" title="' . \Contao\StringUtil::specialchars($title) . '"' . $attributes . '>' . \Contao\Image::getHtml($icon, $label) . '</a> ' : \Contao\Image::getHtml(\preg_replace('/\\.svg$/i', '_.svg', $icon)) . ' ';
    }
    /**
     * Return the cut article button
     *
     * @param array  $row
     * @param string $href
     * @param string $label
     * @param string $title
     * @param string $icon
     * @param string $attributes
     *
     * @return string
     */
    public function cutArticle($row, $href, $label, $title, $icon, $attributes)
    {
        $objPage = \Contao\PageModel::findById($row['pid']);
        return $this->User->isAllowed(\Contao\BackendUser::CAN_EDIT_ARTICLE_HIERARCHY, $objPage->row()) ? '<a href="' . $this->addToUrl($href . '&amp;id=' . $row['id']) . '" title="' . \Contao\StringUtil::specialchars($title) . '"' . $attributes . '>' . \Contao\Image::getHtml($icon, $label) . '</a> ' : \Contao\Image::getHtml(\preg_replace('/\\.svg$/i', '_.svg', $icon)) . ' ';
    }
    /**
     * Return the paste article button
     *
     * @param Contao\DataContainer $dc
     * @param array                $row
     * @param string               $table
     * @param boolean              $cr
     * @param array                $arrClipboard
     *
     * @return string
     */
    public function pasteArticle(\Contao\DataContainer $dc, $row, $table, $cr, $arrClipboard = \null)
    {
        $imagePasteAfter = \Contao\Image::getHtml('pasteafter.svg', \sprintf($GLOBALS['TL_LANG'][$dc->table]['pasteafter'][1], $row['id']));
        $imagePasteInto = \Contao\Image::getHtml('pasteinto.svg', \sprintf($GLOBALS['TL_LANG'][$dc->table]['pasteinto'][1], $row['id']));
        if ($table == $GLOBALS['TL_DCA'][$dc->table]['config']['ptable']) {
            return $row['type'] == 'root' || !$this->User->isAllowed(\Contao\BackendUser::CAN_EDIT_ARTICLE_HIERARCHY, $row) || $cr ? \Contao\Image::getHtml('pasteinto_.svg') . ' ' : '<a href="' . $this->addToUrl('act=' . $arrClipboard['mode'] . '&amp;mode=2&amp;pid=' . $row['id'] . (!\is_array($arrClipboard['id']) ? '&amp;id=' . $arrClipboard['id'] : '')) . '" title="' . \Contao\StringUtil::specialchars(\sprintf($GLOBALS['TL_LANG'][$dc->table]['pasteinto'][1], $row['id'])) . '" onclick="Backend.getScrollOffset()">' . $imagePasteInto . '</a> ';
        }
        $objPage = \Contao\PageModel::findById($row['pid']);
        return $arrClipboard['mode'] == 'cut' && $arrClipboard['id'] == $row['id'] || $arrClipboard['mode'] == 'cutAll' && \in_array($row['id'], $arrClipboard['id']) || !$this->User->isAllowed(\Contao\BackendUser::CAN_EDIT_ARTICLE_HIERARCHY, $objPage->row()) || $cr ? \Contao\Image::getHtml('pasteafter_.svg') . ' ' : '<a href="' . $this->addToUrl('act=' . $arrClipboard['mode'] . '&amp;mode=1&amp;pid=' . $row['id'] . (!\is_array($arrClipboard['id']) ? '&amp;id=' . $arrClipboard['id'] : '')) . '" title="' . \Contao\StringUtil::specialchars(\sprintf($GLOBALS['TL_LANG'][$dc->table]['pasteafter'][1], $row['id'])) . '" onclick="Backend.getScrollOffset()">' . $imagePasteAfter . '</a> ';
    }
    /**
     * Return the delete article button
     *
     * @param array  $row
     * @param string $href
     * @param string $label
     * @param string $title
     * @param string $icon
     * @param string $attributes
     *
     * @return string
     */
    public function deleteArticle($row, $href, $label, $title, $icon, $attributes)
    {
        $objPage = \Contao\PageModel::findById($row['pid']);
        return $this->User->isAllowed(\Contao\BackendUser::CAN_DELETE_ARTICLES, $objPage->row()) ? '<a href="' . $this->addToUrl($href . '&amp;id=' . $row['id']) . '" title="' . \Contao\StringUtil::specialchars($title) . '"' . $attributes . '>' . \Contao\Image::getHtml($icon, $label) . '</a> ' : \Contao\Image::getHtml(\preg_replace('/\\.svg$/i', '_.svg', $icon)) . ' ';
    }
    /**
     * Automatically generate the folder URL aliases
     *
     * @param array $arrButtons
     *
     * @return array
     */
    public function addAliasButton($arrButtons)
    {
        if (!$this->User->hasAccess('tl_article::alias', 'alexf')) {
            return $arrButtons;
        }
        // Generate the aliases
        if (isset($_POST['alias']) && \Contao\Input::post('FORM_SUBMIT') == 'tl_select') {
            /** @var Symfony\Component\HttpFoundation\Session\SessionInterface $objSession */
            $objSession = \Contao\System::getContainer()->get('session');
            $session = $objSession->all();
            $ids = $session['CURRENT']['IDS'];
            foreach ($ids as $id) {
                $objArticle = \Contao\ArticleModel::findByPk($id);
                if ($objArticle === \null) {
                    continue;
                }
                $strAlias = \Contao\System::getContainer()->get('contao.slug')->generate($objArticle->title, $objArticle->pid);
                // The alias has not changed
                if ($strAlias == $objArticle->alias) {
                    continue;
                }
                // Initialize the version manager
                $objVersions = new \Contao\Versions('tl_article', $id);
                $objVersions->initialize();
                // Store the new alias
                $this->Database->prepare("UPDATE tl_article SET alias=? WHERE id=?")->execute($strAlias, $id);
                // Create a new version
                $objVersions->create();
            }
            $this->redirect($this->getReferer());
        }
        // Add the button
        $arrButtons['alias'] = '<button type="submit" name="alias" id="alias" class="tl_submit" accesskey="a">' . $GLOBALS['TL_LANG']['MSC']['aliasSelected'] . '</button> ';
        return $arrButtons;
    }
    /**
     * 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_article::published', 'alexf')) {
            return '';
        }
        $href .= '&amp;tid=' . $row['id'] . '&amp;state=' . ($row['published'] ? '' : 1);
        if (!$row['published']) {
            $icon = 'invisible.svg';
        }
        $objPage = \Contao\PageModel::findById($row['pid']);
        if (!$this->User->isAllowed(\Contao\BackendUser::CAN_EDIT_ARTICLES, $objPage->row())) {
            if ($row['published']) {
                $icon = \preg_replace('/\\.svg$/i', '_.svg', $icon);
                // see #8126
            }
            return \Contao\Image::getHtml($icon) . ' ';
        }
        return '<a href="' . $this->addToUrl($href) . '" title="' . \Contao\StringUtil::specialchars($title) . '"' . $attributes . '>' . \Contao\Image::getHtml($icon, $label, 'data-state="' . ($row['published'] ? 1 : 0) . '"') . '</a> ';
    }
    /**
     * Disable/enable a user group
     *
     * @param integer              $intId
     * @param boolean              $blnVisible
     * @param Contao\DataContainer $dc
     *
     * @throws Contao\CoreBundle\Exception\AccessDeniedException
     */
    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_article']['config']['onload_callback'])) {
            foreach ($GLOBALS['TL_DCA']['tl_article']['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_article::published', 'alexf')) {
            throw new \Contao\CoreBundle\Exception\AccessDeniedException('Not enough permissions to publish/unpublish article ID "' . $intId . '".');
        }
        $objRow = $this->Database->prepare("SELECT * FROM tl_article WHERE id=?")->limit(1)->execute($intId);
        if ($objRow->numRows < 1) {
            throw new \Contao\CoreBundle\Exception\AccessDeniedException('Invalid article ID "' . $intId . '".');
        }
        // Set the current record
        if ($dc) {
            $dc->activeRecord = $objRow;
        }
        $objVersions = new \Contao\Versions('tl_article', $intId);
        $objVersions->initialize();
        // Trigger the save_callback
        if (\is_array($GLOBALS['TL_DCA']['tl_article']['fields']['published']['save_callback'])) {
            foreach ($GLOBALS['TL_DCA']['tl_article']['fields']['published']['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_article SET tstamp={$time}, published='" . ($blnVisible ? '1' : '') . "' WHERE id=?")->execute($intId);
        if ($dc) {
            $dc->activeRecord->tstamp = $time;
            $dc->activeRecord->published = $blnVisible ? '1' : '';
        }
        // Trigger the onsubmit_callback
        if (\is_array($GLOBALS['TL_DCA']['tl_article']['config']['onsubmit_callback'])) {
            foreach ($GLOBALS['TL_DCA']['tl_article']['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();
        }
    }
}
}
