/home/coolpkct/www/websites/cake3.cool.rocks/admin/classes/theme.php
<?php
/**
 * Part of Showkase web site management package
 *
 * @package Showkase
 * @author Jack Hardie {@link http://www.jhardie.com}
 * @copyright Copyright (c) 2014, SimpleViewer Inc.
 */
defined('SK_ACCESS')||die('<h1>403: Forbidden</h1>');
require_once 'classes'.DIRECTORY_SEPARATOR.'layer.php';
 
/**
 * About page
 *
 * @package Showkase
 */
class Theme extends Layer
{
    /**
     * @var string page type
     */
    protected $pageType = 'theme';
        
    /**
     * @var boolean
     */
    protected $supportedPageType = true;
    
    /**
     * @var string current theme directory name
     */
    protected $themeDirectory;
    
    /**
    * Constructor
    * Overrides Page constructor
    */
    public function __construct(Site $site)
    {
        $this->themeSet = $site->getThemeSet();
        $this->themeDirectory = $this->themeSet->getCurrentThemeName();
        $this->config = SkConfig::getInstance();
        $this->pagePathRelSvm =
            $this->config->getRelativeContentPath(getcwd()).
            $this->config->getThemeDataPathRelContent().DIRECTORY_SEPARATOR.
            $this->themeDirectory.DIRECTORY_SEPARATOR;
        $settingsDirectory = rtrim($this->pagePathRelSvm, '\\/');
        $this->settingsPath = $this->pagePathRelSvm.'theme.xml';
        /*
        $iniPath =
            $this->config->getThemesPath().DIRECTORY_SEPARATOR.
            $this->themeDirectory.DIRECTORY_SEPARATOR.
            'theme.ini';
        */
        if (!is_dir($settingsDirectory) && !Filer::skMkdir($settingsDirectory, NEW_DIR_MODE, true)) {
            Board::addMessage('Cannot create theme data folder, changes will not be saved.', 'error');
        }
        $iniFields = $this->readFieldInis($this->themeSet->getThemeIniPaths());
        foreach ($iniFields as $name => $field) {
            $this->layerDefaults[$name] = $field->getValue();
        }
        $this->incomingFields = $site->getAccumulatedFields();
        $this->layerFields = $this->mergeFields($iniFields, $this->incomingFields);
        $layerSettings = PageReader::readPageVars($this->settingsPath);
        $deprecated = array(
            'galleryPageBodyLayout' => 'pageBodyLayout',
            'showSubNav' => 'ss_showAllSubNav'
        );
        $this->loadFields($this->layerFields, $layerSettings, $deprecated);
        $this->accumulatedFields = $this->layerFields + $this->incomingFields;
    }
    
    /**
     * Is this page type a gallery
     *
     * @return boolean
     */
    public function isGallery()
    {
        return false;
    }
    
    /**
     * Overrides Layer::getRef
     *
     * @return string
     */
    public function getRef()
    {
       return $this->themeDirectory;
    }
    
   /**
    * Get theme display name
    *
    * @return string
    */
    public function getThemeDisplayName()
    {
        return $this->themeSet->getThemeDisplayName($this->themeDirectory);
    }
    
    /**
     * Get index thumb width and height for current theme
     *
     * @return array
     */
    public function getIndexThumbSize()
    {
        $columns = $this->getLayerVar('ss_indexThumbColumns');
        $available = $this->getLayerVar('ss_totalIndexThumbWidth');
        $themeWidthSetting = $this->getLayerVar('ss_indexThumbWidth');
        switch (true) {
            case !empty($columns) && !empty ($available) :
                $width = floor($available/$columns);
                break;
            case !empty($themeWidthSetting) :
                $width = $themeWidthSetting;
                break;
            default :
                $width = INDEX_THUMB_WIDTH;
        }
        switch (true) {
            case !is_null($thumbPercent = $this->getLayerVar('ss_indexThumbFormat')) :
                $thumbFormat = $thumbPercent > 0
                    ? $thumbPercent/100
                    : 1;
                $height = ceil($width * $thumbFormat);
                break;
            case !is_null($themeHeightSetting = $this->getLayerVar('ss_indexThumbHeight')) :
                $height = $themeHeightSetting;
                break;
            default :
                $height = INDEX_THUMB_HEIGHT;
        }
        return array('width'=>$width, 'height'=>$height);
    }
    
    /**
     * @return object instance of Theme
     */
    public function getTheme()
    {
        return $this;
    }
}