<?php

/**
 * ArtLayout for Contao
 *
 * Copyright (c) 2016 Benjamin Roth
 *
 * @link    http://www.esales-media.de
 * @license commercial
 */

namespace eSM_artLayout;


/**
 * Provides methodes to handle articles.
 *
 * @property integer $tstamp
 * @property string  $title
 * @property string  $alias
 * @property string  $inColumn
 * @property boolean $showTeaser
 * @property boolean $multiMode
 * @property string  $teaser
 * @property string  $teaserCssID
 * @property string  $classes
 * @property string  $keywords
 * @property boolean $printable
 * @property boolean $published
 * @property integer $start
 * @property integer $stop
 * @property integer $es_type
 * @property string  $es_backgroundSRC
 *
 * @author Leo Feyer <https://github.com/leofeyer>
 */
class ModuleArticle extends \Contao\ModuleArticle
{

  /**
   * Inject some article layout stuff
   *
   * @param boolean $blnNoMarkup
   *
   * @return string
   */
  public function generate($blnNoMarkup=false)
  {
    if ($this->es_type)
    {
      $ArticleLayout = ArticleLayoutsModel::findByPk($this->es_type);

      if ($ArticleLayout !== null)
      {

        // Select background image and add inline styles
        if ($ArticleLayout->showBackgroundImage)
        {
          $Image = \FilesModel::findByPk($this->es_backgroundSRC);

          if (!is_null($Image))
          {
            $arrData = array
            (
              'singleSRC' => $Image->path,
              'size' => $ArticleLayout->bgImageSize
            );
            $objClass = new \stdClass();

            $this->addImageToTemplate($objClass, $arrData, \Config::get('maxImageWidth'));

            if (trim($ArticleLayout->bgStyle))
            {
              $this->arrStyle[] = str_ireplace('##bg_image##',$objClass->src,$ArticleLayout->bgStyle);
            }
            else
            {
              $this->arrStyle[] = "background-image: url('".$objClass->src."');";
            }
          }
        }

        // Set article layout related variables to be used in article template
        $this->useHelper = $ArticleLayout->useHelper;
        $this->maxWidth = $ArticleLayout->maxWidth;
        $this->center = $ArticleLayout->center;
        $this->articleLayoutClass = $ArticleLayout->alias;
      }
    }

    return parent::generate($blnNoMarkup);
  }
}