OSDN Git Service

t#31470:create pager
[pettanr/pettanr.git] / app / models / panel_picture.rb
index a26e0a2..8607279 100644 (file)
@@ -3,7 +3,8 @@ class PanelPicture < ActiveRecord::Base
   belongs_to :picture
   
   validates :panel_id, :numericality => {:allow_blank => 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, :reverse => true, :resize => true, :sync_vh => true
@@ -11,18 +12,67 @@ class PanelPicture < ActiveRecord::Base
   validates :z, :presence => true, :numericality => {:greater_than => 0}
   validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
   
+  before_validation :valid_encode
+  
+  def valid_encode
+    ['link', 'caption'].each do |a|
+      next if attributes[a] == nil
+      raise Pettanr::BadRequest unless attributes[a].valid_encoding?
+    end
+  end
+  
+  def visible? roles
+    if MagicNumber['run_mode'] == 0
+      return false unless guest_role_check(roles)
+    else
+      return false unless reader_role_check(roles)
+    end
+    return true if self.panel.own?(roles)
+    self.panel.visible? roles
+  end
+  
+  def supply_default
+    self.x = 0 if self.x.blank?
+    self.y = 0 if self.y.blank?
+    self.width = self.picture.width if self.width.blank? and self.picture
+    self.height = self.picture.height if self.height.blank? and self.picture
+    self.t = self.new_t if self.t.blank?
+    self.z = self.new_z if self.z.blank?
+  end
+  
+  def overwrite 
+  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
-    '/pictures/' + self.picture.filename
+    '/pictures/' + self.filename
+  end
+  
+  def opt_img_tag opacity = nil
+    o = opacity ? "opacity: #{opacity.to_f/100}; filter:alpha(opacity=#{opacity});" : ''
+    {:src => self.url, :vPicture => self.id, :width => self.width.abs, :height => self.height.abs, :alt => self.caption, :style => "top:#{self.y}px; left:#{self.x}px; z-index:#{self.z}; #{o}"}
+  end
+  
+  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 new_t
+    self.panel.parts.size
+  end
+  
+  def new_z
+    self.panel.panel_elements.size + 1
   end
   
   def self.default_page_size
@@ -46,32 +96,117 @@ class PanelPicture < ActiveRecord::Base
     page_size
   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 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
+    PanelPicture.where(self.list_where()).includes(PanelPicture.list_opt).order('panel_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size)
   end
   
-  def self.list opt = {}, page = 1, page_size = self.default_page_size
-    opt.merge!(self.list_opt) unless opt[:include]
-    opt.merge!({:conditions => 'panels.publish > 0', :order => 'panel_pictures.updated_at desc', :limit => page_size, :offset => (page -1) * page_size})
-    PanelPicture.find(:all, opt)
+  def self.mylist au, page = 1, page_size = Author.default_panel_picture_page_size
+    PanelPicture.where(self.mylist_where(au)).includes(PanelPicture.list_opt).order('panel_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size)
+  end
+  
+  def self.himlist au, page = 1, page_size = Author.default_panel_picture_page_size
+    PanelPicture.where(self.himlist_where(au)).includes(PanelPicture.list_opt).order('panel_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(PanelPicture.where(self.list_where()).includes(PanelPicture.list_opt).count, nil)).page(page).per(page_size)
+  end
+  
+  def self.mylist_paginate au, page = 1, page_size = Author.default_panel_picture_page_size
+    Kaminari.paginate_array(Array.new(PanelPicture.where(self.mylist_where(au)).includes(PanelPicture.list_opt).count, nil)).page(page).per(page_size)
+  end
+  
+  def self.himlist_paginate au, page = 1, page_size = Author.default_panel_picture_page_size
+    Kaminari.paginate_array(Array.new(PanelPicture.where(self.himlist_where(au)).includes(PanelPicture.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.show cid, au
+    opt = {}
+    opt.merge!(PanelPicture.show_opt)
+    res = PanelPicture.find(cid, opt)
+    raise ActiveRecord::Forbidden unless res.visible?(au)
+    res
+  end
+  
+  def self.show_opt
     {:include => {:panel => {:author => {}}, :picture => {:artist => {}, :license => {}}}}
   end
   
-  def self.mylist au, opt = {}, page = 1, panel_picture_page_size = Author.default_panel_picture_page_size
-    opt.merge!(self.list_opt) unless opt[:include]
-    opt.merge!({:conditions => ['panels.author_id = ?', au.id], :order => 'panel_pictures.updated_at desc', :limit => page_size, :offset => (page -1) * panel_picture_page_size})
-    PanelPicture.find(:all, opt)
+  def self.show_json_opt
+    {:include => {:panel => {:include => {:author => {}}}, :picture => {:include => {:artist => {}, :license => {}}} }}
+  end
+  
+  def store au
+    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({}, au)
+  end
+  
+  def remove au
+    PanelPicture.transaction do
+      d = false
+      panel_pictures_attributes = {}
+      
+      self.panel.panel_pictures.each do |panel_picture|
+        attr = panel_picture.attributes
+        if panel_picture == self
+          attr['_destroy'] = true
+          d = true
+        else
+          if d
+            attr['t']  -= 1 
+          end
+        end
+        panel_pictures_attributes[panel_picture.id] = attr
+      end
+      self.panel.attributes = {:panel_pictures_attributes => panel_pictures_attributes}
+      self.panel.store({}, au)
+    end
+  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