sarga/packages/Webkul/Admin/src/Http/Controllers/ConfigurationController.php

146 lines
3.3 KiB
PHP
Raw Normal View History

2018-11-27 04:46:35 +00:00
<?php
namespace Webkul\Admin\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Event;
use Webkul\Admin\Facades\Configuration;
2018-11-27 04:46:35 +00:00
use Webkul\Core\Repositories\CoreConfigRepository as CoreConfig;
2018-12-14 09:32:59 +00:00
use Webkul\Core\Tree;
use Webkul\Admin\Http\Requests\ConfigurationForm;
2018-11-27 04:46:35 +00:00
/**
* Configuration controller
*
* @author Jitendra Singh <jitendra@webkul.com>
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
*/
class ConfigurationController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
protected $_config;
/**
* CoreConfigRepository object
*
* @var array
*/
protected $coreConfig;
2018-12-14 09:32:59 +00:00
/**
*
* @var array
*/
protected $configTree;
2018-11-27 04:46:35 +00:00
/**
* Create a new controller instance.
*
* @param Webkul\Core\Repositories\CoreConfigRepository $coreConfig
* @return void
*/
public function __construct(CoreConfig $coreConfig)
{
$this->middleware('admin');
2018-12-19 09:30:40 +00:00
$this->coreConfig = $coreConfig;
2018-11-27 04:46:35 +00:00
$this->_config = request('_config');
2018-12-14 09:32:59 +00:00
$this->prepareConfigTree();
}
/**
* Prepares config tree
*
* @return void
*/
public function prepareConfigTree()
{
$tree = Tree::create();
2019-01-15 11:54:41 +00:00
foreach (config('core') as $item) {
2018-12-14 09:32:59 +00:00
$tree->add($item);
}
$tree->items = core()->sortItems($tree->items);
$this->configTree = $tree;
2018-11-27 04:46:35 +00:00
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$slugs = $this->getDefaultConfigSlugs();
2018-12-14 09:32:59 +00:00
2019-01-15 11:54:41 +00:00
if (count($slugs)) {
2018-12-26 09:16:27 +00:00
return redirect()->route('admin.configuration.index', $slugs);
}
2018-12-14 09:32:59 +00:00
return view($this->_config['view'], ['config' => $this->configTree]);
}
/**
* Returns slugs
*
* @return array
*/
public function getDefaultConfigSlugs()
{
$slugs = [];
2019-01-15 11:54:41 +00:00
if (! request()->route('slug')) {
$firstItem = current($this->configTree->items);
$secondItem = current($firstItem['children']);
$temp = explode('.', $secondItem['key']);
2018-12-14 09:32:59 +00:00
$slugs = [
'slug' => current($temp),
'slug2' => end($temp)
];
} else {
2019-01-15 11:54:41 +00:00
if (! request()->route('slug2')) {
$secondItem = current($this->configTree->items[request()->route('slug')]['children']);
$temp = explode('.', $secondItem['key']);
$slugs = [
'slug' => current($temp),
'slug2' => end($temp)
];
}
}
return $slugs;
}
/**
* Store a newly created resource in storage.
*
* @param \Webkul\Admin\Http\Requests\ConfigurationForm $request
* @return \Illuminate\Http\Response
*/
2019-01-04 05:43:24 +00:00
public function store()
{
Event::fire('core.configuration.save.after');
2018-12-14 06:26:52 +00:00
$this->coreConfig->create(request()->all());
Event::fire('core.configuration.save.after');
2019-01-17 14:52:01 +00:00
session()->flash('success', trans('admin::app.configuration.save-message'));
2018-12-19 09:30:40 +00:00
return redirect()->back();
2018-11-27 04:46:35 +00:00
}
}