Class: Admin::FormController

Inherits:
AdminController
  • Object
show all
Defined in:
app/controllers/admin/form_controller.rb

Instance Method Summary collapse

Instance Method Details

#browseObject



68
69
70
71
72
73
74
75
76
# File 'app/controllers/admin/form_controller.rb', line 68

def browse
  @form = Form.find_sys_id(_sid, params[:id])

  @subs = @form.form_submissions.sys(_sid)
  if params[:sub_id]
    @subs = @subs.where(:id=>params[:sub_id]) 
  end  
  @subs = @subs.page(params[:page]).per(1)
end

#createObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'app/controllers/admin/form_controller.rb', line 138

def create
  @form = Form.new(params[:form])
  @form.system_id = _sid
  @form.locked_for_delete = true

  @form.submit_label = "Save" if @form.submit_label.is_blank?
  if @form.save
    Activity.add(_sid, "Form '#{@form.title}' created", current_user, "Form")

    redirect_to "/admin/form/#{@form.id}", :notice => "Successfully created Form"
  else
    render :action => 'new'
  end
end

#delete_submissionObject



167
168
169
170
171
172
173
174
# File 'app/controllers/admin/form_controller.rb', line 167

def delete_submission
  @sub = FormSubmission.find_sys_id(_sid, params[:id])
  @sub.user.ban!(current_user.id) if @sub.user
  @sub.destroy
  Activity.add(_sid, "Form '#{@sub.form.title}' record #{@sub.id} deleted", current_user)

  redirect_to params[:after]
end

#destroyObject



185
186
187
188
189
190
191
192
193
194
# File 'app/controllers/admin/form_controller.rb', line 185

def destroy
  @form = Form.find_sys_id(_sid, params[:id])
  if @form.locked_for_delete==1
    redirect_to "/admin/form/#{@form.id}", :notice=>"Cannot delete because form is locked" and return
  end
  Activity.add(_sid, "Form '#{@form.title}' deleted", current_user, "Form")
  @form.destroy

  redirect_to admin_forms_url, :notice => "Successfully destroyed form"
end

#destroy_submissionObject



103
104
105
106
107
108
109
110
111
112
# File 'app/controllers/admin/form_controller.rb', line 103

def destroy_submission
  @sub = FormSubmission.find_sys_id(_sid, params[:id])
  if @sub
    Activity.add(_sid, "Form '#{@sub.form.title}' record #{@sub.id} destroyed", current_user)
    @sub.destroy
    render :json=>{:delete=>"Deleted"}
  else
    render :json=>{:delete=>"Could not delete"}
  end
end

#editObject



153
154
155
# File 'app/controllers/admin/form_controller.rb', line 153

def edit
  @form = Form.find_sys_id(_sid, params[:id])
end

#exportObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'app/controllers/admin/form_controller.rb', line 114

def export
  @form = Form.find_sys_id(_sid, params[:id])
  filename = @form.title.urlise + ".csv"    

  csv_headers(filename)


  csv_string = CSV.generate do |csv|
    csv << @form.form_fields.map { |ff| ff.name }
    @form.form_submissions.includes(:form_submission_fields).each do |fs|
      csv << fs.form_submission_fields.map { |fsf| fsf.value }
    end
  end
  render :text => csv_string 
end

#fieldObject



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'app/controllers/admin/form_controller.rb', line 6

def field
  @form = Form.find_sys_id(_sid, params[:id])
  @field = FormField.find_sys_id(_sid, params[:field_id])

  if request.post?
    if @field.form_field_type.field_type == 'location'
       params[:form_field][:geo_code_from_fields] = params[:fields_to_geocode].join('|')
    end
    if @field.update_attributes(params[:form_field])
      redirect_to "/admin/form/#{@form.id}/fields", :notice=>"Update field" and return
    end
  end
end

#fieldsObject



26
27
28
# File 'app/controllers/admin/form_controller.rb', line 26

def fields
  @form = Form.sys(_sid).where(:id=>params[:id]).includes([{:form_field_groups=>:form_fields}, {:form_fields=>:form_field_type}]).first
end

#generate_htmlObject



20
21
22
23
24
# File 'app/controllers/admin/form_controller.rb', line 20

def generate_html
  @form = Form.find_sys_id(_sid, params[:id])

  render :partial=>"form/show", :locals=>{:form=>@form, :show_title=>true, :show_body=>true}
end

#indexObject



78
79
80
# File 'app/controllers/admin/form_controller.rb', line 78

def index
  @forms = Form.sys(_sid).page(params[:page]).per(50)
end

#listObject



62
63
64
65
66
# File 'app/controllers/admin/form_controller.rb', line 62

def list
  @form = Form.find_sys_id(_sid, params[:id])

  @subs = @form.form_submissions.sys(_sid).page(params[:page]).per(params[:per_page] || 50)
end

#newObject



134
135
136
# File 'app/controllers/admin/form_controller.rb', line 134

def new
  @form = Form.new
end

#showObject



130
131
132
# File 'app/controllers/admin/form_controller.rb', line 130

def show
  @form = Form.find_sys_id(_sid, params[:id])
end

#show_hide_submissionObject



176
177
178
179
180
181
182
183
# File 'app/controllers/admin/form_controller.rb', line 176

def show_hide_submission
  @sub = FormSubmission.find_sys_id(_sid, params[:id])
  @sub.visible = params[:mode]
  @sub.save
  Activity.add(_sid, "Form '#{@sub.form.title}' record #{@sub.id} visibility changed", current_user)

  redirect_to request.referer
end

#show_submissionObject



82
83
84
85
86
# File 'app/controllers/admin/form_controller.rb', line 82

def show_submission 
  @sub = FormSubmission.sys(_sid).where(:id=>params[:id]).includes({:form_submission_fields=>{:form_field=>:form_field_type}}).first

  render :layout=>false
end

#updateObject



157
158
159
160
161
162
163
164
165
# File 'app/controllers/admin/form_controller.rb', line 157

def update
  @form = Form.find_sys_id(_sid, params[:id])
  if @form.update_attributes(params[:form])
    Activity.add(_sid, "Form '#{@form.title}' updated", current_user, "Form")
    redirect_to "/admin/form/#{@form.id}", :notice  => "Successfully updated form"
  else
    render :action => 'edit'
  end
end

#update_fieldsObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/controllers/admin/form_controller.rb', line 30

def update_fields
  @form = Form.find_sys_id(_sid, params[:id])
  
  if params[:delete]
    field_id = params[:delete]
    field = @form.form_fields.where(["form_fields.id = ?", field_id])
    Activity.add(_sid, "Form '#{@form.title}' field '#{field.name}' deleted", current_user, "Form")
    @form.form_fields.delete(field)
    flash[:notice]="Field Deleted" 
  end

  if params[:add]
    form_field_type = FormFieldType.find_sys_id(_sid, params[:add])
    last_field = @form.form_fields.order("display_order").last
    if last_field
      next_display_order = last_field.display_order + 100
    else
      next_display_order = 1000
    end
    ff = FormField.new(:form_id=>@form.id, :display_order=>next_display_order, :form_field_type=>form_field_type, :is_mandatory=>false, :name=>form_field_type.name, :system_id=>_sid)
    
    if ff.save(:validate=>false)
      Activity.add(_sid, "Form '#{@form.title}' field '#{form_field_type.name}' added", current_user, "Form")
      flash[:notice]="Field Added"
    else
      flash[:notice]="Failed to add field"        
    end
  end 

  redirect_to "/admin/form/#{@form.id}/fields"
end

#update_submissionObject



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'app/controllers/admin/form_controller.rb', line 88

def update_submission
  @sub = FormSubmission.find_sys_id(_sid, params[:id])
  if params[:mark]
    Activity.add(_sid, "Form '#{@sub.form.title}' record #{@sub.id} marked", current_user)
    @sub.update_attributes(:marked=>params[:mark]) 
  end
  if params[:visible]
    Activity.add(_sid, "Form '#{@sub.form.title}' record #{@sub.id} visibility changed", current_user)
    @sub.update_attributes(:visible=>params[:visible]) 
  end

  render :json=>{:mark=>@sub.marked, :visible=>@sub.visible}
end