OSDN Git Service

t#31779:element lib
[pettanr/pettanr.git] / app / models / ground_picture.rb
index f9aa110..5042868 100644 (file)
@@ -1,4 +1,6 @@
 class GroundPicture < ActiveRecord::Base
+  include Element
+  include ElementInspire
   belongs_to :panel
   belongs_to :picture
   
@@ -8,26 +10,105 @@ class GroundPicture < ActiveRecord::Base
   validates :y, :numericality => true
   validates :picture_id, :numericality => true, :existence => {:both => false}
   validates :z, :presence => true, :numericality => {:greater_than => 0}
+  validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
+  
+  @@repeat_texts = ['repeat', 'repeat-x', 'repeat-y', 'no-repeat']
+  
+  before_validation :valid_encode
+  
+  def valid_encode
+    ['caption'].each do |a|
+      next if attributes[a] == nil
+      raise Pettanr::BadRequest unless attributes[a].valid_encoding?
+    end
+  end
+  
+  def self.list_opt_for_panel
+    {
+      :ground_pictures => {
+        :picture => {:artist => {}, :license => {}}
+      }
+    }
+  end
+  
+  def self.show_opt_for_panel
+    {
+      :ground_pictures => {
+        :picture => {:artist => {}, :license => {}}
+      }
+    }
+  end
+  
+  def self.json_opt_for_panel
+    {
+      :picture => {:artist => {}, :license => {}}
+    }
+  end
+  
+  def self.has_picture?
+    true
+  end
   
   def supply_default
-    self.repeat = 0
     self.x = 0
     self.y = 0
+    self.repeat = 0
+    if self.panel
+      self.t = self.panel.new_t 
+      self.z = self.panel.new_z 
+    end
   end
   
-  def overwrite
+  def overwrite pid
+    self.panel_id = pid
   end
   
-  def visible? au
-    if au == nil
-      return false if MagicNumber['run_mode'] == 1
-    elsif au.is_a?(Author)
-    elsif au.is_a?(Admin)
-      return true
+  def visible? roles
+    if MagicNumber['run_mode'] == 0
+      return false unless guest_role_check(roles)
     else
-      return false
+      return false unless reader_role_check(roles)
     end
-    self.panel.publish?
+    return true if self.panel.own?(roles)
+    self.panel.visible? roles
+  end
+  
+  def opt_div_style full_url, spot = nil, opacity = 20
+    o = (spot and spot != self) ? "opacity: #{opacity.to_f/100}; filter:alpha(opacity=#{opacity});" : ''
+    "position: absolute; width:#{self.panel.width}px; height:#{self.panel.height}px; top: 0px; left: 0px;  z-index:#{self.z}; background-image: url(#{full_url}); background-repeat: #{self.repeat_text}; background-position: #{self.x}px #{self.y}px; #{o}"
+  end
+  
+  def tmb_opt_img_tag
+    tw, th = PettanImager.thumbnail_size(self.picture.width, self.picture.height)
+    {:src => self.picture.url, :width => tw, :height => th, :alt => self.caption}
+  end
+  
+  def tag_id c = nil
+    'panel' + tag_panel_id + 'ground_picture' + tag_element_id + c.to_s
+  end
+  
+  def field_tag_id f
+    self.tag_id + f.to_s
+  end
+  
+  def tag_panel_id
+    self.panel.new_record? ? '0' : self.panel.id.to_s
+  end
+  
+  def tag_element_id
+    self.new_record? ? '0' : self.id.to_s
+  end
+  
+  def tag_element_type
+    'ground_picture'
+  end
+  
+  def field_tree f
+    'panels-' + self.tag_panel_id + '-ground_pictures_attributes-' + self.tag_element_id + '-' + f.to_s
+  end
+  
+  def repeat_text
+    @@repeat_texts[self.repeat]
   end
   
   def self.default_page_size
@@ -51,30 +132,50 @@ class GroundPicture < ActiveRecord::Base
     page_size
   end
   
+  def self.list_where
+    'panels.publish > 0'
+  end
+  
+  def self.mylist_where au
+    ['panels.author_id = ?', au.id]
+  end
+  
+  def self.himlist_where au
+    ['panels.author_id = ? and panels.publish > 0', au.id]
+  end
+  
   def self.list page = 1, page_size = self.default_page_size
-    opt = {}
-    opt.merge!(GroundPicture.list_opt)
-    opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
-    opt.merge!({:conditions => 'panels.publish > 0', :order => 'ground_pictures.updated_at desc'})
-    GroundPicture.find(:all, opt)
+    GroundPicture.where(self.list_where()).includes(GroundPicture.list_opt).order('ground_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size)
+  end
+  
+  def self.mylist au, page = 1, page_size = Author.default_ground_picture_page_size
+    GroundPicture.where(self.mylist_where(au)).includes(GroundPicture.list_opt).order('ground_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size)
+  end
+  
+  def self.himlist au, page = 1, page_size = Author.default_ground_picture_page_size
+    GroundPicture.where(self.himlist_where(au)).includes(GroundPicture.list_opt).order('ground_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size)
+  end
+  
+  def self.list_paginate page = 1, page_size = self.default_page_size
+    Kaminari.paginate_array(Array.new(GroundPicture.where(self.list_where()).includes(GroundPicture.list_opt).count, nil)).page(page).per(page_size)
+  end
+  
+  def self.mylist_paginate au, page = 1, page_size = Author.default_ground_picture_page_size
+    Kaminari.paginate_array(Array.new(GroundPicture.where(self.mylist_where(au)).includes(GroundPicture.list_opt).count, nil)).page(page).per(page_size)
+  end
+  
+  def self.himlist_paginate au, page = 1, page_size = Author.default_ground_picture_page_size
+    Kaminari.paginate_array(Array.new(GroundPicture.where(self.himlist_where(au)).includes(GroundPicture.list_opt).count, nil)).page(page).per(page_size)
   end
   
   def self.list_opt
-    {:include => {:panel => {:author => {}}, :picture => {:artist => {}, :license => {}}}}
+    {:panel => {:author => {}}, :picture => {:artist => {}, :license => {}} }
   end
   
   def self.list_json_opt
     {:include => {:panel => {:include => {:author => {}}}, :picture => {:include => {:artist => {}, :license => {}}} }}
   end
   
-  def self.mylist au, page = 1, page_size = Author.default_ground_picture_page_size
-    opt = {}
-    opt.merge!(self.list_opt)
-    opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
-    opt.merge!({:conditions => ['panels.author_id = ?', au.id], :order => 'ground_pictures.updated_at desc'})
-    GroundPicture.find(:all, opt)
-  end
-  
   def self.show cid, au
     opt = {}
     opt.merge!(GroundPicture.show_opt)
@@ -91,4 +192,39 @@ class GroundPicture < ActiveRecord::Base
     {:include => {:panel => {:include => {:author => {}}}, :picture => {:include => {:artist => {}, :license => {}}} }}
   end
   
+  def store au
+    if self.new_record?
+      self.panel.ground_pictures.build(self.attributes)
+    else
+      self.panel.ground_pictures.each do |ground_picture|
+        next unless ground_picture == self
+        attr = self.attributes
+        attr.delete 'id'
+        ground_picture.attributes = attr
+        break
+      end
+    end
+    self.panel.store({}, au)
+  end
+  
+  def remove au
+    self.panel.remove_element(self, au)
+  end
+  
+  def scenario
+    if caption.blank?
+      ''
+    else
+      '<p>' + ERB::Util.html_escape(self.caption) + '</p>'
+    end
+  end
+  
+  def plain_scenario
+    if caption.blank?
+      ''
+    else
+      self.caption + "\n"
+    end
+  end
+  
 end