Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/Improvement for cases when a theme was selected but than removed #855

Merged
merged 1 commit into from
Jan 2, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 69 additions & 2 deletions core/View/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
namespace ImpressCMS\Core\View;

use icms;
use ImpressCMS\Core\Database\Criteria\CriteriaCompo;
use ImpressCMS\Core\Database\Criteria\CriteriaItem;
use ImpressCMS\Core\Facades\Config;
use ImpressCMS\Core\View\Theme\ThemeFactory;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Cache\InvalidArgumentException;
use SmartyBC;
Expand Down Expand Up @@ -73,7 +77,7 @@ public function __construct() {

parent::__construct();

foreach (\icms::getInstance()->get('smarty.resource') as $plugin) {
foreach (icms::getInstance()->get('smarty.resource') as $plugin) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call to undeclared method \icms::get

$this->registerResource(
$plugin->getName(),
$plugin
Expand All @@ -84,7 +88,7 @@ public function __construct() {
'modifier' => 'register_modifier',
'compiler' => 'register_compiler_function',
] as $type => $function) {
foreach (\icms::getInstance()->get('smarty.' . $type) as $plugin) {
foreach (icms::getInstance()->get('smarty.' . $type) as $plugin) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call to undeclared method \icms::get

$this->$function(
$plugin->getName(),
[$plugin, 'execute']
Expand Down Expand Up @@ -209,4 +213,67 @@ public static function template_clear_module_cache($mid) {
}
}
}

/**
* @inheritDoc
*/
public function fetch($template = null, $cache_id = null, $compile_id = null, $parent = null)
fiammybe marked this conversation as resolved.
Show resolved Hide resolved
fiammybe marked this conversation as resolved.
Show resolved Hide resolved
fiammybe marked this conversation as resolved.
Show resolved Hide resolved
fiammybe marked this conversation as resolved.
Show resolved Hide resolved
{
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opening brace should be on the same line as the declaration

try {
return parent::fetch($template, $cache_id, $compile_id, $parent);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reference to undeclared class \SmartyBC

} catch (SmartyException $exception) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Catching undeclared class \SmartyException

if (strpos($exception->getMessage(), 'Unable to load template') === 0) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call to method getMessage from undeclared class \SmartyException

$this->handleDeletedTemplateSet($exception);
} else {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method fetch uses an else expression. Else is never necessary and you can simplify the code to work without else.

throw $exception;
}
}
}

/**
* Handle exception for deleted template set
* (if there is possibility automatically auto updates selected theme in config)
*
* @param SmartyException $exception
*
* @throws SmartyException
*/
private function handleDeletedTemplateSet(SmartyException $exception): void {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Method handleDeletedTemplateSet has 26 lines of code (exceeds 25 allowed). Consider refactoring.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parameter $exception has undeclared type \SmartyException

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@throws type of handleDeletedTemplateSet has undeclared type \SmartyException

$isAdmin = $this->getTemplateVars('icms_isadmin');
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call to undeclared method \ImpressCMS\Core\View\Template::getTemplateVars

$themesList = $isAdmin ? ThemeFactory::getAdminThemesList() : ThemeFactory::getThemesList();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid using static access to class '\ImpressCMS\Core\View\Theme\ThemeFactory' in method 'handleDeletedTemplateSet'.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid using static access to class '\ImpressCMS\Core\View\Theme\ThemeFactory' in method 'handleDeletedTemplateSet'.

$configKey = $isAdmin ? 'theme_admin_set' : 'theme_set';

if (empty($themesList)) {
throw $exception;
}

$configCriteria = new CriteriaCompo(
new CriteriaItem('conf_modid', 0)
);
$configCriteria->add(
new CriteriaItem('conf_name', $configKey)
);
$configCriteria->add(
new CriteriaItem('conf_catid', Config::CATEGORY_MAIN)
);

/**
* @var Config $configHandler
*/
$configHandler = icms::getInstance()->get('config');
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call to undeclared method \icms::get

$config = $configHandler->getConfigs($configCriteria);
if (!isset($config[0])) {
throw $exception;
}

if (isset($themesList[$config[0]->conf_value])) {
throw $exception;
}

$config[0]->conf_value = current($themesList);
$config[0]->store();

redirect_header(ICMS_URL);
}

}