1 class PanelPicture < ActiveRecord::Base
5 validates :panel_id, :numericality => {:allow_blank => true}
6 validates :picture_id, :numericality => true, :existence => {:both => false}
7 validates :link, :length => {:maximum => 200}, :url => {:allow_blank => true, :message => I18n.t('errors.messages.url')}
8 validates :x, :presence => true, :numericality => true
9 validates :y, :presence => true, :numericality => true
10 validates :width, :presence => true, :numericality => true, :not_zero => true, :reverse => true, :resize => true, :sync_vh => true
11 validates :height, :presence => true, :numericality => true, :not_zero => true, :reverse => true, :resize => true, :sync_vh => true
12 validates :z, :presence => true, :numericality => {:greater_than => 0}
13 validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
15 before_validation :valid_encode
18 ['link', 'caption'].each do |a|
19 next if attributes[a] == nil
20 raise Pettanr::BadRequest unless attributes[a].valid_encoding?
25 if MagicNumber['run_mode'] == 0
26 return false unless guest_role_check(roles)
28 return false unless reader_role_check(roles)
30 return true if self.panel.own?(roles)
31 self.panel.visible? roles
35 self.x = 0 if self.x.blank?
36 self.y = 0 if self.y.blank?
37 self.width = self.picture.width if self.width.blank? and self.picture
38 self.height = self.picture.height if self.height.blank? and self.picture
39 self.t = self.new_t if self.t.blank?
40 self.z = self.new_z if self.z.blank?
47 res = (self.height > 0 ? '' : 'v') + (self.width > 0 ? '' : 'h')
48 res += '/' unless res.empty?
53 self.flip + self.picture.filename
57 '/pictures/' + self.filename
60 def opt_img_tag opacity = nil
61 o = opacity ? "opacity: #{opacity.to_f/100}; filter:alpha(opacity=#{opacity});" : ''
62 {: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}"}
66 tw, th = PettanImager.thumbnail_size(self.width.abs, self.height.abs)
67 {:src => self.url, :width => tw, :height => th, :alt => self.caption}
75 self.panel.panel_elements.size + 1
78 def self.default_page_size
82 def self.max_page_size
86 def self.page prm = nil
92 def self.page_size prm = self.default_page_size
94 page_size = self.max_page_size if page_size > self.max_page_size
95 page_size = self.default_page_size if page_size < 1
99 def self.list page = 1, page_size = self.default_page_size
101 opt.merge!(PanelPicture.list_opt)
102 opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
103 opt.merge!({:conditions => 'panels.publish > 0', :order => 'panel_pictures.updated_at desc'})
104 PanelPicture.find(:all, opt)
107 def self.mylist au, page = 1, page_size = Author.default_panel_picture_page_size
109 opt.merge!(PanelPicture.list_opt)
110 opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
111 opt.merge!({:conditions => ['panels.author_id = ?', au.id], :order => 'panel_pictures.updated_at desc'})
112 PanelPicture.find(:all, opt)
115 def self.himlist au, page = 1, page_size = Author.default_panel_picture_page_size
117 opt.merge!(self.list_opt)
118 opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
119 opt.merge!({:conditions => ['panels.author_id = ? and panels.publish > 0', au.id], :order => 'panel_pictures.updated_at desc'})
120 PanelPicture.find(:all, opt)
124 {:include => {:panel => {:author => {}}, :picture => {:artist => {}, :license => {}}}}
127 def self.list_json_opt
128 {:include => {:panel => {:include => {:author => {}}}, :picture => {:include => {:artist => {}, :license => {}}} }}
131 def self.show cid, au
133 opt.merge!(PanelPicture.show_opt)
134 res = PanelPicture.find(cid, opt)
135 raise ActiveRecord::Forbidden unless res.visible?(au)
140 {:include => {:panel => {:author => {}}, :picture => {:artist => {}, :license => {}}}}
143 def self.show_json_opt
144 {:include => {:panel => {:include => {:author => {}}}, :picture => {:include => {:artist => {}, :license => {}}} }}
149 self.panel.panel_pictures.build(self.attributes)
151 self.panel.panel_pictures.each do |panel_picture|
152 next unless panel_picture == self
153 attr = self.attributes
155 panel_picture.attributes = attr
159 self.panel.store({}, au)
163 PanelPicture.transaction do
165 panel_pictures_attributes = {}
167 self.panel.panel_pictures.each do |panel_picture|
168 attr = panel_picture.attributes
169 if panel_picture == self
170 attr['_destroy'] = true
177 panel_pictures_attributes[panel_picture.id] = attr
179 self.panel.attributes = {:panel_pictures_attributes => panel_pictures_attributes}
180 self.panel.store({}, au)
188 '<p>' + ERB::Util.html_escape(self.caption) + '</p>'