cirandas.net

ref: master

app/models/block.rb


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
class Block < ApplicationRecord

  attr_accessible :title, :subtitle, :display, :limit, :box_id, :posts_per_page,
                  :visualization_format, :language, :display_user, :position,
                  :box, :edit_modes, :move_modes, :mirror, :visualization, :images_builder, :api_content

  include ActionView::Helpers::TagHelper

  # Block-specific stuff
  include BlockHelper

  delegate :environment, :to => :box, :allow_nil => true

  acts_as_list scope: -> block { where box_id: block.box_id }

  belongs_to :box
  belongs_to :mirror_block, :class_name => "Block"
  has_many :observers, :class_name => "Block", :foreign_key => "mirror_block_id"
  has_many :images, foreign_key: "owner_id"

  extend ActsAsHavingSettings::ClassMethods
  acts_as_having_settings

  settings_items :visualization, :type => Hash, :default => {}

  store_accessor :metadata
  include MetadataScopes

  scope :enabled, -> { where :enabled => true }

  after_save do |block|
    if block.owner.kind_of?(Profile) && block.owner.is_template? && block.mirror?
      block.observers.each do |observer|
        observer.copy_from(block)
        observer.title = block.title
        observer.save
      end
    end
  end

  def embedable?
    false
  end

  def get_limit
    [0,limit.to_i].max
  end

  def embed_code
    me = self
    proc do
      content_tag('iframe', '',
        :src => url_for(:controller => 'embed', :action => 'block', :id => me.id, :only_path => false),
        :frameborder => 0,
        :width => 1024,
        :height => 768,
        :class => "embed block #{me.class.name.to_css_class}"
      )
    end
  end

  # Determines whether a given block must be visible. Optionally a
  # <tt>context</tt> must be specified. <tt>context</tt> must be a hash, and
  # may contain the following keys:
  #
  # * <tt>:article</tt>: the article being viewed currently
  # * <tt>:language</tt>: in which language the block will be displayed
  # * <tt>:user</tt>: the logged user
  def visible?(context = nil)
    return false if display == 'never'

    if context
      return false if language != 'all' && language != context[:locale]
      return false unless display_to_user?(context[:user])

      begin
        return self.send("display_#{display}", context)
      rescue NoMethodError => exception
        raise "Display '#{display}' is not a valid value."
      end
    end

    true
  end

  def visible_to_user?(user)
    visible = self.display_to_user?(user)
    if self.owner.kind_of?(Profile)
      visible &= self.owner.display_info_to?(user)
      visible &= (self.visible? || user && user.has_permission?(:edit_profile_design, self.owner))
    elsif self.owner.kind_of?(Environment)
      visible &= (self.visible? || user && user.has_permission?(:edit_environment_design, self.owner))
    end
    visible
  end

  def display_to_user?(user)
    display_user == 'all' || (user.nil? && display_user == 'not_logged') || (user && display_user == 'logged') || (user && display_user == 'followers' && owner.in_social_circle?(user))
  end

  def display_always(context)
    true
  end

  def display_home_page_only(context)
    if context[:article]
      return context[:article] == owner.home_page
    else
      return home_page_path?(context[:request_path])
    end
  end

  def display_except_home_page(context)
    if context[:article]
      return context[:article] != owner.home_page
    else
      return !home_page_path?(context[:request_path])
    end
  end

  # The condition for displaying a block. It can assume the following values:
  #
  # * <tt>'always'</tt>: the block is always displayed
  # * <tt>'never'</tt>: the block is hidden (it does not appear for visitors)
  # * <tt>'home_page_only'</tt> the block is displayed only when viewing the
  #   homepage of its owner.
  # * <tt>'except_home_page'</tt> the block is displayed only when viewing
  #   the homepage of its owner.
  settings_items :display, :type => :string, :default => 'always'


  # The condition for displaying a block to users. It can assume the following values:
  #
  # * <tt>'all'</tt>: the block is always displayed
  # * <tt>'logged'</tt>: the block is displayed to logged users only
  # * <tt>'not_logged'</tt>: the block is displayed only to not logged users
  settings_items :display_user, :type => :string, :default => 'all'

  # The block can be configured to be displayed in all languages or in just one language. It can assume any locale of the environment:
  #
  # * <tt>'all'</tt>: the block is always displayed
  settings_items :language, :type => :string, :default => 'all'

  # The block can be configured to define the edition modes options. Only can be edited by environment admins
  # It can assume the following values:
  #
  # * <tt>'all'</tt>: the block owner has all edit options for this block
  # * <tt>'none'</tt>: the block owner can't do anything with the block
  settings_items :edit_modes, :type => :string, :default => 'all'
  settings_items :move_modes, :type => :string, :default => 'all'

  # returns the description of the block, used when the user sees a list of
  # blocks to choose one to include in the design.
  #
  # Must be redefined in subclasses to match the description of each block
  # type.
  def self.description
    '(dummy)'
  end

  def self.short_description
    self.pretty_name
  end

  def self.icon
    "/images/icon_block.png"
  end

  def self.icon_path
    basename = self.name.split('::').last.underscore
    File.join('images', 'blocks', basename, 'icon.png')
  end

  def self.pretty_name
    self.name.split('::').last.gsub('Block','')
  end

  def self.default_icon_path
    '/images/icon_block.png'
  end

  def self.preview_path
    base_name = self.name.split('::').last.underscore
    File.join('blocks', base_name,'previews')
  end

  def self.default_preview_path
    "/images/block_preview.png"
  end

  # Is this block editable? (Default to <tt>true</tt>)
  def editable?(user=nil)
    self.edit_modes == "all"
  end

  def movable?
    self.move_modes == "all"
  end

  # must always return false, except on MainBlock clas.
  def main?
    false
  end

  def owner
    box ? box.owner : nil
  end

  def default_title
    ''
  end

  def title
    if self[:title].blank?
      self.default_title
    else
      self[:title]
    end
  end

  def view_title
    title
  end

  def cacheable?
    true
  end

  alias :active_record_cache_key :cache_key
  def cache_key(language='en', user=nil)
    active_record_cache_key + '-' + language
  end

  def timeout
    4.hours
  end

  def has_macro?
    false
  end

  # Override in your subclasses.
  # Define which events and context should cause the block cache to expire
  # Possible events are: :article, :profile, :friendship, :category, :role_assignment
  # Possible contexts are: :profile, :environment
  def self.expire_on
    {
      :profile => [],
      :environment => []
    }
  end

  DISPLAY_OPTIONS = {
    'always'           => _('In all pages'),
    'home_page_only'   => _('Only in the homepage'),
    'except_home_page' => _('In all pages, except in the homepage'),
    'never'            => _('Don\'t display'),
  }

  def display_options_available
    DISPLAY_OPTIONS.keys
  end

  def display_options
    DISPLAY_OPTIONS.slice(*display_options_available)
  end

  def display_user_options
    @display_user_options ||= {
      'all'            => _('All users'),
      'logged'         => _('Logged'),
      'not_logged'     => _('Not logged'),
      'followers'      => owner.class != Environment && owner.organization? ? _('Members') : _('Friends')
    }
  end

  def edit_block_options
    @edit_options ||= {
      'all'            => _('Can be modified'),
      'none'           => _('Cannot be modified')
    }
  end

  def move_block_options
    @move_options ||= {
      'all'            => _('Can be moved'),
      'none'           => _('Cannot be moved')
    }
  end


  def duplicate
    duplicated_block = self.dup
    duplicated_block.display = 'never'
    duplicated_block.created_at = nil
    duplicated_block.updated_at = nil
    duplicated_block.save!
    duplicated_block.insert_at(self.position + 1)
    duplicated_block
  end

  def copy_from(block)
    self.settings = block.settings
    self.position = block.position
  end

  def add_observer(block)
    self.observers << block
  end

  def api_content(options = {})
    nil
  end

  def api_content=(values = {})
  end

  def display_api_content_by_default?
    false
  end

  def allow_edit?(person)
    return false if person.nil? || (!person.is_admin? && !editable?(person))
    if self.owner.kind_of?(Profile)
      return person.has_permission?(:edit_profile_design, owner)
    elsif self.owner.kind_of?(Environment)
      return person.has_permission?(:edit_environment_design, owner)
    end
    false
  end

  def images_builder=(raw_images)
    raw_images.each do |img|
      if img[:remove_image] == true || img[:remove_image] == 'true'
        images.find_by(id: img[:id]).destroy!
      elsif !img[:uploaded_data].blank?
        images.build(img)
      end
    end
  end

  private

  def home_page_path
    home_page_url = Noosfero.root('/')

    if owner.kind_of?(Profile)
      home_page_url += "profile/" if owner.home_page.nil?
      home_page_url += owner.identifier
    end

    return home_page_url
  end

  def home_page_path? path
    return path == home_page_path || path == (home_page_path + '/')
  end

end