Module: CmsEditHelper

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

#add_one_step(key, tab_name, key_number) ⇒ Object



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

def add_one_step(key, tab_name, key_number)
  fields = tab_name ? @form['form']['tabs'][tab_name] :  @form['form']['fields']
  { key_number => fields[key] }
end

#add_step_to_form(index, step, next_step) ⇒ Object



543
544
545
546
# File 'app/helpers/cms_edit_helper.rb', line 543

def add_step_to_form(index, step, next_step)
  @form['form']['actions'][index]['params']['step'] = step
  @form['form']['actions'][index]['params']['next_step'] = next_step
end

#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.



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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'app/helpers/cms_edit_helper.rb', line 87

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
  # readonly
  std_actions = { 1 => 'back' } if @form['readonly']

  actions = @form['form']['actions']
  # shortcut for actions: standard
  actions = nil if actions.class == String && actions == 'standard'
  actions = std_actions if actions.nil?

  # Actions are strictly forbidden
  if @form['form']['actions'] && dc_dont?(@form['form']['actions'])
    actions = []
  elsif actions['standard']
    actions.merge!(std_actions)
    actions['standard'] = nil
  end
  # request for close window button present
  if actions.class == Hash
    case params[:window_close]
    when '0' then actions[1] = 'close'; actions[3] = nil
    when '1' then actions = { 1 => 'close' }
    when '2' then actions = { 1 => 'close' }
    end
  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 = %(<span class="dc-spinner">#{mi_icon('settings-o 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 happens

    parms = @form_params.clone
    if options.class == String
      next if @form['readonly'] and !options.match(/back|close/)

      html << '<li>'
      html << case
        when (options == 'back' || options == 'cancle')
          # If return_to is present link directly to URL
          if parms['xreturn_to'] # disabled for now
            dc_link_to( 'drgcms.back', 'arrow_back', parms['return_to'], class: 'dc-link' )
          else
            parms['action'] = 'index'
            parms['readonly'] = parms['readonly'].to_s.to_i < 2 ? nil : 1  
            dc_link_to( 'drgcms.back', 'arrow_back', parms, class: 'dc-link' )
          end
          
        when options == 'delete'
          parms['operation'] = options
          parms['id']   = @record.id
          dc_link_to( 'drgcms.delete','remove', parms, data: { confirm: t('drgcms.confirm_delete') }, method: :delete, class: 'dc-link' )
          
        when options == 'new'
          parms['action'] = options
          dc_link_to( 'drgcms.new', 'add', parms, class: 'dc-link')
          
        when (options == 'enable' || options == 'disable')
          parms['operation'] = options
          parms['id']        = @record.id
          icon = (options == 'enable' ? 'check_box-o' : 'check_box_outline_blank')
          dc_link_to( "drgcms.#{options}",icon, parms, method: :delete, class: 'dc-link' )
          
        when options == 'edit'
          parms['operation'] = options
          parms['id']        = @record.id
          dc_link_to( "drgcms.#{options}", options, parms, class: 'dc-link' )
          
        when options == 'refresh'
          %(<div class="dc-link" onclick='window.location.href=window.location.href;'>#{fa_icon('refresh')} #{t('drgcms.refresh')}</div></li>)
          
        when options == 'close'
          close = params[:window_close].to_i
          if close < 2
            %(<div class="dc-link" onclick='window.close();'>#{fa_icon('close')} #{t('drgcms.close')}</div></li>)
          else
            %(<div class="dc-link" onclick='history.back();'>#{fa_icon('close')} #{t('drgcms.close')}</div></li>)
          end
      else 
        "err1 #{key}=>#{options}"
      end
      html << '</td>'
    # non standard actions      
    else
      # action will be displayed when show: always or readonly option is declared and form is readonly
      next if @form['readonly'] && !%w[readonly always].include?(options['show'].to_s)

      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'
        parameters = {}
        options['params'].each { |k, v| parameters[k] = dc_value_for_parameter(v) } if options['params']
        if dc_is_action_active?(options) 
          '<li>' +
             dc_submit_tag(caption, icon, { data: parameters, title: options['title'] }) +
          '</li>'
        else
          %(<li><div class="dc-link-no">#{mi_icon(icon)} #{caption}</div></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_to( caption, icon, parms, data: t('drgcms.confirm_delete'), method: :delete, class: 'dc-link' ) +
        '</li>'

      # ajax or link button
      when %w(ajax link window popup).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.



240
241
242
243
244
245
246
247
248
249
250
251
# File 'app/helpers/cms_edit_helper.rb', line 240

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.



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'app/helpers/cms_edit_helper.rb', line 258

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.



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

def dc_document_statistics
  return '' if @record.id.nil? || dc_dont?(@form['form']['info'])

  html =  %(<div id="dc-document-info">#{fa_icon('info md-18')}</div> <div id="dc-document-info-popup" class="div-hidden">)
  u = dc_document_user_for('created_by')
  html << %(<div><span>#{t('drgcms.created_by', 'Created by')}: </span><span>#{u}</span></div>) if u
  u = dc_document_user_for('updated_by')
  html << %(<div><span>#{t('drgcms.updated_by', 'Updated by')}: </span><span>#{u}</span></div>) if u

  html << %(<div><span>#{t('drgcms.created_at', 'Created at')}: </span><span>#{dc_format_value(@record.created_at)}</span></div>) if @record['created_at']
  html << %(<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 << '<div>'
  html << fa_icon('content_copy-o md-18', class: 'dc-link-img dc-link-ajax',
                  'data-url' => url, 'data-request' => 'get', title: t('drgcms.doc_copy_clipboard') )

  url = url_for(controller: :cmsedit, action: :run, table: 'dc_journal', control: 'cmsedit.filter_on',
                filter_oper: 'eq', filter_field: 'doc_id', filter_value: @record.id)

  html << fa_icon('history md-18', class: 'dc-link-img dc-window-open',
                  'data-url' => url, title: t('helpers.label.dc_journal.tabletitle') )
  html << %(<span>ID: </span>
            <span id="record-id" class="hover" onclick="dc_copy_to_clipboard('record-id');" title="Copy document ID to clipboard">#{@record.id}
            </span>)

  (html << '</div></div>').html_safe
end

#dc_document_user_for(field_name) ⇒ Object

Returns username for id. Subroutine of dc_document_statistics



357
358
359
360
361
362
# File 'app/helpers/cms_edit_helper.rb', line 357

def dc_document_user_for(field_name) #:nodoc:
  return if @record[field_name].nil?

  user = DcUser.find(@record[field_name])
  user ? user.name : @record[field_name]
end

#dc_fields_for_formObject

Creates input fields defined in form options



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

def dc_fields_for_form
  html = "<div id='data_fields' " + (@form['form']['height'] ? "style=\"height: #{@form['form']['height']}px;\">" : '>')
  @js  ||= ''
  @css ||= ''
  # fields
  if (form_fields = @form['form']['fields'])
    html << dc_input_form_create(form_fields) + '</div>'
  # tabs
  elsif @form['form']['tabs']
    html = dc_tabs_form_create()
  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']} #{@form['js']}"
  @css << "\n#{@form['css']}\n#{@form['form']['css']}"
  html.html_safe
end

#dc_form_leftObject

If form is divided into two parts, this method gathers html to be painted on right side of the form pane.



414
415
416
417
418
419
420
421
422
# File 'app/helpers/cms_edit_helper.rb', line 414

def dc_form_left
  yaml = @form.dig('form', 'form_left')
  return '' unless yaml

  html = ''
  html << dc_process_eval(yaml['eval'], self) if yaml['eval']

  html.html_safe
end

#dc_form_updateObject

Updates form prior to processing form



403
404
405
406
407
408
# File 'app/helpers/cms_edit_helper.rb', line 403

def dc_form_update
  # update form for steps options
  if @form.dig('form', 'steps')
    dc_form_update_steps
  end
end

#dc_head_for_formObject

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



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

def dc_head_for_form
  @css ||= ''
  head = @form['form']['head']
  return '' if head.nil?

  html    = %(<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_label_for_field(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
                dc_format_value(@record.send(options['name']), options['format'])
              end
    rescue Exception => e
      dc_log_exception(e, 'dc_head_for_form')
      field = '!!!Error'
    end

    klass = dc_style_or_class(nil, options['class'], field, @record)
    style = dc_style_or_class(nil, options['style'], field, @record)
    html << %(<div class="dc-column #{klass}" style="width:#{percent*span}%;#{style}">
  #{label.blank? ? '' : "<span class=\"label\">#{label}</span>"}
  <span id="head-#{options['name']}" class="field">#{field}</span>
</div>)
    current += span
    if current == split
      html << %(</div>\n<div class="dc-row">)
      current = 0
    end
  end
  html << '</div></div>'
  html.html_safe
end

#dc_is_action_active?(options) ⇒ Boolean

Determine if action button on the form is active. Action will be greyed out otherwise.

Returns:

  • (Boolean)

    : Boolean : defined by ‘active’ option’. Default is True.



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

def dc_is_action_active?(options)
  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
    if option.match(/new_record/i)
      (@record.new_record? && option == 'new_record') || (!@record.new_record? && option == 'not_new_record')
    elsif option.match(/\./)
      # shortcut for method and eval option
      dc_process_eval(option, self)
    else
      eval(option['eval'])
    end
  # direct evaluate expression
  when option['eval'] then
    eval(option['eval'])
  when option['method'] then
    # if record present send record otherwise send self as parameter
    dc_process_eval(option['method'], self)
  else
    false
  end  
end

#dc_value_for_parameter(param, current_document = nil) ⇒ 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
43
44
# File 'app/helpers/cms_edit_helper.rb', line 36

def dc_value_for_parameter(param, current_document = nil)#:nodoc:
  if param.class == Hash
    dc_internal_var(param['object'] ||= 'record', param['method'], current_document)
  elsif param.to_s.match(/record|document/)
    current_document ? current_document : @record
  else
    param
  end
end