module Mindapp::Helpers

Public Instance Methods

affirm(s) click to toggle source
# File lib/mindapp2/helpers.rb, line 369
def affirm(s)
  return s =~ /[y|yes|t|true]/i ? true : false
end
ajax?(s) click to toggle source
# File lib/mindapp2/helpers.rb, line 161
def ajax?(s)
  return s.match('file_field') ? false : true
end
align_text(s, pixel=3) click to toggle source
# File lib/mindapp2/helpers.rb, line 132
def align_text(s, pixel=3)
  "<span style='position:relative; top:-#{pixel}px;'>#{s}</span>".html_safe
end
authorize?() click to toggle source
# File lib/mindapp2/helpers.rb, line 78
def authorize? # use in pending tasks
  @runseq= @xmain.runseqs.find @xmain.current_runseq
  return false unless @runseq
  @user = current_ma_user
  set_global
  return false unless eval(@runseq.rule) if @runseq.rule
  return true if true_action?(@runseq.action)
  # return false if check_wait
  return true if @runseq.role.blank?
  unless @runseq.role.empty?
    return false unless @user.role
    return @user.role.upcase.split(',').include?(@runseq.role.upcase)
  end
  return true
end
authorize_init?() click to toggle source
# File lib/mindapp2/helpers.rb, line 93
  def authorize_init? # use when initialize new transaction
    # check module role
    mrole = @service.module.role
    return false if mrole && !current_ma_user
    return false if mrole && !current_ma_user.has_role(mrole)

    # check step 1 role
    xml= @service.xml
    step1 = REXML::Document.new(xml).root.elements['node']
    role= get_option_xml("role", step1) || ""
#    rule= get_option_xml("rule", step1) || true
    return true if role==""
    unless current_ma_user
      return role.blank?
    else
      return false unless current_ma_user.role
      return current_ma_user.has_role(role)
    end
  end
b(s) click to toggle source

methods from application_controller

# File lib/mindapp2/helpers.rb, line 6
def b(s)
  "<b>#{s}</b>".html_safe
end
code_div(s) click to toggle source
# File lib/mindapp2/helpers.rb, line 158
def code_div(s)
  "<pre style='background-color: #efffef;'><code class='ruby' lang='ruby'>    #{s}</code></pre>".html_safe
end
code_text(s) click to toggle source
# File lib/mindapp2/helpers.rb, line 29
def code_text(s) # old def code(s)
  "<pre style='background-color: #efffef;'><code class='ruby' lang='ruby'>#{s}</code></pre>".html_safe
end
controller_exists?(modul) click to toggle source
# File lib/mindapp2/helpers.rb, line 296
def controller_exists?(modul)
  File.exists? "#{Rails.root}/app/controllers/#{modul}_controller.rb"
end
current_ma_user() click to toggle source

methods that I don't know where they came from

# File lib/mindapp2/helpers.rb, line 181
def current_ma_user
  if session[:user_id]
    return @user ||= User.find(session[:user_id]['$oid'])
  else
    return nil
  end
end
date_thai(d= Time.now, options={}) click to toggle source
# File lib/mindapp2/helpers.rb, line 9
def date_thai(d= Time.now, options={})
  unless d
    ""
  else
    y = d.year+543
    if options[:monthfull] || options[:month_full]
      mh= ['มกราคม', 'กุมภาพันธ์', 'มีนาคม', 'เมษายน', 'พฤษภาคม', 'มิถุนายน', 'กรกฏาคม', 'สิงหาคม', 'กันยายน', 'ตุลาคม', 'พฤศจิกายน', 'ธันวาคม']
    else
      mh= ['ม.ค.', 'ก.พ.', 'มี.ค.', 'เม.ย.', 'พ.ค.', 'มิ.ย.', 'ก.ค.', 'ส.ค.', 'ก.ย.', 'ต.ค.', 'พ.ย.', 'ธ.ค.']
    end
    if options[:dateonly] || options[:date_only]
      d.day.to_s+" "+mh[d.month-1]+" "+y.to_s
    else
      d.day.to_s+" "+mh[d.month-1]+" "+y.to_s+" เวลา "+sprintf("%02d",d.hour.to_s)+":"+sprintf("%02d",d.min.to_s)
    end
  end
end
dup_hash(a) click to toggle source
# File lib/mindapp2/helpers.rb, line 299
def dup_hash(a)
  h = Hash.new(0)
  a.each do |aa|
    h[aa] += 1
  end
  return h
end
freemind2action(s) click to toggle source
# File lib/mindapp2/helpers.rb, line 345
def freemind2action(s)
  case s.downcase
  #when 'bookmark' # Excellent
  #  'call'
  when 'bookmark' # Excellent
    'do'
  when 'attach' # Look here
    'form'
  when 'edit' # Refine
    'pdf'
  when 'wizard' # Magic
    'ws'
  when 'help' # Question
    'if'
  when 'forward' # Forward
    'redirect'
  when 'kaddressbook' #Phone
    'invoke' # invoke new service along the way
  when 'list'
    'output'
  when 'mail'
    'mail'
  end
end
get_app() click to toggle source
# File lib/mindapp2/helpers.rb, line 268
def get_app
  f= MM || "#{Rails.root}/app/mindapp/index.mm"
  dir= File.dirname(f)
  t= REXML::Document.new(File.read(MM).gsub("\n","")).root
  recheck= true ; first_pass= true
  while recheck
    recheck= false
    t.elements.each("//node") do |n|
      if n.attributes['LINK'] # has attached file
        if first_pass
          f= "#{dir}/#{n.attributes['LINK']}"
        else
          f= n.attributes['LINK']
        end
        next unless File.exists?(f)
        tt= REXML::Document.new(File.read(f).gsub("\n","")).root.elements["node"]
        make_folders_absolute(f,tt)
        tt.elements.each("node") do |tt_node|
          n.parent.insert_before n, tt_node
        end
        recheck= true
        n.parent.delete_element n
      end
    end
    first_pass = false
  end
  return t
end
get_default_role() click to toggle source
# File lib/mindapp2/helpers.rb, line 60
def get_default_role
  default_role= Mindapp::Role.where(:code =>'default').first
  return default_role ? default_role.name.to_s : ''
end
get_ip() click to toggle source
# File lib/mindapp2/helpers.rb, line 57
def get_ip
  request.env['HTTP_X_FORWARDED_FOR'] || request.env['REMOTE_ADDR']
end
get_option(opt, runseq=@runseq) click to toggle source
# File lib/mindapp2/helpers.rb, line 44
def get_option(opt, runseq=@runseq)
  xml= REXML::Document.new(runseq.xml).root
  url=''
  xml.each_element('///node') do |n|
    text= n.attributes['TEXT']
    url= text if text =~/^#{opt}:\s*/
  end
  c, h= url.split(':', 2)
  opt= h ? h.strip : false
end
get_option_xml(opt, xml) click to toggle source
# File lib/mindapp2/helpers.rb, line 317
def get_option_xml(opt, xml)
  if xml
    url=''
    xml.each_element('node') do |n|
      text= n.attributes['TEXT']
      url= text if text =~/^#{opt}/
    end
    return nil if url.blank?
    c, h= url.split(':', 2)
    opt= h ? h.strip : true
  else
    return nil
  end
end
listed(node) click to toggle source
# File lib/mindapp2/helpers.rb, line 331
def listed(node)
  icons=[]
  node.each_element("icon") do |nn|
    icons << nn.attributes["BUILTIN"]
  end
  return !icons.include?("closed")
end
login?() click to toggle source
# File lib/mindapp2/helpers.rb, line 306
def login?
  session[:user_id] != nil
end
ma_comment?(s) click to toggle source
# File lib/mindapp2/helpers.rb, line 54
def ma_comment?(s)
  s[0]==35
end
ma_log(message) click to toggle source
# File lib/mindapp2/helpers.rb, line 112
def ma_log(message)
    Mindapp::Notice.create :message => ERB::Util.html_escape(message.gsub("`","'")),
      :unread=> true, :ip=> ($ip || request.env["REMOTE_ADDR"])
  # if session[:user_id]
  #   Mindapp::Notice.create :message => ERB::Util.html_escape(message.gsub("`","'")),
  #     :user_id => $user.id, :unread=> true, :ip=>request.env["REMOTE_ADDR"]
  # else
  #   Mindapp::Notice.create :message => ERB::Util.html_escape(message.gsub("`","'")),
  #     :unread=> true, :ip=>request.env["REMOTE_ADDR"]
  # end
end
Also aliased as: ma_notice
ma_notice(message)
Alias for: ma_log
ma_secured?(node) click to toggle source
# File lib/mindapp2/helpers.rb, line 338
def ma_secured?(node)
  icons=[]
  node.each_element("icon") do |nn|
    icons << nn.attributes["BUILTIN"]
  end
  return icons.include?("password")
end
markdown(text) click to toggle source

methods from application_helper

# File lib/mindapp2/helpers.rb, line 127
def markdown(text)
  erbified = ERB.new(text.html_safe).result(binding)
  red = Redcarpet::Markdown.new(Redcarpet::Render::HTML, :autolink => true, :space_after_headers => true)
  red.render(erbified).html_safe
end
name2camel(s) click to toggle source
# File lib/mindapp2/helpers.rb, line 69
def name2camel(s)
  s.gsub(' ','_').camelcase
end
name2code(s) click to toggle source
# File lib/mindapp2/helpers.rb, line 64
def name2code(s)
  # rather not ignore # symbol cause it could be comment
  code, name = s.split(':')
  code.downcase.strip.gsub(' ','_').gsub(/[^#_\/a-zA-Z0-9]/,'')
end
negate(s) click to toggle source
# File lib/mindapp2/helpers.rb, line 372
def negate(s)
  return s =~ /[n|no|f|false]/i ? true : false
end
own_xmain?() click to toggle source
# File lib/mindapp2/helpers.rb, line 309
def own_xmain?
  if $xmain
    return current_ma_user.id==$xvars['user_id']
  else
    # if eval on first step would return true so user can start service
    return true
  end
end
process_services() click to toggle source

def handle_ma_notice

if Mindapp::Notice.recent.count>0
  notice= Mindapp::Notice.recent.last
  notice.update_attribute :unread, false
  "<script>notice('#{notice.message}');</script>"
else
  ""
end

end

# File lib/mindapp2/helpers.rb, line 200
def process_services
  # todo: persist mm_md5
  xml= @app||get_app
  if defined? session
    md5= Digest::MD5.hexdigest(xml.to_s)
    if session[:mm_md5]
      return if session[:mm_md5]==md5
    else
      session[:mm_md5]= md5
    end
  end
  protected_services = []
  protected_modules = []
  mseq= 0
  @services= xml.elements["//node[@TEXT='services']"] || REXML::Document.new
  @services.each_element('node') do |m|
    ss= m.attributes["TEXT"]
    code, name= ss.split(':', 2)
    next if code.blank?
    next if code.comment?
    module_code= code.to_code
    # create or update to GmaModule
    ma_module= Mindapp::Module.find_or_create_by :code=>module_code
    ma_module.update_attributes :uid=>ma_module.id.to_s
    protected_modules << ma_module.uid
    name = module_code if name.blank?
    ma_module.update_attributes :name=> name.strip, :seq=> mseq
    mseq += 1
    seq= 0
    m.each_element('node') do |s|
      service_name= s.attributes["TEXT"].to_s
      scode, sname= service_name.split(':', 2)
      sname ||= scode; sname.strip!
      scode= scode.to_code
      if scode=="role"
        ma_module.update_attribute :role, sname
        next
      end
      if scode.downcase=="link"
        role= get_option_xml("role", s) || ""
        rule= get_option_xml("rule", s) || ""
        ma_service= Mindapp::Service.find_or_create_by :module_code=> ma_module.code, :code=> scode, :name=> sname
        ma_service.update_attributes :xml=>s.to_s, :name=>sname,
          :list=>listed(s), :ma_secured=>ma_secured?(s),
          :module_id=>ma_module.id, :seq => seq,
          :confirm=> get_option_xml("confirm", xml),
          :role => role, :rule => rule, :uid=> ma_service.id.to_s
        seq += 1
        protected_services << ma_service.uid
      else
        # normal service
        step1 = s.elements['node']
        role= get_option_xml("role", step1) || ""
        rule= get_option_xml("rule", step1) || ""
        ma_service= Mindapp::Service.find_or_create_by :module_code=> ma_module.code, :code=> scode
        ma_service.update_attributes :xml=>s.to_s, :name=>sname,
          :list=>listed(s), :ma_secured=>ma_secured?(s),
          :module_id=>ma_module.id, :seq => seq,
          :confirm=> get_option_xml("confirm", xml),
          :role => role, :rule => rule, :uid=> ma_service.id.to_s
        seq += 1
        protected_services << ma_service.uid
      end
    end
  end
  Mindapp::Module.not_in(:uid=>protected_modules).delete_all
  Mindapp::Service.not_in(:uid=>protected_services).delete_all
end
read_binary(path) click to toggle source
# File lib/mindapp2/helpers.rb, line 38
def read_binary(path)
  File.open path, "rb" do |f| f.read end
end
redirect_to_root() click to toggle source
# File lib/mindapp2/helpers.rb, line 41
def redirect_to_root
  redirect_to root_path
end
refresh_to(url='/', option={}) click to toggle source
# File lib/mindapp2/helpers.rb, line 32
def refresh_to(url='/', option={})
  if option[:alert]
    ma_log option[:alert]
  end
  render inline: "<script>window.location.replace('#{url}')</script>"
end
role_name(code) click to toggle source
# File lib/mindapp2/helpers.rb, line 151
def role_name(code)
  role= Mindapp::Role.where(code:code).first
  return role ? role.name : ""
end
set_global() click to toggle source
# File lib/mindapp2/helpers.rb, line 75
def set_global
  $xmain= @xmain ; $runseq = @runseq ; $user = current_ma_user ; $xvars= @xmain.xvars; $ip = request.env["REMOTE_ADDR"]
end
status_icon(status) click to toggle source
# File lib/mindapp2/helpers.rb, line 135
def status_icon(status)
  case status
  when 'R'
    image_tag 'user.png'
  when 'F'
    image_tag 'tick.png'
  when 'I'
    image_tag 'control_play.png'
  when 'E'
    image_tag 'logout.png'
  when 'X'
    image_tag 'cross.png'
  else
    image_tag 'cancel.png'
  end
end
step(s, total) click to toggle source
# File lib/mindapp2/helpers.rb, line 164
def step(s, total) # square text
  s = (s==0)? 1: s.to_i
  total = total.to_i
  out ="<div class='step'>"
  (s-1).times {|ss| out += "<span class='steps_done'>#{(ss+1)}</span>" }
  out += %Q@<span class='step_now' >@
  out += s.to_s
  out += "</span>"
  out += %Q@@
  for i in s+1..total
    out += "<span class='steps_more'>#{i}</span>"
  end
  out += "</div>"
  out.html_safe
end
true_action?(s) click to toggle source
# File lib/mindapp2/helpers.rb, line 72
def true_action?(s)
  %w(call ws redirect invoke email).include? s
end
ui_action?(s) click to toggle source
# File lib/mindapp2/helpers.rb, line 188
def ui_action?(s)
  %w(form output mail pdf).include? s
end
uncomment(s) click to toggle source
# File lib/mindapp2/helpers.rb, line 155
def uncomment(s)
  s.sub(/^#\s/,'')
end