Module: CmseditEditHelper

Defined in:
app/helpers/cmsedit_edit_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_for_form(position) ⇒ Object

Creates actions div for edit form.

Displaying readonly form turned out to be challenge. For now when readonly parameter has value 2, back link will force readonly form. Value 1 or not set will result in normal link.



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
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
209
210
# File 'app/helpers/cmsedit_edit_helper.rb', line 84

def dc_actions_for_form(position)
# create standard actions  
  std_actions = {1 => 'back', 2 => {'type' => 'submit', 'caption' => 'drgcms.save'},
                 3 => {'type' => 'submit', 'caption' => 'drgcms.save&back'} }
# when edit only  
  unless @record.try(:id).nil?
    std_actions.merge!({6 => 'new'} )
    std_actions.merge!(@record.active ? {5 => 'disable'} : {5 => 'enable'} ) if @record.respond_to?('active')
    std_actions.merge!({7 => 'refresh'} )
  end
  actions = @form['form']['actions']
# shortcut for actions: standard 
  actions = nil if actions.class == String && actions == 'standard'
# standard actions
  actions = std_actions if actions.nil?
# readonly 
  actions = {1 => 'back'} if @form['readonly']
# Actions are strictly forbidden 
  if @form['form']['actions'] and dc_dont?(@form['form']['actions'])
    actions = []
  elsif actions['standard']
    actions.merge!(std_actions)
    actions['standard'] = nil
  end
# Update save and save&back
  actions.each do |k,v| 
    if v.class == String 
      if v.match(/save\&back/i)
        actions[k] = {'type' => 'submit', 'caption' => 'drgcms.save&back'}
      elsif v == 'save'
        actions[k] = {'type' => 'submit', 'caption' => 'drgcms.save'}      
      end
    end
  end
# remove standard option and sort so that standard actions come first
  actions.delete('standard')
  actions = actions.to_a.sort {|x,y| x[0] <=> y[0]} 
# Add spinner to the beginning
  html = %Q[<span class="dc-spinner">#{fa_icon('spinner lg spin')}</span><ul class="dc-menu #{position}">]
  
  actions.each do |key, options|
    session[:form_processing] = "form:actions: #{key} #{options}"
    next if options.nil?  # yes it happends
    parms = @parms.clone
    if options.class == String
      next if params[:readonly] and !(options == 'back')
      
      html << '<li class="dc-link dc-animate">'
      html << case 
        when (options == 'back' or options == 'cancle') then
# If return_to is present link directly to URL        
          if parms['xreturn_to'] # disabled for now
            dc_link_to( 'drgcms.back','arrow-left', parms['return_to'] )
          else
            parms['action'] = 'index'
            parms['readonly'] = parms['readonly'].to_s.to_i < 2 ? nil : 1  
            dc_link_to( 'drgcms.back','arrow-left', parms )
          end
          
        when options == 'delete' then
          parms['operation'] = options
          parms['id']   = @record.id
          dc_link_to( 'drgcms.delete','remove', parms, data: { confirm: t('drgcms.confirm_delete') }, method: :delete )
          
        when options == 'new' then
          parms['action'] = options
          dc_link_to( 'drgcms.new', 'plus', parms)
          
        when (options == 'enable' or options == 'disable') then
          parms['operation'] = options
          parms['id']        = @record.id
          icon = (options == 'enable' ? 'thumbs-o-up' : 'thumbs-o-down')
          dc_link_to( "drgcms.#{options}",icon, parms, method: :delete )
          
        when options == 'edit' then
          parms['operation'] = options
          parms['id']        = @record.id
          dc_link_to( "drgcms.#{options}",options, parms )
          
        when options == 'refresh' then
          "<div onclick='window.location.href=window.location.href;'>#{fa_icon('refresh')} #{t('drgcms.refresh')}</div></li>"
      else 
        "err1 #{key}=>#{options}"
      end
      html << '</td>'
    # non standard actions      
    else
      options['title'] = t("#{options['title'].downcase}", options['title']) if options['title']
      html << case 
      # submit button
      when options['type'] == 'submit'
        caption = options['caption'] || 'drgcms.save'
        icon    = options['icon'] || 'save'
        prms = {}
        options['params'].each { |k,v| prms[k] = dc_value_for_parameter(v) } if options['params']
        if dc_is_action_active?(options) 
          '<li class="dc-link-submit dc-animate">' + 
             dc_submit_tag(caption, icon, {:data => prms, :title => options['title'] }) +
          '</li>'
        else
          "<li class=\"dc-link-no\">#{fa_icon(icon)} #{caption}</li>"
        end
      
      # delete with some sugar added
      when options['type'] == 'delete'
        parms['id']   = @record.id
        parms.merge!(options['params'])
        caption = options['caption'] || 'drgcms.delete'
        icon = options['icon'] || 'remove'
        '<li class="dc-link dc-animate">' + 
          dc_link_to( caption, icon, parms, data: t('drgcms.confirm_delete'), method: :delete ) +
        '</li>'
      
      # ajax or link button
      when %w(ajax link window).include?(options['type'])
        dc_link_ajax_window_submit_action(options, @record)
        
# Javascript action        
      when options['type'] == 'script'
        dc_script_action(options)
      else
        '<li>err2</li>'
      end
    end
  end
  (html << '</ul>').html_safe
end

#dc_background_for_result(start) ⇒ Object

Create background div and table definitions for result set.



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

def dc_background_for_result(start)
  if start == :start
    html = '<div class="dc-result-div" ' 
    html << (@form['result_set']['table_style'] ? 'style="overflow-x: scroll;" >' : '>')
  #
    html << "\n<div class=\"dc-result #{@form['result_set']['table_class']}\" "
    html << (@form['result_set']['table_style'] ? "style=\"#{@form['result_set']['table_style']}\" >" : '>')
  else
    html = '</div></div>'
  end
  html.html_safe
end

#dc_check_and_default(value, default, values = nil) ⇒ Object

Checks if value is defined and sets default. If values are sent it also checks if value is found in values. If not it will report error and set value to default. Subroutine of dc_fields_for_tab.



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'app/helpers/cmsedit_edit_helper.rb', line 233

def dc_check_and_default(value, default, values=nil) #:nodoc:
  return default if value.nil?
# check if value is within allowed values  
  if values
    if !values.index(value) 
# parameters should be in downcase. Check downcase version.
      if n = values.index(value.downcase)
        return values[n]
      else
        logger.error("DRG Forms: Value #{value} not within values [#{values.join(',')}]. Default #{default} used!")
        return default
      end
    end
  end
  value
end

#dc_document_statisticsObject

Creates current document statistics div (created_by, created_at, .…) at the bottom of edit form. + lots of more. At the moment also adds icon for dumping current document as json text.



451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
# File 'app/helpers/cmsedit_edit_helper.rb', line 451

def dc_document_statistics
  return '' if @record.new_record? or dc_dont?(@form['form']['info'])
  html =  %Q[<div id="dc-document-info">#{fa_icon('info-circle lg')}</div> <div id="dc-document-info-popup" class="div-hidden"> ]
#
  u = dc_document_user_for('created_by')
  html << %Q[<div><span>#{t('drgcms.created_by', 'Created by')}: </span><span>#{u}</span></div>] if u
  u = dc_document_user_for('updated_by')
  html << %Q[<div><span>#{t('drgcms.updated_by', 'Updated by')}: </span><span>#{u}</span></div>] if u
  html << %Q[<div><span>#{t('drgcms.created_at', 'Created at')}: </span><span>#{dc_format_value(@record.created_at)}</span></div>] if @record['created_at']
  html << %Q[<div><span>#{t('drgcms.updated_at', 'Updated at')}: </span><span>#{dc_format_value(@record.updated_at)}</span></div>] if @record['updated_at']
# copy to clipboard icon
  parms = params.clone
  parms[:controller] = 'dc_common'
  parms[:action]     = 'copy_clipboard'
  url = url_for(parms.permit!)
  html << fa_icon('copy 2x', class: 'dc-link-img dc-link-ajax dc-animate', 
                  'data-url' => url, 'data-request' => 'get', title: t('drgcms.doc_copy_clipboard') )
  (html << '</div></div>').html_safe
end

#dc_document_user_for(field_name) ⇒ Object

Returns username for id. Subroutine of dc_document_statistics



439
440
441
442
443
444
445
# File 'app/helpers/cmsedit_edit_helper.rb', line 439

def dc_document_user_for(field_name) #:nodoc:
  if @record[field_name]
    u = DcUser.find(@record[field_name])
    return u ? u.name : @record[field_name]
  end
#  nil
end

#dc_fields_for_formObject

Creates edit form div.



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
374
375
376
377
378
379
# File 'app/helpers/cmsedit_edit_helper.rb', line 331

def dc_fields_for_form()
  html, tabs, tab_data = '',[], ''
  @js  ||= ''
  @css ||= ''
# Only fields defined  
  if (form_fields = @form['form']['fields'])
    html << "<div id='data_fields' " + (@form['form']['height'] ? "style=\"height: #{@form['form']['height']}px;\">" : '>')  
    html << dc_fields_for_tab(form_fields) + '</div>'
  else
# there are multiple tabs on form 
    first = true # first tab 
    @form['form']['tabs'].keys.sort.each do |tab_name|
      next if tab_name.match('actions')
# Tricky. If field name is not on the tab skip to next tab
      if params[:edit_only]
        is_on_tab = false
        @form['form']['tabs'][tab_name].each {|k,v| is_on_tab = true if params[:edit_only] == v['name'] }
        next unless is_on_tab
      end
# first div is displayed, all others are hidden      
      tab_data << "<div id=\"data_#{tab_name.delete("\s\n")}\""
      tab_data << ' class="div-hidden"' unless first
      tab_data << " style=\"height: #{@form['form']['height']}px;\"" if @form['form']['height']
      tab_data << ">#{dc_fields_for_tab(@form['form']['tabs'][tab_name])}</div>"
      tab_label = @form['form']['tabs'][tab_name]['caption'] || tab_name 
      tabs << [tab_name, tab_label]
      first = false      
    end
# make it all work together
    html << '<ul class="dc-form-ul" >'
    first = true # first tab must be selected
    tabs.each do |tab_name, tab_label| 
      html << "<li id=\"li_#{tab_name}\" data-div=\"#{tab_name.delete("\s\n")}\" class=\"dc-form-li"
      html << ' dc-form-li-selected' if first 
      html << "\">#{t(tab_label, t_name(tab_label,tab_label))}</li>" 
      first = false
    end
    html << '</ul>'
    html << tab_data
  end
  # add last_updated_at hidden field so controller can check if record was updated in db during editing
  html << hidden_field(nil, :last_updated_at, value: @record.updated_at.to_i) if @record.respond_to?(:updated_at)
  # add form time stamp to prevent double form submit
  html << hidden_field(nil, :form_time_stamp, value: Time.now.to_i)
  # add javascript code if defined by form
  @js << "\n#{@form['script']}"
  @css << "\n#{@form['css']}" 
  html.html_safe
end

#dc_fields_for_tab(fields_on_tab) ⇒ Object

Creates input fields for one tab. Subroutine of dc_fields_for_form.



260
261
262
263
264
265
266
267
268
269
270
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/cmsedit_edit_helper.rb', line 260

def dc_fields_for_tab(fields_on_tab) #:nodoc:
  html   = '<div class="dc-form">'
  labels_pos = dc_check_and_default(@form['form']['labels_pos'], 'right', ['top','left','right'])
  hidden_fields  = ''
  odd_even       = nil
  group_option, group_count = 0, 0
  reset_cycle()
  # Select form fields and sort them by key
  form_fields  = fields_on_tab.select {|field| field.class == Integer }
  form_fields.to_a.sort.each do |number, options|
    session[:form_processing] = "form:fields: #{number}=#{options}"
    # ignore if edit_only singe field is required
    next if params[:edit_only] and params[:edit_only] != options['name'] 
    # hidden_fields. Add them at the end
    if options['type'] == 'hidden_field'
      hidden_fields << DrgcmsFormFields::HiddenField.new(self, @record, options).render
      next
    end
    # label
    field_html,label,help = dc_field_label_help(options)
    # Line separator
    html << dc_top_bottom_line(options['top-line']) if options['top-line']
    # Begining of new row
    if group_count == 0
      html << '<div class="row-div">' 
      odd_even = cycle('odd','even')
      group_count  = options['group'] || 1 
      group_option = options['group'] || 1 
    end
    #    
    html << if labels_pos == 'top'
%Q[
<div class="dc-form-label-top dc-color-#{odd_even} dc-align-left" title="#{help}">
  <label for="record_#{options['name']}">#{label} </label>
  <div id="td_record_#{options['name']}">#{field_html}</div>
</div> ]
    else
      # no label
      if dc_dont?(options['caption'])
        label = ''
        label_width = 0
        data_width  = 100
      elsif group_option > 1 
        label_width = group_option != group_count ? 10 : 14        
        data_width  = 21
      else
        label_width = 14
        data_width  = 85      
      end      
%Q[
<div class="dc-form-label dc-color-#{odd_even} dc-align-#{labels_pos} dc-width-#{label_width}" title="#{help}">
  <label for="record_#{options['name']}">#{label} </label>
</div>
<div id="td_record_#{options['name']}" class="dc-form-field dc-color-#{odd_even} dc-width-#{data_width}">#{field_html}</div>
]
    end
    # check if group end
    if (group_count -= 1) == 0
      html << '</div>'
      # insert dummy div when only two fields in group
      html << '<div></div>' if group_option == 2
    end
    
    html << dc_top_bottom_line(options['bottom-line']) if options['bottom-line']
  end
  html << '</div>' << hidden_fields
end

#dc_head_for_formObject

Creates head form div. Head form div is used to display header datausefull to be seen even when tabs are switched.



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
426
427
428
429
430
431
432
433
434
# File 'app/helpers/cmsedit_edit_helper.rb', line 386

def dc_head_for_form()
  @css ||= ''
  head = @form['form']['head']
  return '' if head.nil?
  html    = %Q[<div class="dc-head #{head['class']}">\n<div class="dc-row">]
  split   = head['split'] || 4
  percent = 100/split
  current = 0
  head_fields = head.select {|field| field.class == Integer }
  head_fields.to_a.sort.each do |number, options|
    session[:form_processing] = "form: head: #{number}=#{options}"
    # Label
    caption = options['caption']
    span    = options['span'] || 1
    @css << "\n#{options['css']}" unless options['css'].blank?
    label   = if caption.blank?
      ''
    elsif options['name'] == caption
      t_name(options['name'], options['name'].capitalize.gsub('_',' ') )
    else
      t(caption, caption) 
    end
    # Field value
    begin
      field = if options['eval']
        dc_process_column_eval(options, @record)
      else
        @record.send(options['name'])
      end
    rescue Exception => e
      dc_log_exception(e)
      field = '!!!Error'
    end
    #
    klass = dc_style_or_class(nil, options['class'], field, @record)
    style = dc_style_or_class(nil, options['style'], field, @record)
    html << %Q[<div class="dc-column #{klass}" style="width:#{percent*span}%;#{style}">
  #{label.blank? ? '' : "<span class=\"label\">#{label}</span>"}
  <span class="field">#{field}</span>
</div>]
    current += span
    if current == split
      html << %Q[</div>\n<div class="dc-row">]
      current = 0
    end
  end
  html << '</div></div>'
  html.html_safe
end

#dc_is_action_active?(options) ⇒ Boolean

Creates actions div for edit form.

Displaying readonly form turned out to be challenge. For now when readonly parameter has value 2, back link will force readonly form. Value 1 or not set will result in normal link.



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

def dc_is_action_active?(options)
  if options['when_new']
    dc_deprecate("when_option will be deprecated and replaced by active: not_new_record! Form #{params[:form_name]}") 
    return !(dc_dont?(options['when_new']) and @record.new_record?)
  end
  return true unless options['active']
  # alias record and document so both can be used in eval
  record = document = @record
  option = options['active']
  case 
  # usually only for test
  when option.class  == TrueClass || option['eval'].class == TrueClass then true    
  when option.class == String then
    (@record.new_record? && option == 'new_record') || (!@record.new_record? && option == 'not_new_record')
  # direct evaluate expression
  when option['eval'] then
    eval(option['eval'])
  when option['method'] then
    # if record present send record otherwise send params as parameter
    parms = @record ? @record : params
    dc_process_eval(option['method'],parms)
  else
    false
  end  
end

#dc_top_bottom_line(options) ⇒ Object

Creates top or bottom horizontal line on form.



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

def dc_top_bottom_line(options)
  '<div class="dc-separator"></div>'
end

#dc_value_for_parameter(param) ⇒ Object

Will return value when internal or additional parameters are defined in action Subroutine of dc_actions_for_form.



36
37
38
39
40
41
42
# File 'app/helpers/cmsedit_edit_helper.rb', line 36

def dc_value_for_parameter(param)#:nodoc:
  if param.class == Hash
    dc_internal_var(param['object'] || 'record', param['method'])
  else
    param    
  end
end