application_helper.rb - warvox - VoIP based wardialing tool, forked from rapid7/warvox.
 (HTM) git clone git://jay.scot/warvox
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
       ---
       application_helper.rb (5865B)
       ---
            1 # Methods added to this helper will be available to all templates in the application.
            2 module ApplicationHelper
            3 
            4   def select_tag_for_filter(nvpairs, params)
            5     _url = ( url_for overwrite_params: { }).split('?')[0]
            6     _html = %{<span class="pull-left filter-label">Filter: </span> }
            7     _html << %{<select name="show" class="filter-select" }
            8     _html << %{onchange="window.location='#{_url}' + '?show=' + this.value"> }
            9     nvpairs.each do |pair|
           10       _html << %{<option value="#{h(pair[:scope])}" }
           11       if params[:show] == pair[:scope] || ((params[:show].nil? || params[:show].empty?) && pair[:scope] == "all")
           12         _html << %{ selected="selected" }
           13       end
           14       _html << %{>#{pair[:label]} }
           15       _html << %{</option>}
           16     end
           17     _html << %{</select>}
           18     raw(_html)
           19   end
           20 
           21   def select_match_scope(nvpairs, params)
           22     _url = ( url_for overwrite_params: { }).split('?')[0]
           23     _html = %{<span class="pull-left filter-label">Matching Scope: </span> }
           24     _html << %{<select name="match_scope" class="filter-select" }
           25     _html << %{onchange="window.location='#{_url}' + '?match_scope=' + this.value"> }
           26     nvpairs.each do |pair|
           27       _html << %{<option value="#{h(pair[:scope])}" }
           28       if params[:match_scope] == pair[:scope] || ((params[:match_scope].nil? || params[:match_scope].empty?) && pair[:scope] == "job")
           29         _html << %{ selected="selected" }
           30       end
           31       _html << %{>#{pair[:label]} }
           32       _html << %{</option>}
           33     end
           34     _html << %{</select>}
           35     raw(_html)
           36   end
           37 
           38   def set_focus(element_id)
           39     javascript_tag(" $elem = $(\"#{element_id}\"); if (null !== $elem && $elem.length > 0){$elem.focus()}")
           40   end
           41 
           42   def format_job_details(job)
           43     begin
           44       info = Marshal.load(job.args.to_s)
           45 
           46       ttip = raw("<div class='task_args_formatted'>")
           47       info.each_pair do |k,v|
           48         ttip << raw("<div class='task_args_var'>") + h(truncate(k.to_s, length: 20)) + raw(": </div> ")
           49         ttip << raw("<div class='task_args_val'>") + h(truncate((v.to_s), length: 20)) + raw("&nbsp;</div>")
           50       end
           51       ttip << raw("</div>\n")
           52       outp = raw("<span class='xpopover' rel='popover' data-title=\"#{job.task.capitalize} Task ##{job.id}\" data-content=\"#{ttip}\">#{h job.task.capitalize}</span>")
           53       outp
           54     rescue ::Exception => e
           55       job.status.to_s.capitalize
           56     end
           57   end
           58 
           59   def format_job_status(job)
           60     case job.status
           61     when 'error'
           62       ttip = h(job.error.to_s)
           63       outp = raw("<span class='xpopover' rel='popover' data-title=\"Task Details\" data-content=\"#{ttip}\">#{h job.status.capitalize}</span>")
           64       outp
           65     else
           66       job.status.to_s.capitalize
           67     end
           68   end
           69 
           70   def format_job_rate(job)
           71     pluralize( (job.rate * 60.0).to_i, "call") + "/min"
           72   end
           73 
           74   #
           75   # Includes any javascripts specific to this view. The hosts/show view
           76   # will automatically include any javascripts at public/javascripts/hosts/show.js.
           77   #
           78   # @return [void]
           79   def include_view_javascript
           80     #
           81     # Sprockets treats index.js as special, so the js for the index action must be called _index.js instead.
           82     # http://guides.rubyonrails.org/asset_pipeline.html#using-index-files
           83     #
           84 
           85     controller_action_name = controller.action_name
           86 
           87     if controller_action_name == 'index'
           88       safe_action_name = '_index'
           89     else
           90       safe_action_name = controller_action_name
           91     end
           92 
           93     include_view_javascript_named(safe_action_name)
           94   end
           95 
           96   # Includes the named javascript for this controller if it exists.
           97   #
           98   # @return [void]
           99   def include_view_javascript_named(name)
          100 
          101     controller_path = controller.controller_path
          102     extensions = ['.coffee', '.js.coffee']
          103     javascript_controller_pathname = Rails.root.join('app', 'assets', 'javascripts', controller_path)
          104     pathnames = extensions.collect { |extension|
          105       javascript_controller_pathname.join("#{name}#{extension}")
          106     }
          107 
          108     if pathnames.any?(&:exist?)
          109       path = File.join(controller_path, name)
          110       content_for(:view_javascript) do
          111         javascript_include_tag path
          112       end
          113     end
          114   end
          115 
          116   def escape_javascript_dq(str)
          117     escape_javascript(str.strip).gsub("\\'", "'").gsub("\t", "    ")
          118   end
          119 
          120   def submit_checkboxes_to(name, path, html={})
          121     if html[:confirm]
          122       confirm = html.delete(:confirm)
          123       link_to(name, "#", html.merge({onclick: "if(confirm('#{h confirm}')){ submit_checkboxes_to('#{path}','#{form_authenticity_token}')}else{return false;}" }))
          124     else
          125       link_to(name, "#", html.merge({onclick: "submit_checkboxes_to('#{path}','#{form_authenticity_token}')" }))
          126     end
          127   end
          128 
          129   # Scrub out data that can break the JSON parser
          130   #
          131   # data - The String json to be scrubbed.
          132   #
          133   # Returns the String json with invalid data removed.
          134   def json_data_scrub(data)
          135     data.to_s.gsub(/[\x00-\x1f]/){ |x| "\\x%.2x" % x.unpack("C*")[0] }
          136   end
          137 
          138   # Returns the properly escaped sEcho parameter that DataTables expects.
          139   def echo_data_tables
          140     h(params[:sEcho]).to_json.html_safe
          141   end
          142 
          143   # Generate the markup for the call's row checkbox.
          144   # Returns the String markup html, escaped for json.
          145   def call_checkbox_tag(call)
          146     check_box_tag("result_ids[]", call.id, false, id: nil).to_json.html_safe
          147   end
          148 
          149   def call_number_html(call)
          150     json_data_scrub(h(call.number)).to_json.html_safe
          151   end
          152 
          153   def call_caller_id_html(call)
          154     json_data_scrub(h(call.caller_id)).to_json.html_safe
          155   end
          156 
          157   def call_provider_html(call)
          158     json_data_scrub(h(call.provider.name)).to_json.html_safe
          159   end
          160 
          161   def call_answered_html(call)
          162     json_data_scrub(h(call.answered ? "Yes" : "No")).to_json.html_safe
          163   end
          164 
          165   def call_busy_html(call)
          166     json_data_scrub(h(call.busy ? "Yes" : "No")).to_json.html_safe
          167   end
          168 
          169   def call_audio_length_html(call)
          170     json_data_scrub(h(call.audio_length.to_s)).to_json.html_safe
          171   end
          172 
          173   def call_ring_length_html(call)
          174     json_data_scrub(h(call.ring_length.to_s)).to_json.html_safe
          175   end
          176 
          177 
          178 end