Commit 49fb92b6 authored by Alexandru Lupu - Tremend's avatar Alexandru Lupu - Tremend
Browse files

[DIFE-1518] Add media gallery to Plenary

parent 77b408e7
......@@ -12,28 +12,9 @@ module Decidim
end
def public_name_key
names = []
names.push(translated_attribute(target_name)) if target_name.present?
names.push(translated_attribute(component_name)) if component_name.present?
names.compact!
return I18n.t(model.public_name_key) unless model.public_name_key.respond_to? :call
"#{t(model.public_name_key) } (#{names.join('/')})"
end
def target_name
@target_name = target.try(:name) rescue ""
end
def target
Decidim::Component.find(model.scoped_resource_id)
end
def component_name
@component_name = gallery_component.try(:name) rescue ""
end
def gallery_component
@gallery ||= Decidim::Component.find(model.settings.gallery_id)
model.public_name_key.call(model)
end
end
end
......
<h3 class="section-heading gallery-heading"><%= title %> (<%= t( ".#{gallery_component.settings.gallery_type}", count: items.total_count) %>)</h3>
<div class="row small-up medium-up-2 large-up-3 mb-l gallery image" id="gallery_<%= model.section %>_<%=model.id %>">
<div class="row small-up <%= model.css %> mb-l gallery image" id="gallery_<%= model.section %>_<%=model.id %>">
<%= cell("decidim/gallery/main", model).collection %>
</div>
<% unless items.total_count.zero? || items.last_page? %>
......
<h3 class="section-heading gallery-heading"><%= title %> (<%= t( ".#{gallery_component.settings.gallery_type}", count: items.total_count) %>)</h3>
<div class="row small-up medium-up-2 large-up-3 mb-l gallery video" id="gallery_<%= model.section %>_<%=model.id %>">
<div class="row small-up <%= model.css %> mb-l gallery video" id="gallery_<%= model.section %>_<%=model.id %>">
<%= cell("decidim/gallery/main", model).collection %>
</div>
<% unless items.total_count.zero? || items.last_page? %>
......
......@@ -4,7 +4,6 @@ module Decidim
module Gallery
class MainCell < Decidim::ViewModel
delegate :public_name_key, :has_settings?, to: :model
delegate :current_participatory_space, to: :controller
def show
render gallery_component.settings.gallery_type.to_sym if gallery_component.present?
......@@ -42,7 +41,7 @@ module Decidim
end
def gallery_component
@gallery ||= current_participatory_space.components.where(id: model.gallery_id).first
@gallery ||= Decidim::Component.where(id: model.gallery_id).first
end
def browser
......@@ -52,6 +51,10 @@ module Decidim
)
end
def current_participatory_space
controller.try(:current_participatory_space)
end
protected
def per_page
......
<div class="row">
<div class="row <%= model.scope_name %>">
<div class="column">
<h2 class="gallery-header"><%= title %></h2>
<%= cell("decidim/gallery/main", gallery0_settings, page: 1) if model.settings.gallery0_enabled %>
......
......@@ -10,11 +10,11 @@ module Decidim
end
def gallery1_settings
OpenStruct.new(id: model.id, section: :gallery1, gallery_id: settings.gallery1_id, per_page: settings.gallery1_per_page )
OpenStruct.new(id: model.id, css: settings.css, section: :gallery1, gallery_id: settings.gallery1_id, per_page: settings.gallery1_per_page )
end
def gallery0_settings
OpenStruct.new(id: model.id, section: :gallery0, gallery_id: settings.gallery0_id, per_page: settings.gallery0_per_page )
OpenStruct.new(id: model.id, css: settings.css, section: :gallery0, gallery_id: settings.gallery0_id, per_page: settings.gallery0_per_page )
end
end
end
......
......@@ -5,8 +5,9 @@ module Decidim
class MainContainerSettingsFormCell < Decidim::ViewModel
alias form model
delegate :current_participatory_space, to: :controller
def current_participatory_space
controller.try(:current_participatory_space)
end
end
end
end
<%
i18n_scope = "decidim.gallery.main_container_settings_form"
query = Decidim::Component.where(manifest_name: "gallery")
%>
<% form.fields_for :settings, form.object.settings do |settings_fields| %>
<%= settings_fields.translated :text_field, :title, label: t("title", scope: i18n_scope) %>
<%= settings_fields.check_box :gallery0_enabled, label: t('gallery_enabled', scope: i18n_scope) %>
<%= settings_fields.select(:gallery0_id, query.collect {|p| [ translated_attribute(p.name), p.id ] }, { include_blank: true, label: t('gallery_id', scope: i18n_scope) }) %>
<%= settings_fields.select :gallery0_per_page, [6, 12, 18], prompt: "", label: t('results_per_page', scope: i18n_scope) %>
<%= settings_fields.check_box :gallery1_enabled, label: t('gallery_enabled', scope: i18n_scope) %>
<%= settings_fields.select(:gallery1_id, query.collect {|p| [ translated_attribute(p.name), p.id ] }, { include_blank: true, label: t('gallery_id', scope: i18n_scope) }) %>
<%= settings_fields.select :gallery1_per_page, [6, 12, 18], prompt: "", label: t('results_per_page', scope: i18n_scope) %>
<%= settings_fields.text_field :css, label: t("css", scope: i18n_scope) %>
<% end %>
# frozen_string_literal: true
module Decidim
module Gallery
class PageMainContainerSettingsFormCell < MainContainerSettingsFormCell
end
end
end
# frozen_string_literal: true
module Decidim
module Gallery
module GalleryPageAdmin
module ContentBlock
extend ActiveSupport::Concern
included do
def permission_class_chain
[ Decidim::ContentBlocks::Admin::Permissions ] # + super
end
def pages
current_organization.static_pages
end
def defined_content_blocks
:gallery_container
end
def content_blocks
@content_blocks ||= Decidim::ContentBlock.for_scope(
content_block_scope,
organization: current_organization
).where(scoped_resource_id: pages).where(manifest_name: defined_content_blocks)
end
def content_block_scope
:gallery_page
end
end
end
end
end
end
# frozen_string_literal: true
module Decidim
module Gallery
module GalleryPageAdmin
class ContentBlocksController < Decidim::Admin::ApplicationController
# include Decidim::Assemblies::Admin::Concerns::AssemblyAdmin
include Decidim::Gallery::GalleryPageAdmin::ContentBlock
helper_method :content_block
def edit
enforce_permission_to :update, permission_scope, organization: current_organization
@form = form(Decidim::Admin::ContentBlockForm).from_model(content_block)
end
def update
enforce_permission_to :update, permission_scope, organization: current_organization
@form = form(Decidim::Admin::ContentBlockForm).from_params(params)
Decidim::Admin::UpdateContentBlock.call(@form, content_block, content_block_scope) do
on(:ok) do
redirect_to root_path
end
on(:invalid) do
render :edit
end
end
end
def content_block
@content_block ||= content_blocks.find(params[:id])
end
end
end
end
end
# frozen_string_literal: true
module Decidim
module Gallery
module GalleryPageAdmin
class ListContentBlocksController < Decidim::Admin::ApplicationController
# # include Decidim::Assemblies::Admin::Concerns::AssemblyAdmin
include Decidim::Gallery::GalleryPageAdmin::ContentBlock
helper_method :active_blocks, :inactive_blocks, :content_path
def index
enforce_permission_to :update, permission_scope, organization: current_organization
end
def update
enforce_permission_to :update, permission_scope, organization: current_organization
if params[:manifest_ids].present?
Decidim::Gallery::GalleryAdmin::ReorderContentBlocks.call(current_organization, content_block_scope, params[:manifest_ids]) do
on(:ok) do
head :ok
end
on(:invalid) do
head :bad_request
end
end
else
active_blocks.map(&:unpublish!)
head :ok
end
end
protected
def content_path
update_path
end
def permission_scope
:gallery_page
end
def active_blocks
@active_blocks ||= content_blocks.published
end
def inactive_blocks
@inactive_blocks ||= (unused_manifests + content_blocks.unpublished).uniq
end
def unused_manifests
return [] if content_blocks.size == pages.size
@unused_manifests = []
pages.find_each do |page|
@unused_manifests.push content_blocks.where(scoped_resource_id: page.id).first_or_create!
end
@unused_manifests
end
end
end
end
end
<!-- insert_after ".static__content" -->
<% content_block = Decidim::ContentBlock.published.
for_scope(:gallery_page, organization: current_organization).
where(scoped_resource_id: page.id, manifest_name: :gallery_container).first %>
<% if content_block.present? %>
<%= cell content_block.manifest.cell, content_block %>
<% end %>
<!-- insert_after "erb[loud]:contains('[:new, :static_page]')" -->
<%= link_to "Gallery Content Blocks", gallery_page_content_blocks_path, class: "button expanded small new" %>
<%= decidim_form_for(@form, html: { class: "form edit_content_block" }, url: content_block_path(params[:id])) do |form| %>
<div class="card">
<div class="card-divider">
<h2 class="card-title"><%= content_block.public_name_key.call(content_block) %></h2>
</div>
<div class="card-section">
<%= cell content_block.settings_form_cell, form, content_block: content_block %>
</div>
</div>
<div class="button--double form-general-submit">
<%= form.submit t("participatory_process_group_landing_page_content_blocks.edit.update", scope: "decidim.admin") %>
</div>
<% end %>
<div class="grid-container full">
<div class="grid-x grid-margin-x card-grid">
<div class="cell small-6">
<div class="card">
<div class="card-section">
<p class="mb-m"><%= t("participatory_process_group_landing_page.edit.active_content_blocks", scope: "decidim.admin") %></p>
<ul class="draggable-list js-connect js-list-actives" data-sort-url="<%= content_path %>">
<% active_blocks.each do |content_block| %>
<%= cell "decidim/gallery/content_block", content_block, context: {
link: edit_content_block_path(content_block)
} %>
<% end %>
</ul>
</div>
</div>
</div>
<div class="cell small-6">
<div class="card">
<div class="card-section">
<p class="mb-m"><%= t("participatory_process_group_landing_page.edit.inactive_content_blocks", scope: "decidim.admin") %></p>
<ul class="draggable-list js-connect js-list-availables">
<% inactive_blocks.each do |content_block| %>
<%= cell "decidim/gallery/content_block", content_block, context: {
link: edit_content_block_path(content_block)
} %>
<% end %>
</ul>
</div>
</div>
</div>
</div>
</div>
<script>
document.addEventListener("dragend", function (event) {
var activeBlocks = Array.prototype.slice.call(document.querySelectorAll(".js-list-actives li"));
var activeBlocksManifestName = activeBlocks.map(block => block.dataset.id);
var sortUrl = document.querySelector(".js-list-actives").dataset.sortUrl;
$.ajax({
method: "PUT",
url: sortUrl,
contentType: "application/json",
data: JSON.stringify({ manifest_ids: activeBlocksManifestName })
});
})
</script>
......@@ -8,6 +8,7 @@ en:
gallery_title: Gallery
gallery:
main_container_settings_form:
css: "Css used"
gallery_id: "Gallery"
gallery_enabled: "Gallery is enabled"
results_per_page: "Results per page"
......
......@@ -4,6 +4,8 @@ require "decidim/gallery/admin"
require "decidim/gallery/engine"
require "decidim/gallery/gallery_admin"
require "decidim/gallery/gallery_admin_engine"
require "decidim/gallery/gallery_page_admin"
require "decidim/gallery/gallery_page_admin_engine"
require "decidim/gallery/admin_engine"
require "decidim/gallery/component"
......
......@@ -33,6 +33,9 @@ module Decidim
Decidim::ParticipatoryProcesses::AdminEngine.routes do
mount Decidim::Gallery::GalleryAdminEngine, at: "/assemblies/:assembly_slug/gallery_content_blocks", as: :gallery_content_blocks
end
Decidim::Admin::Engine.routes do
mount Decidim::Gallery::GalleryPageAdminEngine, at: "/static_pages_content_blocks", as: :gallery_page_content_blocks
end
end
initializer "gallery_admin.view_hooks" do
......
......@@ -20,7 +20,44 @@ module Decidim
Decidim.content_blocks.register(:gallery_blog, :gallery_container) do |content_block|
content_block.cell = "decidim/gallery/main_container"
content_block.settings_form_cell = "decidim/gallery/main_container_settings_form"
content_block.public_name_key = "decidim.gallery.content_blocks.header.name"
content_block.public_name_key = Proc.new do |model|
target_name = Decidim::Component.where(id: model.scoped_resource_id).first.name || ""
component_name = Decidim::Component.where(id: model.settings.try(:gallery_id)).first.try(:name) || ""
names = []
names.push(model.organization.translated_attribute(target_name)) if target_name.present?
names.push(model.organization.translated_attribute(component_name)) if component_name.present?
names.compact!
"#{I18n.t("decidim.gallery.content_blocks.header.name") } (#{names.join('/')})"
end
content_block.settings do |settings|
settings.attribute :title, type: :string, translated: true
settings.attribute :gallery0_enabled, type: :boolean, default: true
settings.attribute :gallery0_id, type: :integer
settings.attribute :gallery0_per_page, type: :integer, default: 12
settings.attribute :gallery1_enabled, type: :boolean, default: true
settings.attribute :gallery1_id, type: :integer
settings.attribute :gallery1_per_page, type: :integer, default: 12
settings.attribute :css, type: :string, default: "medium-up-2 large-up-3"
end
end
Decidim.content_blocks.register(:gallery_page, :gallery_container) do |content_block|
content_block.cell = "decidim/gallery/main_container"
content_block.settings_form_cell = "decidim/gallery/page_main_container_settings_form"
content_block.public_name_key = Proc.new do |model|
target_name = Decidim::StaticPage.where(id: model.scoped_resource_id).first.title || ""
component_name = Decidim::Component.where(id: model.settings.try(:gallery_id)).first.try(:name) || ""
names = []
names.push(model.organization.translated_attribute(target_name)) if target_name.present?
names.push(model.organization.translated_attribute(component_name)) if component_name.present?
names.compact!
"#{I18n.t("decidim.gallery.content_blocks.header.name") } (#{names.join('/')})"
end
content_block.settings do |settings|
settings.attribute :title, type: :string, translated: true
......@@ -30,6 +67,7 @@ module Decidim
settings.attribute :gallery1_enabled, type: :boolean, default: true
settings.attribute :gallery1_id, type: :integer
settings.attribute :gallery1_per_page, type: :integer, default: 12
settings.attribute :css, type: :string, default: "medium-up-2 large-up-2"
end
end
end
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment