141 lines
4.3 KiB
PHP
141 lines
4.3 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)
|
|
{
|
|
$page = $this->getPageQuery()
|
|
->where('url', $url)
|
|
->first();
|
|
|
|
if (is_null($page)) {
|
|
return null;
|
|
}
|
|
|
|
$page->moduleSettings = json_decode($page->moduleSettings);
|
|
|
|
return $page;
|
|
}
|
|
|
|
/**
|
|
* 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.*', 'modules.name AS module_name', 'modules.id AS module_id')
|
|
->join('modules', 'modules.handler', '=', 'contents.moduleHandler')
|
|
->join('users', 'users.id', '=', 'contents.userId')
|
|
->addSelect(DB::raw('COALESCE(users.name, users.email) AS author_name'))
|
|
->addSelect('users.email AS author_email')
|
|
->whereNull('contents.deleted_at')
|
|
->where('contents.language', LaravelLocalization::getCurrentLocale())
|
|
->where('modules.enabled', 1)
|
|
->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);
|
|
}
|
|
|
|
$pages = $query->paginate(setting('paginatedResults', 20));
|
|
|
|
return $pages;
|
|
}
|
|
} |