Class: Caboose::PageCustomFieldsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/caboose/page_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


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

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

  f = PageCustomField.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 = PageCustomField::FIELD_TYPE_TEXT
    f.save
    resp.redirect = "/admin/page-custom-fields/#{f.id}"
  end
  
  render :json => resp
end

#admin_deleteObject


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

def admin_delete
  return if !user_is_allowed('pagecustomfields', 'edit')

  if params[:id] == 'bulk'      
    params[:model_ids].each do |fid|
      PageCustomFieldValue.where(:page_custom_field_id => fid).destroy_all
      PageCustomField.where(:id => fid).destroy_all                                    
    end
  else
    fid = params[:id]
    PageCustomFieldValue.where(:page_custom_field_id => fid).destroy_all
    PageCustomField.where(:id => fid).destroy_all        
  end
  
  render :json => { 'redirect' => '/admin/page-custom-fields' }      
end

#admin_editObject


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

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

#admin_indexObject


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

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

#admin_jsonObject


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

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

#admin_json_singleObject


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

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

#admin_optionsObject


113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'app/controllers/caboose/page_custom_fields_controller.rb', line 113

def admin_options
   return if !user_is_allowed_to 'view', 'pagecustomfields'  
  options = []
  case params[:field]
    when nil
      arr = PageCustomField.where(:site_id => @site.id).reorder(:key).all
      options = arr.collect{ |a| { 'value' => a.id, 'text' => a.name }} 	    
    when 'field-type'
      options = [
        { 'value' => PageCustomField::FIELD_TYPE_TEXT     , 'text' => 'Text'     },
         { 'value' => PageCustomField::FIELD_TYPE_SELECT   , 'text' => 'Select'   },
         { 'value' => PageCustomField::FIELD_TYPE_CHECKBOX , 'text' => 'Checkbox' },
         { 'value' => PageCustomField::FIELD_TYPE_DATE     , 'text' => 'Date'     },
         { 'value' => PageCustomField::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
# File 'app/controllers/caboose/page_custom_fields_controller.rb', line 51

def admin_update      
  return if !user_is_allowed('pagecustomfields', 'edit')
  
  resp = Caboose::StdClass.new
  f = PageCustomField.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              
    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/page_custom_fields_controller.rb', line 22

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