Module: CmsIndexHelper

Defined in:
app/helpers/cms_index_helper.rb

Overview

CmseditHelper module defines helper methods used by cmsedit actions. Output is controlled by data found in 3 major sections of DRG CMS form: index, result_set and form sections.

Instance Method Summary collapse

Instance Method Details

#dc_actions_columnObject

Determines actions and width of actions column



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'app/helpers/cms_index_helper.rb', line 252

def dc_actions_column
  actions = @form['result_set']['actions']
  return [{}, 0, false] if actions.nil? || dc_dont?(actions)

  # standard actions
  actions = { 'standard' => true } if actions.class == String && actions == 'standard'
  std_actions = { 2 => 'edit', 5 => 'delete' }
  if actions['standard']
    actions.merge!(std_actions) 
    actions.delete('standard')
  end
  # check must be first action
  has_check = actions.first[1] == 'check'
  width = actions.size == 1 ? 22 : 44
  width = 22 if actions.size > 2 and !has_check
  [actions, width, has_check]
end

Calculates (blank) space required for actions when @record_footer is rendered



273
274
275
276
277
278
# File 'app/helpers/cms_index_helper.rb', line 273

def dc_actions_column_for_footer
  return '' unless @form['result_set']['actions']

  ignore, width, ignore2 = dc_actions_column
  %(<div class="dc-result-actions" style="width: #{width}px;"></div>).html_safe
end

#dc_actions_for_indexObject

Creates action div for cmsedit index action.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'app/helpers/cms_index_helper.rb', line 35

def dc_actions_for_index
  @js  = @form['script'] || @form['js'] || ''
  @css = @form['css'] || ''
  return '' if @form['index'].nil?

  actions = @form['index']['actions']
  return '' if actions.blank?
  
  std_actions = {2 => 'new', 3 => 'sort', 4 => 'filter' }
  std_actions.delete(2) if @form['readonly']

  if actions.class == String
    actions = dc_define_standard_actions(actions, std_actions)
  elsif actions['standard']
    actions.merge!(std_actions)
    actions['standard'] = nil
  end
  
  html_left, html_right = '', ''
  # Remove actions settings and sort
  only_actions = []
  actions.each { |key, value| only_actions << [key, value] if key.class == Integer }
  only_actions.sort_by!(&:first)
  only_actions.each do |key, options|
    session[:form_processing] = "index:actions: #{key}=#{options}"
    next if options.nil? # must be

    url = @parms.clone
    yaml = options.class == String ? {'type' => options} : options # if single definition simulate type parameter
    action = yaml['type'].to_s.downcase 
    if action == 'url'
      dc_deprecate "action: url will be deprecated. Use action: link in index: actions! Form #{params['form_name']}"
      action = 'link' 
    end
    # if return_to is present link directly to URL
    if action == 'link' && yaml['url']
      url = yaml['url']
    else
      url['controller'] = yaml['controller'] if yaml['controller']
      url['action']     = yaml['action'] || action
      url['table']      = yaml['table']  if yaml['table']
      url['form_name']  = yaml['form_name'] if yaml['form_name']
      url['control']    = yaml['control'] if yaml['control']
    end
    # html link options
    html_options = yaml['html'] || {}
    html_options['title'] = yaml['title'] if yaml['title']
    case
    # sort
    when action == 'sort'
      choices = [%w[id id]]
      if @form['index']['sort']
        @form['index']['sort'].split(',').each do |e|
          e.strip!
          choices << [ t("helpers.label.#{@form['table']}.#{e}"), e ]
        end
      end
      data = t('drgcms.sort') + select('sort', 'sort', choices, { include_blank: true }, { class: 'dc-sort-select',
                                 'data-table' => @form['table'], 'data-form' => CmsHelper.form_param(params)} )
      html_right << %(<li><div class="dc-sort">#{data}</li>)

    # filter
    when action == 'filter'
      table = session[@form['table']]
      url = table&.dig(:filter) ?
            url_for(controller: 'cmsedit', action: 'run', control: 'cmsedit.filter_off', t: @form['table'], f: CmsHelper.form_param(params)) :
            ''
      html_right << %(
<li>
  <div class="dc-filter" title="#{DcFilter.title4_filter_off(table)}" data-url="#{url.html_safe}">
    #{mi_icon(url.blank? ? 'search' : 'search_off') }#{DcFilter.menu_filter(self).html_safe}
  </div>
</li>#{DcFilter.get_filter_field(self)}).html_safe

    # new
    when action == 'new'
      caption = yaml['caption'] || 'drgcms.new'
      html_options['class'] = 'dc-link'
      html_left << "<li>#{dc_link_to(caption, 'add', url, html_options)}</li>"

    when action == 'close'
      html_left << %(<li><div class="dc-link" onclick="window.close();"'>#{fa_icon('close')} #{t('drgcms.close')}</div></li>)

    when action == 'back'
      html_left << %(<li><div class="dc-link" onclick="history.back();"'>#{fa_icon('arrow_back')} #{t('drgcms.back')}</div></li>)

    # menu
    when action == 'menu'
      code = if options['caption']
               caption = t(options['caption'], options['caption']) + '&nbsp;' + fa_icon('caret-down')
               caption + eval(options['eval'])
             else # when caption is false, provide own actions
               eval(options['eval'])
             end
      html_left << %(<li><div class="dc-link">#{code}</div></li>)

=begin
# reorder      
    when action == 'reorder' then  
      caption = t('drgcms.reorder')
      parms = @parms.clone
      parms['operation'] = v
      parms['id']       = params[:ids]
      parms['table']     = @form['table']
      dc_link_to( caption, 'reorder', parms, method: :delete )              
=end     
    when action == 'script'
      html_left << dc_script_action(options)

    when action == 'field'
      html_right << dc_field_action(yaml)

    when %w(ajax link window popup submit).include?(action)
      html_left << dc_link_ajax_window_submit_action(options, nil)

    else
      caption = dc_get_caption(yaml) || t("drgcms.#{action}")
      icon    = yaml['icon'] || action
      html_options['class'] = 'dc-link'
      code = dc_link_to(caption, icon, url, html_options)
      html_left << %(<li>#{code}</li>)
    end
  end

  %(
<form id="dc-action-menu">
  <span class="dc-spinner">#{fa_icon('settings-o spin')}</span>

  <div class="dc-action-menu">
    <ul class="dc-left">#{html_left}</ul>
    <ul class="dc-right">#{html_right}</ul>
  </div>
<div style="clear: both;"></div>
</form>
).html_safe
end

#dc_actions_for_result(document) ⇒ Object

Creates actions that could be performed on single row of result set.



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'app/helpers/cms_index_helper.rb', line 283

def dc_actions_for_result(document)
  actions = @form['result_set']['actions']
  return '' if actions.nil? || @form['readonly']

  actions, width, has_check = dc_actions_column()
  has_sub_menu = actions.size > 2 || (has_check && actions.size > 1)

  main_menu, sub_menu = '', ''
  actions.sort_by(&:first).each do |num, action|
    session[:form_processing] = "result_set:actions: #{num}=#{action}"
    parms = @parms.clone
    # if single definition simulate type parameter
    yaml = action.class == String ? { 'type' => action } : action

    if %w(ajax link window popup submit).include?(yaml['type'])
      @record = document # otherwise document fields can't be used as parameters
      html = dc_link_ajax_window_submit_action(yaml, document)
    else
      caption = dc_get_caption(yaml) || "drgcms.#{yaml['type']}"
      title   = t(yaml['help'] || caption, '')
      caption = has_sub_menu ? t(caption, '') : nil
      html = '<li>'
      html << case yaml['type']
      when 'check' then
        main_menu << '<li>' + check_box_tag("check-#{document.id}", false, false, { class: 'dc-check' }) + '</li>'
        next

      when 'edit' then
        parms['action'] = 'edit'
        parms['id'] = document.id
        dc_link_to( caption, 'edit-o', parms, title: title )

      when 'show' then
        parms['action'] = 'show'
        parms['id'] = document.id
        parms['readonly'] = true
        dc_link_to( caption, 'eye', parms, title: title )

      when 'duplicate' then
        parms['id'] = document.id
        # duplicate string will be added to these fields.
        parms['dup_fields'] = yaml['dup_fields'] 
        parms['action'] = 'create'
        dc_link_to( caption, 'content_copy-o', parms, data: { confirm: t('drgcms.confirm_dup') }, method: :post, title: title )

      when 'delete' then
        parms['action'] = 'destroy'
        parms['id'] = document.id
        dc_link_to( caption, 'delete-o', parms, data: { confirm: t('drgcms.confirm_delete') }, method: :delete, title: title )

      else # error. 
        yaml['type'].to_s
      end
      html << '</li>'
    end

    if has_sub_menu
      sub_menu << html
    else
      main_menu << html
    end
  end

  if has_sub_menu
    %(
<ul class="dc-result-actions" style="width: #{width}px;">#{main_menu}
  <li><div class="dc-result-submenu">#{fa_icon('more_vert')}
    <ul id="menu-#{document.id}">#{sub_menu}</ul>
  </div></li>
</ul>)
  else
    %(<ul class="dc-result-actions" style="width: #{width}px;">#{main_menu}</ul>)
  end.html_safe
end

#dc_clicks_for_result(document) ⇒ Object

Creates link for single or double click on result column



413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
# File 'app/helpers/cms_index_helper.rb', line 413

def dc_clicks_for_result(document)
  html = ''
  if @form['result_set']['dblclick']
    yaml = @form['result_set']['dblclick']
    opts = {}
    opts[:controller] = yaml['controller'] || 'cmsedit'
    opts[:action]     = yaml['action']
    opts[:table]      = yaml['table'] || CmsHelper.table_param(params)
    opts[:form_name]  = yaml['form_name'] || CmsHelper.form_param(params) || opts[:table]
    opts[:method]     = yaml['method'] || 'get'
    opts[:id]         = document['id']
    opts[:readonly]   = yaml['readonly'] if yaml['readonly']
    opts[:window_close] = yaml['window_close'] if yaml['window_close']
    html << ' data-dblclick=' + url_for(opts) 
  else
     html << (' data-dblclick=' +
                url_for(action: 'show', controller: 'cmsedit', id: document.id, ids: params[:ids],
                        readonly: (params[:readonly] ? 2 : 1), t: CmsHelper.table_param(params),
                        f: CmsHelper.form_param(params)) ) if @form['form']
  end
  html
end

#dc_columns_for_result(document) ⇒ Object

Creates column for each field of result set document.



472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
# File 'app/helpers/cms_index_helper.rb', line 472

def dc_columns_for_result(document)
  return '' unless @form['result_set']['columns']

  html = ''  
  @form['result_set']['columns'].sort.each do |k,v|
    session[:form_processing] = "result_set:columns: #{k}=#{v}"
    next if v['width'].to_s.match(/hidden|none/i)

    # convert shortcut to hash 
    v = {'name' => v} if v.class == String    
    begin
      # eval
      value = if v['eval']
        dc_process_column_eval(v, document)
      # as field        
      elsif document.respond_to?(v['name'])
        dc_format_value(document.send( v['name'] ), v['format']) 
      # as hash (dc_memory)
      elsif document.class == Hash 
        dc_format_value(document[ v['name'] ], v['format'])
      # error
      else
        "??? #{v['name']}"
      end
    rescue Exception => e
      dc_log_exception(e, 'dc_columns_for_result')
      value = '!!!Error'
    end
    html << '<div class="spacer"></div>'
    # set class
    clas = dc_style_or_class(nil, v['td_class'], value, document)
    # set width and align an additional style
    style = dc_style_or_class(nil, v['td_style'] || v['style'], value, document)
    flex_align  = v['align'].to_s == 'right' ? 'flex-direction:row-reverse;' : ''
    width_align = %Q[width:#{v['width'] || '15%'};#{flex_align}]
    style = "style=\"#{width_align}#{style}\" "

    html << "<div class=\"td #{clas}\" #{style}>#{value}</div>"
  end
  html.html_safe
end

#dc_div_filterObject

Creates filter div for cmsedit index/filter action.



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'app/helpers/cms_index_helper.rb', line 175

def dc_div_filter
  choices = []
  filter = @form['index'] && @form['index']['filter'] ? @form['index']['filter'] + ',' : ''
  filter << 'id as text_field' # filter id is added by default
  filter.split(',').each do |f| 
    f.strip!
    name = f.match(' as ') ? f.split(' ').first : f
    # like another field on the form
    name, like, f = f.split(' ') if f.match(' like ')
    choices << [ t("helpers.label.#{@form['table']}.#{name}", name), f ]
  end
  choices4_operators = t('drgcms.choices4_filter_operators').chomp.split(',').inject([]) do |r, v|
    r << (v.match(':') ? v.split(':') : v )
  end
  # currently selected options
  if session[@form['table']] && session[@form['table']][:filter]
    field_name, operators_value, dummy = session[@form['table']][:filter].split("\t")
  else
    field_name, operators_value = nil, nil
  end
  url_on  = url_for(controller: 'cmsedit', action: :run, control: 'cmsedit.filter_on' ,
                    t: CmsHelper.table_param(params), f: CmsHelper.form_param(params), filter_input: 1)
  url_off = url_for(controller: 'cmsedit', action: :run, control: 'cmsedit.filter_off',
                    t: CmsHelper.table_param(params), f: CmsHelper.form_param(params))
  %(
  <div id="drgcms_filter" class="div-hidden">
    <h1>#{t('drgcms.filter_set')}</h1>
    
    #{ select(nil, 'filter_field1', options_for_select(choices, field_name), { include_blank: true }) }
    #{ select(nil, 'filter_oper', options_for_select(choices4_operators, operators_value)) }
    <div class="dc-menu">
      <div class="dc-link dc-filter-set" data-url="#{url_on}">#{fa_icon('done')} #{t('drgcms.filter_on')}</div>
      <div class="dc-link-ajax" data-url="#{url_off}">
         #{mi_icon('close')}#{t('drgcms.filter_off')}
      </div>
    </div>
  </div>).html_safe
end

#dc_eval_to_array(expression) ⇒ Object

Split eval expression to array by parameters. Ex. Will split dc_name4_value(one ,“two”) => [‘dc_name4_value’, ‘one’, ‘two’]



518
519
520
# File 'app/helpers/cms_index_helper.rb', line 518

def dc_eval_to_array(expression)
  expression.split(/\ |\,|\(|\)/).delete_if {|e| e.blank? }.map {|e| e.gsub(/\'|\"/,'').strip }
end

#dc_filter_popupObject

Creates popup div for setting filter on result set header.



217
218
219
220
221
222
223
224
225
226
227
228
# File 'app/helpers/cms_index_helper.rb', line 217

def dc_filter_popup
  html = %(<div class="filter-popup" style="display: none;"><div>#{t('drgcms.filter_set')}</div><ul>)
  url  = url_for(controller: 'cmsedit', action: 'run', control: 'cmsedit.filter_on',
                 t: @form['table'], f: params['form_name'], filter_input: 1)

  t('drgcms.choices4_filter_operators').chomp.split(',').each do |operator_choice|
    caption, choice = operator_choice.split(':')
    html << %Q[<li data-operator="#{choice}" data-url="#{url}">#{caption}</li>]
  end 
  html << "</ul></div>"
  html.html_safe
end

#dc_form_titleObject

Will return title based on @form



233
234
235
236
237
238
239
# File 'app/helpers/cms_index_helper.rb', line 233

def dc_form_title
  return t("helpers.label.#{@form['table']}.tabletitle", @form['table'])  if @form['title'].nil?
  return t(@form['title'], @form['title']) if @form['title'].class == String

  # Hash
  dc_process_eval(@form['title']['eval'], [@form['title']['caption'] || @form['title']['text'], params])
end

#dc_format_value(value, format = nil) ⇒ Object

Formats value according to format supplied or data type. There is lots of things missing here.



439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
# File 'app/helpers/cms_index_helper.rb', line 439

def dc_format_value(value, format=nil)
  return '' if value.nil?

  klass = value.class.to_s
  return CmsCommonHelper.dc_format_date_time(value, format) if klass.match(/time|date/i)

  format = format.to_s.upcase
  if format[0] == 'N'
    return '' if value == 0 && format.match('Z')

    format.gsub!('Z', '')
    dec = format[1].blank? ? nil : format[1].to_i
    sep = format[2].blank? ? nil : format[2]
    del = format[3].blank? ? nil : format[3]
    cur = format[4].blank? ? nil : format[4]
    dc_format_number(value, dec, sep, del, cur)
  else
    value.to_s
  end
end

#dc_header_for_resultObject

Creates header div for result set.



361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
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
# File 'app/helpers/cms_index_helper.rb', line 361

def dc_header_for_result
  html = '<div class="dc-result-header">'
  if @form['result_set']['actions'] && !@form['readonly']
    ignore, width, has_check = dc_actions_column()
    check_all = fa_icon('check-square-o', class: 'dc-check-all') if has_check
    html << %(<div class="dc-result-actions" style="width:#{width}px;">#{check_all}</div>)
  end
  # preparation for sort icon  
  sort_field, sort_direction = nil, nil
  if session[@form['table']]
    sort_field, sort_direction = session[@form['table']][:sort].to_s.split(' ')
  end

  if (columns = @form['result_set']['columns'])
    columns.sort.each do |k, v|
      session[:form_processing] = "result_set:columns: #{k}=#{v}"
      next if v['width'].to_s.match(/hidden|none/i)

      th = %(<div class="th" style="width:#{v['width'] || '15%'};text-align:#{v['align'] || 'left'};" data-name="#{v['name']}")
      label = v['caption'] || v['label']
      label = (v['name'] ? "helpers.label.#{@form['table']}.#{v['name']}" : '') if label.nil?
      label = t(label) if label.match(/\./)
      # no sorting when embedded documents or custom filter is active
      sort_ok = !dc_dont?(@form['result_set']['sort'], false)
      sort_ok = sort_ok || (@form['index'] && @form['index']['sort'])
      sort_ok = sort_ok && !dc_dont?(v['sort'], false)
      if @tables.size == 1 && sort_ok
        icon = 'sort_unset md-18'
        filter_class = form_has_input_field?(v['name']) ? nil : 'no-filter'
        if v['name'] == sort_field
          icon = sort_direction == '1' ? 'sort_down md-18' : 'sort_up md-18'
        else
          # no icon if filter can not be set
          icon = nil if filter_class
        end
        # sort and filter icon
        icon = mi_icon(icon, class: filter_class) if icon
        url = url_for(controller: 'cmsedit', action: 'run', control: 'cmsedit.sort', sort: v['name'],
                      t: CmsHelper.table_param(params), f: CmsHelper.form_param(params))
        th << %(><span data-url="#{url}">#{label}</span>#{icon}</div>)
      else
        th << ">#{label}</div>"
      end
      html << %(<div class="spacer"></div>) + th
    end
  end
  (html << '</div>').html_safe
end

#dc_row_for_result(document) ⇒ Object

Creates tr code for each row of result set.



463
464
465
466
467
# File 'app/helpers/cms_index_helper.rb', line 463

def dc_row_for_result(document)
  clas  = "dc-#{cycle('odd','even')} " + dc_style_or_class(nil, @form['result_set']['tr_class'], nil, document)
  style = dc_style_or_class('style', @form['result_set']['tr_style'], nil, document)
  "<div  id=\"#{document.id}\" class=\"dc-result-data #{clas}\" #{dc_clicks_for_result(document)} #{style}>".html_safe
end

#dc_title_for_index(result = nil) ⇒ Object

Creates title div for index action. Title div also includes paging options and help link



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

def dc_title_for_index(result = nil)
  dc_table_title(dc_form_title(), result)
end