Files
2025-03-12 17:06:23 +01:00

412 lines
9.5 KiB
PHP

<?php
/**
* Subclass for representing a row from the 'st_theme' table.
*
* @package stThemePlugin
* @subpackage libs
*/
class Theme extends BaseTheme
{
const SYSTEM_DEFAULT_THEMES = array(
'default2',
'responsive',
'argentorwd',
'homeelectronics',
'giallo',
'moderno',
'sportivo',
'quattro',
'coffeestore',
'segno',
'longboard',
'bagging',
'games',
'surfing',
'brassiere',
'yewelry',
'gifts',
'fragrance',
'furniture',
'argento',
'meble'
);
protected $themeConfig = null;
protected $themeConfigCached = null;
protected $editorImages = null;
protected $baseThemes = null;
protected $themeContents = array();
public function getBackendCulture()
{
return sfContext::getInstance()->getUser()->getCulture();
}
public function __toString()
{
return $this->theme;
}
public function getColorScheme()
{
return $this->getOptColorScheme();
}
public function isSystemDefault()
{
return parent::getIsSystemDefault();
}
public function getIsSystemDefault($check_active = true)
{
return parent::getIsSystemDefault() || $this->getTheme() == 'default' || $this->getTheme() == 'homeelectronics' || $check_active && $this->getActive();
}
/**
*
* alias for Theme::setBaseThemeId
*
* @param int $v
*/
public function setBaseThemeList($v)
{
$this->setBaseThemeId($v ? $v : null);
}
/**
* Przeciążenie zapisu
*/
public function delete($con = null)
{
$ret = parent::delete($con);
stTheme::clearCache();
return $ret;
}
public function setName($v)
{
$this->setTheme($v);
}
public function getName()
{
return $this->getTheme();
}
public function hasBaseTheme()
{
return null !== $this->getBaseTheme();
}
public function getBaseTheme()
{
return ThemePeer::retrieveByPKCached($this->base_theme_id);
}
/**
* Zwraca pełna ścieżke tematu
*
* @param boolean $onlyStringNames
* @return Theme[]|string[]
*/
public function getBaseThemePath($onlyStringNames = false)
{
$baseThemes = $this->getBaseThemes();
$baseThemes[] = $this;
if ($onlyStringNames)
{
return array_map(function(Theme $theme) {
return $theme->getTheme();
}, $baseThemes);
}
return $baseThemes;
}
public function getBaseThemes($current = null, array &$baseThemes = array())
{
if (null === $this->baseThemes)
{
if (null === $current)
{
$current = $this->getBaseTheme();
if (null === $current)
{
return array();
}
}
else
{
$current = $current->getBaseTheme();
}
if (null !== $current)
{
$baseThemes[] = $current;
}
elseif ($baseThemes)
{
$this->baseThemes = array_reverse($baseThemes);
}
$this->getBaseThemes($current, $baseThemes);
}
return $this->baseThemes;
}
public function hasThemeContent($content_id)
{
$content = $this->getThemeContent($content_id);
return null !== $content && !empty($content);
}
public function getThemeContent($content_id, $default = null, $culture = null)
{
if (null === $culture)
{
$culture = sfContext::getInstance()->getUser()->getCulture();
}
if (!isset($this->themeContents[$culture]))
{
$fc = stFunctionCache::getInstance('stThemePlugin');
$this->themeContents[$culture] = $fc->cacheCall(array($this, 'loadThemeContents'), array($culture, $this->getId()));
}
return isset($this->themeContents[$culture][$content_id]) ? $this->themeContents[$culture][$content_id] : $default;
}
/**
* Przeciążenie zapisu
*/
public function save($con = null)
{
$updateDefaultTheme = $this->isColumnModified(ThemePeer::ACTIVE) && $this->getActive();
if (in_array($this->theme, self::SYSTEM_DEFAULT_THEMES))
{
$this->setIsSystemDefault(true);
}
$result = parent::save($con);
if ($updateDefaultTheme)
{
$this->updateActiveThemes();
}
if ($updateDefaultTheme || $this->isColumnModified('is_responsive'))
{
$this->getConfig()->save();
ThemePeer::doSimpleStoreUpdate(null, $this)->save();
}
stTheme::clearCache();
return $result;
}
public function getConfigurationPath($system = false)
{
return $system ? sfConfig::get('sf_root_dir').'/config/theme/'.$this->theme.'.yml' : 'config/theme/'.$this->theme.'.yml';
}
public function getCssDir($system_path = false)
{
if ($system_path)
{
$root_dir = realpath(sfConfig::get('sf_web_dir'));
if ($root_dir == '/')
{
$root_dir = '';
}
return $root_dir.'/css/frontend/theme/'.$this->theme;
}
return '/css/frontend/theme/'.$this->theme;
}
public function getImageDir($system_path = false)
{
if ($system_path)
{
$root_dir = realpath(sfConfig::get('sf_web_dir'));
if ($root_dir == '/')
{
$root_dir = '';
}
return $root_dir.'/images/frontend/theme/'.$this->theme;
}
return '/images/frontend/theme/'.$this->theme;
}
public function getEditorCssPath($css, $system_path = false)
{
return $this->getCssDir($system_path).'/_editor/'.$css;
}
public function getEditorImageDir($system_path = false)
{
return $this->getImageDir($system_path).'/_editor';
}
public function getThemeConfigCached()
{
if (null === $this->themeConfigCached)
{
$fc = new stFunctionCache('stThemePlugin');
$this->themeConfigCached = $fc->cacheCall(array($this, 'getThemeConfig'));
}
return $this->themeConfigCached;
}
public function getThemeConfig()
{
if (null === $this->themeConfig)
{
$c = new Criteria();
$c->setLimit(1);
$configs = $this->getThemeConfigs($c);
$this->themeConfig = $configs ? $configs[0] : null;
if (null === $this->themeConfig)
{
$this->themeConfig = new ThemeConfig();
$this->addThemeConfig($this->themeConfig);
}
}
return $this->themeConfig;
}
public function getThemeDir()
{
return 'frontend/theme/' . $this->theme;
}
public function getThemeColorSchemeDir()
{
return $this->getThemeDir() . '/' . $this->getColorScheme();
}
public function getDefaultThemeDir()
{
return $this->getBaseTheme() ? 'frontend/theme/' . $this->getBaseTheme()->getName() : null;
}
public function getTemplateDir($module = null)
{
return $module ? sfLoader::getTemplateDir($module, null) . '/theme/' . $this->theme : sfConfig::get('sf_root_dir') . '/apps/frontend/templates/theme/'. $this->theme;
}
public function getEditorImagePath($image, $system_path = false)
{
return $this->getImageDir($system_path).'/'.$image;
}
public function findEditorImagePath($image, Theme $theme = null)
{
if (null === $theme)
{
$theme = $this;
}
if (is_readable($theme->getEditorImagePath($image, true)))
{
return $theme->getEditorImagePath($image);
}
elseif ($theme->hasBaseTheme())
{
return $this->findEditorImagePath($image, $theme->getBaseTheme());
}
return null;
}
public function getImagePath($image, $system_path = false, $default = false)
{
return $this->getImageDir($system_path).'/'.($default ? $image : $this->getImage($image));
}
public function getImage($image)
{
return $this->hasImage($image) ? $this->editorImages[$image] : $image;
}
public function hasImage($image)
{
if (null === $this->editorImages)
{
$this->editorImages = stThemeConfigGenerator::loadImageConfig($this, SF_ENVIRONMENT == 'theme');
}
return isset($this->editorImages[$image]);
}
public function getIsResponsive()
{
return $this->getConfig()->get('responsive') === $this->id;
}
public function setIsResponsive($value)
{
if (!$value && $this->getConfig()->get('responsive') == $this->id)
{
$this->getConfig()->set('responsive', null);
$this->modifiedColumns['is_responsive'] = true;
}
elseif ($value && $this->getConfig()->get('responsive') != $this->id)
{
$this->getConfig()->set('responsive', $this->id);
$this->modifiedColumns['is_responsive'] = true;
}
}
public function loadThemeContents()
{
$themeContents = array();
foreach ($this->getThemeContents() as $content)
{
$themeContents[$content->getContentId()] = $content->getContent();
}
return $themeContents;
}
protected function updateActiveThemes()
{
$s = new Criteria();
$s->add(ThemePeer::ID, $this->getId(), Criteria::NOT_EQUAL);
$u = new Criteria();
$u->add(ThemePeer::ACTIVE, false);
BasePeer::doUpdate($s, $u, Propel::getConnection());
}
protected function getConfig(): stConfig
{
return stConfig::getInstance('stThemeBackend');
}
}