Files
core/app/CMS/Repositories/Content/Content.php
T

132 lines
4.1 KiB
PHP

<?php
namespace App\CMS\Repositories\Content;
use App\CMS\Contracts\Repositories\Content as IContent;
use App\CMS\Enumerations\ContentStatuses;
use App\CMS\Repositories\Repository;
use App\Models\Content\Content as ContentModel;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
class Content extends Repository implements IContent
{
/**
* @var ContentModel
*/
protected $model;
public function __construct(ContentModel $model)
{
$this->model = $model;
}
/**
* Getter for query with setup filter for non-deleted and non-draft pages
*/
protected function getPageQuery()
{
return ContentModel::where('status', '!=', IContent::STATUS_DRAFT)
->select('contents.*')
->with('author')
->join('modules', 'modules.handler', '=', 'contents.moduleHandler')
->where(function ($query) {
$query->where('publishedFrom', '<=', Carbon::now())
->orWhereNull('publishedFrom');
})
->where(function ($query) {
$query->where('publishedTo', '>=', Carbon::now())
->orWhereNull('publishedTo');
})
->where('language', LaravelLocalization::getCurrentLocale())
->whereNull('contents.deleted_at')
->where('modules.enabled', true);
}
public function all()
{
return $this->getPageQuery()
->get();
}
/**
* @param int $id
*
* @return mixed
*/
public function getOne($id = null)
{
if (is_null($id)) {
$page = new ContentModel();
$page->user_id = \Auth::user()->id;
$page->language = LaravelLocalization::getCurrentLocale();
$page->status = IContent::STATUS_DRAFT;
$page->moduleSettings = [];
} else {
$page = $this->model->findOrFail($id);
}
return $page;
}
/**
* Getter for homepage content page
* @return Content
*/
public function getHomepage()
{
return $this->getPageQuery()
->where('isHomepage', 1)
->first();
}
/**
* Getter for any page specified by its url.
*
* @param $url
*
* @return Content
*/
public function getByURL($url)
{
return $this->getPageQuery()
->where('url', $url)
->first();
}
/**
* Getter for overview of all content pages.
* These content pages may be filtered by using some handler
*
* @param bool $handler
* @param null $statuses
*
* @return
*/
public function getOverview($handler = null, $statuses = null)
{
$query = ContentModel::select('contents.*')
->join('modules', 'modules.handler', '=', 'contents.moduleHandler')
->join('users', 'users.id', '=', 'contents.userId')
->whereNull('contents.deleted_at')
->whereNull('contents.parentId')
->where('contents.language', LaravelLocalization::getCurrentLocale())
->where('modules.enabled', 1)
->with('module', 'subpages', 'templateModel')
->orderBy('contents.order')
->orderBy('contents.created_at', 'DESC');
// filter results using handler
if (!is_null($handler)) {
$query = $query->where('contents.moduleHandler', $handler);
}
// add filtering by status
if (is_array($statuses) && in_array($statuses, ContentStatuses::getSimple())) {
$query = $query->whereIn('contents.status', $statuses);
}
return $query;
}
}