gnu-social/components/Collection/Util/Controller/MetaCollectionController.php

237 lines
8.8 KiB
PHP
Raw Normal View History

<?php
declare(strict_types = 1);
// {{{ License
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
// }}}
/**
* Collections Controller for GNU social
*
* @package GNUsocial
* @category Plugin
*
* @author Phablulo <phablulo@gmail.com>
* @copyright 2018-2019, 2021 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
namespace Component\Collection\Util\Controller;
use App\Core\DB;
use App\Core\Form;
use function App\Core\I18n\_m;
use App\Entity\LocalUser;
use App\Util\Common;
use App\Util\Exception\RedirectException;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormView;
use Symfony\Component\HttpFoundation\Request;
/**
* @template T of object
*
* @extends FeedController<T>
*/
abstract class MetaCollectionController extends FeedController
{
2022-02-26 14:09:41 +00:00
protected const SLUG = 'collectionsEntry';
protected const PLURAL_SLUG = 'collectionsList';
protected string $page_title = 'Collections';
abstract public function getCollectionUrl(int $owner_id, string $owner_nickname, int $collection_id): string;
/**
* @return T[]
*/
abstract public function getCollectionItems(int $owner_id, int $collection_id): array;
/**
* @return T[]
*/
abstract public function getCollectionsByActorId(int $owner_id): array;
/**
* @return T A collection
*/
abstract public function getCollectionBy(int $owner_id, int $collection_id): object;
abstract public function createCollection(int $owner_id, string $name): bool;
/**
* @return ControllerResultType
*/
public function collectionsViewByActorNickname(Request $request, string $nickname): array
{
$user = DB::findOneBy(LocalUser::class, ['nickname' => $nickname]);
return self::collectionsView($request, $user->getId(), $nickname);
}
/**
* @return ControllerResultType
*/
public function collectionsViewByActorId(Request $request, int $id): array
{
return self::collectionsView($request, $id, null);
}
/**
* Generate Collections page
*
* @param int $id actor id
* @param ?string $nickname actor nickname
*
* @return ControllerResultType twig template options
*/
public function collectionsView(Request $request, int $id, ?string $nickname): array
{
$collections = $this->getCollectionsByActorId($id);
2022-02-26 14:09:41 +00:00
$create_title = _m('Create a ' . mb_strtolower(preg_replace('/([a-z0-9])([A-Z])/', '$1 $2', static::SLUG)));
$collections_title = _m('The ' . mb_strtolower(preg_replace('/([a-z0-9])([A-Z])/', '$1 $2', static::PLURAL_SLUG)));
// create collection form
$create = null;
if (Common::user()?->getId() === $id) {
$create = Form::create([
['name', TextType::class, [
'label' => $create_title,
'attr' => [
'placeholder' => _m('Name'),
'required' => 'required',
],
'data' => '',
]],
['add_collection', SubmitType::class, [
'label' => $create_title,
'attr' => [
'title' => $create_title,
],
]],
]);
$create->handleRequest($request);
if ($create->isSubmitted() && $create->isValid()) {
$this->createCollection($id, $create->getData()['name']);
DB::flush();
throw new RedirectException();
}
}
// We need to inject some functions in twig,
// but I don't want to create an environment for this
// as twig docs suggest in https://twig.symfony.com/doc/2.x/advanced.html#functions.
//
// Instead, I'm using an anonymous class to encapsulate
// the functions and passing that class to the template.
2022-02-26 14:09:41 +00:00
// This is suggested at https://web.archive.org/web/20220226132328/https://stackoverflow.com/questions/3595727/twig-pass-function-into-template/50364502
$fn = new class($id, $nickname, $request, $this, static::SLUG) {
public function __construct(private int $id, private string $nickname, private Request $request, private object $parent, private string $slug)
{
}
// there's already an injected function called path,
// that maps to Router::url(name, args), but since
// I want to preserve nicknames, I think it's better
// to use that getUrl function
public function getUrl(int $cid): string
{
return $this->parent->getCollectionUrl($this->id, $this->nickname, $cid);
}
// There are many collections in this page and we need two
// forms for each one of them: one form to edit the collection's
// name and another to remove the collection.
// creating the edit form
public function editForm(object $collection): FormView
{
$edit = Form::create([
['name', TextType::class, [
'attr' => [
'placeholder' => 'New name',
'required' => 'required',
],
'data' => '',
]],
['update_' . $collection->getId(), SubmitType::class, [
'label' => _m('Save'),
'attr' => [
'title' => _m('Save'),
],
]],
]);
$edit->handleRequest($this->request);
if ($edit->isSubmitted() && $edit->isValid()) {
$this->parent->setCollectionName($this->id, $this->nickname, $collection, $edit->getData()['name']);
DB::flush();
throw new RedirectException();
}
return $edit->createView();
}
// creating the remove form
public function rmForm(object $collection): FormView
{
$rm = Form::create([
['remove_' . $collection->getId(), SubmitType::class, [
'label' => _m('Delete ' . $this->slug),
'attr' => [
'title' => _m('Delete ' . $this->slug),
'class' => 'danger',
],
]],
]);
$rm->handleRequest($this->request);
if ($rm->isSubmitted()) {
$this->parent->removeCollection($this->id, $this->nickname, $collection);
DB::flush();
throw new RedirectException();
}
return $rm->createView();
}
};
return [
'_template' => 'collection/meta_collections.html.twig',
'page_title' => $this->page_title,
'list_title' => $collections_title,
'add_collection' => $create?->createView(),
'fn' => $fn,
'collections' => $collections,
];
}
/**
* @return ControllerResultType
*/
public function collectionsEntryViewNotesByNickname(Request $request, string $nickname, int $cid): array
{
$user = DB::findOneBy(LocalUser::class, ['nickname' => $nickname]);
return self::collectionsEntryViewNotesByActorId($request, $user->getId(), $cid);
}
/**
* @return ControllerResultType
*/
public function collectionsEntryViewNotesByActorId(Request $request, int $id, int $cid): array
{
$collection = $this->getCollectionBy($id, $cid);
$vars = $this->getCollectionItems($id, $cid);
return array_merge([
'_template' => 'collections/collection_entry_view.html.twig',
'page_title' => $collection->getName(),
], $vars);
}
}