Bundles.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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;
  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. /**
  30. * @phpstan-use MetaCollectionTrait<BundleCollection>
  31. */
  32. use MetaCollectionTrait;
  33. protected const SLUG = 'bundle';
  34. protected const PLURAL_SLUG = 'bundles';
  35. /**
  36. * @param array<string, mixed> $vars
  37. */
  38. protected function createCollection(Actor $owner, array $vars, string $name): void
  39. {
  40. $column = BundleCollection::create([
  41. 'name' => $name,
  42. 'actor_id' => $owner->getId(),
  43. ]);
  44. DB::persist($column);
  45. DB::persist(BundleCollectionEntry::create(args: [
  46. 'note_id' => $vars['vars']['note_id'],
  47. 'blog_collection_id' => $column->getId(),
  48. ]));
  49. }
  50. /**
  51. * @param array<string, mixed> $vars
  52. * @param array<int> $items
  53. * @param array<mixed> $collections
  54. */
  55. protected function removeItem(Actor $owner, array $vars, array $items, array $collections): bool
  56. {
  57. return DB::dql(<<<'EOF'
  58. DELETE FROM \Plugin\BlogCollections\Entity\BlogCollectionEntry AS entry
  59. WHERE entry.note_id = :note_id
  60. AND entry.blog_collection_id IN (
  61. SELECT blog.id FROM \Plugin\BlogCollections\Entity\BlogCollection AS blog
  62. WHERE blog.actor_id = :user_id
  63. AND blog.id IN (:ids)
  64. )
  65. EOF, [
  66. 'note_id' => $vars['vars']['note_id'],
  67. 'user_id' => $owner->getId(),
  68. 'ids' => $items,
  69. ]);
  70. }
  71. /**
  72. * @param array<string, mixed> $vars
  73. * @param array<int> $items
  74. * @param array<int> $collections
  75. */
  76. protected function addItem(Actor $owner, array $vars, array $items, array $collections): void
  77. {
  78. foreach ($items as $id) {
  79. // prevent user from putting something in a collection (s)he doesn't own:
  80. if (\in_array($id, $collections)) {
  81. DB::persist(BundleCollectionEntry::create(args: [
  82. 'note_id' => $vars['vars']['note_id'],
  83. 'blog_collection_id' => $id,
  84. ]));
  85. }
  86. }
  87. }
  88. /**
  89. * @param array<string, mixed> $vars
  90. */
  91. protected function shouldAddToRightPanel(Actor $user, array $vars, Request $request): bool
  92. {
  93. // TODO: Implement shouldAddToRightPanel() method.
  94. return false;
  95. }
  96. /**
  97. * FIXME incompatible return type
  98. *
  99. * @param null|array<string, mixed> $vars
  100. *
  101. * @return BundleCollection[]|int[]
  102. */
  103. protected function getCollectionsBy(Actor $owner, ?array $vars = null, bool $ids_only = false): array
  104. {
  105. if (\is_null($vars)) {
  106. $res = DB::findBy(BundleCollection::class, ['actor_id' => $owner->getId()]);
  107. } else {
  108. $res = DB::dql(
  109. <<<'EOF'
  110. SELECT entry.blog_collection_id FROM \Plugin\BlogCollections\Entity\BlogCollectionEntry AS entry
  111. INNER JOIN \Plugin\BlogCollections\Entity\BlogCollection AS blog_collection
  112. WITH blog_collection.id = entry.attachment_collection_id
  113. WHERE entry.note_id = :note_id AND blog_collection.actor_id = :id
  114. EOF,
  115. [
  116. 'id' => $owner->getId(),
  117. 'note_id' => $vars['vars']['note_id'],
  118. ],
  119. );
  120. }
  121. if (!$ids_only) {
  122. return $res;
  123. }
  124. return array_map(fn ($x) => $x['blog_collection_id'], $res);
  125. }
  126. }