<?php

use Eae\Database\Eloquent\Collection\WebsiteObjectCollection;

class WebsiteObject extends BaseModel{
    protected $primaryKey = 'website_object_id';
    
    public function objectable(){
        return $this->morphTo(null, "object_type", "object_id");    
    } 
    
    public function related(){
        return $this->belongsToMany('WebsiteObject', "related", "website_object_id", "related_id")->withPivot('type', 'position');    
    } 

    public function parent(){
        return $this->belongsTo('WebsiteObject', 'parent_id');
    }
    
    public function children(){
        return $this->hasMany('WebsiteObject', 'parent_id');
    } 
    
    public function searches(){
        return $this->hasMany('WebsiteObjectSearch', 'website_object_id');    
    } 
    
    public function newCollection(array $models = array()){
        return new WebsiteObjectCollection($models);        
    }
    
    public static function maxPosition($objectType){
        return WebsiteObject::where("object_type", $objectType)->max("position");        
    }
    
    public function getIdAttribute($value)
    {
        return $this->getKey();
    }
    
    public static function hasSlug($slug) {
        $count = WebsiteObject::where("slug", $slug)->count();      
        return $count > 0;
    } 
}
