OSDN Git Service

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