FEATURE: add homepage settings for base of one page pages

This commit is contained in:
Michal Kváček
2016-09-23 22:27:58 +02:00
parent fbb59277b9
commit 7589051216
13 changed files with 333 additions and 21 deletions
@@ -0,0 +1,132 @@
<?php
namespace Templates\Base\OnePage\Http\Controllers\Admin;
use App\Helpers\Settings;
use App\Http\Controllers\Admin\Controller;
use App\Models\Content;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
use Templates\Base\OnePage\Handlers\OnePage;
class SetHomepage extends Controller
{
/**
* @var Content
*/
private $content;
/**
* SetHomepage constructor.
*
* @param Content $content
*/
public function __construct(Content $content)
{
parent::__construct();
$this->content = $content;
}
/**
* Display form for organizing pages on homepage
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function getPages()
{
// get pages which are assigned as homepage parts
$usedPagesIds = (array)json_decode(Settings::get(OnePage::HOMEPAGE_PAGES_SETTINGS_KEY, '{}'));
// get all pages - they will be separated for used and not used on next lines
$allPages = $this->content->getOverview()
->get();
// separate pages to used and not used - with help of $usedPAgesIds array defined above
$pages = $used = $notUsed = [];
foreach ($allPages as $page) {
$pages[$page->id] = $page;
if (in_array($page->id, $usedPagesIds)) {
$used[$page->id] = $page;
} else {
$notUsed[$page->id] = $page;
}
}
return view('template::admin/homepage.set', [
'usedPagesIds' => $usedPagesIds,
'allPages' => $pages,
'usedPages' => $used,
'notUsedPages' => $notUsed
]);
}
/**
* Add page into homepage
*
* @param $id
*
* @return \Illuminate\Http\RedirectResponse
*/
public function addPage($id)
{
// try to get page config
$settingsEntry = \App\Models\Settings::where('key', OnePage::HOMEPAGE_PAGES_SETTINGS_KEY)
->where('language', LaravelLocalization::getCurrentLocale())
->first();
if (!$settingsEntry) {
$settingsEntry = new \App\Models\Settings();
$settingsEntry->language = LaravelLocalization::getCurrentLocale();
$settingsEntry->key = OnePage::HOMEPAGE_PAGES_SETTINGS_KEY;
$usedPagesIds = [];
} else {
$usedPagesIds = (array)json_decode($settingsEntry->value);
}
if (!in_array($id, $usedPagesIds)) {
$usedPagesIds[] = $id;
}
$settingsEntry->value = json_encode($usedPagesIds);
$settingsEntry->save();
return redirect()
->back()
->with('success', trans('template::admin/setHomepage.successfullyAdded'));
}
/**
* Remove page from homepage
*
* @param $id
*
* @return \Illuminate\Http\RedirectResponse
*/
public function removePage($id)
{
$pages = (array)json_decode(Settings::get(OnePage::HOMEPAGE_PAGES_SETTINGS_KEY, '{}'));
if (!in_array($id, $pages)) {
return redirect()
->back()
->with('error', trans('template::admin/setHomepage.invalidPageId'));
} else {
$pagePosition = array_search($id, $pages);
// remove page from config array
unset($pages[$pagePosition]);
$pages = array_values($pages);
// update in database
\App\Models\Settings::where('key', OnePage::HOMEPAGE_PAGES_SETTINGS_KEY)
->where('language', LaravelLocalization::getCurrentLocale())
->update(['value' => json_encode($pages)]);
return redirect()
->back()
->with('success', trans('template::admin/setHomepage.successfullyRemoved'));
}
}
}