<?php /** * ArtLayout for Contao * * Copyright (c) 2016 Benjamin Roth * * @link http://www.esales-media.de * @license commercial */ /** * Callback */ $GLOBALS['TL_DCA']['tl_article']['config']['onload_callback'][] = array('tl_article_eSM_artLayout','registerSubPalettes'); $GLOBALS['TL_DCA']['tl_article']['config']['onload_callback'][] = array('tl_article_eSM_artLayout','setDefaultLayout'); /** * List */ $GLOBALS['TL_DCA']['tl_article']['list']['label']['fields'] = array('title','inColumn','es_type'); $GLOBALS['TL_DCA']['tl_article']['list']['label']['format'] = '%s <span style="color:#b3b3b3;padding-left:3px">[%s]</span> <span style="color:#b3b3b3;padding-left:3px">[%s]</span>'; $GLOBALS['TL_DCA']['tl_article']['list']['label']['label_callback'] = array('tl_article_eSM_artLayout','addLabel'); /** * Extend default palette */ $GLOBALS['TL_DCA']['tl_article']['palettes']['__selector__'][] = 'es_type'; $GLOBALS['TL_DCA']['tl_article']['palettes']['default'] = str_replace('customTpl;', 'customTpl,es_type;', $GLOBALS['TL_DCA']['tl_article']['palettes']['default']); /** * Add fields to tl_article */ $GLOBALS['TL_DCA']['tl_article']['fields']['guests']['eval']['tl_class'] .= ' m12'; $GLOBALS['TL_DCA']['tl_article']['fields']['es_type'] = array ( 'label' => &$GLOBALS['TL_LANG']['tl_article']['es_type'], 'exclude' => true, 'inputType' => 'select', 'options_callback' => array('tl_article_eSM_artLayout','getArticleLayouts'), 'eval' => array('chosen'=>true, 'tl_class'=>'w50', 'includeBlankOption'=>true, 'submitOnChange'=>true, 'alwaysSave'=>true), 'sql' => "int(10) unsigned NOT NULL default '0'", 'relation' => array ( 'type' => 'hasOne', 'load' => 'lazy', 'table' => 'tl_article_layouts' ) ); $GLOBALS['TL_DCA']['tl_article']['fields']['es_maxWidth'] = array ( 'label' => &$GLOBALS['TL_LANG']['tl_article']['es_maxWidth'], 'exclude' => true, 'inputType' => 'checkbox', 'eval' => array('tl_class'=>'w50 m12'), 'sql' => "char(1) NOT NULL default '1'" ); $GLOBALS['TL_DCA']['tl_article']['fields']['es_backgroundSRC'] = array ( 'label' => &$GLOBALS['TL_LANG']['tl_article']['es_backgroundSRC'], 'exclude' => true, 'inputType' => 'fileTree', 'eval' => array('filesOnly'=>true, 'fieldType'=>'radio', 'mandatory'=>true, 'tl_class'=>'clr', 'extensions' => \Config::get('validImageTypes')), 'sql' => "binary(16) NULL" ); $GLOBALS['TL_DCA']['tl_article']['fields']['es_size'] = array ( 'label' => &$GLOBALS['TL_LANG']['tl_article']['es_size'], 'exclude' => true, 'inputType' => 'imageSize', 'options' => System::getImageSizes(), 'reference' => &$GLOBALS['TL_LANG']['MSC'], 'eval' => array('rgxp'=>'natural', 'includeBlankOption'=>true, 'nospace'=>true, 'helpwizard'=>true, 'tl_class'=>'w50'), 'sql' => "varchar(64) NOT NULL default ''" ); /** * Provide miscellaneous methods that are used by the data configuration array. * * @author Benjamin Roth <http://www.esales-media.de> */ class tl_article_eSM_artLayout extends \Backend { /** * Import the back end user object */ public function __construct() { parent::__construct(); $this->import('BackendUser', 'User'); } /** * Return article layouts * * @param DataContainer $dc * * @return array */ public function getArticleLayouts(\DataContainer $dc) { $arrArticleLayouts = array(); $ArticleLayouts = \Database::getInstance()->execute("SELECT al.id, al.title, al.alias, t.name FROM tl_article_layouts al, tl_theme t WHERE t.id = al.pid ORDER BY t.name, al.sorting"); while ($ArticleLayouts->next()) { $arrArticleLayouts[$ArticleLayouts->name][$ArticleLayouts->id] = $ArticleLayouts->title." [".$ArticleLayouts->alias."]"; } return $arrArticleLayouts; } /** * Dynamically add subpalettes on runtime * * @param DataContainer $dc */ public function registerSubPalettes(\DataContainer $dc) { $Layouts = \Database::getInstance()->execute("SELECT * FROM tl_article_layouts"); while ($Layouts->next()) { if ($Layouts->showBackgroundImage) { $GLOBALS['TL_DCA']['tl_article']['subpalettes']['es_type_'.$Layouts->id] = 'es_backgroundSRC'; } } } public function addLabel($row, $label, \DataContainer $dc, $args) { $time = \Date::floorToMinute(); $published = ($row['published'] && ($row['start'] == '' || $row['start'] <= $time) && ($row['stop'] == '' || $row['stop'] > ($time + 60))); $ArticleLayout = \ArticleLayoutsModel::findByPk($row['es_type']); $strLabel = sprintf('%s <span style="color:#b3b3b3;padding-left:3px">[%s]</span> <span class="tl_blue" style="padding-left:3px">[%s]</span>',$row['title'],$GLOBALS['TL_LANG']['COLS'][$row['inColumn']],($ArticleLayout->title ? $ArticleLayout->title : '-')); return '<a href="contao/main.php?do=feRedirect&page='.$row['pid'].'&article='.(($row['alias'] != '' && !Config::get('disableAlias')) ? $row['alias'] : $row['id']).'" title="'.specialchars($GLOBALS['TL_LANG']['MSC']['view']).'" target="_blank">'.Image::getHtml('articles'.($published ? '' : '_').'.gif', '', 'data-icon="articles.gif" data-icon-disabled="articles_.gif"').'</a> '.$strLabel; } /** * Sets default layout if needed * * @param DataContainer $dc */ public function setDefaultLayout(\DataContainer $dc) { if ($_GET['act'] == 'edit') { $ArticleModel = \ArticleModel::findByPk($dc->id); if ($ArticleModel !== null) { // No change or empty value if (!$ArticleModel->tstamp && !$ArticleModel->es_type) { $PageModel = \PageModel::findWithDetails($ArticleModel->pid); $LayoutModel = \LayoutModel::findByPk($PageModel->layout); if ($PageModel !== null) { $ArticleDefaultLayout = \Database::getInstance()->prepare("SELECT al.id FROM tl_article_layouts al, tl_theme t WHERE t.id = al.pid AND isDefault = '1' AND al.pid = ? ORDER BY t.name, al.sorting LIMIT 1")->execute($LayoutModel->id); if ($ArticleDefaultLayout->numRows) { $ArticleModel->es_type = $ArticleDefaultLayout->id; $ArticleModel->save(); } } } } } } }