[CONTROLLER][CollectionController] Refactored methods names and form

titles
[PLUGINS][AttachmentCollections] Renamed respective Controller to
differentiate it between The Controller and itself, renamed templates
and removed unnecessary HTML from templates
This commit is contained in:
Eliseu Amaro 2022-01-02 18:40:09 +00:00 committed by Diogo Peralta Cordeiro
parent 7ad39fdc83
commit 28424402ec
Signed by: diogo
GPG Key ID: 18D2D35001FBFAB0
8 changed files with 74 additions and 91 deletions

View File

@ -33,7 +33,6 @@ namespace Plugin\AttachmentCollections;
use App\Core\DB\DB; use App\Core\DB\DB;
use App\Core\Event; use App\Core\Event;
use function App\Core\I18n\_m;
use App\Core\Modules\Collection; use App\Core\Modules\Collection;
use App\Core\Router\RouteLoader; use App\Core\Router\RouteLoader;
use App\Core\Router\Router; use App\Core\Router\Router;
@ -41,10 +40,11 @@ use App\Entity\Actor;
use App\Entity\Feed; use App\Entity\Feed;
use App\Entity\LocalUser; use App\Entity\LocalUser;
use App\Util\Nickname; use App\Util\Nickname;
use Plugin\AttachmentCollections\Controller as C; use Plugin\AttachmentCollections\Controller\AttachmentCollections as AttachmentCollectionsController;
use Plugin\AttachmentCollections\Entity\AttachmentCollection; use Plugin\AttachmentCollections\Entity\AttachmentCollection;
use Plugin\AttachmentCollections\Entity\AttachmentCollectionEntry; use Plugin\AttachmentCollections\Entity\AttachmentCollectionEntry;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use function App\Core\I18n\_m;
class AttachmentCollections extends Collection class AttachmentCollections extends Collection
{ {
@ -128,23 +128,23 @@ class AttachmentCollections extends Collection
$r->connect( $r->connect(
id: 'collections_view_by_actor_id', id: 'collections_view_by_actor_id',
uri_path: '/actor/{id<\d+>}/collections', uri_path: '/actor/{id<\d+>}/collections',
target: [C\Controller::class, 'collectionsViewByActorId'], target: [AttachmentCollectionsController::class, 'collectionsListViewByActorId'],
); );
$r->connect( $r->connect(
id: 'collections_view_by_nickname', id: 'collections_view_by_nickname',
uri_path: '/@{nickname<' . Nickname::DISPLAY_FMT . '>}/collections', uri_path: '/@{nickname<' . Nickname::DISPLAY_FMT . '>}/collections',
target: [C\Controller::class, 'collectionsByActorNickname'], target: [AttachmentCollectionsController::class, 'collectionsListViewByActorNickname'],
); );
// View notes from a collection by actor id and nickname // View notes from a collection by actor id and nickname
$r->connect( $r->connect(
id: 'collection_notes_view_by_actor_id', id: 'collection_notes_view_by_actor_id',
uri_path: '/actor/{id<\d+>}/collections/{cid<\d+>}', uri_path: '/actor/{id<\d+>}/collections/{cid<\d+>}',
target: [C\Controller::class, 'collectionNotesViewByActorId'], target: [AttachmentCollectionsController::class, 'collectionsEntryViewNotesByActorId'],
); );
$r->connect( $r->connect(
id: 'collection_notes_view_by_nickname', id: 'collection_notes_view_by_nickname',
uri_path: '/@{nickname<' . Nickname::DISPLAY_FMT . '>}/collections/{cid<\d+>}', uri_path: '/@{nickname<' . Nickname::DISPLAY_FMT . '>}/collections/{cid<\d+>}',
target: [C\Controller::class, 'collectionNotesByNickname'], target: [AttachmentCollectionsController::class, 'collectionsEntryViewNotesByNickname'],
); );
return Event::next; return Event::next;
} }

View File

@ -28,7 +28,7 @@ use App\Core\DB\DB;
use App\Core\Router\Router; use App\Core\Router\Router;
use Plugin\AttachmentCollections\Entity\AttachmentCollection; use Plugin\AttachmentCollections\Entity\AttachmentCollection;
class Controller extends CollectionController class AttachmentCollections extends CollectionController
{ {
public function createCollection(int $owner_id, string $name) public function createCollection(int $owner_id, string $name)
{ {
@ -64,7 +64,7 @@ class Controller extends CollectionController
['cid' => $collection_id], ['cid' => $collection_id],
); );
return [ return [
'_template' => 'AttachmentCollections/collection.html.twig', '_template' => 'AttachmentCollections/collection_entry_view.html.twig',
'attachments' => array_values($attachs), 'attachments' => array_values($attachs),
'bare_notes' => array_values($notes), 'bare_notes' => array_values($notes),
]; ];

View File

@ -1,4 +1,4 @@
{% extends 'collections/collection.html.twig' %} {% extends 'collections/collection_entry_view.html.twig' %}
{% block collection_items %} {% block collection_items %}
{% for key, attachment in attachments %} {% for key, attachment in attachments %}

View File

@ -33,8 +33,6 @@ namespace App\Core\Controller;
use App\Core\DB\DB; use App\Core\DB\DB;
use App\Core\Form; use App\Core\Form;
use function App\Core\I18n\_m;
use App\Core\Router\Router;
use App\Entity\LocalUser; use App\Entity\LocalUser;
use App\Util\Common; use App\Util\Common;
use App\Util\Exception\RedirectException; use App\Util\Exception\RedirectException;
@ -42,12 +40,13 @@ use Component\Feed\Util\FeedController;
use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use function App\Core\I18n\_m;
abstract class CollectionController extends FeedController abstract class CollectionController extends FeedController
{ {
protected string $slug = 'collection'; protected string $slug = 'collectionsEntry';
protected string $plural_slug = 'collections'; protected string $plural_slug = 'collectionsList';
protected string $page_title = 'collections'; protected string $page_title = 'Collections';
abstract public function getCollectionUrl(int $owner_id, string $owner_nickname, int $collection_id): string; abstract public function getCollectionUrl(int $owner_id, string $owner_nickname, int $collection_id): string;
abstract public function getCollectionItems(int $owner_id, $collection_id): array; abstract public function getCollectionItems(int $owner_id, $collection_id): array;
@ -55,15 +54,15 @@ abstract class CollectionController extends FeedController
abstract public function getCollectionBy(int $owner_id, int $collection_id); abstract public function getCollectionBy(int $owner_id, int $collection_id);
abstract public function createCollection(int $owner_id, string $name); abstract public function createCollection(int $owner_id, string $name);
public function collectionsByActorNickname(Request $request, string $nickname): array public function collectionsListViewByActorNickname(Request $request, string $nickname): array
{ {
$user = DB::findOneBy(LocalUser::class, ['nickname' => $nickname]); $user = DB::findOneBy(LocalUser::class, ['nickname' => $nickname]);
return self::collectionsView($request, $user->getId(), $nickname); return self::collectionsListView($request, $user->getId(), $nickname);
} }
public function collectionsViewByActorId(Request $request, int $id): array public function collectionsListViewByActorId(Request $request, int $id): array
{ {
return self::collectionsView($request, $id, null); return self::collectionsListView($request, $id, null);
} }
/** /**
@ -74,16 +73,18 @@ abstract class CollectionController extends FeedController
* *
* @return array twig template options * @return array twig template options
*/ */
public function collectionsView(Request $request, int $id, ?string $nickname): array public function collectionsListView(Request $request, int $id, ?string $nickname): array
{ {
$collections = $this->getCollectionsBy($id); $collections = $this->getCollectionsBy($id);
$create_title = _m('Create a ' . mb_strtolower(preg_replace( '/([a-z0-9])([A-Z])/', "$1 $2", $this->slug)));
$collections_title = _m('Your ' . mb_strtolower(preg_replace( '/([a-z0-9])([A-Z])/', "$1 $2", $this->plural_slug)));
// create collection form // create collection form
$create = null; $create = null;
if (Common::user()?->getId() === $id) { if (Common::user()?->getId() === $id) {
$create = Form::create([ $create = Form::create([
['name', TextType::class, [ ['name', TextType::class, [
'label' => _m('Create ' . $this->slug), 'label' => $create_title,
'attr' => [ 'attr' => [
'placeholder' => _m('Name'), 'placeholder' => _m('Name'),
'required' => 'required', 'required' => 'required',
@ -91,9 +92,9 @@ abstract class CollectionController extends FeedController
'data' => '', 'data' => '',
]], ]],
['add_collection', SubmitType::class, [ ['add_collection', SubmitType::class, [
'label' => _m('Create ' . $this->slug), 'label' => $create_title,
'attr' => [ 'attr' => [
'title' => _m('Create ' . $this->slug), 'title' => $create_title,
], ],
]], ]],
]); ]);
@ -190,27 +191,27 @@ abstract class CollectionController extends FeedController
}; };
return [ return [
'_template' => 'collections/collections.html.twig', '_template' => 'collections/collection_list_view.html.twig',
'page_title' => $this->page_title, 'page_title' => $this->page_title,
'list_title' => 'Your ' . $this->plural_slug, 'list_title' => $collections_title,
'add_collection' => $create?->createView(), 'add_collection' => $create?->createView(),
'fn' => $fn, 'fn' => $fn,
'collections' => $collections, 'collections' => $collections,
]; ];
} }
public function collectionNotesByNickname(Request $request, string $nickname, int $cid): array public function collectionsEntryViewNotesByNickname(Request $request, string $nickname, int $cid): array
{ {
$user = DB::findOneBy(LocalUser::class, ['nickname' => $nickname]); $user = DB::findOneBy(LocalUser::class, ['nickname' => $nickname]);
return self::collectionNotesByActorId($request, $user->getId(), $cid); return self::collectionsEntryViewNotesByActorId($request, $user->getId(), $cid);
} }
public function collectionNotesByActorId(Request $request, int $id, int $cid): array public function collectionsEntryViewNotesByActorId(Request $request, int $id, int $cid): array
{ {
$collection = $this->getCollectionBy($id, $cid); $collection = $this->getCollectionBy($id, $cid);
$vars = $this->getCollectionItems($id, $cid); $vars = $this->getCollectionItems($id, $cid);
return array_merge([ return array_merge([
'_template' => 'collections/collection.html.twig', '_template' => 'collections/collection_entry_view.html.twig',
'page_title' => $collection->getName(), 'page_title' => $collection->getName(),
], $vars); ], $vars);
} }

View File

@ -1,20 +0,0 @@
{% extends 'stdgrid.html.twig' %}
{% import '/cards/note/view.html.twig' as noteView %}
{% block title %}{{ page_title | trans }}{% endblock %}
{% block stylesheets %}
{{ parent() }}
<link rel="stylesheet" href="{{ asset('assets/default_theme/css/pages/feeds.css') }}" type="text/css">
{% endblock stylesheets %}
{% block body %}
<h1>{{ page_title | trans }}</h1>
{# Backwards compatibility with hAtom 0.1 #}
<main class="feed" tabindex="0" role="feed">
<div class="h-feed hfeed notes">
{% block collection_items %}
{% endblock collection_items %}
</div>
</main>
{% endblock body %}

View File

@ -0,0 +1,11 @@
{% extends '/feed/feed.html.twig' %}
{% block title %}{{ page_title | trans }}{% endblock %}
{% block body %}
<div class="section-widget section-padding">
<h2 class="section-widget-title">{{ page_title | trans }}</h2>
{% block collection_items %}
{% endblock collection_items %}
</div>
{% endblock body %}

View File

@ -0,0 +1,34 @@
{% extends 'stdgrid.html.twig' %}
{% block title %}{{ page_title | trans }}{% endblock %}
{% block body %}
<div class="section-widget section-padding">
<h2 class="section-widget-title">{{ page_title | trans }}</h2>
{% if add_collection %}
<div class="section-widget section-form">
{{ form(add_collection) }}
</div>
{% endif %}
<div class="section-widget collections-list">
<h3>{{ list_title | trans }}</h3>
{% for col in collections %}
<div class="collection-item">
<a class="name" href="{{ fn.getUrl(col.id) }}">{{ col.name }}</a>
<details title="Expand if you want to edit the collection's name">
<summary>
<span class="collection-action">{{ icon('edit') | raw }}</span>
</summary>
{{ form(fn.editForm(col)) }}
</details>
<details title="Expand if you want to delete the collection">
<summary>
<span class="collection-action">{{ icon('delete') | raw }}</span>
</summary>
{{ form(fn.rmForm(col)) }}
</details>
</div>
{% endfor %}
</div>
</div>
{% endblock body %}

View File

@ -1,43 +0,0 @@
{% extends 'stdgrid.html.twig' %}
{% import '/cards/note/view.html.twig' as noteView %}
{% block title %}{{ page_title | trans }}{% endblock %}
{% block stylesheets %}
{{ parent() }}
<link rel="stylesheet" href="{{ asset('assets/default_theme/css/pages/feeds.css') }}" type="text/css">
{% endblock stylesheets %}
{% block body %}
<h1>{{ page_title | trans }}</h1>
{# Backwards compatibility with hAtom 0.1 #}
<main class="feed" tabindex="0" role="feed">
<div class="h-feed hfeed notes">
{% if add_collection %}
<div class="h-entry hentry note collection-add">
{{ form(add_collection) }}
</div>
{% endif %}
<div class="h-entry hentry note collections-list">
<h3>{{ list_title | trans }}</h3>
{% for col in collections %}
<div class="collection-item">
<a class="name" href="{{ fn.getUrl(col.id) }}">{{ col.name }}</a>
<details title="Expand if you want to edit the collection's name">
<summary>
<div class="collection-action">{{ icon('edit') | raw }}</div>
</summary>
{{ form(fn.editForm(col)) }}
</details>
<details title="Expand if you want to delete the collection">
<summary>
<div class="collection-action">{{ icon('delete') | raw }}</div>
</summary>
{{ form(fn.rmForm(col)) }}
</details>
</div>
{% endfor %}
</div>
</div>
</main>
{% endblock body %}