AttachmentToNote.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 Component\Attachment\Entity;
  20. use App\Core\DB\DB;
  21. use App\Core\Entity;
  22. use DateTimeInterface;
  23. /**
  24. * Entity for relating a attachment to a post
  25. *
  26. * @category DB
  27. * @package GNUsocial
  28. *
  29. * @author Zach Copley <zach@status.net>
  30. * @copyright 2010 StatusNet Inc.
  31. * @author Mikael Nordfeldth <mmn@hethane.se>
  32. * @copyright 2009-2014 Free Software Foundation, Inc http://www.fsf.org
  33. * @author Hugo Sales <hugo@hsal.es>
  34. * @copyright 2020-2021 Free Software Foundation, Inc http://www.fsf.org
  35. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  36. */
  37. class AttachmentToNote extends Entity
  38. {
  39. // {{{ Autocode
  40. // @codeCoverageIgnoreStart
  41. private int $attachment_id;
  42. private int $note_id;
  43. private ?string $title = null;
  44. private DateTimeInterface $modified;
  45. public function setAttachmentId(int $attachment_id): self
  46. {
  47. $this->attachment_id = $attachment_id;
  48. return $this;
  49. }
  50. public function getAttachmentId(): int
  51. {
  52. return $this->attachment_id;
  53. }
  54. public function setNoteId(int $note_id): self
  55. {
  56. $this->note_id = $note_id;
  57. return $this;
  58. }
  59. public function getNoteId(): int
  60. {
  61. return $this->note_id;
  62. }
  63. public function setTitle(?string $title): self
  64. {
  65. $this->title = $title;
  66. return $this;
  67. }
  68. public function getTitle(): ?string
  69. {
  70. return $this->title;
  71. }
  72. public function setModified(DateTimeInterface $modified): self
  73. {
  74. $this->modified = $modified;
  75. return $this;
  76. }
  77. public function getModified(): DateTimeInterface
  78. {
  79. return $this->modified;
  80. }
  81. // @codeCoverageIgnoreEnd
  82. // }}} Autocode
  83. public static function removeWhere(int $note_id, int $attachment_id): mixed
  84. {
  85. return DB::dql(
  86. <<<'EOF'
  87. DELETE FROM attachment_to_note atn
  88. WHERE (atn.attachment_id = :attachment_id
  89. OR atn.note_id = :note_id)
  90. EOF,
  91. ['note_id' => $note_id, 'attachment_id' => $attachment_id],
  92. );
  93. }
  94. public static function removeWhereNoteId(int $note_id): mixed
  95. {
  96. return DB::dql(
  97. <<<'EOF'
  98. DELETE FROM attachment_to_note atn
  99. WHERE atn.note_id = :note_id
  100. EOF,
  101. ['note_id' => $note_id],
  102. );
  103. }
  104. public static function removeWhereAttachmentId(int $attachment_id): mixed
  105. {
  106. return DB::dql(
  107. <<<'EOF'
  108. DELETE FROM attachment_to_note atn
  109. WHERE atn.attachment_id = :attachment_id
  110. EOF,
  111. ['attachment_id' => $attachment_id],
  112. );
  113. }
  114. public static function schemaDef(): array
  115. {
  116. return [
  117. 'name' => 'attachment_to_note',
  118. 'fields' => [
  119. 'attachment_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'Attachment.id', 'multiplicity' => 'one to one', 'name' => 'attachment_to_note_attachment_id_fkey', 'not null' => true, 'description' => 'id of attachment'],
  120. 'note_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'Note.id', 'multiplicity' => 'one to one', 'name' => 'attachment_to_note_note_id_fkey', 'not null' => true, 'description' => 'id of the note it belongs to'],
  121. 'title' => ['type' => 'text', 'description' => 'title of resource when available'],
  122. 'modified' => ['type' => 'timestamp', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'],
  123. ],
  124. 'primary key' => ['attachment_id', 'note_id'],
  125. 'indexes' => [
  126. 'attachment_id_idx' => ['attachment_id'],
  127. 'note_id_idx' => ['note_id'],
  128. ],
  129. ];
  130. }
  131. }