Server : Apache System : Linux 145.162.205.92.host.secureserver.net 5.14.0-611.45.1.el9_7.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Apr 1 05:56:53 EDT 2026 x86_64 User : tradze ( 1001) PHP Version : 8.1.34 Disable Function : NONE Directory : /home/tradze/public_html/app/Modules/Banners/Http/Controllers/Admin/ |
<?php
namespace App\Modules\Banners\Http\Controllers\Admin;
use App\Http\Controllers\AdminController;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Modules\Banners\classname;
use App\Modules\Banners\Http\Requests\BannerRequest;
use App\Modules\Banners\Models\Banner;
use App\Modules\Banners\Models\BannerFile;
use App\Modules\Events\Models\Zenevent;
use App\Modules\Pages\Http\Controllers\Admin\PagesController;
use App\Modules\Pages\Models\Page;
use App\Modules\Services\Models\ServiceType;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Laracasts\Flash\Flash;
use Symfony\Component\Yaml\Tests\B;
use Yajra\Datatables\Datatables;
class BannersController extends AdminController
{
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
$this->data['page_title'] = trans('banners::banners.page_title');
return view('banners::admin.banners_index', $this->data);
}
/**
* Show a list of all companies
*
* @return mixed
*/
public function data(Request $request)
{
//create object contact
$obj = $this->getData($request);
//create array with permissions access
$this->data['can'] = [
'edit' => $this->data['user']->roles()->pluck('slug')[0] == 'developer',
'delete' => $this->data['user']->roles()->pluck('slug')[0] == 'developer',
];
//return datatables data
return Datatables::of($obj)
->addColumn('actions', function ($o) {
$this->data['o']=$o;
return view('banners::admin.banners_list_actions',$this->data)->render();
})
->removeColumn('id')
->removeColumn('updated_at')
->rawColumns(['actions'])
->make(true);
}
/**
* Get object entries
* @param Request $request
*/
protected function getData(Request $request)
{
$obj = Banner::query();
//return object
return $obj;
}
/**
* Show the form for creating a new resource.
*
* @return Response
*/
public function create(Request $request)
{
$this->data['page_title'] = trans('banners::banners.page_title');
$this->data['form']['positions'] = trans('banners::banners.form_position_opt');
$this->data['form']['gallery'] = $this->get_request_gallery($request);
$this->data['form']['display'] = $this->get_display_options($request);
//show page
return view('banners::admin.banners_create_edit', $this->data);
}
/**
* Store a newly created resource in storage.
*
* @return Response
*/
public function store(BannerRequest $request)
{
//start transaction
DB::beginTransaction();
$data = [
'name' => $request->name,
'position' => $request->position,
];
//save new banner
$obj = Banner::create($data);
//save banner gallery
if ($request->has('file_title')){
foreach($request->file_title as $key => $val){
if (!$request->hasFile('photo') || !isset($request->file('photo')[$key])) {
continue; // skip if file not uploaded
}
$file = $request->file('photo')[$key];
$image = BannerFile::create([
'title' => $val,
'url' => $request['file_url'][$key],
'order' => $request['file_order'][$key],
'banner_id' => $obj->id,
]);
$filename = strtolower(str_random(20)).'-'.str_slug(
str_replace($file->getClientOriginalExtension(),'',$file->getClientOriginalName())
);
$extension = $file->getClientOriginalExtension() ?: 'jpg';
$path_file = 'banners/'.$filename.".".$extension;
Storage::disk('public_images')->put(
$path_file,
file_get_contents($file->getRealPath())
);
$image->path = $path_file;
$image->filename = $file->getClientOriginalName();
$image->save();
}
} //endif
//attach new display relations
$display = $this->get_display_options($request);
foreach(array_values($display) as $fields)
$display_in[] = array_keys($fields)[0];
$display_values = [];
foreach($display_in as $dspl){
if ($request->has($dspl)){
foreach($request->$dspl as $option){
$classname = Str::plural(strtolower(class_basename($dspl)));
$display_values[$classname][] = $option;
}
}
}
foreach($display_values as $relation=>$values){
//check existence of the method in banner model
if (method_exists($obj,$relation)){
//save relation between banner and display options in bannerable table
$obj->$relation()->sync($values);
} //endif
} //endforeach
if ($obj){
//commit transaction
DB::commit();
}
else{
//rollback
DB::rollback();
} //endif commit/rollback
//redirect
if ($request->save) {
return redirect(route('admin.banners.edit', ['id' => $obj->id]));
} elseif ($request->save_exit) {
return redirect(route('admin.banners.index'));
}
}
/**
* Edit Service Type
*
* @param $label
* @return \BladeView|bool|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function edit(Banner $banner, Request $request)
{
$this->data['page_title'] = trans('banners::banners.page_title');
$this->data['form']['positions'] = trans('banners::banners.form_position_opt');
$this->data['obj'] = $banner;
$this->data['form']['gallery'] = $this->get_request_gallery($request);
$this->data['form']['saved_gallery'] = $this->get_saved_gallery($banner);
$this->data['form']['display'] = $this->get_display_options($request);
$this->data['form']['saved_display'] = $this->get_saved_display($banner);
//show page
return view('banners::admin.banners_create_edit', $this->data);
}
/**
* Update the specified resource in storage.
*
* @param int $id
* @return Response
*/
public function update(BannerRequest $request, Banner $banner)
{
$obj = $banner;
$display = $this->get_display_options($request);
foreach(array_values($display) as $fields)
$display_in[] = array_keys($fields)[0];
$display_values = [];
foreach($display_in as $dspl){
if ($request->has($dspl)){
foreach($request->$dspl as $option){
$classname = Str::plural(strtolower(class_basename($dspl)));
$display_values[$classname][] = $option;
}
}
}
//start transaction
DB::beginTransaction();
//fields to be updated
$fillable = $obj->getFillable();
$fields = array();
foreach ($request->all() as $field => $f) {
if (in_array($field, $fillable)) {
$fields[$field] = $f;
}
} //end foreach
//update banner fields
Banner::where('id', $obj->id)->update($fields);
//update existing banner gallery
foreach($obj->files as $gal){
$galData=[
'order'=>$request["file_order_{$gal->id}"],
'title'=>$request["file_title_{$gal->id}"],
'url'=>$request["file_url_{$gal->id}"],
];
$gal->update($galData);
//update image
if ($request->hasFile("photo_{$gal->id}")){
$file = $request->file("photo_{$gal->id}");
$filename = strtolower(Str::random(20)).'-'.Str::slug(str_replace($file->getClientOriginalExtension(),'',$file->getClientOriginalName()));
$extension = ($file->getClientOriginalExtension())?:'jpg';
$path_file = 'banners/'.$filename.".".$extension;
//upload file
$upload = Storage::disk('public_images')->put(
$path_file,
file_get_contents($file->getRealPath())
);
$gal->path = $path_file;
$gal->filename = $file->getClientOriginalName();
$gal->save();
} //endif update file to existing gallery
} //end save existing gallery entry
//save new banner in gallery
if ($request->has('file_title')){
foreach($request->file_title as $key=>$val){
$image = BannerFile::create([
'title' => $val,
'url' => $request['file_url'][$key],
'order' => $request['file_order'][$key],
'banner_id' => $obj->id,
]);
$file = $request->file('photo')[$key];
$filename = strtolower(str_random(20)).'-'.str_slug(str_replace($file->getClientOriginalExtension(),'',$file->getClientOriginalName()));
$extension = ($file->getClientOriginalExtension())?:'jpg';
$path_file = 'banners/'.$filename.".".$extension;
//upload file
$upload = Storage::disk('public_images')->put(
$path_file,
file_get_contents($file->getRealPath())
);
$image->path = $path_file;
$image->filename = $file->getClientOriginalName();
$image->save();
} //endforeach
} //endif
//save display relation in models
//first detach all display relations
$banner->pages()->detach();
$banner->zenevents()->detach();
//attach new display relations
foreach($display_values as $relation=>$values){
//check existence of the method in banner model
if (method_exists($banner,$relation)){
//save relation between banner and display options in bannerable table
$banner->$relation()->sync($values);
} //endif
} //endforeach
//commit/rollback transaction
if ($obj){
//commit transaction
DB::commit();
}
else{
//rollback
DB::rollback();
} //endif commit/rollback
//redirect
if ($request->save) {
return redirect(route('admin.banners.edit', ['banner' => $obj->id]));
} elseif ($request->save_exit) {
return redirect(route('admin.banners.index'));
}
}
/**
* Delete page
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function delete(Banner $banners)
{
$this->data['obj'] = $banners;
return view('banners::admin.banners_delete', $this->data);
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy(Banner $banners)
{
$obj = $banners;
//delete gallery
foreach($obj->files as $file){
//remove file from storage
$this->remove_file_from_storage($file->path);
$file->delete();
} //endforeach delete gallery
//delete banner
$obj->delete();
//set success message
Flash::info(trans('banners::banners.message_confirm_delete', ['name' => $obj->name]));
//redirect to contacts list
return redirect(route('admin.banners.index'));
}
/**
* Add new file to gallery
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function add_gallery()
{
return view('banners::admin.partial_new_gallery');
}
/**
* Remove file from gallery
* @param $id
*/
public function del_gallery($id)
{
$obj = BannerFile::findOrFail($id);
//remove file from storage
$this->remove_file_from_storage($obj->path);
//delete file entry from db
$obj->delete();
}
/**
* Remove file from storage
* @param $path
*/
protected function remove_file_from_storage($path)
{
if (Storage::disk('public_images')->exists($path))
Storage::disk('public_images')->delete($path);
}
/**
* Create gallery array from request
* @param $request
* @return mixed
*/
protected function get_request_gallery($request)
{
$data['file_order'] = $request->old('file_order');
$data['file_title'] = $request->old('file_title');
$data['file_url'] = $request->old('file_url');
return $data;
}
/**
* Get banner gallery
* @param $obj
*/
protected function get_saved_gallery(Banner $obj)
{
$results = [];
$list = BannerFile::where('banner_id',$obj->id)
->orderBy('order','asc')
->orderBy('id','asc')
->get();
foreach($list as $li){
$results[$li->id]=[
'id' => $li->id,
'title' => $li->title,
'order' => $li->order,
'url' => $li->url,
'path' => $li->path,
'image' => $li->image,
];
} //endforeach
//return results
return $results;
}
/**
* Get display options
*/
protected function get_display_options(Request $request)
{
$results = [];
//get pages options
$results[] = $this->process_display(new Page());
// $results[] = $this->process_display(new Zenevent());
//return results
return $results;
}
/**
* Retrieve all saved model displays
*
* @param $obj
*/
protected function get_saved_display($obj)
{
//return empty results array if display property is not set in the models
if (empty($obj->display)) return [];
//continue...
$results = [];
foreach($obj->display as $item){
$options = $obj->$item;
if (!$options) continue;
foreach($options as $o){
$results[get_class($o)][] = $o->id;
}//end foreach
}//end foreach
//return results
return $results;
}
/**
* Format display options object
*
* @param $obj
* @return array
*/
protected function process_display($obj)
{
$options = [];
$class=get_class($obj);
foreach($class::all() as $o){
$options[$class][$o->id] = ($o->title)?$o->title:$o->name;
} //endforeach
return $options;
}
}