Class: Caboose::BlockTypesController

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

Instance Method Summary collapse

Methods inherited from ApplicationController

#add_ga_event, #admin_add, #admin_bulk_add, #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_bulk_deleteObject


199
200
201
202
203
204
205
206
207
# File 'app/controllers/caboose/block_types_controller.rb', line 199

def admin_bulk_delete
  render :json => false and return if !logged_in_user.is_super_admin?     
  params[:model_ids].each do |bt_id|
    block_type = BlockType.where(:id => bt_id).first
    block_type.destroy if block_type
  end
  resp = Caboose::StdClass.new('success' => true)
  render :json => resp
end

#admin_createObject


136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'app/controllers/caboose/block_types_controller.rb', line 136

def admin_create
  render :json => false and return if !logged_in_user.is_super_admin?
  resp = Caboose::StdClass.new({
      'error' => nil,
      'redirect' => nil
  })
  bt = BlockType.new(
    :parent_id           => params[:parent_id] ? params[:parent_id] : nil,
    :name                => params[:name].downcase.gsub(' ', '_'),
    :description         => params[:name],                                                                
    :field_type          => params[:parent_id] ? 'text' : 'block',
    :allow_child_blocks  => false,
    :icon => 'checkbox-unchecked'        
  )       
  bt.save
  # Send back the response
  resp.redirect = "/admin/block-types/#{bt.id}"
  render :json => resp
end

#admin_deleteObject


210
211
212
213
214
215
216
217
# File 'app/controllers/caboose/block_types_controller.rb', line 210

def admin_delete
  render :json => false and return if !logged_in_user.is_super_admin?             
  BlockType.find(params[:id]).destroy            
  resp = StdClass.new({
    'redirect' => "/admin/block-types"
  })
  render :json => resp
end

#admin_editObject


83
84
85
86
87
# File 'app/controllers/caboose/block_types_controller.rb', line 83

def admin_edit
  redirect_to '/admin' and return if !logged_in_user.is_super_admin?   
  @block_type = BlockType.find(params[:id])
  render :layout => 'caboose/admin'
end

#admin_edit_btsm_cssObject


319
320
321
322
323
324
325
326
327
# File 'app/controllers/caboose/block_types_controller.rb', line 319

def admin_edit_btsm_css
  redirect_to '/admin' and return if !logged_in_user.is_super_admin?
  @btsm = BlockTypeSiteMembership.find(params[:id])  
  if (@site.id != @btsm.site_id && !@site.is_master)
    @error = "You are not allowed to edit this site."
    render :file => 'caboose/extras/error' and return
  end
  render :layout => 'caboose/admin'  
end

#admin_edit_btsm_htmlObject


295
296
297
298
299
300
301
302
303
# File 'app/controllers/caboose/block_types_controller.rb', line 295

def admin_edit_btsm_html
  redirect_to '/admin' and return if !logged_in_user.is_super_admin?
  @btsm = BlockTypeSiteMembership.find(params[:id])
  if (@site.id != @btsm.site_id && !@site.is_master)
    @error = "You are not allowed to edit this site."
    render :file => 'caboose/extras/error' and return
  end
  render :layout => 'caboose/admin'  
end

#admin_edit_iconObject


76
77
78
79
80
# File 'app/controllers/caboose/block_types_controller.rb', line 76

def admin_edit_icon
  redirect_to '/admin' and return if !logged_in_user.is_super_admin?    
  @block_type = BlockType.find(params[:id])
  render :layout => 'caboose/modal'
end

#admin_edit_render_functionObject


90
91
92
93
94
# File 'app/controllers/caboose/block_types_controller.rb', line 90

def admin_edit_render_function
  redirect_to '/admin' and return if !logged_in_user.is_super_admin?  
  @block_type = BlockType.find(params[:id])
  render :layout => 'caboose/admin'
end

#admin_edit_sassObject


110
111
112
113
114
# File 'app/controllers/caboose/block_types_controller.rb', line 110

def admin_edit_sass
  redirect_to '/admin' and return if !logged_in_user.is_super_admin? 
  @block_type = BlockType.find(params[:id])
  render :layout => 'caboose/admin'
end

#admin_error_logObject


129
130
131
132
133
# File 'app/controllers/caboose/block_types_controller.rb', line 129

def admin_error_log
  redirect_to '/admin' and return if !logged_in_user.is_super_admin?   
  @block_type = BlockType.find(params[:id])
  render :layout => 'caboose/admin'
end

#admin_indexObject


10
11
12
13
14
# File 'app/controllers/caboose/block_types_controller.rb', line 10

def admin_index
  redirect_to '/admin' and return if !logged_in_user.is_super_admin?
  @block_types = BlockType.where("parent_id is null or parent_id = 0").reorder(:name).all
  render :layout => 'caboose/admin'      
end

#admin_jsonObject


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
# File 'app/controllers/caboose/block_types_controller.rb', line 17

def admin_json
  render :json => false and return if !logged_in_user.is_super_admin?
  h = {        
    'name'              => '',
    'description'       => '',
    'name_like'         => '',
    'description_like'  => '',
  }
  if params[:parent_id]
    h['parent_id'] = ''
  else
    h['parent_id_null'] = true
    params[:parent_id_null] = nil if params[:parent_id_null]
  end
  pager = Caboose::Pager.new(params, h, {      
    'model' => 'Caboose::BlockType',
    'sort'  => 'name',
    'desc'  => 'false',
    'base_url' => "/admin/block-types",
    'items_per_page' => 100,
    'skip' => ['parent_id_null']
  })      
  render :json => {
    :pager => pager,
    :models => pager.items.as_json(:include => :sites)
  }
end

#admin_json_singleObject


46
47
48
49
50
# File 'app/controllers/caboose/block_types_controller.rb', line 46

def admin_json_single
  render :json => false and return if !logged_in_user.is_super_admin?
  block_type = BlockType.find(params[:id])
  render :json => block_type.as_json(:include => :sites)      
end

#admin_newObject


68
69
70
71
72
73
# File 'app/controllers/caboose/block_types_controller.rb', line 68

def admin_new
  redirect_to '/admin' and return if !logged_in_user.is_super_admin? 
  @block_type = BlockType.new
  @parent_id = params[:id]
  render :layout => 'caboose/admin'
end

#admin_optionsObject


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
# File 'app/controllers/caboose/block_types_controller.rb', line 237

def admin_options
  return unless user_is_allowed('pages', 'edit')

  options = []
  case params[:field]
    when nil
      if params[:id]          
        bt = BlockType.find(params[:id])            
        options = []
        if bt.options_function && bt.options_function.length > 0
          options = bt.render_options(@site.id)
        elsif bt.options
          options = bt.options.strip.split("\n").collect { |line| { 'value' => line, 'text' => line }}
        end
      else                  
        options = BlockType.where("parent_id is null").reorder(:name).all.collect do |bt| 
          { 'value' => bt.id, 'text' => bt.description } 
        end
      end
    when 'field-type'
      options = [ 
        { 'value' => 'checkbox'           , 'text' => 'Checkbox'                     }, 
        { 'value' => 'checkbox_multiple'  , 'text' => 'Checkbox (multiple)'          }, 
        { 'value' => 'image'              , 'text' => 'Image'                        },
        { 'value' => 'file'               , 'text' => 'File'                         },
        { 'value' => 'richtext'           , 'text' => 'Rich Text'                    }, 
        { 'value' => 'select'             , 'text' => 'Multiple choice (select box)' }, 
        { 'value' => 'text'               , 'text' => 'Textbox'                      }, 
        { 'value' => 'textarea'           , 'text' => 'Textarea'                     },
        { 'value' => 'block'              , 'text' => 'Block'                        }
      ]
    when 'site'      
      options = Site.reorder("description, name").all.collect{ |s| { 'value' => s.id, 'text' => s.description && s.description.strip.length > 0 ? s.description : s.name }}
    when 'tree'          
      options = []            
      BlockType.where("parent_id is null or parent_id = 0").reorder(:name).all.each do |bt|        
        admin_tree_options_helper(options, bt, '')         
      end
    when 'layout'
      options = []
      cat_ids = BlockTypeCategory.layouts.collect{ |cat| cat.id }
      q = ["block_type_category_id in (?)", cat_ids]
      q = ["block_type_category_id in (?) and block_type_site_memberships.site_id = ?", cat_ids, params[:site_id]] if params[:site_id]                                                                
      BlockType.includes(:block_type_site_memberships).where(q).reorder(:description).all.each do |bt|    
        options << { 'value' => bt.id, 'text' => bt.description }
      end    
  end        
  render :json => options
end

#admin_options_for_new_blockObject


221
222
223
224
225
226
227
228
229
230
231
# File 'app/controllers/caboose/block_types_controller.rb', line 221

def admin_options_for_new_block
  return unless user_is_allowed('pages', 'edit')      
  cats = BlockTypeCategory.where("name != ?", 'Layouts').all.collect{ |cat| {
    :block_type_category => cat,
    :block_types => Caboose::BlockType.includes(:block_type_site_memberships)
      .where(:parent_id => nil, :block_type_category_id => cat.id)
      .where("block_type_site_memberships.site_id = ?", @site.id)
      .reorder(:description).all
  }}
  render :json => cats
end

#admin_parse_formObject


53
54
55
56
# File 'app/controllers/caboose/block_types_controller.rb', line 53

def admin_parse_form
  redirect_to '/admin' and return if !logged_in_user.is_super_admin?  
  render :layout => 'caboose/admin'      
end

#admin_parse_tagsObject


59
60
61
62
63
64
# File 'app/controllers/caboose/block_types_controller.rb', line 59

def admin_parse_tags
  redirect_to '/admin' and return if !logged_in_user.is_super_admin?   
  children = params[:children] && params[:children] != 'false' ? params[:children] : nil
  resp = BlockTypeParser.parse_html(params[:html], params[:tags], children)
  render :json => resp
end

#admin_tree_options_helper(options, bt, prefix) ⇒ Object


287
288
289
290
291
292
# File 'app/controllers/caboose/block_types_controller.rb', line 287

def admin_tree_options_helper(options, bt, prefix)
  options << { 'value' => bt.id, 'text' => "#{prefix}#{bt.description}" }
  bt.children.each do |bt2|
    admin_tree_options_helper(options, bt2, " - #{prefix}")
  end      
end

#admin_updateObject


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
# File 'app/controllers/caboose/block_types_controller.rb', line 157

def admin_update
  render :json => false and return if !logged_in_user.is_super_admin?
  
  resp = StdClass.new({'attributes' => {}})
  bt = BlockType.find(params[:id])
  save = true      

  params.each do |k,v|
    case k          
      when 'parent_id'                       then bt.parent_id                      = v
      when 'name'                            then bt.name                           = v
      when 'description'                     then bt.description                    = v
      when 'is_global'                       then bt.is_global                      = v
      when 'icon'                            then bt.icon                           = v
      when 'block_type_category_id'          then bt.block_type_category_id         = v
      when 'render_function'                 then bt.render_function                = v
      when 'use_render_function'             then bt.use_render_function            = v
      when 'use_render_function_for_layout'  then bt.use_render_function_for_layout = v
      when 'use_js_for_modal'                then bt.use_js_for_modal               = v
      when 'allow_child_blocks'              then bt.allow_child_blocks             = v
      when 'default_child_block_type_id'     then bt.default_child_block_type_id    = v
      when 'name'                            then bt.name                           = v
      when 'field_type'                      then bt.field_type                     = v                       
      when 'default'                         then bt.default                        = v
      when 'width'                           then bt.width                          = v
      when 'height'                          then bt.height                         = v
      when 'fixed_placeholder'               then bt.fixed_placeholder              = v
      when 'options'                         then bt.options                        = v
      when 'options_function'                then bt.options_function               = v
      when 'options_url'                     then bt.options_url                    = v
      when 'default_constrain'               then bt.default_constrain              = v
      when 'default_full_width'              then bt.default_full_width             = v
      when 'default_included'                 then bt.default_included             = v
      when 'site_id'                         then resp.btsm_id = bt.toggle_site(v[0], v[1])
    end
  end
  
  resp.success = save && bt.save
  render :json => resp
end

#admin_update_btsm_cssObject


330
331
332
333
334
335
336
337
338
339
# File 'app/controllers/caboose/block_types_controller.rb', line 330

def admin_update_btsm_css
  render :json => false and return if !logged_in_user.is_super_admin?      
  resp = StdClass.new  
  @btsm = BlockTypeSiteMembership.find(params[:id])
  @btsm.custom_css = params['code']
  @btsm.save
  resp.success = true
  resp.message = "Code has been saved!"
  render :json => resp
end

#admin_update_btsm_htmlObject


306
307
308
309
310
311
312
313
314
315
316
# File 'app/controllers/caboose/block_types_controller.rb', line 306

def admin_update_btsm_html
  render :json => false and return if !logged_in_user.is_super_admin?      
  resp = StdClass.new  
  @btsm = BlockTypeSiteMembership.find(params[:id])
  code = params['code'].blank? ? '' : params['code'].gsub('<%==','<%= raw')
  @btsm.custom_html = code
  @btsm.save
  resp.success = true
  resp.message = "Code has been saved!"
  render :json => resp
end

#admin_update_render_functionObject


97
98
99
100
101
102
103
104
105
106
107
# File 'app/controllers/caboose/block_types_controller.rb', line 97

def admin_update_render_function
  render :json => false and return if !logged_in_user.is_super_admin?
  resp = StdClass.new  
  @block_type = BlockType.find(params[:id])
  code = params['code'].blank? ? '' : params['code'].gsub('<%==','<%= raw')
  @block_type.render_function = code
  @block_type.save
  resp.success = true
  resp.message = "Render function has been saved!"
  render :json => resp
end

#admin_update_sassObject


117
118
119
120
121
122
123
124
125
126
# File 'app/controllers/caboose/block_types_controller.rb', line 117

def admin_update_sass
  render :json => false and return if !logged_in_user.is_super_admin?
  resp = StdClass.new  
  @block_type = BlockType.find(params[:id])
  @block_type.custom_sass = params['code']
  @block_type.save
  resp.success = true
  resp.message = "Code has been saved!"
  render :json => resp
end

#api_block_typeObject


354
355
356
357
358
# File 'app/controllers/caboose/block_types_controller.rb', line 354

def api_block_type
  bt = BlockType.where(:name => params[:name]).first
  render :json => { 'error' => 'Invalid block type.' } if bt.nil?            
  render :json => bt.api_hash        
end

#api_block_type_listObject


346
347
348
349
350
351
# File 'app/controllers/caboose/block_types_controller.rb', line 346

def api_block_type_list
  arr = BlockType.where("parent_id is null and share = ?", true).reorder(:name).all.collect do |bt|
    { 'name' => bt.name, 'description' => bt.description }
  end      
  render :json => arr      
end