OSDN Git Service

t#31558:
[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
35   end
36   
37   def visible? roles
38     if MagicNumber['run_mode'] == 0
39       return false unless guest_role_check(roles)
40     else
41       return false unless reader_role_check(roles)
42     end
43     return true if self.panel.own?(roles)
44     self.panel.visible? roles
45   end
46   
47   def opt_div_style full_url, spot = nil, opacity = 20
48     o = (spot and spot != self) ? "opacity: #{opacity.to_f/100}; filter:alpha(opacity=#{opacity});" : ''
49     "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}"
50   end
51   
52   def tmb_opt_img_tag
53     tw, th = PettanImager.thumbnail_size(self.picture.width, self.picture.height)
54     {:src => self.picture.url, :width => tw, :height => th, :alt => self.caption}
55   end
56   
57   def tag_id c = nil
58     'panel' + tag_panel_id + 'ground_picture' + tag_element_id + c.to_s
59   end
60   
61   def field_tag_id f
62     self.tag_id + f.to_s
63   end
64   
65   def tag_panel_id
66     self.panel.new_record? ? '0' : self.panel.id.to_s
67   end
68   
69   def tag_element_id
70     self.new_record? ? '0' : self.id.to_s
71   end
72   
73   def tag_element_type
74     'ground_picture'
75   end
76   
77   def field_tree f
78     'panels-' + self.tag_panel_id + '-ground_pictures_attributes_-' + self.tag_element_id + '-' + f.to_s
79   end
80   
81   def repeat_text
82     @@repeat_texts[self.repeat]
83   end
84   
85   def self.default_page_size
86     25
87   end
88   
89   def self.max_page_size
90     100
91   end
92   
93   def self.page prm = nil
94     page = prm.to_i
95     page = 1 if page < 1
96     page
97   end
98   
99   def self.page_size prm = self.default_page_size
100     page_size = prm.to_i
101     page_size = self.max_page_size if page_size > self.max_page_size
102     page_size = self.default_page_size if page_size < 1
103     page_size
104   end
105   
106   def self.list_where
107     'panels.publish > 0'
108   end
109   
110   def self.mylist_where au
111     ['panels.author_id = ?', au.id]
112   end
113   
114   def self.himlist_where au
115     ['panels.author_id = ? and panels.publish > 0', au.id]
116   end
117   
118   def self.list page = 1, page_size = self.default_page_size
119     GroundPicture.where(self.list_where()).includes(GroundPicture.list_opt).order('ground_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size)
120   end
121   
122   def self.mylist au, page = 1, page_size = Author.default_ground_picture_page_size
123     GroundPicture.where(self.mylist_where(au)).includes(GroundPicture.list_opt).order('ground_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size)
124   end
125   
126   def self.himlist au, page = 1, page_size = Author.default_ground_picture_page_size
127     GroundPicture.where(self.himlist_where(au)).includes(GroundPicture.list_opt).order('ground_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size)
128   end
129   
130   def self.list_paginate page = 1, page_size = self.default_page_size
131     Kaminari.paginate_array(Array.new(GroundPicture.where(self.list_where()).includes(GroundPicture.list_opt).count, nil)).page(page).per(page_size)
132   end
133   
134   def self.mylist_paginate au, page = 1, page_size = Author.default_ground_picture_page_size
135     Kaminari.paginate_array(Array.new(GroundPicture.where(self.mylist_where(au)).includes(GroundPicture.list_opt).count, nil)).page(page).per(page_size)
136   end
137   
138   def self.himlist_paginate au, page = 1, page_size = Author.default_ground_picture_page_size
139     Kaminari.paginate_array(Array.new(GroundPicture.where(self.himlist_where(au)).includes(GroundPicture.list_opt).count, nil)).page(page).per(page_size)
140   end
141   
142   def self.list_opt
143     {:panel => {:author => {}}, :picture => {:artist => {}, :license => {}} }
144   end
145   
146   def self.list_json_opt
147     {:include => {:panel => {:include => {:author => {}}}, :picture => {:include => {:artist => {}, :license => {}}} }}
148   end
149   
150   def self.show cid, au
151     opt = {}
152     opt.merge!(GroundPicture.show_opt)
153     res = GroundPicture.find(cid, opt)
154     raise ActiveRecord::Forbidden unless res.visible?(au)
155     res
156   end
157   
158   def self.show_opt
159     {:include => {:panel => {:author => {}}, :picture => {:artist => {}, :license => {}}}}
160   end
161   
162   def self.show_json_opt
163     {:include => {:panel => {:include => {:author => {}}}, :picture => {:include => {:artist => {}, :license => {}}} }}
164   end
165   
166   def store au
167     if self.new_record?
168       self.panel.ground_pictures.build(self.attributes)
169     else
170       self.panel.ground_pictures.each do |ground_picture|
171         next unless ground_picture == self
172         attr = self.attributes
173         attr.delete 'id'
174         ground_picture.attributes = attr
175         break
176       end
177     end
178     self.panel.store({}, au)
179   end
180   
181   def remove au
182     d = false
183     ground_pictures_attributes = {}
184     self.panel.ground_pictures.each do |ground_picture|
185       attr = ground_picture.attributes
186       if ground_picture == self
187         attr['_destroy'] = true
188         d = true
189       else
190         if d
191  #         attr['t']  -= 1 
192         end
193       end
194       ground_pictures_attributes[ground_picture.id] = attr
195     end
196     self.panel.attributes = {:ground_pictures_attributes => ground_pictures_attributes}
197     self.panel.store({}, au)
198   end
199   
200   def scenario
201     ''
202   end
203   
204 end