OSDN Git Service

3562d32d6ccb3ccf03d44bee41645a4fc936832f
[pettanr/pettanr.git] / app / models / ground_picture.rb
1 class GroundPicture < ActiveRecord::Base
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 :repeat, :numericality => true, :inclusion => { :in => 0..3 }
9   validates :x, :numericality => true
10   validates :y, :numericality => true
11   validates :picture_id, :numericality => true, :existence => {:both => false}
12   validates :z, :presence => true, :numericality => {:greater_than => 0}
13   validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
14   
15   @@repeat_texts = ['repeat', 'repeat-x', 'repeat-y', 'no-repeat']
16   
17   before_validation :valid_encode
18   
19   def valid_encode
20     ['caption'].each do |a|
21       next if attributes[a] == nil
22       raise Pettanr::BadRequest unless attributes[a].valid_encoding?
23     end
24   end
25   
26   def self.list_opt_for_panel
27     {
28       :ground_pictures => {
29         :picture => {:artist => {}, :license => {}}
30       }
31     }
32   end
33   
34   def self.show_opt_for_panel
35     {
36       :ground_pictures => {
37         :picture => {:artist => {}, :license => {}}
38       }
39     }
40   end
41   
42   def self.json_opt_for_panel
43     {
44       :picture => {:artist => {}, :license => {}}
45     }
46   end
47   
48   def self.has_picture?
49     true
50   end
51   
52   def supply_default
53     self.x = 0
54     self.y = 0
55     self.repeat = 0
56     if self.panel
57       self.t = self.panel.new_t 
58       self.z = self.panel.new_z 
59     end
60   end
61   
62   def overwrite pid
63     self.panel_id = pid
64   end
65   
66   def visible? roles
67     if MagicNumber['run_mode'] == 0
68       return false unless guest_role_check(roles)
69     else
70       return false unless reader_role_check(roles)
71     end
72     return true if self.panel.own?(roles)
73     self.panel.visible? roles
74   end
75   
76   def opt_div_style full_url, spot = nil, opacity = 20
77     o = (spot and spot != self) ? "opacity: #{opacity.to_f/100}; filter:alpha(opacity=#{opacity});" : ''
78     "position: absolute; width:#{self.get_panel.width}px; height:#{self.get_panel.height}px; top: 0px; left: 0px;  z-index:#{self.z}; background-image: url(#{full_url}); background-repeat: #{self.repeat_text}; background-position: #{self.x}px #{self.y}px; #{o}"
79   end
80   
81   def tmb_opt_img_tag
82     tw, th = PettanImager.thumbnail_size(self.picture.width, self.picture.height)
83     {:src => self.picture.url, :width => tw, :height => th, :alt => self.caption}
84   end
85   
86   def new_panel
87     @new_panel
88   end
89   
90   def new_panel= v
91     @new_panel = v
92   end
93   
94   def get_panel
95     self.panel || @new_panel
96   end
97   
98   def tag_id c = nil
99     'panel' + tag_panel_id + 'ground_picture' + tag_element_id + c.to_s
100   end
101   
102   def field_tag_id f
103     self.tag_id + f.to_s
104   end
105   
106   def tag_panel_id
107     self.get_panel.new_record? ? '0' : self.get_panel.id.to_s
108   end
109   
110   def tag_element_id
111     self.new_record? ? '0' : self.id.to_s
112   end
113   
114   def tag_element_type
115     'ground_picture'
116   end
117   
118   def field_tree f
119     'panels-' + self.tag_panel_id + '-ground_pictures_attributes-' + self.tag_element_id + '-' + f.to_s
120   end
121   
122   def repeat_text
123     @@repeat_texts[self.repeat]
124   end
125   
126   def self.default_page_size
127     25
128   end
129   
130   def self.max_page_size
131     100
132   end
133   
134   def self.page prm = nil
135     page = prm.to_i
136     page = 1 if page < 1
137     page
138   end
139   
140   def self.page_size prm = self.default_page_size
141     page_size = prm.to_i
142     page_size = self.max_page_size if page_size > self.max_page_size
143     page_size = self.default_page_size if page_size < 1
144     page_size
145   end
146   
147   def self.list_where
148     'panels.publish > 0'
149   end
150   
151   def self.mylist_where au
152     ['panels.author_id = ?', au.id]
153   end
154   
155   def self.himlist_where au
156     ['panels.author_id = ? and panels.publish > 0', au.id]
157   end
158   
159   def self.list page = 1, page_size = self.default_page_size
160     GroundPicture.where(self.list_where()).includes(GroundPicture.list_opt).order('ground_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size)
161   end
162   
163   def self.mylist au, page = 1, page_size = Author.default_ground_picture_page_size
164     GroundPicture.where(self.mylist_where(au)).includes(GroundPicture.list_opt).order('ground_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size)
165   end
166   
167   def self.himlist au, page = 1, page_size = Author.default_ground_picture_page_size
168     GroundPicture.where(self.himlist_where(au)).includes(GroundPicture.list_opt).order('ground_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size)
169   end
170   
171   def self.list_paginate page = 1, page_size = self.default_page_size
172     Kaminari.paginate_array(Array.new(GroundPicture.where(self.list_where()).includes(GroundPicture.list_opt).count, nil)).page(page).per(page_size)
173   end
174   
175   def self.mylist_paginate au, page = 1, page_size = Author.default_ground_picture_page_size
176     Kaminari.paginate_array(Array.new(GroundPicture.where(self.mylist_where(au)).includes(GroundPicture.list_opt).count, nil)).page(page).per(page_size)
177   end
178   
179   def self.himlist_paginate au, page = 1, page_size = Author.default_ground_picture_page_size
180     Kaminari.paginate_array(Array.new(GroundPicture.where(self.himlist_where(au)).includes(GroundPicture.list_opt).count, nil)).page(page).per(page_size)
181   end
182   
183   def self.list_opt
184     {:panel => {:author => {}}, :picture => {:artist => {}, :license => {}} }
185   end
186   
187   def self.list_json_opt
188     {:include => {:panel => {:include => {:author => {}}}, :picture => {:include => {:artist => {}, :license => {}}} }}
189   end
190   
191   def self.show cid, au
192     opt = {}
193     opt.merge!(GroundPicture.show_opt)
194     res = GroundPicture.find(cid, opt)
195     raise ActiveRecord::Forbidden unless res.visible?(au)
196     res
197   end
198   
199   def self.show_opt
200     {:include => {:panel => {:author => {}}, :picture => {:artist => {}, :license => {}}}}
201   end
202   
203   def self.show_json_opt
204     {:include => {:panel => {:include => {:author => {}}}, :picture => {:include => {:artist => {}, :license => {}}} }}
205   end
206   
207   def store au
208     if self.new_record?
209       self.panel.ground_pictures.build(self.attributes)
210     else
211       self.panel.ground_pictures.each do |ground_picture|
212         next unless ground_picture == self
213         attr = self.attributes
214         attr.delete 'id'
215         ground_picture.attributes = attr
216         break
217       end
218     end
219     self.panel.store({}, au)
220   end
221   
222   def remove au
223     self.panel.remove_element(self, au)
224   end
225   
226   def scenario
227     if caption.blank?
228       ''
229     else
230       '<p>' + ERB::Util.html_escape(self.caption) + '</p>'
231     end
232   end
233   
234   def plain_scenario
235     if caption.blank?
236       ''
237     else
238       self.caption + "\n"
239     end
240   end
241   
242 end