User can edit static pages. The edit itself also stores history of changed columns.
122 lines
3.2 KiB
PHP
122 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Http\Requests\Request;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
/**
|
|
* Class Content
|
|
*/
|
|
class Content extends Model
|
|
{
|
|
public $timestamps = true;
|
|
protected $fillable = [
|
|
'user_id',
|
|
'module_handler',
|
|
'published_from',
|
|
'published_to',
|
|
'title_photo',
|
|
'title',
|
|
'url',
|
|
'meta_keywords',
|
|
'meta_description',
|
|
'content',
|
|
'module_settings',
|
|
'status'
|
|
];
|
|
protected $guarded = [];
|
|
const STATUS_DELETED = 'deleted';
|
|
const STATUS_DRAFT = 'draft';
|
|
const STATUS_PROTECTED = 'protected';
|
|
const STATUS_PUBLIC = 'public';
|
|
|
|
/**
|
|
* Getter for overview of all content pages of given handler (module type)
|
|
*
|
|
* @param $handler
|
|
* @param bool $joinAuthor
|
|
*
|
|
*/
|
|
public function getByType($handler, $joinAuthor = false)
|
|
{
|
|
$query = DB::table('contents')
|
|
->select('contents.*')
|
|
->where('contents.status', '<>', self::STATUS_DELETED)
|
|
->where('module_handler', $handler)
|
|
->orderBy('created_at');
|
|
|
|
// join user email and user name to results when needed
|
|
if ($joinAuthor) {
|
|
$query = $query->join('users', 'users.id', '=', 'contents.user_id')
|
|
->select('contents.*', 'users.name AS author_name', 'users.email AS author_email');
|
|
}
|
|
|
|
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
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
*/
|
|
public function author()
|
|
{
|
|
return $this->belongsTo('App\Models\User', 'user_id');
|
|
}
|
|
} |