diff --git a/app/controllers/plans_controller.rb b/app/controllers/plans_controller.rb index 4ecf93e..fa94f43 100644 --- a/app/controllers/plans_controller.rb +++ b/app/controllers/plans_controller.rb @@ -28,6 +28,61 @@ respond_to :html end + + + def possible_templates + authorize Plan.new + + templates = [] + org = Org.find_by(name: params[:plan_org_name]) + funder = Org.find_by(name: params[:plan_funder_name]) + + if org.nil? + if funder.nil? + msg = _("Using the generic Data Management Plan") + + else + templates << Template.where(published: true, org: @funder) + msg = _("No funder templates available using the generic DMP") if @templates.empty + msg = _("Using the funder's DMP") if @templates.count == 1 + msg = _("Please select form one of the funder's DMPs") if @templates.count > 1 + end + + else + if funder.nil? + templates << Template.where(published: true, org: @org) + msg = _("No organisation templates available using the generic DMP") if @templates.empty + msg = _("Using the organisation's DMP") if @templates.count == 1 + msg = _("Please select form one of the organisation's DMPs") if @templates.count > 1 + + else + templates << Template.where(published: true, org: @funder) + + # Swap out any organisational cusotmizations of a funder template + templates.each do |tmplt| + customization = Template.where(published: true, org: @org, customization_of: tmplt.dmptemplate_id) + unless customization.nil? + templates.delete(tmplt) + templates << customization + end + end + + msg = _("No funder/organisation templates available using the generic DMP") if @templates.empty + msg = _("Using the funder/organisation's DMP") if @templates.count == 1 + msg = _("Please select form one of the funder/organisation's DMPs") if @templates.count > 1 + end + end + + # If no templates were available use the generic templates + if templates.empty? + templates << Template.find_by(is_default: true) + end + + @msg = msg + @templates = templates.sort{|x,y| x.title <=> y.title } if templates.count > 1 + + respond_to :json + end # we get here either from selecting a funder or if if the first selection diff --git a/app/policies/plan_policy.rb b/app/policies/plan_policy.rb index 318608d..d6c8703 100644 --- a/app/policies/plan_policy.rb +++ b/app/policies/plan_policy.rb @@ -43,6 +43,10 @@ def status? @plan.readable_by?(@user.id) end + + def possible_templates? + @plan.id.nil? + end # TODO: These routes are no lonmger used =begin diff --git a/app/views/plans/possible_templates.json.erb b/app/views/plans/possible_templates.json.erb new file mode 100644 index 0000000..f0467c6 --- /dev/null +++ b/app/views/plans/possible_templates.json.erb @@ -0,0 +1,4 @@ +{ + 'msg': '<%= @msg %>', + 'templates': <%= @templates.to_json %> +} \ No newline at end of file diff --git a/lib/assets/javascripts/new_plan.js b/lib/assets/javascripts/new_plan.js index 7a93a01..16a0f59 100644 --- a/lib/assets/javascripts/new_plan.js +++ b/lib/assets/javascripts/new_plan.js @@ -8,7 +8,10 @@ // Function to hide/show the clear button when text changed in the dropdown $(".combobox-container input.js-combobox").keyup(function(){ displayClearButton(this); - toggleSubmit(); + }); + + $(".combobox-container input.js-combobox").change(function(){ + retrieveTemplates(); }); // Initialize the clear buttons on load @@ -24,13 +27,13 @@ // If the user clicks the no Organisation checkbox disable the dropdown $("#plan_no_org").click(function(){ $("#plan_org_name").prop("disabled", this.checked).val(""); - toggleSubmit(); + retrieveTemplates(); }); // If the user clicks the no Funder checkbox disable the dropdown $("#plan_no_funder").click(function(){ $("#plan_funder_name").prop("disabled", this.checked).val(""); - toggleSubmit(); + retrieveTemplates(); }); }); @@ -52,7 +55,29 @@ var show = ($("#plan_no_org").prop("checked") || $("#plan_org_name").val().trim().length > 0) && ($("#plan_no_funder").prop("checked") || - $("#plan_funder_name").val().trim().length > 0); + $("#plan_funder_name").val().trim().length > 0) && + $('input[name="plan[template_id]"]:checked'); $(".form-submit").prop("disabled", !show); +} + +// AJAX call to retrieve the list of available templates +function retrieveTemplates(){ + var retrieve = ($("#plan_no_org").prop("checked") || + $("#plan_org_name").val().trim().length > 0) && + ($("#plan_no_funder").prop("checked") || + $("#plan_funder_name").val().trim().length > 0); + + if(retrieve){ + var args = {org_name: $("#plan_org_name").val(), + funder_name: $("#plan_funder_name").val()}; + +console.log(args); + + $.get("/plans/possible_templates", args, function(data){ + console.log('DATA: ' + data); + }); + } + + toggleSubmit(); } \ No newline at end of file