NewsController.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\News;
  4. use App\Section;
  5. use App\Event;
  6. use App\Page;
  7. use App\Home;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Support\Facades\File;
  10. use Illuminate\Support\Facades\Auth;
  11. use App\Notifications\FacebookPost;
  12. use App\Notifications\TwitterPost;
  13. use Illuminate\Support\Facades\Log;
  14. class NewsController extends Controller
  15. {
  16. protected $rules = [
  17. //'section_id' => ['required']
  18. 'title' => ['required'],
  19. //'page_id' => ['required'],
  20. ];
  21. public function index(Request $request)
  22. {
  23. // Se stavo modificando una news metto a null il campo
  24. if ($request->session()->has('current_news')) {
  25. $news_id = session('current_news', 0);
  26. if ($news_id > 0) {
  27. $n = News::where('id', '=', $news_id)->take(1000)->get();
  28. if ($n->count() > 0) {
  29. $n = $n->first();
  30. if ($n->user_id == Auth::user()->id) {
  31. $n->user_id = null;
  32. $n->save();
  33. }
  34. }
  35. $request->session()->forget('current_news');
  36. }
  37. }
  38. $news = News::orderBy('date', 'DESC')->take(1000)->get(); //->paginate(50);
  39. return view('news.index', compact('news'))
  40. ->with('i', (request()->input('page', 1) - 1) * 5);
  41. }
  42. public function load_json()
  43. {
  44. $start = $_GET["start"];
  45. $end = 50; //$_GET["length"];
  46. $total = News::count();
  47. $filtered = $total;
  48. $sort_by = '';
  49. $sort = $_GET["order"][0]["column"];
  50. switch ($sort) {
  51. case '0':
  52. $sort_by = 'date';
  53. break;
  54. case '2':
  55. $sort_by = 'title';
  56. break;
  57. case '3':
  58. $sort_by = 'name';
  59. break;
  60. case '4':
  61. $sort_by = 'name';
  62. break;
  63. case '5':
  64. $sort_by = 'name';
  65. break;
  66. case '6':
  67. $sort_by = 'title';
  68. break;
  69. case '7':
  70. $sort_by = 'online';
  71. break;
  72. case '8':
  73. $sort_by = 'clicks';
  74. break;
  75. default:
  76. $sort_by = 'date';
  77. break;
  78. }
  79. $sort_by_dir = $_GET["order"][0]["dir"];
  80. $search = $_GET["search"]["value"];
  81. if ($sort == '3') {
  82. $news = News::with(['section' => function ($query) use ($sort_by, $sort_by_dir) {
  83. $query->orderBy($sort_by, $sort_by_dir);
  84. }]);
  85. } else if ($sort == '4') {
  86. $news = News::with(['region_1' => function ($query) use ($sort_by, $sort_by_dir) {
  87. $query->orderBy($sort_by, $sort_by_dir);
  88. }]);
  89. } else if ($sort == '5') {
  90. $news = News::with(['region_2' => function ($query) use ($sort_by, $sort_by_dir) {
  91. $query->orderBy($sort_by, $sort_by_dir);
  92. }]);
  93. } else if ($sort == '6') {
  94. $news = News::with(['event' => function ($query) use ($sort_by, $sort_by_dir) {
  95. $query->orderBy($sort_by, $sort_by_dir);
  96. }]);
  97. } else
  98. $news = News::orderBy($sort_by, $sort_by_dir);
  99. if ($search != '') {
  100. $news = $news->where('title', 'LIKE', '%' . $search . '%');
  101. $filtered = $news->count();
  102. }
  103. $news = $news->limit($end)->offset($start)->get(); //->paginate(50);
  104. $aData = array();
  105. foreach ($news as $n) {
  106. $x = array();
  107. $x[] = $n->date;
  108. if ($n->image != '')
  109. $x[] = '<img src="/files/news/' . $n->image . '" style="max-width:100px" />';
  110. else
  111. $x[] = '';
  112. $x[] = $n->title;
  113. $section = @$n->section->name;
  114. if ($n->section)
  115. $section .= '<br>' . isset($n->section) ? $n->section->position($n->id) : '';
  116. $x[] = $section;
  117. $region_1 = @$n->region_1->name;
  118. if ($n->region_1)
  119. $region_1 .= '<br>' . isset($n->region_1) ? $n->region_1->position($n->id) : '';
  120. $x[] = $region_1;
  121. $region_2 = @$n->region_2->name;
  122. if ($n->region_2)
  123. $region_2 .= '<br>' . isset($n->region_2) ? $n->region_2->position($n->id) : '';
  124. $x[] = $region_2;
  125. //$x[] = @$n->event->title;
  126. $x[] = @$n->online ? '<a si class="btn btn-w-m btn-default" href="/admin/news/status/' . $n->id . '/offline">&nbsp;Si&nbsp;</a>' : '<a no class="btn btn-w-m btn-default" href="/admin/news/status/' . $n->id . '/online">No</a>';
  127. $x[] = @$n->clicks;
  128. $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>';
  129. $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>';
  130. $form = '<form method="POST" action="/admin/news/' . $n->id . '" accept-charset="UTF-8" class="form-inline">
  131. <input name="_method" type="hidden" value="DELETE">
  132. <input name="_token" type="hidden" value="' . csrf_token() . '">
  133. <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>
  134. </form>';
  135. $x[] = $form;
  136. $aData[] = $x;
  137. }
  138. $aRet = array('recordsTotal' => $total, 'recordsFiltered' => $filtered, 'data' => $aData);
  139. return json_encode($aRet);
  140. }
  141. /**
  142. * Show the form for creating a new resource.
  143. *
  144. * @return \Illuminate\Http\Response
  145. */
  146. public function create()
  147. {
  148. $sections = Section::where('type', '=', 'section')->orderBy('position')->pluck('name', 'id')->toArray();;
  149. $regions = Section::where('type', '=', 'region')->orderBy('name')->pluck('name', 'id')->toArray();;
  150. $layouts = Section::pluck('layout', 'id')->toArray();
  151. $events = Event::orderBy('title')->pluck('title', 'id')->toArray();;
  152. $pages = Page::where('online', '=', true)->orderBy('title')->pluck('title', 'id')->toArray();;
  153. $section_position = '';
  154. $region_1_position = '';
  155. $region_2_position = '';
  156. $first = '';
  157. $aFirst = array();
  158. $home = Home::first();
  159. if ($home != null) {
  160. $pos = '';
  161. $home->loadData();
  162. if (isset($home->slide1) && $home->slide1 != '')
  163. $aFirst[] = $home->slide1->title . " (immagine grande)";
  164. for ($a = 1; $a <= 5; $a++) {
  165. if (isset($home["left" . $a]) && $home["left" . $a] != '')
  166. $aFirst[] = $home["left" . $a]["title"] . " (slide sinistra)";
  167. }
  168. for ($a = 1; $a <= 5; $a++) {
  169. if (isset($home["right" . $a]) && $home["right" . $a] != '')
  170. $aFirst[] = $home["right" . $a]["title"] . " (slide destra)";
  171. }
  172. $first = '<li>' . implode("</li><li>", $aFirst) . '</li>';
  173. }
  174. return view('news.create', compact('sections', 'regions', 'events', 'pages', 'layouts', 'section_position', 'region_1_position', 'region_2_position', 'first'));
  175. }
  176. /**
  177. * Store a newly created resource in storage.
  178. *
  179. * @param \Illuminate\Http\Request $request
  180. * @return \Illuminate\Http\Response
  181. */
  182. public function store(Request $request)
  183. {
  184. Log::info('=== NEWS STORE METHOD START ===');
  185. Log::info('Request data:', $request->all());
  186. Log::info('Request method:', [$request->method()]);
  187. try {
  188. $request->validate($this->rules);
  189. Log::info('Validation passed');
  190. $input = $request->all();
  191. Log::info('Input data:', $input);
  192. // Check which button was clicked
  193. if (isset($input['save'])) {
  194. Log::info('SAVE button clicked');
  195. }
  196. if (isset($input['save_exit'])) {
  197. Log::info('SAVE_EXIT button clicked');
  198. }
  199. if (isset($input['publish'])) {
  200. Log::info('PUBLISH button clicked');
  201. }
  202. if (isset($input['unpublish'])) {
  203. Log::info('UNPUBLISH button clicked');
  204. }
  205. // Image handling
  206. if (request()->image) {
  207. Log::info('Processing main image upload');
  208. $file = request()->image;
  209. $filename = time() . '_' . $file->getClientOriginalName();
  210. if (! File::exists(public_path() . "/files/news"))
  211. File::makeDirectory(public_path() . "/files/news");
  212. $path = public_path('files/news');
  213. request()->image->move($path, $filename);
  214. $input["image"] = $filename;
  215. Log::info('Main image uploaded:', [$filename]);
  216. } else {
  217. if (isset($input["filename"])) {
  218. $input["image"] = $input["filename"];
  219. Log::info('Using existing filename:', [$input["filename"]]);
  220. }
  221. }
  222. // Handle additional images
  223. for ($i = 1; $i <= 5; $i++) {
  224. if (isset($input["image" . $i])) {
  225. Log::info("Processing image{$i} upload");
  226. $file = $input["image" . $i];
  227. $filename = time() . '_' . $file->getClientOriginalName();
  228. if (! File::exists(public_path() . "/files/news"))
  229. File::makeDirectory(public_path() . "/files/news");
  230. $path = public_path('files/news');
  231. $input["image" . $i]->move($path, $filename);
  232. $input["image" . $i] = $filename;
  233. Log::info("Image{$i} uploaded:", [$filename]);
  234. }
  235. }
  236. // Handle PDF
  237. if (request()->pdf) {
  238. Log::info('Processing PDF upload');
  239. $file = request()->pdf;
  240. $filename = time() . '_' . $file->getClientOriginalName();
  241. if (! File::exists(public_path() . "/files/news"))
  242. File::makeDirectory(public_path() . "/files/news");
  243. $path = public_path('files/news');
  244. request()->pdf->move($path, $filename);
  245. $input["pdf"] = $filename;
  246. Log::info('PDF uploaded:', [$filename]);
  247. }
  248. // Date processing
  249. $final_date = null;
  250. if ($input["date"] != '') {
  251. list($dt, $time) = explode(" ", $input["date"]);
  252. list($day, $month, $year) = explode("/", $dt);
  253. $final_date = $year . "-" . $month . "-" . $day . " " . $time;
  254. Log::info('Date processed:', [$input["date"], $final_date]);
  255. }
  256. $input["date"] = $final_date;
  257. // Handle checkboxes
  258. $input["online"] = false;
  259. $input["homepage"] = isset($input["homepage"]) ? ($input["homepage"] == 'on' ? true : false) : false;
  260. $input["live"] = isset($input["live"]) ? ($input["live"] == 'on' ? true : false) : false;
  261. $input["breaking_news"] = isset($input["breaking_news"]) ? ($input["breaking_news"] == 'on' ? true : false) : false;
  262. $input["no_social"] = isset($input["no_social"]) ? ($input["no_social"] == 'on' ? true : false) : false;
  263. Log::info('Checkbox values:', [
  264. 'homepage' => $input["homepage"],
  265. 'live' => $input["live"],
  266. 'breaking_news' => $input["breaking_news"],
  267. 'no_social' => $input["no_social"]
  268. ]);
  269. Log::info('About to create news record');
  270. $news = News::create($input);
  271. Log::info('News created with ID:', [$news->id]);
  272. // Handle publish action
  273. if (isset($input['publish'])) {
  274. Log::info('Processing publish action');
  275. $news->online = true;
  276. $news->save();
  277. Log::info('News set to online');
  278. // Position the news if date is not in future
  279. if ($final_date <= date("Y-m-d H:i:s")) {
  280. Log::info('Date is not in future, processing positions');
  281. if ($input["section_position"] != '') {
  282. Log::info('Processing section position:', [$input["section_position"]]);
  283. $s = Section::findOrFail($input["section_id"]);
  284. $s[$input["section_position"]] = $news->id;
  285. $s->save();
  286. $news->section_position = '';
  287. }
  288. if ($input["region_1_position"] != '') {
  289. Log::info('Processing region_1 position:', [$input["region_1_position"]]);
  290. $s = Section::findOrFail($input["region_1_id"]);
  291. $s[$input["region_1_position"]] = $news->id;
  292. $s->save();
  293. $news->region_1_position = '';
  294. }
  295. if ($input["region_2_position"] != '') {
  296. Log::info('Processing region_2 position:', [$input["region_2_position"]]);
  297. $s = Section::findOrFail($input["region_2_id"]);
  298. $s[$input["region_2_position"]] = $news->id;
  299. $s->save();
  300. $news->region_2_position = '';
  301. }
  302. }
  303. // Handle social media posting
  304. if (!isset($input['no_social'])) {
  305. Log::info('Processing social media notifications');
  306. try {
  307. @$news->notify(new FacebookPost());
  308. $news->notify(new TwitterPost());
  309. $news->published = true;
  310. $news->save();
  311. Log::info('Social media notifications sent');
  312. } catch (\Exception $e) {
  313. Log::error('Error sending social notifications:', [$e->getMessage()]);
  314. }
  315. } else {
  316. Log::info('Skipping social media (no_social flag set)');
  317. }
  318. }
  319. if (isset($input['unpublish'])) {
  320. Log::info('Processing unpublish action');
  321. $news->online = false;
  322. $news->save();
  323. }
  324. // Handle homepage position
  325. if ($input["homepage_position"] != '' && $news->online) {
  326. Log::info('Processing homepage position:', [$input["homepage_position"]]);
  327. $home = Home::first();
  328. if ($home != null) {
  329. $home->fill([
  330. $input["homepage_position"] => $news->id
  331. ]);
  332. $home->save();
  333. Log::info('Homepage position saved');
  334. }
  335. }
  336. // Determine redirect based on action
  337. Log::info('=== DETERMINING REDIRECT ===');
  338. if (isset($input['publish'])) {
  339. Log::info('Redirecting after publish to news.index');
  340. return redirect()->route('news.index')->with('success', 'News published successfully');
  341. }
  342. if (isset($input['unpublish'])) {
  343. Log::info('Redirecting after unpublish to news.index');
  344. return redirect()->route('news.index')->with('success', 'News unpublished successfully');
  345. }
  346. if (isset($input['save'])) {
  347. Log::info('Redirecting after save to news.edit');
  348. return redirect()->route('news.edit', $news->id)->with('success', 'News saved successfully');
  349. }
  350. if (isset($input['save_exit'])) {
  351. Log::info('Redirecting after save_exit to news.index');
  352. return redirect()->route('news.index')->with('success', 'News saved successfully');
  353. }
  354. Log::info('No specific action, redirecting to news.index');
  355. return redirect()->route('news.index')->with('success', 'News created successfully');
  356. } catch (\Exception $e) {
  357. Log::error('=== ERROR IN STORE METHOD ===');
  358. Log::error('Error message:', [$e->getMessage()]);
  359. Log::error('Error file:', [$e->getFile()]);
  360. Log::error('Error line:', [$e->getLine()]);
  361. Log::error('Stack trace:', [$e->getTraceAsString()]);
  362. return redirect()->back()
  363. ->withInput()
  364. ->with('error', 'An error occurred while saving the news: ' . $e->getMessage());
  365. }
  366. }
  367. /**
  368. * Display the specified resource.
  369. *
  370. * @param \App\News $news
  371. * @return \Illuminate\Http\Response
  372. */
  373. public function show(News $news)
  374. {
  375. return view('news.show', compact('news'));
  376. }
  377. /**
  378. * Show the form for editing the specified resource.
  379. *
  380. * @param \App\News $news
  381. * @return \Illuminate\Http\Response
  382. */
  383. public function edit(News $news)
  384. {
  385. // Aggiorno l'utente che sta guardando la news
  386. if ($news->user_id == null) {
  387. session(['current_news' => $news->id]);
  388. $news->user_id = Auth::user()->id;
  389. $news->save();
  390. }
  391. $sections = Section::where('type', '=', 'section')->orderBy('position')->pluck('name', 'id')->toArray();;
  392. $regions = Section::where('type', '=', 'region')->orderBy('name')->pluck('name', 'id')->toArray();;
  393. $layouts = Section::pluck('layout', 'id')->toArray();
  394. $events = Event::orderBy('title')->pluck('title', 'id')->toArray();;
  395. $pages = Page::where('online', '=', true)->orderBy('title')->pluck('title', 'id')->toArray();;
  396. $final_date = null;
  397. if ($news->date != null) {
  398. list($dt, $time) = explode(" ", $news->date);
  399. list($year, $month, $day) = explode("-", $dt);
  400. $final_date = $day . "/" . $month . "/" . $year . " " . $time;
  401. }
  402. $section_position = $news->section_position;
  403. $region_1_position = $news->region_1_position;
  404. $region_2_position = $news->region_2_position;
  405. $first = '';
  406. $aFirst = array();
  407. $home = Home::first();
  408. if ($home != null) {
  409. $pos = '';
  410. if ($home->slide1 == $news->id)
  411. $news->homepage_position = "slide1";
  412. if ($home->left1 == $news->id)
  413. $news->homepage_position = "left1";
  414. if ($home->left2 == $news->id)
  415. $news->homepage_position = "left2";
  416. if ($home->left3 == $news->id)
  417. $news->homepage_position = "left3";
  418. if ($home->right1 == $news->id)
  419. $news->homepage_position = "right1";
  420. if ($home->right2 == $news->id)
  421. $news->homepage_position = "right2";
  422. if ($home->right3 == $news->id)
  423. $news->homepage_position = "right3";
  424. $home->loadData();
  425. if (isset($home->slide1) && $home->slide1 != '')
  426. $aFirst[] = $home->slide1->title . " (immagine grande)";
  427. for ($a = 1; $a <= 5; $a++) {
  428. if (isset($home["left" . $a]) && $home["left" . $a] != '')
  429. $aFirst[] = $home["left" . $a]["title"] . " (slide sinistra)";
  430. }
  431. for ($a = 1; $a <= 5; $a++) {
  432. if (isset($home["right" . $a]) && $home["right" . $a] != '')
  433. $aFirst[] = $home["right" . $a]["title"] . " (slide destra)";
  434. }
  435. $first = '<li>' . implode("</li><li>", $aFirst) . '</li>';
  436. }
  437. $news->date = $final_date;
  438. return view('news.edit', compact('news', 'sections', 'regions', 'pages', 'events', 'layouts', 'section_position', 'region_1_position', 'region_2_position', 'first'));
  439. }
  440. /**
  441. * Update the specified resource in storage.
  442. *
  443. * @param \Illuminate\Http\Request $request
  444. * @param \App\News $news
  445. * @return \Illuminate\Http\Response
  446. */
  447. public function update(Request $request, News $news)
  448. {
  449. Log::info('=== NEWS UPDATE METHOD START ===');
  450. Log::info('News ID:', [$news->id]);
  451. Log::info('Request data:', $request->all());
  452. $request->validate($this->rules);
  453. Log::info('Validation passed');
  454. $input = $request->all();
  455. $old_section_id = $news->section_id;
  456. $old_region_1_id = $news->region_1_id;
  457. $old_region_2_id = $news->region_2_id;
  458. $old_event_id = $news->event_id;
  459. $section_position = '';
  460. if ($news->section_id > 0) {
  461. if ($news->section->big == $news->id)
  462. $section_position = 'big';
  463. if ($news->section->small1 == $news->id)
  464. $section_position = 'small1';
  465. if ($news->section->small2 == $news->id)
  466. $section_position = 'small2';
  467. if ($news->section->small3 == $news->id)
  468. $section_position = 'small3';
  469. if ($news->section->small4 == $news->id)
  470. $section_position = 'small4';
  471. if ($news->section->small5 == $news->id)
  472. $section_position = 'small5';
  473. if ($news->section->small6 == $news->id)
  474. $section_position = 'small6';
  475. if ($section_position == null)
  476. $section_position = $news->section_position;
  477. }
  478. $region_1_position = '';
  479. if ($news->region_1_id > 0) {
  480. if ($news->region_1->big == $news->id)
  481. $region_1_position = 'big';
  482. if ($news->region_1->small1 == $news->id)
  483. $region_1_position = 'small1';
  484. if ($news->region_1->small2 == $news->id)
  485. $region_1_position = 'small2';
  486. if ($news->region_1->small3 == $news->id)
  487. $region_1_position = 'small3';
  488. if ($news->region_1->small4 == $news->id)
  489. $region_1_position = 'small4';
  490. if ($news->region_1->small5 == $news->id)
  491. $region_1_position = 'small5';
  492. if ($news->region_1->small6 == $news->id)
  493. $region_1_position = 'small6';
  494. if ($region_1_position == '')
  495. $region_1_position = $news->region_1_position;
  496. }
  497. $region_2_position = '';
  498. if ($news->region_2_id > 0) {
  499. if ($news->region_2->big == $news->id)
  500. $region_2_position = 'big';
  501. if ($news->region_2->small1 == $news->id)
  502. $region_2_position = 'small1';
  503. if ($news->region_2->small2 == $news->id)
  504. $region_2_position = 'small2';
  505. if ($news->region_2->small3 == $news->id)
  506. $region_2_position = 'small3';
  507. if ($news->region_2->small4 == $news->id)
  508. $region_2_position = 'small4';
  509. if ($news->region_2->small5 == $news->id)
  510. $region_2_position = 'small5';
  511. if ($news->region_2->small6 == $news->id)
  512. $region_2_position = 'small6';
  513. if ($region_2_position == '')
  514. $region_2_position = $news->region_2_position;
  515. }
  516. if ($old_section_id == $input["section_id"] && $section_position == $input["section_position"])
  517. unset($input["section_position"]);
  518. if ($old_region_1_id == $input["region_1_id"] && $region_1_position == $input["region_1_position"])
  519. unset($input["region_1_position"]);
  520. if ($old_region_2_id == $input["region_2_id"] && $region_2_position == $input["region_2_position"])
  521. unset($input["region_2_position"]);
  522. if (request()->image) {
  523. Log::info('Processing image update');
  524. $file = request()->image;
  525. $filename = time() . '_' . $file->getClientOriginalName();
  526. if (! File::exists(public_path() . "/files/news"))
  527. File::makeDirectory(public_path() . "/files/news");
  528. $path = public_path('files/news');
  529. request()->image->move($path, $filename);
  530. $input["image"] = $filename;
  531. }
  532. for ($i = 1; $i <= 5; $i++) {
  533. if (isset($input["image" . $i])) {
  534. $file = $input["image" . $i];
  535. $filename = time() . '_' . $file->getClientOriginalName();
  536. if (! File::exists(public_path() . "/files/news"))
  537. File::makeDirectory(public_path() . "/files/news");
  538. $path = public_path('files/news');
  539. $input["image" . $i]->move($path, $filename);
  540. $input["image" . $i] = $filename;
  541. }
  542. }
  543. if (isset($input["remove_pdf"])) {
  544. $input["pdf"] = '';
  545. }
  546. if (request()->pdf) {
  547. $file = request()->pdf;
  548. $filename = time() . '_' . $file->getClientOriginalName();
  549. if (! File::exists(public_path() . "/files/news"))
  550. File::makeDirectory(public_path() . "/files/news");
  551. $path = public_path('files/news');
  552. request()->pdf->move($path, $filename);
  553. $input["pdf"] = $filename;
  554. }
  555. $final_date = null;
  556. if ($input["date"] != '') {
  557. list($dt, $time) = explode(" ", $input["date"]);
  558. list($day, $month, $year) = explode("/", $dt);
  559. $final_date = $year . "-" . $month . "-" . $day . " " . $time;
  560. Log::info('Date processed for update:', [$input["date"], $final_date]);
  561. }
  562. $input["date"] = $final_date;
  563. // $input["online"] = isset($input["online"]) ? ($input["online"] == 'on' ? true : false) : false;
  564. $input["homepage"] = isset($input["homepage"]) ? ($input["homepage"] == 'on' ? true : false) : false;
  565. $input["live"] = isset($input["live"]) ? ($input["live"] == 'on' ? true : false) : false;
  566. $input["breaking_news"] = isset($input["breaking_news"]) ? ($input["breaking_news"] == 'on' ? true : false) : false;
  567. Log::info('About to update news record');
  568. $news->update($input);
  569. Log::info('News updated');
  570. $news->user_id = null;
  571. $news->save();
  572. $request->session()->forget('current_news');
  573. if (isset($input['publish'])) {
  574. $news->online = true;
  575. $news->save();
  576. if (isset($input['no_social'])) {
  577. // Non pubblico sui social
  578. } else {
  579. $news->published = true;
  580. $news->save();
  581. }
  582. }
  583. if (isset($input['unpublish'])) {
  584. $news->online = false;
  585. $news->save();
  586. }
  587. if ($_POST["homepage_position"] != '' && $news->online) {
  588. $home = Home::first();
  589. if ($home != null) {
  590. $home->fill([
  591. $_POST["homepage_position"] => $news->id
  592. ]);
  593. $home->save();
  594. }
  595. }
  596. if ($news->event_id != $old_event_id) {
  597. $e = Event::where('id', '=', $old_event_id)->first();
  598. if ($e) {
  599. if ($e->big == $news->id)
  600. $e->big = '';
  601. if ($e->small1 == $news->id)
  602. $e->small1 = '';
  603. if ($e->small2 == $news->id)
  604. $e->small2 = '';
  605. if ($e->small3 == $news->id)
  606. $e->small3 = '';
  607. if ($e->small4 == $news->id)
  608. $e->small4 = '';
  609. $e->save();
  610. }
  611. }
  612. Log::info('=== DETERMINING REDIRECT FOR UPDATE ===');
  613. if (isset($input['publish'])) {
  614. Log::info('Redirecting after publish to news.index');
  615. return redirect()->route('news.index')->with('success', 'News published successfully');
  616. }
  617. if (isset($input['crop'])) {
  618. Log::info('Redirecting to crop page');
  619. return redirect('/admin/news/crop?news_id=' . $news->id);
  620. }
  621. if (isset($input['unpublish'])) {
  622. Log::info('Redirecting after unpublish to news.index');
  623. return redirect()->route('news.index')->with('success', 'News unpublished successfully');
  624. }
  625. if (isset($input['save'])) {
  626. Log::info('Redirecting after save to news.edit');
  627. return redirect()->route('news.edit', $news->id)->with('success', 'News updated successfully');
  628. }
  629. if (isset($input['save_exit'])) {
  630. Log::info('Redirecting after save_exit to news.index');
  631. return redirect()->route('news.index')->with('success', 'News updated successfully');
  632. }
  633. Log::info('No specific action, redirecting to news.edit');
  634. return redirect()->route('news.edit', $news->id)->with('success', 'News updated successfully');
  635. }
  636. /**
  637. * Remove the specified resource from storage.
  638. *
  639. * @param \App\News $news
  640. * @return \Illuminate\Http\Response
  641. */
  642. public function destroy(News $news)
  643. {
  644. $section = null;
  645. if (isset($news->section)) {
  646. $section = $news->section;
  647. }
  648. $region_1 = null;
  649. if (isset($news->region_1)) {
  650. $region_1 = $news->region_1;
  651. }
  652. $region_2 = null;
  653. if (isset($news->region_2)) {
  654. $region_2 = $news->region_2;
  655. }
  656. $news->delete();
  657. return redirect()->route('news.index')
  658. ->with('success', 'News deleted successfully');
  659. }
  660. }