Class: CamaleonCms::CustomFieldGroup

Inherits:
CustomField show all
Defined in:
app/models/camaleon_cms/custom_field_group.rb

Constant Summary

Constants inherited from CamaleonRecord

CamaleonRecord::TRANSLATION_TAG_HIDE_MAP, CamaleonRecord::TRANSLATION_TAG_HIDE_REGEX, CamaleonRecord::TRANSLATION_TAG_RESTORE_MAP, CamaleonRecord::TRANSLATION_TAG_RESTORE_REGEX

Instance Method Summary collapse

Methods included from Metas

#delete_meta, #delete_option, #fix_save_metas_options_no_changed, #get_meta, #get_option, #options, #save_metas_options, #save_metas_options_skip, #set_meta, #set_metas, #set_option, #set_options

Methods inherited from CamaleonRecord

#ability, #cama_build_cache_key, #cama_fetch_cache, #cama_get_cache, #cama_remove_cache, #cama_set_cache, #can?, #current_site, #current_user, #reset_ability

Instance Method Details

#add_fields(items, item_options) ⇒ Object

only used by form on admin panel (protected) return array of failed_fields and full_fields [[failed fields], [all fields]] items: hash of field items item_options: hash of options for each item



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
# File 'app/models/camaleon_cms/custom_field_group.rb', line 64

def add_fields(items, item_options)
  fields.where.not(id: items.to_h.map { |_k, obj| obj['id'] }.uniq).destroy_all
  cache_fields = []
  order_index = 0
  errors_saved = []
  if items.present?
    items.each do |i, item|
      # allow string or symbol keys for incoming params
      id_val = item['id'] || item[:id]

      item[:field_order] = order_index
      options = item_options[i] || {}
      if id_val.present? && (field_item = fields.find_by(id: id_val)).present?
        # If this is an existing select_eval field (or the incoming data would
        # make it a select_eval) ensure the current actor has explicit
        # permission. For updates we preserve the form-like behavior by
        # collecting an error-like non-persisted field in errors_saved and
        # skipping the update when unauthorized.
        existing_key = (field_item.options || {})[:field_key].to_s
        # consider field_key coming from the per-item options (options) or the item itself
        incoming_key = (options[:field_key] || item[:field_key]).to_s
        if (existing_key == 'select_eval' || incoming_key == 'select_eval') && !can?(:manage, :select_eval)
          field_item.errors.add(:base, 'Not authorized to modify select_eval field')
          errors_saved << field_item
          next
        end

        saved = field_item.update(item)
        cache_fields << field_item
      else
        # Check if the incoming options request creation of select_eval
        incoming_key = (options[:field_key] || item[:field_key]).to_s
        if incoming_key == 'select_eval' && !can?(:manage, :select_eval)
          # Add an error-like non-persisted field to errors_saved to preserve behavior
          field_item = fields.new(item)
          field_item.errors.add(:base, 'Not authorized to create select_eval field')
          errors_saved << field_item
          next
        end
        field_item = fields.new(item)
        cache_fields << field_item
        saved = field_item.save
        auto_save_default_values(field_item, options) if saved
        errors_saved << field_item unless saved
      end
      if saved
        field_item.set_meta('_default', options)
        order_index += 1
      end
    end
  end
  [errors_saved, cache_fields]
end

#add_manual_field(item, options) ⇒ Object Also known as: add_field

add fields to group item:

  • sample: “slug”=>“my_slug”, “description”=>“my description (optional)”

  • options (textbox sample):

    "translate":"1"
    
  • field_key (string) | translate (boolean) | default_value (unique value) |

    default_values (array - multiple values for this field) | multiple_options (array)
    
  • multiple_options (used for select, radio and checkboxes ): [{“title”=>“Option Title”,

    "value"=>"2", "default"=>"1"}, {"title"=>"abcde", "value"=>"3"}]
    

****** check all options for each case in Admin::CustomFieldsHelper **** SAMPLE: my_model.add_field({“name”=>“Sub Title”, “slug”=>“subtitle”}, {“field_key”=>“text_box”,

"translate"=>true, default_value: "Get in Touch"})

Adds a manual field to the group (used by admin UI) item: field attributes options: field options



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/models/camaleon_cms/custom_field_group.rb', line 36

def add_manual_field(item, options)
  # Prevent creation of dangerous field types (select_eval) unless the actor is allowed.
  if options[:field_key] == 'select_eval'
    can?(:manage, :select_eval) ||
      raise(CanCan::AccessDenied, 'Not authorized to create select_eval fields')
  end

  c = get_field(item[:slug] || item['slug'])
  return c if c.present?

  field_item = fields.new(item)
  if field_item.save
    field_item.set_options(options)
    auto_save_default_values(field_item, options)
  end
  field_item
end

#get_captionObject

generate the caption for this group



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
# File 'app/models/camaleon_cms/custom_field_group.rb', line 119

def get_caption
  caption = ''
  begin
    case object_class
    when 'PostType_Post'
      caption = "Fields for Contents in <b>#{site.post_types.find(objectid).decorate.the_title}</b>"
    when 'PostType_Category'
      caption = "Fields for Categories in <b>#{site.post_types.find(objectid).decorate.the_title}</b>"
    when 'PostType_PostTag'
      caption = "Fields for Post tags in <b>#{site.post_types.find(objectid).decorate.the_title}</b>"
    when 'Widget::Main'
      caption = "Fields for Widget <b>(#{CamaleonCms::Widget::Main.find(objectid).name.translate})</b>"
    when 'Theme'
      caption = "Field settings for Theme <b>(#{begin
        site.themes.find(objectid).name
      rescue StandardError
        objectid
      end})</b>"
    when 'NavMenu'
      caption = "Field settings for Menus <b>(#{CamaleonCms::NavMenu.find(objectid).name})</b>"
    when 'Site'
      caption = 'Field settings the site'
    when 'PostType'
      caption = 'Fields for all <b>Post_Types</b>'
    when 'Post'
      p = CamaleonCms::Post.find(objectid).decorate
      caption = "Fields for content <b>(#{p.the_title})</b>"
    else # 'Plugin' or other class
      caption = "Fields for <b>#{object_class}</b>"
    end
  rescue StandardError => e
    Rails.logger.debug "Camaleon CMS - Menu Item Error: #{e.message} ==> Attrs: #{attributes}"
  end
  caption
end

#get_field(slug) ⇒ Object

return a field with slug = slug from current group



56
57
58
# File 'app/models/camaleon_cms/custom_field_group.rb', line 56

def get_field(slug)
  fields.find_by(slug: slug)
end