ThemeData can now pass variables to the combiner

Add support for fileuploads to ThemeData
Fixes #1539
This commit is contained in:
Samuel Georges 2016-06-02 05:22:15 +10:00
parent 0c2cf420fd
commit 27dc84c59b
4 changed files with 99 additions and 18 deletions

View File

@ -396,6 +396,15 @@ class Theme
return $this->getConfigValue('form', false);
}
/**
* Returns data specific to this theme
* @return Cms\Models\ThemeData
*/
public function getCustomData()
{
return ThemeData::forTheme($this);
}
/**
* Ensures this theme is registered as a Halcyon them datasource.
* @return void
@ -418,7 +427,7 @@ class Theme
public function __get($name)
{
if ($this->hasCustomData()) {
$theme = ThemeData::forTheme($this);
$theme = $this->getCustomData();
return $theme->{$name};
}
@ -433,11 +442,10 @@ class Theme
public function __isset($key)
{
if ($this->hasCustomData()) {
$theme = ThemeData::forTheme($this);
return isset($theme->{$key});
$theme = $this->getCustomData();
return $theme->offsetExists($key);
}
return false;
}
}

View File

@ -3,6 +3,7 @@
use Lang;
use Model;
use Cms\Classes\Theme as CmsTheme;
use System\Classes\CombineAssets;
/**
* Customization data used by a theme
@ -39,6 +40,11 @@ class ThemeData extends Model
*/
public $rules = [];
/**
* @var array Relations
*/
public $attachOne = [];
/**
* @var ThemeData Cached array of objects
*/
@ -56,6 +62,11 @@ class ThemeData extends Model
$this->setRawAttributes(array_only($this->getAttributes(), $staticAttributes));
}
public function afterSave()
{
CombineAssets::resetCache();
}
/**
* Returns a cached version of this model, based on a Theme object.
* @param $theme Cms\Classes\Theme
@ -78,9 +89,16 @@ class ThemeData extends Model
* Repeater form fields store arrays and must be jsonable.
*/
foreach ($this->getFormFields() as $id => $field) {
if (isset($field['type']) && $field['type'] == 'repeater') {
if (!isset($field['type'])) {
continue;
}
if ($field['type'] == 'repeater') {
$this->jsonable[] = $id;
}
elseif ($field['type'] == 'fileupload') {
$this->attachOne[$id] = 'System\Models\File';
}
}
/*
@ -91,8 +109,9 @@ class ThemeData extends Model
public function beforeValidate()
{
if (!$this->exists)
if (!$this->exists) {
$this->setDefaultValues();
}
}
/**
@ -119,6 +138,7 @@ class ThemeData extends Model
/**
* Returns all fields defined for this model, based on form field definitions.
* @return array
*/
public function getFormFields()
{
@ -129,4 +149,23 @@ class ThemeData extends Model
$theme->getConfigValue('form.tabs.fields', []) +
$theme->getConfigValue('form.secondaryTabs.fields', []);
}
/**
* Returns variables that should be passed to the asset combiner.
* @return array
*/
public function getAssetVariables()
{
$result = [];
foreach ($this->getFormFields() as $attribute => $field) {
if (!$varName = array_get($field, 'assetVar')) {
continue;
}
$result[$varName] = $this->{$attribute};
}
return $result;
}
}

View File

@ -24,6 +24,14 @@
<li>
<a href="<?= Backend::url('cms/themes') ?>"><?= e(trans('cms::lang.dashboard.active_theme.manage_themes')) ?></a>
</li>
<?php if ($theme->hasCustomData()): ?>
<li>
<a
href="<?= Backend::url('cms/themes/update/'.$theme->getDirName()) ?>">
Customize theme
</a>
</li>
<?php endif ?>
</ul>
<?php else: ?>
<div class="callout callout-warning">

View File

@ -1,7 +1,7 @@
<?php namespace System\Classes;
use App;
use URL;
use Url;
use File;
use Lang;
use Cache;
@ -9,11 +9,13 @@ use Route;
use Config;
use Request;
use Response;
use Assetic\Asset\AssetCollection;
use Cms\Classes\Theme;
use Assetic\Asset\FileAsset;
use Assetic\Asset\GlobAsset;
use Assetic\Asset\AssetCache;
use Assetic\Asset\AssetCollection;
use Assetic\Cache\FilesystemCache;
use System\Helpers\Cache as CacheHelper;
use ApplicationException;
use DateTime;
@ -317,6 +319,7 @@ class CombineAssets
$filesSalt = null;
foreach ($assets as $asset) {
$filters = $this->getFilters(File::extension($asset)) ?: [];
$filters = $this->processFilters($filters);
$path = File::symbolizePath($asset, null) ?: $this->localPath . $asset;
$files[] = new FileAsset($path, $filters, public_path());
$filesSalt .= $this->localPath . $asset;
@ -346,7 +349,7 @@ class CombineAssets
$actionExists = Route::getRoutes()->getByAction($combineAction) !== null;
if ($actionExists) {
return URL::action($combineAction, [$outputFilename], false);
return Url::action($combineAction, [$outputFilename], false);
}
else {
return '/combine/'.$outputFilename;
@ -465,6 +468,29 @@ class CombineAssets
}
}
/**
* Preprocess filters to use standard configuration provided by the system.
* @param array $filters
* @return array
*/
protected function processFilters($filters)
{
$theme = Theme::getActiveTheme();
if (!$theme->hasCustomData()) {
return $filters;
}
$assetVars = $theme->getCustomData()->getAssetVariables();
foreach ($filters as $filter) {
if (method_exists($filter, 'setPresets')) {
$filter->setPresets($assetVars);
}
}
return $filters;
}
//
// Bundles
//
@ -651,17 +677,17 @@ class CombineAssets
*/
public static function resetCache()
{
if (!Cache::has('combiner.index')) {
return;
if (Cache::has('combiner.index')) {
$index = (array) @unserialize(@base64_decode(Cache::get('combiner.index'))) ?: [];
foreach ($index as $cacheId) {
Cache::forget($cacheId);
}
Cache::forget('combiner.index');
}
$index = (array) @unserialize(@base64_decode(Cache::get('combiner.index'))) ?: [];
foreach ($index as $cacheId) {
Cache::forget($cacheId);
}
Cache::forget('combiner.index');
CacheHelper::instance()->clearCombiner();
}
/**