2021-12-24 00:38:06 +00:00
|
|
|
<?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/>.
|
|
|
|
|
|
|
|
// }}}
|
|
|
|
|
|
|
|
namespace Plugin\AttachmentCollections\Controller;
|
|
|
|
|
|
|
|
use App\Core\DB\DB;
|
2021-12-26 09:48:16 +00:00
|
|
|
use App\Core\Form;
|
2021-12-24 00:38:06 +00:00
|
|
|
use function App\Core\I18n\_m;
|
2021-12-26 09:48:16 +00:00
|
|
|
use App\Core\Router\Router;
|
2021-12-28 04:39:09 +00:00
|
|
|
use App\Entity\LocalUser;
|
2021-12-26 09:48:16 +00:00
|
|
|
use App\Util\Common;
|
2021-12-28 04:39:09 +00:00
|
|
|
use App\Util\Exception\RedirectException;
|
|
|
|
use Component\Collection\Entity\Collection;
|
2021-12-24 00:38:06 +00:00
|
|
|
use Component\Feed\Util\FeedController;
|
|
|
|
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
2021-12-26 09:48:16 +00:00
|
|
|
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2021-12-24 00:38:06 +00:00
|
|
|
|
|
|
|
class Controller extends FeedController
|
|
|
|
{
|
|
|
|
public function collectionsByActorNickname(Request $request, string $nickname): array
|
|
|
|
{
|
2021-12-28 04:39:09 +00:00
|
|
|
$user = DB::findOneBy(LocalUser::class, ['nickname' => $nickname]);
|
2021-12-24 00:38:06 +00:00
|
|
|
return self::collectionsView($request, $user->getId(), $nickname);
|
|
|
|
}
|
2021-12-28 04:39:09 +00:00
|
|
|
|
2021-12-24 00:38:06 +00:00
|
|
|
public function collectionsViewByActorId(Request $request, int $id): array
|
|
|
|
{
|
|
|
|
return self::collectionsView($request, $id, null);
|
|
|
|
}
|
2021-12-28 04:39:09 +00:00
|
|
|
|
2021-12-25 14:24:40 +00:00
|
|
|
/**
|
|
|
|
* Generate Collections page
|
2021-12-26 09:48:16 +00:00
|
|
|
*
|
2021-12-25 14:24:40 +00:00
|
|
|
* @param int $id actor id
|
|
|
|
* @param ?string $nickname actor nickname
|
2021-12-26 09:48:16 +00:00
|
|
|
*
|
|
|
|
* @return array twig template options
|
2021-12-25 14:24:40 +00:00
|
|
|
*/
|
2021-12-24 00:38:06 +00:00
|
|
|
public function collectionsView(Request $request, int $id, ?string $nickname): array
|
|
|
|
{
|
2021-12-28 04:39:09 +00:00
|
|
|
$collections = DB::findBy(Collection::class, ['actor_id' => $id]);
|
|
|
|
|
2021-12-25 14:24:40 +00:00
|
|
|
// create collection form
|
2021-12-24 00:38:06 +00:00
|
|
|
$create = null;
|
|
|
|
if (Common::user()?->getId() === $id) {
|
|
|
|
$create = Form::create([
|
|
|
|
['name', TextType::class, [
|
|
|
|
'label' => _m('Create collection'),
|
2021-12-26 09:48:16 +00:00
|
|
|
'attr' => [
|
2021-12-24 00:38:06 +00:00
|
|
|
'placeholder' => _m('Name'),
|
2021-12-26 09:48:16 +00:00
|
|
|
'required' => 'required',
|
2021-12-24 00:38:06 +00:00
|
|
|
],
|
|
|
|
'data' => '',
|
|
|
|
]],
|
|
|
|
['add_collection', SubmitType::class, [
|
|
|
|
'label' => _m('Create collection'),
|
|
|
|
'attr' => [
|
|
|
|
'title' => _m('Create collection'),
|
|
|
|
],
|
|
|
|
]],
|
|
|
|
]);
|
|
|
|
$create->handleRequest($request);
|
|
|
|
if ($create->isSubmitted() && $create->isValid()) {
|
|
|
|
DB::persist(Collection::create([
|
2021-12-26 09:48:16 +00:00
|
|
|
'name' => $create->getData()['name'],
|
2021-12-24 00:38:06 +00:00
|
|
|
'actor_id' => $id,
|
|
|
|
]));
|
|
|
|
DB::flush();
|
2021-12-27 18:25:20 +00:00
|
|
|
throw new RedirectException();
|
2021-12-24 00:38:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 14:24:40 +00:00
|
|
|
// We need to inject some functions in twig,
|
2021-12-28 04:39:09 +00:00
|
|
|
// but I don't want to create an environment for this
|
2021-12-25 14:24:40 +00:00
|
|
|
// as twig docs suggest in https://twig.symfony.com/doc/2.x/advanced.html#functions.
|
|
|
|
//
|
|
|
|
// Instead, I'm using an anonymous class to encapsulate
|
2021-12-28 04:39:09 +00:00
|
|
|
// the functions and passing that class to the template.
|
|
|
|
// This is suggested at https://stackoverflow.com/a/50364502.
|
2021-12-26 09:48:16 +00:00
|
|
|
$fn = new class($id, $nickname, $request) {
|
2021-12-24 00:38:06 +00:00
|
|
|
private $id;
|
|
|
|
private $nick;
|
|
|
|
private $request;
|
2021-12-28 04:39:09 +00:00
|
|
|
|
2021-12-24 00:38:06 +00:00
|
|
|
public function __construct($id, $nickname, $request)
|
|
|
|
{
|
2021-12-26 09:48:16 +00:00
|
|
|
$this->id = $id;
|
|
|
|
$this->nick = $nickname;
|
2021-12-24 00:38:06 +00:00
|
|
|
$this->request = $request;
|
|
|
|
}
|
2021-12-25 14:24:40 +00:00
|
|
|
// there's already a 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
|
2021-12-24 00:38:06 +00:00
|
|
|
public function getUrl($cid)
|
|
|
|
{
|
|
|
|
if (\is_null($this->nick)) {
|
|
|
|
return Router::url(
|
|
|
|
'collection_notes_view_by_actor_id',
|
2021-12-26 09:48:16 +00:00
|
|
|
['id' => $this->id, 'cid' => $cid],
|
2021-12-24 00:38:06 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return Router::url(
|
|
|
|
'collection_notes_view_by_nickname',
|
2021-12-26 09:48:16 +00:00
|
|
|
['nickname' => $this->nick, 'cid' => $cid],
|
2021-12-24 00:38:06 +00:00
|
|
|
);
|
|
|
|
}
|
2021-12-25 14:24:40 +00:00
|
|
|
// 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
|
2021-12-24 00:38:06 +00:00
|
|
|
public function editForm($collection)
|
|
|
|
{
|
|
|
|
$edit = Form::create([
|
|
|
|
['name', TextType::class, [
|
|
|
|
'attr' => [
|
|
|
|
'placeholder' => 'New name',
|
2021-12-26 09:48:16 +00:00
|
|
|
'required' => 'required',
|
2021-12-24 00:38:06 +00:00
|
|
|
],
|
|
|
|
'data' => '',
|
|
|
|
]],
|
2021-12-26 09:48:16 +00:00
|
|
|
['update_' . $collection->getId(), SubmitType::class, [
|
2021-12-24 00:38:06 +00:00
|
|
|
'label' => _m('Save'),
|
|
|
|
'attr' => [
|
|
|
|
'title' => _m('Save'),
|
|
|
|
],
|
|
|
|
]],
|
|
|
|
]);
|
|
|
|
$edit->handleRequest($this->request);
|
|
|
|
if ($edit->isSubmitted() && $edit->isValid()) {
|
|
|
|
$collection->setName($edit->getData()['name']);
|
|
|
|
DB::persist($collection);
|
|
|
|
DB::flush();
|
2021-12-27 18:25:20 +00:00
|
|
|
throw new RedirectException();
|
2021-12-24 00:38:06 +00:00
|
|
|
}
|
|
|
|
return $edit->createView();
|
|
|
|
}
|
2021-12-28 04:39:09 +00:00
|
|
|
|
2021-12-25 14:24:40 +00:00
|
|
|
// creating the remove form
|
2021-12-24 00:38:06 +00:00
|
|
|
public function rmForm($collection)
|
|
|
|
{
|
|
|
|
$rm = Form::create([
|
2021-12-26 09:48:16 +00:00
|
|
|
['remove_' . $collection->getId(), SubmitType::class, [
|
2021-12-24 00:38:06 +00:00
|
|
|
'label' => _m('Delete collection'),
|
|
|
|
'attr' => [
|
|
|
|
'title' => _m('Delete collection'),
|
|
|
|
'class' => 'danger',
|
|
|
|
],
|
|
|
|
]],
|
|
|
|
]);
|
|
|
|
$rm->handleRequest($this->request);
|
|
|
|
if ($rm->isSubmitted()) {
|
|
|
|
DB::remove($collection);
|
|
|
|
DB::flush();
|
2021-12-27 18:25:20 +00:00
|
|
|
throw new RedirectException();
|
2021-12-24 00:38:06 +00:00
|
|
|
}
|
|
|
|
return $rm->createView();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return [
|
|
|
|
'_template' => 'AttachmentCollections/collections.html.twig',
|
|
|
|
'page_title' => 'Attachment Collections list',
|
|
|
|
'add_collection' => $create?->createView(),
|
|
|
|
'fn' => $fn,
|
|
|
|
'collections' => $collections,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function collectionNotesByNickname(Request $request, string $nickname, int $cid): array
|
|
|
|
{
|
2021-12-28 04:39:09 +00:00
|
|
|
$user = DB::findOneBy(LocalUser::class, ['nickname' => $nickname]);
|
2021-12-24 00:38:06 +00:00
|
|
|
return self::collectionNotesByActorId($request, $user->getId(), $cid);
|
|
|
|
}
|
2021-12-28 04:39:09 +00:00
|
|
|
|
2021-12-24 00:38:06 +00:00
|
|
|
public function collectionNotesByActorId(Request $request, int $id, int $cid): array
|
|
|
|
{
|
2021-12-28 04:39:09 +00:00
|
|
|
$collection = DB::findOneBy(Collection::class, ['id' => $cid]);
|
|
|
|
[$attachs, $notes] = DB::dql(
|
|
|
|
<<<'EOF'
|
|
|
|
SELECT attach, notice FROM \Plugin\AttachmentCollections\Entity\AttachmentCollectionEntry AS entry
|
|
|
|
LEFT JOIN \Component\Attachment\Entity\Attachment AS attach
|
|
|
|
WITH entry.attachment_id = attach.id
|
|
|
|
LEFT JOIN \App\Entity\Note AS notice
|
|
|
|
WITH entry.note_id = notice.id
|
|
|
|
WHERE entry.collection_id = :cid
|
|
|
|
EOF,
|
2021-12-26 09:48:16 +00:00
|
|
|
['cid' => $cid],
|
2021-12-24 00:38:06 +00:00
|
|
|
);
|
|
|
|
return [
|
|
|
|
'_template' => 'AttachmentCollections/collection.html.twig',
|
|
|
|
'page_title' => $collection->getName(),
|
2021-12-28 04:39:09 +00:00
|
|
|
'attachments' => array_values($attachs),
|
|
|
|
'bare_notes' => array_values($notes),
|
2021-12-24 00:38:06 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|