REFACTORING #1880 @4h30m: another ugly refactoring commit

This commit is contained in:
Michal Kváček
2016-10-25 16:50:04 +02:00
parent 28f67ffe3c
commit cc999dd24e
71 changed files with 827 additions and 608 deletions
+32 -23
View File
@@ -29,14 +29,14 @@ class Content extends Repository implements IContent
return ContentModel::where('status', '!=', IContent::STATUS_DRAFT)
->select('contents.*')
->with('author')
->join('modules', 'modules.handler', '=', 'contents.module_handler')
->join('modules', 'modules.handler', '=', 'contents.moduleHandler')
->where(function ($query) {
$query->where('published_from', '<', Carbon::now())
->orWhereNull('published_from');
$query->where('publishedFrom', '<=', Carbon::now())
->orWhereNull('publishedFrom');
})
->where(function ($query) {
$query->where('published_to', '>', Carbon::now())
->orWhereNull('published_to');
$query->where('publishedTo', '>=', Carbon::now())
->orWhereNull('publishedTo');
})
->where('language', LaravelLocalization::getCurrentLocale())
->whereNull('contents.deleted_at')
@@ -46,9 +46,14 @@ class Content extends Repository implements IContent
public function all()
{
return $this->getPageQuery()
->get();
->get();
}
/**
* @param int $id
*
* @return mixed
*/
public function getOne($id = null)
{
if (is_null($id)) {
@@ -56,16 +61,9 @@ class Content extends Repository implements IContent
$page->user_id = \Auth::user()->id;
$page->language = LaravelLocalization::getCurrentLocale();
$page->status = IContent::STATUS_DRAFT;
$page->module_settings = [];
$page->moduleSettings = [];
} else {
if (is_numeric($id)) {
$page = $this->model->findOrFail($id);
} else {
$page = $this->model->where('language', LaravelLocalization::getCurrentLocale())
->where('url', $id)
->firstOrFail();
}
$page->module_settings = json_decode($page->module_settings);
}
return $page;
@@ -78,8 +76,9 @@ class Content extends Repository implements IContent
public function getHomepage()
{
return $this->getPageQuery()
->where('is_homepage', 1)
->first();
->where('isHomepage', 1)
->first();
}
/**
@@ -91,9 +90,17 @@ class Content extends Repository implements IContent
*/
public function getByURL($url)
{
return $this->getPageQuery()
->where('url', $url)
->first();
$page = $this->getPageQuery()
->where('url', $url)
->first();
if (is_null($page)) {
return null;
}
$page->moduleSettings = json_decode($page->moduleSettings);
return $page;
}
/**
@@ -108,8 +115,8 @@ class Content extends Repository implements IContent
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.module_handler')
->join('users', 'users.id', '=', 'contents.user_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')
@@ -119,7 +126,7 @@ class Content extends Repository implements IContent
// filter results using handler
if (!is_null($handler)) {
$query = $query->where('contents.module_handler', $handler);
$query = $query->where('contents.moduleHandler', $handler);
}
// add filtering by status
@@ -127,6 +134,8 @@ class Content extends Repository implements IContent
$query = $query->whereIn('contents.status', $statuses);
}
return $query->paginate(20);
$pages = $query->paginate(setting('paginatedResults', 20));
return $pages;
}
}
+12 -3
View File
@@ -36,8 +36,8 @@ class Module extends Repository implements IModule
public function getUpdateContentTitle(ModuleModel $module, Content $content = null)
{
$pageHeading = trans('admin/contents.new', ['name' => trans($module->name)]);
$moduleHeading = $this->call($module, 'getEditTitle', $content);
$pageHeading = trans('admin/contents.edit', ['name' => trans($module->name)]);
$moduleHeading = $this->call($module, 'getEditTitle', [$content]);
return is_null($moduleHeading) ? $pageHeading : $moduleHeading;
}
@@ -62,7 +62,7 @@ class Module extends Repository implements IModule
->firstOrFail();
}
public function call(ModuleModel $module, $method, ...$parameters)
public function call(ModuleModel $module, $method, $parameters = [])
{
if (!is_null($module) && is_callable([$module->handler, $method])) {
return call_user_func_array([$module->handler, $method], $parameters);
@@ -70,4 +70,13 @@ class Module extends Repository implements IModule
return null;
}
public function callOnString($moduleHandler, $method, $parameters = [])
{
if (is_callable([$moduleHandler, $method])) {
return call_user_func_array([$moduleHandler, $method], $parameters);
}
return null;
}
}