96 lines
2.5 KiB
PHP
96 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
/**
|
|
* Class CustomFieldType
|
|
*/
|
|
class Template extends Model
|
|
{
|
|
public $timestamps = true;
|
|
protected $fillable = [
|
|
'template_name',
|
|
'module_id',
|
|
'name',
|
|
'path',
|
|
'description',
|
|
'editable'
|
|
];
|
|
protected $guarded = [];
|
|
protected $pathModel = null;
|
|
const EDITED_TEMPLATES_DIRECTORY = 'custom';
|
|
const TEMPLATE_SUFFIX = '.blade.php';
|
|
const CUSTOM_EDIT_DIRECTORY = 1;
|
|
const ORIGINAL_DIRECTORY = 2;
|
|
|
|
/**
|
|
* 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 . self::TEMPLATE_SUFFIX;
|
|
|
|
// compose two versions of path - original and with "custom" directory
|
|
$original = $absolutePath . $_ . $file;
|
|
$edited = $absolutePath . $_ . self::EDITED_TEMPLATES_DIRECTORY . $_ . $file;
|
|
|
|
// decide which path will be returned
|
|
switch ($mode) {
|
|
case self::CUSTOM_EDIT_DIRECTORY:
|
|
return $edited;
|
|
break;
|
|
|
|
case self::ORIGINAL_DIRECTORY:
|
|
return $original;
|
|
break;
|
|
|
|
default:
|
|
if (file_exists($absolutePath . $_ . self::EDITED_TEMPLATES_DIRECTORY . $_ . $file)) {
|
|
return $edited;
|
|
}
|
|
|
|
return $original;
|
|
break;
|
|
}
|
|
}
|
|
} |