FEATURE: change directory structure
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Content;
|
||||
|
||||
use App\CMS\Handlers\Contracts\AppHandler;
|
||||
use App\CMS\Handlers\Contracts\HistoryHandler;
|
||||
use App\CMS\Helpers\Differ;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/**
|
||||
* Class ContentHistory
|
||||
*/
|
||||
class ContentHistory extends Model
|
||||
{
|
||||
protected $table = 'content_history';
|
||||
// this table does not have created_at and updated_at fields
|
||||
public $timestamps = false;
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
'content_id',
|
||||
'edit_batch',
|
||||
'column',
|
||||
'new_value',
|
||||
'old_value'
|
||||
];
|
||||
protected $guarded = [];
|
||||
|
||||
/**
|
||||
* 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