OSDN Git Service

temp
[pettanr/pettanr.git] / app / models / panel_picture.rb
1 class PanelPicture < Pettanr::Item
2   include Element
3   include ElementInspire
4   belongs_to :panel
5   belongs_to :picture
6   
7   validates :panel_id, :numericality => {:allow_blank => true}
8   validates :picture_id, :numericality => true, :existence => {:both => false}
9   validates :link, :length => {:maximum => 200}, :url => {:allow_blank => true, :message => I18n.t('errors.messages.url')}
10   validates :x, :presence => true, :numericality => true
11   validates :y, :presence => true, :numericality => true
12   validates :width, :presence => true, :numericality => true, :not_zero => true, :reverse => true, :resize => true, :sync_vh => true
13   validates :height, :presence => true, :numericality => true, :not_zero => true, :reverse => true, :resize => true, :sync_vh => true
14   validates :z, :presence => true, :numericality => {:greater_than => 0}
15   validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
16   
17   def self.singular
18     'PanelPicture'
19   end
20   
21   def self.plural
22     'PanelPictures'
23   end
24   
25   def self.valid_encode_columns
26     super.merge ['link', 'caption']
27   end
28   
29   def self.owner_type
30     :author
31   end
32   
33   def self.colum_structures
34     @@colum_structures ||= {
35       :width => {
36         :helper => 'panels/size_helper'
37       }, 
38       :height => {
39         :helper => 'panels/size_helper'
40       }
41     }
42   end
43   
44   def self.list_opt_for_panel
45     {
46       :panel_pictures => {
47         :picture => {:artist => {}, :license => {}}
48       }
49     }
50   end
51   
52   def self.show_opt_for_panel
53     {
54       :panel_pictures => {
55         :picture => {:artist => {}, :license => {}}
56       }
57     }
58   end
59   
60   def self.json_opt_for_panel
61     {
62       :picture => {:artist => {}, :license => {}}
63     }
64   end
65   
66   def self.has_picture?
67     true
68   end
69   
70   def visible? roles
71     if MagicNumber['run_mode'] == 0
72       return false unless guest_role_check(roles)
73     else
74       return false unless reader_role_check(roles)
75     end
76     return true if self.panel.own?(roles)
77     self.panel.visible? roles
78   end
79   
80   def supply_default
81     self.x = 0
82     self.y = 0
83     if self.picture
84       self.width = self.picture.width 
85       self.height = self.picture.height 
86     end
87     if self.panel
88       self.t = self.panel.new_t 
89       self.z = self.panel.new_z 
90     end
91   end
92   
93   def overwrite  pid
94     self.panel_id = pid
95   end
96   
97   def flip
98     res = (self.height > 0 ? '' : 'v') + (self.width > 0 ? '' : 'h')
99     res += '/' unless res.empty?
100     res
101   end
102   
103   def filename
104     self.flip + self.picture.filename
105   end
106   
107   def url
108     '/pictures/' + self.filename
109   end
110   
111   def opt_div_style
112     "top:#{self.y}px; left:#{self.x}px; z-index:#{self.z}; position: absolute;"
113   end
114   
115   def opt_img_tag spot = nil, opacity = 20
116     o = (spot and spot != self) ? "opacity: #{opacity.to_f/100}; filter:alpha(opacity=#{opacity});" : ''
117     {:id => self.tag_id(:img), :panel_id => self.tag_panel_id, :element_id => self.tag_element_id, :element_type => self.tag_element_type,
118     :class => "panel-picture", :vPicture => self.id, 
119     :src => self.url, :width => self.width.abs, :height => self.height.abs, :picture_id => self.picture_id, :ext => self.picture.ext, :alt => self.caption, 
120     :style => "#{o}"}
121   end
122   
123   def tmb_opt_img_tag
124     tw, th = PettanImager.thumbnail_size(self.width.abs, self.height.abs)
125     {:src => self.url, :width => tw, :height => th, :alt => self.caption}
126   end
127   
128   def symbol_option
129     self.picture.symbol_option
130   end
131   
132   def boost
133   end
134   
135   def tag_element_type
136     'panel_picture'
137   end
138   
139   def self.default_page_size
140     25
141   end
142   
143   def self.max_page_size
144     100
145   end
146   
147   def self.page prm = nil
148     page = prm.to_i
149     page = 1 if page < 1
150     page
151   end
152   
153   def self.page_size prm = self.default_page_size
154     page_size = prm.to_i
155     page_size = self.max_page_size if page_size > self.max_page_size
156     page_size = self.default_page_size if page_size < 1
157     page_size
158   end
159   
160   def self.list_where
161     'panels.publish > 0'
162   end
163   
164   def self.mylist_where au
165     ['panels.author_id = ?', au.id]
166   end
167   
168   def self.himlist_where au
169     ['panels.author_id = ? and panels.publish > 0', au.id]
170   end
171   
172   def self.list page = 1, page_size = self.default_page_size
173     PanelPicture.where(self.list_where()).includes(PanelPicture.list_opt).order('panel_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size)
174   end
175   
176   def self.mylist au, page = 1, page_size = Author.default_panel_picture_page_size
177     PanelPicture.where(self.mylist_where(au)).includes(PanelPicture.list_opt).order('panel_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size)
178   end
179   
180   def self.himlist au, page = 1, page_size = Author.default_panel_picture_page_size
181     PanelPicture.where(self.himlist_where(au)).includes(PanelPicture.list_opt).order('panel_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size)
182   end
183   
184   def self.list_paginate page = 1, page_size = self.default_page_size
185     Kaminari.paginate_array(Array.new(PanelPicture.where(self.list_where()).includes(PanelPicture.list_opt).count, nil)).page(page).per(page_size)
186   end
187   
188   def self.mylist_paginate au, page = 1, page_size = Author.default_panel_picture_page_size
189     Kaminari.paginate_array(Array.new(PanelPicture.where(self.mylist_where(au)).includes(PanelPicture.list_opt).count, nil)).page(page).per(page_size)
190   end
191   
192   def self.himlist_paginate au, page = 1, page_size = Author.default_panel_picture_page_size
193     Kaminari.paginate_array(Array.new(PanelPicture.where(self.himlist_where(au)).includes(PanelPicture.list_opt).count, nil)).page(page).per(page_size)
194   end
195   
196   def self.list_by_panel_where panel_id
197     ['panel_pictures.panel_id = ?', panel_id]
198   end
199   
200   def self.list_by_panel panel_id, roles, page = 1, page_size = self.default_page_size
201     self.where(self.list_by_panel_where(panel_id)).includes(self.list_opt).order('panel_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size)
202   end
203   
204   def self.list_opt
205     {:panel => {:author => {}}, :picture => {:artist => {}, :license => {}} }
206   end
207   
208   def self.list_json_opt
209     {:include => {:panel => {:include => {:author => {}}}, :picture => {:include => {:artist => {}, :license => {}}} }}
210   end
211   
212   def self.show cid, au
213     opt = {}
214     opt.merge!(PanelPicture.show_opt)
215     res = PanelPicture.find(cid, opt)
216     raise ActiveRecord::Forbidden unless res.visible?(au)
217     res
218   end
219   
220   def self.show_opt
221     {:include => {:panel => {:author => {}}, :picture => {:artist => {}, :license => {}}}}
222   end
223   
224   def self.show_json_opt
225     {:include => {:panel => {:include => {:author => {}}}, :picture => {:include => {:artist => {}, :license => {}}} }}
226   end
227   
228   def store au
229     if self.new_record?
230       self.panel.panel_pictures.build(self.attributes)
231     else
232       self.panel.panel_pictures.each do |panel_picture|
233         next unless panel_picture == self
234         attr = self.attributes
235         attr.delete 'id'
236         panel_picture.attributes = attr
237         break
238       end
239     end
240     self.panel.store({}, au)
241   end
242   
243   def remove au
244     self.panel.remove_element(self, au)
245   end
246   
247   def scenario
248     if caption.blank?
249       ''
250     else
251       '<p>' + ERB::Util.html_escape(self.caption) + '</p>'
252     end
253   end
254   
255   def plain_scenario
256     if caption.blank?
257       ''
258     else
259       self.caption + "\n"
260     end
261   end
262   
263 end