diff --git a/app/controllers/templates_controller.rb b/app/controllers/templates_controller.rb index c1ea7e8..817229d 100644 --- a/app/controllers/templates_controller.rb +++ b/app/controllers/templates_controller.rb @@ -14,32 +14,23 @@ funder_templates, org_templates, customizations = [], [], [] # Get all of the unique template family ids (dmptemplate_id) for each funder and the current org - funder_ids = Org.funders.includes(:templates).collect{|f| f.templates.valid.collect{|ft| ft.dmptemplate_id } }.flatten.uniq - org_ids = current_user.org.templates.valid.collect{|t| t.dmptemplate_id }.flatten.uniq + funder_ids = Org.funders.includes(:templates).collect{|f| f.templates.where(published: true).valid.collect{|ft| ft.dmptemplate_id } }.flatten.uniq + org_ids = current_user.org.templates.where(customization_of: nil).valid.collect{|t| t.dmptemplate_id }.flatten.uniq org_ids.each do |id| current = Template.current(id) live = Template.live(id) - - # If this isn't a customization of a funder template - if current.customization_of.nil? - org_templates << {current: current, live: live} - - # This is a customization of a funder template - else - funder_live = Template.live(current.customization_of) - customizations << current.customization_of - # Mark the customization as stale if the funder has a newer version - funder_templates << {current: current, live: live, stale: funder_live.updated_at > current.created_at} - end + org_templates << {current: current, live: live} end - - # Get the funder templates funder_ids.each do |id| - # If the org has a customization we don't want to load the funder version - unless customizations.include?(id) - funder_templates << {current: Template.current(id), live: Template.live(id)} - end + funder_live = Template.live(id) + current = Template.org_customizations(id, current_user.org_id) + # if we have a current template, check to see if there is a live version + live = current.nil? ? nil : Template.live(current.dmptemplate_id) + # need a current version, default to funder live if no custs exist + current = funder_live unless current.present? + + funder_templates << {current: current, live: live, funder_live: funder_live, stale: funder_live.updated_at > current.created_at} end @funder_templates = funder_templates.sort{|x,y| @@ -82,6 +73,78 @@ redirect_to admin_template_template_path(customisation) end + # GET /org/admin/templates/:id/admin_transfer_customization + # the funder template's id is passed through here + # ----------------------------------------------------- + def admin_transfer_customization + @template = Template.includes(:org).find(params[:id]) + authorize @template + new_customization = Template.deep_copy(@template) + new_customization.org_id = current_user.org_id + new_customization.published = false + new_customization.customization_of = @template.dmptemplate_id + new_customization.dirty = true + new_customization.phases.includes(sections: :questions).each do |phase| + phase.modifiable = false + phase.save + phase.sections.each do |section| + section.modifiable = false + section.save + section.questions.each do |question| + question.modifiable = false + question.save + end + end + end + customizations = Template.includes(:org, phases:[sections: [questions: :annotations]]).where(org_id: current_user.org_id, customization_of: @template.dmptemplate_id).order(version: :desc) + # existing version to port over + max_version = customizations.first + new_customization.dmptemplate_id = max_version.dmptemplate_id + new_customization.version = max_version.version + 1 + # here we rip the customizations out of the old template + # First, we find any customzed 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.phases.includes(:sections.where(number: phase.number).first) + 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.phases.includes(sections: [questions: :annotations]).where(number: phase.number).first + customization_section = customization_phase.sections.where(number: section.number).first + section.questions.each do |question| + # find corresponding question in new template + customization_question = customization_section.questions.where(number: question.number).first + # apply annotations + question.annotations.each do |annotation| + annotation_copy = Annotation.deep_copy(annotation) + annotation_copy.question_id = customization_question.id + annotation_copy.save! + end + end + end + end + end + end + new_customization.save + redirect_to admin_template_template_path(new_customization) + end + # PUT /org/admin/templates/:id/admin_publish # ----------------------------------------------------- def admin_publish diff --git a/app/models/template.rb b/app/models/template.rb index f0fdc00..eac8d84 100644 --- a/app/models/template.rb +++ b/app/models/template.rb @@ -44,6 +44,19 @@ end ## + # Retrieves the most current customization of the template for the + # specified org and dmptemplate_id + # returns nil if no customizations found + # + # @params [integer] dmptemplate_id of the original template + # @params [integer] org_id for the customizing organisation + # @return [nil, Template] the customized template or nil + def self.org_customizations(dmptemplate_id, org_id) + Template.where(customization_of: dmptemplate_id, org_id: org_id).order(version: :desc).valid.first + end + + + ## # deep copy the given template and all of it's associations # # @params [Template] template to be deep copied diff --git a/app/policies/template_policy.rb b/app/policies/template_policy.rb index eeda053..754e622 100644 --- a/app/policies/template_policy.rb +++ b/app/policies/template_policy.rb @@ -53,6 +53,10 @@ user.can_modify_templates? && (template.org_id == user.org_id) end + def admin_transfer_customization? + user.can_modify_templates? + end + class Scope < Scope def resolve diff --git a/app/views/templates/admin_index.html.erb b/app/views/templates/admin_index.html.erb index 94c57a4..a37cabc 100644 --- a/app/views/templates/admin_index.html.erb +++ b/app/views/templates/admin_index.html.erb @@ -111,25 +111,20 @@