jinda 0.5.0 → 0.5.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (32) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +3 -3
  3. data/lib/generators/jinda/install_generator.rb +7 -1
  4. data/lib/generators/jinda/templates/README.md +1 -1
  5. data/lib/generators/jinda/templates/app/assets/stylesheets/app.scss +11 -5
  6. data/lib/generators/jinda/templates/app/assets/stylesheets/articles.scss +16 -6
  7. data/lib/generators/jinda/templates/app/controllers/concerns/jinda_general_concern.rb +179 -0
  8. data/lib/generators/jinda/templates/app/controllers/concerns/jinda_run_concern.rb +365 -0
  9. data/lib/generators/jinda/templates/app/controllers/jinda_org/docs_controller.rb +56 -0
  10. data/lib/generators/jinda/templates/app/controllers/jinda_org/jinda_controller.rb +29 -562
  11. data/lib/generators/jinda/templates/app/controllers/jinda_org/notes_controller.rb +6 -8
  12. data/lib/generators/jinda/templates/app/jinda/index.mm +71 -23
  13. data/lib/generators/jinda/templates/app/jinda/template/view.html.erb +22 -23
  14. data/lib/generators/jinda/templates/app/models/jinda/doc.rb +5 -0
  15. data/lib/generators/jinda/templates/app/views/articles/new_article/form_article.html.erb +2 -0
  16. data/lib/generators/jinda/templates/app/views/docs/doc_edit/doc_edit.html.erb +21 -0
  17. data/lib/generators/jinda/templates/app/views/docs/doc_edit/doc_select.html.erb +14 -0
  18. data/lib/generators/jinda/templates/app/views/docs/doc_new/doc_form.html.erb +26 -0
  19. data/lib/generators/jinda/templates/app/views/docs/doc_new/doc_form.md +36 -0
  20. data/lib/generators/jinda/templates/app/views/docs/doc_xedit/doc_edit.html.erb +21 -0
  21. data/lib/generators/jinda/templates/app/views/docs/edit/select_note.html.erb +14 -0
  22. data/lib/generators/jinda/templates/app/views/docs/index.haml +21 -0
  23. data/lib/generators/jinda/templates/app/views/docs/my.haml +27 -0
  24. data/lib/generators/jinda/templates/app/views/jinda/index.html.haml +3 -3
  25. data/lib/generators/jinda/templates/app/views/jinda/run_output.haml +3 -3
  26. data/lib/jinda/helpers.rb +205 -63
  27. data/lib/jinda/version.rb +1 -1
  28. metadata +15 -8
  29. data/lib/generators/jinda/templates/app/controllers/ctrs_controller.rb-gem-test +0 -2
  30. data/lib/jinda/ template/view.html.erb +0 -25
  31. data/lib/jinda/app/jinda/index.mm +0 -308
  32. data/lib/jinda/app/jinda/template/view.html.erb +0 -27
@@ -0,0 +1,56 @@
1
+ class DocsController < ApplicationController
2
+ before_action :load_doc, only: [:destroy]
3
+ before_action :load_doc_form, only: [:doc_update, :edit, :my]
4
+
5
+ def index
6
+ @documents = Jinda::Doc.desc(:created_at).page(params[:page]).per(10)
7
+ end
8
+
9
+ def edit
10
+ end
11
+
12
+ def my
13
+ @page_title = 'My Document'
14
+ end
15
+
16
+ def doc_update
17
+ # Instead of creaete, Doc record was created in form, when upload file
18
+
19
+ if Jinda::Doc.where(:runseq_id => $xvars["doc_form"]["runseq_id"]).exists?
20
+ @doc = Jinda::Doc.where(:runseq_id => $xvars["doc_form"]["runseq_id"]).first
21
+ @doc.update(description: $xvars["doc_form"]["description"],
22
+ category: $xvars["doc_form"]["jinda_doc"]["category"],
23
+ keywords: $xvars["doc_form"]["keywords"],
24
+ user_id: $xvars["user_id"]
25
+ )
26
+ else
27
+ # create here
28
+ # Todo
29
+ end
30
+ end
31
+
32
+ def destroy
33
+ # duplicated from jinda_controller
34
+ # Expected to use in jinda_controller
35
+ current_ma_user = User.where(:auth_token => cookies[:auth_token]).first if cookies[:auth_token]
36
+
37
+ if Rails.env.test? #Temp solution until fix test of current_ma_user
38
+ current_ma_user = $xvars["current_ma_user"]
39
+ #current_ma_user = @doc.user
40
+ end
41
+
42
+ if current_ma_user.role.upcase.split(',').include?("A") || current_ma_user == @doc.user
43
+ @doc.destroy
44
+ end
45
+ redirect_to :action=>'my'
46
+ end
47
+
48
+ private
49
+
50
+ def load_doc_form
51
+ @docs = Jinda::Doc.all.desc(:created_at).page(params[:page]).per(10)
52
+ end
53
+ def load_doc
54
+ @doc = Jinda::Doc.find(params[:doc_id])
55
+ end
56
+ end
@@ -1,77 +1,19 @@
1
1
  # -*- encoding : utf-8 -*-
2
- ###################################################### @runseq ####################################################
3
- # @runseq => #<Jinda::Runseq _id: 5df31912a54d758417a7afc9,
4
- # created_at: 2019-12-13 04:52:34 UTC,
5
- # updated_at: 2019-12-13 04:52:43 UTC,
6
- # user_id: nil,
7
- # xmain_id: BSON::ObjectId('5df31912a54d758417a7afc7'),
8
- # action: "do",
9
- # status: "R",
10
- # code: "create",
11
- # name: "Create Article",
12
- # role: "",
13
- # rule: "true",
14
- # rstep: 2,
15
- # form_step: 1,
16
- # start: 2019-12-13 04:52:43 UTC,
17
- # stop: nil,
18
- # end: true,
19
- # xml: "<node CREATED='1493419491125' ID='ID_1687683396' MODIFIED='1493483244848' TEXT='create: Create Article'><icon BUILTIN='bookmark'/></node>",
20
- # ip: nil>
21
-
22
- ######################################################################################################################
23
2
 
24
3
  class JindaController < ApplicationController
25
- def index
26
- end
27
- def logs
28
- @xmains = Jinda::Xmain.all.desc(:created_at).page(params[:page]).per(10)
29
- end
30
- def error_logs
31
- @xmains = Jinda::Xmain.in(status:['E']).desc(:created_at).page(params[:page]).per(10)
32
- end
33
- def notice_logs
34
- @notices= Jinda::Notice.desc(:created_at).page(params[:page]).per(10)
35
- end
36
- def pending
37
- @title= "Pending Tasks"
38
- @xmains = Jinda::Xmain.in(status:['R','I']).asc(:created_at)
39
- end
40
- def cancel
41
- Jinda::Xmain.find(params[:id]).update_attributes :status=>'X'
42
- if params[:return]
43
- redirect_to params[:return]
44
- else
45
- redirect_to action:"pending"
46
- end
47
- end
48
- def clear_xmains
49
- Jinda::Xmain.where(:status =>{'$in'=>['R','I']}).update_all(:status=>'X')
50
- redirect_to action:"pending"
51
- end
52
- def ajax_notice
53
- if notice=Jinda::Notice.recent(current_ma_user, request.env["REMOTE_ADDR"])
54
- notice.update_attribute :unread, false
55
- js = "notice('#{notice.message}');"
56
- else
57
- js = ""
58
- end
59
- render plain: "<script>#{js}</script>"
60
- end
61
- ####################################################################################################]
62
- # prepare xmain.runseq eg: how many form_step or total_step and step properties check if authorized
63
- ####################################################################################################]
4
+ include JindaRunConcern
5
+ include JindaGeneralConcern
64
6
  # view menu by user selected what service (module and code) to run (not all services like menu did
65
7
  # Its only one service
8
+
66
9
  def init
67
10
  module_code, code = params[:s].split(":")
68
11
  @service= Jinda::Service.where(:module_code=> module_code, :code=> code).first
69
- # @service= Jinda::Service.where(:module_code=> params[:module], :code=> params[:service]).first
70
12
  if @service && authorize_init?
71
13
  xmain = create_xmain(@service)
72
14
  result = create_runseq(xmain)
73
15
  unless result
74
- message = "cannot find action for xmain #{xmain.id}"
16
+ message = "Node missing action icon: cannot find action for xmain #{xmain.id}, the node required action(icon) in freemind eg: form, list, method"
75
17
  ma_log(message)
76
18
  flash[:notice]= message
77
19
  redirect_to "pending" and return
@@ -79,16 +21,39 @@ class JindaController < ApplicationController
79
21
  xmain.update_attribute(:xvars, @xvars)
80
22
  xmain.runseqs.last.update_attribute(:end,true)
81
23
  #Above line cause error update_attribute in heroku shown in logs and it was proposed to fixed in github:'kul1/g241502'
24
+ # Main action run with :id
82
25
  redirect_to :action=>'run', :id=>xmain.id
26
+
83
27
  else
84
28
  refresh_to "/", :alert => "Error: cannot process"
29
+ error_run_xmain = "Error_run_xmain"
30
+ ma_log(error_run_xmain)
85
31
  end
86
32
  end
87
- ####################################################################################################]
33
+
34
+ ########################################################################]
88
35
  # run if, form, mail, output etc depend on icon in freemind
89
36
  # action from @r.action == do, form, if, output
90
37
  # Then will call def run_do, run_form, run_if, run_output
91
- ####################################################################################################]
38
+ ########################################################################]
39
+ #
40
+ # run
41
+ # init_vars
42
+ # To get all var from global and runseq, action
43
+ # (@runseq.action)
44
+ #
45
+ # run_do, run_form, run_if, run_output
46
+ # run_do => controller => action eg: update, create, document
47
+ # run_form =>
48
+ # run_form.haml => - next step => def end_form
49
+ # - next_step = end_form
50
+ # run_if =>
51
+ # run_output =>
52
+ #
53
+ # end_action
54
+ # => save local var to database :current_runseq
55
+ # :current_runseq => next_runseq
56
+ #
92
57
  def run
93
58
  init_vars(params[:id])
94
59
  if authorize?
@@ -98,502 +63,4 @@ class JindaController < ApplicationController
98
63
  redirect_to_root
99
64
  end
100
65
  end
101
- def run_form
102
- init_vars(params[:id])
103
- if authorize?
104
- if ['F', 'X'].include? @xmain.status
105
- redirect_to_root
106
- else
107
- service= @xmain.service
108
- ###############################################################################################
109
- # Run View Form f created template by jinda rake follow freemind mm file
110
- ###############################################################################################
111
- if service
112
- @title= "Transaction ID #{@xmain.xid}: #{@xmain.name} / #{@runseq.name}"
113
- fhelp= "app/views/#{service.module.code}/#{service.code}/#{@runseq.code}.md"
114
- ###############################################################################################
115
- # Check if help file available for this form
116
- ###############################################################################################
117
- @help = File.read(fhelp) if File.exists?(fhelp)
118
- f= "app/views/#{service.module.code}/#{service.code}/#{@runseq.code}.html.erb"
119
- if File.file?(f)
120
- @ui= File.read(f)
121
- else
122
- # flash[:notice]= "Error: Can not find the view file for this controller"
123
- ma_log "Error: Can not find the view file for this controller"
124
- redirect_to_root
125
- end
126
- end
127
- init_vars(params[:id])
128
- end
129
- else
130
- redirect_to_root
131
- end
132
- end
133
- def run_if
134
- init_vars(params[:id])
135
- condition= eval(@runseq.code).to_s
136
- match_found= false
137
- if condition
138
- xml= REXML::Document.new(@runseq.xml).root
139
- next_runseq= nil
140
- text = xml.elements['//node/node'].attributes['TEXT']
141
- match, name= text.split(':',2)
142
- label= name2code(name.strip)
143
- if condition==match
144
- if label=="end"
145
- @end_job= true
146
- else
147
- next_runseq= @xmain.runseqs.where(:code=> label, :action.ne=>'redirect').first
148
- match_found= true if next_runseq
149
- @runseq_not_f= false
150
- end
151
- end
152
- end
153
- unless match_found || @end_job
154
- next_runseq= @xmain.runseqs.where( rstep:(@xvars['current_step']+1) ).first
155
- end
156
- end_action(next_runseq)
157
- end
158
- def run_redirect
159
- init_vars(params[:id])
160
- # next_runseq= @xmain.runseqs.first :conditions=>["id != ? AND code = ?",@runseq.id, @runseq.code]
161
- next_runseq= @xmain.runseqs.where(:id.ne=>@runseq.id, :code=>@runseq.code).first
162
- @xmain.current_runseq= next_runseq.id
163
- end_action(next_runseq)
164
- end
165
- def run_do
166
- init_vars(params[:id])
167
- @runseq.start ||= Time.now
168
- @runseq.status= 'R' # running
169
- $runseq_id= @runseq.id
170
- $user_id= current_ma_user.try(:id)
171
- set_global
172
- controller = Kernel.const_get(@xvars['custom_controller']).new
173
- result = controller.send(@runseq.code)
174
- init_vars_by_runseq($runseq_id)
175
- @xvars = $xvars
176
- @xvars[@runseq.code.to_sym]= result.to_s
177
- @xvars['current_step']= @runseq.rstep
178
- @runseq.status= 'F' #finish
179
- @runseq.stop= Time.now
180
- @runseq.save
181
- end_action
182
- rescue => e
183
- @xmain.status='E'
184
- @xvars['error']= e.to_s+e.backtrace.to_s
185
- @xmain.xvars= $xvars
186
- @xmain.save
187
- @runseq.status= 'F' #finish
188
- @runseq.stop= Time.now
189
- @runseq.save
190
- refresh_to "/", :alert => "Sorry opeation error at #{@xmain.id} #{@xvars['error']}"
191
- end
192
- def run_output
193
- init_vars(params[:id])
194
- service= @xmain.service
195
- # disp= get_option("ma_display")
196
- disp= get_option("ma_display")
197
- ma_display = (disp && !affirm(disp)) ? false : true
198
- if service
199
- f= "app/views/#{service.module.code}/#{service.code}/#{@runseq.code}.html.erb"
200
- @ui= File.read(f)
201
- if Jinda::Doc.where(:runseq_id=>@runseq.id).exists?
202
- @doc= Jinda::Doc.where(:runseq_id=>@runseq.id).first
203
- @doc.update_attributes :data_text=> render_to_string(:inline=>@ui, :layout=>"utf8"),
204
- :xmain=>@xmain, :runseq=>@runseq, :user=>current_ma_user,
205
- :ip=> get_ip, :service=>service, :ma_display=>ma_display,
206
- :ma_secured => @xmain.service.ma_secured
207
- else
208
- @doc= Jinda::Doc.create :name=> @runseq.name,
209
- :content_type=>"output", :data_text=> render_to_string(:inline=>@ui, :layout=>"utf8"),
210
- :xmain=>@xmain, :runseq=>@runseq, :user=>current_ma_user,
211
- :ip=> get_ip, :service=>service, :ma_display=>ma_display,
212
- :ma_secured => @xmain.service.ma_secured
213
- end
214
- @message = defined?(MSG_NEXT) ? MSG_NEXT : "Next &gt;"
215
- @message = "Finish" if @runseq.end
216
- ma_log("Todo defined?(NSG_NEXT : Next >>)")
217
- eval "@xvars[@runseq.code] = url_for(:controller=>'jinda', :action=>'document', :id=>@doc.id)"
218
- else
219
- # flash[:notice]= "Error: Can not find the view file for this controller"
220
- ma_log "Error: Can not find the view file for this controller"
221
- redirect_to_root
222
- end
223
- #ma_display= get_option("ma_display")
224
- unless ma_display
225
- end_action
226
- end
227
- end
228
- def run_mail
229
- init_vars(params[:id])
230
- service= @xmain.service
231
- f= "app/views/#{service.module.code}/#{service.code}/#{@runseq.code}.html.erb"
232
- @ui= File.read(f).html_safe
233
- @doc= Jinda::Doc.create :name=> @runseq.name,
234
- :content_type=>"mail", :data_text=> render_to_string(:inline=>@ui, :layout=>false),
235
- :xmain=>@xmain, :runseq=>@runseq, :user=>current_ma_user,
236
- :ip=> get_ip, :service=>service, :ma_display=>false,
237
- :ma_secured => @xmain.service.ma_secured
238
- eval "@xvars[:#{@runseq.code}] = url_for(:controller=>'jinda', :action=>'document', :id=>@doc.id)"
239
- mail_from = get_option('from')
240
- # sender= render_to_string(:inline=>mail_from) if mail_from
241
- mail_to = get_option('to')
242
- recipients= render_to_string(:inline=>mail_to) if mail_to
243
- mail_subject = get_option('subject')
244
- subject= render_to_string(:inline=>mail_subject) || "#{@runseq.name}"
245
- JindaMailer.gmail(@doc.data_text, recipients, subject).deliver unless DONT_SEND_MAIL
246
- end_action
247
- end
248
- def end_output
249
- init_vars(params[:xmain_id])
250
- end_action
251
- end
252
-
253
- ####################################################
254
- # search for original_filename if attached #
255
- ####################################################
256
- #
257
- # def end_form
258
- # init_vars(params[:xmain_id])
259
- # eval "@xvars[@runseq.code] = {} unless @xvars[@runseq.code]"
260
- # params.each { |k,v|
261
- # if params[k].respond_to? :original_filename
262
- # get_image(k, params[k])
263
- # elsif params[k].is_a?(Hash)
264
- # eval "@xvars[@runseq.code][k] = v"
265
- # params[k].each { |k1,v1|
266
- # next unless v1.respond_to?(:original_filename)
267
- # get_image1(k, k1, params[k][k1])
268
- # }
269
- # else
270
- # v = v.to_unsafe_h unless v.class == String
271
- # eval "@xvars[@runseq.code][k] = v"
272
- # end
273
- # }
274
- # end_action
275
- # end
276
- # Not working at ?(Hash)
277
- # Temp hardcode below!! require field to load name :filename
278
-
279
-
280
- def end_form
281
- # Check error using xmain_id to redirect_to_root and return
282
- if params[:xmain_id]
283
- init_vars(params[:xmain_id])
284
- else
285
- ma_log "Known Bug : repeated end_form "
286
- redirect_to_root and return
287
- end
288
- eval "@xvars[@runseq.code] = {} unless @xvars[@runseq.code]"
289
- # Search for uploaded file name if exist
290
- params.each { |k,v|
291
- if params[k].respond_to? :original_filename
292
- get_image(k, params[k])
293
- # check if params of array in form eg: edit_article
294
- elsif params[k].is_a?(ActionController::Parameters)
295
- params[k].each { |k1,v1|
296
- # eval "@xvars[@runseq.code][k1] = params.require(k1).permit(k1)"
297
- eval "@xvars[@runseq.code][k1] = v1"
298
- next unless v1.respond_to?(:original_filename)
299
- get_image1(k, k1, params[k][k1])
300
- }
301
- else
302
- # https://stackoverflow.com/questions/34949505/rails-5-unable-to- retrieve-hash-values-from-parameter # bug in to_unsalfe_h rails 5.1.6 https://github.com/getsentry/raven-ruby/issues/799
303
- # Solution:
304
- # https://stackoverflow.com/questions/34949505/rails-5-unable-to-retrieve-hash-values-from-parameter
305
- # v = v.to_unsafe_h unless v.class == String
306
- # v = params.require[k] unless v.class == String
307
- v = v.to_s unless v.class == String
308
- # Create @xvars[@runseq.code]
309
- eval "@xvars[@runseq.code][k] = v"
310
- end
311
- }
312
- end_action
313
- end
314
-
315
- def end_action(next_runseq = nil)
316
- # @runseq.status='F' unless @runseq_not_f
317
- @xmain.xvars= @xvars
318
- @xmain.status= 'R' # running
319
- @xmain.save!
320
- @runseq.status='F'
321
- @runseq.user= current_ma_user
322
- @runseq.stop= Time.now
323
- @runseq.save
324
- next_runseq= @xmain.runseqs.where(:rstep=> @runseq.rstep+1).first unless next_runseq
325
- if @end_job || !next_runseq # job finish
326
- @xmain.xvars= @xvars
327
- @xmain.status= 'F' unless @xmain.status== 'E' # finish
328
- @xmain.stop= Time.now
329
- @xmain.save
330
- if @xvars['p']['return']
331
- redirect_to @xvars['p']['return'] and return
332
- else
333
- if @user
334
- redirect_to :action=>'index' and return
335
- else
336
- redirect_to_root and return
337
- end
338
- end
339
- else
340
- @xmain.update_attribute :current_runseq, next_runseq.id
341
- redirect_to :action=>'run', :id=>@xmain.id and return
342
- end
343
- end
344
- # process images from first level
345
- def get_image(key, params)
346
- doc = Jinda::Doc.create(
347
- :name=> key.to_s,
348
- :xmain=> @xmain.id,
349
- :runseq=> @runseq.id,
350
- :filename=> params.original_filename,
351
- :content_type => params.content_type || 'application/zip',
352
- :data_text=> '',
353
- :ma_display=>true,
354
- :ma_secured => @xmain.service.ma_secured )
355
- if defined?(IMAGE_LOCATION)
356
- filename = "#{IMAGE_LOCATION}/f#{Param.gen(:asset_id)}"
357
- File.open(filename,"wb") { |f| f.write(params.read) }
358
- # File.open(filename,"wb") { |f| f.puts(params.read) }
359
- eval "@xvars[@runseq.code][key] = '#{url_for(:action=>'document', :id=>doc.id, :only_path => true )}' "
360
- doc.update_attributes :url => filename, :basename => File.basename(filename), :cloudinary => false
361
- else
362
- result = Cloudinary::Uploader.upload(params)
363
- eval %Q{ @xvars[@runseq.code][key] = '#{result["url"]}' }
364
- doc.update_attributes :url => result["url"], :basename => File.basename(result["url"]), :cloudinary => true
365
- end
366
- end
367
- # process images from second level, e.g,, fields_for
368
- def get_image1(key, key1, params)
369
- doc = Jinda::Doc.create(
370
- :name=> "#{key}_#{key1}",
371
- :xmain=> @xmain.id,
372
- :runseq=> @runseq.id,
373
- :filename=> params.original_filename,
374
- :content_type => params.content_type || 'application/zip',
375
- :data_text=> '',
376
- :ma_display=>true, :ma_secured => @xmain.service.ma_secured )
377
- if defined?(IMAGE_LOCATION)
378
- filename = "#{IMAGE_LOCATION}/f#{Param.gen(:asset_id)}"
379
- File.open(filename,"wb") { |f| f.write(params.read) }
380
- eval "@xvars[@runseq.code][key][key1] = '#{url_for(:action=>'document', :id=>doc.id, :only_path => true)}' "
381
- doc.update_attributes :url => filename, :basename => File.basename(filename), :cloudinary => false
382
- else
383
- result = Cloudinary::Uploader.upload(params)
384
- eval %Q{ @xvars[@runseq.code][key][key1] = '#{result["url"]}' }
385
- doc.update_attributes :url => result["url"], :basename => File.basename(result["url"]), :cloudinary => true
386
- end
387
- end
388
- def doc_print
389
- render :file=>'public/doc.html', :layout=>'layouts/print'
390
- end
391
- # generate documentation for application
392
- def doc
393
- require 'rdoc'
394
- @app= get_app
395
- @intro = File.read('README.md')
396
- @print= "<div align='right'><img src='/assets/printer.png'/> <a href='/jinda/doc_print' target='_blank'/>Print</a></div>"
397
- doc= render_to_string 'doc.md', :layout => false
398
-
399
- html= Maruku.new(doc).to_html
400
- File.open('public/doc.html','w') {|f| f.puts html }
401
- respond_to do |format|
402
- format.html {
403
- render :plain=> @print+html, :layout => 'layouts/jqm/_page'
404
- # render :text=> Maruku.new(doc).to_html, :layout => false
405
- # format.html {
406
- # h = RDoc::Markup::ToHtml.new
407
- # render :text=> h.convert(doc), :layout => 'layouts/_page'
408
- }
409
- format.pdf {
410
- latex= Maruku.new(doc).to_latex
411
- File.open('tmp/doc.md','w') {|f| f.puts doc}
412
- File.open('tmp/doc.tex','w') {|f| f.puts latex}
413
- # system('pdflatex tmp/doc.tex ')
414
- # send_file( 'tmp/doc.pdf', :type => ‘application/pdf’,
415
- # :disposition => ‘inline’, :filename => 'doc.pdf')
416
- render :plain=>'done'
417
- }
418
- end
419
- end
420
- # handle uploaded image
421
- def document
422
- doc = Jinda::Doc.find params[:id]
423
- if doc.cloudinary
424
- require 'net/http'
425
- require "uri"
426
- uri = URI.parse(doc.url)
427
- data = Net::HTTP.get_response(uri)
428
- send_data(data.body, :filename=>doc.filename, :type=>doc.content_type, :disposition=>"inline")
429
- else
430
- data= read_binary(doc.url)
431
- send_data(data, :filename=>doc.filename, :type=>doc.content_type, :disposition=>"inline")
432
- end
433
- end
434
- def status
435
- @xmain= Jinda::Xmain.where(:xid=>params[:xid]).first
436
- @title= "Task number #{params[:xid]} #{@xmain.name}"
437
- @backbtn= true
438
- @xvars= @xmain.xvars
439
- # flash.now[:notice]= "รายการ #{@xmain.id} ได้ถูกยกเลิกแล้ว" if @xmain.status=='X'
440
- ma_log "Task #{@xmain.id} is cancelled" if @xmain.status=='X'
441
- # flash.now[:notice]= "transaction #{@xmain.id} was cancelled" if @xmain.status=='X'
442
- rescue
443
- refresh_to "/", :alert => "Could not find task number <b> #{params[:xid]} </b>"
444
- end
445
- def help
446
- end
447
- def search
448
- @q = params[:q] || params[:ma_search][:q] || ""
449
- @title = "ผลการค้นหา #{@q}"
450
- @backbtn= true
451
- @cache= true
452
- if @q.blank?
453
- redirect_to "/"
454
- else
455
- s= GmaSearch.create :q=>@q, :ip=> request.env["REMOTE_ADDR"]
456
- do_search
457
- end
458
- end
459
- def err404
460
- # ma_log 'ERROR', 'main/err404'
461
- 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."
462
- 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."
463
- redirect_to '/'
464
- end
465
- def err500
466
- # ma_log 'ERROR', 'main/err500'
467
- 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."
468
- 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."
469
- redirect_to '/'
470
- end
471
-
472
- private
473
- ###############################################################################################
474
- # Each Service at one moment will create one xmain
475
- ###############################################################################################
476
- def create_xmain(service)
477
- c = name2camel(service.module.code)
478
- custom_controller= "#{c}Controller"
479
- params["return"] = request.env['HTTP_REFERER']
480
- Jinda::Xmain.create :service=>service,
481
- :start=>Time.now,
482
- :name=>service.name,
483
- :ip=> get_ip,
484
- :status=>'I', # init
485
- :user=>current_ma_user,
486
- :xvars=> {
487
- :service_id=>service.id,
488
- :p=>params.to_unsafe_h,
489
- :id=>params[:id],
490
- :user_id=>current_ma_user.try(:id),
491
- :custom_controller=>custom_controller,
492
- :host=>request.host,
493
- :referer=>request.env['HTTP_REFERER']
494
- }
495
- end
496
- ###############################################################################################
497
- # Each xmain will create many run_seq as many as steps and form_steps
498
- ###############################################################################################
499
- def create_runseq(xmain)
500
- @xvars= xmain.xvars
501
- default_role= get_default_role
502
- xml= xmain.service.xml
503
- root = REXML::Document.new(xml).root
504
- i= 0; j= 0 # i= step, j= form_step
505
- root.elements.each('node') do |activity|
506
- text= activity.attributes['TEXT']
507
- next if ma_comment?(text)
508
- next if text =~/^rule:\s*/
509
- action= freemind2action(activity.elements['icon'].attributes['BUILTIN']) if activity.elements['icon']
510
- return false unless action
511
- i= i + 1
512
- output_ma_display= false
513
- if action=='output'
514
- ma_display= get_option_xml("ma_display", activity)
515
- if ma_display && !affirm(ma_display)
516
- output_ma_display= false
517
- else
518
- output_ma_display= true
519
- end
520
- end
521
- j= j + 1 if (action=='form' || output_ma_display)
522
- @xvars['referer'] = activity.attributes['TEXT'] if action=='redirect'
523
- if action!= 'if'
524
- scode, name= text.split(':', 2)
525
- name ||= scode; name.strip!
526
- code= name2code(scode)
527
- else
528
- code= text
529
- name= text
530
- end
531
- role= get_option_xml("role", activity) || default_role
532
- rule= get_option_xml("rule", activity) || "true"
533
- runseq= Jinda::Runseq.create :xmain=>xmain.id,
534
- :name=> name, :action=> action,
535
- :code=> code, :role=>role.upcase, :rule=> rule,
536
- :rstep=> i, :form_step=> j, :status=>'I',
537
- :xml=>activity.to_s
538
- xmain.current_runseq= runseq.id if i==1
539
- end
540
- @xvars['total_steps']= i
541
- @xvars['total_form_steps']= j
542
- end
543
-
544
- def init_vars(xmain)
545
- @xmain= Jinda::Xmain.find xmain
546
- @xvars= @xmain.xvars
547
- @runseq= @xmain.runseqs.find @xmain.current_runseq
548
- # authorize?
549
- @xvars['current_step']= @runseq.rstep
550
- @xvars['referrer']= request.referrer
551
- session[:xmain_id]= @xmain.id
552
- session[:runseq_id]= @runseq.id
553
- unless params[:action]=='run_call'
554
- @runseq.start ||= Time.now
555
- @runseq.status= 'R' # running
556
- @runseq.save
557
- end
558
- $xmain= @xmain; $xvars= @xvars
559
- $runseq_id= @runseq.id
560
- $user_id= current_ma_user.try(:id)
561
- end
562
- def init_vars_by_runseq(runseq_id)
563
- @runseq= Jinda::Runseq.find runseq_id
564
- @xmain= @runseq.xmain
565
- @xvars= @xmain.xvars
566
- #@xvars[:current_step]= @runseq.rstep
567
- @runseq.start ||= Time.now
568
- @runseq.status= 'R' # running
569
- @runseq.save
570
- end
571
- def store_asset
572
- if params[:content]
573
- doc = GmaDoc.create! :name=> 'asset',
574
- :filename=> (params[:file_name]||''),
575
- :content_type => (params[:content_type] || 'application/zip'),
576
- :data_text=> '',
577
- :ma_display=>true
578
- path = (IMAGE_LOCATION || "tmp")
579
- File.open("#{path}/f#{doc.id}","wb") { |f|
580
- f.puts(params[:content])
581
- }
582
- render :xml=>"<elocal><doc id='#{doc.id}' /><success /></elocal>"
583
- else
584
- render :xml=>"<elocal><fail /></elocal>"
585
- end
586
- end
587
- def do_search
588
- if current_ma_user.ma_secured?
589
- @docs = GmaDoc.search_ma_secured(@q.downcase, params[:page], PER_PAGE)
590
- else
591
- @docs = GmaDoc.search(@q.downcase, params[:page], PER_PAGE)
592
- end
593
- @xmains = GmaXmain.find(@docs.map(&:ma_xmain_id)).sort { |a,b| b.id<=>a.id }
594
- # @xmains = GmaXmain.find @docs.map(&:created_at).sort { |a,b| b<=>a }
595
- end
596
- def read_binary(path)
597
- File.open path, "rb" do |f| f.read end
598
- end
599
66
  end