Class: MindappController

Inherits:
ApplicationController
  • Object
show all
Defined in:
lib/generators/mindapp/templates/app/controllers/mindapp_controller.rb

Instance Method Summary collapse

Instance Method Details

#ajax_noticeObject



27
28
29
30
31
32
33
34
35
# File 'lib/generators/mindapp/templates/app/controllers/mindapp_controller.rb', line 27

def ajax_notice
  if notice=Mindapp::Notice.recent(current_user, env["REMOTE_ADDR"])
    notice.update_attribute :unread, false
    js = "notice('#{notice.message}');"
  else
    js = ""
  end
  render :text=> "<script>#{js}</script>"
end

#cancelObject



15
16
17
18
19
20
21
22
# File 'lib/generators/mindapp/templates/app/controllers/mindapp_controller.rb', line 15

def cancel
  Mindapp::Xmain.find(params[:id]).update_attributes :status=>'X'
  if params[:return]
    redirect_to params[:return]
  else
    redirect_to action:"pending"
  end
end

#clear_xmainsObject



23
24
25
26
# File 'lib/generators/mindapp/templates/app/controllers/mindapp_controller.rb', line 23

def clear_xmains
    Mindapp::Xmain.where(:status =>{'$in'=>['R','I']}).update_all(:status=>'X')
    redirect_to action:"pending"
end

#docObject

generate documentation for application



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/generators/mindapp/templates/app/controllers/mindapp_controller.rb', line 300

def doc
  require 'rdoc'
  @app= get_app
  @intro = File.read('README.md')
  @print= "<div align='right'><img src='/assets/printer.png'/> <a href='/mindapp/doc_print' target='_blank'/>พิมพ์</a></div>"
  doc= render_to_string 'doc.md', :layout => false
  html= Maruku.new(doc).to_html
  File.open('public/doc.html','w') {|f| f.puts html }
  respond_to do |format|
    format.html {
      render :text=> @print+html, :layout => 'layouts/_page'
      # render :text=> Maruku.new(doc).to_html, :layout => false
    # format.html {
    #   h = RDoc::Markup::ToHtml.new
    #   render :text=> h.convert(doc), :layout => 'layouts/_page'
    }
    format.pdf  {
      latex= Maruku.new(doc).to_latex
      File.open('tmp/doc.md','w') {|f| f.puts doc}
      File.open('tmp/doc.tex','w') {|f| f.puts latex}
      # system('pdflatex tmp/doc.tex ')
      # send_file( 'tmp/doc.pdf', :type => ‘application/pdf’,
        # :disposition => ‘inline’, :filename => 'doc.pdf')
      render :text=>'done'
    }
  end
end

#doc_printObject



296
297
298
# File 'lib/generators/mindapp/templates/app/controllers/mindapp_controller.rb', line 296

def doc_print
  render :file=>'public/doc.html', :layout=>'layouts/print'
end

#documentObject

handle uploaded image



328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/generators/mindapp/templates/app/controllers/mindapp_controller.rb', line 328

def document
  doc = Mindapp::Doc.find params[:id]
  if doc.cloudinary
      require 'net/http'
      require "uri"
      uri = URI.parse(doc.url)
      data = Net::HTTP.get_response(uri)
      send_data(data.body, :filename=>doc.filename, :type=>doc.content_type, :disposition=>"inline")
  else
      data= read_binary(doc.url)
      send_data(data, :filename=>doc.filename, :type=>doc.content_type, :disposition=>"inline")
    end
end

#end_action(next_runseq = nil) ⇒ Object



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
# File 'lib/generators/mindapp/templates/app/controllers/mindapp_controller.rb', line 223

def end_action(next_runseq = nil)
  #    @runseq.status='F' unless @runseq_not_f
  @xmain.xvars= @xvars
  @xmain.status= 'R' # running
  @xmain.save
  @runseq.status='F'
  @runseq.user= current_user
  @runseq.stop= Time.now
  @runseq.save
  next_runseq= @xmain.runseqs.where(:rstep=> @runseq.rstep+1).first unless next_runseq
  if @end_job || !next_runseq # job finish
    @xmain.xvars= @xvars
    @xmain.status= 'F' unless @xmain.status== 'E' # finish
    @xmain.stop= Time.now
    @xmain.save
    if @xvars['p']['return']
      redirect_to @xvars['p']['return'] and return
    else
      if @user
        redirect_to :action=>'pending' and return
      else
        redirect_to_root and return
      end
    end
  else
    @xmain.update_attribute :current_runseq, next_runseq.id
    redirect_to :action=>'run', :id=>@xmain.id and return
  end
end

#end_formObject



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/generators/mindapp/templates/app/controllers/mindapp_controller.rb', line 205

def end_form
  init_vars(params[:xmain_id])
  eval "@xvars[@runseq.code] = {} unless @xvars[@runseq.code]"
  params.each { |k,v|
    if params[k].respond_to? :original_filename
      get_image(k, params[k])
    elsif params[k].is_a?(Hash)
      eval "@xvars[@runseq.code][k] = v"
      params[k].each { |k1,v1|
        next unless v1.respond_to?(:original_filename)
        get_image1(k, k1, params[k][k1])
      }
    else
      eval "@xvars[@runseq.code][k] = v"
    end
  }
  end_action
end

#end_outputObject



201
202
203
204
# File 'lib/generators/mindapp/templates/app/controllers/mindapp_controller.rb', line 201

def end_output
  init_vars(params[:xmain_id])
  end_action
end

#err404Object



366
367
368
369
370
371
# File 'lib/generators/mindapp/templates/app/controllers/mindapp_controller.rb', line 366

def err404
  # ma_log 'ERROR', 'main/err404'
  flash[:notice] = "We're sorry, but something went wrong. We've been notified about this issue and we'll take a look at it shortly."
  ma_log "We're sorry, but something went wrong. We've been notified about this issue and we'll take a look at it shortly."
  redirect_to '/'
end

#err500Object



372
373
374
375
376
377
# File 'lib/generators/mindapp/templates/app/controllers/mindapp_controller.rb', line 372

def err500
  # ma_log 'ERROR', 'main/err500'
  flash[:notice] = "We're sorry, but something went wrong. We've been notified about this issue and we'll take a look at it shortly."
  ma_log "We're sorry, but something went wrong. We've been notified about this issue and we'll take a look at it shortly."
  redirect_to '/'
end

#error_logsObject



8
9
10
# File 'lib/generators/mindapp/templates/app/controllers/mindapp_controller.rb', line 8

def error_logs
  @xmains = Mindapp::Xmain.in(status:['E']).desc(:created_at).page(params[:page]).per(10)
end

#get_image(key, params) ⇒ Object

process images from first level



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/generators/mindapp/templates/app/controllers/mindapp_controller.rb', line 253

def get_image(key, params)
    doc = Mindapp::Doc.create(
      :name=> key.to_s,
      :xmain=> @xmain.id,
      :runseq=> @runseq.id,
      :filename=> params.original_filename,
      :content_type => params.content_type || 'application/zip',
      :data_text=> '',
      :display=>true,
      :secured => @xmain.service.secured )
  if defined?(IMAGE_LOCATION)
    filename = "#{IMAGE_LOCATION}/f#{Param.gen(:asset_id)}"
    File.open(filename,"wb") { |f| f.write(params.read) }
    # File.open(filename,"wb") { |f| f.puts(params.read) }
    eval "@xvars[@runseq.code][key] = '#{url_for(:action=>'document', :id=>doc.id, :only_path => true )}' "
    doc.update_attributes :url => filename, :basename => File.basename(filename), :cloudinary => false
  else
    result = Cloudinary::Uploader.upload(params)
    eval %Q{ @xvars[@runseq.code][key] = '#{result["url"]}' }
    doc.update_attributes :url => result["url"], :basename => File.basename(result["url"]), :cloudinary => true
  end
end

#get_image1(key, key1, params) ⇒ Object

process images from second level, e.g,, fields_for



276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/generators/mindapp/templates/app/controllers/mindapp_controller.rb', line 276

def get_image1(key, key1, params)
  doc = Mindapp::Doc.create(
    :name=> "#{key}_#{key1}",
    :xmain=> @xmain.id,
    :runseq=> @runseq.id,
    :filename=> params.original_filename,
    :content_type => params.content_type || 'application/zip',
    :data_text=> '',
    :display=>true, :secured => @xmain.service.secured )
  if defined?(IMAGE_LOCATION)
    filename = "#{IMAGE_LOCATION}/f#{Param.gen(:asset_id)}"
    File.open(filename,"wb") { |f| f.write(params.read) }
    eval "@xvars[@runseq.code][key][key1] = '#{url_for(:action=>'document', :id=>doc.id, :only_path => true)}' "
    doc.update_attributes :url => filename, :basename => File.basename(filename), :cloudinary => false
  else
    result = Cloudinary::Uploader.upload(params)
    eval %Q{ @xvars[@runseq.code][key][key1] = '#{result["url"]}' }
    doc.update_attributes :url => result["url"], :basename => File.basename(result["url"]), :cloudinary => true
  end
end

#helpObject



352
353
# File 'lib/generators/mindapp/templates/app/controllers/mindapp_controller.rb', line 352

def help
end

#indexObject



3
4
# File 'lib/generators/mindapp/templates/app/controllers/mindapp_controller.rb', line 3

def index
end

#initObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/generators/mindapp/templates/app/controllers/mindapp_controller.rb', line 36

def init
  module_code, code = params[:s].split(":")
  @service= Mindapp::Service.where(:module_code=> module_code, :code=> code).first
  # @service= Mindapp::Service.where(:module_code=> params[:module], :code=> params[:service]).first
  if @service && authorize_init?
    xmain = create_xmain(@service)
    result = create_runseq(xmain)
    unless result
      message = "cannot find action for xmain #{xmain.id}"
      ma_log("ERROR", message)
      flash[:notice]= message
      redirect_to "pending" and return
    end
    xmain.update_attribute(:xvars, @xvars)
    xmain.runseqs.last.update_attribute(:end,true)
    redirect_to :action=>'run', :id=>xmain.id
  else
    refresh_to "/", :alert => "Error: cannot process"
  end
end

#logsObject



5
6
7
# File 'lib/generators/mindapp/templates/app/controllers/mindapp_controller.rb', line 5

def logs
  @xmains = Mindapp::Xmain.all.desc(:created_at).page(params[:page]).per(10)
end

#pendingObject



11
12
13
14
# File 'lib/generators/mindapp/templates/app/controllers/mindapp_controller.rb', line 11

def pending
  @title= "Pending Tasks"
  @xmains = Mindapp::Xmain.in(status:['R','I']).asc(:created_at)
end

#runObject



56
57
58
59
60
61
62
63
64
# File 'lib/generators/mindapp/templates/app/controllers/mindapp_controller.rb', line 56

def run
  init_vars(params[:id])
  if authorize?
    # session[:full_layout]= false
    redirect_to(:action=>"run_#{@runseq.action}", :id=>@xmain.id)
  else
    redirect_to_root
  end
end

#run_doObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/generators/mindapp/templates/app/controllers/mindapp_controller.rb', line 120

def run_do
  init_vars(params[:id])
  @runseq.start ||= Time.now
  @runseq.status= 'R' # running
  $runseq_id= @runseq.id
  $user_id= current_user.try(:id)
  set_global
  controller = Kernel.const_get(@xvars['custom_controller']).new
  result = controller.send(@runseq.code)
  init_vars_by_runseq($runseq_id)
  @xvars = $xvars
  @xvars[@runseq.code.to_sym]= result.to_s
  @xvars['current_step']= @runseq.rstep
  @runseq.status= 'F' #finish
  @runseq.stop= Time.now
  @runseq.save
  end_action
rescue => e
  @xmain.status='E'
  @xvars['error']= e.to_s+e.backtrace.to_s
  @xmain.xvars= $xvars
  @xmain.save
  @runseq.status= 'F' #finish
  @runseq.stop= Time.now
  @runseq.save
  refresh_to "/", :alert => "ขออภัย เกิดข้อผิดพลาดในรหัสการดำเนินงาน #{@xmain.id} #{@xvars['error']}"
end

#run_formObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/generators/mindapp/templates/app/controllers/mindapp_controller.rb', line 65

def run_form
  init_vars(params[:id])
  if authorize?
    if ['F', 'X'].include? @xmain.status
      redirect_to_root
    else
      service= @xmain.service
      if service
        @title= "Transaction ID #{@xmain.xid}: #{@xmain.name} / #{@runseq.name}"
        fhelp= "app/views/#{service.module.code}/#{service.code}/#{@runseq.code}.md"
        @help = File.read(fhelp) if File.exists?(fhelp)
        f= "app/views/#{service.module.code}/#{service.code}/#{@runseq.code}.html.erb"
        @ui= File.read(f)
      else
        # flash[:notice]= "ไม่สามารถค้นหาบริการที่ต้องการได้"
        ma_log "Error: Service not found"
        redirect_to_root
      end
    end
  else
    redirect_to_root
  end
end

#run_ifObject



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
# File 'lib/generators/mindapp/templates/app/controllers/mindapp_controller.rb', line 88

def run_if
  init_vars(params[:id])
  condition= eval(@runseq.code).to_s
  match_found= false
  if condition
    xml= REXML::Document.new(@runseq.xml).root
    next_runseq= nil
    text = xml.elements['//node/node'].attributes['TEXT']
    match, name= text.split(':',2)
    label= name2code(name.strip)
    if condition==match
      if label=="end"
        @end_job= true
      else
        next_runseq= @xmain.runseqs.where(:code=> label, :action.ne=>'redirect').first
        match_found= true if next_runseq
        @runseq_not_f= false
      end
    end
  end
  unless match_found || @end_job
    next_runseq= @xmain.runseqs.where( rstep:(@xvars['current_step']+1) ).first
  end
  end_action(next_runseq)
end

#run_mailObject



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/generators/mindapp/templates/app/controllers/mindapp_controller.rb', line 181

def run_mail
  init_vars(params[:id])
  service= @xmain.service
  f= "app/views/#{service.module.code}/#{service.code}/#{@runseq.code}.html.erb"
  @ui= File.read(f).html_safe
  @doc= Mindapp::Doc.create :name=> @runseq.name,
    :content_type=>"mail", :data_text=> render_to_string(:inline=>@ui, :layout=>false),
    :xmain=>@xmain, :runseq=>@runseq, :user=>current_user,
    :ip=> get_ip, :service=>service, :display=>false,
    :secured => @xmain.service.secured
  eval "@xvars[:#{@runseq.code}] = url_for(:controller=>'mindapp', :action=>'document', :id=>@doc.id)"
  mail_from = get_option('from')
  # sender= render_to_string(:inline=>mail_from) if mail_from
  mail_to = get_option('to')
  recipients= render_to_string(:inline=>mail_to) if mail_to
  mail_subject = get_option('subject')
  subject= render_to_string(:inline=>mail_subject) || "#{@runseq.name}"
  MindappMailer.gmail(@doc.data_text, recipients, subject).deliver unless DONT_SEND_MAIL
  end_action
end

#run_outputObject



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
# File 'lib/generators/mindapp/templates/app/controllers/mindapp_controller.rb', line 147

def run_output
  init_vars(params[:id])
  service= @xmain.service
  disp= get_option("display")
  display = (disp && !affirm(disp)) ? false : true
  if service
    f= "app/views/#{service.module.code}/#{service.code}/#{@runseq.code}.html.erb"
    @ui= File.read(f)
    if Mindapp::Doc.where(:runseq_id=>@runseq.id).exists?
      @doc= Mindapp::Doc.where(:runseq_id=>@runseq.id).first
      @doc.update_attributes :data_text=> render_to_string(:inline=>@ui, :layout=>"utf8"),
        :xmain=>@xmain, :runseq=>@runseq, :user=>current_user,
        :ip=> get_ip, :service=>service, :display=>display,
        :secured => @xmain.service.secured
    else
      @doc= Mindapp::Doc.create :name=> @runseq.name,
        :content_type=>"output", :data_text=> render_to_string(:inline=>@ui, :layout=>"utf8"),
        :xmain=>@xmain, :runseq=>@runseq, :user=>current_user,
        :ip=> get_ip, :service=>service, :display=>display,
        :secured => @xmain.service.secured
    end
    @message = defined?(MSG_NEXT) ? MSG_NEXT : "Next &gt;"
    @message = "สิ้นสุดการทำงาน" if @runseq.end
    eval "@xvars[@runseq.code] = url_for(:controller=>'Mindapp', :action=>'document', :id=>@doc.id)"
  else
    # flash[:notice]= "ไม่สามารถค้นหาบริการที่ต้องการได้"
    ma_log "Error: service not found"
    redirect_to_root
  end
  #display= get_option("display")
  unless display
    end_action
  end
end

#run_redirectObject



113
114
115
116
117
118
119
# File 'lib/generators/mindapp/templates/app/controllers/mindapp_controller.rb', line 113

def run_redirect
  init_vars(params[:id])
  # next_runseq= @xmain.runseqs.first :conditions=>["id != ? AND code = ?",@runseq.id, @runseq.code]
  next_runseq= @xmain.runseqs.where(:id.ne=>@runseq.id, :code=>@runseq.code).first
  @xmain.current_runseq= next_runseq.id
  end_action(next_runseq)
end

#searchObject



354
355
356
357
358
359
360
361
362
363
364
365
# File 'lib/generators/mindapp/templates/app/controllers/mindapp_controller.rb', line 354

def search
  @q = params[:q] || params[:ma_search][:q] || ""
  @title = "ผลการค้นหา #{@q}"
  @backbtn= true
  @cache= true
  if @q.blank?
    redirect_to "/"
  else
    s= GmaSearch.create :q=>@q, :ip=> request.env["REMOTE_ADDR"]
    do_search
  end
end

#statusObject



341
342
343
344
345
346
347
348
349
350
351
# File 'lib/generators/mindapp/templates/app/controllers/mindapp_controller.rb', line 341

def status
  @xmain= Mindapp::Xmain.where(:xid=>params[:xid]).first
  @title= "Task number #{params[:xid]} #{@xmain.name}"
  @backbtn= true
  @xvars= @xmain.xvars
  # flash.now[:notice]= "รายการ #{@xmain.id} ได้ถูกยกเลิกแล้ว" if @xmain.status=='X'
  ma_log "Task #{@xmain.id} is cancelled" if @xmain.status=='X'
  # flash.now[:notice]= "transaction #{@xmain.id} was cancelled" if @xmain.status=='X'
rescue
  refresh_to "/", :alert => "Could not find task number <b> #{params[:xid]} </b>"
end