123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- <?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\Bundles;
- use App\Core\DB;
- use App\Core\Modules\Plugin;
- use App\Entity\Actor;
- use Component\Collection\Util\MetaCollectionTrait;
- use Plugin\Bundles\Entity\BundleCollection;
- use Plugin\Bundles\Entity\BundleCollectionEntry;
- use Symfony\Component\HttpFoundation\Request;
- class Bundles extends Plugin
- {
- /**
- * @phpstan-use MetaCollectionTrait<BundleCollection>
- */
- use MetaCollectionTrait;
- protected const SLUG = 'bundle';
- protected const PLURAL_SLUG = 'bundles';
- /**
- * @param array<string, mixed> $vars
- */
- protected function createCollection(Actor $owner, array $vars, string $name): void
- {
- $column = BundleCollection::create([
- 'name' => $name,
- 'actor_id' => $owner->getId(),
- ]);
- DB::persist($column);
- DB::persist(BundleCollectionEntry::create(args: [
- 'note_id' => $vars['vars']['note_id'],
- 'blog_collection_id' => $column->getId(),
- ]));
- }
- /**
- * @param array<string, mixed> $vars
- * @param array<int> $items
- * @param array<mixed> $collections
- */
- protected function removeItem(Actor $owner, array $vars, array $items, array $collections): bool
- {
- 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,
- ]);
- }
- /**
- * @param array<string, mixed> $vars
- * @param array<int> $items
- * @param array<int> $collections
- */
- protected function addItem(Actor $owner, array $vars, array $items, array $collections): void
- {
- foreach ($items as $id) {
- // prevent user from putting something in a collection (s)he doesn't own:
- if (\in_array($id, $collections)) {
- DB::persist(BundleCollectionEntry::create(args: [
- 'note_id' => $vars['vars']['note_id'],
- 'blog_collection_id' => $id,
- ]));
- }
- }
- }
- /**
- * @param array<string, mixed> $vars
- */
- protected function shouldAddToRightPanel(Actor $user, array $vars, Request $request): bool
- {
- // TODO: Implement shouldAddToRightPanel() method.
- return false;
- }
- /**
- * FIXME incompatible return type
- *
- * @param null|array<string, mixed> $vars
- *
- * @return BundleCollection[]|int[]
- */
- protected function getCollectionsBy(Actor $owner, ?array $vars = null, bool $ids_only = false): array
- {
- if (\is_null($vars)) {
- $res = DB::findBy(BundleCollection::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);
- }
- }
|