Module: CmsHelper

Defined in:
app/helpers/cms_helper.rb

Overview

CmseditHelper module defines common methods used for DRGCMS forms.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#jsObject (readonly)

javascript part created by form helpers



31
32
33
# File 'app/helpers/cms_helper.rb', line 31

def js
  @js
end

Instance Method Details

#dc_field_action(yaml) ⇒ Object

Creates code for including data entry field in index actions.



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'app/helpers/cms_helper.rb', line 146

def dc_field_action(yaml)
  # assign value if value found in parameters
  if params['record']
    value = params['record'][yaml['name']]
    params["p_#{yaml['name']}"] = value
  end
  # find field definition on form
  if ( field_definition = dc_get_field_form_definition(yaml['name']) )
    # some options may be redefined
    field_definition['size'] = yaml['size'] if yaml['size']
    field, label, help = dc_field_label_help(field_definition)
  else
    yaml['type'] = yaml['field_type']
    field, label, help = dc_field_label_help(yaml)
  end
  # input field will have label as placeholder
  field = field.sub('input',"input placeholder=\"#{label}\"")
  %Q[<li class="no-background">#{field}</li>]
end

#dc_field_label_help(options) ⇒ Object

Return field code, label and help text for a field defined on a DRG Form.

Parameters: options : Hash : Field definition

Returns: Array

field_html : String : HTML code for field definition
label : String : Label text
help : String : Help text


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/helpers/cms_helper.rb', line 75

def dc_field_label_help(options)
  label, help = dc_label_help(options)
  # create field object from type and call its render method
  if options['type'].present?
    klass_string = options['type'].camelize
    field_html = if DrgcmsFormFields.const_defined?(klass_string) # when field type defined
      klass = DrgcmsFormFields.const_get(klass_string)
      field = klass.new(self, @record, options).render
      @js  << field.js
      @css << field.css
      field.html
    else
      "Error: Field type #{options['type']} not defined!"
    end
  else
    "Error: Field type missing!"
  end
  [field_html, label, help]
end

#dc_get_field_form_definition(name) ⇒ Object

Will return field form definition if field is defined on form. Field definition will be used for input field on the form.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/helpers/cms_helper.rb', line 48

def dc_get_field_form_definition(name) #:nodoc:
  return if @form['form'].nil?
  
  @form['form']['tabs'].each do |tab|
# Array with 2 elements. First is tabname, second is data      
    my_fields = tab.last
    my_fields.each {|k,v| return v if (k.class == Integer and v['name'] == name) }
  end if @form['form']['tabs'] #  I know. But nice. 
#
  @form['form']['fields'].each do |field|
    next unless field.first.class == Integer # options
    return field.last if field.last['name'] == name
  end if @form['form']['fields']
  nil
end

#dc_html_data(yaml) ⇒ Object

Create ex. class=“my-class” html code from html options for action



169
170
171
172
# File 'app/helpers/cms_helper.rb', line 169

def dc_html_data(yaml)
  return '' if yaml.blank?
  yaml.inject(' ') {|result, e| result = e.last.nil? ? result : result << "#{e.first}=\"#{e.last}\" "}
end

#dc_label_help(options) ⇒ Object

Return label and help text for a field defined on Form.

Parameters: options : Hash : Field definition

Returns:

label : String : Label text
help : String : Help text


105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'app/helpers/cms_helper.rb', line 105

def dc_label_help(options)
  # no label or help in comments
  return [nil, nil] if %w(comment action).include?(options['type'])

  label = options['caption'] || options['text'] || options['label']
  label = if label.blank?
            t_name(options['name'], options['name'].capitalize.gsub('_',' ') )
          elsif options['name']
            t(label, label)
          end
  # help text can be defined in form or in translations starting with helpers. or as helpers.help.collection.field
  help = options['help']
  help ||= "helpers.help.#{@form['table']}.#{options['name']}" if options['name']
  help = t(help, ' ') if help.to_s.match(/helpers\./)
  [label, help]
end

Creates code for link, ajax or windows action for index or form actions.

Parameters:

yaml: Hash : Action definition
record : Object : Currently selected record if available
action_active : Boolean : Is action active or disabled

Returns:

String : HTML code for action


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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'app/helpers/cms_helper.rb', line 185

def dc_link_ajax_window_submit_action(yaml, record=nil, action_active=true)
  parms = {}
  caption = yaml['caption'] || yaml['text']
  caption = caption ? t("#{caption.downcase}", caption) : nil
  icon    = yaml['icon'] ? "#{fa_icon(yaml['icon'])}" : ''
  # action is not active
  unless dc_is_action_active?(yaml)
    return "<li class=\"dc-link-no\">#{icon} #{caption}</li>" 
  end
  # set data-confirm when confirm 
  yaml['html'] ||= {}
  confirm = yaml['html']['data-confirm'] || yaml['confirm']
  yaml['html']['data-confirm'] = t(confirm) if confirm.present?
  yaml['html']['title'] ||= yaml['title']
  yaml['html']['title'] = t(yaml['title']) if yaml['title'].present?
  yaml['html']['target'] ||= yaml['target']
  # direct url
  if yaml['url']
    parms['url'] = yaml['url']
    parms['idr'] = dc_document_path(record) if record
  # make url from action controller
  else
    parms['controller'] = yaml['controller'] || 'cmsedit'
    parms['action']     = yaml['action'] 
    parms['table']      = yaml['table'] || @form['table']
    parms['form_name']  = yaml['form_name']
    parms['control']    = yaml['control'] if yaml['control']
    parms['id']         = record.id if record
  end
  # add current id to parameters
  parms['id'] = dc_document_path(record) if record
  # overwrite with or add additional parameters from environment or record
  yaml['params'].each { |k, v| parms[k] = dc_value_for_parameter(v, record) } if yaml['params']

  parms['table'] = parms['table'].underscore if parms['table'] # might be CamelCase
  # error if controller parameter is missing
  if parms['controller'].nil? && parms['url'].nil?
    "<li>#{'Controller not defined'}</li>"
  else
    #yaml['caption'] ||= yaml['text']
    html_data = dc_html_data(yaml['html'])
    url = url_for(parms) rescue 'URL error'
    url = nil if parms['url'] == '#'

    request = yaml['request'] || yaml['method'] || 'get'
    if yaml['type'] == 'ajax' # ajax button
      clas = "dc-link-ajax dc-animate"
      %Q[<li class="#{clas}" data-url="#{action_active ? url : ''}" #{html_data}
         data-request="#{request}" title="#{yaml['title']}">#{icon}#{caption}</li>]

    elsif yaml['type'] == 'submit'  # submit button
      # It's dirty hack, but will prevent not authorized message and render index action correctly
      parms[:filter] = 'on' 
      url  = url_for(parms) rescue 'URL error'
      clas = "dc-action-submit"
      %Q[<li class="#{clas}" data-url="#{action_active ? url : ''}" #{html_data}
         data-request="#{request}" title="#{yaml['title']}">#{icon}#{caption}</li>]

    elsif yaml['type'] == 'link'  # link button
      clas = "dc-link dc-animate"
      link = dc_link_to(caption, yaml['icon'], parms, yaml['html'] )
      %Q[<li class="#{clas}">#{action_active ? link : caption}</li>]

    elsif yaml['type'] == 'window'
      clas = "dc-link dc-animate dc-window-open"
      %Q[<li class="#{clas}" data-url="#{action_active ? url : ''}" #{html_data}>#{icon}#{caption}</li>]

    else
      '<li>Action Type error</li>'
    end
  end
end

#dc_log_exception(exception, where = '') ⇒ Object

Log exception to rails log. Usefull for debugging eval errors.



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

def dc_log_exception(exception, where = '')
  log = exception ? "\n*** Error:#{where + ':'} #{exception.message}\n#{exception.backtrace.first.inspect}\n" : ''
  log << "DRG Form processing line: #{session[:form_processing]}\n"
  
  logger.error log
end

#dc_script_action(yaml) ⇒ Object

Creates code for script action type.



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

def dc_script_action(yaml)
#  data = {'request' => 'script', 'script' => yaml['js'] || yaml['script'] }
#  %Q[<li class="dc-link-ajax with-link dc-animate">#{ dc_link_to(yaml['caption'], yaml['icon'], '#', data: data ) }</li>]
  icon = dc_icon_for_link yaml['icon']
  data = %Q[data-request="script" data-script="#{yaml['js'] || yaml['script']}"]
  %Q[<li class="dc-link-ajax dc-animate" #{data}>#{icon} #{ t(yaml['caption'],yaml['caption']) }</li>]
end

#dc_tab_label_help(tab_name) ⇒ Object

Return label and help for tab on Form.

Parameters: options : String : Tab name on form

Returns:

label : String : Label text
help : String : Help text


132
133
134
135
136
137
138
139
140
141
# File 'app/helpers/cms_helper.rb', line 132

def dc_tab_label_help(tab_name)
  label = @form['form']['tabs'][tab_name]['caption'] || tab_name
  label = t(label, t_name(label, label))

  help = @form['form']['tabs'][tab_name]['help'] || "helpers.help.#{@form['table']}.#{tab_name}"
  help = t(help, t_name(help, help))
  help = nil if help.match('helpers.') # help not found in translation

  [label, help]
end