Files
core/app/Models/Content/ModelTraits/ContentHistory.php
T

83 lines
2.3 KiB
PHP

<?php
namespace App\Models\Content\ModelTraits;
use App\CMS\Handlers\Contracts\AppHandler;
use App\CMS\Handlers\Contracts\HistoryHandler;
use App\CMS\Helpers\Differ;
use App\Models\Content\Content;
trait ContentHistory
{
/**
* Returns history grouped by revisions (edit_batches)
*
* @param Content $content
*
* @return array
*/
public function getStructuredHistoryOf(Content $content)
{
$data = $content->history()
->select('content_history.*', 'contents.moduleHandler', 'users.email AS user_email', 'users.name AS user_name')
->join('users', 'users.id', '=', 'content_history.user_id')
->join('contents', 'contents.id', '=', 'content_history.content_id')
->orderBy('changed_at', 'DESC')
->get();
$info = [];
$history = [];
foreach ($data as $change) {
$info[$change->edit_batch] = [
'changed_at' => $change->changed_at,
'user' => [
'id' => $change->user_id,
'name' => $change->user_name,
'email' => $change->user_email
]
];
$change = $this->handleChangeset($change);
$history[$change->edit_batch][] = $change;
}
return [
'changesets' => $history,
'info' => $info
];
}
/**
* Getter for history handler of given module
*
* @param $change
*
* @return HistoryHandler
*/
protected function getHistoryHandler($change)
{
/**
* @var $handler AppHandler
*/
// get handler of given content page
$handler = $change->moduleHandler;
return $handler::getHistoryHandler();
}
public function handleChangeset($change)
{
$historyHandler = $this->getHistoryHandler($change);
$change->column_trans = $historyHandler::parseColumnName($change->column);
$change->diff = $historyHandler::parseChangedValue($change);
return $change;
}
public function getDiff($change)
{
return Differ::htmlDiff(nl2br(e($change->old_value)), nl2br(e($change->new_value)));
}
}