FEATURE: move bussiness logic out of models
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
<?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.module_handler', '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->module_handler;
|
||||
|
||||
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(e($change->old_value), e($change->new_value));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user