Module: CmseditIndexHelper

Defined in:
app/helpers/cmsedit_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

Creates code for link or ajax action type. Subroutine of dc_actions_for_result.



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

def __dc_link_or_ajax_action(yaml, parms) #:nodoc:
  rest = {}
  rest['method']  = yaml['method'] || yaml['request'] || 'get'
  rest['caption'] = yaml['caption'] || yaml['text']
  rest['class']   = 'dc-animate'
  rest['title']   = yaml['title']
  
  if yaml['type'] == 'link'
    dc_link_to(yaml['caption'], yaml['icon'], parms, rest ) 
  else
    rest['data-url'] = url_for(parms)
    rest['class'] << " fa fa-#{yaml['icon']}"
    fa_icon(yaml['icon'], rest ) 
  end
end

#dc_actions_columnObject

Determines actions and width of actions column



261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'app/helpers/cmsedit_index_helper.rb', line 261

def dc_actions_column()
  actions = @form['result_set']['actions']
  return [{},0] if actions.nil? or dc_dont?(actions)
# standard actions  
  actions = {'standard' => true} if actions.class == String && actions == 'standard'
  std_actions = {' 2' => 'edit', ' 3' => '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



279
280
281
282
283
# File 'app/helpers/cmsedit_index_helper.rb', line 279

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.



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
# File 'app/helpers/cmsedit_index_helper.rb', line 56

def dc_actions_for_index()
  @js  = @form['script'] || @form['js'] || ''
  @css = @form['css'] || ''
  return '' if @form['index'].nil? or @form['readonly']
  actions = @form['index']['actions']
  return '' if actions.blank?
  
  std_actions = {2 => 'new', 3 => 'sort', 4 => 'filter' }
  if actions.class == String
    actions = 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\">\n  <span class=\"dc-spinner\">\#{fa_icon('spinner lg spin')}</span>\n  <ul class=\"dc-action-menu\">\n"
# 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']} )
# 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']}, { 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  
      caption = t(options['caption'], options['caption']) + '&nbsp;' + fa_icon('caret-down lg')
      caption + eval(options['eval'])      
=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>'
  html << '</form>'
  html.html_safe
end

#dc_actions_for_result(document) ⇒ Object

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



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
# File 'app/helpers/cmsedit_index_helper.rb', line 289

def dc_actions_for_result(document)
  actions = @form['result_set']['actions']
  return '' if actions.nil? or @form['readonly']
#  
  actions, width = dc_actions_column()
  html = %Q[<ul class="actions" style="width: #{width}px;">]
  actions.each do |k,v|
    session[:form_processing] = "result_set:actions: #{k}=#{v}"
    next if k == 'standard' # ignore standard definition
    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']) then
      @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'] == '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 )
=begin        
      when yaml['type'] == 'link' || yaml['type'] == 'ajax' then
        if yaml['url']
          parms['controller'] = yaml['url']
          parms['idr']        = document.id
        else
          parms['id']         = document.id
        end
        parms['controller'] = yaml['controller'] if yaml['controller']
        parms['action']     = yaml['action']     if yaml['action']
        parms['table']      = yaml['table']      if yaml['table']
        parms['form_name']  = yaml['form_name']  if yaml['form_name']
        parms['target']     = yaml['target']     if yaml['target']
        dc_link_or_ajax_action(yaml, parms)
=end        

      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



399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
# File 'app/helpers/cmsedit_index_helper.rb', line 399

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']
    html << ' data-dblclick=' + url_for(opts) 
  else
     html << (' data-dblclick=' +
       url_for(action: 'show', controller: 'cmsedit', id: document.id, 
       readonly: (params[:readonly] ? 2 : 1), table: params[:table],
       form_name: params[:form_name], ids: params[:ids])  ) if @form['form'] 
  end
  html
end

#dc_columns_for_result(document) ⇒ Object

Creates column for each field of result set document.



528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
# File 'app/helpers/cmsedit_index_helper.rb', line 528

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}"
    # 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)
      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)
    width_align = %Q[width: #{v['width'] || '15%'};text-align: #{v['align'] || 'left'};]
    style = "#{width_align}#{style}"

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

#dc_div_filterObject

Creates filter div for cmsedit index/filter action.



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
203
204
205
206
207
208
# File 'app/helpers/cmsedit_index_helper.rb', line 171

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 ="  <div id=\"drgcms_filter\" class=\"div-hidden\">\n    <h1>\#{t('drgcms.filter_set')}</h1>\n    \n    \#{ select(nil, 'filter_field1', options_for_select(choices, field_name), { include_blank: true }) }\n    \#{ select(nil, 'filter_oper', options_for_select(choices4_operators, operators_value)) }\n    <div class=\"dc-menu\">\n      <div class=\"dc-link dc-animate drgcms_popup_submit\" data-url=\"\#{url}\">\#{fa_icon('check-square-o')} \#{t('drgcms.filter_on')}</div>\n      <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>\n    </div>\n  </div>\n"
  html.html_safe
end

#dc_filter_popupObject

Creates popup div for setting filter on result set header.



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

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_format_value(value, format = nil) ⇒ Object

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



423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
# File 'app/helpers/cmsedit_index_helper.rb', line 423

def dc_format_value(value, format=nil)
  return '' if value.nil?
  klass = value.class.to_s
  case when klass.match('Time') then
    format ||= t('time.formats.default')
    value.strftime(format)  
  when klass.match('Date') then
    format ||= t('date.formats.default')
    value.strftime(format)  
  when format.to_s[0] == 'N' then
    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.



358
359
360
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
# File 'app/helpers/cmsedit_index_helper.rb', line 358

def dc_header_for_result()
  html = '<div class="dc-result-header">'
  if @form['result_set']['actions'] and !@form['readonly']
    ignore, width = dc_actions_column()
    html << %Q[<div class="actions" style="width: #{width}px;"></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}"
      th = %Q[<div class="th" style="width: #{v['width'] || '15%'};text-align: #{v['align'] || 'left'};" data-name="#{v['name']}"]
      # when no caption or name is defined it might be just spacer      
      if (caption = v['caption']).nil? 
        caption = v['name'] ? t("helpers.label.#{@form['table']}.#{v['name']}") : ''
      end
      # 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 = sort_ok || (@form['index'] && @form['index']['sort'])
      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(caption, icon, sort: v['name'], table: params[:table], form_name: params[:form_name], action: :index, icon_pos: :last )}</div>"
      else
        th << ">#{caption}</div>"
      end
      html << "<div class=\"spacer\"></div>" + th
    end
  end
  (html << '</div>').html_safe
end

#dc_process_column_eval(yaml, document) ⇒ Object

Process eval option for field value. Used for processing single field column on result_set or form head.



493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
# File 'app/helpers/cmsedit_index_helper.rb', line 493

def dc_process_column_eval(yaml, document)
  # dc_name4_id
  if yaml['eval'].match('dc_name4_id')
    a = yaml['eval'].split(/\ |\,/)
    if a.size == 3
      dc_name4_id(a[1], a[2], nil, document[ yaml['name'] ])
    else
      dc_name4_id(a[1], a[2], a[3], document[ yaml['name'] ])
    end
  # dc_name4_value  
  elsif yaml['eval'].match('dc_name4_value')
    dc_name4_value( @form['table'], yaml['name'], document[ yaml['name'] ] )
  elsif yaml['eval'].match('eval ')
  # TO DO evaluate with specified parameters
  else
    parameters = if yaml['params']
      # pass document as parameter
      if yaml['params'] == 'document' or yaml['params'] == 'record'     
        document
      else
        yaml['params'].chomp.split(',').inject([]) do |result,e| 
          result << document[e.strip]
        end        
      end        
    else
      document[ yaml['name'] ]
    end
    # evaluate by calling send method 
    dc_process_eval(yaml['eval'], parameters)
  end
end

#dc_process_eval(evaluate, parameters) ⇒ Object

Process eval. Breaks eval option and calls with send method. Parameters:

evaluate : String : Expression to be evaluated
parameters : Array : array of parameters which will be send to method


478
479
480
481
482
483
484
485
486
487
# File 'app/helpers/cmsedit_index_helper.rb', line 478

def dc_process_eval(evaluate, parameters)
  # evaluate by calling send method 
  clas, method = evaluate.split('.')
  if method.nil?
    send(clas, *parameters)
  else
    klass = clas.camelize.constantize
    klass.send(method, *parameters)
  end  
end

#dc_row_for_result(document) ⇒ Object

Creates tr code for each row of result set.



466
467
468
469
470
# File 'app/helpers/cmsedit_index_helper.rb', line 466

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_style_or_class(selector, yaml, value, record) ⇒ Object

Defines style or class for row (tr) or column (td)



446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
# File 'app/helpers/cmsedit_index_helper.rb', line 446

def dc_style_or_class(selector, yaml, value, record)
  return '' if yaml.nil?
# alias record and value so both names can be used in eval
  field, document = value, record
  html = selector ? "#{selector}=\"" : ''
  html << if yaml.class == String
    yaml
  # direct evaluate expression
  elsif yaml['eval']
    eval(yaml['eval']) rescue 'background-color:red;'
  elsif yaml['method']
    dc_process_eval(yaml['method'],record)
  end
  html << '"' if selector 
  html
end

#dc_table_title_for_result(result = nil) ⇒ Object

Creates title div for cmsedit index result set records. Title div also includes paging options.



230
231
232
233
234
235
236
237
# File 'app/helpers/cmsedit_index_helper.rb', line 230

def dc_table_title_for_result(result=nil)
  title = if @form['title'] # form has title section
    t(@form['title'], @form['title'])
  else # get name from translations
    t("helpers.label.#{@form['table']}.tabletitle", @form['table'])
  end
  dc_table_title(title, result)
end

#define_standard_actions(actions_params, standard) ⇒ Object

Get standard actions when actions directive contains single line. Subroutine of dc_actions_for_index

Allows for actions: new, filter, standard syntax



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/helpers/cmsedit_index_helper.rb', line 38

def define_standard_actions(actions_params, standard)
  actions = {}
  actions_params.split(',').each do |an_action|
    an_action.strip!
    if an_action == 'standard'
      actions.merge!(standard)
    else
      standard.each do |index, action| 
        (actions[index] = action; break) if action == an_action
      end
    end
  end 
  actions
end