| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832 |
- <?php
- namespace App\Http\Controllers;
- use App\News;
- use App\Section;
- use App\Event;
- use App\Page;
- use App\Home;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\File;
- use Illuminate\Support\Facades\Auth;
- use App\Notifications\FacebookPost;
- use App\Notifications\TwitterPost;
- use Illuminate\Support\Facades\Log;
- class NewsController extends Controller
- {
- protected $rules = [
- //'section_id' => ['required']
- 'title' => ['required'],
- //'page_id' => ['required'],
- ];
- public function index(Request $request)
- {
- // Se stavo modificando una news metto a null il campo
- if ($request->session()->has('current_news')) {
- $news_id = session('current_news', 0);
- if ($news_id > 0) {
- $n = News::where('id', '=', $news_id)->take(1000)->get();
- if ($n->count() > 0) {
- $n = $n->first();
- if ($n->user_id == Auth::user()->id) {
- $n->user_id = null;
- $n->save();
- }
- }
- $request->session()->forget('current_news');
- }
- }
- $news = News::orderBy('date', 'DESC')->take(1000)->get(); //->paginate(50);
- return view('news.index', compact('news'))
- ->with('i', (request()->input('page', 1) - 1) * 5);
- }
- public function load_json()
- {
- $start = $_GET["start"];
- $end = 50; //$_GET["length"];
- $total = News::count();
- $filtered = $total;
- $sort_by = '';
- $sort = $_GET["order"][0]["column"];
- switch ($sort) {
- case '0':
- $sort_by = 'date';
- break;
- case '2':
- $sort_by = 'title';
- break;
- case '3':
- $sort_by = 'name';
- break;
- case '4':
- $sort_by = 'name';
- break;
- case '5':
- $sort_by = 'name';
- break;
- case '6':
- $sort_by = 'title';
- break;
- case '7':
- $sort_by = 'online';
- break;
- case '8':
- $sort_by = 'clicks';
- break;
- default:
- $sort_by = 'date';
- break;
- }
- $sort_by_dir = $_GET["order"][0]["dir"];
- $search = $_GET["search"]["value"];
- if ($sort == '3') {
- $news = News::with(['section' => function ($query) use ($sort_by, $sort_by_dir) {
- $query->orderBy($sort_by, $sort_by_dir);
- }]);
- } else if ($sort == '4') {
- $news = News::with(['region_1' => function ($query) use ($sort_by, $sort_by_dir) {
- $query->orderBy($sort_by, $sort_by_dir);
- }]);
- } else if ($sort == '5') {
- $news = News::with(['region_2' => function ($query) use ($sort_by, $sort_by_dir) {
- $query->orderBy($sort_by, $sort_by_dir);
- }]);
- } else if ($sort == '6') {
- $news = News::with(['event' => function ($query) use ($sort_by, $sort_by_dir) {
- $query->orderBy($sort_by, $sort_by_dir);
- }]);
- } else
- $news = News::orderBy($sort_by, $sort_by_dir);
- if ($search != '') {
- $news = $news->where('title', 'LIKE', '%' . $search . '%');
- $filtered = $news->count();
- }
- $news = $news->limit($end)->offset($start)->get(); //->paginate(50);
- $aData = array();
- foreach ($news as $n) {
- $x = array();
- $x[] = $n->date;
- if ($n->image != '')
- $x[] = '<img src="/files/news/' . $n->image . '" style="max-width:100px" />';
- else
- $x[] = '';
- $x[] = $n->title;
- $section = @$n->section->name;
- if ($n->section)
- $section .= '<br>' . isset($n->section) ? $n->section->position($n->id) : '';
- $x[] = $section;
- $region_1 = @$n->region_1->name;
- if ($n->region_1)
- $region_1 .= '<br>' . isset($n->region_1) ? $n->region_1->position($n->id) : '';
- $x[] = $region_1;
- $region_2 = @$n->region_2->name;
- if ($n->region_2)
- $region_2 .= '<br>' . isset($n->region_2) ? $n->region_2->position($n->id) : '';
- $x[] = $region_2;
- //$x[] = @$n->event->title;
- $x[] = @$n->online ? '<a si class="btn btn-w-m btn-default" href="/admin/news/status/' . $n->id . '/offline"> Si </a>' : '<a no class="btn btn-w-m btn-default" href="/admin/news/status/' . $n->id . '/online">No</a>';
- $x[] = @$n->clicks;
- $x[] = '<a href="/admin/news/duplicate/' . $n->id . '" type="button" class="btn btn-w-m btn-primary"><i class="fa fa-clone" aria-hidden="true"></i></a>';
- $x[] = '<a href="/admin/news/' . $n->id . '/edit" type="button" class="btn btn-w-m btn-primary"><i class="fa fa-pencil" aria-hidden="true"></i></a>';
- $form = '<form method="POST" action="/admin/news/' . $n->id . '" accept-charset="UTF-8" class="form-inline">
- <input name="_method" type="hidden" value="DELETE">
- <input name="_token" type="hidden" value="' . csrf_token() . '">
- <button type="submit" class="btn btn-w-m btn-danger" onclick="return confirm(\'Sei sicuro?\')"><i class="fa fa-trash-o" aria-hidden="true"></i></button>
- </form>';
- $x[] = $form;
- $aData[] = $x;
- }
- $aRet = array('recordsTotal' => $total, 'recordsFiltered' => $filtered, 'data' => $aData);
- return json_encode($aRet);
- }
- /**
- * Show the form for creating a new resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function create()
- {
- $sections = Section::where('type', '=', 'section')->orderBy('position')->pluck('name', 'id')->toArray();;
- $regions = Section::where('type', '=', 'region')->orderBy('name')->pluck('name', 'id')->toArray();;
- $layouts = Section::pluck('layout', 'id')->toArray();
- $events = Event::orderBy('title')->pluck('title', 'id')->toArray();;
- $pages = Page::where('online', '=', true)->orderBy('title')->pluck('title', 'id')->toArray();;
- $section_position = '';
- $region_1_position = '';
- $region_2_position = '';
- $first = '';
- $aFirst = array();
- $home = Home::first();
- if ($home != null) {
- $pos = '';
- $home->loadData();
- if (isset($home->slide1) && $home->slide1 != '')
- $aFirst[] = $home->slide1->title . " (immagine grande)";
- for ($a = 1; $a <= 5; $a++) {
- if (isset($home["left" . $a]) && $home["left" . $a] != '')
- $aFirst[] = $home["left" . $a]["title"] . " (slide sinistra)";
- }
- for ($a = 1; $a <= 5; $a++) {
- if (isset($home["right" . $a]) && $home["right" . $a] != '')
- $aFirst[] = $home["right" . $a]["title"] . " (slide destra)";
- }
- $first = '<li>' . implode("</li><li>", $aFirst) . '</li>';
- }
- return view('news.create', compact('sections', 'regions', 'events', 'pages', 'layouts', 'section_position', 'region_1_position', 'region_2_position', 'first'));
- }
- /**
- * Store a newly created resource in storage.
- *
- * @param \Illuminate\Http\Request $request
- * @return \Illuminate\Http\Response
- */
- public function store(Request $request)
- {
- Log::info('=== NEWS STORE METHOD START ===');
- Log::info('Request data:', $request->all());
- Log::info('Request method:', [$request->method()]);
- Log::info('Has files:', [$request->hasFile('image')]);
- Log::info('Files data:', $request->file());
- try {
- $request->validate($this->rules);
- Log::info('Validation passed');
- $input = $request->all();
- Log::info('Input data after validation:', $input);
- // Check which button was clicked
- if (isset($input['save'])) {
- Log::info('SAVE button clicked');
- }
- if (isset($input['save_exit'])) {
- Log::info('SAVE_EXIT button clicked');
- }
- if (isset($input['publish'])) {
- Log::info('PUBLISH button clicked');
- }
- if (isset($input['unpublish'])) {
- Log::info('UNPUBLISH button clicked');
- }
- if (isset($input['crop'])) {
- Log::info('CROP button clicked');
- }
- // === IMAGE HANDLING SECTION ===
- Log::info('=== IMAGE HANDLING START ===');
- // Check for filename from crop
- if (isset($input["filename"]) && $input["filename"] != '') {
- Log::info('Filename from crop found:', [$input["filename"]]);
- // Verify file exists
- $filePath = public_path('files/news/' . $input["filename"]);
- $fileExists = file_exists($filePath);
- Log::info('Crop file verification:', [
- 'filename' => $input["filename"],
- 'full_path' => $filePath,
- 'exists' => $fileExists,
- 'is_readable' => $fileExists ? is_readable($filePath) : false,
- 'file_size' => $fileExists ? filesize($filePath) : 0
- ]);
- if ($fileExists) {
- $input["image"] = $input["filename"];
- Log::info('Using cropped image filename:', [$input["filename"]]);
- } else {
- Log::error('Cropped image file does not exist!');
- }
- }
- // Check for direct file upload
- elseif ($request->hasFile('image')) {
- Log::info('Direct image upload detected');
- $file = $request->file('image');
- Log::info('Upload file details:', [
- 'original_name' => $file->getClientOriginalName(),
- 'size' => $file->getSize(),
- 'mime_type' => $file->getMimeType(),
- 'is_valid' => $file->isValid()
- ]);
- if ($file->isValid()) {
- $filename = time() . '_' . $file->getClientOriginalName();
- if (!File::exists(public_path() . "/files/news")) {
- Log::info('Creating news directory');
- File::makeDirectory(public_path() . "/files/news");
- }
- $path = public_path('files/news');
- $file->move($path, $filename);
- $input["image"] = $filename;
- Log::info('Direct image uploaded successfully:', [
- 'filename' => $filename,
- 'path' => $path . '/' . $filename
- ]);
- } else {
- Log::error('Invalid file upload');
- }
- } else {
- Log::info('No image provided (neither crop nor direct upload)');
- }
- Log::info('Final image value:', [$input["image"] ?? 'none']);
- Log::info('=== IMAGE HANDLING END ===');
- // Handle additional images
- for ($i = 1; $i <= 5; $i++) {
- if ($request->hasFile("image" . $i)) {
- Log::info("Processing image{$i} upload");
- $file = $request->file("image" . $i);
- $filename = time() . '_' . $file->getClientOriginalName();
- if (!File::exists(public_path() . "/files/news"))
- File::makeDirectory(public_path() . "/files/news");
- $path = public_path('files/news');
- $file->move($path, $filename);
- $input["image" . $i] = $filename;
- Log::info("Image{$i} uploaded:", [$filename]);
- }
- }
- // Handle PDF
- if ($request->hasFile('pdf')) {
- Log::info('Processing PDF upload');
- $file = $request->file('pdf');
- $filename = time() . '_' . $file->getClientOriginalName();
- if (!File::exists(public_path() . "/files/news"))
- File::makeDirectory(public_path() . "/files/news");
- $path = public_path('files/news');
- $file->move($path, $filename);
- $input["pdf"] = $filename;
- Log::info('PDF uploaded:', [$filename]);
- }
- // Date processing
- $final_date = null;
- if (isset($input["date"]) && $input["date"] != '') {
- list($dt, $time) = explode(" ", $input["date"]);
- list($day, $month, $year) = explode("/", $dt);
- $final_date = $year . "-" . $month . "-" . $day . " " . $time;
- Log::info('Date processed:', [$input["date"], $final_date]);
- }
- $input["date"] = $final_date;
- // Handle checkboxes
- $input["online"] = false;
- $input["homepage"] = isset($input["homepage"]) ? ($input["homepage"] == 'on' ? true : false) : false;
- $input["live"] = isset($input["live"]) ? ($input["live"] == 'on' ? true : false) : false;
- $input["breaking_news"] = isset($input["breaking_news"]) ? ($input["breaking_news"] == 'on' ? true : false) : false;
- $input["no_social"] = isset($input["no_social"]) ? ($input["no_social"] == 'on' ? true : false) : false;
- Log::info('Final input data before saving:', $input);
- Log::info('About to create news record');
- $news = News::create($input);
- Log::info('News created successfully:', [
- 'id' => $news->id,
- 'title' => $news->title,
- 'image' => $news->image,
- 'created_at' => $news->created_at
- ]);
- // Verify the image was saved correctly
- if ($news->image) {
- $savedImagePath = public_path('files/news/' . $news->image);
- $imageExists = file_exists($savedImagePath);
- Log::info('Saved news image verification:', [
- 'image_field' => $news->image,
- 'full_path' => $savedImagePath,
- 'exists' => $imageExists,
- 'size' => $imageExists ? filesize($savedImagePath) : 0
- ]);
- }
- // Handle publish action
- if (isset($input['publish'])) {
- Log::info('Processing publish action');
- $news->online = true;
- $news->save();
- Log::info('News set to online');
- // Position the news if date is not in future
- if ($final_date <= date("Y-m-d H:i:s")) {
- Log::info('Date is not in future, processing positions');
- if (isset($input["section_position"]) && $input["section_position"] != '') {
- Log::info('Processing section position:', [$input["section_position"]]);
- $s = Section::findOrFail($input["section_id"]);
- $s[$input["section_position"]] = $news->id;
- $s->save();
- $news->section_position = '';
- }
- if (isset($input["region_1_position"]) && $input["region_1_position"] != '') {
- Log::info('Processing region_1 position:', [$input["region_1_position"]]);
- $s = Section::findOrFail($input["region_1_id"]);
- $s[$input["region_1_position"]] = $news->id;
- $s->save();
- $news->region_1_position = '';
- }
- if (isset($input["region_2_position"]) && $input["region_2_position"] != '') {
- Log::info('Processing region_2 position:', [$input["region_2_position"]]);
- $s = Section::findOrFail($input["region_2_id"]);
- $s[$input["region_2_position"]] = $news->id;
- $s->save();
- $news->region_2_position = '';
- }
- }
- // Handle social media posting
- if (!isset($input['no_social'])) {
- Log::info('Processing social media notifications');
- try {
- @$news->notify(new FacebookPost());
- $news->notify(new TwitterPost());
- $news->published = true;
- $news->save();
- Log::info('Social media notifications sent');
- } catch (\Exception $e) {
- Log::error('Error sending social notifications:', [$e->getMessage()]);
- }
- } else {
- Log::info('Skipping social media (no_social flag set)');
- }
- }
- if (isset($input['unpublish'])) {
- Log::info('Processing unpublish action');
- $news->online = false;
- $news->save();
- }
- // Handle homepage position
- if (isset($input["homepage_position"]) && $input["homepage_position"] != '' && $news->online) {
- Log::info('Processing homepage position:', [$input["homepage_position"]]);
- $home = Home::first();
- if ($home != null) {
- $home->fill([
- $input["homepage_position"] => $news->id
- ]);
- $home->save();
- Log::info('Homepage position saved');
- }
- }
- // Determine redirect based on action
- Log::info('=== DETERMINING REDIRECT ===');
- if (isset($input['publish'])) {
- Log::info('Redirecting after publish to news.index');
- return redirect()->route('news.index')->with('success', 'News published successfully');
- }
- if (isset($input['unpublish'])) {
- Log::info('Redirecting after unpublish to news.index');
- return redirect()->route('news.index')->with('success', 'News unpublished successfully');
- }
- if (isset($input['save'])) {
- Log::info('Redirecting after save to news.edit');
- return redirect()->route('news.edit', $news->id)->with('success', 'News saved successfully');
- }
- if (isset($input['save_exit'])) {
- Log::info('Redirecting after save_exit to news.index');
- return redirect()->route('news.index')->with('success', 'News saved successfully');
- }
- Log::info('No specific action, redirecting to news.index');
- return redirect()->route('news.index')->with('success', 'News created successfully');
- } catch (\Exception $e) {
- Log::error('=== ERROR IN STORE METHOD ===');
- Log::error('Error message:', [$e->getMessage()]);
- Log::error('Error file:', [$e->getFile()]);
- Log::error('Error line:', [$e->getLine()]);
- Log::error('Stack trace:', [$e->getTraceAsString()]);
- return redirect()->back()
- ->withInput()
- ->with('error', 'An error occurred while saving the news: ' . $e->getMessage());
- }
- }
- /**
- * Display the specified resource.
- *
- * @param \App\News $news
- * @return \Illuminate\Http\Response
- */
- public function show(News $news)
- {
- return view('news.show', compact('news'));
- }
- /**
- * Show the form for editing the specified resource.
- *
- * @param \App\News $news
- * @return \Illuminate\Http\Response
- */
- public function edit(News $news)
- {
- // Aggiorno l'utente che sta guardando la news
- if ($news->user_id == null) {
- session(['current_news' => $news->id]);
- $news->user_id = Auth::user()->id;
- $news->save();
- }
- $sections = Section::where('type', '=', 'section')->orderBy('position')->pluck('name', 'id')->toArray();;
- $regions = Section::where('type', '=', 'region')->orderBy('name')->pluck('name', 'id')->toArray();;
- $layouts = Section::pluck('layout', 'id')->toArray();
- $events = Event::orderBy('title')->pluck('title', 'id')->toArray();;
- $pages = Page::where('online', '=', true)->orderBy('title')->pluck('title', 'id')->toArray();;
- $final_date = null;
- if ($news->date != null) {
- list($dt, $time) = explode(" ", $news->date);
- list($year, $month, $day) = explode("-", $dt);
- $final_date = $day . "/" . $month . "/" . $year . " " . $time;
- }
- $section_position = $news->section_position;
- $region_1_position = $news->region_1_position;
- $region_2_position = $news->region_2_position;
- $first = '';
- $aFirst = array();
- $home = Home::first();
- if ($home != null) {
- $pos = '';
- if ($home->slide1 == $news->id)
- $news->homepage_position = "slide1";
- if ($home->left1 == $news->id)
- $news->homepage_position = "left1";
- if ($home->left2 == $news->id)
- $news->homepage_position = "left2";
- if ($home->left3 == $news->id)
- $news->homepage_position = "left3";
- if ($home->right1 == $news->id)
- $news->homepage_position = "right1";
- if ($home->right2 == $news->id)
- $news->homepage_position = "right2";
- if ($home->right3 == $news->id)
- $news->homepage_position = "right3";
- $home->loadData();
- if (isset($home->slide1) && $home->slide1 != '')
- $aFirst[] = $home->slide1->title . " (immagine grande)";
- for ($a = 1; $a <= 5; $a++) {
- if (isset($home["left" . $a]) && $home["left" . $a] != '')
- $aFirst[] = $home["left" . $a]["title"] . " (slide sinistra)";
- }
- for ($a = 1; $a <= 5; $a++) {
- if (isset($home["right" . $a]) && $home["right" . $a] != '')
- $aFirst[] = $home["right" . $a]["title"] . " (slide destra)";
- }
- $first = '<li>' . implode("</li><li>", $aFirst) . '</li>';
- }
- $news->date = $final_date;
- return view('news.edit', compact('news', 'sections', 'regions', 'pages', 'events', 'layouts', 'section_position', 'region_1_position', 'region_2_position', 'first'));
- }
- /**
- * Update the specified resource in storage.
- *
- * @param \Illuminate\Http\Request $request
- * @param \App\News $news
- * @return \Illuminate\Http\Response
- */
- public function update(Request $request, News $news)
- {
- Log::info('=== NEWS UPDATE METHOD START ===');
- Log::info('News ID:', [$news->id]);
- Log::info('Request data:', $request->all());
- $request->validate($this->rules);
- Log::info('Validation passed');
- $input = $request->all();
- $old_section_id = $news->section_id;
- $old_region_1_id = $news->region_1_id;
- $old_region_2_id = $news->region_2_id;
- $old_event_id = $news->event_id;
- $section_position = '';
- if ($news->section_id > 0) {
- if ($news->section->big == $news->id)
- $section_position = 'big';
- if ($news->section->small1 == $news->id)
- $section_position = 'small1';
- if ($news->section->small2 == $news->id)
- $section_position = 'small2';
- if ($news->section->small3 == $news->id)
- $section_position = 'small3';
- if ($news->section->small4 == $news->id)
- $section_position = 'small4';
- if ($news->section->small5 == $news->id)
- $section_position = 'small5';
- if ($news->section->small6 == $news->id)
- $section_position = 'small6';
- if ($section_position == null)
- $section_position = $news->section_position;
- }
- $region_1_position = '';
- if ($news->region_1_id > 0) {
- if ($news->region_1->big == $news->id)
- $region_1_position = 'big';
- if ($news->region_1->small1 == $news->id)
- $region_1_position = 'small1';
- if ($news->region_1->small2 == $news->id)
- $region_1_position = 'small2';
- if ($news->region_1->small3 == $news->id)
- $region_1_position = 'small3';
- if ($news->region_1->small4 == $news->id)
- $region_1_position = 'small4';
- if ($news->region_1->small5 == $news->id)
- $region_1_position = 'small5';
- if ($news->region_1->small6 == $news->id)
- $region_1_position = 'small6';
- if ($region_1_position == '')
- $region_1_position = $news->region_1_position;
- }
- $region_2_position = '';
- if ($news->region_2_id > 0) {
- if ($news->region_2->big == $news->id)
- $region_2_position = 'big';
- if ($news->region_2->small1 == $news->id)
- $region_2_position = 'small1';
- if ($news->region_2->small2 == $news->id)
- $region_2_position = 'small2';
- if ($news->region_2->small3 == $news->id)
- $region_2_position = 'small3';
- if ($news->region_2->small4 == $news->id)
- $region_2_position = 'small4';
- if ($news->region_2->small5 == $news->id)
- $region_2_position = 'small5';
- if ($news->region_2->small6 == $news->id)
- $region_2_position = 'small6';
- if ($region_2_position == '')
- $region_2_position = $news->region_2_position;
- }
- if ($old_section_id == $input["section_id"] && $section_position == $input["section_position"])
- unset($input["section_position"]);
- if ($old_region_1_id == $input["region_1_id"] && $region_1_position == $input["region_1_position"])
- unset($input["region_1_position"]);
- if ($old_region_2_id == $input["region_2_id"] && $region_2_position == $input["region_2_position"])
- unset($input["region_2_position"]);
- if (request()->image) {
- Log::info('Processing image update');
- $file = request()->image;
- $filename = time() . '_' . $file->getClientOriginalName();
- if (! File::exists(public_path() . "/files/news"))
- File::makeDirectory(public_path() . "/files/news");
- $path = public_path('files/news');
- request()->image->move($path, $filename);
- $input["image"] = $filename;
- }
- for ($i = 1; $i <= 5; $i++) {
- if (isset($input["image" . $i])) {
- $file = $input["image" . $i];
- $filename = time() . '_' . $file->getClientOriginalName();
- if (! File::exists(public_path() . "/files/news"))
- File::makeDirectory(public_path() . "/files/news");
- $path = public_path('files/news');
- $input["image" . $i]->move($path, $filename);
- $input["image" . $i] = $filename;
- }
- }
- if (isset($input["remove_pdf"])) {
- $input["pdf"] = '';
- }
- if (request()->pdf) {
- $file = request()->pdf;
- $filename = time() . '_' . $file->getClientOriginalName();
- if (! File::exists(public_path() . "/files/news"))
- File::makeDirectory(public_path() . "/files/news");
- $path = public_path('files/news');
- request()->pdf->move($path, $filename);
- $input["pdf"] = $filename;
- }
- $final_date = null;
- if ($input["date"] != '') {
- list($dt, $time) = explode(" ", $input["date"]);
- list($day, $month, $year) = explode("/", $dt);
- $final_date = $year . "-" . $month . "-" . $day . " " . $time;
- Log::info('Date processed for update:', [$input["date"], $final_date]);
- }
- $input["date"] = $final_date;
- // $input["online"] = isset($input["online"]) ? ($input["online"] == 'on' ? true : false) : false;
- $input["homepage"] = isset($input["homepage"]) ? ($input["homepage"] == 'on' ? true : false) : false;
- $input["live"] = isset($input["live"]) ? ($input["live"] == 'on' ? true : false) : false;
- $input["breaking_news"] = isset($input["breaking_news"]) ? ($input["breaking_news"] == 'on' ? true : false) : false;
- Log::info('About to update news record');
- $news->update($input);
- Log::info('News updated');
- $news->user_id = null;
- $news->save();
- $request->session()->forget('current_news');
- if (isset($input['publish'])) {
- $news->online = true;
- $news->save();
- if (isset($input['no_social'])) {
- // Non pubblico sui social
- } else {
- $news->published = true;
- $news->save();
- }
- }
- if (isset($input['unpublish'])) {
- $news->online = false;
- $news->save();
- }
- if ($_POST["homepage_position"] != '' && $news->online) {
- $home = Home::first();
- if ($home != null) {
- $home->fill([
- $_POST["homepage_position"] => $news->id
- ]);
- $home->save();
- }
- }
- if ($news->event_id != $old_event_id) {
- $e = Event::where('id', '=', $old_event_id)->first();
- if ($e) {
- if ($e->big == $news->id)
- $e->big = '';
- if ($e->small1 == $news->id)
- $e->small1 = '';
- if ($e->small2 == $news->id)
- $e->small2 = '';
- if ($e->small3 == $news->id)
- $e->small3 = '';
- if ($e->small4 == $news->id)
- $e->small4 = '';
- $e->save();
- }
- }
- Log::info('=== DETERMINING REDIRECT FOR UPDATE ===');
- if (isset($input['publish'])) {
- Log::info('Redirecting after publish to news.index');
- return redirect()->route('news.index')->with('success', 'News published successfully');
- }
- if (isset($input['crop'])) {
- Log::info('Redirecting to crop page');
- return redirect('/admin/news/crop?news_id=' . $news->id);
- }
- if (isset($input['unpublish'])) {
- Log::info('Redirecting after unpublish to news.index');
- return redirect()->route('news.index')->with('success', 'News unpublished successfully');
- }
- if (isset($input['save'])) {
- Log::info('Redirecting after save to news.edit');
- return redirect()->route('news.edit', $news->id)->with('success', 'News updated successfully');
- }
- if (isset($input['save_exit'])) {
- Log::info('Redirecting after save_exit to news.index');
- return redirect()->route('news.index')->with('success', 'News updated successfully');
- }
- Log::info('No specific action, redirecting to news.edit');
- return redirect()->route('news.edit', $news->id)->with('success', 'News updated successfully');
- }
- /**
- * Remove the specified resource from storage.
- *
- * @param \App\News $news
- * @return \Illuminate\Http\Response
- */
- public function destroy(News $news)
- {
- $section = null;
- if (isset($news->section)) {
- $section = $news->section;
- }
- $region_1 = null;
- if (isset($news->region_1)) {
- $region_1 = $news->region_1;
- }
- $region_2 = null;
- if (isset($news->region_2)) {
- $region_2 = $news->region_2;
- }
- $news->delete();
- return redirect()->route('news.index')
- ->with('success', 'News deleted successfully');
- }
- }
|