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

158 lines
3.6 KiB
PHP
Raw Normal View History

2018-11-27 04:46:35 +00:00
<?php
namespace Webkul\Admin\Http\Controllers;
use Illuminate\Support\Facades\Event;
2019-07-01 11:33:36 +00:00
use Webkul\Core\Repositories\CoreConfigRepository;
2018-12-14 09:32:59 +00:00
use Webkul\Core\Tree;
2019-01-31 09:48:36 +00:00
use Illuminate\Support\Facades\Storage;
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
*/
2019-07-01 11:33:36 +00:00
protected $coreConfigRepository;
2018-11-27 04:46:35 +00:00
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.
*
2020-03-05 05:34:57 +00:00
* @param \Webkul\Core\Repositories\CoreConfigRepository $coreConfigRepository
2018-11-27 04:46:35 +00:00
* @return void
*/
2019-07-01 11:33:36 +00:00
public function __construct(CoreConfigRepository $coreConfigRepository)
2018-11-27 04:46:35 +00:00
{
$this->middleware('admin');
2019-07-01 11:33:36 +00:00
$this->coreConfigRepository = $coreConfigRepository;
2018-12-19 09:30:40 +00:00
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.
*
2019-08-19 09:30:24 +00:00
* @return \Illuminate\View\View
2018-11-27 04:46:35 +00:00
*/
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()
{
2019-01-15 11:54:41 +00:00
if (! request()->route('slug')) {
$firstItem = current($this->configTree->items);
$secondItem = current($firstItem['children']);
return $this->getSlugs($secondItem);
}
if (! request()->route('slug2')) {
$secondItem = current($this->configTree->items[request()->route('slug')]['children']);
return $this->getSlugs($secondItem);
}
2019-06-06 11:55:06 +00:00
return [];
}
/**
* Store a newly created resource in storage.
*
2020-03-05 05:34:57 +00:00
* @param \Webkul\Admin\Http\Requests\ConfigurationForm $request
* @return \Illuminate\Http\Response
*/
public function store(ConfigurationForm $request)
{
2019-12-24 14:01:13 +00:00
Event::dispatch('core.configuration.save.before');
2019-07-01 11:33:36 +00:00
$this->coreConfigRepository->create(request()->all());
2019-12-24 14:01:13 +00:00
Event::dispatch('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
}
2019-01-31 09:48:36 +00:00
/**
* download the file for the specified resource.
*
* @return \Illuminate\Http\Response
*/
2019-01-31 11:34:32 +00:00
public function download()
2019-01-31 09:48:36 +00:00
{
2019-01-31 11:34:32 +00:00
$path = request()->route()->parameters()['path'];
2019-01-31 09:48:36 +00:00
2019-01-31 11:34:32 +00:00
$fileName = 'configuration/'. $path;
2019-07-01 11:33:36 +00:00
$config = $this->coreConfigRepository->findOneByField('value', $fileName);
2019-01-31 09:48:36 +00:00
return Storage::download($config['value']);
}
/**
2020-03-05 05:34:57 +00:00
* @param string $secondItem
* @return array
*/
private function getSlugs($secondItem): array
{
$temp = explode('.', $secondItem['key']);
return ['slug' => current($temp), 'slug2' => end($temp)];
}
2018-11-27 04:46:35 +00:00
}