OSDN Git Service

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