<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Event extends Model
{

    protected $fillable = [
        'title',
        'text_short',
        'text',
        'image',
        'slug',
        'online',
        'big',
        'small1',
        'small2',
        'small3',
        'small4'
    ];

    function loadLayout()
    {

        $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;
            }
        }

    }

    public function advs()
    {
        return $this->hasMany('App\EventAdv');
    }
    
    public function getAdvs()
    {
        
        $advs['Sopra 234x60 1'] = array();
        $advs['Sopra 234x60 2'] = array();
        $advs['Sopra 234x60 3'] = array();
        $advs['Sopra 234x60 4'] = array();
        $advs['Sotto 234x60 1'] = array();
        $advs['Sotto 234x60 2'] = array();
        $advs['Sotto 234x60 3'] = array();
        $advs['Sotto 234x60 4'] = array();
        $advs['728x90'] = array();

        $event_advs = $this->advs()->where('online', '=', true)->get();
        foreach($event_advs as $a)
        {
            $advs[$a->position][] = $a;
        }
        return $advs;

    }

}
