File: /var/www/vhost/disk-apps/demo.sports-crowd.com/app/Http/Controllers/AppSectionController.php
<?php
namespace App\Http\Controllers;
use App\Tag;
use DataTables;
use App\AppSection;
use App\AppSectionTag;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
class AppSectionController extends Controller
{
private $util;
private $validationTypes;
public function __construct()
{
$this->util = new UtilController();
$this->validationTypes[] = (object) [
'name' => 'Seleccione',
'value' => ''
];
$this->validationTypes[] = (object) [
'name' => 'Ninguno',
'value' => 'none'
];
$this->validationTypes[] = (object) [
'name' => 'Validar documento',
'value' => 'document'
];
$this->validationTypes[] = (object) [
'name' => 'Validar abonado',
'value' => 'isSubscriber'
];
$this->validationTypes[] = (object) [
'name' => 'Validar monedas',
'value' => 'coins'
];
$this->validationTypes[] = (object) [
'name' => 'Próximamente nueva funcionalidad',
'value' => 'comingSoon'
];
$this->validationTypes[] = (object) [
'name' => 'Todos',
'value' => 'all'
];
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return view('app_section.list');
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$multiselectItems = Tag::select('id', 'name')->where('active', true)->get();
return view('app_section.create')
->with('multiselectItems', $multiselectItems)
->with('validationTypes', $this->validationTypes);
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$object = AppSection::findOrFail($id);
$multiselectItems = Tag::select('id', 'name')->where('active', true)->get();
$multiselectValues = AppSectionTag::select('tag_id')->where('app_section_id', $id)->get();
return view('app_section.edit', compact('object'))
->with('multiselectItems', $multiselectItems)
->with('multiselectValues', $multiselectValues)
->with('validationTypes', $this->validationTypes);
}
public function tableFilter()
{
DB::statement("SET sql_mode = ''");
$obj = AppSection::select(
'app_sections.id',
'app_sections.name',
'app_sections.active',
'app_sections.description',
'app_sections.path',
'app_sections.button_text_color',
'app_sections.button_color',
'app_sections.button_image',
'app_sections.button_link',
'app_sections.icon_name',
'app_sections.button_order',
'app_sections.tab',
'app_sections.validate_data_type',
'app_sections.chip_new',
'app_sections.enable_on_home',
'app_sections.enable_on_tabs',
'app_sections.enable_on_settings',
'app_sections.enable_section_on_home',
'app_sections.link_section_home',
DB::raw('GROUP_CONCAT(DISTINCT(tags.name)) AS segmentation'),
DB::raw("IF(app_sections.button_width IS NULL, 'NULL', IF(app_sections.button_width = 6, 'Ancho mediano', 'Ancho completo')) AS width"),
)
->leftjoin('app_section_tags', 'app_sections.id', '=', 'app_section_tags.app_section_id')
->leftjoin('tags', function ($join) {
$join->on('tags.id', '=', 'app_section_tags.tag_id')->where('tags.active', 1);
})
->groupBy('app_sections.id');
DB::statement("SET sql_mode = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'");
return DataTables::of($obj)
->addColumn('actions', function ($obj) {
return '
<i class="fa fa-pencil iconMini" onClick="clickEdit(' . $obj->id . ')" data-id="' . $obj->id . '" title="Editar"></i>
<i class="fa fa-trash iconMini" onClick="clickDelete(' . $obj->id . ')" data-id="' . $obj->id . '" title="Eliminar"></i>
';
})
->editColumn('button_image', function ($obj) {
if (!$obj->button_image) {
return $this->util->generateEmptyImageColumn();
} else {
return $this->util->generateImageColumn($obj->button_image, $obj->name, $obj->id);
}
})
->editColumn('button_text_color', function ($obj) {
return '<div class="text_color_display">
<i style="width: 20px; height:20px; display: block; background-color: ' . $obj->button_text_color . '"></i>
</div>';
})
->editColumn('button_color', function ($obj) {
return '<div class="text_color_display">
<i style="width: 20px; height:20px; display: block; background-color: ' . $obj->button_color . '"></i>
</div>';
})
->editColumn('active', function ($obj) {
if ($obj->active == 0) {
return '<div class="switch"><label><div class="checkbox checbox-switch switch-success"> <label> ' . __('messages.no') . ' <input type="checkbox" onChange="chk(' . $obj->id . ')" data-id="' . $obj->id . '" id="Checkactive' . $obj->id . '" name="Checkactivo" /> <span></span>' . __('messages.yes') . ' </label></div> </label> </div>';
} else {
return '<div class="switch"><label> <div class="checkbox checbox-switch switch-success"> <label> ' . __('messages.no') . ' <input type="checkbox" onChange="chk(' . $obj->id . ')" data-id="' . $obj->id . '" id="Checkactive' . $obj->id . '" name="Checkactivo" checked="" />
<span></span> ' . __('messages.yes') . ' </label> </div> </label> </div>';
}
})
->editColumn('validate_data_type', function ($obj) {
$validate_data_type = [];
if ($obj->validate_data_type) {
foreach (explode(',', $obj->validate_data_type) as $item) {
$object = collect($this->validationTypes)->where('value', $item)->first();
if ($object)
$validate_data_type[] = $object->name;
}
}
return implode(',', $validate_data_type);
})
->rawColumns(['actions', 'button_image', 'button_text_color', 'button_color', 'active'])
->make(true);
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
try {
if (!AppSection::where([['name', $request->input('name')], ['active', 1]])->first()) {
$tags = $request["tags"];
$request->request->remove('tags');
if ($model = AppSection::create($request->all())) {
if ($tags != null) {
foreach ($tags as $tagId) {
AppSectionTag::create([
'tag_id' => $tagId,
'app_section_id' => $model->id
]);
}
}
return response(array("r" => true, "type" => "success", "title" => "", "m" => __('messages.created_successfully'), "data" => $model->id));
} else {
return response(array("r" => false, "type" => "error", "title" => "Oops...", "m" => __('messages.error_creating'), "data" => null));
}
} else {
return response(array("r" => false, "type" => "error", "title" => "Oops...", "m" => __('messages.already_exists', ['name' => $request->input('name')]), "data" => null));
}
} catch (\Exception $e) {
return response(
array(
"r" => false,
"type" => "error",
"title" => "Oops...",
"m" => __($e->getMessage()),
"data" => null
)
);
}
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
try {
if (!AppSection::where([['id', '!=', $id], ['name', $request->input('name')], ['active', 1]])->first()) {
$tags = $request["tags"];
$request->request->remove('tags');
$request->request->remove('generalMultiselect');
if (AppSection::where('id', $id)->update($request->all())) {
AppSectionTag::where('app_section_id', $id)->delete();
if ($tags != null) {
foreach ($tags as $tagId) {
AppSectionTag::create([
'tag_id' => $tagId,
'app_section_id' => $id
]);
}
}
return response(array("r" => true, "type" => "success", "title" => "", "m" => __('messages.updated_successfully'), "data" => $id));
} else {
return response(array("r" => false, "type" => "error", "title" => "Oops...", "m" => __('messages.error_updating'), "data" => null));
}
} else {
return response(array("r" => false, "type" => "error", "title" => "Oops...", "m" => __('messages.already_exists', ['name' => $request->input('name')]), "data" => null));
}
} catch (\Exception $e) {
return response(
array(
"r" => false,
"type" => "error",
"title" => "Oops...",
"m" => __($e->getMessage()),
"data" => null
)
);
}
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
try {
AppSectionTag::where('app_section_id', $id)->delete();
if (AppSection::where('id', $id)->delete()) {
return response(array("r" => true, "type" => "success", "title" => "", "m" => __('messages.deleted_successfully'), "data" => null));
} else {
return response(array("r" => false, "type" => "error", "title" => "Oops...", "m" => __('messages.error_removing'), "data" => null));
}
} catch (\Illuminate\Database\QueryException $e) {
return response(array("r" => false, "type" => "error", "title" => "Oops...", "m" => __('messages.delete_relation_data'), "data" => null));
}
}
public function activate(Request $request)
{
try {
$id = $request['id'];
$state = $request['state'];
$section = AppSection::find($id);
$section->active = $state;
$section->update();
return array('r' => true, 'd' => null, 'm' => __('messages.updated_successfully'));
} catch (\Throwable $th) {
return array('r' => false, 'd' => null, 'm' => __('messages.error_updating'));
}
}
public function list()
{
try {
$appSection = AppSection::where('active', 1)->get();
return response(array("r" => true, "type" => "success", "title" => "", "m" => '', "data" => $appSection));
} catch (\Throwable $th) {
return response(array("r" => false, "type" => "error", "title" => "Oops...", "m" => __('messages.error'), "data" => null));
}
}
public function saveImage(Request $request)
{
try {
$app_section = AppSection::find($request->id);
if ($app_section->enable_section_on_home == 1) {
$extension = $request->file('image')->getClientOriginalExtension();
$filenametostore = 'home_section_' . $request->id . '.' . $extension;
Storage::disk('s3')->put(config('s3.app_section') . $filenametostore, fopen($request->file('image'), 'r+'), 'public');
$url = config('filesystems.disks.s3.url') . '/app_section/home_section_images/' . $filenametostore;
$app_section->link_section_home = $url;
$app_section->button_width = null;
$app_section->button_color = null;
$app_section->button_text_color = null;
$app_section->icon_name = null;
$app_section->update();
} else {
$extension = $request->file('image')->getClientOriginalExtension();
$filenametostore = 'home_buttons_' . $request->id . '.' . $extension;
Storage::disk('s3')->put(config('s3.app_section_button') . $filenametostore, fopen($request->file('image'), 'r+'), 'public');
$url = config('filesystems.disks.s3.url') . '/app_section/start_button_images/' . $filenametostore;
$app_section->button_image = $url;
$app_section->button_color = null;
$app_section->button_text_color = null;
$app_section->icon_name = null;
$app_section->update();
}
return response(array("r" => true, "type" => "success", "title" => "", "m" => __('messages.updated_successfully'), "data" => null));
} catch (\Throwable $th) {
return response(array("r" => false, "type" => "error", "title" => "Oops...", "m" => __('messages.error_updating'), "data" => $th->getMessage()));
}
}
public function activateTicketingSections($enableTicketing = true, $enableFlashTicketing = false)
{
// Seccion boleteria normal
AppSection::where('path', '/tickets')->update(['active' => $enableTicketing]);
// Seccion boleteria flash
AppSection::where('path', '/flash-tickets')->update(['active' => $enableFlashTicketing]);
}
}