REFACTORING: move part of content business logic into repository

This commit is contained in:
Michal Kváček
2016-10-18 15:36:26 +02:00
parent eabbf48ee0
commit 662700b4f2
19 changed files with 504 additions and 383 deletions
+28 -84
View File
@@ -1,24 +1,20 @@
<?php
namespace App\Models\Content\ModelTraits;
use App\CMS\Enumerations\ContentStatuses;
use App\CMS\Contracts\Repositories\Content as IContent;
use App\Http\Requests\Request;
use App\Models\Content\ContentHasFile;
use App\Models\Content\ContentHistory;
use App\Models\FileManager\Directory;
use App\Models\FileManager\File;
use Carbon\Carbon;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\DB;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
use App\Models\Content\Content as ContentModel;
trait Content {
trait Content
{
/**
* @var File
*/
private $file;
protected $foundSubpages = [];
protected $subpagesInit = false;
@@ -28,49 +24,43 @@ trait Content {
}
/**
* Getter for query with setup filter for non-deleted and non-draft pages
* Is current content draft?
*
* @return bool
*/
public function getPageQuery()
public function isDraft()
{
return ContentModel::where('status', '!=', ContentModel::STATUS_DRAFT)
->select('contents.*')
->with('author')
->join('modules', 'modules.handler', '=', 'contents.module_handler')
->where(function ($query) {
$query->where('published_from', '<', Carbon::now())
->orWhereNull('published_from');
})
->where(function ($query) {
$query->where('published_to', '>', Carbon::now())
->orWhereNull('published_to');
})
->where('language', LaravelLocalization::getCurrentLocale())
->where('modules.enabled', true);
return $this->status == IContent::STATUS_DRAFT;
}
/**
* Getter for homepage content page
* @return Content
* Is current content visible for all visitors?
*
* @return bool
*/
public function getHomepage()
public function isPublic()
{
return $this->getPageQuery()
->where('is_homepage', 1)
->first();
return $this->status == IContent::STATUS_PUBLIC;
}
/**
* Getter for any page specified by its url.
* Is current content visible only for logged users?
*
* @param $url
*
* @return Content
* @return bool
*/
public function getByURL($url)
public function isProtected()
{
return $this->getPageQuery()
->where('url', $url)
->first();
return $this->status == IContent::STATUS_PROTECTED;
}
/**
* Is current content visible for logged user OR for everyone?
*
* @return bool
*/
public function isVisible()
{
return $this->isPublic() || $this->isProtected();
}
/**
@@ -122,53 +112,6 @@ trait Content {
return $rootPages->get();
}
/**
* Getter for overview of all content pages.
* These content pages may be filtered by using some handler
*
* @param bool $handler
* @param bool $status
* @param bool $joinAuthor
* @param bool $joinParent
*
* @return
*/
public function getOverview($handler = false, $status = false, $joinAuthor = false, $joinParent = false)
{
$query = DB::table('contents')
->select('contents.*', 'modules.name AS module_name', 'modules.id AS module_id')
->join('modules', 'modules.handler', '=', 'contents.module_handler')
->whereNull('contents.deleted_at')
->where('contents.language', LaravelLocalization::getCurrentLocale())
->where('modules.enabled', 1)
->orderBy('created_at', 'DESC');
// filter results using handler
if ($handler) {
$query = $query->where('contents.module_handler', $handler);
}
// join user email and user name to results when needed
if ($joinAuthor) {
$query = $query->join('users', 'users.id', '=', 'contents.user_id')
->addSelect(DB::raw('COALESCE(users.name, users.email) AS author_name'))
->addSelect('users.email AS author_email');
}
if ($joinParent) {
$query = $query->leftJoin('contents as parent', 'parent.id', '=', 'contents.parent_id')
->addSelect('parent.title AS parent_title')
->addSelect('parent.url AS parent_url');
}
// add filtering by status
if (in_array($status, ContentStatuses::getSimple())) {
$query = $query->where('contents.status', $status);
}
return $query;
}
/**
* Updates current model with information about changed columns
*
@@ -329,6 +272,7 @@ trait Content {
return count($allFiles);
}
/**
* Save history entry into database
*