[COMPONENT][Collection] Make MetaCollectionPlugin a trait and abstract collection delete and name update

This commit is contained in:
Diogo Peralta Cordeiro 2022-01-04 21:58:49 +00:00
parent 754135743e
commit 9df9c6a19c
No known key found for this signature in database
GPG Key ID: 18D2D35001FBFAB0
5 changed files with 28 additions and 15 deletions

View File

@ -11,10 +11,10 @@ use Component\Feed\Feed;
class Collection extends Controller class Collection extends Controller
{ {
public function query(string $query, ?string $language = null, ?Actor $actor = null) public function query(string $query, ?string $locale = null, ?Actor $actor = null)
{ {
$actor ??= Common::actor(); $actor ??= Common::actor();
$language ??= $actor->getTopLanguage()->getLocale(); $locale ??= Common::currentLanguage()->getLocale();
return Feed::query($query, $this->int('page') ?? 1, $language, $actor); return Feed::query($query, $this->int('page') ?? 1, $locale, $actor);
} }
} }

View File

@ -77,7 +77,7 @@ abstract class MetaCollectionController extends FeedController
$collections = $this->getCollectionsByActorId($id); $collections = $this->getCollectionsByActorId($id);
$create_title = _m('Create a ' . mb_strtolower(preg_replace('/([a-z0-9])([A-Z])/', '$1 $2', $this->slug))); $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))); $collections_title = _m('The ' . 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) {
@ -127,7 +127,7 @@ abstract class MetaCollectionController extends FeedController
$this->parent = $parent; $this->parent = $parent;
$this->slug = $slug; $this->slug = $slug;
} }
// there's already a injected function called path, // there's already an injected function called path,
// that maps to Router::url(name, args), but since // that maps to Router::url(name, args), but since
// I want to preserve nicknames, I think it's better // I want to preserve nicknames, I think it's better
// to use that getUrl function // to use that getUrl function
@ -159,8 +159,7 @@ abstract class MetaCollectionController extends FeedController
]); ]);
$edit->handleRequest($this->request); $edit->handleRequest($this->request);
if ($edit->isSubmitted() && $edit->isValid()) { if ($edit->isSubmitted() && $edit->isValid()) {
$collection->setName($edit->getData()['name']); $this->parent->setCollectionName($this->id, $this->nick, $collection, $edit->getData()['name']);
DB::persist($collection);
DB::flush(); DB::flush();
throw new RedirectException(); throw new RedirectException();
} }
@ -181,7 +180,7 @@ abstract class MetaCollectionController extends FeedController
]); ]);
$rm->handleRequest($this->request); $rm->handleRequest($this->request);
if ($rm->isSubmitted()) { if ($rm->isSubmitted()) {
DB::remove($collection); $this->parent->removeCollection($this->id, $this->nick, $collection);
DB::flush(); DB::flush();
throw new RedirectException(); throw new RedirectException();
} }

View File

@ -35,7 +35,6 @@ use App\Core\DB\DB;
use App\Core\Event; use App\Core\Event;
use App\Core\Form; use App\Core\Form;
use function App\Core\I18n\_m; use function App\Core\I18n\_m;
use App\Core\Modules\Plugin;
use App\Entity\Actor; use App\Entity\Actor;
use App\Util\Common; use App\Util\Common;
use App\Util\Exception\RedirectException; use App\Util\Exception\RedirectException;
@ -45,10 +44,10 @@ 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;
abstract class MetaCollectionPlugin extends Plugin trait MetaCollectionTrait
{ {
protected string $slug = 'collection'; //protected string $slug = 'collection';
protected string $plural_slug = 'collections'; //protected string $plural_slug = 'collections';
abstract protected function createCollection(Actor $owner, array $vars, string $name); abstract protected function createCollection(Actor $owner, array $vars, string $name);
abstract protected function removeItems(Actor $owner, array $vars, $items, array $collections); abstract protected function removeItems(Actor $owner, array $vars, $items, array $collections);

View File

@ -34,20 +34,24 @@ 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 function App\Core\I18n\_m;
use App\Core\Modules\Plugin;
use App\Core\Router\RouteLoader; use App\Core\Router\RouteLoader;
use App\Core\Router\Router; use App\Core\Router\Router;
use App\Entity\Actor; 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 Component\Collection\Util\MetaCollectionPlugin; use Component\Collection\Util\MetaCollectionTrait;
use Plugin\AttachmentCollections\Controller\AttachmentCollections as AttachmentCollectionsController; 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;
class AttachmentCollections extends MetaCollectionPlugin class AttachmentCollections extends Plugin
{ {
use MetaCollectionTrait;
protected string $slug = 'collection';
protected string $plural_slug = 'collections';
protected function createCollection(Actor $owner, array $vars, string $name) protected function createCollection(Actor $owner, array $vars, string $name)
{ {
$col = AttachmentCollection::create([ $col = AttachmentCollection::create([

View File

@ -77,4 +77,15 @@ class AttachmentCollections extends MetaCollectionController
{ {
return DB::findOneBy(AttachmentCollection::class, ['id' => $collection_id]); return DB::findOneBy(AttachmentCollection::class, ['id' => $collection_id]);
} }
public function setCollectionName(int $actor_id, string $actor_nickname, AttachmentCollection $collection, string $name)
{
$collection->setName($name);
DB::persist($collection);
}
public function removeCollection(int $actor_id, string $actor_nickname, AttachmentCollection $collection)
{
DB::remove($collection);
}
} }