NoteToLink.php 4.4 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\Link\Entity;
  20. use App\Core\DB\DB;
  21. use App\Core\Entity;
  22. use App\Core\Event;
  23. use DateTimeInterface;
  24. /**
  25. * Entity for relating a Link to a post
  26. *
  27. * @category DB
  28. * @package GNUsocial
  29. *
  30. * @author Diogo Peralta Cordeiro <mail@diogo.site>
  31. * @copyright 2021 Free Software Foundation, Inc http://www.fsf.org
  32. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  33. */
  34. class NoteToLink extends Entity
  35. {
  36. // {{{ Autocode
  37. // @codeCoverageIgnoreStart
  38. private int $link_id;
  39. private int $note_id;
  40. private DateTimeInterface $modified;
  41. public function setLinkId(int $link_id): self
  42. {
  43. $this->link_id = $link_id;
  44. return $this;
  45. }
  46. public function getLinkId(): int
  47. {
  48. return $this->link_id;
  49. }
  50. public function setNoteId(int $note_id): self
  51. {
  52. $this->note_id = $note_id;
  53. return $this;
  54. }
  55. public function getNoteId(): int
  56. {
  57. return $this->note_id;
  58. }
  59. public function setModified(DateTimeInterface $modified): self
  60. {
  61. $this->modified = $modified;
  62. return $this;
  63. }
  64. public function getModified(): DateTimeInterface
  65. {
  66. return $this->modified;
  67. }
  68. // @codeCoverageIgnoreEnd
  69. // }}} Autocode
  70. /**
  71. * Create an instance of NoteToLink or fill in the
  72. * properties of $obj with the associative array $args. Doesn't
  73. * persist the result
  74. *
  75. * @param null|mixed $obj
  76. */
  77. public static function create(array $args, $obj = null)
  78. {
  79. $link = DB::find('link', ['id' => $args['link_id']]);
  80. $note = DB::find('note', ['id' => $args['note_id']]);
  81. Event::handle('NewLinkFromNote', [$link, $note]);
  82. $obj = new self();
  83. return parent::create($args, $obj);
  84. }
  85. public static function removeWhereNoteId(int $note_id): mixed
  86. {
  87. return DB::dql(
  88. <<<'EOF'
  89. DELETE FROM note_to_link ntl
  90. WHERE ntl.note_id = :note_id
  91. EOF,
  92. ['note_id' => $note_id],
  93. );
  94. }
  95. public static function removeWhere(int $link_id, int $note_id): mixed
  96. {
  97. return DB::dql(
  98. <<<'EOF'
  99. DELETE FROM note_to_link ntl
  100. WHERE (ntl.link_id = :link_id
  101. OR ntl.note_id = :note_id)
  102. EOF,
  103. ['link_id' => $link_id, 'note_id' => $note_id],
  104. );
  105. }
  106. public static function removeWhereLinkId(int $link_id): mixed
  107. {
  108. return DB::dql(
  109. <<<'EOF'
  110. DELETE FROM note_to_link ntl
  111. WHERE ntl.link_id = :link_id
  112. EOF,
  113. ['link_id' => $link_id],
  114. );
  115. }
  116. public static function schemaDef(): array
  117. {
  118. return [
  119. 'name' => 'note_to_link',
  120. 'fields' => [
  121. 'link_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'link.id', 'multiplicity' => 'one to one', 'name' => 'note_to_link_link_id_fkey', 'not null' => true, 'description' => 'id of link'],
  122. 'note_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'Note.id', 'multiplicity' => 'one to one', 'name' => 'note_to_link_note_id_fkey', 'not null' => true, 'description' => 'id of the note it belongs to'],
  123. 'modified' => ['type' => 'timestamp', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'],
  124. ],
  125. 'primary key' => ['link_id', 'note_id'],
  126. 'indexes' => [
  127. 'link_id_idx' => ['link_id'],
  128. 'note_id_idx' => ['note_id'],
  129. ],
  130. ];
  131. }
  132. }