OSDN Git Service

fix license picture
[pettanr/pettanr.git] / app / models / panel_picture.rb
1 class PanelPicture < Peta::Element
2   load_manifest
3   belongs_to :panel
4   belongs_to :picture
5   
6   validates :panel_id, :numericality => {:allow_blank => true}
7   validates :picture_id, :numericality => true, :existence => {:both => false}
8   validates :link, :length => {:maximum => 200}, :url => {:allow_blank => true, :message => I18n.t('errors.messages.url')}
9   validates :x, :presence => true, :numericality => true
10   validates :y, :presence => true, :numericality => true
11   validates :width, :presence => true, :numericality => true, :not_zero => true, :reverse => true, :sync_vh => true
12   validates :height, :presence => true, :numericality => true, :not_zero => true, :reverse => true, :sync_vh => true
13   validates :z, :presence => true, :numericality => {:greater_than => 0}
14   validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
15   
16   def attr_y
17     self.attributes['y']
18   end
19   
20   def self.by_author_list_includes
21     {
22       :panel => {
23         :author => {}
24       }
25     }
26   end
27   
28   def self.has_picture?
29     true
30   end
31   
32   def supply_default
33     self.x = 0
34     self.y = 0
35     if self.picture
36       self.width = self.picture.width 
37       self.height = self.picture.height 
38     end
39     if self.panel
40       self.t = self.panel.new_t 
41       self.z = self.panel.new_z 
42     end
43   end
44   
45   def overwrite  pid
46     self.panel_id = pid
47   end
48   
49   def flip
50     res = (self.height > 0 ? '' : 'v') + (self.width > 0 ? '' : 'h')
51     res = res # format of /1.png?subdir=v
52     # res += '/' unless res.empty? # format of /v/1.png
53     res
54   end
55   
56   def filename
57     self.picture.filename + '?subdir=' + self.flip
58   end
59   
60   def url
61     '/pictures/' + self.filename
62   end
63   
64   def opt_div_style
65     "top:#{self.attr_y}px; left:#{self.x}px; z-index:#{self.z}; position: absolute;"
66   end
67   
68   def opt_img_tag spot = nil, opacity = 20
69     o = (spot and spot != self) ? "opacity: #{opacity.to_f/100}; filter:alpha(opacity=#{opacity});" : ''
70     self.tag_attributes(:img, {
71       :class => "panel-picture", :vPicture => self.id, 
72       :src => self.url, :width => self.width.abs, :height => self.height.abs, :picture_id => self.picture_id, :ext => self.picture.ext, :alt => self.caption, 
73       :style => "#{o}"
74     })
75   end
76   
77   def tmb_opt_img_tag
78     tw, th = PettanImager.thumbnail_size(self.width.abs, self.height.abs)
79     {:src => self.url, :width => tw, :height => th, :alt => self.caption}
80   end
81   
82   def symbol_option
83     self.picture.symbol_option
84   end
85   
86   def self.public_list_where
87     'panels.publish > 0'
88   end
89   
90   def self.show_opt
91     {:include => {:panel => {:author => {}}, :picture => {:artist => {}, :license => {}}}}
92   end
93   
94   def scenario
95     if caption.blank?
96       ''
97     else
98       '<p>' + ERB::Util.html_escape(self.caption) + '</p>'
99     end
100   end
101   
102   def plain_scenario
103     if caption.blank?
104       ''
105     else
106       self.caption + "\n"
107     end
108   end
109   
110 end