erp_forms 2.0.6 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (82) hide show
  1. data/app/controllers/erp_forms/erp_app/desktop/dynamic_forms/base_controller.rb +4 -37
  2. data/app/controllers/erp_forms/erp_app/desktop/dynamic_forms/data_controller.rb +348 -82
  3. data/app/controllers/erp_forms/erp_app/desktop/dynamic_forms/forms_controller.rb +110 -15
  4. data/app/controllers/erp_forms/erp_app/desktop/dynamic_forms/models_controller.rb +19 -3
  5. data/app/mailers/dynamic_form_mailer.rb +35 -0
  6. data/app/models/dynamic_datum.rb +97 -22
  7. data/app/models/dynamic_form.rb +172 -98
  8. data/app/models/dynamic_form_document.rb +4 -11
  9. data/app/models/dynamic_form_model.rb +46 -22
  10. data/app/models/ticket.rb +4 -0
  11. data/app/views/dynamic_form_mailer/widget_email_with_attachments.html.erb +3 -0
  12. data/app/views/dynamic_form_mailer/widget_email_with_attachments.text.erb +3 -0
  13. data/app/widgets/dynamic_forms/base.rb +101 -19
  14. data/app/widgets/dynamic_forms/javascript/dynamic_forms.js +40 -38
  15. data/app/widgets/dynamic_forms/views/error.html.erb +1 -0
  16. data/db/data_migrations/20110608185830_create_default_dynamic_models_and_forms.rb +2 -0
  17. data/db/data_migrations/20120824013449_create_ticket_form.rb +1 -0
  18. data/db/data_migrations/20121026013449_update_ticket_form.rb +64 -0
  19. data/db/data_migrations/{20120904191738_update_contact_form.rb → 20121026191738_update_contact_form.rb} +4 -0
  20. data/db/migrate/20110530193446_dynamic_forms.rb +12 -0
  21. data/db/migrate/20121007022323_upgrade_dynamic_forms_table.rb +31 -0
  22. data/lib/erp_forms.rb +4 -0
  23. data/lib/erp_forms/dynamic_form_field.rb +31 -28
  24. data/lib/erp_forms/dynamic_grid_column.rb +2 -2
  25. data/lib/erp_forms/engine.rb +5 -0
  26. data/lib/erp_forms/extensions/active_record/acts_as_commentable.rb +33 -0
  27. data/lib/erp_forms/extensions/active_record/acts_as_dynamic_form_model.rb +80 -0
  28. data/lib/erp_forms/extensions/active_record/has_dynamic_forms.rb +4 -4
  29. data/lib/erp_forms/extensions/extensions.rb +2 -0
  30. data/lib/erp_forms/extensions/railties/action_view.rb +2 -2
  31. data/lib/erp_forms/version.rb +2 -2
  32. data/public/javascripts/erp_app/desktop/applications/dynamic_forms/center_region.js +2 -1
  33. data/public/javascripts/erp_app/desktop/applications/dynamic_forms/dynamic_data_grid.js +264 -82
  34. data/public/javascripts/erp_app/desktop/applications/dynamic_forms/form_builder.js +1476 -58
  35. data/public/javascripts/erp_app/desktop/applications/dynamic_forms/module.js +24 -1
  36. data/public/javascripts/erp_app/desktop/applications/dynamic_forms/west_region.js +493 -210
  37. data/public/javascripts/erp_app/shared/dynamic_forms/dynamic_form_fields.js +82 -20
  38. data/public/javascripts/erp_app/shared/dynamic_forms/dynamic_forms_validation.js +23 -3
  39. data/spec/dummy/config/application.rb +6 -2
  40. data/spec/dummy/config/environments/spec.rb +3 -0
  41. data/spec/dummy/db/data_migrations/20110109173616_create_capability_scope_types.erp_tech_svcs.rb +15 -0
  42. data/spec/dummy/db/data_migrations/20110525001935_add_usd_currency.erp_base_erp_svcs.rb +12 -0
  43. data/spec/dummy/db/data_migrations/20110608185830_create_default_dynamic_models_and_forms.erp_forms.rb +33 -0
  44. data/spec/dummy/db/data_migrations/20110609150135_add_iso_codes.erp_base_erp_svcs.rb +19 -0
  45. data/spec/dummy/db/data_migrations/20110728201729_erp_app_setup.erp_app.rb +252 -0
  46. data/spec/dummy/db/data_migrations/20110728201733_update_preferences.erp_app.rb +53 -0
  47. data/spec/dummy/db/data_migrations/20110802200222_schedule_delete_expired_sessions_job.erp_tech_svcs.rb +16 -0
  48. data/spec/dummy/db/data_migrations/20110816161238_create_desktop_app_audit_log_viewer.erp_app.rb +21 -0
  49. data/spec/dummy/db/data_migrations/20110817160743_add_file_manager_application.erp_app.rb +32 -0
  50. data/spec/dummy/db/data_migrations/20110828190913_create_desktop_app_dynamic_forms.erp_forms.rb +19 -0
  51. data/spec/dummy/db/data_migrations/20110913145838_setup_compass_ae_instance.erp_base_erp_svcs.rb +12 -0
  52. data/spec/dummy/db/data_migrations/20111108183739_add_default_capabilities.erp_app.rb +23 -0
  53. data/spec/dummy/db/data_migrations/20111108183740_add_new_contact_widgets.erp_app.rb +42 -0
  54. data/spec/dummy/db/data_migrations/20111111144706_setup_audit_log_types.erp_tech_svcs.rb +22 -0
  55. data/spec/dummy/db/data_migrations/20120109173616_create_download_capability_type.erp_tech_svcs.rb +14 -0
  56. data/spec/dummy/db/data_migrations/20120229160222_add_userinfo_widget.erp_app.rb +29 -0
  57. data/spec/dummy/db/data_migrations/20120405193721_create_party_and_role_type_for_communication_events.erp_app.rb +11 -0
  58. data/spec/dummy/db/data_migrations/20120411180756_create_user_management_mobile_application.erp_app.rb +19 -0
  59. data/spec/dummy/db/data_migrations/20120418164215_create_configuration_management_desktop_application.erp_app.rb +23 -0
  60. data/spec/dummy/db/data_migrations/20120824013449_create_ticket_form.erp_forms.rb +67 -0
  61. data/spec/dummy/db/data_migrations/20121026013449_update_ticket_form.erp_forms.rb +65 -0
  62. data/spec/dummy/db/data_migrations/20121026191738_update_contact_form.erp_forms.rb +27 -0
  63. data/spec/dummy/db/data_migrations/20121116155018_create_group_relationship_and_role_types.erp_tech_svcs.rb +20 -0
  64. data/spec/dummy/db/data_migrations/20121130201859_upgrade_remove_system_mgmt_app.erp_app.rb +16 -0
  65. data/spec/dummy/db/data_migrations/20121130212146_note_capabilities.erp_tech_svcs.rb +24 -0
  66. data/spec/dummy/db/data_migrations/20121218175028_create_security_management_desktop_application.erp_app.rb +23 -0
  67. data/spec/dummy/db/migrate/20121213234847_base_erp_services.erp_base_erp_svcs.rb +461 -0
  68. data/spec/dummy/db/migrate/20121213234848_base_tech_services.erp_tech_svcs.rb +255 -0
  69. data/spec/dummy/db/migrate/20121213234849_create_has_attribute_tables.erp_tech_svcs.rb +39 -0
  70. data/spec/dummy/db/migrate/20121213234850_base_app_framework.erp_app.rb +276 -0
  71. data/spec/dummy/db/migrate/20121213234851_dynamic_forms.erp_forms.rb +95 -0
  72. data/spec/dummy/db/migrate/20121213234852_create_tickets.erp_forms.rb +19 -0
  73. data/spec/dummy/db/migrate/20121213234853_upgrade_dynamic_forms_table.erp_forms.rb +32 -0
  74. data/spec/dummy/db/migrate/20130107181041_create_groups.erp_tech_svcs.rb +19 -0
  75. data/spec/dummy/db/migrate/20130107181042_upgrade_security.erp_tech_svcs.rb +54 -0
  76. data/spec/dummy/db/migrate/20130107181043_upgrade_security2.erp_tech_svcs.rb +270 -0
  77. data/spec/dummy/db/schema.rb +879 -0
  78. data/spec/dummy/db/spec.sqlite3 +0 -0
  79. data/spec/dummy/log/spec.log +13580 -0
  80. data/spec/models/dynamic_form_model_spec.rb +1 -1
  81. data/spec/spec_helper.rb +11 -3
  82. metadata +136 -14
@@ -1,47 +1,14 @@
1
1
  class ErpForms::ErpApp::Desktop::DynamicForms::BaseController < ::ErpApp::Desktop::BaseController
2
- IGNORED_PARAMS = %w{action controller uuid widget_name widget_action dynamic_form_id dynamic_form_model_id model_name use_dynamic_form authenticity_token}
2
+ @@datetime_format = "%m/%d/%Y %l:%M%P"
3
+ IGNORED_PARAMS = %w{action controller uuid widget_name widget_action dynamic_form_id dynamic_form_model_id model_name use_dynamic_form authenticity_token file}
3
4
 
4
- protected
5
- def build_section_hash(website_section, website)
6
- website_section_hash = {
7
- :text => website_section.title,
8
- :siteName => website.title,
9
- :siteId => website.id,
10
- :type => website_section.type,
11
- :isSecured => !website_section.roles.empty?,
12
- :isSection => true,
13
- :inMenu => website_section.in_menu,
14
- :hasLayout => !website_section.layout.blank?,
15
- :id => "section_#{website_section.id}",
16
- :url => "http://#{website.hosts.first.host}/#{website_section.permalink}"
17
- }
18
-
19
- if website_section.is_a?(Blog) || website_section.type == 'Blog'
20
- website_section_hash[:isBlog] = true
21
- website_section_hash[:iconCls] = 'icon-blog'
22
- website_section_hash[:leaf] = true
23
- else
24
- unless website_section.children.empty?
25
- website_section_hash[:leaf] = false
26
- website_section_hash[:children] = []
27
- website_section.positioned_children.each do |child|
28
- website_section_hash[:children] << build_section_hash(child, website)
29
- end
30
- else
31
- website_section_hash[:leaf] = true
32
- end
33
- website_section_hash[:iconCls] = website_section_hash[:isSecured] ? 'icon-document_lock' : 'icon-document'
34
- end
35
-
36
- website_section_hash
37
- end
38
-
5
+ protected
39
6
  def page
40
7
  offset = params[:start].to_f
41
8
  offset > 0 ? (offset / params[:limit].to_f).to_i + 1 : 1
42
9
  end
43
10
 
44
11
  def per_page
45
- params[:limit].nil? ? 20 : params[:limit].to_i
12
+ params[:limit].nil? ? 20 : params[:limit].to_i
46
13
  end
47
14
  end
@@ -1,124 +1,390 @@
1
1
  module ErpForms::ErpApp::Desktop::DynamicForms
2
2
  class DataController < ErpForms::ErpApp::Desktop::DynamicForms::BaseController
3
- @@datetime_format = "%m/%d/%Y %l:%M%P"
3
+ before_filter :set_file_support
4
4
 
5
5
  # setup dynamic data grid
6
6
  def setup
7
- form = DynamicForm.get_form(params[:model_name])
8
- definition = form.definition_object
9
-
10
- columns = []
11
- definition.each do |field_hash|
12
- if field_hash[:display_in_grid]
13
- field_hash[:width] = (field_hash[:width].to_f * 0.56).round.to_i # for some reason grid column widths are greater than form field widths
14
- columns << DynamicGridColumn.build_column(field_hash)
7
+ begin
8
+ form = DynamicForm.get_form(params[:model_name])
9
+ raise "No Default Form found for this model." if form.nil?
10
+ definition = form.definition_object
11
+
12
+ columns = []
13
+ definition.each do |field_hash|
14
+ if field_hash[:display_in_grid]
15
+ # for some reason grid column widths are greater than form field widths
16
+ field_hash[:width] = (field_hash[:width].to_f * 0.56).round.to_i unless field_hash[:width].nil?
17
+ columns << DynamicGridColumn.build_column(field_hash)
18
+ end
15
19
  end
16
- end
17
20
 
18
- columns << DynamicGridColumn.build_column({ :fieldLabel => "Updated By", :name => 'updated_username', :xtype => 'textfield' })
19
- columns << DynamicGridColumn.build_column({ :fieldLabel => "Created By", :name => 'created_username', :xtype => 'textfield' })
20
- columns << DynamicGridColumn.build_column({ :fieldLabel => "Created At", :name => 'created_at', :xtype => 'datefield', :width => 75 })
21
- columns << DynamicGridColumn.build_column({ :fieldLabel => "Updated At", :name => 'updated_at', :xtype => 'datefield', :width => 75 })
22
- columns << DynamicGridColumn.build_edit_column("Ext.getCmp('#{params[:model_name]}').editRecord(rec,'#{params[:model_name]}');")
23
- columns << DynamicGridColumn.build_delete_column("Ext.getCmp('#{params[:model_name]}').deleteRecord(rec,'#{params[:model_name]}');")
21
+ columns << DynamicGridColumn.build_column({ :fieldLabel => "Created By", :name => 'created_username', :xtype => 'textfield', :width => 100 })
22
+ columns << DynamicGridColumn.build_column({ :fieldLabel => "Created At", :name => 'created_at', :xtype => 'datefield', :width => 115 })
23
+ columns << DynamicGridColumn.build_column({ :fieldLabel => "Updated By", :name => 'updated_username', :xtype => 'textfield', :width => 100 })
24
+ columns << DynamicGridColumn.build_column({ :fieldLabel => "Updated At", :name => 'updated_at', :xtype => 'datefield', :width => 115 })
24
25
 
25
- definition << DynamicFormField.textfield({ :fieldLabel => "Updated By", :name => 'updated_username' })
26
- definition << DynamicFormField.textfield({ :fieldLabel => "Created By", :name => 'created_username' })
27
- definition << DynamicFormField.datefield({ :fieldLabel => "Created At", :name => 'created_at' })
28
- definition << DynamicFormField.datefield({ :fieldLabel => "Updated At", :name => 'updated_at' })
29
- definition << DynamicFormField.hidden({ :fieldLabel => "ID", :name => 'id' })
30
- definition << DynamicFormField.hidden({ :fieldLabel => "Form ID", :name => 'form_id' })
31
- definition << DynamicFormField.hidden({ :fieldLabel => "Model Name", :name => 'model_name' })
26
+ definition << DynamicFormField.textfield({ :fieldLabel => "Created By", :name => 'created_username' })
27
+ definition << DynamicFormField.datefield({ :fieldLabel => "Created At", :name => 'created_at' })
28
+ definition << DynamicFormField.textfield({ :fieldLabel => "Updated By", :name => 'updated_username' })
29
+ definition << DynamicFormField.datefield({ :fieldLabel => "Updated At", :name => 'updated_at' })
30
+ definition << DynamicFormField.hiddenfield({ :fieldLabel => "ID", :name => 'id' })
31
+ definition << DynamicFormField.hiddenfield({ :fieldLabel => "Form ID", :name => 'form_id' })
32
+ definition << DynamicFormField.hiddenfield({ :fieldLabel => "Model Name", :name => 'model_name' })
32
33
 
33
- render :inline => "{
34
- \"success\": true,
35
- \"columns\": [#{columns.join(',')}],
36
- \"fields\": #{definition.to_json}
37
- }"
34
+ render :inline => "{
35
+ \"success\": true,
36
+ \"columns\": [#{columns.join(',')}],
37
+ \"fields\": #{definition.to_json}
38
+ }"
39
+ rescue Exception => e
40
+ Rails.logger.error e.message
41
+ Rails.logger.error e.backtrace.join("\n")
42
+ render :inline => {
43
+ :success => false,
44
+ :message => e.message
45
+ }.to_json
46
+ end
38
47
  end
39
48
 
40
49
  # get dynamic data records
41
50
  def index
42
- sort = params[:sort] || 'created_at'
43
- dir = params[:dir] || 'DESC'
44
-
45
- myDynamicObject = DynamicFormModel.get_constant(params[:model_name])
46
-
47
- dynamic_records = myDynamicObject.paginate(:page => page, :per_page => per_page, :order => "#{sort} #{dir}")
48
- related_fields = dynamic_records.first.form.related_fields rescue []
49
-
50
- wi = []
51
- dynamic_records.each do |i|
52
- wihash = i.data.dynamic_attributes_with_related_data(related_fields, false)
53
- wihash[:id] = i.id
54
- wihash[:created_username] = i.data.created_by.nil? ? '' : i.data.created_by.username
55
- wihash[:updated_username] = i.data.updated_by.nil? ? '' : i.data.updated_by.username
56
- wihash[:created_at] = i.data.created_at
57
- wihash[:updated_at] = i.data.updated_at
58
- wihash[:form_id] = (i.data.updated_with_form_id ? i.data.updated_with_form_id : i.data.created_with_form_id)
59
- wihash[:model_name] = params[:model_name]
60
- wi << wihash
61
- end
51
+ begin
52
+ sort = (params[:sort] || 'created_at').downcase
53
+ dir = (params[:dir] || 'desc').downcase
54
+ query_filter = params[:query_filter].strip rescue nil
55
+
56
+ myDynamicObject = DynamicFormModel.get_constant(params[:model_name])
62
57
 
63
- render :inline => "{ total:#{dynamic_records.total_entries}, data:#{wi.to_json} }"
58
+ if $USE_SOLR_FOR_DYNAMIC_FORM_MODELS and myDynamicObject.is_searchable?
59
+ solr_search_results = myDynamicObject.search do
60
+ keywords query_filter unless params[:query_filter].blank?
61
+ paginate(:page => page, :per_page => per_page)
62
+ order_by(sort.to_sym, dir.to_sym)
63
+ end
64
+ dynamic_records = solr_search_results.results
65
+ else
66
+ dynamic_records = myDynamicObject.paginate(:page => page, :per_page => per_page, :order => "#{sort} #{dir}")
67
+ dynamic_records = dynamic_records.joins(:dynamic_data).where("UPPER(dynamic_data.dynamic_attributes) LIKE UPPER('%#{query_filter}%')") unless params[:query_filter].blank?
68
+ end
69
+
70
+ related_fields = dynamic_records.first.form.related_fields rescue []
71
+
72
+ wi = []
73
+ dynamic_records.each do |i|
74
+ wihash = i.data.dynamic_attributes_with_related_data(related_fields, false)
75
+ wihash[:id] = i.id
76
+ wihash[:created_username] = i.data.created_by.nil? ? '' : i.data.created_by.username
77
+ wihash[:updated_username] = i.data.updated_by.nil? ? '' : i.data.updated_by.username
78
+ wihash[:created_at] = i.data.created_at.getlocal.strftime(@@datetime_format)
79
+ wihash[:updated_at] = i.data.updated_at.getlocal.strftime(@@datetime_format)
80
+ wihash[:form_id] = (i.data.updated_with_form_id ? i.data.updated_with_form_id : i.data.created_with_form_id)
81
+ wihash[:model_name] = params[:model_name]
82
+ wi << wihash
83
+ end
84
+
85
+ render :inline => "{ total:#{dynamic_records.total_entries}, data:#{wi.to_json} }"
86
+ rescue Exception => e
87
+ Rails.logger.error e.message
88
+ Rails.logger.error e.backtrace.join("\n")
89
+ render :inline => {
90
+ :success => false,
91
+ :message => e.message
92
+ }.to_json
93
+ end
64
94
  end
65
95
 
96
+ # get a single record with sorted_dynamic_attributes
66
97
  def get
67
- myDynamicObject = DynamicFormModel.get_constant(params[:model_name])
68
- @record = myDynamicObject.find(params[:id])
98
+ @record = DynamicFormModel.get_constant(params[:model_name]).find(params[:id])
99
+
100
+ data = @record.data.sorted_dynamic_attributes
101
+ result_hash = {:success => true, :data => data, :metadata => get_metadata, :comments => get_comments, :has_file_assets => has_file_assets?}
102
+
103
+ render :json => (@record ? result_hash : {:success => false})
104
+ end
105
+
106
+ # get a single record with dynamic_attributes_with_related_data
107
+ def get_with_related_data
108
+ @record = DynamicFormModel.get_constant(params[:model_name]).find(params[:id])
69
109
 
70
110
  related_fields = @record.form.related_fields
71
111
  data = @record.data.dynamic_attributes_with_related_data(related_fields, true)
112
+ result_hash = {:success => true, :data => data, :metadata => get_metadata, :comments => get_comments, :has_file_assets => has_file_assets?}
113
+
114
+ render :json => (@record ? result_hash : {:success => false})
115
+ end
116
+
117
+ # create a dynamic data record
118
+ def create
119
+ begin
120
+ check_file_upload_size
121
+
122
+ form_data = JSON.parse(params[:form_data_json])
123
+ form_data[:dynamic_form_id] = params[:dynamic_form_id]
124
+ form_data[:model_name] = params[:model_name]
125
+ form_data.symbolize_keys!
126
+
127
+ @record = DynamicFormModel.get_instance(params[:model_name])
128
+
129
+ form_data[:created_by] = current_user unless current_user.nil?
130
+ form_data[:created_with_form_id] = params[:dynamic_form_id] if params[:dynamic_form_id]
131
+ @record = @record.save_all_attributes(form_data, ErpForms::ErpApp::Desktop::DynamicForms::BaseController::IGNORED_PARAMS)
132
+ is_secure = (@record.file_security_default == 'private')
133
+ save_file_asset(form_data, is_secure) unless params[:file].nil?
134
+
135
+ data = @record.data.sorted_dynamic_attributes
136
+ result_hash = {
137
+ :success => true,
138
+ :id => @record.id,
139
+ :model_name => params[:model_name],
140
+ :form_id => form_data[:created_with_form_id],
141
+ :data => data,
142
+ :metadata => get_metadata,
143
+ :comments => get_comments,
144
+ :has_file_assets => has_file_assets?
145
+ }
146
+ render :inline => @record ? result_hash.to_json : {:success => false}.to_json
147
+ rescue Exception => e
148
+ Rails.logger.error e.message
149
+ Rails.logger.error e.backtrace.join("\n")
150
+ render :inline => {
151
+ :success => false,
152
+ :message => e.message
153
+ }.to_json
154
+ end
155
+ end
156
+
157
+ # update a dynamic data record
158
+ def update
159
+ begin
160
+ check_file_upload_size
161
+
162
+ form_data = JSON.parse(params[:form_data_json])
163
+ form_data[:dynamic_form_id] = params[:dynamic_form_id]
164
+ form_data[:model_name] = params[:model_name]
165
+ form_data.symbolize_keys!
166
+
167
+ @record = DynamicFormModel.get_constant(params[:model_name]).find(params[:id])
168
+
169
+ form_data[:updated_by] = current_user unless current_user.nil?
170
+ form_data[:updated_with_form_id] = params[:dynamic_form_id] if params[:dynamic_form_id]
171
+ @record = @record.save_all_attributes(form_data, ErpForms::ErpApp::Desktop::DynamicForms::BaseController::IGNORED_PARAMS)
172
+ save_file_asset(form_data, nil) unless params[:file].nil?
173
+
174
+ data = @record.data.sorted_dynamic_attributes
175
+ result_hash = {
176
+ :success => true,
177
+ :id => params[:id],
178
+ :model_name => params[:model_name],
179
+ :form_id => form_data[:updated_with_form_id],
180
+ :data => data,
181
+ :metadata => get_metadata,
182
+ :comments => get_comments,
183
+ :has_file_assets => has_file_assets?
184
+ }
185
+ render :inline => @record ? result_hash.to_json : {:success => false}.to_json
186
+ rescue Exception => e
187
+ Rails.logger.error e.message
188
+ Rails.logger.error e.backtrace.join("\n")
189
+ render :inline => {
190
+ :success => false,
191
+ :message => e.message
192
+ }.to_json
193
+ end
194
+ end
195
+
196
+ # delete a dynamic data record
197
+ def delete
198
+ begin
199
+ @record = DynamicFormModel.get_constant(params[:model_name])
200
+ @record.destroy(params[:id])
201
+ render :json => {:success => true}
202
+ rescue Exception => e
203
+ Rails.logger.error e.message
204
+ Rails.logger.error e.backtrace.join("\n")
205
+ render :inline => {
206
+ :success => false,
207
+ :message => e.message
208
+ }.to_json
209
+ end
210
+ end
211
+
212
+ # file tree
213
+ def get_files
214
+ @record = DynamicFormModel.get_constant(params[:model_name]).find(params[:id])
215
+ if @record.nil?
216
+ render :json => []
217
+ else
218
+ set_root_node(params)
219
+ render :json => @file_support.build_tree(base_path, :file_asset_holder => @record, :preload => true)
220
+ end
221
+ end
222
+
223
+ # for plupload via filetree
224
+ def upload_file
225
+ result = {}
226
+ upload_path = params[:directory]
227
+ name = params[:name]
228
+ data = request.raw_post
229
+
230
+ begin
231
+ @record = DynamicFormModel.get_constant(params[:model_name]).find(params[:id])
232
+ set_root_node(params)
233
+ file = @record.add_file(data, File.join(@file_support.root,base_path,name))
234
+
235
+ is_secure = (@record.file_security_default == 'private')
236
+ set_file_security(file, is_secure)
237
+
238
+ result = {:success => true}
239
+ rescue Exception => e
240
+ Rails.logger.error e.message
241
+ Rails.logger.error e.backtrace.join("\n")
242
+ result = {:success => false, :error => "Error uploading file. #{e.message}"}
243
+ end
244
+
245
+ render :inline => result.to_json
246
+ end
247
+
248
+ # toggle security on file
249
+ def update_file_security
250
+ begin
251
+ path = params[:path]
252
+ secure = params[:secure]
253
+
254
+ @record = DynamicFormModel.get_constant(params[:model_name]).find(params[:id])
255
+ file = @record.files.find(:first, :conditions => ['name = ? and directory = ?', ::File.basename(path), ::File.dirname(path)])
256
+
257
+ set_file_security(file, (secure == 'true'))
72
258
 
259
+ render :json => {:success => true}
260
+ rescue Exception => e
261
+ Rails.logger.error e.message
262
+ Rails.logger.error e.backtrace.join("\n")
263
+ render :inline => {
264
+ :success => false,
265
+ :message => e.message
266
+ }.to_json
267
+ end
268
+ end
269
+
270
+ # file tree
271
+ def delete_file
272
+ messages = []
273
+
274
+ nodes_to_delete = (params[:selected_nodes] ? JSON(params[:selected_nodes]) : [params[:node]])
275
+
276
+ begin
277
+ @record = DynamicFormModel.get_constant(params[:model_name]).find(params[:id])
278
+ result = false
279
+ nodes_to_delete.each do |path|
280
+ path = "#{path}/" if params[:leaf] == 'false' and path.match(/\/$/).nil?
281
+ begin
282
+ name = File.basename(path)
283
+ result, message, is_folder = @file_support.delete_file(File.join(@file_support.root,path))
284
+ if result and !is_folder
285
+ file = @record.files.find(:first, :conditions => ['name = ? and directory = ?', ::File.basename(path), ::File.dirname(path)])
286
+ file.destroy
287
+ end
288
+ messages << message
289
+ rescue Exception => e
290
+ Rails.logger.error e.message
291
+ Rails.logger.error e.backtrace.join("\n")
292
+ render :json => {:success => false, :error => "Error deleting #{name}"} and return
293
+ end
294
+ end # end nodes_to_delete.each
295
+ if result
296
+ render :json => {:success => true, :message => messages.join(',')}
297
+ else
298
+ render :json => {:success => false, :error => messages.join(',')}
299
+ end
300
+ rescue ErpTechSvcs::Utils::CompassAccessNegotiator::Errors::UserDoesNotHaveCapability => ex
301
+ render :json => {:success => false, :message => ex.message}
302
+ end
303
+ end
304
+
305
+ protected
306
+ def get_metadata
73
307
  metadata = {
74
308
  :created_username => (@record.data.created_by.nil? ? '' : @record.data.created_by.username),
75
309
  :updated_username => (@record.data.updated_by.nil? ? '' : @record.data.updated_by.username),
76
310
  :created_at => @record.data.created_at.getlocal.strftime(@@datetime_format),
77
311
  :updated_at => @record.data.updated_at.getlocal.strftime(@@datetime_format)
78
312
  }
313
+ end
79
314
 
80
- result_hash = {:success => true, :data => data, :metadata => metadata}
315
+ def has_file_assets?
316
+ @record.allow_files? and @record.respond_to?(:files)
317
+ end
81
318
 
82
- if @record.respond_to?(:comments)
83
- result_hash[:comments] = @record.comments.order('id ASC').all
84
- result_hash[:comments].each_with_index do |c, i|
85
- result_hash[:comments][i] = c.to_hash
86
- result_hash[:comments][i][:created_at] = c.created_at.getlocal.strftime(@@datetime_format)
87
- result_hash[:comments][i][:updated_at] = c.updated_at.getlocal.strftime(@@datetime_format)
319
+ def get_comments
320
+ if @record.allow_comments? and @record.respond_to?(:comments)
321
+ comments = @record.comments.order('id ASC').all
322
+ comments.each_with_index do |c, i|
323
+ comments[i] = c.to_hash
324
+ comments[i][:created_at] = c.created_at.getlocal.strftime(@@datetime_format)
325
+ comments[i][:updated_at] = c.updated_at.getlocal.strftime(@@datetime_format)
88
326
  end
89
327
  end
328
+ comments
329
+ end
90
330
 
91
- render :json => @record ? result_hash : {:success => false}
331
+ def check_file_upload_size
332
+ unless params[:file].nil?
333
+ if params[:file].tempfile.size > ErpTechSvcs::Config.max_file_size_in_mb.megabytes
334
+ raise "File cannot be larger than #{ErpTechSvcs::Config.max_file_size_in_mb}MB"
335
+ end
336
+ end
92
337
  end
93
338
 
94
- # create a dynamic data record
95
- def create
96
- @myDynamicObject = DynamicFormModel.get_instance(params[:model_name])
339
+ def save_file_asset(form_data, is_secure)
340
+ result = {}
341
+ name = params[:file].original_filename
342
+ data = params[:file].tempfile
97
343
 
98
- params[:created_by] = current_user unless current_user.nil?
99
- params[:created_with_form_id] = params[:dynamic_form_id] if params[:dynamic_form_id]
100
- @myDynamicObject = DynamicFormModel.save_all_attributes(@myDynamicObject, params, ErpForms::ErpApp::Desktop::DynamicForms::BaseController::IGNORED_PARAMS)
101
-
102
- render :json => @myDynamicObject ? {:success => true} : {:success => false}
103
- end
344
+ begin
345
+ set_root_node(form_data)
346
+ file = @record.add_file(data, File.join(@file_support.root, base_path, name))
104
347
 
105
- # update a dynamic data record
106
- def update
107
- @myDynamicObject = DynamicFormModel.get_constant(params[:model_name]).find(params[:id])
348
+ set_file_security(file, is_secure)
349
+
350
+ return {:success => true}
351
+ rescue Exception => e
352
+ Rails.logger.error e.message
353
+ Rails.logger.error e.backtrace
354
+ raise "Error uploading file. #{e.message}"
355
+ end
356
+ end
357
+
358
+ def set_file_security(file, is_secure)
359
+ unless is_secure.nil?
360
+ is_secure = (is_secure == 'true' ? true : false) if is_secure.is_a?(String)
361
+
362
+ if is_secure
363
+ c = file.add_capability(:download)
364
+ roles = ['admin', @record.role_iid]
365
+ roles.each do |r|
366
+ role = SecurityRole.find_by_internal_identifier(r)
367
+ role.add_capability(c)
368
+ end
369
+ else
370
+ file.remove_capability(:download)
371
+ end
372
+
373
+ # if we're using S3, set file permissions to private or public_read
374
+ @file_support.set_permissions(File.join(file.directory,file.name).sub(%r{^/},''), ((is_secure == 'true') ? :private : :public_read)) if ErpTechSvcs::Config.file_storage == :s3
375
+ end
376
+ end
108
377
 
109
- params[:updated_by] = current_user unless current_user.nil?
110
- params[:updated_with_form_id] = params[:dynamic_form_id] if params[:dynamic_form_id]
111
- @myDynamicObject = DynamicFormModel.save_all_attributes(@myDynamicObject, params, ErpForms::ErpApp::Desktop::DynamicForms::BaseController::IGNORED_PARAMS)
112
-
113
- render :json => @myDynamicObject ? {:success => true} : {:success => false}
378
+ def set_root_node(form_data)
379
+ @root_node = File.join(ErpTechSvcs::Config.file_assets_location, form_data[:model_name], @record.id.to_s)
114
380
  end
115
381
 
116
- # delete a dynamic data record
117
- def delete
118
- @myDynamicObject = DynamicFormModel.get_constant(params[:model_name])
119
- @myDynamicObject.destroy(params[:id])
120
- render :json => {:success => true}
382
+ def base_path
383
+ @base_path = (@root_node.nil? ? nil : File.join(@file_support.root, @root_node))
121
384
  end
122
-
385
+
386
+ def set_file_support
387
+ @file_support = ErpTechSvcs::FileSupport::Base.new(:storage => ErpTechSvcs::Config.file_storage)
388
+ end
123
389
  end
124
390
  end