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

269 lines
11 KiB
PHP
Raw Normal View History

2020-01-26 08:47:05 +00:00
<?php
namespace Webkul\Velocity\Http\Controllers\Admin;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
2020-01-26 08:47:05 +00:00
use Webkul\Velocity\Repositories\VelocityMetadataRepository;
class ConfigurationController extends Controller
{
/**
* VelocityMetadataRepository object
*
2020-03-05 13:37:08 +00:00
* @var \Webkul\Velocity\Repositories\VelocityMetadataRepository
2020-01-26 08:47:05 +00:00
*/
protected $velocityMetaDataRepository;
2020-08-17 05:58:12 +00:00
/**
* Locale
*/
2020-06-11 09:32:39 +00:00
protected $locale;
2020-08-17 05:58:12 +00:00
/**
* Channel
*/
protected $channel;
2020-01-26 08:47:05 +00:00
/**
* Create a new controller instance.
*
2020-03-05 13:37:08 +00:00
* @param \Webkul\Velocity\Repositories\MetadataRepository $velocityMetaDataRepository
* @return void
2020-01-26 08:47:05 +00:00
*/
2020-03-05 13:37:08 +00:00
public function __construct (VelocityMetadataRepository $velocityMetadataRepository)
{
2020-01-26 08:47:05 +00:00
$this->_config = request('_config');
2020-03-04 06:11:09 +00:00
$this->velocityHelper = app('Webkul\Velocity\Helpers\Helper');
2020-01-26 08:47:05 +00:00
$this->velocityMetaDataRepository = $velocityMetadataRepository;
2020-06-11 09:32:39 +00:00
$this->locale = request()->get('locale') ?: app()->getLocale();
2020-08-17 05:58:12 +00:00
$this->channel = request()->get('channel') ?: 'default';
2020-01-26 08:47:05 +00:00
}
2020-03-05 13:37:08 +00:00
/**
* @return \Illuminate\View\View
*/
2020-01-26 08:47:05 +00:00
public function renderMetaData()
{
2020-08-17 05:58:12 +00:00
$velocityMetaData = $this->velocityHelper->getVelocityMetaData($this->locale, $this->channel, false);
2020-06-11 09:32:39 +00:00
if (! $velocityMetaData) {
2020-08-17 05:58:12 +00:00
$this->createMetaData($this->locale, $this->channel);
2020-06-11 09:32:39 +00:00
2020-08-17 05:58:12 +00:00
$velocityMetaData = $this->velocityHelper->getVelocityMetaData($this->locale, $this->channel);
2020-02-12 11:16:06 +00:00
}
2020-01-26 08:47:05 +00:00
2020-06-11 09:32:39 +00:00
$velocityMetaData->advertisement = $this->manageAddImages(json_decode($velocityMetaData->advertisement, true) ?: []);
2020-01-26 08:47:05 +00:00
return view($this->_config['view'], [
2020-03-04 06:32:53 +00:00
'metaData' => $velocityMetaData,
2020-01-26 08:47:05 +00:00
]);
}
2020-03-05 13:37:08 +00:00
/**
* @param int $id
* @return \Illuminate\Http\Response
*/
2020-01-26 08:47:05 +00:00
public function storeMetaData($id)
{
// check if radio button value
if (request()->get('slides') == "on") {
$params = request()->all() + [
2020-03-04 06:32:53 +00:00
'slider' => 1,
2020-01-26 08:47:05 +00:00
];
} else {
$params = request()->all() + [
2020-03-04 06:32:53 +00:00
'slider' => 0,
2020-01-26 08:47:05 +00:00
];
}
2020-06-11 05:41:38 +00:00
$velocityMetaData = $this->velocityMetaDataRepository->findOneWhere([
2020-06-11 09:32:39 +00:00
'id' => $id,
2020-06-11 05:41:38 +00:00
]);
$advertisement = json_decode($velocityMetaData->advertisement, true);
2020-01-26 08:47:05 +00:00
$params['advertisement'] = [];
2020-02-20 07:38:52 +00:00
if (isset($params['images'])) {
foreach ($params['images'] as $index => $images) {
$params['advertisement'][$index] = $this->uploadAdvertisementImages($images, $index, $advertisement);
2020-01-26 08:47:05 +00:00
}
2020-02-13 08:30:59 +00:00
2020-02-20 07:38:52 +00:00
if ($advertisement) {
2020-02-13 08:30:59 +00:00
foreach ($advertisement as $key => $image_array) {
if (! isset($params['images'][$key])) {
foreach ($advertisement[$key] as $image) {
Storage::delete($image);
}
}
}
}
2020-01-26 08:47:05 +00:00
}
2020-02-04 14:29:38 +00:00
if (isset($params['product_view_images'])) {
foreach ($params['product_view_images'] as $index => $productViewImage) {
if ($productViewImage !== "") {
$params['product_view_images'][$index] = $this->uploadImage($productViewImage, $index);
}
2020-01-26 08:47:05 +00:00
}
2020-02-04 14:29:38 +00:00
$params['product_view_images'] = json_encode($params['product_view_images']);
2020-01-26 08:47:05 +00:00
}
$params['advertisement'] = json_encode($params['advertisement']);
$params['home_page_content'] = str_replace('=&gt;', '=>', $params['home_page_content']);
unset($params['images']);
unset($params['slides']);
2020-06-11 09:32:39 +00:00
$params['locale'] = $this->locale;
2020-08-17 05:58:12 +00:00
2020-01-26 08:47:05 +00:00
// update row
$product = $this->velocityMetaDataRepository->update($params, $id);
2020-08-01 13:27:15 +00:00
session()->flash('success', trans('admin::app.response.update-success', ['name' => trans('velocity::app.admin.meta-data.title')]));
2020-01-26 08:47:05 +00:00
2020-06-11 09:32:39 +00:00
return redirect()->route($this->_config['redirect'], ['locale' => $this->locale]);
2020-01-26 08:47:05 +00:00
}
2020-03-05 13:37:08 +00:00
/**
2020-06-11 09:32:39 +00:00
* @param array $data
* @param int $index
* @param array $advertisement
2020-08-17 05:58:12 +00:00
*
2020-03-05 13:37:08 +00:00
* @return array
*/
public function uploadAdvertisementImages($data, $index, $advertisement)
2020-01-26 08:47:05 +00:00
{
2020-03-05 13:37:08 +00:00
$saveImage = [];
$saveData = $advertisement;
2020-04-07 08:27:01 +00:00
2020-01-26 08:47:05 +00:00
foreach ($data as $imageId => $image) {
2020-04-07 08:27:01 +00:00
if ($image != "") {
$file = 'images.' . $index . '.' . $imageId;
$dir = 'velocity/images';
2020-08-17 05:58:12 +00:00
2020-04-07 08:27:01 +00:00
if (Str::contains($imageId, 'image_')) {
if (request()->hasFile($file) && $image) {
$filter_index = substr($imageId, 6, 1);
if ( isset($data[$filter_index]) ) {
$size = array_key_last($saveData[$index]);
2020-08-17 05:58:12 +00:00
2020-04-07 08:27:01 +00:00
$saveImage[$size + 1] = request()->file($file)->store($dir);
} else {
$saveImage[substr($imageId, 6, 1)] = request()->file($file)->store($dir);
}
}
} else {
if ( isset($advertisement[$index][$imageId]) && $advertisement[$index][$imageId] && !request()->hasFile($file)) {
$saveImage[$imageId] = $advertisement[$index][$imageId];
2020-08-17 05:58:12 +00:00
2020-04-07 08:27:01 +00:00
unset($advertisement[$index][$imageId]);
}
2020-08-17 05:58:12 +00:00
2020-04-07 08:27:01 +00:00
if (request()->hasFile($file) && isset($advertisement[$index][$imageId])) {
Storage::delete($advertisement[$index][$imageId]);
2020-08-17 05:58:12 +00:00
2020-04-07 08:27:01 +00:00
$saveImage[$imageId] = request()->file($file)->store($dir);
2020-02-13 08:30:59 +00:00
}
}
} else {
2020-04-07 08:27:01 +00:00
if ($saveData) {
$subIndex = substr($imageId, -1);
2020-02-13 08:30:59 +00:00
2020-04-07 08:27:01 +00:00
if (isset($advertisement[$index][$subIndex])) {
$saveImage[$subIndex] = $advertisement[$index][$subIndex];
2020-04-07 08:27:01 +00:00
if (sizeof($advertisement[$index]) == 1) {
unset($advertisement[$index]);
} else {
unset($advertisement[$index][$subIndex]);
}
}
}
2020-01-26 08:47:05 +00:00
}
}
2020-02-20 07:38:52 +00:00
if (isset($advertisement[$index]) && $advertisement[$index]) {
foreach ($advertisement[$index] as $imageId) {
Storage::delete($imageId);
}
}
2020-03-05 13:37:08 +00:00
return $saveImage;
2020-01-26 08:47:05 +00:00
}
2020-03-05 13:37:08 +00:00
/**
2020-06-11 09:32:39 +00:00
* @param array $data
* @param int $index
2020-08-17 05:58:12 +00:00
*
2020-03-05 13:37:08 +00:00
* @return mixed
*/
2020-01-26 08:47:05 +00:00
public function uploadImage($data, $index)
{
$type = 'product_view_images';
$request = request();
$image = '';
$file = $type . '.' . $index;
$dir = "velocity/$type";
if ($request->hasFile($file)) {
Storage::delete($dir . $file);
$image = $request->file($file)->store($dir);
}
return $image;
}
2020-03-05 13:37:08 +00:00
/**
* @param array $addImages
2020-08-17 05:58:12 +00:00
*
2020-03-05 13:37:08 +00:00
* @return array
*/
public function manageAddImages($addImages)
{
2020-03-05 13:37:08 +00:00
$imagePaths = [];
foreach ($addImages as $id => $images) {
foreach ($images as $key => $image) {
2020-02-20 07:38:52 +00:00
if ($image) {
2020-03-05 13:37:08 +00:00
continue;
}
2020-03-05 13:37:08 +00:00
$imagePaths[$id][] = [
'id' => $key,
'type' => null,
'path' => $image,
'url' => Storage::url($image),
];
}
}
2020-08-17 05:58:12 +00:00
2020-03-05 13:37:08 +00:00
return $imagePaths;
}
2020-06-11 09:32:39 +00:00
2020-08-17 05:58:12 +00:00
private function createMetaData($locale, $channel)
2020-06-11 09:32:39 +00:00
{
\DB::table('velocity_meta_data')->insert([
'locale' => $locale,
2020-08-17 05:58:12 +00:00
'channel' => $channel,
2020-11-01 06:08:17 +00:00
'header_content_count' => '5',
2020-06-11 09:32:39 +00:00
'home_page_content' => "<p>@include('shop::home.advertisements.advertisement-four')@include('shop::home.featured-products') @include('shop::home.product-policy') @include('shop::home.advertisements.advertisement-three') @include('shop::home.new-products') @include('shop::home.advertisements.advertisement-two')</p>",
'footer_left_content' => __('velocity::app.admin.meta-data.footer-left-raw-content'),
'footer_middle_content' => '<div class="col-lg-6 col-md-12 col-sm-12 no-padding"><ul type="none"><li><a href="{!! url(\'page/about-us\') !!}">About Us</a></li><li><a href="{!! url(\'page/cutomer-service\') !!}">Customer Service</a></li><li><a href="{!! url(\'page/whats-new\') !!}">What&rsquo;s New</a></li><li><a href="{!! url(\'page/contact-us\') !!}">Contact Us </a></li></ul></div><div class="col-lg-6 col-md-12 col-sm-12 no-padding"><ul type="none"><li><a href="{!! url(\'page/return-policy\') !!}"> Order and Returns </a></li><li><a href="{!! url(\'page/payment-policy\') !!}"> Payment Policy </a></li><li><a href="{!! url(\'page/shipping-policy\') !!}"> Shipping Policy</a></li><li><a href="{!! url(\'page/privacy-policy\') !!}"> Privacy and Cookies Policy </a></li></ul></div>',
2020-06-11 09:32:39 +00:00
'slider' => 1,
'subscription_bar_content' => '<div class="social-icons col-lg-6"><a href="https://webkul.com" target="_blank" class="unset" rel="noopener noreferrer"><i class="fs24 within-circle rango-facebook" title="facebook"></i> </a> <a href="https://webkul.com" target="_blank" class="unset" rel="noopener noreferrer"><i class="fs24 within-circle rango-twitter" title="twitter"></i> </a> <a href="https://webkul.com" target="_blank" class="unset" rel="noopener noreferrer"><i class="fs24 within-circle rango-linked-in" title="linkedin"></i> </a> <a href="https://webkul.com" target="_blank" class="unset" rel="noopener noreferrer"><i class="fs24 within-circle rango-pintrest" title="Pinterest"></i> </a> <a href="https://webkul.com" target="_blank" class="unset" rel="noopener noreferrer"><i class="fs24 within-circle rango-youtube" title="Youtube"></i> </a> <a href="https://webkul.com" target="_blank" class="unset" rel="noopener noreferrer"><i class="fs24 within-circle rango-instagram" title="instagram"></i></a></div>',
'product_policy' => '<div class="row col-12 remove-padding-margin"><div class="col-lg-4 col-sm-12 product-policy-wrapper"><div class="card"><div class="policy"><div class="left"><i class="rango-van-ship fs40"></i></div> <div class="right"><span class="font-setting fs20">Free Shipping on Order $20 or More</span></div></div></div></div> <div class="col-lg-4 col-sm-12 product-policy-wrapper"><div class="card"><div class="policy"><div class="left"><i class="rango-exchnage fs40"></i></div> <div class="right"><span class="font-setting fs20">Product Replace &amp; Return Available </span></div></div></div></div> <div class="col-lg-4 col-sm-12 product-policy-wrapper"><div class="card"><div class="policy"><div class="left"><i class="rango-exchnage fs40"></i></div> <div class="right"><span class="font-setting fs20">Product Exchange and EMI Available </span></div></div></div></div></div>',
]);
}
2020-01-26 08:47:05 +00:00
}