# encoding: utf-8

class Diploma
  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::MultiParameterAttributes

  include OrbitModel::LanguageRestrict
  include OrbitModel::Status
  include OrbitTag::Taggable
  
  LANGUAGE_TYPES = [ "English", "Chinese" ]


  field :school_name, localize: true
  field :country, localize: true
  field :department, localize: true
  field :degree, localize: true
  
  field :year
  field :language
  field :keywords
  field :start_date , :type => Date
  field :end_date , :type => Date
  field :url
  field :note
  field :create_user_id, :type => BSON::ObjectId
  field :update_user_id, :type => BSON::ObjectId

  paginates_per 10
  
  # before_save :clean_checkboxs

  validates :school_name, :at_least_one => true

  before_validation :add_http
    
  validates :url, :format => /^(http|https):\/\/(([a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5})|((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))(:[0-9]{1,5})?(\/.*)?/i, :unless => Proc.new{self.url.blank?}


  def self.widget_datas
  
	where( :is_hidden => false ).desc(:is_top, :created_at)

  end

  protected

  def add_http
    unless self.url.blank? || self.url[/^http:\/\//] || self.url[/^https:\/\//]
      self.url = 'http://' + self.url
    end
  end

  def clean_checkboxs
    self.tagged_ids.delete('')
  end
  
end