jinda 0.5.1 → 0.5.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +3 -3
  3. data/lib/generators/jinda/install_generator.rb +6 -0
  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 -573
  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 +5 -3
  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/generators/jinda/templates/app/views/layouts/_head.html.erb +7 -10
  27. data/lib/generators/jinda/templates/app/views/layouts/{_metatag.html.erb → _meta_tag.html.erb} +0 -0
  28. data/lib/generators/jinda/templates/app/views/layouts/jqm/_full.haml +1 -9
  29. data/lib/generators/jinda/templates/app/views/notes/new/new_note.html.erb +1 -1
  30. data/lib/jinda/helpers.rb +201 -57
  31. data/lib/jinda/version.rb +1 -1
  32. metadata +16 -11
  33. data/lib/generators/jinda/templates/app/controllers/ctrs_controller.rb-gem-test +0 -2
  34. data/lib/generators/jinda/templates/app/views/layouts/_meta_tags.html.erb +0 -5
  35. data/lib/generators/jinda/templates/app/views/new.html.erb +0 -7
  36. data/lib/jinda/ template/view.html.erb +0 -25
  37. data/lib/jinda/app/jinda/index.mm +0 -308
  38. 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,513 +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
- # call controller to do the freemind task using Star symbol eg: Update
174
- result = controller.send(@runseq.code)
175
- init_vars_by_runseq($runseq_id)
176
- @xvars = $xvars
177
- @xvars[@runseq.code.to_sym]= result.to_s
178
- @xvars['current_step']= @runseq.rstep
179
- @runseq.status= 'F' #finish
180
- @runseq.stop= Time.now
181
- @runseq.save
182
- end_action
183
- rescue => e
184
- @xmain.status='E'
185
- @xvars['error']= e.to_s+e.backtrace.to_s
186
- @xmain.xvars= $xvars
187
- @xmain.save
188
- @runseq.status= 'F' #finish
189
- @runseq.stop= Time.now
190
- @runseq.save
191
- refresh_to "/", :alert => "Sorry opeation error at #{@xmain.id} #{@xvars['error']}"
192
- end
193
- def run_output
194
- init_vars(params[:id])
195
- service= @xmain.service
196
- # disp= get_option("display")
197
- # disp = Nil or :"??????"
198
- disp= get_option("display")
199
- ma_display = (disp && !affirm(disp)) ? false : true
200
- # Todo check if file is available
201
- # if service and file exist
202
- if service && !@runseq.code.blank?
203
- f= "app/views/#{service.module.code}/#{service.code}/#{@runseq.code}.html.erb"
204
- @ui= File.read(f)
205
- if Jinda::Doc.where(:runseq_id=>@runseq.id).exists?
206
- @doc= Jinda::Doc.where(:runseq_id=>@runseq.id).first
207
- @doc.update_attributes :data_text=> render_to_string(:inline=>@ui, :layout=>"utf8"),
208
- :xmain=>@xmain, :runseq=>@runseq, :user=>current_ma_user,
209
- :ip=> get_ip, :service=>service, :ma_display=>ma_display,
210
- :ma_secured => @xmain.service.ma_secured,
211
- :filename => "#{@runseq.code}.html.erb"
212
- else
213
- @doc= Jinda::Doc.create :name=> @runseq.name,
214
- :content_type=>"output", :data_text=> render_to_string(:inline=>@ui, :layout=>"utf8"),
215
- :xmain=>@xmain, :runseq=>@runseq, :user=>current_ma_user,
216
- :ip=> get_ip, :service=>service, :ma_display=>ma_display,
217
- :ma_secured => @xmain.service.ma_secured,
218
- :filename => "#{@runseq.code}.html.erb"
219
- end
220
- # @message = defined?(MSG_NEXT) ? MSG_NEXT : "Next &gt;"
221
- @message = defined?(MSG_NEXT) ? MSG_NEXT : "Next >>"
222
- @message = "Finish" if @runseq.end
223
- ma_log("Todo defined?(NSG_NEXT : Next >>)")
224
- eval "@xvars[@runseq.code] = url_for(:controller=>'jinda', :action=>'document', :id=>@doc.id)"
225
- else
226
- flash[:notice]= "Error: Can not find the view file for this controller"
227
- ma_log "Error: Can not find the view file for this controller"
228
- redirect_to_root
229
- end
230
- #
231
- # Check if ma_display available
232
- #
233
- # ma_display= get_option("ma_display")
234
- unless ma_display
235
- end_action
236
- end
237
- # display from @ui
238
- end
239
- def run_mail
240
- init_vars(params[:id])
241
- service= @xmain.service
242
- f= "app/views/#{service.module.code}/#{service.code}/#{@runseq.code}.html.erb"
243
- @ui= File.read(f).html_safe
244
- @doc= Jinda::Doc.create :name=> @runseq.name,
245
- :content_type=>"mail", :data_text=> render_to_string(:inline=>@ui, :layout=>false),
246
- :xmain=>@xmain, :runseq=>@runseq, :user=>current_ma_user,
247
- :ip=> get_ip, :service=>service, :ma_display=>false,
248
- :ma_secured => @xmain.service.ma_secured
249
- eval "@xvars[:#{@runseq.code}] = url_for(:controller=>'jinda', :action=>'document', :id=>@doc.id)"
250
- mail_from = get_option('from')
251
- # sender= render_to_string(:inline=>mail_from) if mail_from
252
- mail_to = get_option('to')
253
- recipients= render_to_string(:inline=>mail_to) if mail_to
254
- mail_subject = get_option('subject')
255
- subject= render_to_string(:inline=>mail_subject) || "#{@runseq.name}"
256
- JindaMailer.gmail(@doc.data_text, recipients, subject).deliver unless DONT_SEND_MAIL
257
- end_action
258
- end
259
- def end_output
260
- init_vars(params[:xmain_id])
261
- end_action
262
- end
263
-
264
- ####################################################
265
- # search for original_filename if attached #
266
- ####################################################
267
- #
268
- # def end_form
269
- # init_vars(params[:xmain_id])
270
- # eval "@xvars[@runseq.code] = {} unless @xvars[@runseq.code]"
271
- # params.each { |k,v|
272
- # if params[k].respond_to? :original_filename
273
- # get_image(k, params[k])
274
- # elsif params[k].is_a?(Hash)
275
- # eval "@xvars[@runseq.code][k] = v"
276
- # params[k].each { |k1,v1|
277
- # next unless v1.respond_to?(:original_filename)
278
- # get_image1(k, k1, params[k][k1])
279
- # }
280
- # else
281
- # v = v.to_unsafe_h unless v.class == String
282
- # eval "@xvars[@runseq.code][k] = v"
283
- # end
284
- # }
285
- # end_action
286
- # end
287
- # Not working at ?(Hash)
288
- # Temp hardcode below!! require field to load name :filename
289
-
290
-
291
- def end_form
292
- # Check error using xmain_id to redirect_to_root and return
293
- if params[:xmain_id]
294
- init_vars(params[:xmain_id])
295
- else
296
- ma_log "Known Bug : repeated end_form "
297
- redirect_to_root and return
298
- end
299
- eval "@xvars[@runseq.code] = {} unless @xvars[@runseq.code]"
300
- # Search for uploaded file name if exist
301
- params.each { |k,v|
302
- if params[k].respond_to? :original_filename
303
- get_image(k, params[k])
304
- # check if params of array in form eg: edit_article
305
- elsif params[k].is_a?(ActionController::Parameters)
306
- params[k].each { |k1,v1|
307
- # eval "@xvars[@runseq.code][k1] = params.require(k1).permit(k1)"
308
- eval "@xvars[@runseq.code][k1] = v1"
309
- next unless v1.respond_to?(:original_filename)
310
- get_image1(k, k1, params[k][k1])
311
- }
312
- else
313
- # 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
314
- # Solution:
315
- # https://stackoverflow.com/questions/34949505/rails-5-unable-to-retrieve-hash-values-from-parameter
316
- # v = v.to_unsafe_h unless v.class == String
317
- # v = params.require[k] unless v.class == String
318
- v = v.to_s unless v.class == String
319
- # Create @xvars[@runseq.code]
320
- eval "@xvars[@runseq.code][k] = v"
321
- end
322
- }
323
- end_action
324
- end
325
-
326
- def end_action(next_runseq = nil)
327
- # @runseq.status='F' unless @runseq_not_f
328
- @xmain.xvars= @xvars
329
- @xmain.status= 'R' # running
330
- @xmain.save!
331
- @runseq.status='F'
332
- @runseq.user= current_ma_user
333
- @runseq.stop= Time.now
334
- @runseq.save
335
- next_runseq= @xmain.runseqs.where(:rstep=> @runseq.rstep+1).first unless next_runseq
336
- if @end_job || !next_runseq # job finish
337
- @xmain.xvars= @xvars
338
- @xmain.status= 'F' unless @xmain.status== 'E' # finish
339
- @xmain.stop= Time.now
340
- @xmain.save
341
- if @xvars['p']['return']
342
- redirect_to @xvars['p']['return'] and return
343
- else
344
- if @user
345
- redirect_to :action=>'index' and return
346
- else
347
- redirect_to_root and return
348
- end
349
- end
350
- else
351
- @xmain.update_attribute :current_runseq, next_runseq.id
352
- redirect_to :action=>'run', :id=>@xmain.id and return
353
- end
354
- end
355
- # process images from first level
356
- def get_image(key, params)
357
- doc = Jinda::Doc.create(
358
- :name=> key.to_s,
359
- :xmain=> @xmain.id,
360
- :runseq=> @runseq.id,
361
- :filename=> params.original_filename,
362
- :content_type => params.content_type || 'application/zip',
363
- :data_text=> '',
364
- :ma_display=>true,
365
- :ma_secured => @xmain.service.ma_secured )
366
- if defined?(IMAGE_LOCATION)
367
- filename = "#{IMAGE_LOCATION}/f#{Param.gen(:asset_id)}"
368
- File.open(filename,"wb") { |f| f.write(params.read) }
369
- # File.open(filename,"wb") { |f| f.puts(params.read) }
370
- eval "@xvars[@runseq.code][key] = '#{url_for(:action=>'document', :id=>doc.id, :only_path => true )}' "
371
- doc.update_attributes :url => filename, :basename => File.basename(filename), :cloudinary => false
372
- else
373
- result = Cloudinary::Uploader.upload(params)
374
- eval %Q{ @xvars[@runseq.code][key] = '#{result["url"]}' }
375
- doc.update_attributes :url => result["url"], :basename => File.basename(result["url"]), :cloudinary => true
376
- end
377
- end
378
- # process images from second level, e.g,, fields_for
379
- def get_image1(key, key1, params)
380
- doc = Jinda::Doc.create(
381
- :name=> "#{key}_#{key1}",
382
- :xmain=> @xmain.id,
383
- :runseq=> @runseq.id,
384
- :filename=> params.original_filename,
385
- :content_type => params.content_type || 'application/zip',
386
- :data_text=> '',
387
- :ma_display=>true, :ma_secured => @xmain.service.ma_secured )
388
- if defined?(IMAGE_LOCATION)
389
- filename = "#{IMAGE_LOCATION}/f#{Param.gen(:asset_id)}"
390
- File.open(filename,"wb") { |f| f.write(params.read) }
391
- eval "@xvars[@runseq.code][key][key1] = '#{url_for(:action=>'document', :id=>doc.id, :only_path => true)}' "
392
- doc.update_attributes :url => filename, :basename => File.basename(filename), :cloudinary => false
393
- else
394
- result = Cloudinary::Uploader.upload(params)
395
- eval %Q{ @xvars[@runseq.code][key][key1] = '#{result["url"]}' }
396
- doc.update_attributes :url => result["url"], :basename => File.basename(result["url"]), :cloudinary => true
397
- end
398
- end
399
- def doc_print
400
- render :file=>'public/doc.html', :layout=>'layouts/print'
401
- end
402
- # generate documentation for application
403
- def doc
404
- require 'rdoc'
405
- @app= get_app
406
- @intro = File.read('README.md')
407
- @print= "<div align='right'><img src='/assets/printer.png'/> <a href='/jinda/doc_print' target='_blank'/>Print</a></div>"
408
- doc= render_to_string 'doc.md', :layout => false
409
-
410
- html= Maruku.new(doc).to_html
411
- File.open('public/doc.html','w') {|f| f.puts html }
412
- respond_to do |format|
413
- format.html {
414
- render :plain=> @print+html, :layout => 'layouts/jqm/_page'
415
- # render :text=> Maruku.new(doc).to_html, :layout => false
416
- # format.html {
417
- # h = RDoc::Markup::ToHtml.new
418
- # render :text=> h.convert(doc), :layout => 'layouts/_page'
419
- }
420
- format.pdf {
421
- latex= Maruku.new(doc).to_latex
422
- File.open('tmp/doc.md','w') {|f| f.puts doc}
423
- File.open('tmp/doc.tex','w') {|f| f.puts latex}
424
- # system('pdflatex tmp/doc.tex ')
425
- # send_file( 'tmp/doc.pdf', :type => ‘application/pdf’,
426
- # :disposition => ‘inline’, :filename => 'doc.pdf')
427
- render :plain=>'done'
428
- }
429
- end
430
- end
431
- # handle uploaded image
432
- def document
433
- doc = Jinda::Doc.find params[:id]
434
- if doc.cloudinary
435
- require 'net/http'
436
- require "uri"
437
- uri = URI.parse(doc.url)
438
- data = Net::HTTP.get_response(uri)
439
- send_data(data.body, :filename=>doc.filename, :type=>doc.content_type, :disposition=>"inline")
440
- else
441
- data= read_binary(doc.url)
442
- send_data(data, :filename=>doc.filename, :type=>doc.content_type, :disposition=>"inline")
443
- end
444
- end
445
- def status
446
- @xmain= Jinda::Xmain.where(:xid=>params[:xid]).first
447
- @title= "Task number #{params[:xid]} #{@xmain.name}"
448
- @backbtn= true
449
- @xvars= @xmain.xvars
450
- # flash.now[:notice]= "รายการ #{@xmain.id} ได้ถูกยกเลิกแล้ว" if @xmain.status=='X'
451
- ma_log "Task #{@xmain.id} is cancelled" if @xmain.status=='X'
452
- # flash.now[:notice]= "transaction #{@xmain.id} was cancelled" if @xmain.status=='X'
453
- rescue
454
- refresh_to "/", :alert => "Could not find task number <b> #{params[:xid]} </b>"
455
- end
456
- def help
457
- end
458
- def search
459
- @q = params[:q] || params[:ma_search][:q] || ""
460
- @title = "ผลการค้นหา #{@q}"
461
- @backbtn= true
462
- @cache= true
463
- if @q.blank?
464
- redirect_to "/"
465
- else
466
- s= GmaSearch.create :q=>@q, :ip=> request.env["REMOTE_ADDR"]
467
- do_search
468
- end
469
- end
470
- def err404
471
- # ma_log 'ERROR', 'main/err404'
472
- 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."
473
- 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."
474
- redirect_to '/'
475
- end
476
- def err500
477
- # ma_log 'ERROR', 'main/err500'
478
- 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."
479
- 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."
480
- redirect_to '/'
481
- end
482
-
483
- private
484
- ###############################################################################################
485
- # Each Service at one moment will create one xmain
486
- ###############################################################################################
487
- def create_xmain(service)
488
- c = name2camel(service.module.code)
489
- custom_controller= "#{c}Controller"
490
- params["return"] = request.env['HTTP_REFERER']
491
- Jinda::Xmain.create :service=>service,
492
- :start=>Time.now,
493
- :name=>service.name,
494
- :ip=> get_ip,
495
- :status=>'I', # init
496
- :user=>current_ma_user,
497
- :xvars=> {
498
- :service_id=>service.id,
499
- :p=>params.to_unsafe_h,
500
- :id=>params[:id],
501
- :user_id=>current_ma_user.try(:id),
502
- :custom_controller=>custom_controller,
503
- :host=>request.host,
504
- :referer=>request.env['HTTP_REFERER']
505
- }
506
- end
507
- ###############################################################################################
508
- # Each xmain will create many run_seq as many as steps and form_steps
509
- ###############################################################################################
510
- def create_runseq(xmain)
511
- @xvars= xmain.xvars
512
- default_role= get_default_role
513
- xml= xmain.service.xml
514
- root = REXML::Document.new(xml).root
515
- i= 0; j= 0 # i= step, j= form_step
516
- root.elements.each('node') do |activity|
517
- text= activity.attributes['TEXT']
518
- next if ma_comment?(text)
519
- next if text =~/^rule:\s*/
520
- action= freemind2action(activity.elements['icon'].attributes['BUILTIN']) if activity.elements['icon']
521
- return false unless action
522
- i= i + 1
523
- output_ma_display= false
524
- if action=='output'
525
- ma_display= get_option_xml("display", activity)
526
- if ma_display && !affirm(ma_display)
527
- output_ma_display= false
528
- else
529
- output_ma_display= true
530
- end
531
- end
532
- j= j + 1 if (action=='form' || output_ma_display)
533
- @xvars['referer'] = activity.attributes['TEXT'] if action=='redirect'
534
- if action!= 'if' && !text.blank?
535
- scode, name= text.split(':', 2)
536
- name ||= scode; name.strip!
537
- code= name2code(scode)
538
- else
539
- code= text
540
- name= text
541
- end
542
- role= get_option_xml("role", activity) || default_role
543
- rule= get_option_xml("rule", activity) || "true"
544
- runseq= Jinda::Runseq.create :xmain=>xmain.id,
545
- :name=> name, :action=> action,
546
- :code=> code, :role=>role.upcase, :rule=> rule,
547
- :rstep=> i, :form_step=> j, :status=>'I',
548
- :xml=>activity.to_s
549
- xmain.current_runseq= runseq.id if i==1
550
- end
551
- @xvars['total_steps']= i
552
- @xvars['total_form_steps']= j
553
- end
554
-
555
- def init_vars(xmain)
556
- @xmain= Jinda::Xmain.find xmain
557
- @xvars= @xmain.xvars
558
- @runseq= @xmain.runseqs.find @xmain.current_runseq
559
- # authorize?
560
- @xvars['current_step']= @runseq.rstep
561
- @xvars['referrer']= request.referrer
562
- session[:xmain_id]= @xmain.id
563
- session[:runseq_id]= @runseq.id
564
- unless params[:action]=='run_call'
565
- @runseq.start ||= Time.now
566
- @runseq.status= 'R' # running
567
- @runseq.save
568
- end
569
- $xmain= @xmain; $xvars= @xvars
570
- $runseq_id= @runseq.id
571
- $user_id= current_ma_user.try(:id)
572
- end
573
- def init_vars_by_runseq(runseq_id)
574
- @runseq= Jinda::Runseq.find runseq_id
575
- @xmain= @runseq.xmain
576
- @xvars= @xmain.xvars
577
- #@xvars[:current_step]= @runseq.rstep
578
- @runseq.start ||= Time.now
579
- @runseq.status= 'R' # running
580
- @runseq.save
581
- end
582
- def store_asset
583
- if params[:content]
584
- doc = GmaDoc.create! :name=> 'asset',
585
- :filename=> (params[:file_name]||''),
586
- :content_type => (params[:content_type] || 'application/zip'),
587
- :data_text=> '',
588
- :ma_display=>true
589
- path = (IMAGE_LOCATION || "tmp")
590
- File.open("#{path}/f#{doc.id}","wb") { |f|
591
- f.puts(params[:content])
592
- }
593
- render :xml=>"<elocal><doc id='#{doc.id}' /><success /></elocal>"
594
- else
595
- render :xml=>"<elocal><fail /></elocal>"
596
- end
597
- end
598
- def do_search
599
- if current_ma_user.ma_secured?
600
- @docs = GmaDoc.search_ma_secured(@q.downcase, params[:page], PER_PAGE)
601
- else
602
- @docs = GmaDoc.search(@q.downcase, params[:page], PER_PAGE)
603
- end
604
- @xmains = GmaXmain.find(@docs.map(&:ma_xmain_id)).sort { |a,b| b.id<=>a.id }
605
- # @xmains = GmaXmain.find @docs.map(&:created_at).sort { |a,b| b<=>a }
606
- end
607
- def read_binary(path)
608
- File.open path, "rb" do |f| f.read end
609
- end
610
66
  end