NewsController.php 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  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 File;
  10. use Auth;
  11. use App\Notifications\FacebookPost;
  12. use App\Notifications\TwitterPost;
  13. class NewsController extends Controller
  14. {
  15. protected $rules = [
  16. //'section_id' => ['required']
  17. 'title' => ['required'],
  18. //'page_id' => ['required'],
  19. ];
  20. public function index(Request $request)
  21. {
  22. // Se stavo modificando una news metto a null il campo
  23. if ($request->session()->has('current_news')) {
  24. $news_id = session('current_news', 0);
  25. if ($news_id > 0)
  26. {
  27. $n = News::where('id', '=', $news_id)->take(1000)->get();
  28. if ($n->count() > 0)
  29. {
  30. $n = $n->first();
  31. if ($n->user_id == Auth::user()->id)
  32. {
  33. $n->user_id = null;
  34. $n->save();
  35. }
  36. }
  37. $request->session()->forget('current_news');
  38. }
  39. }
  40. $news = News::orderBy('date', 'DESC')->take(1000)->get();//->paginate(50);
  41. return view('news.index',compact('news'))
  42. ->with('i', (request()->input('page', 1) - 1) * 5);
  43. }
  44. public function load_json()
  45. {
  46. $start = $_GET["start"];
  47. $end = 50;//$_GET["length"];
  48. $total = News::count();
  49. $filtered = $total;
  50. $sort_by = '';
  51. $sort = $_GET["order"][0]["column"];
  52. switch ($sort) {
  53. case '0':
  54. $sort_by = 'date';
  55. break;
  56. case '2':
  57. $sort_by = 'title';
  58. break;
  59. case '3':
  60. $sort_by = 'name';
  61. break;
  62. case '4':
  63. $sort_by = 'name';
  64. break;
  65. case '5':
  66. $sort_by = 'name';
  67. break;
  68. case '6':
  69. $sort_by = 'title';
  70. break;
  71. case '7':
  72. $sort_by = 'online';
  73. break;
  74. case '8':
  75. $sort_by = 'clicks';
  76. break;
  77. default:
  78. $sort_by = 'date';
  79. break;
  80. }
  81. $sort_by_dir = $_GET["order"][0]["dir"];
  82. $search = $_GET["search"]["value"];
  83. if ($sort == '3')
  84. {
  85. $news = News::with(['section' => function ($query) use ($sort_by, $sort_by_dir) {
  86. $query->orderBy($sort_by, $sort_by_dir);
  87. }]);
  88. }
  89. else if ($sort == '4')
  90. {
  91. $news = News::with(['region_1' => function ($query) use ($sort_by, $sort_by_dir) {
  92. $query->orderBy($sort_by, $sort_by_dir);
  93. }]);
  94. }
  95. else if ($sort == '5')
  96. {
  97. $news = News::with(['region_2' => function ($query) use ($sort_by, $sort_by_dir) {
  98. $query->orderBy($sort_by, $sort_by_dir);
  99. }]);
  100. }
  101. else if ($sort == '6')
  102. {
  103. $news = News::with(['event' => function ($query) use ($sort_by, $sort_by_dir) {
  104. $query->orderBy($sort_by, $sort_by_dir);
  105. }]);
  106. }
  107. else
  108. $news = News::orderBy($sort_by, $sort_by_dir);
  109. if ($search != '')
  110. {
  111. $news = $news->where('title', 'LIKE', '%' . $search . '%');
  112. $filtered = $news->count();
  113. }
  114. $news = $news->limit($end)->offset($start)->get();//->paginate(50);
  115. $aData = array();
  116. foreach($news as $n)
  117. {
  118. $x = array();
  119. $x[] = $n->date;
  120. if ($n->image != '')
  121. $x[] = '<img src="/files/news/' . $n->image . '" style="max-width:100px" />';
  122. else
  123. $x[] = '';
  124. $x[] = $n->title;
  125. $section = @$n->section->name;
  126. if($n->section)
  127. $section .= '<br>' . isset($n->section) ? $n->section->position($n->id) : '';
  128. $x[] = $section;
  129. $region_1 = @$n->region_1->name;
  130. if($n->region_1)
  131. $region_1 .= '<br>' . isset($n->region_1) ? $n->region_1->position($n->id) : '';
  132. $x[] = $region_1;
  133. $region_2 = @$n->region_2->name;
  134. if($n->region_2)
  135. $region_2 .= '<br>' . isset($n->region_2) ? $n->region_2->position($n->id) : '';
  136. $x[] = $region_2;
  137. //$x[] = @$n->event->title;
  138. $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>';
  139. $x[] = @$n->clicks;
  140. $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>';
  141. $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>';
  142. $form = '<form method="POST" action="/admin/news/' . $n->id . '" accept-charset="UTF-8" class="form-inline">
  143. <input name="_method" type="hidden" value="DELETE">
  144. <input name="_token" type="hidden" value="' . csrf_token() . '">
  145. <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>
  146. </form>';
  147. $x[] = $form;
  148. $aData[] = $x;
  149. }
  150. $aRet = array('recordsTotal' => $total, 'recordsFiltered' => $filtered, 'data' => $aData);
  151. return json_encode($aRet);
  152. }
  153. /**
  154. * Show the form for creating a new resource.
  155. *
  156. * @return \Illuminate\Http\Response
  157. */
  158. public function create()
  159. {
  160. $sections = Section::where('type', '=', 'section')->orderBy('position')->pluck('name', 'id')->toArray();;
  161. $regions = Section::where('type', '=', 'region')->orderBy('name')->pluck('name', 'id')->toArray();;
  162. $layouts = Section::pluck('layout', 'id')->toArray();
  163. $events = Event::orderBy('title')->pluck('title', 'id')->toArray();;
  164. $pages = Page::where('online', '=', true)->orderBy('title')->pluck('title', 'id')->toArray();;
  165. $section_position = '';
  166. $region_1_position = '';
  167. $region_2_position = '';
  168. $first = '';
  169. $aFirst = array();
  170. $home = Home::first();
  171. if ($home != null)
  172. {
  173. $pos = '';
  174. $home->loadData();
  175. if (isset($home->slide1) && $home->slide1 != '')
  176. $aFirst[] = $home->slide1->title . " (immagine grande)";
  177. for($a=1;$a<=5;$a++)
  178. {
  179. if (isset($home["left" . $a]) && $home["left" . $a] != '')
  180. $aFirst[] = $home["left" . $a]["title"] . " (slide sinistra)";
  181. }
  182. for($a=1;$a<=5;$a++)
  183. {
  184. if (isset($home["right" . $a]) && $home["right" . $a] != '')
  185. $aFirst[] = $home["right" . $a]["title"] . " (slide destra)";
  186. }
  187. $first = '<li>' . implode("</li><li>", $aFirst) . '</li>';
  188. }
  189. return view('news.create', compact('sections', 'regions', 'events', 'pages', 'layouts', 'section_position', 'region_1_position', 'region_2_position', 'first'));
  190. }
  191. /**
  192. * Store a newly created resource in storage.
  193. *
  194. * @param \Illuminate\Http\Request $request
  195. * @return \Illuminate\Http\Response
  196. */
  197. public function store(Request $request)
  198. {
  199. $request->validate($this->rules);
  200. $input = $request->all();
  201. if(request()->image)
  202. {
  203. $file = request()->image;
  204. $filename = time() . '_' . $file->getClientOriginalName();
  205. if (! File::exists(public_path()."/files/news"))
  206. File::makeDirectory(public_path()."/files/news");
  207. $path = public_path('files/news');
  208. request()->image->move($path, $filename);
  209. $input["image"] = $filename;
  210. }
  211. else
  212. {
  213. if (isset($_POST["filename"]))
  214. $input["image"] = $_POST["filename"];
  215. }
  216. for($i=1;$i<=5;$i++)
  217. {
  218. if(isset($input["image" . $i]))
  219. {
  220. $file = $input["image" . $i];
  221. $filename = time() . '_' . $file->getClientOriginalName();
  222. if (! File::exists(public_path()."/files/news"))
  223. File::makeDirectory(public_path()."/files/news");
  224. $path = public_path('files/news');
  225. $input["image" . $i]->move($path, $filename);
  226. $input["image" . $i] = $filename;
  227. }
  228. }
  229. if(request()->pdf)
  230. {
  231. $file = request()->pdf;
  232. $filename = time() . '_' . $file->getClientOriginalName();
  233. if (! File::exists(public_path()."/files/news"))
  234. File::makeDirectory(public_path()."/files/news");
  235. $path = public_path('files/news');
  236. request()->pdf->move($path, $filename);
  237. $input["pdf"] = $filename;
  238. }
  239. $final_date = null;
  240. if ($input["date"] != '')
  241. {
  242. list($dt, $time) = explode(" ", $input["date"]);
  243. list($day, $month, $year) = explode("/", $dt);
  244. $final_date = $year . "-" . $month . "-" . $day . " " . $time;
  245. }
  246. $input["date"] = $final_date;
  247. // $input["online"] = isset($input["online"]) ? ($input["online"] == 'on' ? true : false) : false;
  248. $input["online"] = false;
  249. $input["homepage"] = isset($input["homepage"]) ? ($input["homepage"] == 'on' ? true : false) : false;
  250. $input["live"] = isset($input["live"]) ? ($input["live"] == 'on' ? true : false) : false;
  251. $input["breaking_news"] = isset($input["breaking_news"]) ? ($input["breaking_news"] == 'on' ? true : false) : false;
  252. $news = News::create($input);
  253. if (isset($input['publish']))
  254. {
  255. $news->online = true;
  256. $news->save();
  257. // Posiziono la notizia se la data non è
  258. if ($final_date <= date("Y-m-d H:i:s"))
  259. {
  260. if ($_POST["section_position"] != '')
  261. {
  262. $s = Section::findOrFail($input["section_id"]);
  263. $s[$_POST["section_position"]] = $news->id;
  264. $s->save();
  265. $news->section_position = '';
  266. }
  267. if ($_POST["region_1_position"] != '')
  268. {
  269. $s = Section::findOrFail($input["region_1_id"]);
  270. $s[$_POST["region_1_position"]] = $news->id;
  271. $s->save();
  272. $news->region_1_position = '';
  273. }
  274. if ($_POST["region_2_position"] != '')
  275. {
  276. $s = Section::findOrFail($input["region_2_id"]);
  277. $s[$_POST["region_2_position"]] = $news->id;
  278. $s->save();
  279. $news->region_2_position = '';
  280. }
  281. }
  282. if (isset($input['no_social']))
  283. {
  284. }
  285. else
  286. {
  287. @$news->notify(new FacebookPost());
  288. $news->notify(new TwitterPost());
  289. $news->published = true;
  290. $news->save();
  291. }
  292. }
  293. if (isset($input['unpublish']))
  294. {
  295. $news->online = false;
  296. $news->save();
  297. }
  298. if ($_POST["homepage_position"] != '' && $news->online)
  299. {
  300. $home = Home::first();
  301. if ($home != null)
  302. {
  303. $home->fill([
  304. $_POST["homepage_position"] => $news->id
  305. ]);
  306. $home->save();
  307. }
  308. }
  309. /*
  310. // Posizione
  311. if ($_POST["section_position"] != '')
  312. {
  313. $field = $input["section_position"];
  314. $s = Section::findOrFail($input["section_id"]);
  315. $s->big = ($field != 'big' && $s->big == $news->id) ? '' : $s->big;
  316. $s->small1 = ($field != 'small1' && $s->small1 == $news->id) ? '' : $s->small1;
  317. $s->small2 = ($field != 'small2' && $s->small2 == $news->id) ? '' : $s->small2;
  318. $s->small3 = ($field != 'small3' && $s->small3 == $news->id) ? '' : $s->small3;
  319. $s->small4 = ($field != 'small4' && $s->small4 == $news->id) ? '' : $s->small4;
  320. $s->small5 = ($field != 'small5' && $s->small5 == $news->id) ? '' : $s->small5;
  321. $s->small6 = ($field != 'small6' && $s->small6 == $news->id) ? '' : $s->small6;
  322. $s->$field = $news->id;
  323. $s->save();
  324. // CLEARLAYOUT
  325. $s->clearLayout();
  326. }
  327. if ($_POST["region_1_position"] != '')
  328. {
  329. $field = $input["region_1_position"];
  330. $r = Section::findOrFail($input["region_1_id"]);
  331. $r->big = ($field != 'big' && $r->big == $news->id) ? '' : $r->big;
  332. $r->small1 = ($field != 'small1' && $r->small1 == $news->id) ? '' : $r->small1;
  333. $r->small2 = ($field != 'small2' && $r->small2 == $news->id) ? '' : $r->small2;
  334. $r->small3 = ($field != 'small3' && $r->small3 == $news->id) ? '' : $r->small3;
  335. $r->small4 = ($field != 'small4' && $r->small4 == $news->id) ? '' : $r->small4;
  336. $r->small5 = ($field != 'small5' && $r->small5 == $news->id) ? '' : $r->small5;
  337. $r->small6 = ($field != 'small6' && $r->small6 == $news->id) ? '' : $r->small6;
  338. $r->$field = $news->id;
  339. $r->save();
  340. // CLEARLAYOUT
  341. $r->clearLayout();
  342. }
  343. if ($_POST["region_2_position"] != '')
  344. {
  345. $field = $input["region_2_position"];
  346. $r = Section::findOrFail($input["region_2_id"]);
  347. $r->big = ($field != 'big' && $r->big == $news->id) ? '' : $r->big;
  348. $r->small1 = ($field != 'small1' && $r->small1 == $news->id) ? '' : $r->small1;
  349. $r->small2 = ($field != 'small2' && $r->small2 == $news->id) ? '' : $r->small2;
  350. $r->small3 = ($field != 'small3' && $r->small3 == $news->id) ? '' : $r->small3;
  351. $r->small4 = ($field != 'small4' && $r->small4 == $news->id) ? '' : $r->small4;
  352. $r->small5 = ($field != 'small5' && $r->small5 == $news->id) ? '' : $r->small5;
  353. $r->small6 = ($field != 'small6' && $r->small6 == $news->id) ? '' : $r->small6;
  354. $r->$field = $news->id;
  355. $r->save();
  356. // CLEARLAYOUT
  357. $r->clearLayout();
  358. }
  359. */
  360. if (isset($input['publish']))
  361. return redirect()->route('news.index')->with('success','News updated successfully');
  362. if (isset($input['unpublish']))
  363. return redirect()->route('news.index')->with('success','News updated successfully');
  364. if (isset($input['save']))
  365. {
  366. return redirect()->route('news.edit', $news->id)->with('success','News updated successfully');
  367. }
  368. if (isset($input['save_exit']))
  369. {
  370. return redirect()->route('news.index')->with('success','News updated successfully');
  371. }
  372. }
  373. /**
  374. * Display the specified resource.
  375. *
  376. * @param \App\News $news
  377. * @return \Illuminate\Http\Response
  378. */
  379. public function show(News $news)
  380. {
  381. return view('news.show',compact('news'));
  382. }
  383. /**
  384. * Show the form for editing the specified resource.
  385. *
  386. * @param \App\News $news
  387. * @return \Illuminate\Http\Response
  388. */
  389. public function edit(News $news)
  390. {
  391. // Aggiorno l'utente che sta guardando la news
  392. if ($news->user_id == null)
  393. {
  394. session(['current_news' => $news->id]);
  395. $news->user_id = Auth::user()->id;
  396. $news->save();
  397. }
  398. $sections = Section::where('type', '=', 'section')->orderBy('position')->pluck('name', 'id')->toArray();;
  399. $regions = Section::where('type', '=', 'region')->orderBy('name')->pluck('name', 'id')->toArray();;
  400. $layouts = Section::pluck('layout', 'id')->toArray();
  401. $events = Event::orderBy('title')->pluck('title', 'id')->toArray();;
  402. $pages = Page::where('online', '=', true)->orderBy('title')->pluck('title', 'id')->toArray();;
  403. $final_date = null;
  404. if ($news->date != null)
  405. {
  406. list($dt, $time) = explode(" ", $news->date);
  407. list($year, $month, $day) = explode("-", $dt);
  408. $final_date = $day . "/" . $month . "/" . $year . " " . $time;
  409. }
  410. /*
  411. $section_position = '';
  412. if ($news->section_id > 0)
  413. {
  414. if ($news->section->big == $news->id)
  415. $section_position = 'big';
  416. if ($news->section->small1 == $news->id)
  417. $section_position = 'small1';
  418. if ($news->section->small2 == $news->id)
  419. $section_position = 'small2';
  420. if ($news->section->small3 == $news->id)
  421. $section_position = 'small3';
  422. if ($news->section->small4 == $news->id)
  423. $section_position = 'small4';
  424. if ($news->section->small5 == $news->id)
  425. $section_position = 'small5';
  426. if ($news->section->small6 == $news->id)
  427. $section_position = 'small6';
  428. if ($section_position == null)
  429. $section_position = $news->section_position;
  430. }
  431. $region_1_position = '';
  432. if ($news->region_1_id > 0)
  433. {
  434. if ($news->region_1->big == $news->id)
  435. $region_1_position = 'big';
  436. if ($news->region_1->small1 == $news->id)
  437. $region_1_position = 'small1';
  438. if ($news->region_1->small2 == $news->id)
  439. $region_1_position = 'small2';
  440. if ($news->region_1->small3 == $news->id)
  441. $region_1_position = 'small3';
  442. if ($news->region_1->small4 == $news->id)
  443. $region_1_position = 'small4';
  444. if ($news->region_1->small5 == $news->id)
  445. $region_1_position = 'small5';
  446. if ($news->region_1->small6 == $news->id)
  447. $region_1_position = 'small6';
  448. if ($region_1_position == '')
  449. $region_1_position = $news->region_1_position;
  450. }
  451. $region_2_position = '';
  452. if ($news->region_2_id > 0)
  453. {
  454. if ($news->region_2->big == $news->id)
  455. $region_2_position = 'big';
  456. if ($news->region_2->small1 == $news->id)
  457. $region_2_position = 'small1';
  458. if ($news->region_2->small2 == $news->id)
  459. $region_2_position = 'small2';
  460. if ($news->region_2->small3 == $news->id)
  461. $region_2_position = 'small3';
  462. if ($news->region_2->small4 == $news->id)
  463. $region_2_position = 'small4';
  464. if ($news->region_2->small5 == $news->id)
  465. $region_2_position = 'small5';
  466. if ($news->region_2->small6 == $news->id)
  467. $region_2_position = 'small6';
  468. if ($region_2_position == '')
  469. $region_2_position = $news->region_2_position;
  470. }
  471. */
  472. $section_position = $news->section_position;
  473. $region_1_position = $news->region_1_position;
  474. $region_2_position = $news->region_2_position;
  475. $first = '';
  476. $aFirst = array();
  477. $home = Home::first();
  478. if ($home != null)
  479. {
  480. $pos = '';
  481. if ($home->slide1 == $news->id)
  482. $news->homepage_position = "slide1";
  483. if ($home->left1 == $news->id)
  484. $news->homepage_position = "left1";
  485. if ($home->left2 == $news->id)
  486. $news->homepage_position = "left2";
  487. if ($home->left3 == $news->id)
  488. $news->homepage_position = "left3";
  489. if ($home->right1 == $news->id)
  490. $news->homepage_position = "right1";
  491. if ($home->right2 == $news->id)
  492. $news->homepage_position = "right2";
  493. if ($home->right3 == $news->id)
  494. $news->homepage_position = "right3";
  495. $home->loadData();
  496. if (isset($home->slide1) && $home->slide1 != '')
  497. $aFirst[] = $home->slide1->title . " (immagine grande)";
  498. for($a=1;$a<=5;$a++)
  499. {
  500. if (isset($home["left" . $a]) && $home["left" . $a] != '')
  501. $aFirst[] = $home["left" . $a]["title"] . " (slide sinistra)";
  502. }
  503. for($a=1;$a<=5;$a++)
  504. {
  505. if (isset($home["right" . $a]) && $home["right" . $a] != '')
  506. $aFirst[] = $home["right" . $a]["title"] . " (slide destra)";
  507. }
  508. $first = '<li>' . implode("</li><li>", $aFirst) . '</li>';
  509. }
  510. $news->date = $final_date;
  511. return view('news.edit',compact('news', 'sections', 'regions', 'pages', 'events', 'layouts', 'section_position', 'region_1_position', 'region_2_position', 'first'));
  512. }
  513. /**
  514. * Update the specified resource in storage.
  515. *
  516. * @param \Illuminate\Http\Request $request
  517. * @param \App\News $news
  518. * @return \Illuminate\Http\Response
  519. */
  520. public function update(Request $request, News $news)
  521. {
  522. $request->validate($this->rules);
  523. $input = $request->all();
  524. $old_section_id = $news->section_id;
  525. $old_region_1_id = $news->region_1_id;
  526. $old_region_2_id = $news->region_2_id;
  527. $old_event_id = $news->event_id;
  528. $section_position = '';
  529. if ($news->section_id > 0)
  530. {
  531. if ($news->section->big == $news->id)
  532. $section_position = 'big';
  533. /*if ($news->section->medium1 == $news->id)
  534. $section_position = 'medium1';
  535. if ($news->section->medium2 == $news->id)
  536. $section_position = 'medium2';
  537. if ($news->section->medium3 == $news->id)
  538. $section_position = 'medium3';*/
  539. if ($news->section->small1 == $news->id)
  540. $section_position = 'small1';
  541. if ($news->section->small2 == $news->id)
  542. $section_position = 'small2';
  543. if ($news->section->small3 == $news->id)
  544. $section_position = 'small3';
  545. if ($news->section->small4 == $news->id)
  546. $section_position = 'small4';
  547. if ($news->section->small5 == $news->id)
  548. $section_position = 'small5';
  549. if ($news->section->small6 == $news->id)
  550. $section_position = 'small6';
  551. if ($section_position == null)
  552. $section_position = $news->section_position;
  553. }
  554. $region_1_position = '';
  555. if ($news->region_1_id > 0)
  556. {
  557. if ($news->region_1->big == $news->id)
  558. $region_1_position = 'big';
  559. /*if ($news->region_1->medium1 == $news->id)
  560. $region_1_position = 'medium1';
  561. if ($news->region_1->medium2 == $news->id)
  562. $region_1_position = 'medium2';
  563. if ($news->region_1->medium3 == $news->id)
  564. $region_1_position = 'medium3';*/
  565. if ($news->region_1->small1 == $news->id)
  566. $region_1_position = 'small1';
  567. if ($news->region_1->small2 == $news->id)
  568. $region_1_position = 'small2';
  569. if ($news->region_1->small3 == $news->id)
  570. $region_1_position = 'small3';
  571. if ($news->region_1->small4 == $news->id)
  572. $region_1_position = 'small4';
  573. if ($news->region_1->small5 == $news->id)
  574. $region_1_position = 'small5';
  575. if ($news->region_1->small6 == $news->id)
  576. $region_1_position = 'small6';
  577. if ($region_1_position == '')
  578. $region_1_position = $news->region_1_position;
  579. }
  580. $region_2_position = '';
  581. if ($news->region_2_id > 0)
  582. {
  583. if ($news->region_2->big == $news->id)
  584. $region_2_position = 'big';
  585. /*if ($news->region_2->medium1 == $news->id)
  586. $region_2_position = 'medium1';
  587. if ($news->region_2->medium2 == $news->id)
  588. $region_2_position = 'medium2';
  589. if ($news->region_2->medium3 == $news->id)
  590. $region_2_position = 'medium3';*/
  591. if ($news->region_2->small1 == $news->id)
  592. $region_2_position = 'small1';
  593. if ($news->region_2->small2 == $news->id)
  594. $region_2_position = 'small2';
  595. if ($news->region_2->small3 == $news->id)
  596. $region_2_position = 'small3';
  597. if ($news->region_2->small4 == $news->id)
  598. $region_2_position = 'small4';
  599. if ($news->region_2->small5 == $news->id)
  600. $region_2_position = 'small5';
  601. if ($news->region_2->small6 == $news->id)
  602. $region_2_position = 'small6';
  603. if ($region_2_position == '')
  604. $region_2_position = $news->region_2_position;
  605. }
  606. if ($old_section_id == $input["section_id"] && $section_position == $input["section_position"])
  607. unset($input["section_position"]);
  608. if ($old_region_1_id == $input["region_1_id"] && $region_1_position == $input["region_1_position"])
  609. unset($input["region_1_position"]);
  610. if ($old_region_2_id == $input["region_2_id"] && $region_2_position == $input["region_2_position"])
  611. unset($input["region_2_position"]);
  612. if(request()->image)
  613. {
  614. $file = request()->image;
  615. $filename = time() . '_' . $file->getClientOriginalName();
  616. if (! File::exists(public_path()."/files/news"))
  617. File::makeDirectory(public_path()."/files/news");
  618. $path = public_path('files/news');
  619. request()->image->move($path, $filename);
  620. $input["image"] = $filename;
  621. }
  622. for($i=1;$i<=5;$i++)
  623. {
  624. if(isset($input["image" . $i]))
  625. {
  626. $file = $input["image" . $i];
  627. $filename = time() . '_' . $file->getClientOriginalName();
  628. if (! File::exists(public_path()."/files/news"))
  629. File::makeDirectory(public_path()."/files/news");
  630. $path = public_path('files/news');
  631. $input["image" . $i]->move($path, $filename);
  632. $input["image" . $i] = $filename;
  633. }
  634. }
  635. if (isset($input["remove_pdf"]))
  636. {
  637. $input["pdf"] = '';
  638. }
  639. if(request()->pdf)
  640. {
  641. $file = request()->pdf;
  642. $filename = time() . '_' . $file->getClientOriginalName();
  643. if (! File::exists(public_path()."/files/news"))
  644. File::makeDirectory(public_path()."/files/news");
  645. $path = public_path('files/news');
  646. request()->pdf->move($path, $filename);
  647. $input["pdf"] = $filename;
  648. }
  649. $final_date = null;
  650. if ($input["date"] != '')
  651. {
  652. list($dt, $time) = explode(" ", $input["date"]);
  653. list($day, $month, $year) = explode("/", $dt);
  654. $final_date = $year . "-" . $month . "-" . $day . " " . $time;
  655. }
  656. $input["date"] = $final_date;
  657. // $input["online"] = isset($input["online"]) ? ($input["online"] == 'on' ? true : false) : false;
  658. $input["homepage"] = isset($input["homepage"]) ? ($input["homepage"] == 'on' ? true : false) : false;
  659. $input["live"] = isset($input["live"]) ? ($input["live"] == 'on' ? true : false) : false;
  660. $input["breaking_news"] = isset($input["breaking_news"]) ? ($input["breaking_news"] == 'on' ? true : false) : false;
  661. $news->update($input);
  662. $news->user_id = null;
  663. $news->save();
  664. $request->session()->forget('current_news');
  665. if (isset($input['publish']))
  666. {
  667. $news->online = true;
  668. $news->save();
  669. if (isset($input['no_social']))
  670. {
  671. // Non pubblico sui social
  672. }
  673. else
  674. {
  675. @$news->notify(new FacebookPost());
  676. $news->notify(new TwitterPost());
  677. $news->published = true;
  678. $news->save();
  679. }
  680. }
  681. if (isset($input['unpublish']))
  682. {
  683. $news->online = false;
  684. $news->save();
  685. }
  686. if ($_POST["homepage_position"] != '' && $news->online)
  687. {
  688. $home = Home::first();
  689. if ($home != null)
  690. {
  691. $home->fill([
  692. $_POST["homepage_position"] => $news->id
  693. ]);
  694. $home->save();
  695. }
  696. }
  697. if ($news->event_id != $old_event_id)
  698. {
  699. $e = Event::where('id', '=', $old_event_id)->first();
  700. if ($e)
  701. {
  702. if ($e->big == $news->id)
  703. $e->big = '';
  704. if ($e->small1 == $news->id)
  705. $e->small1 = '';
  706. if ($e->small2 == $news->id)
  707. $e->small2 = '';
  708. if ($e->small3 == $news->id)
  709. $e->small3 = '';
  710. if ($e->small4 == $news->id)
  711. $e->small4 = '';
  712. $e->save();
  713. }
  714. }
  715. /*
  716. // Posizione
  717. if ($_POST["section_position"] != '')
  718. {
  719. if ($input["section_id"] == '')
  720. {
  721. if ($old_section_id > 0)
  722. {
  723. $s_old = Section::findOrFail($old_section_id);
  724. $s_old->big = ($s_old->big == $news->id) ? '' : $s_old->big;
  725. $s_old->small1 = ($s_old->small1 == $news->id) ? '' : $s_old->small1;
  726. $s_old->small2 = ($s_old->small2 == $news->id) ? '' : $s_old->small2;
  727. $s_old->small3 = ($s_old->small3 == $news->id) ? '' : $s_old->small3;
  728. $s_old->small4 = ($s_old->small4 == $news->id) ? '' : $s_old->small4;
  729. $s_old->small5 = ($s_old->small5 == $news->id) ? '' : $s_old->small5;
  730. $s_old->small6 = ($s_old->small6 == $news->id) ? '' : $s_old->small6;
  731. $s_old->save();
  732. // CLEARLAYOUT
  733. $s_old->clearLayout();
  734. }
  735. }
  736. else
  737. {
  738. // Se online e data > adesso
  739. if ($news->online && $news->data < date("Y-m-d H:i:s"))
  740. {
  741. $field = $input["section_position"];
  742. $s = Section::findOrFail($input["section_id"]);
  743. // Se ho cambiato sezione, tolgo dalla vecchia me metto nella nuova
  744. if ($input["section_id"] != $old_section_id)
  745. {
  746. if ($old_section_id > 0)
  747. {
  748. $s_old = Section::findOrFail($old_section_id);
  749. $s_old->big = ($s_old->big == $news->id) ? '' : $s_old->big;
  750. $s_old->small1 = ($s_old->small1 == $news->id) ? '' : $s_old->small1;
  751. $s_old->small2 = ($s_old->small2 == $news->id) ? '' : $s_old->small2;
  752. $s_old->small3 = ($s_old->small3 == $news->id) ? '' : $s_old->small3;
  753. $s_old->small4 = ($s_old->small4 == $news->id) ? '' : $s_old->small4;
  754. $s_old->small5 = ($s_old->small5 == $news->id) ? '' : $s_old->small5;
  755. $s_old->small6 = ($s_old->small6 == $news->id) ? '' : $s_old->small6;
  756. $s_old->save();
  757. // CLEARLAYOUT
  758. $s_old->clearLayout();
  759. }
  760. }
  761. else
  762. {
  763. $s->big = ($field != 'big' && $s->big == $news->id) ? '' : $s->big;
  764. $s->small1 = ($field != 'small1' && $s->small1 == $news->id) ? '' : $s->small1;
  765. $s->small2 = ($field != 'small2' && $s->small2 == $news->id) ? '' : $s->small2;
  766. $s->small3 = ($field != 'small3' && $s->small3 == $news->id) ? '' : $s->small3;
  767. $s->small4 = ($field != 'small4' && $s->small4 == $news->id) ? '' : $s->small4;
  768. $s->small5 = ($field != 'small5' && $s->small5 == $news->id) ? '' : $s->small5;
  769. $s->small6 = ($field != 'small6' && $s->small6 == $news->id) ? '' : $s->small6;
  770. }
  771. $s->$field = $news->id;
  772. $s->save();
  773. // CLEARLAYOUT
  774. $s->clearLayout();
  775. }
  776. }
  777. }
  778. else
  779. {
  780. if ($input["section_id"] == '')
  781. {
  782. if ($old_section_id > 0)
  783. {
  784. $s_old = Section::findOrFail($old_section_id);
  785. $s_old->big = ($s_old->big == $news->id) ? '' : $s_old->big;
  786. $s_old->small1 = ($s_old->small1 == $news->id) ? '' : $s_old->small1;
  787. $s_old->small2 = ($s_old->small2 == $news->id) ? '' : $s_old->small2;
  788. $s_old->small3 = ($s_old->small3 == $news->id) ? '' : $s_old->small3;
  789. $s_old->small4 = ($s_old->small4 == $news->id) ? '' : $s_old->small4;
  790. $s_old->small5 = ($s_old->small5 == $news->id) ? '' : $s_old->small5;
  791. $s_old->small6 = ($s_old->small6 == $news->id) ? '' : $s_old->small6;
  792. $s_old->save();
  793. // CLEARLAYOUT
  794. $s_old->clearLayout();
  795. }
  796. }
  797. else
  798. {
  799. if ($input["section_id"] != $old_section_id)
  800. {
  801. $s_old = Section::findOrFail($old_section_id);
  802. $s_old->big = ($s_old->big == $news->id) ? '' : $s_old->big;
  803. $s_old->small1 = ($s_old->small1 == $news->id) ? '' : $s_old->small1;
  804. $s_old->small2 = ($s_old->small2 == $news->id) ? '' : $s_old->small2;
  805. $s_old->small3 = ($s_old->small3 == $news->id) ? '' : $s_old->small3;
  806. $s_old->small4 = ($s_old->small4 == $news->id) ? '' : $s_old->small4;
  807. $s_old->small5 = ($s_old->small5 == $news->id) ? '' : $s_old->small5;
  808. $s_old->small6 = ($s_old->small6 == $news->id) ? '' : $s_old->small6;
  809. $s_old->save();
  810. // CLEARLAYOUT
  811. $s_old->clearLayout();
  812. }
  813. else
  814. {
  815. $s_old = Section::findOrFail($input["section_id"]);
  816. $s_old->big = ($s_old->big == $news->id) ? '' : $s_old->big;
  817. $s_old->small1 = ($s_old->small1 == $news->id) ? '' : $s_old->small1;
  818. $s_old->small2 = ($s_old->small2 == $news->id) ? '' : $s_old->small2;
  819. $s_old->small3 = ($s_old->small3 == $news->id) ? '' : $s_old->small3;
  820. $s_old->small4 = ($s_old->small4 == $news->id) ? '' : $s_old->small4;
  821. $s_old->small5 = ($s_old->small5 == $news->id) ? '' : $s_old->small5;
  822. $s_old->small6 = ($s_old->small6 == $news->id) ? '' : $s_old->small6;
  823. print $s_old->small3;
  824. $s_old->save();
  825. // CLEARLAYOUT
  826. $s_old->clearLayout();
  827. }
  828. }
  829. }
  830. if ($_POST["region_1_position"] != '')
  831. {
  832. if ($input["region_1_id"] == '')
  833. {
  834. if ($old_region_1_id > 0)
  835. {
  836. $s_old = Section::findOrFail($old_region_1_id);
  837. $s_old->big = ($s_old->big == $news->id) ? '' : $s_old->big;
  838. $s_old->small1 = ($s_old->small1 == $news->id) ? '' : $s_old->small1;
  839. $s_old->small2 = ($s_old->small2 == $news->id) ? '' : $s_old->small2;
  840. $s_old->small3 = ($s_old->small3 == $news->id) ? '' : $s_old->small3;
  841. $s_old->small4 = ($s_old->small4 == $news->id) ? '' : $s_old->small4;
  842. $s_old->small5 = ($s_old->small5 == $news->id) ? '' : $s_old->small5;
  843. $s_old->small6 = ($s_old->small6 == $news->id) ? '' : $s_old->small6;
  844. $s_old->save();
  845. // CLEARLAYOUT
  846. $s_old->clearLayout();
  847. }
  848. }
  849. else
  850. {
  851. $field = $input["region_1_position"];
  852. $r = Section::findOrFail($input["region_1_id"]);
  853. // Se ho cambiato sezione, tolgo dalla vecchia me metto nella nuova
  854. if ($input["region_1_id"] != $old_region_1_id)
  855. {
  856. if ($old_region_1_id > 0)
  857. {
  858. $s_old = Section::findOrFail($old_region_1_id);
  859. $s_old->big = ($s_old->big == $news->id) ? '' : $s_old->big;
  860. $s_old->small1 = ($s_old->small1 == $news->id) ? '' : $s_old->small1;
  861. $s_old->small2 = ($s_old->small2 == $news->id) ? '' : $s_old->small2;
  862. $s_old->small3 = ($s_old->small3 == $news->id) ? '' : $s_old->small3;
  863. $s_old->small4 = ($s_old->small4 == $news->id) ? '' : $s_old->small4;
  864. $s_old->small5 = ($s_old->small5 == $news->id) ? '' : $s_old->small5;
  865. $s_old->small6 = ($s_old->small6 == $news->id) ? '' : $s_old->small6;
  866. $s_old->save();
  867. // CLEARLAYOUT
  868. $s_old->clearLayout();
  869. }
  870. }
  871. else
  872. {
  873. $r->big = ($field != 'big' && $r->big == $news->id) ? '' : $r->big;
  874. $r->small1 = ($field != 'small1' && $r->small1 == $news->id) ? '' : $r->small1;
  875. $r->small2 = ($field != 'small2' && $r->small2 == $news->id) ? '' : $r->small2;
  876. $r->small3 = ($field != 'small3' && $r->small3 == $news->id) ? '' : $r->small3;
  877. $r->small4 = ($field != 'small4' && $r->small4 == $news->id) ? '' : $r->small4;
  878. $r->small5 = ($field != 'small5' && $r->small5 == $news->id) ? '' : $r->small5;
  879. $r->small6 = ($field != 'small6' && $r->small6 == $news->id) ? '' : $r->small6;
  880. }
  881. $r->$field = $news->id;
  882. $r->save();
  883. // CLEARLAYOUT
  884. $r->clearLayout();
  885. }
  886. }
  887. if ($_POST["region_2_position"] != '')
  888. {
  889. if ($input["region_2_id"] == '')
  890. {
  891. if ($old_region_2_id > 0)
  892. {
  893. $s_old = Section::findOrFail($old_region_2_id);
  894. $s_old->big = ($s_old->big == $news->id) ? '' : $s_old->big;
  895. $s_old->small1 = ($s_old->small1 == $news->id) ? '' : $s_old->small1;
  896. $s_old->small2 = ($s_old->small2 == $news->id) ? '' : $s_old->small2;
  897. $s_old->small3 = ($s_old->small3 == $news->id) ? '' : $s_old->small3;
  898. $s_old->small4 = ($s_old->small4 == $news->id) ? '' : $s_old->small4;
  899. $s_old->small5 = ($s_old->small5 == $news->id) ? '' : $s_old->small5;
  900. $s_old->small6 = ($s_old->small6 == $news->id) ? '' : $s_old->small6;
  901. $s_old->save();
  902. // CLEARLAYOUT
  903. $s_old->clearLayout();
  904. }
  905. }
  906. else
  907. {
  908. $field = $input["region_2_position"];
  909. $r = Section::findOrFail($input["region_2_id"]);
  910. // Se ho cambiato sezione, tolgo dalla vecchia me metto nella nuova
  911. if ($input["region_2_id"] != $old_region_2_id)
  912. {
  913. if ($old_region_2_id > 0)
  914. {
  915. $s_old = Section::findOrFail($old_region_2_id);
  916. $s_old->big = ($s_old->big == $news->id) ? '' : $s_old->big;
  917. $s_old->small1 = ($s_old->small1 == $news->id) ? '' : $s_old->small1;
  918. $s_old->small2 = ($s_old->small2 == $news->id) ? '' : $s_old->small2;
  919. $s_old->small3 = ($s_old->small3 == $news->id) ? '' : $s_old->small3;
  920. $s_old->small4 = ($s_old->small4 == $news->id) ? '' : $s_old->small4;
  921. $s_old->small5 = ($s_old->small5 == $news->id) ? '' : $s_old->small5;
  922. $s_old->small6 = ($s_old->small6 == $news->id) ? '' : $s_old->small6;
  923. $s_old->save();
  924. // CLEARLAYOUT
  925. $s_old->clearLayout();
  926. }
  927. }
  928. else
  929. {
  930. $r->big = ($field != 'big' && $r->big == $news->id) ? '' : $r->big;
  931. $r->small1 = ($field != 'small1' && $r->small1 == $news->id) ? '' : $r->small1;
  932. $r->small2 = ($field != 'small2' && $r->small2 == $news->id) ? '' : $r->small2;
  933. $r->small3 = ($field != 'small3' && $r->small3 == $news->id) ? '' : $r->small3;
  934. $r->small4 = ($field != 'small4' && $r->small4 == $news->id) ? '' : $r->small4;
  935. $r->small5 = ($field != 'small5' && $r->small5 == $news->id) ? '' : $r->small5;
  936. $r->small6 = ($field != 'small6' && $r->small6 == $news->id) ? '' : $r->small6;
  937. }
  938. $r->$field = $news->id;
  939. $r->save();
  940. // CLEARLAYOUT
  941. $r->clearLayout();
  942. }
  943. }
  944. */
  945. if (isset($input['publish']))
  946. {
  947. return redirect()->route('news.index')->with('success','News updated successfully');
  948. }
  949. if (isset($input['crop']))
  950. {
  951. return redirect('/admin/news/crop?news_id=' . $news->id);
  952. // return redirect()->route('news.crop')->with('success','News updated successfully');
  953. }
  954. if (isset($input['unpublish']))
  955. {
  956. return redirect()->route('news.index')->with('success','News updated successfully');
  957. }
  958. if (isset($input['save']))
  959. {
  960. return redirect()->route('news.edit', $news->id)->with('success','News updated successfully');
  961. }
  962. if (isset($input['save_exit']))
  963. {
  964. return redirect()->route('news.index')->with('success','News updated successfully');
  965. }
  966. }
  967. /**
  968. * Remove the specified resource from storage.
  969. *
  970. * @param \App\News $news
  971. * @return \Illuminate\Http\Response
  972. */
  973. public function destroy(News $news)
  974. {
  975. $section = null;
  976. if (isset($news->section))
  977. {
  978. $section = $news->section;
  979. }
  980. $region_1 = null;
  981. if (isset($news->region_1))
  982. {
  983. $region_1 = $news->region_1;
  984. }
  985. $region_2 = null;
  986. if (isset($news->region_2))
  987. {
  988. $region_2 = $news->region_2;
  989. }
  990. $news->delete();
  991. /*if ($section != null)
  992. {
  993. // CLEARLAYOUT
  994. $section->clearLayout();
  995. }
  996. if ($region_1 != null)
  997. {
  998. // CLEARLAYOUT
  999. $region_1->clearLayout();
  1000. }
  1001. if ($region_2 != null)
  1002. {
  1003. // CLEARLAYOUT
  1004. $region_2->clearLayout();
  1005. }*/
  1006. return redirect()->route('news.index')
  1007. ->with('success','News deleted successfully');
  1008. }
  1009. }