<?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');

/**
 * 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),
  'sql'                     => "int(10) unsigned NOT NULL default '0'"
);

$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';
      }
    }

  }
}