diff --git a/app/controllers/templates_controller.rb b/app/controllers/templates_controller.rb index 3ef036b..b59bc27 100644 --- a/app/controllers/templates_controller.rb +++ b/app/controllers/templates_controller.rb @@ -34,8 +34,12 @@ # are any funder templates customized @templates_customizations = {} Template.where(org_id: current_user.org_id, customization_of: funders_templates.keys).order(version: :desc).each do |temp| - if @templates_customizations[temp.dmptemplate_id].nil? - @templates_customizations[temp.dmptemplate_id] = temp + if @templates_customizations[temp.customization_of].nil? + @templates_customizations[temp.customization_of] = {} + @templates_customizations[temp.customization_of][:temp] = temp + @templates_customizations[temp.customization_of][:published] = temp.published + else + @templates_customizations[temp.customization_of][:published] = @templates_customizations[temp.customization_of][:published] || temp.published end end end @@ -64,13 +68,58 @@ end end end - customizations = Template.where(org_id: current_user.org_id, customization_of: @template.dmptemplate_id).order(version: :desc) + customizations = Template.includes(phases: [sections: [questions: :suggested_answers ]]).where(org_id: current_user.org_id, customization_of: @template.dmptemplate_id).order(version: :desc) if customizations.present? # existing customization to port over max_version = customizations.first new_customization.dmptemplate_id = max_version.dmptemplate_id new_customization.version = max_version.version + 1 - # port customization data...? + # here we rip the customizations out of the old template + # First, we find any customized phases or sections + max_version.phases.each do |phase| + # check if the phase was added as a customization + if phase.modifiable + # deep copy the phase and add it to the template + phase_copy = Phase.deep_copy(phase) + phase_copy.number = new_customization.phases.length + 1 + phase_copy.template_id = new_customization.id + phase_copy.save! + else + # iterate over the sections to see if any of them are customizations + phase.sections.each do |section| + if section.modifiable + # this is a custom section + section_copy = Section.deep_copy(section) + customization_phase = new_customization.includes(:sections).phase.where(number: phase.number) + section_copy.phase_id = customization_phase.id + # custom sections get added to the end + section_copy.number = customization_phase.sections.length + 1 + # section from phase with corresponding number in the main_template + section_copy.save! + else + # not a customized section, iterate over questions + customization_phase = new_customization.includes(sections: [questions: :suggested_answers]) + customization_section = customization_phase.section.where(number: section.number) + section.questions.each do |question| + # find corresponding question in new template + customization_question = customization_section.question.where(number: question.number) + # apply suggested_answers + question.suggested_answers.each do |suggested_answer| + suggested_answer_copy = SuggestedAnswer.deep_copy(suggested_answer) + suggested_answer_copy.org_id = current_user.org_id + suggested_answer_copy.question_id = customization_question.id + suggested_answer_copy.save! + end + # guidance attached to a question is also a form of customization + # It will soon become an annotation of the question, and be combined with + # suggested answers + customization_question.guidance = customization_question.guidance + question.guidance + customization_question.save! + end + end + end + end + end else # first time customization new_customization.version = 0 @@ -82,6 +131,15 @@ new_customization.save! @template = new_customization end + # needed for some post-migration edge cases + # some customized templates which were edited + if @template.customization_of.present? && @template.published + new_version = Template.deep_copy(@template) + new_version.version = @template.version + 1 + new_version.published = false + new_version.save! + @template = new_version + end authorize @template end @@ -92,7 +150,7 @@ authorize @template if @template.published? # published templates cannot be edited - redirect_to admin_template_template_path(@template), notice: I18n.t('org_admin.templates.read_only') + redirect_to admin_template_template_path(@template), notice: I18n.t('org_admin.templates.read_only') and return end @template.description = params["template-desc"] if @template.update_attributes(params[:template]) @@ -102,10 +160,6 @@ new_version.version = @template.version + 1 new_version.published = false new_version.save! - # if the organisation is a funder - if @template.org.funder? - # do something about all the customizations? - end end redirect_to admin_index_template_path(), notice: I18n.t('org_admin.templates.updated_message') else diff --git a/app/views/templates/admin_index.html.erb b/app/views/templates/admin_index.html.erb index 0f7f193..834fa0d 100644 --- a/app/views/templates/admin_index.html.erb +++ b/app/views/templates/admin_index.html.erb @@ -113,13 +113,14 @@ <% customized = 0 %> <% template = org_template # template to be edited%> <% if @templates_customizations[org_template.dmptemplate_id].present? %> - <% if @templates_customizations[org_template.dmptemplate_id].updated_at < org_template.updated_at %> + <% if @templates_customizations[org_template.dmptemplate_id][:temp].updated_at < org_template.updated_at %> <%= t("org_admin.templates.original_changed") %> <% customized = 3 %> <% else %> - <% template = @templates_customizations[org_template.dmptemplate_id] %> - <% if !@templates_customizations[org_template.dmptemplate_id].published %> - <%= t("org_admin.templates.unpublished_changes") %> + <% template = @templates_customizations[org_template.dmptemplate_id][:temp] %> + <% if !@templates_customizations[org_template.dmptemplate_id][:temp].published %> + <%= @templates_customizations[org_template.dmptemplate_id][:published] ? t("helpers.yes_label") : t("helpers.no_label")%> + <%= " " + t("org_admin.templates.unpublished_changes") %> <% customized = 2 %> <% else %> <%= t("helpers.yes_label") %> diff --git a/config/locales/en-UK.yml b/config/locales/en-UK.yml index 99325c2..d40237b 100644 --- a/config/locales/en-UK.yml +++ b/config/locales/en-UK.yml @@ -285,7 +285,7 @@ destroyed_message: "Information was successfully deleted." read_only: "Published templates cannot be edited." section_delete_message: "You are about to delete '%{section_title}'. This will affect questions linked to this section. Are you sure?" - unpublished_changes: "Yes [Unpublished Changes]" + unpublished_changes: "[Unpublished Changes]" original_changed: "Yes [Original Template Has Changed]" versions: