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 37e6b6a..2acb25a 100644 --- a/app/views/templates/admin_index.html.erb +++ b/app/views/templates/admin_index.html.erb @@ -111,25 +111,19 @@ <% if hash[:current].customization_of.nil? %> - <%= _('Published') %> - <% else %> <% if hash[:stale] %> <%= _('Original funder template has changed!')%> - <% elsif hash[:live].nil? %> <%= b_label = _('Un-published') %> - - <% elsif hash[:current].dirty? %> + <% elsif !hash[:current].published? %> <%= _('You have un-published changes') %> - <% else %> <%= _('Published') %> <% end %> - <% end %> @@ -138,13 +132,19 @@ <% if hash[:current].customization_of.nil? %> - <% b_label = _('Customise') %> + <% b_label = _('Customise') %> <%= link_to b_label, admin_customize_template_path(hash[:current]), method: :get, class: "dmp_table_link" %> <% else %> - <% b_label = _('Edit customisation') %> - <%= link_to b_label, admin_template_template_path(hash[:current]), class: "dmp_table_link" %> + <% if hash[:stale] %> + <% b_label = _('Transfer customisation') %> + <%= link_to b_label, admin_transfer_customization_template_path(hash[:funder_live]), class: "dmp_table_link" %> + <% else %> + <% b_label = _('Edit customisation') %> + <%= link_to b_label, admin_template_template_path(hash[:current]), class: "dmp_table_link" %> + <% end %> + <% end %> - + <% if !hash[:current].customization_of.nil? %> <% if hash[:live].nil? || hash[:current].dirty? %> <%= link_to _('Publish'), admin_publish_template_path(hash[:current]), method: :put, class: "dmp_table_link" %> diff --git a/config/locale/app.pot b/config/locale/app.pot index 1520d3f..28cb6fc 100644 --- a/config/locale/app.pot +++ b/config/locale/app.pot @@ -8,8 +8,8 @@ msgstr "" "Project-Id-Version: app 1.0.0\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2017-06-08 13:02+0100\n" -"PO-Revision-Date: 2017-06-08 13:02+0100\n" +"POT-Creation-Date: 2017-06-08 14:43+0000\n" +"PO-Revision-Date: 2017-06-08 14:43+0000\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" "Language: \n" @@ -1320,6 +1320,9 @@ msgid "Top banner text" msgstr "" +msgid "Transfer customisation" +msgstr "" + msgid "Un-published" msgstr "" diff --git a/config/locale/de/app.po b/config/locale/de/app.po index 8146f34..0474a51 100644 --- a/config/locale/de/app.po +++ b/config/locale/de/app.po @@ -1391,6 +1391,9 @@ msgid "Top banner text" msgstr "" +msgid "Transfer customisation" +msgstr "" + #, fuzzy msgid "Un-published" msgstr "Veröffentlicht" diff --git a/config/locale/en_GB/app.po b/config/locale/en_GB/app.po index d0ebad7..029f950 100644 --- a/config/locale/en_GB/app.po +++ b/config/locale/en_GB/app.po @@ -1369,6 +1369,9 @@ msgid "Top banner text" msgstr "Top banner text" +msgid "Transfer customisation" +msgstr "" + msgid "Un-published" msgstr "Un-published" diff --git a/config/locale/en_US/app.po b/config/locale/en_US/app.po index 4cb0fb5..e7dcee2 100644 --- a/config/locale/en_US/app.po +++ b/config/locale/en_US/app.po @@ -1369,6 +1369,9 @@ msgid "Top banner text" msgstr "Top banner text" +msgid "Transfer customisation" +msgstr "" + msgid "Un-published" msgstr "Un-published" diff --git a/config/locale/es/app.po b/config/locale/es/app.po index 25d8f59..dbae9a1 100644 --- a/config/locale/es/app.po +++ b/config/locale/es/app.po @@ -1384,6 +1384,9 @@ msgid "Top banner text" msgstr "Texto del banner superior" +msgid "Transfer customisation" +msgstr "" + #, fuzzy msgid "Un-published" msgstr "Publicado" diff --git a/config/locale/fr/app.po b/config/locale/fr/app.po index 1bc520c..31e100c 100644 --- a/config/locale/fr/app.po +++ b/config/locale/fr/app.po @@ -1381,6 +1381,9 @@ msgid "Top banner text" msgstr "Texte de la bannière en haut décran" +msgid "Transfer customisation" +msgstr "" + #, fuzzy msgid "Un-published" msgstr "Publiée" diff --git a/config/routes.rb b/config/routes.rb index 4591fc9..2250094 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -131,6 +131,7 @@ put 'admin_update' put 'admin_publish' put 'admin_unpublish' + get 'admin_transfer_customization' end end