Bundles.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. declare(strict_types = 1);
  3. // {{{ License
  4. // This file is part of GNU social - https://www.gnu.org/software/social
  5. //
  6. // GNU social is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // GNU social is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  18. // }}}
  19. namespace Plugin\Bundles;
  20. use App\Core\DB\DB;
  21. use App\Core\Modules\Plugin;
  22. use App\Entity\Actor;
  23. use Component\Collection\Util\MetaCollectionTrait;
  24. use Plugin\Bundles\Entity\BundleCollection;
  25. use Plugin\Bundles\Entity\BundleCollectionEntry;
  26. use Symfony\Component\HttpFoundation\Request;
  27. class Bundles extends Plugin
  28. {
  29. use MetaCollectionTrait;
  30. protected const SLUG = 'bundle';
  31. protected const PLURAL_SLUG = 'bundles';
  32. protected function createCollection(Actor $owner, array $vars, string $name)
  33. {
  34. $column = BundleCollection::create([
  35. 'name' => $name,
  36. 'actor_id' => $owner->getId(),
  37. ]);
  38. DB::persist($column);
  39. DB::persist(BundleCollectionEntry::create(args: [
  40. 'note_id' => $vars['vars']['note_id'],
  41. 'blog_collection_id' => $column->getId(),
  42. ]));
  43. }
  44. protected function removeItem(Actor $owner, array $vars, array $items, array $collections)
  45. {
  46. return DB::dql(<<<'EOF'
  47. DELETE FROM \Plugin\BlogCollections\Entity\BlogCollectionEntry AS entry
  48. WHERE entry.note_id = :note_id
  49. AND entry.blog_collection_id IN (
  50. SELECT blog.id FROM \Plugin\BlogCollections\Entity\BlogCollection AS blog
  51. WHERE blog.actor_id = :user_id
  52. AND blog.id IN (:ids)
  53. )
  54. EOF, [
  55. 'note_id' => $vars['vars']['note_id'],
  56. 'user_id' => $owner->getId(),
  57. 'ids' => $items,
  58. ]);
  59. }
  60. protected function addItem(Actor $owner, array $vars, array $items, array $collections)
  61. {
  62. foreach ($items as $id) {
  63. // prevent user from putting something in a collection (s)he doesn't own:
  64. if (\in_array($id, $collections)) {
  65. DB::persist(BundleCollectionEntry::create(args: [
  66. 'note_id' => $vars['vars']['note_id'],
  67. 'blog_collection_id' => $id,
  68. ]));
  69. }
  70. }
  71. }
  72. protected function shouldAddToRightPanel(Actor $user, $vars, Request $request): bool
  73. {
  74. // TODO: Implement shouldAddToRightPanel() method.
  75. return false;
  76. }
  77. protected function getCollectionsBy(Actor $owner, ?array $vars = null, bool $ids_only = false): array
  78. {
  79. if (\is_null($vars)) {
  80. $res = DB::findBy(BundleCollection::class, ['actor_id' => $owner->getId()]);
  81. } else {
  82. $res = DB::dql(
  83. <<<'EOF'
  84. SELECT entry.blog_collection_id FROM \Plugin\BlogCollections\Entity\BlogCollectionEntry AS entry
  85. INNER JOIN \Plugin\BlogCollections\Entity\BlogCollection AS blog_collection
  86. WITH blog_collection.id = entry.attachment_collection_id
  87. WHERE entry.note_id = :note_id AND blog_collection.actor_id = :id
  88. EOF,
  89. [
  90. 'id' => $owner->getId(),
  91. 'note_id' => $vars['vars']['note_id'],
  92. ],
  93. );
  94. }
  95. if (!$ids_only) {
  96. return $res;
  97. }
  98. return array_map(fn ($x) => $x['blog_collection_id'], $res);
  99. }
  100. }