. // }}} namespace Plugin\BlogCollections; use App\Core\DB\DB; use App\Core\Modules\Plugin; use App\Entity\Actor; use Component\Collection\Util\MetaCollectionTrait; use Plugin\BlogCollections\Entity\BlogCollection; use Plugin\BlogCollections\Entity\BlogCollectionEntry; use Symfony\Component\HttpFoundation\Request; class BlogCollections extends Plugin { use MetaCollectionTrait; protected function createCollection(Actor $owner, array $vars, string $name) { $column = BlogCollection::create([ 'name' => $name, 'actor_id' => $owner->getId(), ]); DB::persist($column); DB::persist(BlogCollectionEntry::create(args: [ 'note_id' => $vars['vars']['note_id'], 'blog_collection_id' => $column->getId(), ])); } protected function removeItem(Actor $owner, array $vars, array $items, array $collections) { return DB::dql(<<<'EOF' DELETE FROM \Plugin\BlogCollections\Entity\BlogCollectionEntry AS entry WHERE entry.note_id = :note_id AND entry.blog_collection_id IN ( SELECT blog.id FROM \Plugin\BlogCollections\Entity\BlogCollection AS blog WHERE blog.actor_id = :user_id AND blog.id IN (:ids) ) EOF, [ 'note_id' => $vars['vars']['note_id'], 'user_id' => $owner->getId(), 'ids' => $items, ]); } protected function addItem(Actor $owner, array $vars, array $items, array $collections) { foreach ($items as $id) { // prevent user from putting something in a collection (s)he doesn't own: if (\in_array($id, $collections)) { DB::persist(BlogCollectionEntry::create(args: [ 'note_id' => $vars['vars']['note_id'], 'blog_collection_id' => $id, ])); } } } protected function shouldAddToRightPanel(Actor $user, $vars, Request $request): bool { // TODO: Implement shouldAddToRightPanel() method. return false; } protected function getCollectionsBy(Actor $owner, ?array $vars = null, bool $ids_only = false): array { if (\is_null($vars)) { $res = DB::findBy(BlogCollection::class, ['actor_id' => $owner->getId()]); } else { $res = DB::dql( <<<'EOF' SELECT entry.blog_collection_id FROM \Plugin\BlogCollections\Entity\BlogCollectionEntry AS entry INNER JOIN \Plugin\BlogCollections\Entity\BlogCollection AS blog_collection WITH blog_collection.id = entry.attachment_collection_id WHERE entry.note_id = :note_id AND blog_collection.actor_id = :id EOF, [ 'id' => $owner->getId(), 'note_id' => $vars['vars']['note_id'], ], ); } if (!$ids_only) { return $res; } return array_map(fn ($x) => $x['blog_collection_id'], $res); } }