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

227 lines
7.0 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;
/**
* 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-03-05 13:37:08 +00:00
/**
* @return \Illuminate\View\View
*/
2020-01-26 08:47:05 +00:00
public function renderMetaData()
{
$velocityMetaData = $this->velocityHelper->getVelocityMetaData();
2020-03-06 10:32:52 +00:00
if ($velocityMetaData && $velocityMetaData->advertisement) {
2020-02-12 11:16:06 +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
];
}
$velocityMetaData = $this->velocityMetaDataRepository->findorFail($id);
$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']);
// update row
$product = $this->velocityMetaDataRepository->update($params, $id);
session()->flash('success', trans('admin::app.response.update-success', ['name' => 'Velocity Theme']));
return redirect()->route($this->_config['redirect']);
}
2020-03-05 13:37:08 +00:00
/**
* @param array $data
* @param int $index
* @param array $advertisement
* @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';
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]);
$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];
unset($advertisement[$index][$imageId]);
}
if (request()->hasFile($file) && isset($advertisement[$index][$imageId])) {
Storage::delete($advertisement[$index][$imageId]);
$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
/**
* @param array $data
* @param int $index
* @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
* @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-03-05 13:37:08 +00:00
return $imagePaths;
}
2020-01-26 08:47:05 +00:00
}