Class: DcCommonController
- Inherits:
-
DcApplicationController
- Object
- ActionController::Base
- DcApplicationController
- DcCommonController
- Defined in:
- app/controllers/dc_common_controller.rb
Overview
This controller holds some common actions used by CMS.
Instance Method Summary collapse
-
#ad_click ⇒ Object
Register and record click when ad link is clicked.
-
#autocomplete ⇒ Object
This action is called on ajax autocomplete call.
-
#copy_clipboard ⇒ Object
Copy current record to clipboard as json text.
-
#login ⇒ Object
Alternative login action with remember_me cookie.
-
#logout ⇒ Object
Default user logout action.
-
#paste_clipboard ⇒ Object
Paste data from clipboard into text_area and update documents in destination database.
-
#process_login ⇒ Object
Default user login action.
-
#restore_from_journal ⇒ Object
Action for restoring document data from journal document.
-
#toggle_edit_mode ⇒ Object
Toggle CMS edit mode.This action is called when user clicks CMS option on top of the browser.
Methods inherited from DcApplicationController
#dc_dump, #dc_edit_mode?, #dc_find_form_file, #dc_get_site, #dc_log_visit, #dc_render_404, #dc_user_has_role, #set_page_title
Instance Method Details
#ad_click ⇒ Object
Register and record click when ad link is clicked.
73 74 75 76 77 78 79 80 81 82 83 |
# File 'app/controllers/dc_common_controller.rb', line 73 def ad_click if params[:id] and (ad = DcAd.find(params[:id])) ad.clicked += 1 ad.save DcAdStat.create!(dc_ad_id: params[:id], ip: request.ip, type: 2 ) else logger.error "ERROR ADS: Invalid ad id=#{params[:id]} ip=#{request.ip}." end render body: nil end |
#autocomplete ⇒ Object
This action is called on ajax autocomplete call. It checks if user has rights to view data.
URL parameters:
- table
-
Table (collection) model name in lower case indicating table which will be searched.
- id
-
Name of id key field that will be returned. Default is ‘_id’
- input
-
Search data entered in input field.
- search
-
when passed without dot it defines field name on which search
will be performed. When passed with dot class_method.method_name is assumed. Method name will be parsed and any class with class method name can be evaluated. Class method must accept input parameter and return array [ [_id, value],.. ] which will be used in autocomplete field.
Return: JSON array [label, value, id] of first 20 documents that confirm to query.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'app/controllers/dc_common_controller.rb', line 47 def autocomplete # return '' unless session[:edit_mode] > 0 # return render plain: t('drgcms.not_authorized') unless dc_user_can(DcPermission::CAN_VIEW) # TODO Double check if previous line works as it should. table = params['table'].classify.constantize id = [params['id']] || '_id' # call method in class if search parameter has . This is for user defined searches # result must be returned as array of [id, search_field_value] a = if params['search'].match(/\./) name, method = params['search'].split('.') table.send(method, params['input']).inject([]) do |r,v| r << { label: v[0], value: v[0], id: v[1].to_s } end # simply search which will search and return field_name defined in params['search'] else table.where(params['search'] => /#{params['input']}/i).limit(20).inject([]) do |r,v| r << { label: v[params['search']], value: v[params['search']], id: v.id.to_s } end end render inline: a.to_json, formats: 'js' end |
#copy_clipboard ⇒ Object
Copy current record to clipboard as json text. It will actually ouput an window with data formatted as json.
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'app/controllers/dc_common_controller.rb', line 188 def copy_clipboard # Only administrators can perform this operation return render(plain: t('drgcms.not_authorized') ) unless dc_user_has_role('admin') # respond_to do |format| # just open new window to same url and come back with html request format.json { dc_render_ajax(operation: 'window', url: request.url ) } format.html do doc = dc_find_document(params[:table], params[:id], params[:ids]) text = "<br><br>[#{params[:table]},#{params[:id]},#{params[:ids]}]<br>" render plain: text + doc.as_document.to_json end end end |
#login ⇒ Object
Alternative login action with remember_me cookie. If found it will automatically login user otherwise user will be presented with regular login dialog.
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'app/controllers/dc_common_controller.rb', line 130 def login if .signed[:remember_me] user = DcUser.find(.signed[:remember_me]) if user fill_login_data(user, true) return redirect_to params[:return_to] else clear_login_data # on the safe side end end # Display login route = params[:route] || 'poll' redirect_to "/#{route}?poll_id=login&return_to=#{params[:return_to]}" end |
#logout ⇒ Object
Default user logout action.
121 122 123 124 |
# File 'app/controllers/dc_common_controller.rb', line 121 def logout clear_login_data redirect_to params[:return_to] || '/' end |
#paste_clipboard ⇒ Object
Paste data from clipboard into text_area and update documents in destination database. This action is called twice. First time for displaying text_area field and second time ajax call for processing data.
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 |
# File 'app/controllers/dc_common_controller.rb', line 210 def paste_clipboard # Only administrators can perform this operation return render(plain: t('drgcms.not_authorized') ) unless dc_user_has_role('admin') result = '' respond_to do |format| # just open new window to same url and come back with html request format.html { return render('paste_clipboard', layout: 'cms') } format.json { table, id, ids = nil params[:data].split("\n").each do |line| line.chomp! next if line.size < 5 # empty line. Skip begin if line[0] == '[' # id(s) result << "<br>#{line}" line = line[/\[(.*?)\]/, 1] # just what is between [] table, id, ids = line.split(',') elsif line[0] == '{' # document data result << process_document(line, table, id, ids) end rescue Exception => e result << " Runtime error. #{e.}\n" break end end } end dc_render_ajax(div: 'result', value: result ) end |
#process_login ⇒ Object
Default user login action.
103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'app/controllers/dc_common_controller.rb', line 103 def process_login # Somebody is probably playing return dc_render_404 unless ( params[:record] and params[:record][:username] and params[:record][:password] ) unless params[:record][:password].blank? #password must not be empty user = DcUser.find_by(username: params[:record][:username]) if user and user.authenticate(params[:record][:password]) fill_login_data(user, params[:record][:remember_me].to_i == 1) return redirect_to params[:return_to] || '/' end end flash[:error] = t('drgcms.invalid_username') redirect_to params[:return_to_error] || '/' end |
#restore_from_journal ⇒ Object
Action for restoring document data from journal document.
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 |
# File 'app/controllers/dc_common_controller.rb', line 149 def restore_from_journal # Only administrators can perform this operation unless dc_user_has_role('admin') return render inline: { 'msg_info' => (t ('drgcms.not_authorized')) }.to_json, formats: 'js' end # selected fields to hash restore = {} params[:select].each {|key,value| restore[key] = value if value == '1' } result = if restore.size == 0 { 'msg_error' => (t ('drgcms.dc_journal.zero_selected')) } else journal_doc = DcJournal.find(params[:id]) # update hash with data to be restored JSON.parse(journal_doc.diff).each {|k,v| restore[k] = v.first if restore[k] } # determine tables and document ids tables = journal_doc.tables.split(';') ids = (journal_doc.ids.blank? ? [] : journal_doc.ids.split(';') ) << journal_doc.doc_id # find document doc = nil tables.each_index do |i| doc = if doc.nil? (tables[i].classify.constantize).find(ids[i]) else doc.send(tables[i].pluralize).find(ids[i]) end end # restore and save values restore.each { |field,value| doc.send("#{field}=",value) } doc.save # TODO Error checking { 'msg_info' => (t ('drgcms.dc_journal.restored')) } end render inline: result.to_json, formats: 'js' end |
#toggle_edit_mode ⇒ Object
Toggle CMS edit mode.This action is called when user clicks CMS option on top of the browser.
89 90 91 92 93 94 95 96 97 98 |
# File 'app/controllers/dc_common_controller.rb', line 89 def toggle_edit_mode session[:edit_mode] ||= 0 # called without logged in if session[:edit_mode] < 1 dc_render_404 else session[:edit_mode] = (session[:edit_mode] == 1) ? 2 : 1 redirect_to params[:return_to] end end |