diff --git a/app/controllers/admin/communication/extranets/posts_controller.rb b/app/controllers/admin/communication/extranets/posts_controller.rb index a1fe5349ba183a9257235adf50dca3d6753f9b0e..c1d4a5f65ae05fe169ad18f51a95016aa9fd7141 100644 --- a/app/controllers/admin/communication/extranets/posts_controller.rb +++ b/app/controllers/admin/communication/extranets/posts_controller.rb @@ -1,6 +1,82 @@ class Admin::Communication::Extranets::PostsController < Admin::Communication::Extranets::ApplicationController + load_and_authorize_resource class: Communication::Extranet::Post, through: :extranet + def index breadcrumb add_breadcrumb Communication::Extranet.human_attribute_name(:feature_posts) end + + def show + @preview = true + breadcrumb + end + + def preview + render layout: 'admin/layouts/preview' + end + + def new + @post.extranet = @extranet + if current_user.person.present? + @post.author = current_user.person + end + breadcrumb + end + + def edit + breadcrumb + add_breadcrumb t('edit') + end + + def create + @post.extranet = @extranet + @post.add_photo_import params[:photo_import] + if @post.save + redirect_to admin_communication_extranet_post_path(@post), notice: t('admin.successfully_created_html', model: @post.to_s) + else + load_categories + breadcrumb + render :new, status: :unprocessable_entity + end + end + + def update + @post.add_photo_import params[:photo_import] + if @post.update(post_params) + redirect_to admin_communication_extranet_post_path(@post), notice: t('admin.successfully_updated_html', model: @post.to_s) + else + load_categories + breadcrumb + add_breadcrumb t('edit') + render :edit, status: :unprocessable_entity + end + end + + def destroy + @post.destroy + redirect_to admin_communication_extranet_posts_url, notice: t('admin.successfully_destroyed_html', model: @post.to_s) + end + + protected + + def breadcrumb + super + add_breadcrumb Communication::Extranet::Post.model_name.human(count: 2), + admin_communication_extranet_posts_path + breadcrumb_for @post + end + + def post_params + params.require(:communication_extranet_post) + .permit( + :title, :summary, :text, + :published, :published_at, :slug, + :featured_image, :featured_image_delete, :featured_image_infos, :featured_image_alt, :featured_image_credit, + :author_id + ) + .merge( + university_id: current_university.id + ) + end + end \ No newline at end of file diff --git a/app/models/communication/extranet.rb b/app/models/communication/extranet.rb index 56c563be878e972b4019a38a8776348b5092c654..269d82c7414ea74f3fc92a38a01b77309d527979 100644 --- a/app/models/communication/extranet.rb +++ b/app/models/communication/extranet.rb @@ -57,6 +57,8 @@ class Communication::Extranet < ApplicationRecord attachable.variant :thumb, resize_to_limit: [228, 228] end + has_many :posts + validates_presence_of :name, :host validates :logo, size: { less_than: 1.megabytes } validates :favicon, size: { less_than: 1.megabytes } diff --git a/app/models/communication/extranet/post.rb b/app/models/communication/extranet/post.rb index 35e3fb3958c39a6a2f89634ecfaa812ce3fc5bfe..47d81a776dd56f81d25d4d3c3d996b7fb58b7a04 100644 --- a/app/models/communication/extranet/post.rb +++ b/app/models/communication/extranet/post.rb @@ -38,4 +38,25 @@ class Communication::Extranet::Post < ApplicationRecord belongs_to :author, class_name: 'University::Person' belongs_to :extranet, class_name: 'Communication::Extranet' + + validates :title, presence: true + + before_validation :set_published_at + + def to_s + "#{title}" + end + + protected + + def slug_unavailable?(slug) + self.class.unscoped + .where(extranet_id: self.extranet_id, slug: slug) + .where.not(id: self.id) + .exists? + end + + def set_published_at + self.published_at = Time.zone.now if published && published_at.nil? + end end diff --git a/app/views/admin/communication/extranets/posts/_form.html.erb b/app/views/admin/communication/extranets/posts/_form.html.erb new file mode 100644 index 0000000000000000000000000000000000000000..e82335fbeb15088be8e18bc6e108175dcef5957c --- /dev/null +++ b/app/views/admin/communication/extranets/posts/_form.html.erb @@ -0,0 +1,38 @@ +<%= simple_form_for [:admin, post] do |f| %> + <%= f.error_notification %> + <%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %> + + <div class="row"> + <div class="col-md-8"> + <%= osuny_panel t('content') do %> + <%= f.input :title %> + <%= render 'admin/application/summary/form', f: f, about: post %> + <% end %> + </div> + <div class="col-md-4"> + <%= osuny_panel t('metadata') do %> + <% if can? :publish, post %> + <div class="row pure__row--small"> + <div class="col-6"> + <%= f.input :published %> + </div> + </div> + <%= f.input :published_at, html5: true, as: :date %> + <% end %> + <%= f.association :author, + collection: @extranet.connected_persons.ordered, + label_method: :to_s_alphabetical %> + <%= f.input :slug, + as: :string, + input_html: post.persisted? ? {} : { + class: 'js-slug-input', + data: { source: '#communication_extranet_post_title' } + } %> + <% end %> + <%= render 'admin/application/featured_image/edit', about: @post, f: f %> + </div> + </div> + <% content_for :action_bar_right do %> + <%= submit f %> + <% end %> +<% end %> diff --git a/app/views/admin/communication/extranets/posts/_list.html.erb b/app/views/admin/communication/extranets/posts/_list.html.erb new file mode 100644 index 0000000000000000000000000000000000000000..1986fdaa671f6d831f4f2f32b74f15101b5025d7 --- /dev/null +++ b/app/views/admin/communication/extranets/posts/_list.html.erb @@ -0,0 +1,38 @@ +<div class="table-responsive"> + <table class="<%= table_classes %>"> + <thead> + <tr> + <th class="ps-0" width="60%"><%= Communication::Extranet::Post.human_attribute_name('title') %></th> + <th><%= Communication::Extranet::Post.human_attribute_name('featured_image') %></th> + <th colspan="2" class="ps-3"><%= Communication::Extranet::Post.human_attribute_name('meta') %></th> + </tr> + </thead> + <tbody> + <% posts.each do |post| %> + <tr <% unless post.published? %>class="draft"<% end %>> + <td class="ps-0"><%= link_to post, + admin_communication_extranet_post_path(extranet_id: post.extranet.id, id: post.id), + class: "#{'draft' unless post.published?}" %></td> + <td><%= image_tag post.featured_image.representation(resize: '200x'), + width: 100 if post.featured_image.attached? && post.featured_image.representable? %></td> + <td class="ps-3 small"> + <%= l post.published_at, format: :date_with_explicit_month if post.published_at %><br> + <%= post.author %> + </td> + <td> + <div class="btn-group" role="group"> + <%= link_to t('edit'), + edit_admin_communication_extranet_post_path(website_id: post.extranet.id, id: post.id), + class: button_classes if can?(:update, post) %> + <%= link_to t('delete'), + admin_communication_extranet_post_path(website_id: post.extranet.id, id: post.id), + method: :delete, + data: { confirm: t('please_confirm') }, + class: button_classes_danger if can?(:destroy, post) %> + </div> + </td> + </tr> + <% end %> + </tbody> + </table> +</div> diff --git a/app/views/admin/communication/extranets/posts/edit.html.erb b/app/views/admin/communication/extranets/posts/edit.html.erb new file mode 100644 index 0000000000000000000000000000000000000000..834ab342d7f5e13313cf8ebb245946da21961cfc --- /dev/null +++ b/app/views/admin/communication/extranets/posts/edit.html.erb @@ -0,0 +1,5 @@ +<% content_for :title, @post %> + +<%= render 'admin/communication/extranets/sidebar' do %> + <%= render 'form', post: @post %> +<% end %> diff --git a/app/views/admin/communication/extranets/posts/index.html.erb b/app/views/admin/communication/extranets/posts/index.html.erb index 479eb6c916b1ef5e3496f2da2fe31f50b3af6b36..57d0771ca34721682a96d22959fe3a2418445a8c 100644 --- a/app/views/admin/communication/extranets/posts/index.html.erb +++ b/app/views/admin/communication/extranets/posts/index.html.erb @@ -1,4 +1,9 @@ <% content_for :title, Communication::Extranet.human_attribute_name(:feature_posts) %> <%= render 'admin/communication/extranets/sidebar' do %> + <%= render 'admin/communication/extranets/posts/list', posts: @posts %> +<% end %> + +<% content_for :action_bar_right do %> + <%= create_link Communication::Extranet::Post %> <% end %> \ No newline at end of file diff --git a/app/views/admin/communication/extranets/posts/new.html.erb b/app/views/admin/communication/extranets/posts/new.html.erb new file mode 100644 index 0000000000000000000000000000000000000000..50458a1077b8aeffec6bc23b38efe9ebb23cd35a --- /dev/null +++ b/app/views/admin/communication/extranets/posts/new.html.erb @@ -0,0 +1,5 @@ +<% content_for :title, Communication::Website::Post.model_name.human %> + +<%= render 'admin/communication/extranets/sidebar' do %> + <%= render 'form', post: @post %> +<% end %> diff --git a/app/views/admin/communication/extranets/posts/preview.html.erb b/app/views/admin/communication/extranets/posts/preview.html.erb new file mode 100644 index 0000000000000000000000000000000000000000..14bd7cd482e0666eb021ef6bb70d0732497bf91f --- /dev/null +++ b/app/views/admin/communication/extranets/posts/preview.html.erb @@ -0,0 +1,3 @@ +<% content_for :title, @post.title %> +<% content_for :image, kamifusen_tag(@post.featured_image) if @post.featured_image.attached? %> +<%= render 'admin/communication/blocks/preview', about: @post %> diff --git a/app/views/admin/communication/extranets/posts/show.html.erb b/app/views/admin/communication/extranets/posts/show.html.erb new file mode 100644 index 0000000000000000000000000000000000000000..bfc161d16a70a42169601ec0b373ad5a08a393b9 --- /dev/null +++ b/app/views/admin/communication/extranets/posts/show.html.erb @@ -0,0 +1,47 @@ +<% content_for :title, @post %> + +<%= render 'admin/communication/extranets/sidebar' do %> + <div class="row"> + <div class="col-xl-8"> + <%= render 'admin/application/summary/show', about: @post %> + <%= render 'admin/communication/blocks/list', about: @post %> + </div> + <div class="col-xl-4"> + <% + action = '' + action += link_to t('open'), + '', + target: :_blank, + class: 'btn btn-light btn-xs' if @post.published + %> + <%= osuny_panel t('metadata'), action: action do %> + <div class="row pure__row--small"> + <div class="col-6"> + <%= osuny_label Communication::Website::Post.human_attribute_name('published') %> + <p> + <%= t @post.published %><% if @post.published & @post.published_at %>, + <%= l @post.published_at.to_date, format: :long if @post.published_at %> + <% end %> + </p> + </div> + </div> + <% if @post.author %> + <%= osuny_label Communication::Extranet::Post.human_attribute_name('author') %> + <p><%= @post.author %></p> + <% end %> + <%= osuny_label Communication::Website::Post.human_attribute_name('slug') %> + <p><%= @post.slug %></p> + <% end %> + <%= render 'admin/application/featured_image/show', about: @post %> + </div> + </div> +<% end %> + +<% content_for :action_bar_left do %> + <%= destroy_link @post %> +<% end %> + +<% content_for :action_bar_right do %> + <%= preview_link %> + <%= edit_link @post %> +<% end %> diff --git a/app/views/admin/communication/extranets/posts/static.html.erb b/app/views/admin/communication/extranets/posts/static.html.erb new file mode 100644 index 0000000000000000000000000000000000000000..4a3dd84b08f110417a3def554c4a6b007057a696 --- /dev/null +++ b/app/views/admin/communication/extranets/posts/static.html.erb @@ -0,0 +1,25 @@ +--- +title: "<%= @about.title %>" +date: "<%= @about.published_at&.iso8601 %>" +<%= render 'admin/application/static/permalink' %> +<%= render 'admin/application/static/design', full_width: false, toc_offcanvas: false %> +<% if @about.pinned %> +weight: 1 +<% end %> +<% if @about.author %> +authors: + - "<%= @about.translated_author.slug %>" +<% end %> +<% if @about.categories.any? %> +categories: + <% @about.categories.each do |category| %> + - "<%= category.slug_with_ancestors_slugs %>" + <% end %> +<% end %> +<%= render 'admin/application/i18n/static' %> +<%= render 'admin/application/featured_image/static' %> +<%= render 'admin/application/meta_description/static' %> +<%= render 'admin/application/summary/static' %> +<%= render 'admin/communication/blocks/static', about: @about %> +--- +<%= prepare_html_for_static @about.text, @about.university %> diff --git a/app/views/admin/communication/photo_imports/_selector.html.erb b/app/views/admin/communication/photo_imports/_selector.html.erb index 6486def70eaad4f95c67d350aa35791c68e60c8d..e68b465d8d4397cbeffbf3ede68f83d1a2e5da89 100644 --- a/app/views/admin/communication/photo_imports/_selector.html.erb +++ b/app/views/admin/communication/photo_imports/_selector.html.erb @@ -12,8 +12,8 @@ about_featured_image_credit = "##{about_identifier}_featured_image_credit" # fr, en... lang = about&.language&.iso_code if about.respond_to? :language # /admin/communication/photo_import.json?query=Page%20de%20test&per_page=12&page=1&lang=fr -unsplash_path = admin_communication_unsplash_path(website_id: nil, lang: nil, format: :json) -pexels_path = admin_communication_pexels_path(website_id: nil, lang: nil, format: :json) +unsplash_path = admin_communication_unsplash_path(website_id: nil, extranet_id: nil, lang: nil, format: :json) +pexels_path = admin_communication_pexels_path(website_id: nil, extranet_id: nil, lang: nil, format: :json) %> <div id="photo-import-app" v-cloak> diff --git a/config/locales/communication/en.yml b/config/locales/communication/en.yml index b763fef654ca73d46bb1ed9d7d48767e30250e21..69d2e573f8ed2dba7eeb78774e3c6371e332daa0 100644 --- a/config/locales/communication/en.yml +++ b/config/locales/communication/en.yml @@ -10,6 +10,10 @@ en: communication/extranet: one: Extranet other: Extranets + communication/extranet/post: + one: Post + other: Posts + all: All posts communication/website: one: Website other: Websites @@ -70,6 +74,15 @@ en: sso_name_identifier_format: Name Identifier Format sso_target_url: Target URL terms: Terms of service + communication/extranet/post: + author: Author + featured_image: Featured image + featured_image_alt: Alt text + featured_image_credit: Credit + published: Published? + published_at: Publication date + slug: Slug + title: Title communication/website: about: About about_: Independent website diff --git a/config/locales/communication/fr.yml b/config/locales/communication/fr.yml index c60961d1f39ff337991bdd24d1c7920df3373c16..de2e0b10d6a3f86e2da557ecdca0bb3a8c543e80 100644 --- a/config/locales/communication/fr.yml +++ b/config/locales/communication/fr.yml @@ -10,6 +10,10 @@ fr: communication/extranet: one: Extranet other: Extranets + communication/extranet/post: + one: Actualité + other: Actualités + all: Toutes les actualités communication/website: one: Site Web other: Sites Web @@ -70,6 +74,15 @@ fr: sso_name_identifier_format: Name Identifier Format sso_target_url: URL cible terms: Conditions d'utilisation + communication/extranet/post: + author: Auteur·rice + featured_image: Image à la une + featured_image_alt: Texte alternatif + featured_image_credit: Crédit + published: Publié ? + published_at: Date de publication + slug: Identifiant + title: Titre communication/website: about: Sujet du site about_: Site indépendant diff --git a/config/routes/admin/communication.rb b/config/routes/admin/communication.rb index ff54b5e6a83fc11a8af22c8f95b12f7475ca2bb9..6c9d653d7ef401061cc4a09347649d288fb9fb1b 100644 --- a/config/routes/admin/communication.rb +++ b/config/routes/admin/communication.rb @@ -73,7 +73,7 @@ namespace :communication do post :disconnect end end - resources :posts, only: :index, controller: 'extranets/posts' + resources :posts, controller: 'extranets/posts' resources :assets, only: :index, controller: 'extranets/assets' resources :jobs, only: :index, controller: 'extranets/jobs' end