jinda 0.5.1 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 86e26953c4782d41554c6d2d906ce58d2a2320ddfa49446d14920819fbd19990
4
- data.tar.gz: 6e4752ee8dcab6758bcb5d3640fcb5af993800bfa965c6a937ab794f0066d5d8
3
+ metadata.gz: d32e253610977d016f3a715060ae616171a2c71331474c9f7dc7091682b4d58b
4
+ data.tar.gz: 6af32a30a010f037e392fb630713c5b42a8ebc1482da18faae7605aaff8d804d
5
5
  SHA512:
6
- metadata.gz: c0ade9552fde2d3b14032f796845465cf591e70d8d906821301ada8f3e2e30456d9b2632cf7642663d1601e54d463d0ef848c78c8ed6a7eb94a48845d1605b56
7
- data.tar.gz: 59b9411947dbc7290dc94fb7b4f37700554507a8b910c01324b29012e359428999a911118212e0490e0fbf5e2dc10e79af8b9e89d64b56b796c118f2d5c4c07d
6
+ metadata.gz: b4fa824aa0b3598642f0e2b818883e2b1eea7f5dcfb5eacc04bd3dbc1c279dbc6ec697dca9a6b0759f8ba6f4b5eab9c12d31736b70a520d2d84fa4eff9aab99b
7
+ data.tar.gz: '04840e4df4f2d0e02a8f200bf5707d294b7993c53d2fc009c4ef558a9a9bab87cb4a3700f666b19a7b09fe094f655dd9e33c6815138ae8d194ab4d8c746d9d32'
data/README.md CHANGED
@@ -14,8 +14,8 @@ Rails Application Generator using Freemind
14
14
 
15
15
  These versions works for sure but others may do.
16
16
 
17
- * Ruby 2.6.3
18
- * Rails 6.0.1
17
+ * Ruby 2.7.0
18
+ * Rails 6.0.2
19
19
  * MongoDB 6
20
20
  * Freemind 1.0.1
21
21
 
@@ -40,7 +40,7 @@ app without ActiveRecord
40
40
 
41
41
  ## Add jinda to your Gemfile:
42
42
 
43
- gem 'jinda', '~> 0.5.1'
43
+ gem 'jinda', '~> 0.5.2'
44
44
 
45
45
  For Development (most updated)
46
46
 
@@ -36,7 +36,7 @@ app without ActiveRecord
36
36
 
37
37
  ## Add jinda to your Gemfile:
38
38
 
39
- gem 'jinda', '~> 0.5.1'
39
+ gem 'jinda', '~> 0.5.2'
40
40
 
41
41
  For Development (most updated)
42
42
 
@@ -0,0 +1,156 @@
1
+ module JindaGeneralConcern
2
+ extend ActiveSupport::Concern
3
+
4
+ def index
5
+ end
6
+
7
+ def logs
8
+ @xmains = Jinda::Xmain.all.desc(:created_at).page(params[:page]).per(10)
9
+ end
10
+
11
+ def error_logs
12
+ @xmains = Jinda::Xmain.in(status:['E']).desc(:created_at).page(params[:page]).per(10)
13
+ end
14
+
15
+ def notice_logs
16
+ @notices= Jinda::Notice.desc(:created_at).page(params[:page]).per(10)
17
+ end
18
+
19
+ def pending
20
+ @title= "Pending Tasks"
21
+ @xmains = Jinda::Xmain.in(status:['R','I']).asc(:created_at)
22
+ end
23
+
24
+ def cancel
25
+ Jinda::Xmain.find(params[:id]).update_attributes :status=>'X'
26
+ if params[:return]
27
+ redirect_to params[:return]
28
+ else
29
+ redirect_to action:"pending"
30
+ end
31
+ end
32
+
33
+ # process images from first level
34
+ def get_image(key, params)
35
+ doc = Jinda::Doc.create(
36
+ :name=> key.to_s,
37
+ :xmain=> @xmain.id,
38
+ :runseq=> @runseq.id,
39
+ :filename=> params.original_filename,
40
+ :content_type => params.content_type || 'application/zip',
41
+ :data_text=> '',
42
+ :ma_display=>true,
43
+ :ma_secured => @xmain.service.ma_secured )
44
+ if defined?(IMAGE_LOCATION)
45
+ filename = "#{IMAGE_LOCATION}/f#{Param.gen(:asset_id)}"
46
+ File.open(filename,"wb") { |f| f.write(params.read) }
47
+ # File.open(filename,"wb") { |f| f.puts(params.read) }
48
+ eval "@xvars[@runseq.code][key] = '#{url_for(:action=>'document', :id=>doc.id, :only_path => true )}' "
49
+ doc.update_attributes :url => filename, :basename => File.basename(filename), :cloudinary => false
50
+ else
51
+ result = Cloudinary::Uploader.upload(params)
52
+ eval %Q{ @xvars[@runseq.code][key] = '#{result["url"]}' }
53
+ doc.update_attributes :url => result["url"], :basename => File.basename(result["url"]), :cloudinary => true
54
+ end
55
+ end
56
+
57
+ # process images from second level, e.g,, fields_for
58
+ def get_image1(key, key1, params)
59
+ doc = Jinda::Doc.create(
60
+ :name=> "#{key}_#{key1}",
61
+ :xmain=> @xmain.id,
62
+ :runseq=> @runseq.id,
63
+ :filename=> params.original_filename,
64
+ :content_type => params.content_type || 'application/zip',
65
+ :data_text=> '',
66
+ :ma_display=>true, :ma_secured => @xmain.service.ma_secured )
67
+ if defined?(IMAGE_LOCATION)
68
+ filename = "#{IMAGE_LOCATION}/f#{Param.gen(:asset_id)}"
69
+ File.open(filename,"wb") { |f| f.write(params.read) }
70
+ eval "@xvars[@runseq.code][key][key1] = '#{url_for(:action=>'document', :id=>doc.id, :only_path => true)}' "
71
+ doc.update_attributes :url => filename, :basename => File.basename(filename), :cloudinary => false
72
+ else
73
+ result = Cloudinary::Uploader.upload(params)
74
+ eval %Q{ @xvars[@runseq.code][key][key1] = '#{result["url"]}' }
75
+ doc.update_attributes :url => result["url"], :basename => File.basename(result["url"]), :cloudinary => true
76
+ end
77
+ end
78
+
79
+ def doc_print
80
+ render :file=>'public/doc.html', :layout=>'layouts/print'
81
+ end
82
+
83
+ # generate documentation for application
84
+ def doc
85
+ require 'rdoc'
86
+ @app= get_app
87
+ @intro = File.read('README.md')
88
+ @print= "<div align='right'><img src='/assets/printer.png'/> <a href='/jinda/doc_print' target='_blank'/>Print</a></div>"
89
+ doc= render_to_string 'doc.md', :layout => false
90
+
91
+ html= Maruku.new(doc).to_html
92
+ File.open('public/doc.html','w') {|f| f.puts html }
93
+ respond_to do |format|
94
+ format.html {
95
+ render :plain=> @print+html, :layout => 'layouts/jqm/_page'
96
+ # render :text=> Maruku.new(doc).to_html, :layout => false
97
+ # format.html {
98
+ # h = RDoc::Markup::ToHtml.new
99
+ # render :text=> h.convert(doc), :layout => 'layouts/_page'
100
+ }
101
+ format.pdf {
102
+ latex= Maruku.new(doc).to_latex
103
+ File.open('tmp/doc.md','w') {|f| f.puts doc}
104
+ File.open('tmp/doc.tex','w') {|f| f.puts latex}
105
+ # system('pdflatex tmp/doc.tex ')
106
+ # send_file( 'tmp/doc.pdf', :type => ‘application/pdf’,
107
+ # :disposition => ‘inline’, :filename => 'doc.pdf')
108
+ render :plain=>'done'
109
+ }
110
+ end
111
+ end
112
+
113
+ def status
114
+ @xmain= Jinda::Xmain.where(:xid=>params[:xid]).first
115
+ @title= "Task number #{params[:xid]} #{@xmain.name}"
116
+ @backbtn= true
117
+ @xvars= @xmain.xvars
118
+ # flash.now[:notice]= "รายการ #{@xmain.id} ได้ถูกยกเลิกแล้ว" if @xmain.status=='X'
119
+ ma_log "Task #{@xmain.id} is cancelled" if @xmain.status=='X'
120
+ # flash.now[:notice]= "transaction #{@xmain.id} was cancelled" if @xmain.status=='X'
121
+ rescue
122
+ refresh_to "/", :alert => "Could not find task number <b> #{params[:xid]} </b>"
123
+ end
124
+
125
+ def help
126
+ end
127
+
128
+ def search
129
+ @q = params[:q] || params[:ma_search][:q] || ""
130
+ @title = "ผลการค้นหา #{@q}"
131
+ @backbtn= true
132
+ @cache= true
133
+ if @q.blank?
134
+ redirect_to "/"
135
+ else
136
+ s= GmaSearch.create :q=>@q, :ip=> request.env["REMOTE_ADDR"]
137
+ do_search
138
+ end
139
+ end
140
+
141
+ def err404
142
+ # ma_log 'ERROR', 'main/err404'
143
+ 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."
144
+ 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."
145
+ redirect_to '/'
146
+ end
147
+
148
+ def err500
149
+ # ma_log 'ERROR', 'main/err500'
150
+ 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."
151
+ 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."
152
+ redirect_to '/'
153
+ end
154
+
155
+ end
156
+
@@ -0,0 +1,236 @@
1
+ module JindaRunConcern
2
+ extend ActiveSupport::Concern
3
+
4
+ def run_form
5
+ init_vars(params[:id])
6
+ if authorize?
7
+ if ['F', 'X'].include? @xmain.status
8
+ redirect_to_root
9
+ else
10
+ service= @xmain.service
11
+ ###############################################################################################
12
+ # Run View Form f created template by jinda rake follow freemind mm file
13
+ ###############################################################################################
14
+ if service
15
+ @title= "Transaction ID #{@xmain.xid}: #{@xmain.name} / #{@runseq.name}"
16
+ fhelp= "app/views/#{service.module.code}/#{service.code}/#{@runseq.code}.md"
17
+ ###############################################################################################
18
+ # Check if help file available for this form
19
+ ###############################################################################################
20
+ @help = File.read(fhelp) if File.exists?(fhelp)
21
+ f= "app/views/#{service.module.code}/#{service.code}/#{@runseq.code}.html.erb"
22
+ if File.file?(f)
23
+ @ui= File.read(f)
24
+ else
25
+ # flash[:notice]= "Error: Can not find the view file for this controller"
26
+ ma_log "Error: Can not find the view file for this controller"
27
+ redirect_to_root
28
+ end
29
+ end
30
+ init_vars(params[:id])
31
+ end
32
+ else
33
+ redirect_to_root
34
+ end
35
+ end
36
+
37
+ def run_if
38
+ init_vars(params[:id])
39
+ condition= eval(@runseq.code).to_s
40
+ match_found= false
41
+ if condition
42
+ xml= REXML::Document.new(@runseq.xml).root
43
+ next_runseq= nil
44
+ text = xml.elements['//node/node'].attributes['TEXT']
45
+ match, name= text.split(':',2)
46
+ label= name2code(name.strip)
47
+ if condition==match
48
+ if label=="end"
49
+ @end_job= true
50
+ else
51
+ next_runseq= @xmain.runseqs.where(:code=> label, :action.ne=>'redirect').first
52
+ match_found= true if next_runseq
53
+ @runseq_not_f= false
54
+ end
55
+ end
56
+ end
57
+ unless match_found || @end_job
58
+ next_runseq= @xmain.runseqs.where( rstep:(@xvars['current_step']+1) ).first
59
+ end
60
+ end_action(next_runseq)
61
+ end
62
+
63
+ def run_redirect
64
+ init_vars(params[:id])
65
+ # next_runseq= @xmain.runseqs.first :conditions=>["id != ? AND code = ?",@runseq.id, @runseq.code]
66
+ next_runseq= @xmain.runseqs.where(:id.ne=>@runseq.id, :code=>@runseq.code).first
67
+ @xmain.current_runseq= next_runseq.id
68
+ end_action(next_runseq)
69
+ end
70
+
71
+ def run_do
72
+ init_vars(params[:id])
73
+ @runseq.start ||= Time.now
74
+ @runseq.status= 'R' # running
75
+ $runseq_id= @runseq.id
76
+ $user_id= current_ma_user.try(:id)
77
+ set_global
78
+ controller = Kernel.const_get(@xvars['custom_controller']).new
79
+ # call controller to do the freemind task using Star symbol eg: Update
80
+ result = controller.send(@runseq.code)
81
+ init_vars_by_runseq($runseq_id)
82
+ @xvars = $xvars
83
+ @xvars[@runseq.code.to_sym]= result.to_s
84
+ @xvars['current_step']= @runseq.rstep
85
+ @runseq.status= 'F' #finish
86
+ @runseq.stop= Time.now
87
+ @runseq.save
88
+ end_action
89
+ rescue => e
90
+ @xmain.status='E'
91
+ @xvars['error']= e.to_s+e.backtrace.to_s
92
+ @xmain.xvars= $xvars
93
+ @xmain.save
94
+ @runseq.status= 'F' #finish
95
+ @runseq.stop= Time.now
96
+ @runseq.save
97
+ refresh_to "/", :alert => "Sorry opeation error at #{@xmain.id} #{@xvars['error']}"
98
+ end
99
+
100
+ def run_output
101
+ init_vars(params[:id])
102
+ service= @xmain.service
103
+ # disp= get_option("display")
104
+ # disp = Nil or :"??????"
105
+ disp= get_option("display")
106
+ ma_display = (disp && !affirm(disp)) ? false : true
107
+ # Todo check if file is available
108
+ # if service and file exist
109
+ if service && !@runseq.code.blank?
110
+ f= "app/views/#{service.module.code}/#{service.code}/#{@runseq.code}.html.erb"
111
+ @ui= File.read(f)
112
+ if Jinda::Doc.where(:runseq_id=>@runseq.id).exists?
113
+ @doc= Jinda::Doc.where(:runseq_id=>@runseq.id).first
114
+ @doc.update_attributes :data_text=> render_to_string(:inline=>@ui, :layout=>"utf8"),
115
+ :xmain=>@xmain, :runseq=>@runseq, :user=>current_ma_user,
116
+ :ip=> get_ip, :service=>service, :ma_display=>ma_display,
117
+ :ma_secured => @xmain.service.ma_secured,
118
+ :filename => "#{@runseq.code}.html.erb"
119
+ else
120
+ @doc= Jinda::Doc.create :name=> @runseq.name,
121
+ :content_type=>"output", :data_text=> render_to_string(:inline=>@ui, :layout=>"utf8"),
122
+ :xmain=>@xmain, :runseq=>@runseq, :user=>current_ma_user,
123
+ :ip=> get_ip, :service=>service, :ma_display=>ma_display,
124
+ :ma_secured => @xmain.service.ma_secured,
125
+ :filename => "#{@runseq.code}.html.erb"
126
+ end
127
+ # @message = defined?(MSG_NEXT) ? MSG_NEXT : "Next &gt;"
128
+ @message = defined?(MSG_NEXT) ? MSG_NEXT : "Next >>"
129
+ @message = "Finish" if @runseq.end
130
+ ma_log("Todo defined?(NSG_NEXT : Next >>)")
131
+ eval "@xvars[@runseq.code] = url_for(:controller=>'jinda', :action=>'document', :id=>@doc.id)"
132
+ else
133
+ flash[:notice]= "Error: Can not find the view file for this controller"
134
+ ma_log "Error: Can not find the view file for this controller"
135
+ redirect_to_root
136
+ end
137
+ # Check if ma_display available
138
+ # ma_display= get_option("ma_display")
139
+ unless ma_display
140
+ end_action
141
+ end
142
+ # display from @ui
143
+ end
144
+
145
+ def run_mail
146
+ init_vars(params[:id])
147
+ service= @xmain.service
148
+ f= "app/views/#{service.module.code}/#{service.code}/#{@runseq.code}.html.erb"
149
+ @ui= File.read(f).html_safe
150
+ @doc= Jinda::Doc.create :name=> @runseq.name,
151
+ :content_type=>"mail", :data_text=> render_to_string(:inline=>@ui, :layout=>false),
152
+ :xmain=>@xmain, :runseq=>@runseq, :user=>current_ma_user,
153
+ :ip=> get_ip, :service=>service, :ma_display=>false,
154
+ :ma_secured => @xmain.service.ma_secured
155
+ eval "@xvars[:#{@runseq.code}] = url_for(:controller=>'jinda', :action=>'document', :id=>@doc.id)"
156
+ mail_from = get_option('from')
157
+ # sender= render_to_string(:inline=>mail_from) if mail_from
158
+ mail_to = get_option('to')
159
+ recipients= render_to_string(:inline=>mail_to) if mail_to
160
+ mail_subject = get_option('subject')
161
+ subject= render_to_string(:inline=>mail_subject) || "#{@runseq.name}"
162
+ JindaMailer.gmail(@doc.data_text, recipients, subject).deliver unless DONT_SEND_MAIL
163
+ end_action
164
+ end
165
+
166
+ def end_output
167
+ init_vars(params[:xmain_id])
168
+ end_action
169
+ end
170
+
171
+ def end_form
172
+ # Check error using xmain_id to redirect_to_root and return
173
+ if params[:xmain_id]
174
+ init_vars(params[:xmain_id])
175
+ else
176
+ ma_log "Known Bug : repeated end_form "
177
+ redirect_to_root and return
178
+ end
179
+ eval "@xvars[@runseq.code] = {} unless @xvars[@runseq.code]"
180
+ # Search for uploaded file name if exist
181
+ params.each { |k,v|
182
+ if params[k].respond_to? :original_filename
183
+ get_image(k, params[k])
184
+ # check if params of array in form eg: edit_article
185
+ elsif params[k].is_a?(ActionController::Parameters)
186
+ params[k].each { |k1,v1|
187
+ # eval "@xvars[@runseq.code][k1] = params.require(k1).permit(k1)"
188
+ eval "@xvars[@runseq.code][k1] = v1"
189
+ next unless v1.respond_to?(:original_filename)
190
+ get_image1(k, k1, params[k][k1])
191
+ }
192
+ else
193
+ # 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
194
+ # Solution:
195
+ # https://stackoverflow.com/questions/34949505/rails-5-unable-to-retrieve-hash-values-from-parameter
196
+ # v = v.to_unsafe_h unless v.class == String
197
+ # v = params.require[k] unless v.class == String
198
+ v = v.to_s unless v.class == String
199
+ # Create @xvars[@runseq.code]
200
+ eval "@xvars[@runseq.code][k] = v"
201
+ end
202
+ }
203
+ end_action
204
+ end
205
+
206
+ def end_action(next_runseq = nil)
207
+ # @runseq.status='F' unless @runseq_not_f
208
+ @xmain.xvars= @xvars
209
+ @xmain.status= 'R' # running
210
+ @xmain.save!
211
+ @runseq.status='F'
212
+ @runseq.user= current_ma_user
213
+ @runseq.stop= Time.now
214
+ @runseq.save
215
+ next_runseq= @xmain.runseqs.where(:rstep=> @runseq.rstep+1).first unless next_runseq
216
+ if @end_job || !next_runseq # job finish
217
+ @xmain.xvars= @xvars
218
+ @xmain.status= 'F' unless @xmain.status== 'E' # finish
219
+ @xmain.stop= Time.now
220
+ @xmain.save
221
+ if @xvars['p']['return']
222
+ redirect_to @xvars['p']['return'] and return
223
+ else
224
+ if @user
225
+ redirect_to :action=>'index' and return
226
+ else
227
+ redirect_to_root and return
228
+ end
229
+ end
230
+ else
231
+ @xmain.update_attribute :current_runseq, next_runseq.id
232
+ redirect_to :action=>'run', :id=>@xmain.id and return
233
+ end
234
+ end
235
+
236
+ end
@@ -1,72 +1,14 @@
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)
@@ -84,11 +26,12 @@ class JindaController < ApplicationController
84
26
  refresh_to "/", :alert => "Error: cannot process"
85
27
  end
86
28
  end
87
- ####################################################################################################]
29
+
30
+ ########################################################################]
88
31
  # run if, form, mail, output etc depend on icon in freemind
89
32
  # action from @r.action == do, form, if, output
90
33
  # Then will call def run_do, run_form, run_if, run_output
91
- ####################################################################################################]
34
+ ########################################################################]
92
35
  def run
93
36
  init_vars(params[:id])
94
37
  if authorize?
@@ -98,513 +41,5 @@ class JindaController < ApplicationController
98
41
  redirect_to_root
99
42
  end
100
43
  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
44
 
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
45
  end