<?php

namespace App;

use App\News;

use Illuminate\Database\Eloquent\Model;

use App\Notifications\FacebookPost;
use App\Notifications\TwitterPost;

class Section extends Model
{

    protected $fillable = [
        'name',
        'type',
        'position',
        'layout',
        'big',
        'medium1',
        'medium2',
        'medium3',
        'small1',
        'small2',
        'small3',
        'small4',
        'small5',
        'small6',
        'slug',
        'before',
        'after',
        'show_social',
        'show_social_twitter'
    ];

    function loadLayout()
    {

        $id = $this->id;

        $this->data = $this;
        if ($this->big != '')
        {
            $n = News::find($this->big);
            if ($n != null)
            {
                if (!$n->online)
                    $n = null;

                $this->big = $n;
            }
        }
        
        if ($this->small1 != '')
        {
            $n = News::find($this->small1);
            if($n != null)
            {
                if (!$n->online)
                    $n = null;

                $this->small1 = $n;
            }
        }
        if ($this->small2 != '')
        {
            $n = News::find($this->small2);
            if($n != null)
            {
                if (!$n->online)
                    $n = null;

                $this->small2 = $n;
            }
        }
        if ($this->small3 != '')
        {
            $n = News::find($this->small3);
            if($n != null)
            {
                if (!$n->online)
                    $n = null;

                $this->small3 = $n;
            }
        }
        if ($this->small4 != '')
        {
            $n = News::find($this->small4);
            if($n != null)
            {
                if (!$n->online)
                    $n = null;

                $this->small4 = $n;
            }
        }
        if ($this->small5 != '')
        {
            $n = News::find($this->small5);
            if($n != null)
            {
                if (!$n->online)
                    $n = null;

                $this->small5 = $n;
            }
        }
        if ($this->small6 != '')
        {
            $n = News::find($this->small6);
            if($n != null)
            {
                if (!$n->online)
                    $n = null;

                $this->small6 = $n;
            }
        }

    }

    /*
    function generateLayout()
    {

        $id = $this->id;

        $aExist = array();
        if ($this->big != '')
        {
            $n = News::find($this->big);
            if ($n != null)
            {
                if ($n->section_id == $id && $n->section_position != '')
                {
                    $n->section_position = '';
                    $n->save();
                }
                if ($n->region_1_id == $id && $n->region_1_position != '')
                {
                    $n->region_1_position = '';
                    $n->save();
                }
                if ($n->region_2_id == $id && $n->region_2_position != '')
                {
                    $n->region_2_position = '';
                    $n->save();
                }
            }
            $aExist[] = $this->big;
        }
        if ($this->small1 != '')
        {
            $n = News::find($this->small1);
            if ($n != null)
            {
                if ($n->section_id == $id && $n->section_position != '')
                {
                    $n->section_position = '';
                    $n->save();
                }
                if ($n->region_1_id == $id && $n->region_1_position != '')
                {
                    $n->region_1_position = '';
                    $n->save();
                }
                if ($n->region_2_id == $id && $n->region_2_position != '')
                {
                    $n->region_2_position = '';
                    $n->save();
                }
            }
            $aExist[] = $this->small1;
        }
        if ($this->small2 != '')
        {
            $n = News::find($this->small2);
            if ($n != null)
            {
                if ($n->section_id == $id && $n->section_position != '')
                {
                    $n->section_position = '';
                    $n->save();
                }
                if ($n->region_1_id == $id && $n->region_1_position != '')
                {
                    $n->region_1_position = '';
                    $n->save();
                }
                if ($n->region_2_id == $id && $n->region_2_position != '')
                {
                    $n->region_2_position = '';
                    $n->save();
                }
            }
            $aExist[] = $this->small2;
        }
        if ($this->small3 != '')
        {
            $n = News::find($this->small3);
            if ($n != null)
            {
                if ($n->section_id == $id && $n->section_position != '')
                {
                    $n->section_position = '';
                    $n->save();
                }
                if ($n->region_1_id == $id && $n->region_1_position != '')
                {
                    $n->region_1_position = '';
                    $n->save();
                }
                if ($n->region_2_id == $id && $n->region_2_position != '')
                {
                    $n->region_2_position = '';
                    $n->save();
                }
            }
            $aExist[] = $this->small3;
        }
        if ($this->small4 != '')
        {
            $n = News::find($this->small4);
            if ($n != null)
            {
                if ($n->section_id == $id && $n->section_position != '')
                {
                    $n->section_position = '';
                    $n->save();
                }
                if ($n->region_1_id == $id && $n->region_1_position != '')
                {
                    $n->region_1_position = '';
                    $n->save();
                }
                if ($n->region_2_id == $id && $n->region_2_position != '')
                {
                    $n->region_2_position = '';
                    $n->save();
                }
            }
            $aExist[] = $this->small4;
        }
        if ($this->small5 != '')
        {
            $n = News::find($this->small5);
            if ($n != null)
            {
                if ($n->section_id == $id && $n->section_position != '')
                {
                    $n->section_position = '';
                    $n->save();
                }
                if ($n->region_1_id == $id && $n->region_1_position != '')
                {
                    $n->region_1_position = '';
                    $n->save();
                }
                if ($n->region_2_id == $id && $n->region_2_position != '')
                {
                    $n->region_2_position = '';
                    $n->save();
                }
            }
            $aExist[] = $this->small5;
        }
        if ($this->small6 != '')
        {
            $n = News::find($this->small6);
            if ($n != null)
            {
                if ($n->section_id == $id && $n->section_position != '')
                {
                    $n->section_position = '';
                    $n->save();
                }
                if ($n->region_1_id == $id && $n->region_1_position != '')
                {
                    $n->region_1_position = '';
                    $n->save();
                }
                if ($n->region_2_id == $id && $n->region_2_position != '')
                {
                    $n->region_2_position = '';
                    $n->save();
                }
            }
            $aExist[] = $this->small6;
        }
            
        $this->data = $this;
        if ($this->big != '')
        {
            $n = News::find($this->big);
            if ($n != null)
            {
                if (!$n->online)
                    $n = null;

                $position = 'big';

                // Controllo se c'è una news dopo questa da pubblicare che va in questa sezione/posizione, se si faccio il replace
                $checks = News::whereNotIn('id', $aExist)->where('date', '<=', date('Y-m-d H:i:s'))->where('online', '=', true)->where(function ($q) use ($id) {
                    $q->where('region_1_id', '=', $id)->orWhere('region_2_id', '=', $id)->orWhere('section_id', '=', $id);
                })->where(function ($q) use ($position) {
                    $q->where('region_1_position', '=', $position)->orWhere('region_2_position', '=', $position)->orWhere('section_position', '=', $position);
                })->get();
                foreach($checks as $c)
                {
                    
                    if ($c->section_id == $id && $c->section_position == $position)
                    {
                        $s = Section::findOrFail($this->id);
                        $s->big = $c->id;
                        $s->save();
                        $c->section_position = '';
                        if (!$c->published)
                        {
                            $c->notify(new FacebookPost());
                            $c->notify(new TwitterPost());
                            $c->published = true;
                        }
                        $c->save();
                        $n = $c;
                        break;
                    }
                    if ($c->region_1_id == $id && $c->region_1_position == $position)
                    {
                        $s = Section::findOrFail($this->id);
                        $s->big = $c->id;
                        $s->save();
                        $c->region_1_position = '';
                        if (!$c->published)
                        {
                            $c->notify(new FacebookPost());
                            $c->notify(new TwitterPost());
                            $c->published = true;
                        }
                        $c->save();
                        $n = $c;
                        break;
                    }
                    if ($c->region_2_id == $id && $c->region_2_position == $position)
                    {
                        $s = Section::findOrFail($this->id);
                        $s->big = $c->id;
                        $s->save();
                        $c->region_2_position = '';
                        if (!$c->published)
                        {
                            $c->notify(new FacebookPost());
                            $c->notify(new TwitterPost());
                            $c->published = true;
                        }
                        $c->save();
                        $n = $c;
                        break;
                    }
                }

                $this->big = $n;
            }
        }
        
        if ($this->small1 != '')
        {
            $n = News::find($this->small1);
            if($n != null)
            {
                if (!$n->online)
                    $n = null;

                    $position = 'small1';

                    // Controllo se c'è una news dopo questa da pubblicare che va in questa sezione/posizione, se si faccio il replace
                    $checks = News::whereNotIn('id', $aExist)->where('date', '<=', date('Y-m-d H:i:s'))->where('online', '=', true)->where(function ($q) use ($id) {
                        $q->where('region_1_id', '=', $id)->orWhere('region_2_id', '=', $id)->orWhere('section_id', '=', $id);
                    })->where(function ($q) use ($position) {
                        $q->where('region_1_position', '=', $position)->orWhere('region_2_position', '=', $position)->orWhere('section_position', '=', $position);
                    })->get();
                    foreach($checks as $c)
                    {
                        if ($c->section_id == $id && $c->section_position == $position)
                        {
                            $s = Section::findOrFail($this->id);
                            $s->small1 = $c->id;
                            $s->save();
                            $c->section_position = '';
                            if (!$c->published)
                            {
                                $c->notify(new FacebookPost());
                                $c->notify(new TwitterPost());
                                $c->published = true;
                            }
                            $c->save();
                            $n = $c;
                            break;
                        }
                        if ($c->region_1_id == $id && $c->region_1_position == $position)
                        {
                            $s = Section::findOrFail($this->id);
                            $s->small1 = $c->id;
                            $s->save();
                            $c->region_1_position = '';
                            if (!$c->published)
                            {
                                $c->notify(new FacebookPost());
                                $c->notify(new TwitterPost());
                                $c->published = true;
                            }
                            $c->save();
                            $n = $c;
                            break;
                        }
                        if ($c->region_2_id == $id && $c->region_2_position == $position)
                        {
                            $s = Section::findOrFail($this->id);
                            $s->small1 = $c->id;
                            $s->save();
                            $c->region_2_position = '';
                            if (!$c->published)
                            {
                                $c->notify(new FacebookPost());
                                $c->notify(new TwitterPost());
                                $c->published = true;
                            }
                            $c->save();
                            $n = $c;
                            break;
                        }
                    }

                $this->small1 = $n;
            }
        }
        if ($this->small2 != '')
        {
            $n = News::find($this->small2);
            if($n != null)
            {
                if (!$n->online)
                    $n = null;

                    $position = 'small2';

                    // Controllo se c'è una news dopo questa da pubblicare che va in questa sezione/posizione, se si faccio il replace
                    $checks = News::whereNotIn('id', $aExist)->where('date', '<=', date('Y-m-d H:i:s'))->where('online', '=', true)->where(function ($q) use ($id) {
                        $q->where('region_1_id', '=', $id)->orWhere('region_2_id', '=', $id)->orWhere('section_id', '=', $id);
                    })->where(function ($q) use ($position) {
                        $q->where('region_1_position', '=', $position)->orWhere('region_2_position', '=', $position)->orWhere('section_position', '=', $position);
                    })->get();
                    foreach($checks as $c)
                    {
                        if ($c->section_id == $id && $c->section_position == $position)
                        {
                            $s = Section::findOrFail($this->id);
                            $s->small2 = $c->id;
                            $s->save();
                            $c->section_position = '';
                            if (!$c->published)
                            {
                                $c->notify(new FacebookPost());
                                $c->notify(new TwitterPost());
                                $c->published = true;
                            }
                            $c->save();
                            $n = $c;
                            break;
                        }
                        if ($c->region_1_id == $id && $c->region_1_position == $position)
                        {
                            $s = Section::findOrFail($this->id);
                            $s->small2 = $c->id;
                            $s->save();
                            $c->region_1_position = '';
                            if (!$c->published)
                            {
                                $c->notify(new FacebookPost());
                                $c->notify(new TwitterPost());
                                $c->published = true;
                            }
                            $c->save();
                            $n = $c;
                            break;
                        }
                        if ($c->region_2_id == $id && $c->region_2_position == $position)
                        {
                            $s = Section::findOrFail($this->id);
                            $s->small2 = $c->id;
                            $s->save();
                            $c->region_2_position = '';
                            if (!$c->published)
                            {
                                $c->notify(new FacebookPost());
                                $c->notify(new TwitterPost());
                                $c->published = true;
                            }
                            $c->save();
                            $n = $c;
                            break;
                        }
                    }

                $this->small2 = $n;
            }
        }
        if ($this->small3 != '')
        {
            $n = News::find($this->small3);
            if($n != null)
            {
                if (!$n->online)
                    $n = null;

                $position = 'small3';

                // Controllo se c'è una news dopo questa da pubblicare che va in questa sezione/posizione, se si faccio il replace
                $checks = News::whereNotIn('id', $aExist)->where('date', '<=', date('Y-m-d H:i:s'))->where('online', '=', true)->where(function ($q) use ($id) {
                    $q->where('region_1_id', '=', $id)->orWhere('region_2_id', '=', $id)->orWhere('section_id', '=', $id);
                })->where(function ($q) use ($position) {
                    $q->where('region_1_position', '=', $position)->orWhere('region_2_position', '=', $position)->orWhere('section_position', '=', $position);
                })->get();
                foreach($checks as $c)
                {
                    if ($c->section_id == $id && $c->section_position == $position)
                    {
                        $s = Section::findOrFail($this->id);
                        $s->small3 = $c->id;
                        $s->save();
                        $c->section_position = '';
                        if (!$c->published)
                        {
                            $c->notify(new FacebookPost());
                            $c->notify(new TwitterPost());
                            $c->published = true;
                        }
                        $c->save();
                        $n = $c;
                        break;
                    }
                    if ($c->region_1_id == $id && $c->region_1_position == $position)
                    {
                        $s = Section::findOrFail($this->id);
                        $s->small3 = $c->id;
                        $s->save();
                        $c->region_1_position = '';
                        if (!$c->published)
                        {
                            $c->notify(new FacebookPost());
                            $c->notify(new TwitterPost());
                            $c->published = true;
                        }
                        $c->save();
                        $n = $c;
                        break;
                    }
                    if ($c->region_2_id == $id && $c->region_2_position == $position)
                    {
                        $s = Section::findOrFail($this->id);
                        $s->small3 = $c->id;
                        $s->save();
                        $c->region_2_position = '';
                        if (!$c->published)
                        {
                            $c->notify(new FacebookPost());
                            $c->notify(new TwitterPost());
                            $c->published = true;
                        }
                        $c->save();
                        $n = $c;
                        break;
                    }
                }

                $this->small3 = $n;
            }
        }
        if ($this->small4 != '')
        {
            $n = News::find($this->small4);
            if($n != null)
            {
                if (!$n->online)
                    $n = null;
                
                    $position = 'small4';

                    // Controllo se c'è una news dopo questa da pubblicare che va in questa sezione/posizione, se si faccio il replace
                    $checks = News::whereNotIn('id', $aExist)->where('date', '<=', date('Y-m-d H:i:s'))->where('online', '=', true)->where(function ($q) use ($id) {
                        $q->where('region_1_id', '=', $id)->orWhere('region_2_id', '=', $id)->orWhere('section_id', '=', $id);
                    })->where(function ($q) use ($position) {
                        $q->where('region_1_position', '=', $position)->orWhere('region_2_position', '=', $position)->orWhere('section_position', '=', $position);
                    })->get();
                    foreach($checks as $c)
                    {
                        if ($c->section_id == $id && $c->section_position == $position)
                        {
                            $s = Section::findOrFail($this->id);
                            $s->small4 = $c->id;
                            $s->save();
                            $c->section_position = '';
                            if (!$c->published)
                            {
                                $c->notify(new FacebookPost());
                                $c->notify(new TwitterPost());
                                $c->published = true;
                            }
                            $c->save();
                            $n = $c;
                            break;
                        }
                        if ($c->region_1_id == $id && $c->region_1_position == $position)
                        {
                            $s = Section::findOrFail($this->id);
                            $s->small4 = $c->id;
                            $s->save();
                            $c->region_1_position = '';
                            if (!$c->published)
                            {
                                $c->notify(new FacebookPost());
                                $c->notify(new TwitterPost());
                                $c->published = true;
                            }
                            $c->save();
                            $n = $c;
                            break;
                        }
                        if ($c->region_2_id == $id && $c->region_2_position == $position)
                        {
                            $s = Section::findOrFail($this->id);
                            $s->small4 = $c->id;
                            $s->save();
                            $c->region_2_position = '';
                            if (!$c->published)
                            {
                                $c->notify(new FacebookPost());
                                $c->notify(new TwitterPost());
                                $c->published = true;
                            }
                            $c->save();
                            $n = $c;
                            break;
                        }
                    }

                $this->small4 = $n;
            }
        }
        if ($this->small5 != '')
        {
            $n = News::find($this->small5);
            if($n != null)
            {
                if (!$n->online)
                    $n = null;

                    $position = 'small5';

                    // Controllo se c'è una news dopo questa da pubblicare che va in questa sezione/posizione, se si faccio il replace
                    $checks = News::whereNotIn('id', $aExist)->where('date', '<=', date('Y-m-d H:i:s'))->where('online', '=', true)->where(function ($q) use ($id) {
                        $q->where('region_1_id', '=', $id)->orWhere('region_2_id', '=', $id)->orWhere('section_id', '=', $id);
                    })->where(function ($q) use ($position) {
                        $q->where('region_1_position', '=', $position)->orWhere('region_2_position', '=', $position)->orWhere('section_position', '=', $position);
                    })->get();
                    foreach($checks as $c)
                    {
                        if ($c->section_id == $id && $c->section_position == $position)
                        {
                            $s = Section::findOrFail($this->id);
                            $s->small5 = $c->id;
                            $s->save();
                            $c->section_position = '';
                            if (!$c->published)
                            {
                                $c->notify(new FacebookPost());
                                $c->notify(new TwitterPost());
                                $c->published = true;
                            }
                            $c->save();
                            $n = $c;
                            break;
                        }
                        if ($c->region_1_id == $id && $c->region_1_position == $position)
                        {
                            $s = Section::findOrFail($this->id);
                            $s->small5 = $c->id;
                            $s->save();
                            $c->region_1_position = '';
                            if (!$c->published)
                            {
                                $c->notify(new FacebookPost());
                                $c->notify(new TwitterPost());
                                $c->published = true;
                            }
                            $c->save();
                            $n = $c;
                            break;
                        }
                        if ($c->region_2_id == $id && $c->region_2_position == $position)
                        {
                            $s = Section::findOrFail($this->id);
                            $s->small5 = $c->id;
                            $s->save();
                            $c->region_2_position = '';
                            if (!$c->published)
                            {
                                $c->notify(new FacebookPost());
                                $c->notify(new TwitterPost());
                                $c->published = true;
                            }
                            $c->save();
                            $n = $c;
                            break;
                        }
                    }

                $this->small5 = $n;
            }
        }
        if ($this->small6 != '')
        {
            $n = News::find($this->small6);
            if($n != null)
            {
                if (!$n->online)
                    $n = null;

                    $position = 'small6';

                    // Controllo se c'è una news dopo questa da pubblicare che va in questa sezione/posizione, se si faccio il replace
                    $checks = News::whereNotIn('id', $aExist)->where('date', '<=', date('Y-m-d H:i:s'))->where('online', '=', true)->where(function ($q) use ($id) {
                        $q->where('region_1_id', '=', $id)->orWhere('region_2_id', '=', $id)->orWhere('section_id', '=', $id);
                    })->where(function ($q) use ($position) {
                        $q->where('region_1_position', '=', $position)->orWhere('region_2_position', '=', $position)->orWhere('section_position', '=', $position);
                    })->get();
                    foreach($checks as $c)
                    {
                        if ($c->section_id == $id && $c->section_position == $position)
                        {
                            $s = Section::findOrFail($this->id);
                            $s->small6 = $c->id;
                            $s->save();
                            $c->section_position = '';
                            if (!$c->published)
                            {
                                $c->notify(new FacebookPost());
                                $c->notify(new TwitterPost());
                                $c->published = true;
                            }
                            $c->save();
                            $n = $c;
                            break;
                        }
                        if ($c->region_1_id == $id && $c->region_1_position == $position)
                        {
                            $s = Section::findOrFail($this->id);
                            $s->small6 = $c->id;
                            $s->save();
                            $c->region_1_position = '';
                            if (!$c->published)
                            {
                                $c->notify(new FacebookPost());
                                $c->notify(new TwitterPost());
                                $c->published = true;
                            }
                            $c->save();
                            $n = $c;
                            break;
                        }
                        if ($c->region_2_id == $id && $c->region_2_position == $position)
                        {
                            $s = Section::findOrFail($this->id);
                            $s->small6 = $c->id;
                            $s->save();
                            $c->region_2_position = '';
                            if (!$c->published)
                            {
                                $c->notify(new FacebookPost());
                                $c->notify(new TwitterPost());
                                $c->published = true;
                            }
                            $c->save();
                            $n = $c;
                            break;
                        }
                    }

                $this->small6 = $n;
            }
        }

    }*/

    function news_online($type)
    {
        if ($type == 'section')
            return News::where('section_id', '=', $this->id)->where('online', '=', true)->count();
        else
        {
            $id = $this->id;
            return News::where('online', '=', true)->where(function ($q) use ($id) {
                $q->where('region_1_id', '=', $id)->orWhere('region_2_id', '=', $id);
            })->count();
        }
    }

    function news($type)
    {
        if ($type == 'section')
            return News::where('section_id', '=', $this->id)->count();
        else
            return News::where('region_1_id', '=', $this->id)->orWhere('region_2_id', '=', $this->id)->count();
    }
    
    public function advs()
    {
        return $this->hasMany('App\SectionAdv');
    }

    public function getAdvs()
    {

        $advs = array();

        $sections_advs = $this->advs()->where('online', '=', true)->get(); //SectionAdv::where('section_id', '=', $section->id)->orderBy('position')->get();
        foreach($sections_advs as $a)
        {
            $advs[$a->position][] = $a;
        }
        
        return $advs;

    }

    public function position($news_id)
    {

        $img = '';
        if ($this->layout != '')
        {
            if($this->big == $news_id)
                $img = $this->layout . '_big';
            /*if($this->medium1 == $news_id)
                $img = $this->layout . '_medium1';
            if($this->medium2 == $news_id)
                $img = $this->layout . '_medium2';
            if($this->medium3 == $news_id)
                $img = $this->layout . '_medium3';*/
            if($this->small1 == $news_id)
                $img = $this->layout . '_small1';
            if($this->small2 == $news_id)
                $img = $this->layout . '_small2';
            if($this->small3 == $news_id)
                $img = $this->layout . '_small3';
            if($this->small4 == $news_id)
                $img = $this->layout . '_small4';
            if($this->small5 == $news_id)
                $img = $this->layout . '_small5';
            if($this->small6 == $news_id)
                $img = $this->layout . '_small6';
            
            

        }
        
        $ret = '';
        if ($img != '')
            $ret = '<img src="/images/layout/' . $img . '.jpg" style="max-width:100px;">';
        
        return $ret;

    }

    /*
    public function clearLayout()
    {

        $id = $this->id;

        $aSel = array();
        if ($this->big)
            $aSel[] = $this->big;
        if ($this->small1)
            $aSel[] = $this->small1;
        if ($this->small2)
            $aSel[] = $this->small2;
        if ($this->small3)
            $aSel[] = $this->small3;
        if ($this->small4)
            $aSel[] = $this->small4;
        if ($this->small5)
            $aSel[] = $this->small5;
        if ($this->small6)
            $aSel[] = $this->small6;

        $news = News::whereNotIn('id', $aSel)->where(function ($q) use ($id) {
            $q->where('section_id', '=', $id)->orWhere('region_1_id', '=', $id)->orWhere('region_2_id', '=', $id);
        })->where(function ($q)  {
            $q->where('region_1_position', '<>', '')->orWhere('region_2_position', '<>', '')->orWhere('section_position', '<>', '');
        })->where('online', '=', true)->where('date', '<=', date("Y-m-d 23:59:59"))->where('breaking_news', '=', false)->orderBy('date', 'DESC')->get();
        
        $idx = 0;
        if ($this->big == '')
        {
            $this->big = @$news[$idx]->id;
            $idx++;
        }
        else
        {
            $n = News::where('id', '=', $this->big)->where('online', '=', true)->where('date', '<=', date("Y-m-d 23:59:59"))->where('breaking_news', '=', false)->first();
            if (!$n || !$n->online) 
            {
                $this->big = @$news[$idx]->id;

                $idx++;
            }
        }

        if ($this->small1 == '')
        {
            $this->small1 = @$news[$idx]->id;
            $idx++;
        }
        else
        {
            $n = News::where('id', '=', $this->small1)->where('online', '=', true)->where('date', '<=', date("Y-m-d 23:59:59"))->where('breaking_news', '=', false)->first();
            if (!$n || !$n->online)
            {
                $this->small1 = @$news[$idx]->id;
                $idx++;
            }
        }
        if ($this->small2 == '')
        {
            $this->small2 = @$news[$idx]->id;
            $idx++;
        }
        else
        {
            $n = News::where('id', '=', $this->small2)->where('online', '=', true)->where('date', '<=', date("Y-m-d 23:59:59"))->where('breaking_news', '=', false)->first();
            if (!$n || !$n->online)
            {
                $this->small2 = @$news[$idx]->id;
                $idx++;
            }
        }
        if ($this->small3 == '')
        {
            $this->small3 = @$news[$idx]->id;
            $idx++;
        }
        else
        {
            $n = News::where('id', '=', $this->small3)->where('online', '=', true)->where('date', '<=', date("Y-m-d 23:59:59"))->where('breaking_news', '=', false)->first();
            if (!$n || !$n->online)
            {
                $this->small3 = @$news[$idx]->id;
                $idx++;
            }
        }

        if ($this->layout == 'layout_3' || $this->layout == 'layout_4' || $this->layout == 'layout_5')
        {
            if ($this->small4 == '')
            {
                $this->small4 = @$news[$idx]->id;
                $idx++;
            }
            else
            {
                $n = News::where('id', '=', $this->small4)->where('online', '=', true)->where('date', '<=', date("Y-m-d 23:59:59"))->where('breaking_news', '=', false)->first();
                if (!$n || !$n->online)
                {
                    $this->small4 = @$news[$idx]->id;
                    $idx++;
                }
            }

            if ($this->layout == 'layout_4')
            {
                if ($this->small5 == '')
                {
                    $this->small5 = @$news[$idx]->id;
                    $idx++;
                }
                else
                {
                    $n = News::where('id', '=', $this->small5)->where('online', '=', true)->where('date', '<=', date("Y-m-d 23:59:59"))->where('breaking_news', '=', false)->first();
                    if (!$n || !$n->online)
                    {
                        $this->small5 = @$news[$idx]->id;
                        $idx++;
                    }
                }
                if ($this->small6 == '')
                {
                    $this->small6 = @$news[$idx]->id;
                    $idx++;
                }
                else
                {
                    $n = News::where('id', '=', $this->small6)->where('online', '=', true)->where('date', '<=', date("Y-m-d 23:59:59"))->where('breaking_news', '=', false)->first();
                    if (!$n || !$n->online)
                    {
                        $this->small6 = @$news[$idx]->id;
                        $idx++;
                    }
                }
            }
            else
            {
                $this->small5 = '';
                $this->small6 = '';
            }
        }
        else
        {
            $this->small4 = '';
            $this->small5 = '';
            $this->small6 = '';
        }
        $this->save();

        $this->generateLayout();
        // $section->save();

    }
    */

}