diff --git a/app/controllers/plans_controller.rb b/app/controllers/plans_controller.rb index 0db2114..25216b7 100644 --- a/app/controllers/plans_controller.rb +++ b/app/controllers/plans_controller.rb @@ -367,8 +367,7 @@ @exported_plan = ExportedPlan.new.tap do |ep| ep.plan = @plan ep.user = current_user - #ep.format = request.format.try(:symbol) - ep.format = request.format.to_sym + ep.format = params[:format].to_sym plan_settings = @plan.settings(:export) Settings::Template::DEFAULT_SETTINGS.each do |key, value| @@ -376,27 +375,30 @@ end end - @exported_plan.save! # FIXME: handle invalid request types without erroring? - file_name = @exported_plan.project_name + begin + @exported_plan.save! + file_name = @exported_plan.project_name - respond_to do |format| - format.html - format.xml - format.json - format.csv { send_data @exported_plan.as_csv, filename: "#{file_name}.csv" } - format.text { send_data @exported_plan.as_txt, filename: "#{file_name}.txt" } - format.docx { headers["Content-Disposition"] = "attachment; filename=\"#{file_name}.docx\""} - format.pdf do - @formatting = @plan.settings(:export).formatting - render pdf: file_name, - margin: @formatting[:margin], - footer: { - center: t('helpers.plan.export.pdf.generated_by'), - font_size: 8, - spacing: (@formatting[:margin][:bottom] / 2) - 4, - right: '[page] of [topage]' - } + respond_to do |format| + format.html + format.csv { send_data @exported_plan.as_csv, filename: "#{file_name}.csv" } + format.text { send_data @exported_plan.as_txt, filename: "#{file_name}.txt" } + format.docx { headers["Content-Disposition"] = "attachment; filename=\"#{file_name}.docx\""} + format.pdf do + @formatting = @plan.settings(:export).formatting + render pdf: file_name, + margin: @formatting[:margin], + footer: { + center: _('This document was generated by %{application_name}') % {application_name: Rails.configuration.branding[:application][:name]}, + font_size: 8, + spacing: (@formatting[:margin][:bottom] / 2) - 4, + right: '[page] of [topage]' + } + end end + rescue ActiveRecord::RecordInvalid => e + redirect_to show_export_plan_path(@plan), notice: _('%{format} is not a valid exporting format. Available formats to export are %{available_formats}.') % + {format: params[:format], available_formats: ExportedPlan::VALID_FORMATS.to_s} end elsif !user_signed_in? then respond_to do |format| @@ -404,7 +406,7 @@ end elsif !@plan.editable_by(current_user.id) then respond_to do |format| - format.html { redirect_to projects_url, notice: _('This account does not have access to that plan.') } + format.html { redirect_to plans_path, notice: _('This account does not have access to that plan.') } end end end diff --git a/app/controllers/roles_controller.rb b/app/controllers/roles_controller.rb index d9d539a..35c95d6 100644 --- a/app/controllers/roles_controller.rb +++ b/app/controllers/roles_controller.rb @@ -5,7 +5,8 @@ def create @role = Role.new(role_params) authorize @role - @role.access_level = params[:role][:access_level].to_i + access_level = params[:role][:access_level].to_i + set_access_level(access_level) if params[:user].present? message = _('User added to project') user = User.find_by(email: params[:user]) @@ -31,7 +32,8 @@ def update @role = Role.find(params[:id]) authorize @role - @role.access_level = params[:role][:access_level].to_i + access_level = params[:role][:access_level].to_i + set_access_level(access_level) if @role.update_attributes(role_params) flash[:notice] = _('Sharing details successfully updated.') UserMailer.permissions_change_notification(@role).deliver @@ -56,6 +58,19 @@ private def role_params - params.require(:role).permit(:plan_id, :access_level) + params.require(:role).permit(:plan_id) end + + def set_access_level(access_level) + if access_level >= 1 + @role.commenter = true + end + if access_level >= 2 + @role.editor = true + end + if access_level >= 3 + @role.administrator = true + end + end + end \ No newline at end of file diff --git a/app/models/exported_plan.rb b/app/models/exported_plan.rb index 44b0e3a..0ce6426 100644 --- a/app/models/exported_plan.rb +++ b/app/models/exported_plan.rb @@ -1,5 +1,6 @@ class ExportedPlan < ActiveRecord::Base include GlobalHelpers + include SettingsTemplateHelper # TODO: REMOVE AND HANDLE ATTRIBUTE SECURITY IN THE CONTROLLER! attr_accessible :plan_id, :user_id, :format, :user, :plan, :as => [:default, :admin] @@ -8,7 +9,7 @@ belongs_to :plan belongs_to :user - VALID_FORMATS = ['csv', 'html', 'json', 'pdf', 'text', 'xml', 'docx'] + VALID_FORMATS = ['csv', 'html', 'pdf', 'text', 'docx'] validates :format, inclusion: { in: VALID_FORMATS, @@ -109,24 +110,33 @@ def as_csv CSV.generate do |csv| - csv << ["Section","Question","Answer","Selected option(s)","Answered by","Answered at"] + csv << [_('Section'),_('Question'),_('Answer'),_('Selected option(s)'),_('Answered by'),_('Answered at')] self.sections.each do |section| self.questions_for_section(section).each do |question| answer = self.plan.answer(question.id) - options_string = answer.options.collect {|o| o.text}.join('; ') - - csv << [section.title, sanitize_text(question.text), sanitize_text(answer.text), options_string, answer.try(:user).try(:name), answer.created_at] + q_format = question.question_format + if q_format.title == _('Check box') || q_format.title == _('Multi select box') || + q_format.title == _('Radio buttons') || q_format.title == _('Dropdown') + options_string = answer.options.collect {|o| o.text}.join('; ') + else + options_string = '' + end + csv << [section.title, sanitize_text(question.text), sanitize_text(answer.text), options_string, user.name, answer.updated_at] end end end end def as_txt - output = "#{self.plan.project.title}\n\n#{self.plan.version.phase.title}\n" - output += "\nDetails:\n\n" - attrs = self.plan.settings(:export)[:value]['fields'][:admin].collect{|f| f.to_s} - attrs.each do |attr| - output += attr + ": " + self.send(attr) + "\n" + output = "#{self.plan.title}\n\n#{self.plan.template.title}\n" + output += "\n"+_('Details')+"\n\n" + puts 'admin_details: '+self.admin_details.inspect + + self.admin_details.each do |at| + value = self.send(at) + if value.present? + output += admin_field_t(at.to_s) + ": " + value + "\n" + end end self.sections.each do |section| @@ -137,14 +147,18 @@ output += "\n* #{qtext}" answer = self.plan.answer(question.id, false) - if answer.nil? || answer.text.nil? then + if answer.nil? output += _('Question not answered.')+ "\n" else - output += answer.options.collect {|o| o.text}.join("\n") - if question.option_comment_display == true then - output += "\n#{sanitize_text(answer.text)}\n" + q_format = question.question_format + if q_format.title == _('Check box') || q_format.title == _('Multi select box') || + q_format.title == _('Radio buttons') || q_format.title == _('Dropdown') + output += answer.options.collect {|o| o.text}.join("\n") + if question.option_comment_display + output += "\n#{sanitize_text(answer.text)}\n" + end else - output += "\n" + output += "\n#{sanitize_text(answer.text)}\n" end end end diff --git a/app/models/role.rb b/app/models/role.rb index cf49c87..cee961b 100644 --- a/app/models/role.rb +++ b/app/models/role.rb @@ -1,8 +1,6 @@ class Role < ActiveRecord::Base include FlagShihTzu - before_validation :check_access_level - ## # Associations belongs_to :user @@ -14,6 +12,7 @@ has_flags 1 => :creator, 2 => :administrator, 3 => :editor, + 4 => :commenter, column: 'access' validates :user, :plan, :access, presence: true @@ -24,41 +23,17 @@ # 3 if the user is an administrator # 2 if the user is an editor # 1 if the user can only read + # used to facilliatte formtastic # # @return [Integer] def access_level - if self.administrator? then + if self.administrator? return 3 - elsif self.editor? then + elsif self.editor? return 2 - else + elsif self.commenter? return 1 end end - ## - # define a new access level for the current project group - # if >=3, the user is a project administrator - # if >=2, the user is an editor - # - # @param new_access_level [Integer] the access level to give the user - def access_level=(new_access_level) - new_access_level = new_access_level.to_i - if new_access_level >= 3 then - self.administrator = true - else - self.administrator = false - end - if new_access_level >= 2 then - self.editor = true - else - self.editor = false - end - self.creator = true unless self.administrator? || self.editor? - end - - # Ensures that the access attribute is set (will default to creator - see logic in access_level=) - def check_access_level - self.access_level = self.access_level - end end diff --git a/app/views/plans/export.docx.caracal b/app/views/plans/export.docx.caracal index 5adfd22..778eb3c 100644 --- a/app/views/plans/export.docx.caracal +++ b/app/views/plans/export.docx.caracal @@ -19,21 +19,20 @@ # Structure for docx format #--------------------------------------------- -docx.h1 @exported_plan.plan.project.title.upcase, font: 'Arial', color: '000000' -docx.h2 @exported_plan.plan.title.upcase, font: 'Arial', color: '000000' +docx.h1 @exported_plan.plan.title.upcase, font: 'Arial', color: '000000' +docx.h2 @plan.template.title.upcase, font: 'Arial', color: '000000' #---- PLAN ADMIN DETAILS ----- if @exported_plan.admin_details.present? docx.p - docx.h3 'Admin Details'.upcase , italic: false, font: 'Arial', color: '000000' + docx.h3 _('Admin Details').upcase , italic: false, font: 'Arial', color: '000000' @exported_plan.admin_details.each do |field| value = @exported_plan.send(field) - label = "helpers.plan.export.#{field}" if value.present? - docx.p do - text I18n.t(label), bold: true, color: '000000' - text ': ', bold: true, color: '000000' - text value + docx.p do |p| + p.text admin_field_t(field.to_s), bold: true, color: '000000' + p.text ': ', bold: true, color: '000000' + p.text value end end end @@ -50,7 +49,7 @@ answer = @exported_plan.plan.answer(question.id, false) if answer.nil? - docx.p 'Question not answered', italic: true + docx.p _('Question not answered'), italic: true else q_format = question.question_format if q_format.title == _('Check box') || q_format.title == _('Multi select box') || diff --git a/app/views/plans/export.pdf.erb b/app/views/plans/export.pdf.erb index 5fe2cbc..c41b244 100644 --- a/app/views/plans/export.pdf.erb +++ b/app/views/plans/export.pdf.erb @@ -3,11 +3,7 @@