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



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'app/helpers/cms_index_helper.rb', line 242

def dc_actions_column
  actions = @form['result_set']['actions']
  return [{}, 0] 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

  width = @form['result_set']['actions_width'] || 18*actions.size
  [actions, width]
end

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



261
262
263
264
265
266
# File 'app/helpers/cms_index_helper.rb', line 261

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

  ignore, width = dc_actions_column
  %Q[<div class="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
# 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
  
  # start div with hidden spinner image
  html = %(
<form id="dc-action-menu">
  <span class="dc-spinner">#{fa_icon('spinner lg spin')}</span>
  <ul class="dc-action-menu">)

  # 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' and 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
    yhtml = yaml['html'] || {}
    yhtml['title'] = yaml['title'] if yaml['title']

    code = case
    # sort
    when action == 'sort' then 
      choices = [['id','id']]
      if @form['index']['sort']
        @form['index']['sort'].split(',').each do |s| 
          s.strip!
          choices << [ t("helpers.label.#{@form['table']}.#{s}"), s ]
        end
      end
      fa_icon('sort-alpha-asc') + ' ' + t('drgcms.sort') + ' ' + 
              select('sort', 'sort', choices, { include_blank: true }, 
              { class: 'drgcms_sort', 'data-table' => @form['table'], 'data-form' => params['form_name']} )

    # filter
    when action == 'filter' then 
      caption = t('drgcms.filter')
      caption << '&nbsp;' + fa_icon('caret-down lg') + DcFilter.menu_filter(self)
      # add filter OFF link
      sess = session[@form['table']]
      if sess and sess[:filter]
        caption << '&nbsp;&nbsp;' + dc_link_to(nil,'remove lg',
                   { controller: 'cmsedit', filter: 'off', table: @form['table'], form_name: params['form_name'] },
                   { title: DcFilter.title4_filter_off(sess[:filter]) })
      end
      caption

    # new
    when action == 'new' then 
      caption = yaml['caption'] || 'drgcms.new'
      dc_link_to(caption,'plus', url, yhtml )

    # menu
    when action == 'menu' then
      if options['caption']
        caption = t(options['caption'], options['caption']) + '&nbsp;' + fa_icon('caret-down lg')
        caption + eval(options['eval'])
      else # when caption is false, provide own actions
        eval(options['eval'])
      end
=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 << dc_script_action(options)
      next

    when action == 'field'
      html << dc_field_action(yaml) 
      next 

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

    else
      caption = yaml['caption'] || yaml['text']
      icon    = yaml['icon'] ? yaml['icon'] : action
      dc_link_to(caption, icon, url, yhtml)
    end
    html << "<li class=\"dc-link dc-animate\">#{code}</li>"
    html << DcFilter.get_filter_field(self) if action == 'filter'
  end
  html << '</ul></form>'
  html.html_safe
end

#dc_actions_for_result(document) ⇒ Object

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



271
272
273
274
275
276
277
278
279
280
281
282
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
# File 'app/helpers/cms_index_helper.rb', line 271

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

  actions, width = dc_actions_column()
  html = %Q[<ul class="actions" style="width: #{width}px;">]
  actions.sort_by(&:first).each do |k, v|
    session[:form_processing] = "result_set:actions: #{k}=#{v}"
    parms = @parms.clone
    # if single definition simulate type parameter
    yaml = v.class == String ? { 'type' => v } : v
    # code already includes li tag
    if %w(ajax link window 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
      html << '<li class="dc-link">'
      html << case
      when yaml['type'] == 'check' then
        check_box_tag("check-#{document.id}", false,false,{ class: 'dc-check' })

      when yaml['type'] == 'edit' then
        parms['action'] = 'edit'
        parms['id']     = document.id
        dc_link_to( nil, 'pencil lg', parms )

      when yaml['type'] == '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( nil, 'copy lg', parms, data: { confirm: t('drgcms.confirm_dup') }, method: :post )

      when yaml['type'] == 'delete' then
        parms['action'] = 'destroy'
        parms['id']     = document.id
        #parms['return_to'] = request.url
        dc_link_to( nil, 'remove lg', parms, data: { confirm: t('drgcms.confirm_delete') }, method: :delete )

      # undocumented so far
      when yaml['type'] == 'edit_embedded'
        parms['controller'] = 'cmsedit'
        parms['table'] +=  ";#{yaml['table']}"
        parms['ids']   ||= ''
        parms['ids']   +=  "#{document.id};"
        dc_link_to( nil, 'table lg', parms, method: :get )
    
      else # error. 
        yaml['type'].to_s
      end
      html << '</li>'
    end
  end
  html << '</ul>'
  html.html_safe
end

#dc_clicks_for_result(document) ⇒ Object

Creates link for single or double click on result column



378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# File 'app/helpers/cms_index_helper.rb', line 378

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']
    opts[:form_name]  = yaml['form_name']
    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), table: params[:table],
                        form_name: params[:form_name]) ) if @form['form']
  end
  html
end

#dc_columns_for_result(document) ⇒ Object

Creates column for each field of result set document.



437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
# File 'app/helpers/cms_index_helper.rb', line 437

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.



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
197
198
199
200
201
202
# File 'app/helpers/cms_index_helper.rb', line 165

def dc_div_filter
  choices = []
  filter = (@form['index'] and @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
    if f.match(' like ')
      a    = f.split(' ')
      name = a.first
      f    = a.last
    end
    choices << [ t("helpers.label.#{@form['table']}.#{name}", name), f ] 
  end
  choices4_operators = t('drgcms.choices4_filter_operators').chomp.split(',').inject([]) {|r,v| r << (v.match(':') ? v.split(':') : v )}
  # currently selected options
  if session[@form['table']] and session[@form['table']][:filter]
    field_name, operators_value, dummy = session[@form['table']][:filter].split("\t")
  else
    field_name, operators_value = nil, nil
  end
  #{ form_tag :table => @form['table'], filter: :on, filter_input: 1, action: :index, method: :post }
  url = url_for(table: @form['table'],form_name: params['form_name'], filter: :on, filter_input: 1, action: :index, controller: :cmsedit)  
  html =<<EOT
  <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-animate drgcms_popup_submit" data-url="#{url}">#{fa_icon('check-square-o')} #{t('drgcms.filter_on')}</div>
      <div class="dc-link dc-animate">#{dc_link_to('drgcms.filter_off','close', {action: :index, filter: 'off', table: @form['table'], form_name: params['form_name']}) }</div>
    </div>
  </div>
EOT
  html.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’]



483
484
485
# File 'app/helpers/cms_index_helper.rb', line 483

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.



207
208
209
210
211
212
213
214
215
216
217
218
# File 'app/helpers/cms_index_helper.rb', line 207

def dc_filter_popup
  html = %Q[<div class="filter-popup" style="display: none;">
  <div>#{t('drgcms.filter_set')}</div>
  <ul>]
  url = url_for(table: @form['table'],form_name: params['form_name'], filter: :on, filter_input: 1, action: :index, controller: :cmsedit)
  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



223
224
225
226
227
228
229
# File 'app/helpers/cms_index_helper.rb', line 223

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.



404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# File 'app/helpers/cms_index_helper.rb', line 404

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.



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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'app/helpers/cms_index_helper.rb', line 331

def dc_header_for_result
  html = '<div class="dc-result-header">'
  if @form['result_set']['actions'] and !@form['readonly']
    ignore, width = dc_actions_column()
    if width > 0 && @form['result_set']['actions'][0].to_s == 'check'
      check_all = fa_icon('check-square-o', class: 'dc-check-all')
    end
    html << %Q[<div class="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 = %Q[<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 = @form['result_set'].nil? || (@form['result_set'] && @form['result_set']['filter'].nil?)
      sort_ok = true
      sort_ok = sort_ok || (@form['index'] && @form['index']['sort'])
      sort_ok = sort_ok && !dc_dont?(v['sort'], false)
      if @tables.size == 1 and sort_ok
        icon = 'sort lg'
        if v['name'] == sort_field
          icon = sort_direction == '1' ? 'sort-alpha-asc lg' : 'sort-alpha-desc lg'
        end        
        th << ">#{dc_link_to(label, icon, sort: v['name'], table: params[:table], form_name: params[:form_name], action: :index, icon_pos: :last )}</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.



428
429
430
431
432
# File 'app/helpers/cms_index_helper.rb', line 428

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



235
236
237
# File 'app/helpers/cms_index_helper.rb', line 235

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