diff --git a/app/controllers/org_admin/templates_controller.rb b/app/controllers/org_admin/templates_controller.rb index c83ebf5..07d99a4 100644 --- a/app/controllers/org_admin/templates_controller.rb +++ b/app/controllers/org_admin/templates_controller.rb @@ -7,22 +7,17 @@ # ----------------------------------------------------- def index authorize Template - valid_orgs = current_user.can_super_admin? ? Org.not_funder : Org.find(current_user.org) - org_templates = Template.get_latest_template_versions(valid_orgs).page(1) - funder_templates = Template.get_latest_template_versions(Org.funder).page(1) + # Apply scoping + orgs_hash = apply_scoping(params[:scope] || 'all') + funders_hash = apply_scoping(params[:scope] || 'all', true) - # If the user is an Org Admin look for customizations to funder templates - customizations = {} - if current_user.can_org_admin? - funder_templates.each do |funder_template| - customization = Template.org_customizations(funder_template.dmptemplate_id, current_user.org_id) - customizations[customization.customization_of] = customization if customization.present? - end - end + # Apply pagination + orgs_hash[:templates] = orgs_hash[:templates].page(1) + funders_hash[:templates] = funders_hash[:templates].page(1) # Gather up all of the publication dates for the live versions of each template. published = {} - [funder_templates, org_templates].each do |collection| + [funders_hash[:templates], orgs_hash[:templates]].each do |collection| collection.each do |template| live = Template.live(template.dmptemplate_id) published[template.dmptemplate_id] = live.updated_at if live.present? @@ -30,12 +25,13 @@ end render 'index', locals: { - funder_templates: funder_templates, - org_templates: org_templates, - customized_templates: customizations, + funder_templates: funders_hash[:templates], + org_templates: orgs_hash[:templates], + customized_templates: funders_hash[:customizations], published: published, current_org: current_user.org, - orgs: Org.all + orgs: Org.all, + scopes: { orgs: orgs_hash[:scopes], funders: funders_hash[:scopes] } } end @@ -293,53 +289,50 @@ # ----------------------------------------------------- def funders authorize Template - if params[:page] == 'ALL' - templates = Template.get_latest_template_versions(Org.funder) - else - templates = Template.get_latest_template_versions(Org.funder).page(params[:page]) - end - # Include the default template in the list of funder templates - templates << Template.default - - # If the user is an Org Admin look for customizations to funder templates - customizations = [] - unless current_user.can_super_admin? - templates.each do |funder_template| - customization = Template.org_customizations(funder_template.id, current_user.org_id) - customizations << customization if customization.present? - end - end + # Apply scoping + hash = apply_scoping(params[:scope] || 'all', true) + + # Apply pagination + hash[:templates] = hash[:templates].page(params[:page]) if params[:page] != 'ALL' # Gather up all of the publication dates for the live versions of each template. published = {} - templates.each do |template| + hash[:templates].each do |template| live = Template.live(template.dmptemplate_id) published[template.dmptemplate_id] = live.updated_at if live.present? end - paginable_renderise(partial: 'funder_templates_list', scope: templates, - locals: {current_org: current_user.org.id, customizations: customizations, published: published}) + paginable_renderise partial: 'funder_templates_list', + scope: hash[:templates], + locals: { current_org: current_user.org.id, + customizations: hash[:customizations], + published: published, + scopes: hash[:scopes] } end # GET /org_admin/templates/orgs/:page (AJAX) # ----------------------------------------------------- def orgs authorize Template - valid_orgs = current_user.can_super_admin? ? Org.not_funder : Org.find(current_user.org) - if params[:page] == 'ALL' - templates = Template.get_latest_template_versions(valid_orgs) - else - templates = Template.get_latest_template_versions(valid_orgs).page(params[:page]) - end + # Apply scoping + hash = apply_scoping(params[:scope] || 'all') + + # Apply pagination + hash[:templates] = hash[:templates].page(params[:page]) if params[:page] != 'ALL' # Gather up all of the publication dates for the live versions of each template. published = {} - templates.each do |template| + hash[:templates].each do |template| live = Template.live(template.dmptemplate_id) published[template.dmptemplate_id] = live.updated_at if live.present? end - paginable_renderise(partial: 'templates_list', scope: templates, locals: {current_org: current_user.org.id, published: published}) + paginable_renderise partial: 'templates_list', + scope: hash[:templates], + locals: { current_org: current_user.org.id, + customizations: hash[:customizations], + published: published, + scopes: hash[:scopes] } end # PUT /org_admin/templates/:id/copy (AJAX) @@ -469,5 +462,78 @@ def plan_params params.require(:plan).permit(:org_id, :funder_id) end + + # Applies scoping to the template list + def apply_scoping(scope, funders = false) + if funders + templates = Template.get_latest_template_versions(Org.funder) + + # Include the default template in the list of funder templates + templates << Template.default + + # If the user is an Org Admin look for customizations to funder templates + customizations = {} + if current_user.can_org_admin? + families = templates.collect(&:dmptemplate_id).uniq + Template.org_customizations(families, current_user.org_id).each do |customization| + customizations[customization.customization_of] = customization if customization.present? + end + end + + scopes = calculate_table_scopes(templates, customizations) + + # We scope based on the customizations if the user is NOT a super admin + if params[:scope].present? && params[:scope] != 'all' + if current_user.can_super_admin? + templates = templates.where(published: true) if params[:scope] == 'published' + templates = templates.where(published: false) if params[:scope] == 'unpublished' + else + scoped = templates.select do |t| + c = customizations[t.dmptemplate_id] + (params[:scope] == 'unpublished' && (!c.present? || !c.published?)) || (params[:scope] == 'published' && c.present? && c.published?) + end + templates = Template.where(id: scoped.collect(&:id)) + end + end + + else + valid_orgs = current_user.can_super_admin? ? Org.not_funder : Org.find(current_user.org) + templates = Template.get_latest_template_versions(valid_orgs) + + scopes = calculate_table_scopes(templates, {}) + + if params[:scope].present? && params[:scope] != 'all' + templates = templates.where(published: true) if params[:scope] == 'published' + templates = templates.where(published: false) if params[:scope] == 'unpublished' + end + end + + { templates: templates, + customizations: customizations || {}, + scopes: scopes } + end + + # Gets the nbr of templates and nbr of published/unpublished templates + def calculate_table_scopes(templates, customizations) + scopes = { all: templates.length, published: 0, unpublished: 0 } + templates.each do |t| + # If we have customizations use their status + if customizations.length > 0 + c = customizations[t.dmptemplate_id] + # If the template was not customized then its unpublished + if c.nil? + scopes[:unpublished] += 1 + else + scopes[:published] += 1 if c.published? + scopes[:unpublished] += 1 unless c.published? + end + else + # Otherwise just use the template's published status + scopes[:published] += 1 if t.published? + scopes[:unpublished] += 1 unless t.published? + end + end + scopes + end end end \ No newline at end of file diff --git a/app/models/template.rb b/app/models/template.rb index deea49b..0ff158d 100644 --- a/app/models/template.rb +++ b/app/models/template.rb @@ -97,13 +97,13 @@ # specified org and dmptemplate_id # returns nil if no customizations found # - # @params [integer] dmptemplate_id of the original template + # @params dmptemplate_ids 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 + Template.where(customization_of: dmptemplate_id, org_id: org_id).order(version: :desc).valid end - + # Retrieves current templates with their org associated for a set of valid orgs # TODO pass an array of org ids instead of Org instances def self.get_latest_template_versions(orgs) diff --git a/app/views/org_admin/templates/_funder_templates_list.html.erb b/app/views/org_admin/templates/_funder_templates_list.html.erb index 5d1275c..36944c9 100644 --- a/app/views/org_admin/templates/_funder_templates_list.html.erb +++ b/app/views/org_admin/templates/_funder_templates_list.html.erb @@ -10,6 +10,13 @@ <% end %> + <% if scopes.present? %> + + <% end %> <%= _('Template Name') %> <%= _('Funder') %> diff --git a/app/views/org_admin/templates/_templates_list.html.erb b/app/views/org_admin/templates/_templates_list.html.erb index 43b5f77..97a5de1 100644 --- a/app/views/org_admin/templates/_templates_list.html.erb +++ b/app/views/org_admin/templates/_templates_list.html.erb @@ -10,6 +10,13 @@ <% end %> + <% if scopes.present? %> + + <% end %> <%= _('Template Name') %> <%= current_user.can_super_admin? ? _('Organisation') : _('Description') %> diff --git a/app/views/org_admin/templates/index.html.erb b/app/views/org_admin/templates/index.html.erb index 2424f36..550ebc9 100644 --- a/app/views/org_admin/templates/index.html.erb +++ b/app/views/org_admin/templates/index.html.erb @@ -41,7 +41,7 @@ controller: 'org_admin/templates', action: 'orgs', scope: org_templates, - locals: {current_org: current_org.id, published: published}) %> + locals: {current_org: current_org.id, published: published, scopes: scopes[:orgs]}) %> <% else %>

<%= _('There are currently no templates defined for your organisation.') %> <% end %> @@ -54,7 +54,7 @@ controller: 'org_admin/templates', action: 'funders', scope: funder_templates, - locals: {current_org: current_org.id, customizations: customized_templates, published: published}) %> + locals: {current_org: current_org.id, customizations: customized_templates, published: published, scopes: scopes[:funders]}) %> <% else %>

<%= _('There are currently no funder templates.') %> <% end %> diff --git a/lib/assets/javascripts/views/org_admin/templates/index.js b/lib/assets/javascripts/views/org_admin/templates/index.js index 086e10a..0456ac5 100644 --- a/lib/assets/javascripts/views/org_admin/templates/index.js +++ b/lib/assets/javascripts/views/org_admin/templates/index.js @@ -43,4 +43,9 @@ }); enableActions($('form#super-admin-switch-org')); } + + // Update the contents of the table when user clicks on a scope link + $('.template-scope').on('ajax:success', 'a[data-remote="true"]', (e, data) => { + $(e.target).closest('.paginable').html(data); + }); }); diff --git a/lib/assets/stylesheets/overrides.scss b/lib/assets/stylesheets/overrides.scss index b4d87e6..485284a 100644 --- a/lib/assets/stylesheets/overrides.scss +++ b/lib/assets/stylesheets/overrides.scss @@ -37,6 +37,15 @@ background-color: $grey; color: $white; } +thead th.table-scope { + background-color: $white; + font-weight: normal; + + a { + color: $grey; + padding: 2px 15px 2px 2px; + } +} .table-hover tbody tr:hover td, .table-hover tbody tr:hover th { background-color: #CCC; }