module PIM::Layouts

Constants

ALL_CATEGORY_MENU_ENTRIES
CATEGORIES_OVERVIEW_JSON_MEMBERS
CATEGORIES_OVERVIEW_MEMBERS
DEFAULT_CATEGORY_MENU_ENTRIES
LAYOUT_FUNCTION_MAPPING

When adding new function mappings, please also add them to Java “DataModelServiceImpl” class!

Public Instance Methods

additional_section_attributes(layout, item) click to toggle source
# File pim.rb, line 6229
def additional_section_attributes layout, item

  result = {}

  section_attributes_blocks = []
  section_attributes_blocks << @section_attributes if @section_attributes
  parent_modules.each { |p| section_attributes_blocks << p.section_attributes if p.section_attributes }

  begin
    section_attributes_blocks.reverse.each do |section_attributes_block|
      section_attributes_block.call(layout, item, result)
    end
  rescue Exception => e
    PIM.log_error("Could not determine additional section attributes for layout #{layout}", e)
  end

  result.each_pair do |section, attributes|
    attributes.map! { |attribute| to_section_attribute(attribute) }
  end

  result
end
additional_section_attributes_as_json(layout, item) click to toggle source
# File pim.rb, line 6252
def additional_section_attributes_as_json layout, item
  as_json(additional_section_attributes(layout, item))
end
all_layouts() click to toggle source
# File pim.rb, line 6157
def all_layouts
  layouts = {}
  layouts.merge!(@module_layouts) if @module_layouts
  add_parent_objects(layouts) { |p| p.module_layouts }
  layouts
end
check_categories_overview(overview_definition) click to toggle source
# File pim.rb, line 6311
def check_categories_overview overview_definition

  ignore_undefined_categories = overview_definition[:ignore_undefined_categories] || false

  show_categories = overview_definition[:show_categories]
  if is_array?(show_categories) and not is_empty?(show_categories)

    show_categories = []
    overview_definition[:show_categories].each do |category_name|

      if category_name.is_a?(Class)

        category = category_name
        if not category <= PIM::Item
          message = "Category '#{category_name.to_s}' in categories overview is not of correct class"
          raise message if !ignore_undefined_categories
          PIM.log_debug message
          next
        end

      elsif category_name.is_a?(String)

        category = query_category(category_name)
        if !category
          # Guess the module name and search again
          category_module = category_name.split('::').first
          category = query_module_category(category_module, category_name) if category_module
        end

        if !category
          message = "Category '#{category_name}' in categories overview is not defined"
          raise message if !ignore_undefined_categories
          PIM.log_debug message
          next
        end

      else
        raise "Unknown type of category specified in categories overview"
      end

      show_categories << category.to_s

    end

  elsif show_categories == false
    show_categories = nil
  else

    show_categories = []
    data_module.all_categories(true).each_value do |category|
      show_categories << category.to_s
    end
    show_categories.sort!

  end

  overview_definition[:show_categories] = show_categories

  show_entries = overview_definition[:show_entries]
  show_search_box = overview_definition[:show_search_box]

  if not is_empty?(show_entries)

    overview_definition[:show_entries] = []
    show_entries.each do |show_entry|
      show_entry = to_sym(show_entry)
      if ALL_CATEGORY_MENU_ENTRIES.include?(show_entry) and not overview_definition[:show_entries].include?(show_entry)
        overview_definition[:show_entries] << show_entry
      end
    end

  elsif show_search_box
    overview_definition[:show_entries] = ALL_CATEGORY_MENU_ENTRIES
  else
    overview_definition[:show_entries] = DEFAULT_CATEGORY_MENU_ENTRIES
  end

end
default_layout_mapping() click to toggle source
# File pim.rb, line 6176
def default_layout_mapping
  LayoutMapping.new
end
has_layout?(name) click to toggle source
# File pim.rb, line 6164
def has_layout? name
  return false if is_empty?(name)
  name = to_sym(name)
  return true if module_layouts.has_key?(name)
  parent_modules.each { |p| return true if p.has_layout?(name) }
  return false
end
layout(name, section_definitions = nil, layout_opts = nil) click to toggle source
# File pim.rb, line 6117
def layout name, section_definitions = nil, layout_opts = nil

  name = to_sym(name)
  if section_definitions

    raise "Layout with name '#{name}' is already defined in data model #{self}" if module_layouts.has_key? name

    sections = {}
    section_definitions.each do |s|
      s_name = to_sym((s[:name] || s[:id]))
      s_label = s[:label]
      s_width = s[:width]
      s_attributes = {}
      s_preview_image_attribute = s[:preview_image_attribute]
      if is_array?(s[:attributes])
        s[:attributes].each do |attribute|
          attribute = to_section_attribute(attribute)
          attribute_name = attribute.name
          if not data_module.has_option?(:ignore_undefined_attributes_in_layouts, data_module.has_option?(:ignore_undefined_attributes))
            check_attribute!(attribute_name, "Attribute '#{attribute_name}' used in layout '#{name}', section '#{s_name}' is not defined")
          end
          s_attributes[attribute_name] = attribute
        end
      end
      section = PIM::Section.new(s_name, s_label, s_attributes, s_width, s_preview_image_attribute)
      DataModuleObject.set_data_module(section, data_module)
      sections[s_name] = section
    end

    layout = PIM::Layout.new(name, sections, layout_opts)
    module_layouts[name] = layout
    layout
  elsif name.is_a? PIM::Layout
    name
  else
    all_layouts[name]
  end

end
layouts_as_json() click to toggle source
# File pim.rb, line 6256
def layouts_as_json
  as_json(Hash[all_layouts.sort])
end
module_layouts() click to toggle source
# File pim.rb, line 6172
def module_layouts
  @module_layouts ||= {}
end
user_categories_overview(user) click to toggle source
# File pim.rb, line 6290
def user_categories_overview user

  overview_block = categories_overview
  parent_modules.each { |p| overview_block = p.categories_overview if not overview_block }

  overview_definition = CategoriesOverview.new
  if overview_block
    begin
      overview_block.call(user, overview_definition)
    rescue Exception => e
      PIM.log_error("Could not determine categories overview for user #{user.user_id}", e)
    end
  end

  # Check overview definition and fill with default values, if necessary
  check_categories_overview(overview_definition)

  overview_definition

end
user_layout_mapping(user) click to toggle source
# File pim.rb, line 6180
def user_layout_mapping user

  mapping_block = layout_mapping
  parent_modules.each { |p| mapping_block = p.layout_mapping if not mapping_block }

  mapping_definition = LayoutMapping.new
  if mapping_block
    begin
      mapping_block.call(user, mapping_definition)
    rescue Exception => e
      PIM.log_error("Could not determine layout mapping for user #{user.user_id}", e)
    end
  end

  # Set default layout, if mapped layout does not exist
  mapping_definition.each_pair do |key, value|

    # Ensure the mapping only includes String values, but use Symbol for comparison!
    mapping_definition[key] = value.to_s
    value = PIM.to_sym(value)

    if key == value and not has_layout?(value)

      default_value = LAYOUT_FUNCTION_MAPPING[key]

      # Drill down the default layouts until we find a defined layout
      checked_layouts = Set.new
      while not has_layout?(default_value) and not checked_layouts.include?(default_value)
        default_value = (mapping_definition[default_value].to_sym) || LAYOUT_FUNCTION_MAPPING[default_value]
        break if default_value.nil?
        checked_layouts << default_value
      end

      mapping_definition[key] = default_value.to_s

      if default_value.nil? or not has_layout?(default_value)
        PIM.log_warn("Layout '#{value}' does not exist and no existing default layout was found")
      end

    elsif not has_layout?(value)
      PIM.log_warn("Layout '#{value}' mapped from '#{key}' does not exist") if not has_layout?(value)
    end

  end

  mapping_definition

end

Protected Instance Methods

add_additional_section_attributes(result, section, attribute_name, category_name, &block) click to toggle source
# File pim.rb, line 6419
def add_additional_section_attributes result, section, attribute_name, category_name, &block
  return if is_empty?(category_name)

  attribute = attribute(attribute_name)
  if !attribute
    log_warn "Additional category attribute '#{attribute_name}' is not defined"
    return
  elsif attribute.type_name != 'AdditionalCategory'
    log_warn "Attribute '#{attribute_name}' is not of type 'AdditionalCategory'"
    return
  end

  additional_module_name = attribute.param(:additional_module)
  if is_empty?(additional_module_name)
    log_warn "Attribute '#{attribute.name}' does not have an 'additional_module' parameter"
    return
  end

  additional_module = PIM.get_module(additional_module_name)
  if !additional_module
    log_warn "Additional module '#{additional_module}' referenced in attribute '#{attribute.name}' is not defined"
    return
  end

  category = query_module_category(additional_module, category_name)
  if !category
    log_warn "Category '#{category_name}' specified in '#{attribute.name}' is not defined in additional module '#{additional_module_name}'"
    return
  end

  attributes = result[section] || []
  category.all_attributes.each do |attribute|
    next if is_meta_attribute?(attribute) or attributes.include?(attribute)
    if block
      next unless block.call(attribute)
    end
    attributes << attribute
  end
  result[section] = attributes

end
categories_overview(overview_definition = nil, &block) click to toggle source
# File pim.rb, line 6392
def categories_overview overview_definition = nil, &block
  return @categories_overview if overview_definition.nil? and not block
  raise "Categories overview for data model '#{self.to_s}' is already defined" if @categories_overview
  if not block
    categories_overview do |user, overview|
      CATEGORIES_OVERVIEW_MEMBERS.each do |m|
        next if not overview_definition.include?(m)
        overview[m] = overview_definition[m]
      end
    end
  else
    @categories_overview = block
  end
end
layout_mapping(&block) click to toggle source
# File pim.rb, line 6407
def layout_mapping &block
  return @layout_mapping if not block
  raise "Layout mapping block for data model '#{self.to_s}' is already defined" if @layout_mapping
  @layout_mapping = block
end
section_attribute(attribute, params = {})
section_attributes(&block) click to toggle source
# File pim.rb, line 6413
def section_attributes &block
  return @section_attributes if not block
  raise "Section attributes block for data model '#{self.to_s}' is already defined" if @section_attributes
  @section_attributes = block
end
to_section_attribute(attribute, params = {}) click to toggle source
# File pim.rb, line 6461
def to_section_attribute attribute, params = {}
  if attribute.is_a?(SectionAttribute)
    return attribute
  elsif is_hash?(attribute)
    attribute_name = to_sym(attribute[:name])
    attribute_params = {}
    attribute_params.merge!(attribute)
    return SectionAttribute.new(attribute_name, attribute_params)
  else
    attribute_name = to_sym(attribute.to_s)
    return SectionAttribute.new(attribute_name, params)
  end
end
Also aliased as: section_attribute