Class: Caboose::EventCustomFieldsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/caboose/event_custom_fields_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#add_ga_event, #admin_bulk_add, #admin_bulk_delete, #admin_bulk_update, #before_action, #before_before_action, #hashify_query_string, #init_cart, #logged_in?, #logged_in_user, #login_user, #logout_user, #parse_url_params, #reject_param, #under_construction_or_forwarding_domain?, #user_is_allowed, #user_is_allowed_to, #validate_cookie, #validate_token, #var, #verify_logged_in

Instance Method Details

#admin_addObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/controllers/caboose/event_custom_fields_controller.rb', line 73

def admin_add
  return if !user_is_allowed('eventcustomfields', 'add')
  
  resp = Caboose::StdClass.new

  f = EventCustomField.new      
  f.name = params[:key]            

  if f.name.nil? || f.name.length == 0
    resp.error = 'A field key is required.'      
  else
    f.site_id = @site.id
    f.key = f.name.gsub(' ', '_').gsub('-', '_').downcase
    f.field_type = EventCustomField::FIELD_TYPE_TEXT
    f.save
    resp.redirect = "/admin/event-custom-fields/#{f.id}"
  end
  
  render :json => resp
end

#admin_deleteObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/controllers/caboose/event_custom_fields_controller.rb', line 95

def admin_delete
  return if !user_is_allowed('eventcustomfields', 'edit')
  if params[:id] == 'bulk'      
    params[:model_ids].each do |fid|
      EventCustomFieldValue.where(:event_custom_field_id => fid).destroy_all
      EventCustomField.where(:id => fid).destroy_all                                    
    end
  else
    fid = params[:id]
    EventCustomFieldValue.where(:event_custom_field_id => fid).destroy_all
    EventCustomField.where(:id => fid).destroy_all        
  end
  
  render :json => { 'redirect' => '/admin/event-custom-fields' }      
end

#admin_editObject



44
45
46
47
48
# File 'app/controllers/caboose/event_custom_fields_controller.rb', line 44

def admin_edit
  return if !user_is_allowed('eventcustomfields', 'edit')    
  @event_custom_field = EventCustomField.find(params[:id])      
  render :layout => 'caboose/admin'
end

#admin_field_optionsObject



112
113
114
115
116
117
118
119
120
121
122
# File 'app/controllers/caboose/event_custom_fields_controller.rb', line 112

def admin_field_options
  options = []
  pcf = EventCustomField.where(:site_id => @site.id, :id => params[:pcfid]).first
  if pcf && !pcf.options.blank?
    pcf.options.split(/\n/).each do |f|
      opt = {'text' => f, 'value' => f}
      options << opt
    end
  end
  render :json => options
end

#admin_indexObject



7
8
9
10
# File 'app/controllers/caboose/event_custom_fields_controller.rb', line 7

def admin_index
  return if !user_is_allowed_to 'view', 'eventcustomfields'              
  render :layout => 'caboose/admin'    
end

#admin_jsonObject



13
14
15
16
17
18
19
20
# File 'app/controllers/caboose/event_custom_fields_controller.rb', line 13

def admin_json
  return if !user_is_allowed_to 'view', 'eventcustomfields'
  pager = self.fields_pager        
  render :json => {
    :pager => pager,
    :models => pager.items
  }      
end

#admin_json_singleObject



37
38
39
40
41
# File 'app/controllers/caboose/event_custom_fields_controller.rb', line 37

def admin_json_single
  return if !user_is_allowed_to 'view', 'eventcustomfields'
  f = EventCustomField.find(params[:id])      
  render :json => f
end

#admin_optionsObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'app/controllers/caboose/event_custom_fields_controller.rb', line 126

def admin_options
   return if !user_is_allowed_to 'view', 'eventcustomfields'  
  options = []
  case params[:field]
    when nil
      arr = EventCustomField.where(:site_id => @site.id).reorder(:key).all
      options = arr.collect{ |a| { 'value' => a.id, 'text' => a.name }} 	    
    when 'field-type'
      options = [
        { 'value' => EventCustomField::FIELD_TYPE_TEXT     , 'text' => 'Text'     },
         { 'value' => EventCustomField::FIELD_TYPE_SELECT   , 'text' => 'Select'   },
         { 'value' => EventCustomField::FIELD_TYPE_CHECKBOX , 'text' => 'Checkbox' },
         { 'value' => EventCustomField::FIELD_TYPE_DATE     , 'text' => 'Date'     },
         { 'value' => EventCustomField::FIELD_TYPE_DATETIME , 'text' => 'Datetime' }          
       ]                            
   end        
  render :json => options
end

#admin_updateObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/controllers/caboose/event_custom_fields_controller.rb', line 51

def admin_update      
  return if !user_is_allowed('eventcustomfields', 'edit')
  
  resp = Caboose::StdClass.new
  f = EventCustomField.find(params[:id])
  
  save = true
  params.each do |name, value|    
    case name          
      when 'key'           then f.key            = value
      when 'name'          then f.name           = value
      when 'field_type'    then f.field_type     = value
      when 'default_value' then f.default_value  = value
      when 'options'       then f.options        = value
      when 'options_url'   then f.options_url    = value             
    end
  end
  resp.success = save && f.save      
  render :json => resp
end

#fields_pagerObject



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/controllers/caboose/event_custom_fields_controller.rb', line 22

def fields_pager
  return Caboose::Pager.new(params, {
    'site_id'     => @site.id,
    'key_like'    => '',
    'name_like'   => ''
  }, {
    'model' => 'Caboose::EventCustomField',
    'sort'  => 'key',
    'desc'  => 'false',
    'items_per_page' => 100,
    'base_url' => '/admin/event-custom-fields'      
  })
end