FEATURE #1373: add history of content
This commit contains only core funcionality of displaying history. Module settings must be implemented in each module.
This commit is contained in:
@@ -2,6 +2,10 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Handlers\Contracts\AppHandler;
|
||||
use App\Handlers\Contracts\HistoryHandler;
|
||||
use App\Handlers\Handler;
|
||||
use App\Helpers\Differ;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/**
|
||||
@@ -10,10 +14,8 @@ use Illuminate\Database\Eloquent\Model;
|
||||
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',
|
||||
@@ -22,8 +24,81 @@ class ContentHistory extends Model
|
||||
'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
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $change
|
||||
*
|
||||
* @return HistoryHandler
|
||||
*/
|
||||
protected function getHistoryHandler($change)
|
||||
{
|
||||
/**
|
||||
* @var $handler AppHandler
|
||||
* @var $historyHandler HistoryHandler
|
||||
*/
|
||||
|
||||
// get handler of given content page
|
||||
if ($change->column == 'module_settings') {
|
||||
$handler = $handler = $change->module_handler;
|
||||
} else {
|
||||
$handler = Handler::class;
|
||||
}
|
||||
|
||||
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($change->old_value, $change->new_value);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user