Module: KitHelper

Defined in:
app/helpers/kit_helper.rb

Instance Method Summary collapse

Instance Method Details

#_sidObject



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

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



354
355
356
357
358
359
360
# File 'app/helpers/kit_helper.rb', line 354

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



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

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(mode, object, field, form, form_field = nil, options = {}) ⇒ Object



145
146
147
148
149
150
151
# File 'app/helpers/kit_helper.rb', line 145

def cm_editor(mode, object, field, form, form_field = nil, options = {})
  form_field ||= "#{object}_#{field}"

  mode = 'htmlmixed' if mode == 'html'
  mode = 'css' if mode == 'sass' || mode == 'scss'
  render :partial=>'utility/cm_editor', :locals=>{:mode=>mode, :field=>field, :form_field=>form_field, :form=>form, :options=>options}
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



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
423
424
425
# File 'app/helpers/kit_helper.rb', line 384

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



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

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)


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

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

#feature?(name) ⇒ Boolean

Returns:

  • (Boolean)


380
381
382
# File 'app/helpers/kit_helper.rb', line 380

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

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



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
329
330
331
# File 'app/helpers/kit_helper.rb', line 304

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



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

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



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

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



464
465
466
# File 'app/helpers/kit_helper.rb', line 464

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

#friendly_page_path(path) ⇒ Object



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

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

#hide_if(bool) ⇒ Object



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

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

#hide_unless(bool) ⇒ Object



157
158
159
# File 'app/helpers/kit_helper.rb', line 157

def hide_unless(bool)
  hide_if(!bool)
end

#host_nameObject



114
115
116
117
118
119
120
121
122
123
# File 'app/helpers/kit_helper.rb', line 114

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


341
342
343
344
345
346
347
348
349
350
351
352
# File 'app/helpers/kit_helper.rb', line 341

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



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

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



337
338
339
# File 'app/helpers/kit_helper.rb', line 337

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



161
162
163
164
165
# File 'app/helpers/kit_helper.rb', line 161

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

#is_admin?Boolean

Returns:

  • (Boolean)


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

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

#is_debug?Boolean

Returns:

  • (Boolean)


269
270
271
# File 'app/helpers/kit_helper.rb', line 269

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

#javascriptsObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/helpers/kit_helper.rb', line 69

def javascripts
  if @page
    (@page.page_template.layout.javascripts + "," + @page.page_template.javascripts).split(',').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.split(',').uniq
      end
    end
    begin
    if controller.template_being_used
      js += controller.template_being_used.javascripts.split(',').uniq 
    end
    rescue 
      logger.debug "Javascripts error"
    end

    return js
  end
end

#js_form_validation(form_selector) ⇒ Object



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

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

#kit_default_headerObject



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

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

#kit_javascripts(additional = []) ⇒ Object



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

def kit_javascripts(additional = [])
  op = []
  (additional + javascripts).each do |name|
    next if name.is_blank?
    js = HtmlAsset.fetch(_sid, name.downcase.strip, "js")
    if js
      op << "<script type='text/javascript' src='/kit/js/#{js.kit_name.downcase}'></script>"
    else
      op << "<!-- javascript missing '#{name.downcase.strip}' -->"
    end
  end
  op.join("\n").html_safe
end

#kit_pagination(model) ⇒ Object



125
126
127
128
129
130
131
132
133
# File 'app/helpers/kit_helper.rb', line 125

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



439
440
441
442
443
444
445
446
447
448
449
# File 'app/helpers/kit_helper.rb', line 439

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_stylesheets(additional = []) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'app/helpers/kit_helper.rb', line 93

def kit_stylesheets(additional = [])
  op = []
  begin
  s = stylesheets 
  rescue Exception => e
    s = ['application'] 
    logger.info "#{e.message}"
  end
  (additional + s).each do |name|
    next if name.is_blank?
    sheet = HtmlAsset.fetch(_sid, name.downcase.strip, "css")
    if sheet
      op << "<link rel='stylesheet' type='text/css' href='/kit/css/#{sheet.kit_name.downcase}'>"
    else
      op << "<!-- stylesheet missing '#{name.downcase.strip}' -->"
    end
  end
  op.join("\n").html_safe
end

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



135
136
137
138
139
140
141
142
143
# File 'app/helpers/kit_helper.rb', line 135

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



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

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

#obscure_email(email) ⇒ Object



468
469
470
471
472
473
474
475
476
477
# File 'app/helpers/kit_helper.rb', line 468

def obscure_email(email)
    return nil if email.nil? #Don't bother if the parameter is nil.
    lower = ('a'..'z').to_a
    upper = ('A'..'Z').to_a
    email.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

#optional_field(name) ⇒ Object



299
300
301
# File 'app/helpers/kit_helper.rb', line 299

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

#page_draft(page) ⇒ Object



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

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

#page_edit_draft_path(page) ⇒ Object



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

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

#page_edit_path(page) ⇒ Object



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

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

#page_metaObject



186
187
188
189
190
191
192
# File 'app/helpers/kit_helper.rb', line 186

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

#page_titleObject



214
215
216
217
218
219
220
221
222
223
224
# File 'app/helpers/kit_helper.rb', line 214

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



435
436
437
# File 'app/helpers/kit_helper.rb', line 435

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



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

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

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



362
363
364
365
366
# File 'app/helpers/kit_helper.rb', line 362

def pref(name, system = false, default = nil)
  return nil unless current_user || system
  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



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

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



202
203
204
205
206
207
208
# File 'app/helpers/kit_helper.rb', line 202

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

#selected_item(name) ⇒ Object



194
195
196
197
198
199
200
# File 'app/helpers/kit_helper.rb', line 194

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


278
279
280
# File 'app/helpers/kit_helper.rb', line 278

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

#strike_if(content, strike) ⇒ Object



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

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

#style_word(word) ⇒ Object



226
227
228
229
230
# File 'app/helpers/kit_helper.rb', line 226

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



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

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

#version_page_path(page, version) ⇒ Object



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

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

#wrapper_classObject



210
211
212
# File 'app/helpers/kit_helper.rb', line 210

def wrapper_class
  @background_class
end