Section.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  1. <?php
  2. namespace App;
  3. use App\News;
  4. use Illuminate\Database\Eloquent\Model;
  5. use App\Notifications\FacebookPost;
  6. use App\Notifications\TwitterPost;
  7. class Section extends Model
  8. {
  9. protected $fillable = [
  10. 'name',
  11. 'type',
  12. 'position',
  13. 'layout',
  14. 'big',
  15. 'medium1',
  16. 'medium2',
  17. 'medium3',
  18. 'small1',
  19. 'small2',
  20. 'small3',
  21. 'small4',
  22. 'small5',
  23. 'small6',
  24. 'slug',
  25. 'before',
  26. 'after',
  27. 'show_social',
  28. 'show_social_twitter'
  29. ];
  30. function loadLayout()
  31. {
  32. $id = $this->id;
  33. $this->data = $this;
  34. if ($this->big != '')
  35. {
  36. $n = News::find($this->big);
  37. if ($n != null)
  38. {
  39. if (!$n->online)
  40. $n = null;
  41. $this->big = $n;
  42. }
  43. }
  44. if ($this->small1 != '')
  45. {
  46. $n = News::find($this->small1);
  47. if($n != null)
  48. {
  49. if (!$n->online)
  50. $n = null;
  51. $this->small1 = $n;
  52. }
  53. }
  54. if ($this->small2 != '')
  55. {
  56. $n = News::find($this->small2);
  57. if($n != null)
  58. {
  59. if (!$n->online)
  60. $n = null;
  61. $this->small2 = $n;
  62. }
  63. }
  64. if ($this->small3 != '')
  65. {
  66. $n = News::find($this->small3);
  67. if($n != null)
  68. {
  69. if (!$n->online)
  70. $n = null;
  71. $this->small3 = $n;
  72. }
  73. }
  74. if ($this->small4 != '')
  75. {
  76. $n = News::find($this->small4);
  77. if($n != null)
  78. {
  79. if (!$n->online)
  80. $n = null;
  81. $this->small4 = $n;
  82. }
  83. }
  84. if ($this->small5 != '')
  85. {
  86. $n = News::find($this->small5);
  87. if($n != null)
  88. {
  89. if (!$n->online)
  90. $n = null;
  91. $this->small5 = $n;
  92. }
  93. }
  94. if ($this->small6 != '')
  95. {
  96. $n = News::find($this->small6);
  97. if($n != null)
  98. {
  99. if (!$n->online)
  100. $n = null;
  101. $this->small6 = $n;
  102. }
  103. }
  104. }
  105. /*
  106. function generateLayout()
  107. {
  108. $id = $this->id;
  109. $aExist = array();
  110. if ($this->big != '')
  111. {
  112. $n = News::find($this->big);
  113. if ($n != null)
  114. {
  115. if ($n->section_id == $id && $n->section_position != '')
  116. {
  117. $n->section_position = '';
  118. $n->save();
  119. }
  120. if ($n->region_1_id == $id && $n->region_1_position != '')
  121. {
  122. $n->region_1_position = '';
  123. $n->save();
  124. }
  125. if ($n->region_2_id == $id && $n->region_2_position != '')
  126. {
  127. $n->region_2_position = '';
  128. $n->save();
  129. }
  130. }
  131. $aExist[] = $this->big;
  132. }
  133. if ($this->small1 != '')
  134. {
  135. $n = News::find($this->small1);
  136. if ($n != null)
  137. {
  138. if ($n->section_id == $id && $n->section_position != '')
  139. {
  140. $n->section_position = '';
  141. $n->save();
  142. }
  143. if ($n->region_1_id == $id && $n->region_1_position != '')
  144. {
  145. $n->region_1_position = '';
  146. $n->save();
  147. }
  148. if ($n->region_2_id == $id && $n->region_2_position != '')
  149. {
  150. $n->region_2_position = '';
  151. $n->save();
  152. }
  153. }
  154. $aExist[] = $this->small1;
  155. }
  156. if ($this->small2 != '')
  157. {
  158. $n = News::find($this->small2);
  159. if ($n != null)
  160. {
  161. if ($n->section_id == $id && $n->section_position != '')
  162. {
  163. $n->section_position = '';
  164. $n->save();
  165. }
  166. if ($n->region_1_id == $id && $n->region_1_position != '')
  167. {
  168. $n->region_1_position = '';
  169. $n->save();
  170. }
  171. if ($n->region_2_id == $id && $n->region_2_position != '')
  172. {
  173. $n->region_2_position = '';
  174. $n->save();
  175. }
  176. }
  177. $aExist[] = $this->small2;
  178. }
  179. if ($this->small3 != '')
  180. {
  181. $n = News::find($this->small3);
  182. if ($n != null)
  183. {
  184. if ($n->section_id == $id && $n->section_position != '')
  185. {
  186. $n->section_position = '';
  187. $n->save();
  188. }
  189. if ($n->region_1_id == $id && $n->region_1_position != '')
  190. {
  191. $n->region_1_position = '';
  192. $n->save();
  193. }
  194. if ($n->region_2_id == $id && $n->region_2_position != '')
  195. {
  196. $n->region_2_position = '';
  197. $n->save();
  198. }
  199. }
  200. $aExist[] = $this->small3;
  201. }
  202. if ($this->small4 != '')
  203. {
  204. $n = News::find($this->small4);
  205. if ($n != null)
  206. {
  207. if ($n->section_id == $id && $n->section_position != '')
  208. {
  209. $n->section_position = '';
  210. $n->save();
  211. }
  212. if ($n->region_1_id == $id && $n->region_1_position != '')
  213. {
  214. $n->region_1_position = '';
  215. $n->save();
  216. }
  217. if ($n->region_2_id == $id && $n->region_2_position != '')
  218. {
  219. $n->region_2_position = '';
  220. $n->save();
  221. }
  222. }
  223. $aExist[] = $this->small4;
  224. }
  225. if ($this->small5 != '')
  226. {
  227. $n = News::find($this->small5);
  228. if ($n != null)
  229. {
  230. if ($n->section_id == $id && $n->section_position != '')
  231. {
  232. $n->section_position = '';
  233. $n->save();
  234. }
  235. if ($n->region_1_id == $id && $n->region_1_position != '')
  236. {
  237. $n->region_1_position = '';
  238. $n->save();
  239. }
  240. if ($n->region_2_id == $id && $n->region_2_position != '')
  241. {
  242. $n->region_2_position = '';
  243. $n->save();
  244. }
  245. }
  246. $aExist[] = $this->small5;
  247. }
  248. if ($this->small6 != '')
  249. {
  250. $n = News::find($this->small6);
  251. if ($n != null)
  252. {
  253. if ($n->section_id == $id && $n->section_position != '')
  254. {
  255. $n->section_position = '';
  256. $n->save();
  257. }
  258. if ($n->region_1_id == $id && $n->region_1_position != '')
  259. {
  260. $n->region_1_position = '';
  261. $n->save();
  262. }
  263. if ($n->region_2_id == $id && $n->region_2_position != '')
  264. {
  265. $n->region_2_position = '';
  266. $n->save();
  267. }
  268. }
  269. $aExist[] = $this->small6;
  270. }
  271. $this->data = $this;
  272. if ($this->big != '')
  273. {
  274. $n = News::find($this->big);
  275. if ($n != null)
  276. {
  277. if (!$n->online)
  278. $n = null;
  279. $position = 'big';
  280. // Controllo se c'è una news dopo questa da pubblicare che va in questa sezione/posizione, se si faccio il replace
  281. $checks = News::whereNotIn('id', $aExist)->where('date', '<=', date('Y-m-d H:i:s'))->where('online', '=', true)->where(function ($q) use ($id) {
  282. $q->where('region_1_id', '=', $id)->orWhere('region_2_id', '=', $id)->orWhere('section_id', '=', $id);
  283. })->where(function ($q) use ($position) {
  284. $q->where('region_1_position', '=', $position)->orWhere('region_2_position', '=', $position)->orWhere('section_position', '=', $position);
  285. })->get();
  286. foreach($checks as $c)
  287. {
  288. if ($c->section_id == $id && $c->section_position == $position)
  289. {
  290. $s = Section::findOrFail($this->id);
  291. $s->big = $c->id;
  292. $s->save();
  293. $c->section_position = '';
  294. if (!$c->published)
  295. {
  296. $c->notify(new FacebookPost());
  297. $c->notify(new TwitterPost());
  298. $c->published = true;
  299. }
  300. $c->save();
  301. $n = $c;
  302. break;
  303. }
  304. if ($c->region_1_id == $id && $c->region_1_position == $position)
  305. {
  306. $s = Section::findOrFail($this->id);
  307. $s->big = $c->id;
  308. $s->save();
  309. $c->region_1_position = '';
  310. if (!$c->published)
  311. {
  312. $c->notify(new FacebookPost());
  313. $c->notify(new TwitterPost());
  314. $c->published = true;
  315. }
  316. $c->save();
  317. $n = $c;
  318. break;
  319. }
  320. if ($c->region_2_id == $id && $c->region_2_position == $position)
  321. {
  322. $s = Section::findOrFail($this->id);
  323. $s->big = $c->id;
  324. $s->save();
  325. $c->region_2_position = '';
  326. if (!$c->published)
  327. {
  328. $c->notify(new FacebookPost());
  329. $c->notify(new TwitterPost());
  330. $c->published = true;
  331. }
  332. $c->save();
  333. $n = $c;
  334. break;
  335. }
  336. }
  337. $this->big = $n;
  338. }
  339. }
  340. if ($this->small1 != '')
  341. {
  342. $n = News::find($this->small1);
  343. if($n != null)
  344. {
  345. if (!$n->online)
  346. $n = null;
  347. $position = 'small1';
  348. // Controllo se c'è una news dopo questa da pubblicare che va in questa sezione/posizione, se si faccio il replace
  349. $checks = News::whereNotIn('id', $aExist)->where('date', '<=', date('Y-m-d H:i:s'))->where('online', '=', true)->where(function ($q) use ($id) {
  350. $q->where('region_1_id', '=', $id)->orWhere('region_2_id', '=', $id)->orWhere('section_id', '=', $id);
  351. })->where(function ($q) use ($position) {
  352. $q->where('region_1_position', '=', $position)->orWhere('region_2_position', '=', $position)->orWhere('section_position', '=', $position);
  353. })->get();
  354. foreach($checks as $c)
  355. {
  356. if ($c->section_id == $id && $c->section_position == $position)
  357. {
  358. $s = Section::findOrFail($this->id);
  359. $s->small1 = $c->id;
  360. $s->save();
  361. $c->section_position = '';
  362. if (!$c->published)
  363. {
  364. $c->notify(new FacebookPost());
  365. $c->notify(new TwitterPost());
  366. $c->published = true;
  367. }
  368. $c->save();
  369. $n = $c;
  370. break;
  371. }
  372. if ($c->region_1_id == $id && $c->region_1_position == $position)
  373. {
  374. $s = Section::findOrFail($this->id);
  375. $s->small1 = $c->id;
  376. $s->save();
  377. $c->region_1_position = '';
  378. if (!$c->published)
  379. {
  380. $c->notify(new FacebookPost());
  381. $c->notify(new TwitterPost());
  382. $c->published = true;
  383. }
  384. $c->save();
  385. $n = $c;
  386. break;
  387. }
  388. if ($c->region_2_id == $id && $c->region_2_position == $position)
  389. {
  390. $s = Section::findOrFail($this->id);
  391. $s->small1 = $c->id;
  392. $s->save();
  393. $c->region_2_position = '';
  394. if (!$c->published)
  395. {
  396. $c->notify(new FacebookPost());
  397. $c->notify(new TwitterPost());
  398. $c->published = true;
  399. }
  400. $c->save();
  401. $n = $c;
  402. break;
  403. }
  404. }
  405. $this->small1 = $n;
  406. }
  407. }
  408. if ($this->small2 != '')
  409. {
  410. $n = News::find($this->small2);
  411. if($n != null)
  412. {
  413. if (!$n->online)
  414. $n = null;
  415. $position = 'small2';
  416. // Controllo se c'è una news dopo questa da pubblicare che va in questa sezione/posizione, se si faccio il replace
  417. $checks = News::whereNotIn('id', $aExist)->where('date', '<=', date('Y-m-d H:i:s'))->where('online', '=', true)->where(function ($q) use ($id) {
  418. $q->where('region_1_id', '=', $id)->orWhere('region_2_id', '=', $id)->orWhere('section_id', '=', $id);
  419. })->where(function ($q) use ($position) {
  420. $q->where('region_1_position', '=', $position)->orWhere('region_2_position', '=', $position)->orWhere('section_position', '=', $position);
  421. })->get();
  422. foreach($checks as $c)
  423. {
  424. if ($c->section_id == $id && $c->section_position == $position)
  425. {
  426. $s = Section::findOrFail($this->id);
  427. $s->small2 = $c->id;
  428. $s->save();
  429. $c->section_position = '';
  430. if (!$c->published)
  431. {
  432. $c->notify(new FacebookPost());
  433. $c->notify(new TwitterPost());
  434. $c->published = true;
  435. }
  436. $c->save();
  437. $n = $c;
  438. break;
  439. }
  440. if ($c->region_1_id == $id && $c->region_1_position == $position)
  441. {
  442. $s = Section::findOrFail($this->id);
  443. $s->small2 = $c->id;
  444. $s->save();
  445. $c->region_1_position = '';
  446. if (!$c->published)
  447. {
  448. $c->notify(new FacebookPost());
  449. $c->notify(new TwitterPost());
  450. $c->published = true;
  451. }
  452. $c->save();
  453. $n = $c;
  454. break;
  455. }
  456. if ($c->region_2_id == $id && $c->region_2_position == $position)
  457. {
  458. $s = Section::findOrFail($this->id);
  459. $s->small2 = $c->id;
  460. $s->save();
  461. $c->region_2_position = '';
  462. if (!$c->published)
  463. {
  464. $c->notify(new FacebookPost());
  465. $c->notify(new TwitterPost());
  466. $c->published = true;
  467. }
  468. $c->save();
  469. $n = $c;
  470. break;
  471. }
  472. }
  473. $this->small2 = $n;
  474. }
  475. }
  476. if ($this->small3 != '')
  477. {
  478. $n = News::find($this->small3);
  479. if($n != null)
  480. {
  481. if (!$n->online)
  482. $n = null;
  483. $position = 'small3';
  484. // Controllo se c'è una news dopo questa da pubblicare che va in questa sezione/posizione, se si faccio il replace
  485. $checks = News::whereNotIn('id', $aExist)->where('date', '<=', date('Y-m-d H:i:s'))->where('online', '=', true)->where(function ($q) use ($id) {
  486. $q->where('region_1_id', '=', $id)->orWhere('region_2_id', '=', $id)->orWhere('section_id', '=', $id);
  487. })->where(function ($q) use ($position) {
  488. $q->where('region_1_position', '=', $position)->orWhere('region_2_position', '=', $position)->orWhere('section_position', '=', $position);
  489. })->get();
  490. foreach($checks as $c)
  491. {
  492. if ($c->section_id == $id && $c->section_position == $position)
  493. {
  494. $s = Section::findOrFail($this->id);
  495. $s->small3 = $c->id;
  496. $s->save();
  497. $c->section_position = '';
  498. if (!$c->published)
  499. {
  500. $c->notify(new FacebookPost());
  501. $c->notify(new TwitterPost());
  502. $c->published = true;
  503. }
  504. $c->save();
  505. $n = $c;
  506. break;
  507. }
  508. if ($c->region_1_id == $id && $c->region_1_position == $position)
  509. {
  510. $s = Section::findOrFail($this->id);
  511. $s->small3 = $c->id;
  512. $s->save();
  513. $c->region_1_position = '';
  514. if (!$c->published)
  515. {
  516. $c->notify(new FacebookPost());
  517. $c->notify(new TwitterPost());
  518. $c->published = true;
  519. }
  520. $c->save();
  521. $n = $c;
  522. break;
  523. }
  524. if ($c->region_2_id == $id && $c->region_2_position == $position)
  525. {
  526. $s = Section::findOrFail($this->id);
  527. $s->small3 = $c->id;
  528. $s->save();
  529. $c->region_2_position = '';
  530. if (!$c->published)
  531. {
  532. $c->notify(new FacebookPost());
  533. $c->notify(new TwitterPost());
  534. $c->published = true;
  535. }
  536. $c->save();
  537. $n = $c;
  538. break;
  539. }
  540. }
  541. $this->small3 = $n;
  542. }
  543. }
  544. if ($this->small4 != '')
  545. {
  546. $n = News::find($this->small4);
  547. if($n != null)
  548. {
  549. if (!$n->online)
  550. $n = null;
  551. $position = 'small4';
  552. // Controllo se c'è una news dopo questa da pubblicare che va in questa sezione/posizione, se si faccio il replace
  553. $checks = News::whereNotIn('id', $aExist)->where('date', '<=', date('Y-m-d H:i:s'))->where('online', '=', true)->where(function ($q) use ($id) {
  554. $q->where('region_1_id', '=', $id)->orWhere('region_2_id', '=', $id)->orWhere('section_id', '=', $id);
  555. })->where(function ($q) use ($position) {
  556. $q->where('region_1_position', '=', $position)->orWhere('region_2_position', '=', $position)->orWhere('section_position', '=', $position);
  557. })->get();
  558. foreach($checks as $c)
  559. {
  560. if ($c->section_id == $id && $c->section_position == $position)
  561. {
  562. $s = Section::findOrFail($this->id);
  563. $s->small4 = $c->id;
  564. $s->save();
  565. $c->section_position = '';
  566. if (!$c->published)
  567. {
  568. $c->notify(new FacebookPost());
  569. $c->notify(new TwitterPost());
  570. $c->published = true;
  571. }
  572. $c->save();
  573. $n = $c;
  574. break;
  575. }
  576. if ($c->region_1_id == $id && $c->region_1_position == $position)
  577. {
  578. $s = Section::findOrFail($this->id);
  579. $s->small4 = $c->id;
  580. $s->save();
  581. $c->region_1_position = '';
  582. if (!$c->published)
  583. {
  584. $c->notify(new FacebookPost());
  585. $c->notify(new TwitterPost());
  586. $c->published = true;
  587. }
  588. $c->save();
  589. $n = $c;
  590. break;
  591. }
  592. if ($c->region_2_id == $id && $c->region_2_position == $position)
  593. {
  594. $s = Section::findOrFail($this->id);
  595. $s->small4 = $c->id;
  596. $s->save();
  597. $c->region_2_position = '';
  598. if (!$c->published)
  599. {
  600. $c->notify(new FacebookPost());
  601. $c->notify(new TwitterPost());
  602. $c->published = true;
  603. }
  604. $c->save();
  605. $n = $c;
  606. break;
  607. }
  608. }
  609. $this->small4 = $n;
  610. }
  611. }
  612. if ($this->small5 != '')
  613. {
  614. $n = News::find($this->small5);
  615. if($n != null)
  616. {
  617. if (!$n->online)
  618. $n = null;
  619. $position = 'small5';
  620. // Controllo se c'è una news dopo questa da pubblicare che va in questa sezione/posizione, se si faccio il replace
  621. $checks = News::whereNotIn('id', $aExist)->where('date', '<=', date('Y-m-d H:i:s'))->where('online', '=', true)->where(function ($q) use ($id) {
  622. $q->where('region_1_id', '=', $id)->orWhere('region_2_id', '=', $id)->orWhere('section_id', '=', $id);
  623. })->where(function ($q) use ($position) {
  624. $q->where('region_1_position', '=', $position)->orWhere('region_2_position', '=', $position)->orWhere('section_position', '=', $position);
  625. })->get();
  626. foreach($checks as $c)
  627. {
  628. if ($c->section_id == $id && $c->section_position == $position)
  629. {
  630. $s = Section::findOrFail($this->id);
  631. $s->small5 = $c->id;
  632. $s->save();
  633. $c->section_position = '';
  634. if (!$c->published)
  635. {
  636. $c->notify(new FacebookPost());
  637. $c->notify(new TwitterPost());
  638. $c->published = true;
  639. }
  640. $c->save();
  641. $n = $c;
  642. break;
  643. }
  644. if ($c->region_1_id == $id && $c->region_1_position == $position)
  645. {
  646. $s = Section::findOrFail($this->id);
  647. $s->small5 = $c->id;
  648. $s->save();
  649. $c->region_1_position = '';
  650. if (!$c->published)
  651. {
  652. $c->notify(new FacebookPost());
  653. $c->notify(new TwitterPost());
  654. $c->published = true;
  655. }
  656. $c->save();
  657. $n = $c;
  658. break;
  659. }
  660. if ($c->region_2_id == $id && $c->region_2_position == $position)
  661. {
  662. $s = Section::findOrFail($this->id);
  663. $s->small5 = $c->id;
  664. $s->save();
  665. $c->region_2_position = '';
  666. if (!$c->published)
  667. {
  668. $c->notify(new FacebookPost());
  669. $c->notify(new TwitterPost());
  670. $c->published = true;
  671. }
  672. $c->save();
  673. $n = $c;
  674. break;
  675. }
  676. }
  677. $this->small5 = $n;
  678. }
  679. }
  680. if ($this->small6 != '')
  681. {
  682. $n = News::find($this->small6);
  683. if($n != null)
  684. {
  685. if (!$n->online)
  686. $n = null;
  687. $position = 'small6';
  688. // Controllo se c'è una news dopo questa da pubblicare che va in questa sezione/posizione, se si faccio il replace
  689. $checks = News::whereNotIn('id', $aExist)->where('date', '<=', date('Y-m-d H:i:s'))->where('online', '=', true)->where(function ($q) use ($id) {
  690. $q->where('region_1_id', '=', $id)->orWhere('region_2_id', '=', $id)->orWhere('section_id', '=', $id);
  691. })->where(function ($q) use ($position) {
  692. $q->where('region_1_position', '=', $position)->orWhere('region_2_position', '=', $position)->orWhere('section_position', '=', $position);
  693. })->get();
  694. foreach($checks as $c)
  695. {
  696. if ($c->section_id == $id && $c->section_position == $position)
  697. {
  698. $s = Section::findOrFail($this->id);
  699. $s->small6 = $c->id;
  700. $s->save();
  701. $c->section_position = '';
  702. if (!$c->published)
  703. {
  704. $c->notify(new FacebookPost());
  705. $c->notify(new TwitterPost());
  706. $c->published = true;
  707. }
  708. $c->save();
  709. $n = $c;
  710. break;
  711. }
  712. if ($c->region_1_id == $id && $c->region_1_position == $position)
  713. {
  714. $s = Section::findOrFail($this->id);
  715. $s->small6 = $c->id;
  716. $s->save();
  717. $c->region_1_position = '';
  718. if (!$c->published)
  719. {
  720. $c->notify(new FacebookPost());
  721. $c->notify(new TwitterPost());
  722. $c->published = true;
  723. }
  724. $c->save();
  725. $n = $c;
  726. break;
  727. }
  728. if ($c->region_2_id == $id && $c->region_2_position == $position)
  729. {
  730. $s = Section::findOrFail($this->id);
  731. $s->small6 = $c->id;
  732. $s->save();
  733. $c->region_2_position = '';
  734. if (!$c->published)
  735. {
  736. $c->notify(new FacebookPost());
  737. $c->notify(new TwitterPost());
  738. $c->published = true;
  739. }
  740. $c->save();
  741. $n = $c;
  742. break;
  743. }
  744. }
  745. $this->small6 = $n;
  746. }
  747. }
  748. }*/
  749. function news_online($type)
  750. {
  751. if ($type == 'section')
  752. return News::where('section_id', '=', $this->id)->where('online', '=', true)->count();
  753. else
  754. {
  755. $id = $this->id;
  756. return News::where('online', '=', true)->where(function ($q) use ($id) {
  757. $q->where('region_1_id', '=', $id)->orWhere('region_2_id', '=', $id);
  758. })->count();
  759. }
  760. }
  761. function news($type)
  762. {
  763. if ($type == 'section')
  764. return News::where('section_id', '=', $this->id)->count();
  765. else
  766. return News::where('region_1_id', '=', $this->id)->orWhere('region_2_id', '=', $this->id)->count();
  767. }
  768. public function advs()
  769. {
  770. return $this->hasMany('App\SectionAdv');
  771. }
  772. public function getAdvs()
  773. {
  774. $advs = array();
  775. $sections_advs = $this->advs()->where('online', '=', true)->get(); //SectionAdv::where('section_id', '=', $section->id)->orderBy('position')->get();
  776. foreach($sections_advs as $a)
  777. {
  778. $advs[$a->position][] = $a;
  779. }
  780. return $advs;
  781. }
  782. public function position($news_id)
  783. {
  784. $img = '';
  785. if ($this->layout != '')
  786. {
  787. if($this->big == $news_id)
  788. $img = $this->layout . '_big';
  789. /*if($this->medium1 == $news_id)
  790. $img = $this->layout . '_medium1';
  791. if($this->medium2 == $news_id)
  792. $img = $this->layout . '_medium2';
  793. if($this->medium3 == $news_id)
  794. $img = $this->layout . '_medium3';*/
  795. if($this->small1 == $news_id)
  796. $img = $this->layout . '_small1';
  797. if($this->small2 == $news_id)
  798. $img = $this->layout . '_small2';
  799. if($this->small3 == $news_id)
  800. $img = $this->layout . '_small3';
  801. if($this->small4 == $news_id)
  802. $img = $this->layout . '_small4';
  803. if($this->small5 == $news_id)
  804. $img = $this->layout . '_small5';
  805. if($this->small6 == $news_id)
  806. $img = $this->layout . '_small6';
  807. }
  808. $ret = '';
  809. if ($img != '')
  810. $ret = '<img src="/images/layout/' . $img . '.jpg" style="max-width:100px;">';
  811. return $ret;
  812. }
  813. /*
  814. public function clearLayout()
  815. {
  816. $id = $this->id;
  817. $aSel = array();
  818. if ($this->big)
  819. $aSel[] = $this->big;
  820. if ($this->small1)
  821. $aSel[] = $this->small1;
  822. if ($this->small2)
  823. $aSel[] = $this->small2;
  824. if ($this->small3)
  825. $aSel[] = $this->small3;
  826. if ($this->small4)
  827. $aSel[] = $this->small4;
  828. if ($this->small5)
  829. $aSel[] = $this->small5;
  830. if ($this->small6)
  831. $aSel[] = $this->small6;
  832. $news = News::whereNotIn('id', $aSel)->where(function ($q) use ($id) {
  833. $q->where('section_id', '=', $id)->orWhere('region_1_id', '=', $id)->orWhere('region_2_id', '=', $id);
  834. })->where(function ($q) {
  835. $q->where('region_1_position', '<>', '')->orWhere('region_2_position', '<>', '')->orWhere('section_position', '<>', '');
  836. })->where('online', '=', true)->where('date', '<=', date("Y-m-d 23:59:59"))->where('breaking_news', '=', false)->orderBy('date', 'DESC')->get();
  837. $idx = 0;
  838. if ($this->big == '')
  839. {
  840. $this->big = @$news[$idx]->id;
  841. $idx++;
  842. }
  843. else
  844. {
  845. $n = News::where('id', '=', $this->big)->where('online', '=', true)->where('date', '<=', date("Y-m-d 23:59:59"))->where('breaking_news', '=', false)->first();
  846. if (!$n || !$n->online)
  847. {
  848. $this->big = @$news[$idx]->id;
  849. $idx++;
  850. }
  851. }
  852. if ($this->small1 == '')
  853. {
  854. $this->small1 = @$news[$idx]->id;
  855. $idx++;
  856. }
  857. else
  858. {
  859. $n = News::where('id', '=', $this->small1)->where('online', '=', true)->where('date', '<=', date("Y-m-d 23:59:59"))->where('breaking_news', '=', false)->first();
  860. if (!$n || !$n->online)
  861. {
  862. $this->small1 = @$news[$idx]->id;
  863. $idx++;
  864. }
  865. }
  866. if ($this->small2 == '')
  867. {
  868. $this->small2 = @$news[$idx]->id;
  869. $idx++;
  870. }
  871. else
  872. {
  873. $n = News::where('id', '=', $this->small2)->where('online', '=', true)->where('date', '<=', date("Y-m-d 23:59:59"))->where('breaking_news', '=', false)->first();
  874. if (!$n || !$n->online)
  875. {
  876. $this->small2 = @$news[$idx]->id;
  877. $idx++;
  878. }
  879. }
  880. if ($this->small3 == '')
  881. {
  882. $this->small3 = @$news[$idx]->id;
  883. $idx++;
  884. }
  885. else
  886. {
  887. $n = News::where('id', '=', $this->small3)->where('online', '=', true)->where('date', '<=', date("Y-m-d 23:59:59"))->where('breaking_news', '=', false)->first();
  888. if (!$n || !$n->online)
  889. {
  890. $this->small3 = @$news[$idx]->id;
  891. $idx++;
  892. }
  893. }
  894. if ($this->layout == 'layout_3' || $this->layout == 'layout_4' || $this->layout == 'layout_5')
  895. {
  896. if ($this->small4 == '')
  897. {
  898. $this->small4 = @$news[$idx]->id;
  899. $idx++;
  900. }
  901. else
  902. {
  903. $n = News::where('id', '=', $this->small4)->where('online', '=', true)->where('date', '<=', date("Y-m-d 23:59:59"))->where('breaking_news', '=', false)->first();
  904. if (!$n || !$n->online)
  905. {
  906. $this->small4 = @$news[$idx]->id;
  907. $idx++;
  908. }
  909. }
  910. if ($this->layout == 'layout_4')
  911. {
  912. if ($this->small5 == '')
  913. {
  914. $this->small5 = @$news[$idx]->id;
  915. $idx++;
  916. }
  917. else
  918. {
  919. $n = News::where('id', '=', $this->small5)->where('online', '=', true)->where('date', '<=', date("Y-m-d 23:59:59"))->where('breaking_news', '=', false)->first();
  920. if (!$n || !$n->online)
  921. {
  922. $this->small5 = @$news[$idx]->id;
  923. $idx++;
  924. }
  925. }
  926. if ($this->small6 == '')
  927. {
  928. $this->small6 = @$news[$idx]->id;
  929. $idx++;
  930. }
  931. else
  932. {
  933. $n = News::where('id', '=', $this->small6)->where('online', '=', true)->where('date', '<=', date("Y-m-d 23:59:59"))->where('breaking_news', '=', false)->first();
  934. if (!$n || !$n->online)
  935. {
  936. $this->small6 = @$news[$idx]->id;
  937. $idx++;
  938. }
  939. }
  940. }
  941. else
  942. {
  943. $this->small5 = '';
  944. $this->small6 = '';
  945. }
  946. }
  947. else
  948. {
  949. $this->small4 = '';
  950. $this->small5 = '';
  951. $this->small6 = '';
  952. }
  953. $this->save();
  954. $this->generateLayout();
  955. // $section->save();
  956. }
  957. */
  958. }