Copied templates are automatically stored *only* into custom directory. They cannot be under control of VCS (while they would need to be commited after creating) and therefore are stored only localy. Previous version contained bug, that "previous" template was taken from "original" destination.
180 lines
5.1 KiB
PHP
180 lines
5.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests;
|
|
use App\Http\Requests\EditTemplateRequest;
|
|
use App\Models\Template;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\File;
|
|
|
|
class EditTemplate extends Controller
|
|
{
|
|
/**
|
|
* Render form for editing template
|
|
*
|
|
* @param $template
|
|
*
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
|
*/
|
|
public function getEdit($template)
|
|
{
|
|
$template = Template::findOrFail($template);
|
|
$path = $template->getPath();
|
|
|
|
// load template content
|
|
$absolutePath = $template->getPathToTemplate();
|
|
$content = file_get_contents($absolutePath);
|
|
|
|
return view('admin.templates.edit', [
|
|
'template' => $template,
|
|
'templateContent' => $content,
|
|
'isWritable' => is_writable(dirname($absolutePath)),
|
|
'path' => $path,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Save meta information about template and edited content
|
|
*
|
|
* @param EditTemplateRequest $request
|
|
* @param $templateId
|
|
*
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function postEdit(EditTemplateRequest $request, $templateId)
|
|
{
|
|
// find template
|
|
$template = Template::findOrFail($templateId);
|
|
|
|
// setup meta information
|
|
$template->name = $request->name;
|
|
|
|
if ($request->description) {
|
|
$template->description = $request->description;
|
|
}
|
|
|
|
// save meta information into database
|
|
$template->save();
|
|
|
|
// try to save content
|
|
return $this->saveContent($template, $request);
|
|
}
|
|
|
|
/**
|
|
* Save physical content of given template
|
|
*
|
|
* @param Template $template
|
|
* @param EditTemplateRequest $request
|
|
*
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
private function saveContent(Template $template, EditTemplateRequest $request)
|
|
{
|
|
// get templates with paths
|
|
$originalTemplate = $template->getPathToTemplate();
|
|
$customTemplate = $template->getPathToTemplate(Template::CUSTOM_EDIT_DIRECTORY);
|
|
|
|
// get paths
|
|
$originalDir = dirname($originalTemplate);
|
|
$customDir = dirname($customTemplate);
|
|
|
|
// try to save content
|
|
if (!is_writable($customDir) && !is_writable($originalDir)) {
|
|
return redirect()
|
|
->route('templates.edit', ['id' => $request->id])
|
|
->withInput()
|
|
->with('error', trans('admin/templates.cannotWriteContent'));
|
|
}
|
|
|
|
// save new content only if something was changed
|
|
if ($this->isContentDifferent($originalTemplate, $customTemplate, $request->templateContent)) {
|
|
|
|
// create directory for custom updates
|
|
if (!is_dir($customDir)) {
|
|
File::makeDirectory($customDir);
|
|
}
|
|
|
|
// make copy of existing template (if exists)
|
|
$this->backupTemplates($customTemplate);
|
|
|
|
// save content
|
|
File::put($customTemplate, $request->templateContent);
|
|
}
|
|
|
|
return redirect()
|
|
->route('templates.overview', ['id' => $template->path])
|
|
->with('success', trans('admin/templates.editSuccessfull'));
|
|
}
|
|
|
|
/**
|
|
* Get last "revision" of edited template.
|
|
*
|
|
* Template changes are not discarded, however new file is created. Last template is renamed to
|
|
* <template>.blade.php.<num>, where <num> is result of this method.
|
|
*
|
|
* @param $template
|
|
*
|
|
* @return int
|
|
*/
|
|
private function getNewRevisionNumber($template)
|
|
{
|
|
$number = 0;
|
|
while (File::exists($template . '.' . $number)) {
|
|
$number++;
|
|
}
|
|
|
|
return $number;
|
|
}
|
|
|
|
/**
|
|
* If exists previously edited template, rename it to <template>.blade.php.<number>
|
|
*
|
|
* @param $template
|
|
*/
|
|
private function backupTemplates($template)
|
|
{
|
|
// get last sequence number
|
|
$number = $this->getNewRevisionNumber($template);
|
|
|
|
// nothing to backup
|
|
if ($number == 0) {
|
|
return;
|
|
}
|
|
|
|
// rename template
|
|
File::move($template, $template . '.' . $number);
|
|
}
|
|
|
|
/**
|
|
* Compare data from previous/original template
|
|
*
|
|
* @param $originalTemplate
|
|
* @param $customTemplate
|
|
* @param $currentContent
|
|
*
|
|
* @return bool
|
|
*/
|
|
private function isContentDifferent($originalTemplate, $customTemplate, $currentContent)
|
|
{
|
|
/**
|
|
* get last revision of template - we need to substract 1 from result from getNewRevisionNumber(),
|
|
* because this method returns number of first non-existing template
|
|
*/
|
|
$lastRevision = $this->getNewRevisionNumber($customTemplate) - 1;
|
|
|
|
// select compared file
|
|
if ($lastRevision < 0) {
|
|
$diffFile = $originalTemplate;
|
|
} else {
|
|
$diffFile = $customTemplate . '.' . $lastRevision;
|
|
}
|
|
|
|
// get content
|
|
$originalContent = file_get_contents($diffFile);
|
|
|
|
return $originalContent != $currentContent;
|
|
}
|
|
}
|