Class: DcPollRenderer

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::FormHelper, ActionView::Helpers::FormOptionsHelper, CmseditHelper, DcApplicationHelper
Defined in:
app/helpers/dc_poll_renderer.rb

Overview

Renders code for displaying a poll. Polls may replace forms when user interaction is required in browser.

Instance Attribute Summary

Attributes included from DcApplicationHelper

#design, #form, #ids, #menu, #menu_item, #options, #page, #page_title, #part, #parts, #record_footer, #site, #tables

Attributes included from CmseditHelper

#js

Instance Method Summary collapse

Methods included from DcApplicationHelper

#_origin, #dc_add2_record_cookie, #dc_big_table, #dc_choices4, #dc_choices4_all_collections, #dc_choices4_cmsmenu, #dc_choices4_field, #dc_choices4_folders_list, #dc_choices4_menu, #dc_choices4_site_policies, #dc_date_time, #dc_deprecate, #dc_dont?, #dc_edit_mode?, #dc_edit_title, #dc_error_messages_for, #dc_flash_messages, #dc_format_date_time, #dc_get_site, #dc_icon4_boolean, #dc_iframe_edit, #dc_internal_var, #dc_label_for, #dc_limit_string, #dc_link_for_create, #dc_link_for_edit, #dc_link_for_edit1, #dc_link_menu_tag, #dc_link_to, #dc_menu_class, #dc_name4_id, #dc_name4_value, #dc_new_title, #dc_page_bottom, #dc_page_class, #dc_page_edit_menu, #dc_page_top, #dc_render, #dc_render_design, #dc_render_design_part, #dc_render_from_site, #dc_render_partial, #dc_replace_in_design, #dc_submit_tag, #dc_table_title, #dc_user_can_view, #dc_user_has_role, #decamelize_type, #forms_merge, #t, #t_name, #t_tablename

Methods included from CmseditHelper

#_get_user_for, #dc_actions_column, #dc_actions_for_form, #dc_actions_for_index, #dc_actions_for_result, #dc_background_for_result, #dc_check_and_default, #dc_clicks_for_result, #dc_columns_for_result, #dc_div_filter, #dc_document_statistics, #dc_fields_for_form, #dc_fields_for_tab, #dc_format_value, #dc_header_for_result, #dc_link_or_ajax, #dc_row_for_result, #dc_style_or_class, #dc_table_title_for_result, #define_standard_actions, #top_bottom_line

Constructor Details

#initialize(parent, opts = {}) ⇒ DcPollRenderer

Object initialization.



38
39
40
41
42
43
# File 'app/helpers/dc_poll_renderer.rb', line 38

def initialize( parent, opts={} ) #:nodoc:
  @parent   = parent
  @opts     = opts
  @part_css = ''
  self
end

Instance Method Details

#defaultObject

Default poll renderer method. Renders data for specified pool.



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

def default
# poll_id may be defined in params or opts
  poll_id = @opts[:poll_id] || @parent.params[:poll_id]
  return '<br>Poll id is not defined?<br>' if poll_id.nil?
#  
  poll = DcPoll.find(poll_id)
  poll = DcPoll.find_by(name: poll_id) if poll.nil? # name instead of id
  return "<div class=\"dc-form-error\">Invalid Poll id #{poll_id}</div>" if poll.nil?
  html = @opts[:div] ? "<div id='#{@opts[:div]}'>" : ''
  html << '<a name="poll-top"></a>'

  # Operation called before poll is displayed. Usefull for filling predefined values into flash[:record][value]
  # Called method must return at least one result if process can continue.
  if poll.pre_display.to_s.size > 1
    begin
      continue, message = eval(poll.pre_display.strip + '(@parent)')
    rescue Exception => e
      return "<div class=\"dc-form-error\">Error! Poll pre display. Error: #{e.message}</div>" 
    end
    return message unless continue
    html << message if message
  end

# there might be more than one poll displayed on page. Check if messages and values are for me
  if @parent.flash[:poll_id].nil? or @parent.flash[:poll_id] == poll_id
# If flash[:record] is present copy content to params record hash
    @parent.flash[:record].each {|k,v| @parent.params["p_#{k}"] = v } if @parent.flash[:record]  
# Error during procesing request
    html << "<div class=\"dc-form-error\">#{@parent.flash[:error]}</div>\n" if @parent.flash[:error].to_s.size > 0
    html << "<div class=\"dc-form-info\">#{@parent.flash[:info]}</div>\n" if @parent.flash[:info]
  end
# div and form tag
  html <<  "<div class=\"poll-div\">\n"
# edit link  
  if @opts[:edit_mode] > 1
    @opts[:editparams].merge!( controller: 'cmsedit', action: 'edit', id: poll._id, table: 'dc_poll', form_name: 'dc_poll' )
    @opts[:editparams].merge!(title: "#{t('drgcms.edit')}: #{poll.name}")
    @opts[:editparams].delete(:ids) # this is from page, but it gets in a way
    html << dc_link_for_edit( @opts[:editparams] )
  end
#  
  html << case
  when poll.operation == 'poll_submit' then
    @parent.form_tag(action: poll.operation, method: :put)
  when poll.operation == 'link' then
    @parent.form_tag( poll.parameters, method: :put)
  end
# header 
  html << "<div class='poll-title'>#{poll.title}</div>" unless poll.title[0] == '-' # - on first position will not display title
  html << poll.sub_text.to_s # if poll.sub_text.to_s.size > 5
  html << if poll.display == 'lr'
    "\n" + '<table class="poll-data-table">'
  else
    '<div class="poll-data-div">' + "\n"
  end
# items. Convert each item to yaml
  @end_od_data = false
  if poll.form.to_s.size < 10
    items = poll.dc_poll_items
    items.sort! {|a,b| a.order <=> b.order }
    items.each do |item|
      next unless item.active # disabled items
  # convert options to yaml
      yaml = YAML.load(item.options) || {}
      yaml['name'] = item.name
      yaml['html'] ||= {}
      yaml['html']['size'] = item.size
      (yaml['html']['class'] ||= 'dc-submit') if item.type == 'submit_tag'
      yaml['text'] = item.text
      yaml['mandatory']  = item.mandatory
      yaml['type']       = item.type
      html << do_one_item(poll, yaml)
    end
# Form. Just call do_one_item for each form item
  else
    yaml = YAML.load(poll.form.gsub('&nbsp;',' ')) # very annoying. They come with copy&paste ;-)
# if entered without numbering yaml is returned as Hash otherwise as Array
    yaml.each { |i| html << do_one_item(poll, (i.class == Hash ? i : i.last)) } # 
  end
# hide some fields usefull as parameters  
# was html << @parent.hidden_field_tag('return_to', @opts[:return_to] || @parent.params[:return_to] || '/')
  html << @parent.hidden_field_tag('return_to', @opts[:return_to] || @parent.params[:return_to] || @parent.request.url)
  html << @parent.hidden_field_tag('return_to_error', @parent.request.url )
  html << @parent.hidden_field_tag('poll_id', poll_id )
  html << @parent.hidden_field_tag('page_id', @parent.page.id )
  html << "</form></div>"
  html << '</div>' if @opts[:div]
  
  @part_css = poll.css
  html
end

#do_one_item(poll, yaml) ⇒ Object

Outputs code required for poll item. Subroutine of default method.



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

def do_one_item(poll, yaml)
  html = ''
  yaml['separator'] ||= ''
  yaml['text']      ||= ''
# 
  text = yaml['text'].match(/\./) ? t(yaml['text']) : yaml['text'] 
  if yaml['mandatory']
    text << '<font color="red"> *</font>' 
    yaml['html'] ||= {}
    yaml['html']['required'] = true
  end
  
# Just add text if comment and go to next one    
  if yaml['type'] == 'comment'
    html << if poll.display == 'lr'
      "<tr><td colspan='2' class='poll-data-text'>#{text}</td></tr>"
    else
      "<div class='poll-data-text'>#{text}</div>"
    end    
    return html
  end
# Set default value, if not already set
  if yaml['default'] 
    if yaml['default'].match('eval')
      e = yaml['default'].match(/\((.*?)\)/)[1]
      yaml['default'] = eval e
    end
    key = "p_#{yaml['name']}"
    params[key] = yaml['default'] unless params[key]
  end
# create form_field object and retrieve html code
  clas_string = yaml['type'].camelize
  field_html = if DrgcmsFormFields.const_defined?(clas_string) 
    clas = DrgcmsFormFields.const_get(clas_string)
    o = clas.new(@parent, @record, yaml).render
#TODO collect all javascript and add it at the end
    o.html + (o.js.size > 0 ? @parent.javascript_tag(o.js) : '')
  else # litle error string
    "Error: Code for field type #{yaml['type']} not defined!"
  end

#  field = send('dc_' + yaml['type'], yaml) # call dc_xxx method to get field html code
  if yaml['type'].match(/submit_tag|link_to/)
# There can be more than one links on form. End the data at first link or submit.
    if !@end_of_data 
      html << if poll.display == 'lr'
        "</table><br>\n"
      else
        "</div>\n"
      end
      # captcha
      if poll.captcha_type.to_s.size > 1
        @opts.merge!(:captcha_type => poll.captcha_type)
        captcha = DcCaptchaRenderer.new(@parent, @opts)
        html << captcha.render_html
        @part_css = captcha.render_css
      end
      @end_od_data = true
    end
# submit and link tag 
    clas = yaml['type'].match(/submit_tag/) ? '' : 'dc-link-submit'
    html << "<span class='#{clas} dc-animate'>#{field_html}#{yaml['separator']}</span>"
# other elements
  else
    html << if poll.display == 'lr'
      "<tr><td class='poll-data-text'>#{text}</td><td class='poll-data-field #{yaml['class']}'>#{field_html}</td></tr>\n"
    else
      "<div class='poll-data-text'>#{text}</div><div class='poll-data-field #{yaml['class']}'>#{field_html}#{yaml['separator']}</div>\n"
    end    
  end
end

#paramsObject

Dummy params method for accesing session params object from form.



48
49
50
# File 'app/helpers/dc_poll_renderer.rb', line 48

def params
  @parent.params
end

#render_cssObject

Return CSS part of code.



233
234
235
# File 'app/helpers/dc_poll_renderer.rb', line 233

def render_css
  @part_css
end

#render_htmlObject

Renderer dispatcher. Method returns HTML part of code.



225
226
227
228
# File 'app/helpers/dc_poll_renderer.rb', line 225

def render_html
  method = @opts[:method] || 'default'
  respond_to?(method) ? send(method) : "Error DcPoll: Method #{method} doesn't exist!"
end