79 lines
2.4 KiB
PHP
79 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Cms\Repositories\Templates;
|
|
|
|
use App\CMS\Contracts\Repositories\Template as ITemplate;
|
|
use App\CMS\Repositories\Repository;
|
|
use App\Models\Module;
|
|
use App\Models\Templates\Template as TemplateModel;
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
class Template extends Repository implements ITemplate
|
|
{
|
|
public function setModel(TemplateModel $model)
|
|
{
|
|
$this->model = $model;
|
|
}
|
|
|
|
/**
|
|
* Getter for absolute path to template
|
|
*
|
|
* @param null $mode
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getPathToTemplate($mode = null)
|
|
{
|
|
$templateName = $this->model->template_name;
|
|
$path = $this->model->templatePath;
|
|
|
|
// delete namespace if given
|
|
if (strpos($this->model->template_name, '::')) {
|
|
list(, $templateName) = explode('::', $templateName);
|
|
}
|
|
|
|
// get template name, do not care about directories (they are already contained in path)
|
|
$parts = explode('.', $templateName);
|
|
$templateName = last($parts);
|
|
|
|
// just a shortcut for this unbelievable long constant name...
|
|
$_ = DIRECTORY_SEPARATOR;
|
|
|
|
// path parts
|
|
$absolutePath = base_path($path->path);
|
|
$templatePath = str_replace('.', $_, $templateName);
|
|
$file = $templatePath . ITemplate::TEMPLATE_SUFFIX;
|
|
|
|
// compose two versions of path - original and with "custom" directory
|
|
$original = $absolutePath . $_ . $file;
|
|
$edited = $absolutePath . $_ . ITemplate::EDITED_TEMPLATES_DIRECTORY . $_ . $file;
|
|
|
|
// decide which path will be returned
|
|
switch ($mode) {
|
|
case ITemplate::CUSTOM_EDIT_DIRECTORY:
|
|
return $edited;
|
|
break;
|
|
|
|
case ITemplate::ORIGINAL_DIRECTORY:
|
|
return $original;
|
|
break;
|
|
|
|
default:
|
|
if (file_exists($absolutePath . $_ . ITemplate::EDITED_TEMPLATES_DIRECTORY . $_ . $file)) {
|
|
return $edited;
|
|
}
|
|
|
|
return $original;
|
|
break;
|
|
}
|
|
}
|
|
|
|
public function getForModule(Module $module)
|
|
{
|
|
return Cache::remember('templates-for-module-'.$module->id, setting('cacheModules', 3600), function () use ($module) {
|
|
return TemplateModel::where('module_id', $module->id)
|
|
->select(['template_name', 'name', 'description'])
|
|
->get();
|
|
});
|
|
}
|
|
} |