Cover.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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\Cover\Entity;
  20. use App\Core\DB\DB;
  21. use App\Core\Entity;
  22. use App\Util\Common;
  23. use Component\Attachment\Entity\Attachment;
  24. use DateTimeInterface;
  25. /**
  26. * For storing a cover
  27. *
  28. * @package GNUsocial
  29. * @category CoverPlugin
  30. *
  31. * @author Daniel Brandao <up201705812@fe.up.pt>
  32. * @copyright 2020 Free Software Foundation, Inc http://www.fsf.org
  33. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  34. */
  35. class Cover extends Entity
  36. {
  37. // {{{ Autocode
  38. // @codeCoverageIgnoreStart
  39. private int $actor_id;
  40. private int $attachment_id;
  41. private DateTimeInterface $created;
  42. private DateTimeInterface $modified;
  43. public function setActorId(int $actor_id): self
  44. {
  45. $this->actor_id = $actor_id;
  46. return $this;
  47. }
  48. public function getActorId(): int
  49. {
  50. return $this->actor_id;
  51. }
  52. public function setAttachmentId(int $attachment_id): self
  53. {
  54. $this->attachment_id = $attachment_id;
  55. return $this;
  56. }
  57. public function getAttachmentId(): int
  58. {
  59. return $this->attachment_id;
  60. }
  61. public function setCreated(DateTimeInterface $created): self
  62. {
  63. $this->created = $created;
  64. return $this;
  65. }
  66. public function getCreated(): DateTimeInterface
  67. {
  68. return $this->created;
  69. }
  70. public function setModified(DateTimeInterface $modified): self
  71. {
  72. $this->modified = $modified;
  73. return $this;
  74. }
  75. public function getModified(): DateTimeInterface
  76. {
  77. return $this->modified;
  78. }
  79. // @codeCoverageIgnoreEnd
  80. // }}} Autocode
  81. private ?Attachment $attachment = null;
  82. /**
  83. * get cover attachment
  84. */
  85. public function getAttachment(): Attachment
  86. {
  87. $this->attachment = $this->attachment ?: DB::find('attachment', ['id' => $this->attachment_id]);
  88. return $this->attachment;
  89. }
  90. /**
  91. * get cover attachment path
  92. */
  93. public function getAttachmentPath(): string
  94. {
  95. return Common::config('cover', 'dir') . $this->getAttachment()->getBestTitle();
  96. }
  97. /**
  98. * Delete this cover and the corresponding attachment and thumbnails, which this owns
  99. *
  100. * @return array attachments deleted (if delete_attachments_now is true)
  101. */
  102. public function delete(bool $flush = false, bool $delete_attachments_now = false, bool $cascading = false): array
  103. {
  104. // Don't go into a loop if we're deleting from Attachment
  105. if (!$cascading) {
  106. $attachments = $this->getAttachment()->kill();
  107. } else {
  108. DB::remove(DB::getReference('cover', ['actor_id' => $this->actor_id]));
  109. $attachment_path = $this->getAttachmentPath();
  110. $attachments[] = $attachment_path;
  111. if ($flush) {
  112. DB::flush();
  113. }
  114. return $delete_attachments_now ? [] : $attachments;
  115. }
  116. return [];
  117. }
  118. public static function schemaDef(): array
  119. {
  120. return [
  121. 'name' => 'cover',
  122. 'fields' => [
  123. 'actor_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'Actor.id', 'multiplicity' => 'one to one', 'not null' => true, 'description' => 'foreign key to actor table'],
  124. 'attachment_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'Attachment.id', 'multiplicity' => 'one to one', 'not null' => true, 'description' => 'foreign key to attachment table'],
  125. 'created' => ['type' => 'datetime', 'not null' => true, 'description' => 'date this record was created', 'default' => 'CURRENT_TIMESTAMP'],
  126. 'modified' => ['type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified', 'default' => 'CURRENT_TIMESTAMP'],
  127. ],
  128. 'primary key' => ['actor_id'],
  129. 'indexes' => [
  130. 'cover_attachment_id_idx' => ['attachment_id'],
  131. ],
  132. ];
  133. }
  134. }