OSDN Git Service

t#31485:editate ground_colors
[pettanr/pettanr.git] / app / models / panel_picture.rb
1 class PanelPicture < ActiveRecord::Base
2   belongs_to :panel
3   belongs_to :picture
4   
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}
14   
15   before_validation :valid_encode
16   
17   def valid_encode
18     ['link', 'caption'].each do |a|
19       next if attributes[a] == nil
20       raise Pettanr::BadRequest unless attributes[a].valid_encoding?
21     end
22   end
23   
24   def visible? roles
25     if MagicNumber['run_mode'] == 0
26       return false unless guest_role_check(roles)
27     else
28       return false unless reader_role_check(roles)
29     end
30     return true if self.panel.own?(roles)
31     self.panel.visible? roles
32   end
33   
34   def supply_default
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?
41   end
42   
43   def overwrite 
44   end
45   
46   def flip
47     res = (self.height > 0 ? '' : 'v') + (self.width > 0 ? '' : 'h')
48     res += '/' unless res.empty?
49     res
50   end
51   
52   def filename
53     self.flip + self.picture.filename
54   end
55   
56   def url
57     '/pictures/' + self.filename
58   end
59   
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}"}
63   end
64   
65   def tmb_opt_img_tag
66     tw, th = PettanImager.thumbnail_size(self.width.abs, self.height.abs)
67     {:src => self.url, :width => tw, :height => th, :alt => self.caption}
68   end
69   
70   def new_t
71     self.panel.parts.size
72   end
73   
74   def new_z
75     self.panel.panel_elements.size + 1
76   end
77   
78   def self.default_page_size
79     25
80   end
81   
82   def self.max_page_size
83     100
84   end
85   
86   def self.page prm = nil
87     page = prm.to_i
88     page = 1 if page < 1
89     page
90   end
91   
92   def self.page_size prm = self.default_page_size
93     page_size = prm.to_i
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
96     page_size
97   end
98   
99   def self.list page = 1, page_size = self.default_page_size
100     opt = {}
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)
105   end
106   
107   def self.mylist au, page = 1, page_size = Author.default_panel_picture_page_size
108     opt = {}
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)
113   end
114   
115   def self.himlist au, page = 1, page_size = Author.default_panel_picture_page_size
116     opt = {}
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)
121   end
122   
123   def self.list_opt
124     {:include => {:panel => {:author => {}}, :picture => {:artist => {}, :license => {}}}}
125   end
126   
127   def self.list_json_opt
128     {:include => {:panel => {:include => {:author => {}}}, :picture => {:include => {:artist => {}, :license => {}}} }}
129   end
130   
131   def self.show cid, au
132     opt = {}
133     opt.merge!(PanelPicture.show_opt)
134     res = PanelPicture.find(cid, opt)
135     raise ActiveRecord::Forbidden unless res.visible?(au)
136     res
137   end
138   
139   def self.show_opt
140     {:include => {:panel => {:author => {}}, :picture => {:artist => {}, :license => {}}}}
141   end
142   
143   def self.show_json_opt
144     {:include => {:panel => {:include => {:author => {}}}, :picture => {:include => {:artist => {}, :license => {}}} }}
145   end
146   
147   def store au
148     if self.new_record?
149       self.panel.panel_pictures.build(self.attributes)
150     else
151       self.panel.panel_pictures.each do |panel_picture|
152         next unless panel_picture == self
153         attr = self.attributes
154         attr.delete 'id'
155         panel_picture.attributes = attr
156         break
157       end
158     end
159     self.panel.store({}, au)
160   end
161   
162   def remove au
163     PanelPicture.transaction do
164       d = false
165       panel_pictures_attributes = {}
166       
167       self.panel.panel_pictures.each do |panel_picture|
168         attr = panel_picture.attributes
169         if panel_picture == self
170           attr['_destroy'] = true
171           d = true
172         else
173           if d
174             attr['t']  -= 1 
175           end
176         end
177         panel_pictures_attributes[panel_picture.id] = attr
178       end
179       self.panel.attributes = {:panel_pictures_attributes => panel_pictures_attributes}
180       self.panel.store({}, au)
181     end
182   end
183   
184   def scenario
185     if caption.blank?
186       ''
187     else
188       '<p>' + ERB::Util.html_escape(self.caption) + '</p>'
189     end
190   end
191   
192   def plain_scenario
193     if caption.blank?
194       ''
195     else
196       self.caption + "\n"
197     end
198   end
199   
200 end