AttachmentToLink.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 remote url to an attachment
  25. *
  26. * @category DB
  27. * @package GNUsocial
  28. *
  29. * @author Diogo Peralta Cordeiro <mail@diogo.site>
  30. * @copyright 2021 Free Software Foundation, Inc http://www.fsf.org
  31. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  32. */
  33. class AttachmentToLink extends Entity
  34. {
  35. // {{{ Autocode
  36. // @codeCoverageIgnoreStart
  37. private int $link_id;
  38. private int $attachment_id;
  39. private DateTimeInterface $modified;
  40. public function setLinkId(int $link_id): self
  41. {
  42. $this->link_id = $link_id;
  43. return $this;
  44. }
  45. public function getLinkId(): int
  46. {
  47. return $this->link_id;
  48. }
  49. public function setAttachmentId(int $attachment_id): self
  50. {
  51. $this->attachment_id = $attachment_id;
  52. return $this;
  53. }
  54. public function getAttachmentId(): int
  55. {
  56. return $this->attachment_id;
  57. }
  58. public function setModified(DateTimeInterface $modified): self
  59. {
  60. $this->modified = $modified;
  61. return $this;
  62. }
  63. public function getModified(): DateTimeInterface
  64. {
  65. return $this->modified;
  66. }
  67. // @codeCoverageIgnoreEnd
  68. // }}} Autocode
  69. public static function removeWhereAttachmentId(int $attachment_id): mixed
  70. {
  71. return DB::dql(
  72. <<<'EOF'
  73. DELETE FROM attachment_to_link atl
  74. WHERE atl.attachment_id = :attachment_id
  75. EOF,
  76. ['attachment_id' => $attachment_id],
  77. );
  78. }
  79. public static function removeWhere(int $link_id, int $attachment_id): mixed
  80. {
  81. return DB::dql(
  82. <<<'EOF'
  83. DELETE FROM attachment_to_link atl
  84. WHERE (atl.link_id = :link_id
  85. OR atl.attachment_id = :attachment_id)
  86. EOF,
  87. ['link_id' => $link_id, 'attachment_id' => $attachment_id],
  88. );
  89. }
  90. public static function removeWhereLinkId(int $link_id): mixed
  91. {
  92. return DB::dql(
  93. <<<'EOF'
  94. DELETE FROM attachment_to_link atl
  95. WHERE atl.link_id = :link_id
  96. EOF,
  97. ['link_id' => $link_id],
  98. );
  99. }
  100. public static function schemaDef(): array
  101. {
  102. return [
  103. 'name' => 'attachment_to_link',
  104. 'fields' => [
  105. 'link_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'Link.id', 'multiplicity' => 'one to one', 'name' => 'attachment_to_note_note_id_fkey', 'not null' => true, 'description' => 'id of the note it belongs to'],
  106. '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'],
  107. 'modified' => ['type' => 'timestamp', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'],
  108. ],
  109. 'primary key' => ['link_id'],
  110. 'indexes' => [
  111. 'link_id_idx' => ['link_id'],
  112. 'attachment_id_idx' => ['attachment_id'],
  113. ],
  114. ];
  115. }
  116. }