OSDN Git Service

t#:
[pettanr/pettanr.git] / app / models / panel_picture.rb
1 class PanelPicture < Pettanr::Content
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 :picture_id, :numericality => true, :existence => {:both => false}
9   validates :link, :length => {:maximum => 200}, :url => {:allow_blank => true, :message => I18n.t('errors.messages.url')}
10   validates :x, :presence => true, :numericality => true
11   validates :y, :presence => true, :numericality => true
12   validates :width, :presence => true, :numericality => true, :not_zero => true, :reverse => true, :resize => true, :sync_vh => true
13   validates :height, :presence => true, :numericality => true, :not_zero => true, :reverse => true, :resize => true, :sync_vh => true
14   validates :z, :presence => true, :numericality => {:greater_than => 0}
15   validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
16   
17   def self.owner_model
18     Panel
19   end
20   
21   def self.valid_encode_columns
22     super + ['link', 'caption']
23   end
24   
25   def self.colum_structures
26     @@colum_structures ||= {
27       :width => {
28         :helper => 'panels/size_helper'
29       }, 
30       :height => {
31         :helper => 'panels/size_helper'
32       }
33     }
34   end
35   
36   def self.list_opt_for_panel
37     {
38       :panel_pictures => {
39         :picture => {:artist => {}, :license => {}}
40       }
41     }
42   end
43   
44   def self.show_opt_for_panel
45     {
46       :panel_pictures => {
47         :picture => {:artist => {}, :license => {}}
48       }
49     }
50   end
51   
52   def self.json_opt_for_panel
53     {
54       :picture => {:artist => {}, :license => {}}
55     }
56   end
57   
58   def self.has_picture?
59     true
60   end
61   
62   def visible? operators
63     return false unless super
64     self.owner_model.visible? operators
65   end
66   
67   def supply_default
68     self.x = 0
69     self.y = 0
70     if self.picture
71       self.width = self.picture.width 
72       self.height = self.picture.height 
73     end
74     if self.panel
75       self.t = self.panel.new_t 
76       self.z = self.panel.new_z 
77     end
78   end
79   
80   def overwrite  pid
81     self.panel_id = pid
82   end
83   
84   def flip
85     res = (self.height > 0 ? '' : 'v') + (self.width > 0 ? '' : 'h')
86     res += '/' unless res.empty?
87     res
88   end
89   
90   def filename
91     self.flip + self.picture.filename
92   end
93   
94   def url
95     '/pictures/' + self.filename
96   end
97   
98   def opt_div_style
99     "top:#{self.y}px; left:#{self.x}px; z-index:#{self.z}; position: absolute;"
100   end
101   
102   def opt_img_tag spot = nil, opacity = 20
103     o = (spot and spot != self) ? "opacity: #{opacity.to_f/100}; filter:alpha(opacity=#{opacity});" : ''
104     {:id => self.tag_id(:img), :panel_id => self.tag_panel_id, :element_id => self.tag_element_id, :element_type => self.tag_element_type,
105     :class => "panel-picture", :vPicture => self.id, 
106     :src => self.url, :width => self.width.abs, :height => self.height.abs, :picture_id => self.picture_id, :ext => self.picture.ext, :alt => self.caption, 
107     :style => "#{o}"}
108   end
109   
110   def tmb_opt_img_tag
111     tw, th = PettanImager.thumbnail_size(self.width.abs, self.height.abs)
112     {:src => self.url, :width => tw, :height => th, :alt => self.caption}
113   end
114   
115   def symbol_option
116     self.picture.symbol_option
117   end
118   
119   def boost
120   end
121   
122   def tag_element_type
123     'panel_picture'
124   end
125   
126   def self.list_where
127     'panels.publish > 0'
128   end
129   
130   def self.list_order
131     'panel_pictures.updated_at desc'
132   end
133   
134   def self.list_opt
135     {:panel => {:author => {}}, :picture => {:artist => {}, :license => {}} }
136   end
137   
138   def self.list_json_opt
139     {:include => {:panel => {:include => {:author => {}}}, :picture => {:include => {:artist => {}, :license => {}}} }}
140   end
141   
142   def self.show_opt
143     {:include => {:panel => {:author => {}}, :picture => {:artist => {}, :license => {}}}}
144   end
145   
146   def self.show_json_opt
147     {:include => {:panel => {:include => {:author => {}}}, :picture => {:include => {:artist => {}, :license => {}}} }}
148   end
149   
150   def store operators
151     if self.new_record?
152       self.panel.panel_pictures.build(self.attributes)
153     else
154       self.panel.panel_pictures.each do |panel_picture|
155         next unless panel_picture == self
156         attr = self.attributes
157         attr.delete 'id'
158         panel_picture.attributes = attr
159         break
160       end
161     end
162     self.panel.store({}, operators)
163   end
164   
165   def remove operators
166     self.panel.remove_element(self, operators)
167   end
168   
169   def scenario
170     if caption.blank?
171       ''
172     else
173       '<p>' + ERB::Util.html_escape(self.caption) + '</p>'
174     end
175   end
176   
177   def plain_scenario
178     if caption.blank?
179       ''
180     else
181       self.caption + "\n"
182     end
183   end
184   
185 end