Class: ErpForms::ErpApp::Desktop::DynamicForms::FormsController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/erp_forms/erp_app/desktop/dynamic_forms/forms_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject

create dynamic form


155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'app/controllers/erp_forms/erp_app/desktop/dynamic_forms/forms_controller.rb', line 155

def create
  if params[:form_definition] and params[:description] and params[:model_name]
    dform = DynamicForm.new
    dform = assign_form_attributes(dform)
    dform.dynamic_form_model_id = DynamicFormModel.find_by_model_name(params[:model_name]).id
    dform.default = false
    dform.created_by_id = current_user.id

    if dform.save
      # update solr config for model 
      DynamicFormModel.get_constant(dform.model_name).sunspot_setup if ErpForms.use_solr?

      render :json => {:success => true, :id => dform.id}
    else
      render :json => {:success => false}
    end    
  else
    render :json => {:success => false, :error => 'Insufficient info to create form.'}
  end
end

#deleteObject

delete dynamic form


124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'app/controllers/erp_forms/erp_app/desktop/dynamic_forms/forms_controller.rb', line 124

def delete
  dform = DynamicForm.find_by_id(params[:id])
  unless dform.nil?
    begin
       dform.destroy 
       render :json => {:success => true}
    rescue Exception => e
      render :json => {:success => false, :error => e.message} 
    end         
  else
    render :json => {:success => false, :error => 'Could not find form.'}
  end
end

#getObject

get a single form


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/controllers/erp_forms/erp_app/desktop/dynamic_forms/forms_controller.rb', line 79

def get
  dform = DynamicForm.find_by_id(params[:id]) if params[:id]
  dform = DynamicForm.get_form(params[:model_name], params[:internal_identifier]) if dform.nil? and params[:model_name]

  if dform.nil? 
    render :json => {:success => false, :error => "Don't know how to find form"} and return
  end      

  form = dform.to_extjs_formpanel(
              { :url => "/erp_forms/erp_app/desktop/dynamic_forms/data/#{params[:form_action]}",
                :record_id => params[:record_id]
              })

  render :json => form
end

#get_definitionObject

get a single form definition


53
54
55
56
57
58
59
60
61
62
# File 'app/controllers/erp_forms/erp_app/desktop/dynamic_forms/forms_controller.rb', line 53

def get_definition
  dform = DynamicForm.find_by_id(params[:id]) if params[:id]
  dform = DynamicForm.get_form(params[:model_name], params[:internal_identifier]) if dform.nil? and params[:model_name]

  if dform.nil?
    render :json => {:success => false}
  else
    render :json => dform.definition
  end
end

#get_recordObject

get a single form record


65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/controllers/erp_forms/erp_app/desktop/dynamic_forms/forms_controller.rb', line 65

def get_record
  dform = DynamicForm.find(params[:id]) rescue nil

  unless dform.nil?
    dform_hash = dform.to_hash
    dform_hash[:created_by] = dform.created_by.username rescue 'Unknown'
    dform_hash[:updated_by] = dform.updated_by.username rescue 'Unknown'
    dform_hash[:created_at] = dform.created_at.getlocal.strftime(@@datetime_format)
    dform_hash[:updated_at] = dform.updated_at.getlocal.strftime(@@datetime_format)      
  end
  render :json => [dform_hash]
end

#get_treeObject

get tree of dynamic models and forms


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/controllers/erp_forms/erp_app/desktop/dynamic_forms/forms_controller.rb', line 4

def get_tree
  models = params[:node] == "root" ? DynamicFormModel.all(:order => :model_name) : DynamicFormModel.find_all_by_id(params[:id])
  tree = []

  models.each do |form_model|
    # DynamicFormDocument only exists for the purpose of Extending
    next if form_model.model_name == 'DynamicFormDocument'
    
    model_hash = {
      :text => form_model.model_name,
      :iconCls => 'icon-data',
      :id => "form_model_#{form_model.id}",
      :formModelId => form_model.id,
      :isFormModel => true, 
      :allow_comments => form_model.allow_comments, 
      :allow_files => form_model.allow_files, 
      :file_security_default => form_model.file_security_default, 
      :show_in_multitask => form_model.show_in_multitask, 
      :isForm => false, 
      :leaf => false,
      :expanded => true,
      :children => []
    }

    #handle sections
    form_model.dynamic_forms.each do |form|
      form_hash = {
        :text => form.description, 
        :iconCls => (form.default ? 'icon-document_ok' : 'icon-document'), 
        :id => "form_#{form.id}",
        :formId => form.id, 
        :isFormModel => false, 
        :isDefaultForm => form.default,
        :formModelName => form_model.model_name,
        :isForm => true, 
        :leaf => true 
      }

      model_hash[:children] << form_hash
    end
          
    #added website to main tree
    tree << model_hash
  end

  render :json => tree
end

get related data for a related field


96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'app/controllers/erp_forms/erp_app/desktop/dynamic_forms/forms_controller.rb', line 96

def related_field
  if params[:model].blank? or (params[:displayField].blank? and params[:search_fields].blank?)
    render :inline => '[]'
  else
    related_model = params[:model].camelize.constantize
    query = related_model

    unless params[:search_fields].blank?
      #related_searchbox
      search_fields = params[:search_fields].split(',')
      unless params[:query].blank?
        sql = ''
        search_fields.each_with_index do |f,i|
          sql += " OR " if i > 0
          sql += "UPPER(#{f}) LIKE UPPER('%#{params[:query]}%')"
        end
        query = query.where(sql)
      end
      query = query.paginate(:page => page, :per_page => per_page)
    end

    total = query.count
    data = query.all
    render :inline => { :data => data, :total => total}.to_json
  end
end

#updateObject

update dynamic form


139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'app/controllers/erp_forms/erp_app/desktop/dynamic_forms/forms_controller.rb', line 139

def update
  dform = DynamicForm.find_by_id(params[:id])
  dform = assign_form_attributes(dform)
  dform.updated_by_id = current_user.id

  if dform.save
    # update solr config for model 
    DynamicFormModel.get_constant(dform.model_name).sunspot_setup if ErpForms.use_solr?

    render :json => {:success => true}
  else
    render :json => {:success => false}
  end
end