NewsController.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  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. Log::info('Has files:', [$request->hasFile('image')]);
  188. Log::info('Files data:', $request->file());
  189. try {
  190. $request->validate($this->rules);
  191. Log::info('Validation passed');
  192. $input = $request->all();
  193. Log::info('Input data after validation:', $input);
  194. // Check which button was clicked
  195. if (isset($input['save'])) {
  196. Log::info('SAVE button clicked');
  197. }
  198. if (isset($input['save_exit'])) {
  199. Log::info('SAVE_EXIT button clicked');
  200. }
  201. if (isset($input['publish'])) {
  202. Log::info('PUBLISH button clicked');
  203. }
  204. if (isset($input['unpublish'])) {
  205. Log::info('UNPUBLISH button clicked');
  206. }
  207. if (isset($input['crop'])) {
  208. Log::info('CROP button clicked');
  209. }
  210. // === IMAGE HANDLING SECTION ===
  211. Log::info('=== IMAGE HANDLING START ===');
  212. // Check for filename from crop
  213. if (isset($input["filename"]) && $input["filename"] != '') {
  214. Log::info('Filename from crop found:', [$input["filename"]]);
  215. // Verify file exists
  216. $filePath = public_path('files/news/' . $input["filename"]);
  217. $fileExists = file_exists($filePath);
  218. Log::info('Crop file verification:', [
  219. 'filename' => $input["filename"],
  220. 'full_path' => $filePath,
  221. 'exists' => $fileExists,
  222. 'is_readable' => $fileExists ? is_readable($filePath) : false,
  223. 'file_size' => $fileExists ? filesize($filePath) : 0
  224. ]);
  225. if ($fileExists) {
  226. $input["image"] = $input["filename"];
  227. Log::info('Using cropped image filename:', [$input["filename"]]);
  228. } else {
  229. Log::error('Cropped image file does not exist!');
  230. }
  231. }
  232. // Check for direct file upload
  233. elseif ($request->hasFile('image')) {
  234. Log::info('Direct image upload detected');
  235. $file = $request->file('image');
  236. Log::info('Upload file details:', [
  237. 'original_name' => $file->getClientOriginalName(),
  238. 'size' => $file->getSize(),
  239. 'mime_type' => $file->getMimeType(),
  240. 'is_valid' => $file->isValid()
  241. ]);
  242. if ($file->isValid()) {
  243. $filename = time() . '_' . $file->getClientOriginalName();
  244. if (!File::exists(public_path() . "/files/news")) {
  245. Log::info('Creating news directory');
  246. File::makeDirectory(public_path() . "/files/news");
  247. }
  248. $path = public_path('files/news');
  249. $file->move($path, $filename);
  250. $input["image"] = $filename;
  251. Log::info('Direct image uploaded successfully:', [
  252. 'filename' => $filename,
  253. 'path' => $path . '/' . $filename
  254. ]);
  255. } else {
  256. Log::error('Invalid file upload');
  257. }
  258. } else {
  259. Log::info('No image provided (neither crop nor direct upload)');
  260. }
  261. Log::info('Final image value:', [$input["image"] ?? 'none']);
  262. Log::info('=== IMAGE HANDLING END ===');
  263. // Handle additional images
  264. for ($i = 1; $i <= 5; $i++) {
  265. if ($request->hasFile("image" . $i)) {
  266. Log::info("Processing image{$i} upload");
  267. $file = $request->file("image" . $i);
  268. $filename = time() . '_' . $file->getClientOriginalName();
  269. if (!File::exists(public_path() . "/files/news"))
  270. File::makeDirectory(public_path() . "/files/news");
  271. $path = public_path('files/news');
  272. $file->move($path, $filename);
  273. $input["image" . $i] = $filename;
  274. Log::info("Image{$i} uploaded:", [$filename]);
  275. }
  276. }
  277. // Handle PDF
  278. if ($request->hasFile('pdf')) {
  279. Log::info('Processing PDF upload');
  280. $file = $request->file('pdf');
  281. $filename = time() . '_' . $file->getClientOriginalName();
  282. if (!File::exists(public_path() . "/files/news"))
  283. File::makeDirectory(public_path() . "/files/news");
  284. $path = public_path('files/news');
  285. $file->move($path, $filename);
  286. $input["pdf"] = $filename;
  287. Log::info('PDF uploaded:', [$filename]);
  288. }
  289. // Date processing
  290. $final_date = null;
  291. if (isset($input["date"]) && $input["date"] != '') {
  292. list($dt, $time) = explode(" ", $input["date"]);
  293. list($day, $month, $year) = explode("/", $dt);
  294. $final_date = $year . "-" . $month . "-" . $day . " " . $time;
  295. Log::info('Date processed:', [$input["date"], $final_date]);
  296. }
  297. $input["date"] = $final_date;
  298. // Handle checkboxes
  299. $input["online"] = false;
  300. $input["homepage"] = isset($input["homepage"]) ? ($input["homepage"] == 'on' ? true : false) : false;
  301. $input["live"] = isset($input["live"]) ? ($input["live"] == 'on' ? true : false) : false;
  302. $input["breaking_news"] = isset($input["breaking_news"]) ? ($input["breaking_news"] == 'on' ? true : false) : false;
  303. $input["no_social"] = isset($input["no_social"]) ? ($input["no_social"] == 'on' ? true : false) : false;
  304. Log::info('Final input data before saving:', $input);
  305. Log::info('About to create news record');
  306. $news = News::create($input);
  307. Log::info('News created successfully:', [
  308. 'id' => $news->id,
  309. 'title' => $news->title,
  310. 'image' => $news->image,
  311. 'created_at' => $news->created_at
  312. ]);
  313. // Verify the image was saved correctly
  314. if ($news->image) {
  315. $savedImagePath = public_path('files/news/' . $news->image);
  316. $imageExists = file_exists($savedImagePath);
  317. Log::info('Saved news image verification:', [
  318. 'image_field' => $news->image,
  319. 'full_path' => $savedImagePath,
  320. 'exists' => $imageExists,
  321. 'size' => $imageExists ? filesize($savedImagePath) : 0
  322. ]);
  323. }
  324. // Handle publish action
  325. if (isset($input['publish'])) {
  326. Log::info('Processing publish action');
  327. $news->online = true;
  328. $news->save();
  329. Log::info('News set to online');
  330. // Position the news if date is not in future
  331. if ($final_date <= date("Y-m-d H:i:s")) {
  332. Log::info('Date is not in future, processing positions');
  333. if (isset($input["section_position"]) && $input["section_position"] != '') {
  334. Log::info('Processing section position:', [$input["section_position"]]);
  335. $s = Section::findOrFail($input["section_id"]);
  336. $s[$input["section_position"]] = $news->id;
  337. $s->save();
  338. $news->section_position = '';
  339. }
  340. if (isset($input["region_1_position"]) && $input["region_1_position"] != '') {
  341. Log::info('Processing region_1 position:', [$input["region_1_position"]]);
  342. $s = Section::findOrFail($input["region_1_id"]);
  343. $s[$input["region_1_position"]] = $news->id;
  344. $s->save();
  345. $news->region_1_position = '';
  346. }
  347. if (isset($input["region_2_position"]) && $input["region_2_position"] != '') {
  348. Log::info('Processing region_2 position:', [$input["region_2_position"]]);
  349. $s = Section::findOrFail($input["region_2_id"]);
  350. $s[$input["region_2_position"]] = $news->id;
  351. $s->save();
  352. $news->region_2_position = '';
  353. }
  354. }
  355. // Handle social media posting
  356. if (!isset($input['no_social'])) {
  357. Log::info('Processing social media notifications');
  358. try {
  359. @$news->notify(new FacebookPost());
  360. $news->notify(new TwitterPost());
  361. $news->published = true;
  362. $news->save();
  363. Log::info('Social media notifications sent');
  364. } catch (\Exception $e) {
  365. Log::error('Error sending social notifications:', [$e->getMessage()]);
  366. }
  367. } else {
  368. Log::info('Skipping social media (no_social flag set)');
  369. }
  370. }
  371. if (isset($input['unpublish'])) {
  372. Log::info('Processing unpublish action');
  373. $news->online = false;
  374. $news->save();
  375. }
  376. // Handle homepage position
  377. if (isset($input["homepage_position"]) && $input["homepage_position"] != '' && $news->online) {
  378. Log::info('Processing homepage position:', [$input["homepage_position"]]);
  379. $home = Home::first();
  380. if ($home != null) {
  381. $home->fill([
  382. $input["homepage_position"] => $news->id
  383. ]);
  384. $home->save();
  385. Log::info('Homepage position saved');
  386. }
  387. }
  388. // Determine redirect based on action
  389. Log::info('=== DETERMINING REDIRECT ===');
  390. if (isset($input['publish'])) {
  391. Log::info('Redirecting after publish to news.index');
  392. return redirect()->route('news.index')->with('success', 'News published successfully');
  393. }
  394. if (isset($input['unpublish'])) {
  395. Log::info('Redirecting after unpublish to news.index');
  396. return redirect()->route('news.index')->with('success', 'News unpublished successfully');
  397. }
  398. if (isset($input['save'])) {
  399. Log::info('Redirecting after save to news.edit');
  400. return redirect()->route('news.edit', $news->id)->with('success', 'News saved successfully');
  401. }
  402. if (isset($input['save_exit'])) {
  403. Log::info('Redirecting after save_exit to news.index');
  404. return redirect()->route('news.index')->with('success', 'News saved successfully');
  405. }
  406. Log::info('No specific action, redirecting to news.index');
  407. return redirect()->route('news.index')->with('success', 'News created successfully');
  408. } catch (\Exception $e) {
  409. Log::error('=== ERROR IN STORE METHOD ===');
  410. Log::error('Error message:', [$e->getMessage()]);
  411. Log::error('Error file:', [$e->getFile()]);
  412. Log::error('Error line:', [$e->getLine()]);
  413. Log::error('Stack trace:', [$e->getTraceAsString()]);
  414. return redirect()->back()
  415. ->withInput()
  416. ->with('error', 'An error occurred while saving the news: ' . $e->getMessage());
  417. }
  418. }
  419. /**
  420. * Display the specified resource.
  421. *
  422. * @param \App\News $news
  423. * @return \Illuminate\Http\Response
  424. */
  425. public function show(News $news)
  426. {
  427. return view('news.show', compact('news'));
  428. }
  429. /**
  430. * Show the form for editing the specified resource.
  431. *
  432. * @param \App\News $news
  433. * @return \Illuminate\Http\Response
  434. */
  435. public function edit(News $news)
  436. {
  437. // Aggiorno l'utente che sta guardando la news
  438. if ($news->user_id == null) {
  439. session(['current_news' => $news->id]);
  440. $news->user_id = Auth::user()->id;
  441. $news->save();
  442. }
  443. $sections = Section::where('type', '=', 'section')->orderBy('position')->pluck('name', 'id')->toArray();;
  444. $regions = Section::where('type', '=', 'region')->orderBy('name')->pluck('name', 'id')->toArray();;
  445. $layouts = Section::pluck('layout', 'id')->toArray();
  446. $events = Event::orderBy('title')->pluck('title', 'id')->toArray();;
  447. $pages = Page::where('online', '=', true)->orderBy('title')->pluck('title', 'id')->toArray();;
  448. $final_date = null;
  449. if ($news->date != null) {
  450. list($dt, $time) = explode(" ", $news->date);
  451. list($year, $month, $day) = explode("-", $dt);
  452. $final_date = $day . "/" . $month . "/" . $year . " " . $time;
  453. }
  454. $section_position = $news->section_position;
  455. $region_1_position = $news->region_1_position;
  456. $region_2_position = $news->region_2_position;
  457. $first = '';
  458. $aFirst = array();
  459. $home = Home::first();
  460. if ($home != null) {
  461. $pos = '';
  462. if ($home->slide1 == $news->id)
  463. $news->homepage_position = "slide1";
  464. if ($home->left1 == $news->id)
  465. $news->homepage_position = "left1";
  466. if ($home->left2 == $news->id)
  467. $news->homepage_position = "left2";
  468. if ($home->left3 == $news->id)
  469. $news->homepage_position = "left3";
  470. if ($home->right1 == $news->id)
  471. $news->homepage_position = "right1";
  472. if ($home->right2 == $news->id)
  473. $news->homepage_position = "right2";
  474. if ($home->right3 == $news->id)
  475. $news->homepage_position = "right3";
  476. $home->loadData();
  477. if (isset($home->slide1) && $home->slide1 != '')
  478. $aFirst[] = $home->slide1->title . " (immagine grande)";
  479. for ($a = 1; $a <= 5; $a++) {
  480. if (isset($home["left" . $a]) && $home["left" . $a] != '')
  481. $aFirst[] = $home["left" . $a]["title"] . " (slide sinistra)";
  482. }
  483. for ($a = 1; $a <= 5; $a++) {
  484. if (isset($home["right" . $a]) && $home["right" . $a] != '')
  485. $aFirst[] = $home["right" . $a]["title"] . " (slide destra)";
  486. }
  487. $first = '<li>' . implode("</li><li>", $aFirst) . '</li>';
  488. }
  489. $news->date = $final_date;
  490. return view('news.edit', compact('news', 'sections', 'regions', 'pages', 'events', 'layouts', 'section_position', 'region_1_position', 'region_2_position', 'first'));
  491. }
  492. /**
  493. * Update the specified resource in storage.
  494. *
  495. * @param \Illuminate\Http\Request $request
  496. * @param \App\News $news
  497. * @return \Illuminate\Http\Response
  498. */
  499. public function update(Request $request, News $news)
  500. {
  501. Log::info('=== NEWS UPDATE METHOD START ===');
  502. Log::info('News ID:', [$news->id]);
  503. Log::info('Request data:', $request->all());
  504. $request->validate($this->rules);
  505. Log::info('Validation passed');
  506. $input = $request->all();
  507. $old_section_id = $news->section_id;
  508. $old_region_1_id = $news->region_1_id;
  509. $old_region_2_id = $news->region_2_id;
  510. $old_event_id = $news->event_id;
  511. $section_position = '';
  512. if ($news->section_id > 0) {
  513. if ($news->section->big == $news->id)
  514. $section_position = 'big';
  515. if ($news->section->small1 == $news->id)
  516. $section_position = 'small1';
  517. if ($news->section->small2 == $news->id)
  518. $section_position = 'small2';
  519. if ($news->section->small3 == $news->id)
  520. $section_position = 'small3';
  521. if ($news->section->small4 == $news->id)
  522. $section_position = 'small4';
  523. if ($news->section->small5 == $news->id)
  524. $section_position = 'small5';
  525. if ($news->section->small6 == $news->id)
  526. $section_position = 'small6';
  527. if ($section_position == null)
  528. $section_position = $news->section_position;
  529. }
  530. $region_1_position = '';
  531. if ($news->region_1_id > 0) {
  532. if ($news->region_1->big == $news->id)
  533. $region_1_position = 'big';
  534. if ($news->region_1->small1 == $news->id)
  535. $region_1_position = 'small1';
  536. if ($news->region_1->small2 == $news->id)
  537. $region_1_position = 'small2';
  538. if ($news->region_1->small3 == $news->id)
  539. $region_1_position = 'small3';
  540. if ($news->region_1->small4 == $news->id)
  541. $region_1_position = 'small4';
  542. if ($news->region_1->small5 == $news->id)
  543. $region_1_position = 'small5';
  544. if ($news->region_1->small6 == $news->id)
  545. $region_1_position = 'small6';
  546. if ($region_1_position == '')
  547. $region_1_position = $news->region_1_position;
  548. }
  549. $region_2_position = '';
  550. if ($news->region_2_id > 0) {
  551. if ($news->region_2->big == $news->id)
  552. $region_2_position = 'big';
  553. if ($news->region_2->small1 == $news->id)
  554. $region_2_position = 'small1';
  555. if ($news->region_2->small2 == $news->id)
  556. $region_2_position = 'small2';
  557. if ($news->region_2->small3 == $news->id)
  558. $region_2_position = 'small3';
  559. if ($news->region_2->small4 == $news->id)
  560. $region_2_position = 'small4';
  561. if ($news->region_2->small5 == $news->id)
  562. $region_2_position = 'small5';
  563. if ($news->region_2->small6 == $news->id)
  564. $region_2_position = 'small6';
  565. if ($region_2_position == '')
  566. $region_2_position = $news->region_2_position;
  567. }
  568. if ($old_section_id == $input["section_id"] && $section_position == $input["section_position"])
  569. unset($input["section_position"]);
  570. if ($old_region_1_id == $input["region_1_id"] && $region_1_position == $input["region_1_position"])
  571. unset($input["region_1_position"]);
  572. if ($old_region_2_id == $input["region_2_id"] && $region_2_position == $input["region_2_position"])
  573. unset($input["region_2_position"]);
  574. if (request()->image) {
  575. Log::info('Processing image update');
  576. $file = request()->image;
  577. $filename = time() . '_' . $file->getClientOriginalName();
  578. if (! File::exists(public_path() . "/files/news"))
  579. File::makeDirectory(public_path() . "/files/news");
  580. $path = public_path('files/news');
  581. request()->image->move($path, $filename);
  582. $input["image"] = $filename;
  583. }
  584. for ($i = 1; $i <= 5; $i++) {
  585. if (isset($input["image" . $i])) {
  586. $file = $input["image" . $i];
  587. $filename = time() . '_' . $file->getClientOriginalName();
  588. if (! File::exists(public_path() . "/files/news"))
  589. File::makeDirectory(public_path() . "/files/news");
  590. $path = public_path('files/news');
  591. $input["image" . $i]->move($path, $filename);
  592. $input["image" . $i] = $filename;
  593. }
  594. }
  595. if (isset($input["remove_pdf"])) {
  596. $input["pdf"] = '';
  597. }
  598. if (request()->pdf) {
  599. $file = request()->pdf;
  600. $filename = time() . '_' . $file->getClientOriginalName();
  601. if (! File::exists(public_path() . "/files/news"))
  602. File::makeDirectory(public_path() . "/files/news");
  603. $path = public_path('files/news');
  604. request()->pdf->move($path, $filename);
  605. $input["pdf"] = $filename;
  606. }
  607. $final_date = null;
  608. if ($input["date"] != '') {
  609. list($dt, $time) = explode(" ", $input["date"]);
  610. list($day, $month, $year) = explode("/", $dt);
  611. $final_date = $year . "-" . $month . "-" . $day . " " . $time;
  612. Log::info('Date processed for update:', [$input["date"], $final_date]);
  613. }
  614. $input["date"] = $final_date;
  615. // $input["online"] = isset($input["online"]) ? ($input["online"] == 'on' ? true : false) : false;
  616. $input["homepage"] = isset($input["homepage"]) ? ($input["homepage"] == 'on' ? true : false) : false;
  617. $input["live"] = isset($input["live"]) ? ($input["live"] == 'on' ? true : false) : false;
  618. $input["breaking_news"] = isset($input["breaking_news"]) ? ($input["breaking_news"] == 'on' ? true : false) : false;
  619. Log::info('About to update news record');
  620. $news->update($input);
  621. Log::info('News updated');
  622. $news->user_id = null;
  623. $news->save();
  624. $request->session()->forget('current_news');
  625. if (isset($input['publish'])) {
  626. $news->online = true;
  627. $news->save();
  628. if (isset($input['no_social'])) {
  629. // Non pubblico sui social
  630. } else {
  631. $news->published = true;
  632. $news->save();
  633. }
  634. }
  635. if (isset($input['unpublish'])) {
  636. $news->online = false;
  637. $news->save();
  638. }
  639. if ($_POST["homepage_position"] != '' && $news->online) {
  640. $home = Home::first();
  641. if ($home != null) {
  642. $home->fill([
  643. $_POST["homepage_position"] => $news->id
  644. ]);
  645. $home->save();
  646. }
  647. }
  648. if ($news->event_id != $old_event_id) {
  649. $e = Event::where('id', '=', $old_event_id)->first();
  650. if ($e) {
  651. if ($e->big == $news->id)
  652. $e->big = '';
  653. if ($e->small1 == $news->id)
  654. $e->small1 = '';
  655. if ($e->small2 == $news->id)
  656. $e->small2 = '';
  657. if ($e->small3 == $news->id)
  658. $e->small3 = '';
  659. if ($e->small4 == $news->id)
  660. $e->small4 = '';
  661. $e->save();
  662. }
  663. }
  664. Log::info('=== DETERMINING REDIRECT FOR UPDATE ===');
  665. if (isset($input['publish'])) {
  666. Log::info('Redirecting after publish to news.index');
  667. return redirect()->route('news.index')->with('success', 'News published successfully');
  668. }
  669. if (isset($input['crop'])) {
  670. Log::info('Redirecting to crop page');
  671. return redirect('/admin/news/crop?news_id=' . $news->id);
  672. }
  673. if (isset($input['unpublish'])) {
  674. Log::info('Redirecting after unpublish to news.index');
  675. return redirect()->route('news.index')->with('success', 'News unpublished successfully');
  676. }
  677. if (isset($input['save'])) {
  678. Log::info('Redirecting after save to news.edit');
  679. return redirect()->route('news.edit', $news->id)->with('success', 'News updated successfully');
  680. }
  681. if (isset($input['save_exit'])) {
  682. Log::info('Redirecting after save_exit to news.index');
  683. return redirect()->route('news.index')->with('success', 'News updated successfully');
  684. }
  685. Log::info('No specific action, redirecting to news.edit');
  686. return redirect()->route('news.edit', $news->id)->with('success', 'News updated successfully');
  687. }
  688. /**
  689. * Remove the specified resource from storage.
  690. *
  691. * @param \App\News $news
  692. * @return \Illuminate\Http\Response
  693. */
  694. public function destroy(News $news)
  695. {
  696. $section = null;
  697. if (isset($news->section)) {
  698. $section = $news->section;
  699. }
  700. $region_1 = null;
  701. if (isset($news->region_1)) {
  702. $region_1 = $news->region_1;
  703. }
  704. $region_2 = null;
  705. if (isset($news->region_2)) {
  706. $region_2 = $news->region_2;
  707. }
  708. $news->delete();
  709. return redirect()->route('news.index')
  710. ->with('success', 'News deleted successfully');
  711. }
  712. }