Skip to content

Commit

Permalink
Merge pull request #75 from creecros/cache-toggle-feature
Browse files Browse the repository at this point in the history
Cache toggle feature
  • Loading branch information
creecros authored Mar 6, 2019
2 parents 5900403 + f0346e2 commit ec506b0
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 11 deletions.
1 change: 1 addition & 0 deletions Controller/CustomizerConfigController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function save()
$values = $this->request->getValues();

if (array_key_exists('use_custom_login', $values) === false) { $this->configModel->save(['use_custom_login' => '']); }
if (array_key_exists('enable_cache', $values) === false) { $this->configModel->save(['enable_cache' => '']); }

if ($this->configModel->save($values)) {
$this->languageModel->loadCurrentLanguage();
Expand Down
73 changes: 68 additions & 5 deletions Controller/CustomizerFileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,36 @@ protected function renderFileWithCache(array $file, $mimetype)
{
$etag = md5($file['path']);

if ($this->request->getHeader('If-None-Match') === '"'.$etag.'"') {
$this->response->status(304);
} else {
try {
$this->response->withContentType($mimetype);
$this->response->withCache(5 * 86400, $etag);
$this->response->send();
$this->objectStorage->output($file['path']);
} catch (ObjectStorageException $e) {
$this->logger->error($e->getMessage());
}
}
}

/**
* Output file without cache
*
* @param array $file
* @param $mimetype
*/
protected function renderFileWithoutCache(array $file, $mimetype)
{
$etag = md5($file['path']);

if ($this->request->getHeader('If-None-Match') === '"'.$etag.'"') {
$this->response->status(304);
} else {
try {
$this->response->withContentType($mimetype);
$this->response->withOutCache();
// $this->response->withCache(5 * 86400, $etag);
$this->response->send();
$this->objectStorage->output($file['path']);
} catch (ObjectStorageException $e) {
Expand Down Expand Up @@ -84,23 +107,59 @@ public function logo()
{
if ($this->logoexists()) {
$file = $this->customizerFileModel->getByType(1);
$this->renderFileWithCache($file, $this->helper->file->getImageMimeType($file['name']));
if ($this->configModel->get('enable_cache', '') == 'checked') {
$this->renderFileWithCache($file, $this->helper->file->getImageMimeType($file['name']));
} else {
$this->renderFileWithoutCache($file, $this->helper->file->getImageMimeType($file['name']));
}
}
}

public function logo_setting()
{
if ($this->logoexists()) {
$file = $this->customizerFileModel->getByType(1);
$this->renderFileWithoutCache($file, $this->helper->file->getImageMimeType($file['name']));
}
}

public function loginlogo()
{
if ($this->loginlogoexists()) {
$file = $this->customizerFileModel->getByType(3);
$this->renderFileWithCache($file, $this->helper->file->getImageMimeType($file['name']));
if ($this->configModel->get('enable_cache', '') == 'checked') {
$this->renderFileWithCache($file, $this->helper->file->getImageMimeType($file['name']));
} else {
$this->renderFileWithoutCache($file, $this->helper->file->getImageMimeType($file['name']));
}
}
}

public function loginlogo_setting()
{
if ($this->loginlogoexists()) {
$file = $this->customizerFileModel->getByType(3);
$this->renderFileWithoutCache($file, $this->helper->file->getImageMimeType($file['name']));
}
}

public function icon()
{
if ($this->iconexists()) {
$file = $this->customizerFileModel->getByType(2);
$this->renderFileWithCache($file, $this->helper->file->getImageMimeType($file['name']));
if ($this->configModel->get('enable_cache', '') == 'checked') {
$this->renderFileWithCache($file, $this->helper->file->getImageMimeType($file['name']));
} else {
$this->renderFileWithoutCache($file, $this->helper->file->getImageMimeType($file['name']));
}
}
}

public function icon_setting()
{
if ($this->iconexists()) {
$file = $this->customizerFileModel->getByType(2);
$this->renderFileWithoutCache($file, $this->helper->file->getImageMimeType($file['name']));
}
}

Expand Down Expand Up @@ -142,7 +201,11 @@ public function iconexists()
public function image()
{
$file = $this->customizerFileModel->getById($this->request->getIntegerParam('file_id'));
$this->renderFileWithCache($file, $this->helper->file->getImageMimeType($file['name']));
if ($this->configModel->get('enable_cache', '') == 'checked') {
$this->renderFileWithCache($file, $this->helper->file->getImageMimeType($file['name']));
} else {
$this->renderFileWithoutCache($file, $this->helper->file->getImageMimeType($file['name']));
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public function getPluginAuthor()

public function getPluginVersion()
{
return '1.8.4';
return '1.9.0';
}

public function getPluginHomepage()
Expand Down
25 changes: 20 additions & 5 deletions Template/file/show.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<div class="panel-heading">
<h3 class="panel-title"><?= t('Header Image') ?></h3>
</div>
<img id="hl1" src="<?= $this->url->href('CustomizerFileController', 'logo', array('plugin' => 'customizer', 'file_id' => $logo['id'])) ?>" alt="<?= $this->text->e($logo['name']) ?>" height="<?= $this->task->configModel->get('headerlogo_size', '30') ?>">
<img id="hl1" src="<?= $this->url->href('CustomizerFileController', 'logo_setting', array('plugin' => 'customizer', 'file_id' => $logo['id'])) ?>" alt="<?= $this->text->e($logo['name']) ?>" height="<?= $this->task->configModel->get('headerlogo_size', '30') ?>">
<br>
<br>
<ul class="upload-link">
Expand Down Expand Up @@ -55,7 +55,7 @@
<h3 class="panel-title"><?= t('Login Image') ?></h3>
</div>

<img id="ll1" src="<?= $this->url->href('CustomizerFileController', 'loginlogo', array('plugin' => 'customizer', 'file_id' => $loginlogo['id'])) ?>" alt="<?= $this->text->e($loginlogo['name']) ?>" height="<?= $this->task->configModel->get('loginlogo_size', '50') ?>">
<img id="ll1" src="<?= $this->url->href('CustomizerFileController', 'loginlogo_setting', array('plugin' => 'customizer', 'file_id' => $loginlogo['id'])) ?>" alt="<?= $this->text->e($loginlogo['name']) ?>" height="<?= $this->task->configModel->get('loginlogo_size', '50') ?>">
<br>
<br>
<ul class="upload-link">
Expand Down Expand Up @@ -95,7 +95,7 @@
<h3 class="panel-title"><?= t('Favicon Image') ?></h3>
</div>

<img src="<?= $this->url->href('CustomizerFileController', 'icon', array('plugin' => 'customizer', 'file_id' => $flavicon['id'])) ?>" alt="<?= $this->text->e($flavicon['name']) ?>" height="16">
<img src="<?= $this->url->href('CustomizerFileController', 'icon_setting', array('plugin' => 'customizer', 'file_id' => $flavicon['id'])) ?>" alt="<?= $this->text->e($flavicon['name']) ?>" height="16">
<br>
<br>
<ul class="upload-link">
Expand All @@ -119,7 +119,22 @@
</ul>
<?php endif ?>
</div>
<div class="form-actions mb-20 ml-15">

<table>
<tr>
<th width="25%"><strong><?= t('Enable Cache') ?></strong>
<p class="form-help enable-cache-desc"><?= e('Once enabled, site assets will begin to be cached for 5 days, increasing speed of site. However, you will need to clear your cache to see any new images uploaded. The settings page, will be unaffected by this setting.') ?></p>
</th>
<th>
<label class="switch">
<input id="toggle" name="enable_cache" type="checkbox" value="checked" <?= $this->task->configModel->get('enable_cache','') ?>>
<span class="slider round"></span>
</label>
</th>
</tr>
</table>

<div class="form-actions mb-20 ml-15">
<button type="submit" name="save" value="save" class="btn btn-blue"><?= t('Save') ?></button>
</div>
</div>
Expand Down Expand Up @@ -271,7 +286,7 @@
</div>
<div id="preview-form-login" class="preview-form-login">
<?php if ($customizer['loginCheck']): ?>
<?= $this->url->link('<img src="' . $this->url->href('CustomizerFileController', 'loginlogo', array('plugin' => 'customizer')) . '" height="' . $customizer['logoSize'] . '">', 'CustomizerFileController', 'link', array('plugin' => 'customizer')) ?>
<?= $this->url->link('<img src="' . $this->url->href('CustomizerFileController', 'loginlogo_setting', array('plugin' => 'customizer')) . '" height="' . $customizer['logoSize'] . '">', 'CustomizerFileController', 'link', array('plugin' => 'customizer')) ?>
<?php else: ?>
<?= $this->url->link('K<span>B</span>', 'DashboardController', 'show', array(), false, '', t('Dashboard')) ?>
<?php endif ?>
Expand Down

0 comments on commit ec506b0

Please sign in to comment.