Files
core/app/Models/Templates/ModelTraits/Template.php
T

78 lines
2.2 KiB
PHP

<?php
namespace App\Models\Templates\ModelTraits;
use App\Models\Templates\TemplatePath;
use App\Models\Templates\Template as TemplateModel;
trait Template {
protected $pathModel = null;
/**
* In one path may be many templates
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function getPath()
{
if (!$this->pathModel) {
$this->pathModel = TemplatePath::where('id', $this->path)
->first();
}
return $this->pathModel;
}
/**
* Getter for absolute path to template
*
* @param null $mode
*
* @return string
*/
public function getPathToTemplate($mode = null)
{
$templateName = $this->template_name;
$path = $this->getPath();
// delete namespace if given
if (strpos($this->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 . TemplateModel::TEMPLATE_SUFFIX;
// compose two versions of path - original and with "custom" directory
$original = $absolutePath . $_ . $file;
$edited = $absolutePath . $_ . TemplateModel::EDITED_TEMPLATES_DIRECTORY . $_ . $file;
// decide which path will be returned
switch ($mode) {
case TemplateModel::CUSTOM_EDIT_DIRECTORY:
return $edited;
break;
case TemplateModel::ORIGINAL_DIRECTORY:
return $original;
break;
default:
if (file_exists($absolutePath . $_ . TemplateModel::EDITED_TEMPLATES_DIRECTORY . $_ . $file)) {
return $edited;
}
return $original;
break;
}
}
}