Module: KitHelper

Included in:
AccountHelper
Defined in:
app/helpers/kit_helper.rb

Instance Method Summary collapse

Instance Method Details

#_sidObject



369
370
371
# File 'app/helpers/kit_helper.rb', line 369

def _sid
  @kit_system.get_system_id
end

#app_nameObject

kit_calendar - see calendar_helper



6
7
8
# File 'app/helpers/kit_helper.rb', line 6

def app_name
  Preference.get_cached(_sid, "app_name")
end

#bold_if(text, bold = true) ⇒ Object



351
352
353
354
355
356
357
# File 'app/helpers/kit_helper.rb', line 351

def bold_if(text, bold=true)
  if bold
    return "<b>#{text}</b>".html_safe
  else
    return text
  end
end

#button_to_with_params(label, url, data = {}) ⇒ Object



246
247
248
# File 'app/helpers/kit_helper.rb', line 246

def button_to_with_params(label, url, data = {})
  render :partial=>"utility/button_to_with_params", :locals=>{:url=>url, :data=>data, :button_label=>label} 
end

#cm_editor(name, options = {}) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'app/helpers/kit_helper.rb', line 131

def cm_editor(name, options = {})
  options[:mode] ||= "html"
  options[:mode] = 'htmlmixed' if options[:mode] == 'html'
  options[:mode] = 'css' if options[:mode] == 'sass' || options[:mode] == 'scss'
  
  options[:id] ||= name.gsub('[','_').gsub(']','') + rand(100000000).to_s

  logger.debug "*** #{name}"
  if options[:value].is_blank? && name =~ /([^\[]+)\[([^\]]+)\]/
    o = $1
    f = $2
    options[:value] = eval("@#{o}.#{f}") rescue ""
  end

  options[:value] ||= ''

  render :partial=>'utility/cm_editor', :locals=>{:options=>options, :name=>name}
end

#current_path_with_new_params(params) ⇒ Object



47
48
49
# File 'app/helpers/kit_helper.rb', line 47

def current_path_with_new_params(params)
  "#{current_without_params}?#{params}"
end

#current_url(patterns) ⇒ Object



381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# File 'app/helpers/kit_helper.rb', line 381

def current_url(patterns)
  # pattern something like /static/:param1/:param2
  rs = request.fullpath.split('/')
  rs.delete_at(0)

  pss = rs.last.split('?')
  p = {}
  if pss && pss[1]
    pss[1].split('&').each do |pair|
      k,v = pair.split('=')
      p[k] = v
    end

    rs[rs.length-1] = pss[0]
  end
  return nil unless rs
  patterns = [patterns] if patterns.instance_of?(String)
  patterns.each do |pattern|
    ps = pattern.split('/')
    ps.delete_at(0)
    next unless ps.length==rs.length
    result = p.clone
    match = true
    for i in 0..ps.length-1 do
      if ps[i][0].chr==':'
        key = ps[i][1..200].to_sym
        result[key] = rs[i]      
      else
        if ps[i] != rs[i]
          match = false
          break
        end
      end
    end
    if match 
      result[:matched] = pattern
      return result 
    end
  end
  return nil

end

#current_without_paramsObject



51
52
53
# File 'app/helpers/kit_helper.rb', line 51

def current_without_params
  request.fullpath.split("?")[0]
end

#date_picker(selector, options = {}) ⇒ Object



456
457
458
459
# File 'app/helpers/kit_helper.rb', line 456

def date_picker(selector, options = {})
  options[:date_format] = "dd/mm/yy"
  doc_ready("$('#{selector}').datetimepicker({ showOn: 'both', dateFormat: '#{options[:date_format]}'});")
end

#debug?Boolean

Returns:

  • (Boolean)


262
263
264
# File 'app/helpers/kit_helper.rb', line 262

def debug?
  is_debug?
end

#doc_ready(s = nil) ⇒ Object



16
17
18
19
20
21
22
23
# File 'app/helpers/kit_helper.rb', line 16

def doc_ready(s=nil)
"<script type='text/javascript'>
  $(document).ready(function() {
    #{s if s}
    #{yield if block_given?}
  });
</script>".html_safe
end


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/helpers/kit_helper.rb', line 25

def edit_link
 
  op = "$(\""

  op += "<div id=\'edit_status\'><a href=\'/db\'>Dashboard</a>"
 
  if @page
   op += " | <a id='info_link' href=\'/page/#{@page.id}/info\'>Info</a> "
   op += "| <a href=\'#{request.fullpath}?edit=1\'>Edit</a>" unless @page.locked?
    el = op
  end
  op += "</div>\").appendTo('body');\n"

  if @page
    op += "$('#edit_status').addClass(\"#{@page.status_id==Page.published_status(_sid) ? 'published' : 'not_published'}\");\n"
    op += "$('#edit_status #info_link').addClass('deleted');" if @page.is_deleted==1
  end

  op.html_safe
end

#entity_encode(s) ⇒ Object



465
466
467
468
469
470
471
472
473
# File 'app/helpers/kit_helper.rb', line 465

def entity_encode(s)
    lower = ('a'..'z').to_a
      upper = ('A'..'Z').to_a
      s.split('').map { |char|
          output = lower.index(char) + 97 if lower.include?(char)
          output = upper.index(char) + 65 if upper.include?(char)
          output ? "&##{output};" : (char == '@' ? '&#0064;' : char)
      }.join
end

#feature?(name) ⇒ Boolean

Returns:

  • (Boolean)


377
378
379
# File 'app/helpers/kit_helper.rb', line 377

def feature?(name)
  Preference.licensed?(_sid, name)
end

#field(name, type = 'text', title = nil, is_template = true, wrapper = 'div', hide_if_empty = false) ⇒ Object



301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'app/helpers/kit_helper.rb', line 301

def field(name, type = 'text', title = nil, is_template = true, wrapper = 'div', hide_if_empty = false)
  return '' unless @page
  if @page.respond_to?(name) 
    return "<span id='page_#{name}'>#{@page.send(name)}</span>".html_safe
  end

  value = @page.render_field(name, @page.draft ? -1 : 0)

  if @page.editable
    r = nil
    title ||= name.titleize

    if value==nil
      value = "[---#{title}---]" 
      is_template = false
    end

    return ("<#{wrapper} class='mercury-region' data-type='#{Page.mercury_region(type)}' data-fieldid='#{name}' data-field='#{title}' id='#{name}'>" + 
           (is_template ? (render :inline=>value, :handler=>:erb) : value) + 
           "</#{wrapper}>").html_safe
  else
    if hide_if_empty && value!=nil && value.starts_with?("[---") && value.ends_with?("---]")
      return ""
    else
      return (is_template ? render(:inline=>value) : value).html_safe rescue ""
    end
  end
end

#field_reveal(data, length) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'app/helpers/kit_helper.rb', line 164

def field_reveal(data, length)
  return "" unless data
  if data.length <= length
    return data
  end
  key = data.md5  + rand(10000000).to_s
  s = "<div style='display: inline;' id='#{key}'>"
  s += "<div style='display: inline;' class='short'>"
  s += truncate(data, :length=>length, :omission=>'')
  s += "... "
  s += link_to_function "More", "field_show_more('#{key}');", :class=>'field_show_more'
  s += "</div><div style='display: none;' class='long'>"
  s += data
  s += " "
  s += link_to_function "Less", "field_hide_more('#{key}');", :class=>'field_show_more'
  s += "</div></div>"
  return s.html_safe
end

#field_values(field_name, page_template_ids = nil, only_published_and_visible = true, version = 0) ⇒ Object



428
429
430
# File 'app/helpers/kit_helper.rb', line 428

def field_values(field_name, page_template_ids = nil, only_published_and_visible = true, version = 0) 
  Page.field_values(_sid, field_name, page_template_ids, only_published_and_visible)
end

#form_check_codeObject



10
11
12
13
14
# File 'app/helpers/kit_helper.rb', line 10

def form_check_code
  check = "#{rand(1000000).to_s}.#{Time.now}"

  hidden_field_tag :check, Digest::MD5.hexdigest(check)
end

#format_date(date) ⇒ Object



461
462
463
# File 'app/helpers/kit_helper.rb', line 461

def format_date(date)
  date.strftime(Preference.getCached(_sid, 'date_time_format') || '%d-%b-%y %H:%M')
end

#friendly_page_path(path) ⇒ Object



250
251
252
# File 'app/helpers/kit_helper.rb', line 250

def friendly_page_path(path)
  "/#{page.class.name.tableize.pluralize}/#{page.name}"
end

#hide_if(bool) ⇒ Object



150
151
152
# File 'app/helpers/kit_helper.rb', line 150

def hide_if(bool)
  bool ? "display:none;" : ""
end

#hide_unless(bool) ⇒ Object



154
155
156
# File 'app/helpers/kit_helper.rb', line 154

def hide_unless(bool)
  hide_if(!bool)
end

#host_nameObject



100
101
102
103
104
105
106
107
108
109
# File 'app/helpers/kit_helper.rb', line 100

def host_name
  h = Preference.getCached(_sid, 'host')
  h ||= request.domain

  if h =~ /^https?:\/\/(.*)$/
    return $1.strip.downcase
  else
    return h.strip.downcase
  end
end


338
339
340
341
342
343
344
345
346
347
348
349
# File 'app/helpers/kit_helper.rb', line 338

def icon_link(is_function, text, link, icon = false, toptions = nil)
  icon = 'no' if icon==false
  options = { :class => '' }
  options.merge! toptions if toptions
  options[:class] += " icon #{icon + '_icon'}"

  if is_function
    link_to_function text, link, options
  else
    link_to text, link, options
  end
end

#icon_to(text, link, icon = false, options = nil) ⇒ Object



330
331
332
# File 'app/helpers/kit_helper.rb', line 330

def icon_to(text, link, icon = false, options = nil) 
  icon_link(false, text, link, icon, options)
end

#icon_to_function(text, fnction, icon = false, options = nil) ⇒ Object



334
335
336
# File 'app/helpers/kit_helper.rb', line 334

def icon_to_function(text, fnction, icon = false, options = nil) 
  icon_link(true, text, fnction, icon, options)
end

#icon_to_show(label, element, options = {}) ⇒ Object



158
159
160
161
162
# File 'app/helpers/kit_helper.rb', line 158

def icon_to_show(label, element, options = {}) 
  id = "link_to_show_#{element}_#{rand(10000000)}"
  options[:id] ||= id
  return icon_to_function label, "$('##{options[:id]}').hide(); $('##{element}').slideDown();", false, options
end


496
497
498
# File 'app/helpers/kit_helper.rb', line 496

def ip_info_link(ip)
 link_to ip, "http://www.infobyip.com/ip-#{ip}.html"
end

#is_admin?Boolean

Returns:

  • (Boolean)


258
259
260
# File 'app/helpers/kit_helper.rb', line 258

def is_admin?
  return current_user && current_user.superadmin?
end

#is_debug?Boolean

Returns:

  • (Boolean)


266
267
268
# File 'app/helpers/kit_helper.rb', line 266

def is_debug?
  return Preference.getCached(_sid, "no_debug") != "true"
end

#is_mobile?Boolean

Returns:

  • (Boolean)


500
501
502
503
504
505
506
507
# File 'app/helpers/kit_helper.rb', line 500

def is_mobile?
 if @is_mobile==nil
   browser = Browser.new(:ua=>request.env['HTTP_USER_AGENT'])
   @is_mobile = browser.mobile?
 end
  
 return @is_mobile
end

#javascriptsObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/helpers/kit_helper.rb', line 65

def javascripts
  if @page
    (@page.page_template.layout.javascripts + @page.page_template.javascripts).uniq rescue []
  else
    js = []
    layout_name = controller.layout_name_being_used rescue "application"
    if layout_name.not_blank?
      layout = Layout.sys(_sid).where(:name=>layout_name).first
      if layout
        js += layout.javascripts.uniq
      end
    end
    begin
    if controller.template_being_used
      js += controller.template_being_used.javascripts.uniq 
    end
    rescue 
      logger.debug "Javascripts error"
    end

    return js
  end
end

#js_form_validation(form_selector) ⇒ Object



452
453
454
# File 'app/helpers/kit_helper.rb', line 452

def js_form_validation(form_selector)
  doc_ready("$('#{form_selector}').validate();")
end

#kit_default_headerObject



373
374
375
# File 'app/helpers/kit_helper.rb', line 373

def kit_default_header
  kit_render :partial=>"layouts/kit_header"
end

#kit_javascriptsObject



55
56
57
58
59
60
61
62
63
# File 'app/helpers/kit_helper.rb', line 55

def kit_javascripts
  op = []
  javascripts.each do |js|
    if js
      op << "<script type='text/javascript' src='/kit/#{js.system_id}/js/#{js.kit_name.downcase}'></script>"
    end
  end
  op.join("\n").html_safe
end

#kit_pagination(model) ⇒ Object



111
112
113
114
115
116
117
118
119
# File 'app/helpers/kit_helper.rb', line 111

def kit_pagination(model)
  r = '<div class="kit_pagination">'
  r += " <div class='page_info'>
      Showing #{[model.per_page, model.total_entries].min} of #{model.total_entries} 
    </div>"  unless model.instance_of?(ActiveRecord::Relation)
    r += paginate model
    r += "</div>"
    r.html_safe
end

#kit_render(options = {}) ⇒ Object



436
437
438
439
440
441
442
443
444
445
446
# File 'app/helpers/kit_helper.rb', line 436

def kit_render(options = {})
  custom_template = PageTemplate.get_custom_template(_sid, options[:partial], request, true)
  if custom_template
    options[:type] = custom_template.template_type || 'erb'
    options[:inline] = custom_template.body
    options.delete(:partial)
    render options
  else
    render options
  end
end

#kit_stylesheetsObject



89
90
91
92
93
94
95
96
97
# File 'app/helpers/kit_helper.rb', line 89

def kit_stylesheets
  op = []
  stylesheets.each do |sheet|
    if sheet
      op << "<link rel='stylesheet' type='text/css' href='/kit/#{sheet.system_id}/css/#{sheet.kit_name.downcase}'>"
    end
  end 
  op.join("\n").html_safe
end

#kit_submit(label, options = {}) ⇒ Object



121
122
123
124
125
126
127
128
129
# File 'app/helpers/kit_helper.rb', line 121

def kit_submit(label, options = {})
  options[:class] ||= ''
  options[:class] += " action"
  trans_name = label.urlise
  options[:id] ||= "submit[#{trans_name}]"
  options[:name] ||= trans_name
  options["data-value"] = trans_name
  link_to_function label, "submit_form(this)", options
end

#logoObject



230
231
232
# File 'app/helpers/kit_helper.rb', line 230

def 
  render :partial=>"admin/shared/name"
end

#obscure_email(email) ⇒ Object



475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
# File 'app/helpers/kit_helper.rb', line 475

def obscure_email(email)
    return nil if email.nil? #Don't bother if the parameter is nil.
    op = []

    m = rand(4)+1
    for i in 0..email.length-1
      if (i % m)==0
        random_class = "c#{rand(1000000)}"
        op << "<style type='text/css'>span.#{random_class} { display: none; }</style>"
        op << "<span class='#{random_class}'>"
        for x in 0..rand(5) 
          op << (97+rand(25)).chr
        end
        op << "</span>"
      end 
      op << entity_encode(email[i])
    end

    op.join('').html_safe
end

#optional_field(name) ⇒ Object



296
297
298
# File 'app/helpers/kit_helper.rb', line 296

def optional_field(name)
  field(name, 'text', nil, true, 'div', true)
end

#page_draft(page) ⇒ Object



242
243
244
# File 'app/helpers/kit_helper.rb', line 242

def page_draft(page)
  "/page/#{page.id}?draft=1"
end

#page_edit_draft_path(page) ⇒ Object



238
239
240
# File 'app/helpers/kit_helper.rb', line 238

def page_edit_draft_path(page)
  page_edit_path + "&draft=1"
end

#page_edit_path(page) ⇒ Object



234
235
236
# File 'app/helpers/kit_helper.rb', line 234

def page_edit_path(page)
  "/page/#{page.id}?edit=1"
end

#page_metaObject



183
184
185
186
187
188
189
# File 'app/helpers/kit_helper.rb', line 183

def page_meta
  if @page
    render :partial=>"pages/meta"
  else
    ""
  end
end

#page_titleObject



211
212
213
214
215
216
217
218
219
220
221
# File 'app/helpers/kit_helper.rb', line 211

def page_title
  if @page_title
    return @page_title
  elsif @page && @page.respond_to?(:title)
    return @page.title
  elsif self.respond_to?(:title)
    return self.title
  else
    Preference.getCached(_sid, "site_name")
  end
end

#pages_field_matches(field_name, field_value, page_template_ids = nil, only_published_and_visible = true, version = 0) ⇒ Object



432
433
434
# File 'app/helpers/kit_helper.rb', line 432

def pages_field_matches(field_name, field_value,  page_template_ids = nil, only_published_and_visible = true, version = 0) 
  Page.field_matches(_sid, field_name, field_value,  page_template_ids = nil, only_published_and_visible = true, version = 0) 
end

#pounds(n) ⇒ Object



424
425
426
# File 'app/helpers/kit_helper.rb', line 424

def pounds(n)
  number_to_currency(n, :unit=>"&pound;")
end

#pref(name, system = false, default = nil) ⇒ Object



359
360
361
362
363
# File 'app/helpers/kit_helper.rb', line 359

def pref(name, system = false, default = nil)
  return nil unless system || current_user 
  p = Preference.getCached(_sid, name, system ? nil : current_user.id)
  return p ? p : default
end

#render_snippet(page, page_snippet_id, snippet_id, defaults) ⇒ Object



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'app/helpers/kit_helper.rb', line 279

def render_snippet(page, page_snippet_id, snippet_id, defaults)
  page_snippet = page.page_snippets.where(:instance_id=>"snippet_#{page_snippet_id}").first

  unless page_snippet
    return "[Snippet missing #{snippet_id}]"
  end

  snippet = Snippet.find_by_name_or_id(snippet_id)
  field_id = "snippet_#{page_snippet_id}"

  r = %?
  <div class='mercury-region' data-type='snippetable' data-fieldid='snippet_#{page_snippet_id}' data-field='snippet_#{page_snippet_id}' id='snippet_#{page_snippet_id}'><div class="mercury-snippet" data-snippet="snippet_#{page_snippet_id}">#{render :inline=>page_snippet.render}</div></div>
  ?.html_safe

  return r
end

#selected_class(name) ⇒ Object



199
200
201
202
203
204
205
# File 'app/helpers/kit_helper.rb', line 199

def selected_class(name)
  if request.fullpath =~ /#{name}/
    "'selected'"
  else 
    ""
  end
end

#selected_item(name) ⇒ Object



191
192
193
194
195
196
197
# File 'app/helpers/kit_helper.rb', line 191

def selected_item(name)
  if request.fullpath =~ /#{name}/
    "class='selected'"
  else 
    ""
  end
end

#snippet(page_snippet_id, snippet_id, defaults = {}) ⇒ Object

def template_snippet(id)

  page_snippet = PageSnippet.where(:instance_id=>id).first
  render :inline=>page_snippet.render
end


275
276
277
# File 'app/helpers/kit_helper.rb', line 275

def snippet(page_snippet_id, snippet_id, defaults = {}) 
  render_snippet(@page, page_snippet_id, snippet_id, defaults)
end

#strike_if(content, strike) ⇒ Object



448
449
450
# File 'app/helpers/kit_helper.rb', line 448

def strike_if(content, strike)
  "<span style=\"#{'text-decoration: line-through;' if strike}\">#{content}</span>".html_safe
end

#style_word(word) ⇒ Object



223
224
225
226
227
# File 'app/helpers/kit_helper.rb', line 223

def style_word(word) 
  letter = word[0]
  rest = word[1..word.length]
  "<span class='letter #{letter}'>#{rest}</span>".html_safe
end

#sys_pref(name) ⇒ Object



365
366
367
# File 'app/helpers/kit_helper.rb', line 365

def sys_pref(name)
  pref(name, true)
end

#version_page_path(page, version) ⇒ Object



254
255
256
# File 'app/helpers/kit_helper.rb', line 254

def version_page_path(page, version)
  "/#{page.class.name.tableize.pluralize}/#{page.id}?v=#{version}"
end

#wrapper_classObject



207
208
209
# File 'app/helpers/kit_helper.rb', line 207

def wrapper_class
  @background_class
end