sarga/packages/Webkul/CMS/src/Http/Controllers/Admin/PageController.php

314 lines
8.5 KiB
PHP
Raw Normal View History

<?php
namespace Webkul\CMS\Http\Controllers\Admin;
use Webkul\CMS\Http\Controllers\Controller;
use Webkul\CMS\Repositories\CMSRepository as CMS;
use Webkul\Core\Repositories\ChannelRepository as Channel;
use Webkul\Core\Repositories\LocaleRepository as Locale;
/**
* CMS controller
*
* @author Prashant Singh <prashant.singh852@webkul.com> @prashant-webkul
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
*/
class PageController extends Controller
{
/**
* To hold the request variables from route file
*/
protected $_config;
/**
* To hold the channel reposotry instance
*/
protected $channel;
/**
* To hold the locale reposotry instance
*/
protected $locale;
/**
* To hold the CMSRepository instance
*/
protected $cms;
public function __construct(Channel $channel, Locale $locale, CMS $cms)
{
2019-08-02 14:21:28 +00:00
/**
* Pass the class instance through admin middleware
*/
2019-12-11 09:55:19 +00:00
// $this->middleware('auth:admin');
$this->middleware('admin');
2019-08-02 14:21:28 +00:00
/**
* Channel repository instance
*/
$this->channel = $channel;
/**
* Locale repository instance
*/
$this->locale = $locale;
/**
* CMS repository instance
*/
$this->cms = $cms;
$this->_config = request('_config');
}
/**
* Loads the index page showing the static pages resources
*/
public function index()
{
return view($this->_config['view']);
}
/**
* To create a new CMS page
*
* @return view
*/
public function create()
{
return view($this->_config['view']);
}
/**
* To store a new CMS page in storage
*
* @return view
*/
public function store()
{
2019-08-20 10:37:41 +00:00
$data = request()->all();
// part one of the validation in case partials pages were generated or generating partial pages
$this->validate(request(), [
'channels' => 'required',
'locales' => 'required',
'url_key' => 'required'
]);
2019-08-21 04:47:16 +00:00
$channels = $data['channels'];
$locales = $data['locales'];
2019-08-20 13:51:20 +00:00
2019-08-21 04:47:16 +00:00
$this->validate(request(), [
'html_content' => 'required|string',
'page_title' => 'required|string',
'meta_title' => 'required|string',
'meta_description' => 'string',
'meta_keywords' => 'required|string'
]);
2019-08-20 13:51:20 +00:00
2019-08-21 04:47:16 +00:00
$data['content']['html'] = $data['html_content'];
$data['content']['page_title'] = $data['page_title'];
$data['content']['meta_keywords'] = $data['meta_keywords'];
$data['content']['meta_title'] = $data['meta_title'];
$data['content']['meta_description'] = $data['meta_description'];
2019-08-20 10:37:41 +00:00
2019-08-21 04:47:16 +00:00
$data['content'] = json_encode($data['content']);
2019-08-20 10:37:41 +00:00
2019-08-21 04:47:16 +00:00
$totalCount = 0;
$actualCount = 0;
2019-08-20 10:37:41 +00:00
foreach ($channels as $channel) {
foreach ($locales as $locale) {
2019-08-21 04:47:16 +00:00
$pageFound = $this->cms->findOneWhere([
2019-08-20 10:37:41 +00:00
'channel_id' => $channel,
'locale_id' => $locale,
2019-08-21 04:47:16 +00:00
'url_key' => $data['url_key']
2019-08-20 10:37:41 +00:00
]);
2019-08-21 04:47:16 +00:00
$totalCount++;
$data['channel_id'] = $channel;
$data['locale_id'] = $locale;
2019-08-21 04:47:16 +00:00
if (! $pageFound) {
$result = $this->cms->create($data);
2019-08-21 04:47:16 +00:00
if ($result) {
$actualCount++;
}
}
2019-08-21 04:47:16 +00:00
unset($pageFound);
}
}
2019-08-21 04:47:16 +00:00
if (($actualCount != 0 && $totalCount != 0) && ($actualCount == $totalCount)) {
session()->flash('success', trans('admin::app.cms.pages.create-success'));
2019-08-21 04:47:16 +00:00
} else if (($actualCount != 0 && $totalCount != 0) && ($actualCount != $totalCount)) {
session()->flash('warning', trans('admin::app.cms.pages.create-partial'));
} else {
2019-08-21 04:47:16 +00:00
session()->flash('error', trans('admin::app.cms.pages.create-failure'));
}
return redirect()->route($this->_config['redirect']);
}
/**
* To edit a previously created CMS page
*
* @return view
*/
public function edit($id)
{
$page = $this->cms->findOrFail($id);
if (request()->has('channel') && request()->has('locale')) {
$channel = $this->channel->findOneWhere([
'code' => request()->input('channel')
]);
$locale = $this->locale->findOneWhere([
'code' => request()->input('locale')
]);
$page = $this->cms->findOneWhere([
'channel_id' => $channel->id,
'locale_id' => $locale->id,
'url_key' => $page->url_key
]);
if (! $page) {
$page = $this->cms->create([
'url_key' => str_random(8),
'channel' => $channel->code,
'locale' => $locale->code
]);
return redirect()->route('admin.cms.edit', $page->id);
}
} else {
$page = $this->cms->findOrFail($id);
}
return view($this->_config['view'])->with('page', $page);
}
/**
* To update the previously created CMS page in storage
*
* @param Integer $id
*
* @return View
*/
public function update($id)
{
$page = $this->cms->findOrFail($id);
$data = request()->all();
$this->validate(request(), [
'page_title' => 'required|string',
'html_content' => 'required|string',
'meta_title' => 'required|string',
'meta_description' => 'string',
'meta_keywords' => 'required|string'
]);
2019-08-02 14:21:28 +00:00
$data['content']['html'] = $data['html_content'];
$data['content']['page_title'] = $data['page_title'];
$data['content']['meta_keywords'] = $data['meta_keywords'];
$data['content']['meta_title'] = $data['meta_title'];
$data['content']['meta_description'] = $data['meta_description'];
$data['content'] = json_encode($data['content']);
$result = $this->cms->update($data, $id);
if ($result) {
session()->flash('success', trans('admin::app.cms.pages.update-success'));
} else {
session()->flash('success', trans('admin::app.cms.pages.update-failure'));
}
return redirect()->route($this->_config['redirect']);
}
/**
* To preview the content of the currently creating page or previously creating page
*
* @param Integer $id
*
* @return mixed
*/
public function preview($id)
{
$page = $this->cms->findOrFail($id);
2019-08-02 14:21:28 +00:00
return view('shop::cms.page')->with('page', $page);
}
/**
* To delete the previously create CMS page
*
* @param Integer $id
*
* @return Response JSON
*/
public function delete($id)
{
$page = $this->cms->findOrFail($id);
if ($page->delete()) {
2019-08-14 08:12:03 +00:00
session()->flash('success', trans('admin::app.cms.pages.delete-success'));
return response()->json(['message' => true], 200);
} else {
2019-08-14 08:12:03 +00:00
session()->flash('success', trans('admin::app.cms.pages.delete-failure'));
return response()->json(['message' => false], 200);
}
}
2019-08-17 10:12:24 +00:00
/**
* To mass delete the CMS resource from storage
*
* @return Response redirect
2019-08-17 10:12:24 +00:00
*/
public function massDelete()
{
$data = request()->all();
if ($data['indexes']) {
$pageIDs = explode(',', $data['indexes']);
$actualCount = count($pageIDs);
$count = 0;
foreach ($pageIDs as $pageId) {
$page = $this->cms->find($pageId);
if ($page) {
$page->delete();
$count++;
}
}
if ($actualCount == $count) {
session()->flash('success', trans('admin::app.datagrid.mass-ops.delete-success', [
'resource' => 'CMS Pages'
]));
} else {
session()->flash('success', trans('admin::app.datagrid.mass-ops.partial-action', [
'resource' => 'CMS Pages'
]));
}
} else {
session()->flash('warning', trans('admin::app.datagrid.mass-ops.no-resource'));
}
return redirect()->route('admin.cms.index');
}
}