OSDN Git Service

t#31486:add t and caption on ground
[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   
12   @@repeat_texts = ['repeat', 'repeat-x', 'repeat-y', 'no-repeat']
13   
14   before_validation :valid_encode
15   
16   def valid_encode
17     ['caption'].each do |a|
18       next if attributes[a] == nil
19       raise Pettanr::BadRequest unless attributes[a].valid_encoding?
20     end
21   end
22   
23   def supply_default
24     self.repeat = 0
25     self.x = 0
26     self.y = 0
27   end
28   
29   def overwrite
30   end
31   
32   def visible? roles
33     if MagicNumber['run_mode'] == 0
34       return false unless guest_role_check(roles)
35     else
36       return false unless reader_role_check(roles)
37     end
38     return true if self.panel.own?(roles)
39     self.panel.visible? roles
40   end
41   
42   def opt_div_style full_url, spot = nil, opacity = 20
43     o = (spot and spot != self) ? "opacity: #{opacity.to_f/100}; filter:alpha(opacity=#{opacity});" : ''
44     "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}"
45   end
46   
47   def tmb_opt_img_tag
48     tw, th = PettanImager.thumbnail_size(self.picture.width, self.picture.height)
49     {:src => self.picture.url, :width => tw, :height => th, :alt => self.caption}
50   end
51   
52   def repeat_text
53     @@repeat_texts[self.repeat]
54   end
55   
56   def self.default_page_size
57     25
58   end
59   
60   def self.max_page_size
61     100
62   end
63   
64   def self.page prm = nil
65     page = prm.to_i
66     page = 1 if page < 1
67     page
68   end
69   
70   def self.page_size prm = self.default_page_size
71     page_size = prm.to_i
72     page_size = self.max_page_size if page_size > self.max_page_size
73     page_size = self.default_page_size if page_size < 1
74     page_size
75   end
76   
77   def self.list_where
78     'panels.publish > 0'
79   end
80   
81   def self.mylist_where au
82     ['panels.author_id = ?', au.id]
83   end
84   
85   def self.himlist_where au
86     ['panels.author_id = ? and panels.publish > 0', au.id]
87   end
88   
89   def self.list page = 1, page_size = self.default_page_size
90     GroundPicture.where(self.list_where()).includes(GroundPicture.list_opt).order('ground_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size)
91   end
92   
93   def self.mylist au, page = 1, page_size = Author.default_ground_picture_page_size
94     GroundPicture.where(self.mylist_where(au)).includes(GroundPicture.list_opt).order('ground_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size)
95   end
96   
97   def self.himlist au, page = 1, page_size = Author.default_ground_picture_page_size
98     GroundPicture.where(self.himlist_where(au)).includes(GroundPicture.list_opt).order('ground_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size)
99   end
100   
101   def self.list_paginate page = 1, page_size = self.default_page_size
102     Kaminari.paginate_array(Array.new(GroundPicture.where(self.list_where()).includes(GroundPicture.list_opt).count, nil)).page(page).per(page_size)
103   end
104   
105   def self.mylist_paginate au, page = 1, page_size = Author.default_ground_picture_page_size
106     Kaminari.paginate_array(Array.new(GroundPicture.where(self.mylist_where(au)).includes(GroundPicture.list_opt).count, nil)).page(page).per(page_size)
107   end
108   
109   def self.himlist_paginate au, page = 1, page_size = Author.default_ground_picture_page_size
110     Kaminari.paginate_array(Array.new(GroundPicture.where(self.himlist_where(au)).includes(GroundPicture.list_opt).count, nil)).page(page).per(page_size)
111   end
112   
113   def self.list_opt
114     {:panel => {:author => {}}, :picture => {:artist => {}, :license => {}} }
115   end
116   
117   def self.list_json_opt
118     {:include => {:panel => {:include => {:author => {}}}, :picture => {:include => {:artist => {}, :license => {}}} }}
119   end
120   
121   def self.show cid, au
122     opt = {}
123     opt.merge!(GroundPicture.show_opt)
124     res = GroundPicture.find(cid, opt)
125     raise ActiveRecord::Forbidden unless res.visible?(au)
126     res
127   end
128   
129   def self.show_opt
130     {:include => {:panel => {:author => {}}, :picture => {:artist => {}, :license => {}}}}
131   end
132   
133   def self.show_json_opt
134     {:include => {:panel => {:include => {:author => {}}}, :picture => {:include => {:artist => {}, :license => {}}} }}
135   end
136   
137   def store au
138     if self.new_record?
139       self.panel.ground_pictures.build(self.attributes)
140     else
141       self.panel.ground_pictures.each do |ground_picture|
142         next unless ground_picture == self
143         attr = self.attributes
144         attr.delete 'id'
145         ground_picture.attributes = attr
146         break
147       end
148     end
149     self.panel.store({}, au)
150   end
151   
152   def remove au
153     d = false
154     ground_pictures_attributes = {}
155     self.panel.ground_pictures.each do |ground_picture|
156       attr = ground_picture.attributes
157       if ground_picture == self
158         attr['_destroy'] = true
159         d = true
160       else
161         if d
162  #         attr['t']  -= 1 
163         end
164       end
165       ground_pictures_attributes[ground_picture.id] = attr
166     end
167     self.panel.attributes = {:ground_pictures_attributes => ground_pictures_attributes}
168     self.panel.store({}, au)
169   end
170   
171   def scenario
172     ''
173   end
174   
175 end