AttachmentCollections.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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\AttachmentCollections\Controller;
  20. use App\Core\DB\DB;
  21. use App\Core\Router\Router;
  22. use Component\Collection\Util\Controller\MetaCollectionController;
  23. use Plugin\AttachmentCollections\Entity\AttachmentCollection;
  24. class AttachmentCollections extends MetaCollectionController
  25. {
  26. public function createCollection(int $owner_id, string $name)
  27. {
  28. DB::persist(AttachmentCollection::create([
  29. 'name' => $name,
  30. 'actor_id' => $owner_id,
  31. ]));
  32. }
  33. public function getCollectionUrl(int $owner_id, ?string $owner_nickname, int $collection_id): string
  34. {
  35. if (\is_null($owner_nickname)) {
  36. return Router::url(
  37. 'collection_notes_view_by_actor_id',
  38. ['id' => $owner_id, 'cid' => $collection_id],
  39. );
  40. }
  41. return Router::url(
  42. 'collection_notes_view_by_nickname',
  43. ['nickname' => $owner_nickname, 'cid' => $collection_id],
  44. );
  45. }
  46. public function getCollectionItems(int $owner_id, $collection_id): array
  47. {
  48. [$attachs, $notes] = DB::dql(
  49. <<<'EOF'
  50. SELECT attach, notice FROM \Plugin\AttachmentCollections\Entity\AttachmentCollectionEntry AS entry
  51. LEFT JOIN \Component\Attachment\Entity\Attachment AS attach
  52. WITH entry.attachment_id = attach.id
  53. LEFT JOIN \App\Entity\Note AS notice
  54. WITH entry.note_id = notice.id
  55. WHERE entry.attachment_collection_id = :cid
  56. EOF,
  57. ['cid' => $collection_id],
  58. );
  59. return [
  60. '_template' => 'AttachmentCollections/collection_entry_view.html.twig',
  61. 'attachments' => array_values($attachs),
  62. 'bare_notes' => array_values($notes),
  63. ];
  64. }
  65. public function getCollectionsByActorId(int $owner_id): array
  66. {
  67. return DB::findBy(AttachmentCollection::class, ['actor_id' => $owner_id], order_by: ['id' => 'desc']);
  68. }
  69. public function getCollectionBy(int $owner_id, int $collection_id): AttachmentCollection
  70. {
  71. return DB::findOneBy(AttachmentCollection::class, ['id' => $collection_id]);
  72. }
  73. public function setCollectionName(int $actor_id, string $actor_nickname, AttachmentCollection $collection, string $name)
  74. {
  75. $collection->setName($name);
  76. DB::persist($collection);
  77. }
  78. public function removeCollection(int $actor_id, string $actor_nickname, AttachmentCollection $collection)
  79. {
  80. DB::remove($collection);
  81. }
  82. }