FEATURE #1372: add feature for editing static pages.
User can edit static pages. The edit itself also stores history of changed columns.
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Http\Requests\Request;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
@@ -55,6 +56,60 @@ class Content extends Model
|
||||
return $query;
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates current model with information about changed columns
|
||||
*
|
||||
* @param array $fields
|
||||
* @param Request $request
|
||||
*
|
||||
* @return int number of changed columns
|
||||
*/
|
||||
public function updateWithHistory(array $fields, Request $request)
|
||||
{
|
||||
$editBatch = uniqid();
|
||||
$changed = 0;
|
||||
|
||||
foreach ($fields as $fieldDB => $fieldRequest) {
|
||||
// remove any whitespaces around real content
|
||||
$request->$fieldRequest = trim($request->$fieldRequest);
|
||||
|
||||
if ($this->$fieldDB != $request->$fieldRequest) {
|
||||
|
||||
// setup history object
|
||||
$history = [
|
||||
'user_id' => \Auth::user()->id,
|
||||
'content_id' => $this->id,
|
||||
'edit_batch' => $editBatch,
|
||||
'column' => $fieldRequest,
|
||||
'old_value' => $this->$fieldDB,
|
||||
'new_value' => $request->$fieldRequest
|
||||
];
|
||||
|
||||
// save history
|
||||
ContentHistory::create($history);
|
||||
|
||||
// save change into database
|
||||
$this->$fieldDB = $request->$fieldRequest;
|
||||
|
||||
$changed++;
|
||||
}
|
||||
}
|
||||
|
||||
$this->save();
|
||||
|
||||
return $changed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark relationship with its historical records
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
||||
*/
|
||||
public function history()
|
||||
{
|
||||
return $this->hasMany('App\Models\ContentHistory');
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark relationship with users table
|
||||
*
|
||||
|
||||
@@ -9,13 +9,14 @@ use Illuminate\Database\Eloquent\Model;
|
||||
*/
|
||||
class ContentHistory extends Model
|
||||
{
|
||||
protected $table = 'content_history';
|
||||
|
||||
public $timestamps = true;
|
||||
// this table does not have created_at and updated_at fields
|
||||
public $timestamps = false;
|
||||
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
'content_id',
|
||||
'changed_at',
|
||||
'edit_batch',
|
||||
'column',
|
||||
'new_value',
|
||||
|
||||
Reference in New Issue
Block a user