diff --git a/app/controllers/templates_controller.rb b/app/controllers/templates_controller.rb index 742ce33..1a8a3ba 100644 --- a/app/controllers/templates_controller.rb +++ b/app/controllers/templates_controller.rb @@ -323,4 +323,29 @@ @current = Template.current(@template.dmptemplate_id) end + # PUT /org/admin/templates/:id/admin_copy + # ----------------------------------------------------- + def admin_copy + @template = Template.find(params[:id]) + authorize @template + + # Unpublish the live version + new_copy = Template.deep_copy(@template) + new_copy.title = "Copy of " + @template.title + new_copy.version = 0 + new_copy.published = false + new_copy.dmptemplate_id = loop do + random = rand 2147483647 + break random unless Template.exists?(dmptemplate_id: random) + end + + if new_copy.save + flash[:notice] = 'Template was successfully copied.' + redirect_to admin_template_template_path(id: new_copy.id, edit: true), notice: _('Information was successfully created.') + else + flash[:notice] = failed_create_error(new_copy, _('template')) + end + + end + end diff --git a/app/policies/template_policy.rb b/app/policies/template_policy.rb index 754e622..006ed23 100644 --- a/app/policies/template_policy.rb +++ b/app/policies/template_policy.rb @@ -57,6 +57,9 @@ user.can_modify_templates? end + def admin_copy? + user.can_modify_templates? && (template.org_id == user.org_id) + end class Scope < Scope def resolve @@ -64,4 +67,4 @@ end end -end \ No newline at end of file +end diff --git a/app/views/templates/admin_index.html.erb b/app/views/templates/admin_index.html.erb index ee4c3b5..ac225c7 100644 --- a/app/views/templates/admin_index.html.erb +++ b/app/views/templates/admin_index.html.erb @@ -63,6 +63,7 @@ <% else %>
<%= link_to _('Unpublish'), admin_unpublish_template_path(hash[:current]), method: :put, class: "dmp_table_link" %> <% end %> + <%= link_to _('Copy'), admin_copy_template_path(id: hash[:current].id), method: :put, class: "dmp_table_link" %> <% end %> diff --git a/config/routes.rb b/config/routes.rb index 4cea84f..65b9185 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -137,6 +137,7 @@ put 'admin_update' put 'admin_publish' put 'admin_unpublish' + put 'admin_copy' get 'admin_transfer_customization' end end