FEATURE #1507: modularize content management
This commit is contained in:
+33
-13
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Enumerations\ContentStatuses;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
@@ -67,24 +68,39 @@ class Content extends Model
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for overview of all content pages of given handler (module type)
|
||||
* Getter for overview of all content pages.
|
||||
*
|
||||
* @param $handler
|
||||
* These content pages may be filtered by using some handler
|
||||
*
|
||||
* @param bool $handler
|
||||
* @param bool $status
|
||||
* @param bool $joinAuthor
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public function getByType($handler, $joinAuthor = false)
|
||||
public function getOverview($handler = false, $status = false, $joinAuthor = false)
|
||||
{
|
||||
$query = DB::table('contents')
|
||||
->select('contents.*')
|
||||
->select('contents.*', 'modules.name AS module_name')
|
||||
->where('contents.status', '<>', self::STATUS_DELETED)
|
||||
->where('module_handler', $handler)
|
||||
->orderBy('created_at');
|
||||
->join('modules', 'modules.handler', '=', 'contents.module_handler')
|
||||
->orderBy('created_at', 'DESC');
|
||||
|
||||
// filter results using handler
|
||||
if ($handler) {
|
||||
$query = $query->where('module_handler', $handler);
|
||||
}
|
||||
|
||||
// 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');
|
||||
->addSelect('users.name AS author_name')
|
||||
->addSelect('users.email AS author_email');
|
||||
}
|
||||
|
||||
// add filtering by status
|
||||
if (in_array($status, ContentStatuses::getSimple())) {
|
||||
$query = $query->where('contents.status', $status);
|
||||
}
|
||||
|
||||
return $query;
|
||||
@@ -93,14 +109,18 @@ class Content extends Model
|
||||
/**
|
||||
* Updates current model with information about changed columns
|
||||
*
|
||||
* @param array $fields
|
||||
* @param array $request
|
||||
* @param array $fields
|
||||
* @param array $request
|
||||
* @param string $revisionId
|
||||
*
|
||||
* @return int number of changed columns
|
||||
*/
|
||||
public function updateWithHistory(array $fields, array $request)
|
||||
public function updateWithHistory(array $fields, array $request, $revisionId = null)
|
||||
{
|
||||
$editBatch = uniqid();
|
||||
if (!$revisionId) {
|
||||
$revisionId = uniqid();
|
||||
}
|
||||
|
||||
$changed = 0;
|
||||
|
||||
foreach ($fields as $fieldDB => $fieldRequest) {
|
||||
@@ -118,7 +138,7 @@ class Content extends Model
|
||||
$history = [
|
||||
'user_id' => \Auth::user()->id,
|
||||
'content_id' => $this->id,
|
||||
'edit_batch' => $editBatch,
|
||||
'edit_batch' => $revisionId,
|
||||
'column' => $fieldRequest,
|
||||
'old_value' => $this->$fieldDB,
|
||||
'new_value' => $request[$fieldRequest]
|
||||
@@ -136,7 +156,7 @@ class Content extends Model
|
||||
|
||||
$this->save();
|
||||
|
||||
return [$editBatch, $changed];
|
||||
return [$revisionId, $changed];
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user