OSDN Git Service

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