OSDN Git Service

element
[pettanr/pettanr.git] / app / models / panel_picture.rb
index b93c667..4c463d9 100644 (file)
-class PanelPicture < ActiveRecord::Base
+class PanelPicture < Peta::Element
+  load_manifest
+  include ElementInspire
   belongs_to :panel
-  belongs_to :resource_picture
+  belongs_to :picture
   
   validates :panel_id, :numericality => {:allow_blank => true}
-  validates :resource_picture_id, :presence => true, :numericality => true, :existence => true
-  validates :link, :length => {:maximum => 200}
+  validates :picture_id, :numericality => true, :existence => {:both => false}
+  validates :link, :length => {:maximum => 200}, :url => {:allow_blank => true, :message => I18n.t('errors.messages.url')}
   validates :x, :presence => true, :numericality => true
   validates :y, :presence => true, :numericality => true
-  validates :width, :presence => true, :numericality => true, :not_zero => true
-  validates :height, :presence => true, :numericality => true, :not_zero => true
+  validates :width, :presence => true, :numericality => true, :not_zero => true, :reverse => true, :resize => true, :sync_vh => true
+  validates :height, :presence => true, :numericality => true, :not_zero => true, :reverse => true, :resize => true, :sync_vh => true
   validates :z, :presence => true, :numericality => {:greater_than => 0}
   validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
   
+  def self.colum_structures
+    @@colum_structures ||= {
+      :width => {
+        :helper => 'panels/size_helper'
+      }, 
+      :height => {
+        :helper => 'panels/size_helper'
+      }
+    }
+  end
+  
+  def self.list_opt_for_panel
+    {
+      :panel_pictures => {
+        :picture => {:artist => {}, :license => {}}
+      }
+    }
+  end
+  
+  def self.show_opt_for_panel
+    {
+      :panel_pictures => {
+        :picture => {:artist => {}, :license => {}}
+      }
+    }
+  end
+  
+  def self.json_opt_for_panel
+    {
+      :picture => {:artist => {}, :license => {}}
+    }
+  end
+  
+  def self.has_picture?
+    true
+  end
+  
+  def visible? operators
+    return false unless super
+    self.owner_model.visible? operators
+  end
+  
+  def supply_default
+    self.x = 0
+    self.y = 0
+    if self.picture
+      self.width = self.picture.width 
+      self.height = self.picture.height 
+    end
+    if self.panel
+      self.t = self.panel.new_t 
+      self.z = self.panel.new_z 
+    end
+  end
+  
+  def overwrite  pid
+    self.panel_id = pid
+  end
+  
   def flip
-    res = (self.height < 0 ? '' : 'v') + (self.width == 0 ? '' : 'h')
+    res = (self.height > 0 ? '' : 'v') + (self.width > 0 ? '' : 'h')
     res += '/' unless res.empty?
     res
   end
   
   def filename
-    flip + self.resource_picture.filename
+    self.flip + self.picture.filename
   end
   
   def url
-    '/resource_pictures/' + filename
+    '/pictures/' + self.filename
   end
   
-  def self.default_page_size
-    25
+  def opt_div_style
+    "top:#{self.y}px; left:#{self.x}px; z-index:#{self.z}; position: absolute;"
   end
   
-  def self.max_page_size
-    100
+  def opt_img_tag spot = nil, opacity = 20
+    o = (spot and spot != self) ? "opacity: #{opacity.to_f/100}; filter:alpha(opacity=#{opacity});" : ''
+    self.tag_attributes(:img, {
+      :class => "panel-picture", :vPicture => self.id, 
+      :src => self.url, :width => self.width.abs, :height => self.height.abs, :picture_id => self.picture_id, :ext => self.picture.ext, :alt => self.caption, 
+      :style => "#{o}"
+    })
   end
   
-  def self.page prm = nil
-    page = prm.to_i
-    page = 1 if page < 1
-    page
+  def tmb_opt_img_tag
+    tw, th = PettanImager.thumbnail_size(self.width.abs, self.height.abs)
+    {:src => self.url, :width => tw, :height => th, :alt => self.caption}
   end
   
-  def self.page_size prm = self.default_page_size
-    page_size = prm.to_i
-    page_size = self.max_page_size if page_size > self.max_page_size
-    page_size = self.default_page_size if page_size < 1
-    page_size
+  def symbol_option
+    self.picture.symbol_option
   end
   
-  def self.offset cnt, prm = nil
-    offset = prm.to_i
-    offset = cnt - 1 if offset >= cnt
-    offset = cnt - offset.abs if offset < 0
-    offset = 0 if offset < 0
-    offset
+  def boost
   end
   
-  def self.list opt = {}, page = 1, page_size = self.default_page_size
-    opt.merge!(self.list_opt) unless opt[:include]
-    opt.merge!({:order => 'updated_at desc', :limit => page_size, :offset => (page -1) * page_size})
-    PanelPicture.find(:all, opt)
+  def tag_element_type
+    'panel_picture'
+  end
+  
+  def self.list_where
+    'panels.publish > 0'
+  end
+  
+  def self.list_order
+    'panel_pictures.updated_at desc'
   end
   
   def self.list_opt
-    {:include => [:panel, {:resource_picture => [:artist, :license]}]}
+    {:panel => {:author => {}}, :picture => {:artist => {}, :license => {}} }
   end
   
   def self.list_json_opt
-    {:include => {:panel => {}, :resource_picture => [:artist, :license]}}
+    {:include => {:panel => {:include => {:author => {}}}, :picture => {:include => {:artist => {}, :license => {}}} }}
+  end
+  
+  def self.show_opt
+    {:include => {:panel => {:author => {}}, :picture => {:artist => {}, :license => {}}}}
+  end
+  
+  def self.show_json_opt
+    {:include => {:panel => {:include => {:author => {}}}, :picture => {:include => {:artist => {}, :license => {}}} }}
+  end
+  
+  def store operators
+    if self.new_record?
+      self.panel.panel_pictures.build(self.attributes)
+    else
+      self.panel.panel_pictures.each do |panel_picture|
+        next unless panel_picture == self
+        attr = self.attributes
+        attr.delete 'id'
+        panel_picture.attributes = attr
+        break
+      end
+    end
+    self.panel.store({}, operators)
+  end
+  
+  def remove operators
+    self.panel.remove_element(self, operators)
+  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