Module: Mindapp::Helpers

Includes:
REXML
Included in:
ApplicationHelper
Defined in:
lib/mindapp/helpers.rb

Instance Method Summary collapse

Instance Method Details

#affirm(s) ⇒ Object



370
371
372
# File 'lib/mindapp/helpers.rb', line 370

def affirm(s)
  return s =~ /[y|yes|t|true]/i ? true : false
end

#ajax?(s) ⇒ Boolean

Returns:

  • (Boolean)


162
163
164
# File 'lib/mindapp/helpers.rb', line 162

def ajax?(s)
  return s.match('file_field') ? false : true
end

#align_text(s, pixel = 3) ⇒ Object



133
134
135
# File 'lib/mindapp/helpers.rb', line 133

def align_text(s, pixel=3)
  "<span style='position:relative; top:-#{pixel}px;'>#{s}</span>".html_safe
end

#authorize?Boolean

use in pending tasks

Returns:

  • (Boolean)


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/mindapp/helpers.rb', line 79

def authorize? # use in pending tasks
  @runseq= @xmain.runseqs.find @xmain.current_runseq
  return false unless @runseq
  @user = current_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?Boolean

use when initialize new transaction

Returns:

  • (Boolean)


94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/mindapp/helpers.rb', line 94

def authorize_init? # use when initialize new transaction
  # check module role
  mrole = @service.module.role
  return false if mrole && !current_user
  return false if mrole && !current_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_user
    return role.blank?
  else
    return false unless current_user.role
    return current_user.has_role(role)
  end
end

#b(s) ⇒ Object

methods from application_controller



7
8
9
# File 'lib/mindapp/helpers.rb', line 7

def b(s)
  "<b>#{s}</b>".html_safe
end

#code_div(s) ⇒ Object



159
160
161
# File 'lib/mindapp/helpers.rb', line 159

def code_div(s)
  "<pre style='background-color: #efffef;'><code class='ruby' lang='ruby'>    #{s}</code></pre>".html_safe
end

#code_text(s) ⇒ Object

old def code(s)



30
31
32
# File 'lib/mindapp/helpers.rb', line 30

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) ⇒ Boolean

Returns:

  • (Boolean)


297
298
299
# File 'lib/mindapp/helpers.rb', line 297

def controller_exists?(modul)
  File.exists? "#{Rails.root}/app/controllers/#{modul}_controller.rb"
end

#current_userObject

methods that I don’t know where they came from



182
183
184
185
186
187
188
# File 'lib/mindapp/helpers.rb', line 182

def current_user
  if session[:user_id]
    return @user ||= User.find(session[:user_id]['$oid'])
  else
    return nil
  end
end

#date_thai(d = Time.now, options = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/mindapp/helpers.rb', line 10

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) ⇒ Object



300
301
302
303
304
305
306
# File 'lib/mindapp/helpers.rb', line 300

def dup_hash(a)
  h = Hash.new(0)
  a.each do |aa|
    h[aa] += 1
  end
  return h
end

#freemind2action(s) ⇒ Object



346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# File 'lib/mindapp/helpers.rb', line 346

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 'pencil'
    'output'
  when 'mail'
    'mail'
  end
end

#get_appObject



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/mindapp/helpers.rb', line 269

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_roleObject



61
62
63
64
# File 'lib/mindapp/helpers.rb', line 61

def get_default_role
  default_role= Mindapp::Role.where(:code =>'default').first
  return default_role ? default_role.name.to_s : ''
end

#get_ipObject



58
59
60
# File 'lib/mindapp/helpers.rb', line 58

def get_ip
  request.env['HTTP_X_FORWARDED_FOR'] || request.env['REMOTE_ADDR']
end

#get_option(opt, runseq = @runseq) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/mindapp/helpers.rb', line 45

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) ⇒ Object



318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/mindapp/helpers.rb', line 318

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


27
28
29
# File 'lib/mindapp/helpers.rb', line 27

def link_to_blank(body, url_options = {}, html_options = {})
  link_to(body, url_options, html_options.merge(target: "_blank"))
end

#listed(node) ⇒ Object



332
333
334
335
336
337
338
# File 'lib/mindapp/helpers.rb', line 332

def listed(node)
  icons=[]
  node.each_element("icon") do |nn|
    icons << nn.attributes["BUILTIN"]
  end
  return !icons.include?("closed")
end

#login?Boolean

Returns:

  • (Boolean)


307
308
309
# File 'lib/mindapp/helpers.rb', line 307

def login?
  session[:user_id] != nil
end

#ma_comment?(s) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/mindapp/helpers.rb', line 55

def ma_comment?(s)
  s[0]==35
end

#ma_log(message) ⇒ Object Also known as: ma_notice



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/mindapp/helpers.rb', line 113

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=> env["REMOTE_ADDR"]
  # else
  #   Mindapp::Notice.create :message => ERB::Util.html_escape(message.gsub("`","'")),
  #     :unread=> true, :ip=> env["REMOTE_ADDR"]
  # end
end

#markdown(text) ⇒ Object

methods from application_helper



128
129
130
131
132
# File 'lib/mindapp/helpers.rb', line 128

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) ⇒ Object



70
71
72
# File 'lib/mindapp/helpers.rb', line 70

def name2camel(s)
  s.gsub(' ','_').camelcase
end

#name2code(s) ⇒ Object



65
66
67
68
69
# File 'lib/mindapp/helpers.rb', line 65

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) ⇒ Object



373
374
375
# File 'lib/mindapp/helpers.rb', line 373

def negate(s)
  return s =~ /[n|no|f|false]/i ? true : false
end

#own_xmain?Boolean

Returns:

  • (Boolean)


310
311
312
313
314
315
316
317
# File 'lib/mindapp/helpers.rb', line 310

def own_xmain?
  if $xmain
    return current_user.id==$xvars['user_id']
  else
    # if eval on first step would return true so user can start service
    return true
  end
end

#process_servicesObject

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



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
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/mindapp/helpers.rb', line 201

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), :secured=>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), :secured=>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) ⇒ Object



39
40
41
# File 'lib/mindapp/helpers.rb', line 39

def read_binary(path)
  File.open path, "rb" do |f| f.read end
end

#redirect_to_rootObject



42
43
44
# File 'lib/mindapp/helpers.rb', line 42

def redirect_to_root
  redirect_to root_path
end

#refresh_to(url = '/', option = {}) ⇒ Object



33
34
35
36
37
38
# File 'lib/mindapp/helpers.rb', line 33

def refresh_to(url='/', option={})
  if option[:alert]
    ma_log option[:alert]
  end
  render :text => "<script>window.location.replace('#{url}')</script>"
end

#role_name(code) ⇒ Object



152
153
154
155
# File 'lib/mindapp/helpers.rb', line 152

def role_name(code)
  role= Mindapp::Role.where(code:code).first
  return role ? role.name : ""
end

#secured?(node) ⇒ Boolean

Returns:

  • (Boolean)


339
340
341
342
343
344
345
# File 'lib/mindapp/helpers.rb', line 339

def secured?(node)
  icons=[]
  node.each_element("icon") do |nn|
    icons << nn.attributes["BUILTIN"]
  end
  return icons.include?("password")
end

#set_globalObject



76
77
78
# File 'lib/mindapp/helpers.rb', line 76

def set_global
  $xmain= @xmain ; $runseq = @runseq ; $user = current_user ; $xvars= @xmain.xvars; $ip = request.env["REMOTE_ADDR"]
end

#status_icon(status) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/mindapp/helpers.rb', line 136

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) ⇒ Object

square text



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/mindapp/helpers.rb', line 165

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) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/mindapp/helpers.rb', line 73

def true_action?(s)
  %w(call ws redirect invoke email).include? s
end

#ui_action?(s) ⇒ Boolean

Returns:

  • (Boolean)


189
190
191
# File 'lib/mindapp/helpers.rb', line 189

def ui_action?(s)
  %w(form output mail pdf).include? s
end

#uncomment(s) ⇒ Object



156
157
158
# File 'lib/mindapp/helpers.rb', line 156

def uncomment(s)
  s.sub(/^#\s/,'')
end