<?php

namespace IqContent\LaravelFilemanager\Models;

use Cviebrock\EloquentSluggable\Sluggable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;

class IQContentFolder extends Model
{

    use Sluggable;

    //use the connection to sec. Datebase sterntours
    protected $connection = 'mysql_stern';

    protected $table = 'i_q_content_folders';

    protected $fillable = [
        'folder_id', 'name', 'slug', 'identifier', 'path', 'color', 'pos', 'active'
    ];


    public function sluggable()
    {
        return [
            'slug' => [
                'source' => 'name'
            ]
        ];
    }

    public function setIdentifierAttribute( $value ) {
        if(!isset($value) || $value == ""){
            $this->attributes['identifier'] = Str::slug(pre_slug($this->name), '-');
        }else{
            $this->attributes['identifier']   = Str::slug(pre_slug($value), '-');
        }
    }

    public function folder() {
        return $this->belongsTo('IqContent\LaravelFilemanager\Models\IQContentFolder', 'folder_id');
    }

    public function folders() {
        return $this->hasMany('IqContent\LaravelFilemanager\Models\IQContentFolder', 'folder_id', 'id');
    }

    public function files() {
        return $this->hasMany('IqContent\LaravelFilemanager\Models\IQContentFile', 'folder_id', 'id');
    }
}
