| 48 | | def create_step1 |
|---|
| 49 | | @help = 'general' |
|---|
| 50 | | @title = _('New form type wizard Step 1 of 4') |
|---|
| 51 | | # Find objects |
|---|
| 52 | | @form_categories = FormCategory.find :all, :readonly => true, :order => 'name' |
|---|
| 53 | | |
|---|
| 54 | | # Options for selection box |
|---|
| 55 | | @options = @form_categories.collect { |c| [c.name, c.id] } |
|---|
| 56 | | |
|---|
| 57 | | if @options.empty? |
|---|
| 58 | | flash[:error] = _('No form categories') |
|---|
| 59 | | redirect_to :action => 'list' |
|---|
| 60 | | end |
|---|
| 61 | | |
|---|
| 62 | | if request.post? |
|---|
| 63 | | @form_type = FormType.new params[:form_type] |
|---|
| 64 | | |
|---|
| 65 | | if @form_type.save |
|---|
| 66 | | user_entered_page_group(:form_type_wizard, @form_type.id) |
|---|
| 67 | | redirect_to :action => 'create_step2', :id => @form_type.id |
|---|
| 68 | | else |
|---|
| 69 | | flash[:error] = _('Unable to create new form type') |
|---|
| 70 | | end |
|---|
| 71 | | else |
|---|
| 72 | | @form_type = FormType.new |
|---|
| 73 | | end |
|---|
| 74 | | end |
|---|
| 75 | | |
|---|
| 76 | | def create_step2 |
|---|
| 77 | | @help = 'general' |
|---|
| 78 | | @form_type = FormType.find(params[:id], :conditions => ['ready = ?', false]) |
|---|
| 79 | | @form_type.form_fields.clear |
|---|
| 80 | | @form_type.form_template.destroy if @form_type.form_template |
|---|
| 81 | | @title = _('New form type wizard Step 2 of 4') |
|---|
| 82 | | |
|---|
| 83 | | if request.post? |
|---|
| 84 | | # Create new ODT file |
|---|
| 85 | | @form_template = FormTemplate.new(params[:form_template]) |
|---|
| 86 | | if @form_template.valid? |
|---|
| 87 | | @form_type.form_template = @form_template |
|---|
| 88 | | @form_type.create_fields_from_user_field_declarations(@form_template.get_user_field_declarations) |
|---|
| 89 | | @form_type.create_fields_from_image_field_declarations(@form_template.get_image_field_declarations) |
|---|
| 90 | | |
|---|
| 91 | | if @form_template.save |
|---|
| 92 | | redirect_to :action => 'create_step3', :id => @form_type.id |
|---|
| 93 | | else |
|---|
| 94 | | flash[:error] = _('Unable to save template') |
|---|
| 95 | | end |
|---|
| 96 | | end |
|---|
| 97 | | else |
|---|
| 98 | | @form_template = FormTemplate.new |
|---|
| 99 | | end |
|---|
| 100 | | end |
|---|
| 101 | | |
|---|
| 102 | | def create_step3 |
|---|
| 103 | | @help = 'general' |
|---|
| 104 | | @form_type = FormType.find(params[:id], :conditions => ['ready = ?', false]) |
|---|
| 105 | | @title = _('New form type wizard Step 3 of 4 (%s)', @form_type.name) |
|---|
| 106 | | end |
|---|
| 107 | | |
|---|
| 108 | | def create_step4 |
|---|
| 109 | | @help = 'general' |
|---|
| 110 | | setup_create_step4 |
|---|
| 111 | | end |
|---|
| 112 | | |
|---|
| 113 | | def create_step5 |
|---|
| 114 | | @help = 'general' |
|---|
| 115 | | @form_type = FormType.find(params[:id], :conditions => ['ready = ?', false]) |
|---|
| 116 | | |
|---|
| 117 | | @form_type.ready = true |
|---|
| 118 | | @form_type.editable = params[:form_type][:editable] |
|---|
| 119 | | if @form_type.save |
|---|
| 120 | | info 'New form type created' |
|---|
| 121 | | flash[:notice] = _('New form type created') |
|---|
| 122 | | redirect_to :action => 'list' |
|---|
| 123 | | user_left_page_group(:form_type_wizard) |
|---|
| 124 | | else |
|---|
| 125 | | redirect_to :back |
|---|
| 126 | | end |
|---|
| 127 | | end |
|---|
| 128 | | |
|---|
| 129 | | def preview |
|---|
| 130 | | @form_type = FormType.find params[:id] |
|---|
| 131 | | @fields = @form_type.form_fields |
|---|
| 132 | | |
|---|
| 133 | | render :layout => false |
|---|
| 134 | | end |
|---|
| 135 | | |
|---|
| 136 | | def update_form_fields |
|---|
| 137 | | # Find objects |
|---|
| 138 | | @form_type = FormType.find(params[:id], :conditions => ['ready = ?', false]) |
|---|
| 139 | | |
|---|
| 140 | | if request.post? |
|---|
| 141 | | FormField.update(params[:field].keys, params[:field].values) |
|---|
| 142 | | |
|---|
| 143 | | # Move forward in the wizard after update, if Finish was pressed. |
|---|
| 144 | | return create_step5 if params[:commit] == _('Finish') |
|---|
| 145 | | end |
|---|
| 146 | | |
|---|
| 147 | | redirect_to :action => 'create_step4', :id => @form_type.id |
|---|
| 148 | | end |
|---|
| 149 | | |
|---|
| 150 | | def update_form_field_groups |
|---|
| 151 | | # Find objects |
|---|
| 152 | | @form_type = FormType.find(params[:id], :conditions => ['ready = ?', false]) |
|---|
| 153 | | |
|---|
| 154 | | if request.post? |
|---|
| 155 | | FormFieldGroup.update(params[:group].keys, params[:group].values) |
|---|
| 156 | | |
|---|
| 157 | | if params[:remove_group] |
|---|
| 158 | | for id in params[:remove_group].keys |
|---|
| 159 | | FormFieldGroup.destroy(id.to_i) |
|---|
| 160 | | end |
|---|
| 161 | | end |
|---|
| 162 | | end |
|---|
| 163 | | redirect_to :action => 'create_step4', :id => @form_type.id |
|---|
| 164 | | end |
|---|
| 165 | | |
|---|
| 166 | | def add_form_field_group |
|---|
| 167 | | setup_create_step4 |
|---|
| 168 | | if request.post? |
|---|
| 169 | | @new_field_group = FormFieldGroup.new params['new_field_group'] |
|---|
| 170 | | @new_field_group.form_type = @form_type |
|---|
| 171 | | @new_field_group.place = @new_field_group.form_type.form_field_groups.size + 1 |
|---|
| 172 | | if @new_field_group.valid? |
|---|
| 173 | | @form_type.form_field_groups << @new_field_group |
|---|
| 174 | | flash[:notice] = _('New field group added') |
|---|
| 175 | | redirect_to :action => 'create_step4', :id => @form_type.id |
|---|
| 176 | | else |
|---|
| 177 | | flash[:error] = _('Unable to create new field group') |
|---|
| 178 | | render :action => 'create_step4', :id => @form_type.id |
|---|
| 179 | | end |
|---|
| 180 | | else |
|---|
| 181 | | flash[:error] = _('Unable to create new field group') |
|---|
| 182 | | redirect_to :action => 'list' |
|---|
| 183 | | end |
|---|
| 184 | | end |
|---|
| 185 | | |
|---|
| 186 | | def add_enumerated_value |
|---|
| 187 | | setup_create_step4 |
|---|
| 188 | | if request.post? |
|---|
| 189 | | @enum_value = EnumeratedValue.new params['enum_value'] |
|---|
| 190 | | if @enum_value.save |
|---|
| 191 | | flash[:notice] = _('New enumerated value added') |
|---|
| 192 | | redirect_to :action => 'create_step4', :id => @form_type.id |
|---|
| 193 | | else |
|---|
| 194 | | flash[:error] = _('Unable to create new enumerated value') |
|---|
| 195 | | render :action => 'create_step4', :id => @form_type.id |
|---|
| 196 | | end |
|---|
| 197 | | else |
|---|
| 198 | | flash[:error] = _('Unable to create new enumerated value') |
|---|
| 199 | | redirect_to :action => 'list' |
|---|
| 200 | | end |
|---|
| 201 | | end |
|---|
| 202 | | |
|---|
| 203 | | def remove_enumerated_value |
|---|
| 204 | | invalid_request and return unless request.post? |
|---|
| 205 | | form_type = FormType.find(params[:id], :conditions => ['ready = ?', false]) |
|---|
| 206 | | |
|---|
| 207 | | enum = EnumeratedValue.find(params[:remove_enum]) |
|---|
| 208 | | if form_type.form_fields.include?(enum.form_field) |
|---|
| 209 | | enum.destroy |
|---|
| 210 | | flash[:notice] = _('Enumerated value removed') |
|---|
| 211 | | else |
|---|
| 212 | | invalid_request and return |
|---|
| 213 | | end |
|---|
| 214 | | redirect_to :action => :create_step4, :id => form_type.id |
|---|
| 215 | | end |
|---|
| 216 | | |
|---|
| 217 | | # Function: edit |
|---|
| 218 | | # ============== |
|---|
| | 50 | # Function: view |
|---|
| 220 | | # Edit existing form type |
|---|
| 221 | | def edit |
|---|
| 222 | | @help = 'general' |
|---|
| 223 | | @title = _('Edit form type') |
|---|
| 224 | | @form_type = FormType.find(params[:id], :conditions => ['ready = ?', true]) |
|---|
| 225 | | @fields = @form_type.form_fields.find(:all, :conditions => ['type_code != ?', FormField::AUTOMATIC]) |
|---|
| 226 | | |
|---|
| 227 | | if request.post? |
|---|
| 228 | | @form_type.attributes = params[:form_type] |
|---|
| 229 | | @form_type.editable = params[:form_type][:editable] |
|---|
| 230 | | if FormField.update(params[:field].keys, params[:field].values) and @form_type.save |
|---|
| 231 | | info "Form type #{@form_type.name} modified" |
|---|
| 232 | | flash[:notice] = _('Form type %s modified', @form_type.name) |
|---|
| 233 | | redirect_to :action => 'list' |
|---|
| 234 | | else |
|---|
| 235 | | flash[:error] = _('Unable to save form type') |
|---|
| 236 | | end |
|---|
| 237 | | end |
|---|
| | 52 | # Shows information about a form type |
|---|
| | 53 | def view |
|---|
| | 54 | @form_type = FormType.find(params[:id], :conditions => ['ready = ?', true]) if params[:id] |
|---|
| | 55 | @form_type = FormType.find(session[:new_form_type_id], :conditions => ['ready = ?', false]) unless @form_type |
|---|
| | 56 | |
|---|
| | 57 | @title = _('Form type "%s"', @form_type.name) |
|---|
| 251 | | |
|---|
| 252 | | # Function: download_template |
|---|
| 253 | | # =========================== |
|---|
| 254 | | # |
|---|
| 255 | | # Offer OpenDocument template for download |
|---|
| 256 | | def download_template |
|---|
| 257 | | # Find object |
|---|
| 258 | | template = FormTemplate.find params[:id] |
|---|
| 259 | | |
|---|
| 260 | | send_data(template.data, :filename => 'form_template.odt', :disposition => 'inline') |
|---|
| 261 | | end |
|---|
| 262 | | |
|---|
| 263 | | def change_template |
|---|
| 264 | | @form_type = FormType.find(params[:id], :conditions => ['ready = ?', true]) |
|---|
| 265 | | @title = _('Change the form template', @form_type.name) |
|---|
| 266 | | |
|---|
| 267 | | if request.post? |
|---|
| 268 | | @new_form_template = FormTemplate.new(params[:new_form_template]) |
|---|
| 269 | | if @new_form_template.save |
|---|
| 270 | | @form_type.form_template.destroy if @form_type.form_template |
|---|
| 271 | | @form_type.form_template = @new_form_template |
|---|
| 272 | | @new_form_template.save |
|---|
| 273 | | @form_type.create_html_template_and_images(true) |
|---|
| 274 | | flash[:notice] = _('Form template changed.') |
|---|
| 275 | | redirect_to(:action => 'list') |
|---|
| 276 | | end |
|---|
| 277 | | end |
|---|
| 278 | | end |
|---|
| 279 | | |
|---|
| 280 | | protected |
|---|
| 281 | | |
|---|
| 282 | | def setup_create_step4 |
|---|
| 283 | | @form_type = FormType.find(params[:id], :conditions => ['ready = ?', false]) |
|---|
| 284 | | @title = _('New form type wizard Step 4 of 4 (%s)', @form_type.name) |
|---|
| 285 | | @fields = @form_type.form_fields.find(:all, :conditions => ['type_code != ? AND form_field_group_id IS NOT NULL', FormField::AUTOMATIC]) + |
|---|
| 286 | | @form_type.form_fields.find(:all, :conditions => ['type_code != ? AND form_field_group_id IS NULL', FormField::AUTOMATIC]) |
|---|
| 287 | | @groups = @form_type.form_field_groups |
|---|
| 288 | | @available_field_groups = [ [_('Not in a group'), nil] ] + @groups.collect {|g| [g.name, g.id]} |
|---|
| 289 | | # Make sure that all current places are found in the options |
|---|
| 290 | | @group_place_options = (@groups.collect { |g| g.place } + (1..@groups.size).to_a).sort.uniq.collect { |i| ["#{i}.", i] } |
|---|
| 291 | | @new_field_group = FormFieldGroup.new |
|---|
| 292 | | @enum_value = EnumeratedValue.new |
|---|
| 293 | | end |
|---|