diff --git a/app/controllers/plans_controller.rb b/app/controllers/plans_controller.rb index 8747488..116bffd 100644 --- a/app/controllers/plans_controller.rb +++ b/app/controllers/plans_controller.rb @@ -30,74 +30,70 @@ def create - if user_signed_in? then - @plan = Plan.new - @plan.save - authorize @plan + @plan = Plan.new + @plan.save + authorize @plan - if params[:template_id] - @templates = [ Template.find(params[:template_id] ) ] + if params[:template_id] + @templates = [ Template.find(params[:template_id] ) ] + else + + funder_id = params[:plan][:funder_id] + if !funder_id.blank? + # get all funder @templates + funder = Org.find(params[:plan][:funder_id]) + @templates = get_most_recent( funder.templates.where("published = ?", true).all ) + + orgtemplates = current_user.org.templates.all + replacements = [] + + # replace any that are customised by the org + orgtemplates.each do |orgt| + base_template = orgt.customization_of + @templates.delete(base_template) + replacements << orgt + end + @templates + replacements + else + # get all org @templates which are not customisations + @templates = current_user.org.templates.where(customization_of: nil) - funder_id = params[:plan][:funder_id] - if !funder_id.blank? - # get all funder @templates - funder = Org.find(params[:plan][:funder_id]) - @templates = get_most_recent( funder.templates.where("published = ?", true).all ) - - orgtemplates = current_user.org.templates.all - replacements = [] - - # replace any that are customised by the org - orgtemplates.each do |orgt| - base_template = orgt.customization_of - @templates.delete(base_template) - replacements << orgt - end - @templates + replacements - - else - # get all org @templates which are not customisations - @templates = current_user.org.templates.where(customization_of: nil) - - # if none of these get the basic dcc template - if @templates.blank? - @templates = Template.find_by_is_default(true) - end - end - end - - # if we have more than one template then back to the user - # using the 'create' template - # to choose otherwise just create the plan - # and go to the plan/show template - if @templates.length > 1 - return - end - - @plan.template = @templates[0] - - @plan.principal_investigator = current_user.name - - @plan.title = _('My plan')+' ('+@plan.template.title+')' # We should use interpolated string since the order of the words from this message could vary among languages - - @plan.assign_creator(current_user.id) - - @plan.set_possible_guidance_groups - - @selected_guidance_groups = @plan.guidance_groups.map{ |pgg| [pgg.name, pgg.id, :checked => false] } - @selected_guidance_groups.sort! - - respond_to do |format| - if @plan.save - format.html { redirect_to({:action => "show", :id => @plan.id, :editing => true }, {:notice => _('Plan was successfully created.')}) } - else - @error = "Something went wrong" - format.html { render action: "new" } + # if none of these get the basic dcc template + if @templates.blank? + @templates = Template.find_by_is_default(true) end end - else - render(:file => File.join(Rails.root, 'public/403.html'), :status => 403, :layout => false) + end + + # if we have more than one template then back to the user + # using the 'create' template + # to choose otherwise just create the plan + # and go to the plan/show template + if @templates.length > 1 + return + end + + @plan.template = @templates[0] + + @plan.principal_investigator = current_user.name + + @plan.title = _('My plan')+' ('+@plan.template.title+')' # We should use interpolated string since the order of the words from this message could vary among languages + + @plan.assign_creator(current_user.id) + + @plan.set_possible_guidance_groups + + @selected_guidance_groups = @plan.guidance_groups.map{ |pgg| [pgg.name, pgg.id, :checked => false] } + @selected_guidance_groups.sort! + + respond_to do |format| + if @plan.save + format.html { redirect_to({:action => "show", :id => @plan.id, :editing => true }, {:notice => _('Plan was successfully created.')}) } + else + flash[:notice] = generate_error_notice(@plan) + format.html { render action: "new" } + end end end @@ -105,29 +101,14 @@ # GET /plans/show def show - puts 'plans#show' @plan = Plan.eager_load(params[:id]) authorize @plan - - @editing = params[:editing] && @plan.administerable_by?(current_user.id) + @editing = (!params[:editing].nil? && @plan.administerable_by?(current_user.id)) @selected_guidance_groups = [] all_guidance_groups = @plan.plan_guidance_groups @selected_guidance_groups = all_guidance_groups.map{ |pgg| [ pgg.guidance_group.name, pgg.guidance_group.id, :checked => pgg.selected ] } @selected_guidance_groups.sort! - - if user_signed_in? && @plan.readable_by?(current_user.id) then - respond_to do |format| - format.html # show.html.erb - end - elsif user_signed_in? then - respond_to do |format| - format.html { redirect_to projects_url, notice: _('This account does not have access to that plan.') } - end - else - respond_to do |format| - format.html { redirect_to edit_user_registration_path } - end - end + respond_to :html end @@ -142,43 +123,29 @@ # # GET /plans/1/edit def edit - @plan = Plan.find(params[:id]) - - @phase = nil - if params[:phase] - @phase = Phase.find(params[:phase]) - end - authorize @plan + # If there was no phase specified use the template's 1st phase + @phase = (params[:phase].nil? ? @plan.template.phases.first : Phase.find(params[:phase])) @readonly = @plan.editable_by?(current_user.id) - if !user_signed_in? then - respond_to do |format| - format.html { redirect_to edit_user_registration_path } - end - elsif !@plan.readable_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.') } - end - end + respond_to :html end + # PUT /plans/1 # PUT /plans/1.json def update @plan = Plan.find(params[:id]) authorize @plan - if user_signed_in? && @plan.editable_by?(current_user.id) then - respond_to do |format| - if @plan.update_attributes(params[:plan]) - format.html { redirect_to @plan, :editing => false, notice: _('Plan was successfully updated.') } - format.json { head :no_content } - else - format.html { render action: "edit" } - end + + respond_to do |format| + if @plan.update_attributes(params[:plan]) + format.html { redirect_to @plan, :editing => false, notice: _('Plan was successfully updated.') } + format.json { head :no_content } + else + flash[:notice] = generate_error_notice(@plan) + format.html { render action: "edit" } end - else - render(:file => File.join(Rails.root, 'public/403.html'), :status => 403, :layout => false) end end @@ -187,19 +154,17 @@ def update_guidance_choices @plan = Plan.find(params[:id]) authorize @plan - if user_signed_in? && @plan.editable_by?(current_user.id) then - guidance_ids = params[:plan][:plan_guidance_group_ids] - @plan.plan_guidance_groups.each do |pgg| - pgg.selected = guidance_ids.include?(pgg.guidance_group_id.to_s) - pgg.save! - end - @plan.save! + guidance_ids = params[:plan][:plan_guidance_group_ids] + +# TODO: This always appears to be empty for a new plan. What SHOULD it contain, all guidance_groups? + @plan.plan_guidance_groups.each do |pgg| + pgg.selected = guidance_ids.include?(pgg.guidance_group_id.to_s) + pgg.save! + end + @plan.save! - respond_to do |format| - format.json { head :no_content } - end - else - render(:file => File.join(Rails.root, 'public/403.html'), :status => 403, :layout => false) + respond_to do |format| + format.json { head :no_content } end end @@ -207,29 +172,15 @@ @plan = Plan.find(params[:id]) authorize @plan @plan_data = @plan.to_hash - if !user_signed_in? then - respond_to do |format| - format.html { redirect_to edit_user_registration_path } - end - elsif !@plan.editable_by?(current_user.id) then - respond_to do |format| - format.html { redirect_to plans_url, notice: _('This account does not have access to that plan.') } - end - end end def destroy @plan = Plan.find(params[:id]) authorize @plan - if user_signed_in? && @plan.editable_by?(current_user.id) then - @plan.destroy - - respond_to do |format| - format.html { redirect_to plans_url } - end - else - render(:file => File.join(Rails.root, 'public/403.html'), :status => 403, :layout => false) + @plan.destroy + respond_to do |format| + format.html { redirect_to plans_url, notice: _('Plan was successfully deleted.') } end end @@ -238,24 +189,19 @@ def status @plan = Plan.find(params[:id]) authorize @plan - if user_signed_in? && @plan.readable_by(current_user.id) then - respond_to do |format| - format.json { render json: @plan.status } - end - else - render(:file => File.join(Rails.root, 'public/403.html'), :status => 403, :layout => false) + respond_to do |format| + format.json { render json: @plan.status } end end + +# TODO: Remove these endpoints now that we're no longer using them +=begin def section_answers @plan = Plan.find(params[:id]) authorize @plan - if user_signed_in? && @plan.readable_by(current_user.id) then - respond_to do |format| - format.json { render json: @plan.section_answers(params[:section_id]) } - end - else - render(:file => File.join(Rails.root, 'public/403.html'), :status => 403, :layout => false) + respond_to do |format| + format.json { render json: @plan.section_answers(params[:section_id]) } end end @@ -336,74 +282,73 @@ render(:file => File.join(Rails.root, 'public/403.html'), :status => 403, :layout => false) end end +=end def answer @plan = Plan.find(params[:id]) authorize @plan - if user_signed_in? && @plan.readable_by(current_user.id) then + if !params[:q_id].nil? respond_to do |format| format.json { render json: @plan.answer(params[:q_id], false).to_json(:include => :options) } end else - render(:file => File.join(Rails.root, 'public/403.html'), :status => 403, :layout => false) + respond_to do |format| + format.json { render json: {} } + end end end +# TODO: This one is unreachable ... it has no route defined +=begin def show_export @plan = Plan.find(params[:id]) authorize @plan render 'show_export' end - +=end + def export @plan = Plan.find(params[:id]) authorize @plan - if user_signed_in? && @plan.readable_by?(current_user.id) then - @exported_plan = ExportedPlan.new.tap do |ep| - ep.plan = @plan - ep.user = current_user - ep.format = params[:format].to_sym - plan_settings = @plan.settings(:export) + # If no format is specified, default to PDF + params[:format] = 'pdf' if params[:format].nil? - Settings::Template::DEFAULT_SETTINGS.each do |key, value| - ep.settings(:export).send("#{key}=", plan_settings.send(key)) + @exported_plan = ExportedPlan.new.tap do |ep| + ep.plan = @plan + ep.user = current_user + ep.format = params[:format].to_sym + plan_settings = @plan.settings(:export) + + Settings::Template::DEFAULT_SETTINGS.each do |key, value| + ep.settings(:export).send("#{key}=", plan_settings.send(key)) + end + end + + begin + @exported_plan.save! + file_name = @exported_plan.project_name + + 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 - - begin - @exported_plan.save! - file_name = @exported_plan.project_name - - 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| - format.html { redirect_to edit_user_registration_path } - end - elsif !@plan.editable_by(current_user.id) then - respond_to do |format| - format.html { redirect_to plans_path, notice: _('This account does not have access to that plan.') } - 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 end