2022-02-05 16:15:34 +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/>.
|
|
|
|
|
|
|
|
// }}}
|
|
|
|
|
2022-02-15 20:01:27 +00:00
|
|
|
namespace Plugin\Bundles;
|
2022-02-05 16:15:34 +00:00
|
|
|
|
|
|
|
use App\Core\DB\DB;
|
|
|
|
use App\Core\Modules\Plugin;
|
|
|
|
use App\Entity\Actor;
|
|
|
|
use Component\Collection\Util\MetaCollectionTrait;
|
2022-02-15 20:01:27 +00:00
|
|
|
use Plugin\Bundles\Entity\BundleCollection;
|
|
|
|
use Plugin\Bundles\Entity\BundleCollectionEntry;
|
2022-02-05 16:15:34 +00:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
|
2022-02-15 20:01:27 +00:00
|
|
|
class Bundles extends Plugin
|
2022-02-05 16:15:34 +00:00
|
|
|
{
|
|
|
|
use MetaCollectionTrait;
|
2022-02-26 14:09:41 +00:00
|
|
|
protected const SLUG = 'bundle';
|
|
|
|
protected const PLURAL_SLUG = 'bundles';
|
2022-02-05 16:15:34 +00:00
|
|
|
|
|
|
|
protected function createCollection(Actor $owner, array $vars, string $name)
|
|
|
|
{
|
2022-02-15 20:01:27 +00:00
|
|
|
$column = BundleCollection::create([
|
2022-02-05 16:15:34 +00:00
|
|
|
'name' => $name,
|
|
|
|
'actor_id' => $owner->getId(),
|
|
|
|
]);
|
|
|
|
DB::persist($column);
|
2022-02-15 20:01:27 +00:00
|
|
|
DB::persist(BundleCollectionEntry::create(args: [
|
2022-02-05 16:15:34 +00:00
|
|
|
'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)) {
|
2022-02-15 20:01:27 +00:00
|
|
|
DB::persist(BundleCollectionEntry::create(args: [
|
2022-02-05 16:15:34 +00:00
|
|
|
'note_id' => $vars['vars']['note_id'],
|
|
|
|
'blog_collection_id' => $id,
|
|
|
|
]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function shouldAddToRightPanel(Actor $user, $vars, Request $request): bool
|
|
|
|
{
|
|
|
|
// TODO: Implement shouldAddToRightPanel() method.
|
2022-02-06 21:37:04 +00:00
|
|
|
return false;
|
2022-02-05 16:15:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function getCollectionsBy(Actor $owner, ?array $vars = null, bool $ids_only = false): array
|
|
|
|
{
|
|
|
|
if (\is_null($vars)) {
|
2022-02-15 20:01:27 +00:00
|
|
|
$res = DB::findBy(BundleCollection::class, ['actor_id' => $owner->getId()]);
|
2022-02-05 16:15:34 +00:00
|
|
|
} 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);
|
|
|
|
}
|
|
|
|
}
|