OSDN Git Service

t#31834:new base
[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_index
87     @new_index
88   end
89   
90   def new_index= v
91     @new_index = v
92   end
93   
94   def new_panel
95     @new_panel
96   end
97   
98   def new_panel= v
99     @new_panel = v
100   end
101   
102   def get_panel
103     self.panel || @new_panel
104   end
105   
106   def tag_id c = nil
107     'panel' + tag_panel_id + 'ground_picture' + tag_element_id + c.to_s
108   end
109   
110   def field_tag_id f
111     self.tag_id + f.to_s
112   end
113   
114   def tag_panel_id
115     self.get_panel.new_record? ? '0' : self.get_panel.id.to_s
116   end
117   
118   def tag_element_id
119     self.new_record? ? '0' : self.id.to_s
120   end
121   
122   def tag_element_type
123     'ground_picture'
124   end
125   
126   def tag_new_index
127     self.new_index.to_s
128   end
129   
130   def repeat_text
131     @@repeat_texts[self.repeat]
132   end
133   
134   def self.default_page_size
135     25
136   end
137   
138   def self.max_page_size
139     100
140   end
141   
142   def self.page prm = nil
143     page = prm.to_i
144     page = 1 if page < 1
145     page
146   end
147   
148   def self.page_size prm = self.default_page_size
149     page_size = prm.to_i
150     page_size = self.max_page_size if page_size > self.max_page_size
151     page_size = self.default_page_size if page_size < 1
152     page_size
153   end
154   
155   def self.list_where
156     'panels.publish > 0'
157   end
158   
159   def self.mylist_where au
160     ['panels.author_id = ?', au.id]
161   end
162   
163   def self.himlist_where au
164     ['panels.author_id = ? and panels.publish > 0', au.id]
165   end
166   
167   def self.list page = 1, page_size = self.default_page_size
168     GroundPicture.where(self.list_where()).includes(GroundPicture.list_opt).order('ground_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size)
169   end
170   
171   def self.mylist au, page = 1, page_size = Author.default_ground_picture_page_size
172     GroundPicture.where(self.mylist_where(au)).includes(GroundPicture.list_opt).order('ground_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size)
173   end
174   
175   def self.himlist au, page = 1, page_size = Author.default_ground_picture_page_size
176     GroundPicture.where(self.himlist_where(au)).includes(GroundPicture.list_opt).order('ground_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size)
177   end
178   
179   def self.list_paginate page = 1, page_size = self.default_page_size
180     Kaminari.paginate_array(Array.new(GroundPicture.where(self.list_where()).includes(GroundPicture.list_opt).count, nil)).page(page).per(page_size)
181   end
182   
183   def self.mylist_paginate au, page = 1, page_size = Author.default_ground_picture_page_size
184     Kaminari.paginate_array(Array.new(GroundPicture.where(self.mylist_where(au)).includes(GroundPicture.list_opt).count, nil)).page(page).per(page_size)
185   end
186   
187   def self.himlist_paginate au, page = 1, page_size = Author.default_ground_picture_page_size
188     Kaminari.paginate_array(Array.new(GroundPicture.where(self.himlist_where(au)).includes(GroundPicture.list_opt).count, nil)).page(page).per(page_size)
189   end
190   
191   def self.list_opt
192     {:panel => {:author => {}}, :picture => {:artist => {}, :license => {}} }
193   end
194   
195   def self.list_json_opt
196     {:include => {:panel => {:include => {:author => {}}}, :picture => {:include => {:artist => {}, :license => {}}} }}
197   end
198   
199   def self.show cid, au
200     opt = {}
201     opt.merge!(GroundPicture.show_opt)
202     res = GroundPicture.find(cid, opt)
203     raise ActiveRecord::Forbidden unless res.visible?(au)
204     res
205   end
206   
207   def self.show_opt
208     {:include => {:panel => {:author => {}}, :picture => {:artist => {}, :license => {}}}}
209   end
210   
211   def self.show_json_opt
212     {:include => {:panel => {:include => {:author => {}}}, :picture => {:include => {:artist => {}, :license => {}}} }}
213   end
214   
215   def store au
216     if self.new_record?
217       self.panel.ground_pictures.build(self.attributes)
218     else
219       self.panel.ground_pictures.each do |ground_picture|
220         next unless ground_picture == self
221         attr = self.attributes
222         attr.delete 'id'
223         ground_picture.attributes = attr
224         break
225       end
226     end
227     self.panel.store({}, au)
228   end
229   
230   def remove au
231     self.panel.remove_element(self, au)
232   end
233   
234   def scenario
235     if caption.blank?
236       ''
237     else
238       '<p>' + ERB::Util.html_escape(self.caption) + '</p>'
239     end
240   end
241   
242   def plain_scenario
243     if caption.blank?
244       ''
245     else
246       self.caption + "\n"
247     end
248   end
249   
250 end