Cover.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. // {{{ License
  3. // This file is part of GNU social - https://www.gnu.org/software/social
  4. //
  5. // GNU social is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // GNU social is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  17. // }}}
  18. namespace Plugin\Cover\Entity;
  19. use App\Core\DB\DB;
  20. use App\Core\Entity;
  21. use App\Util\Common;
  22. use DateTimeInterface;
  23. /**
  24. * For storing a cover
  25. *
  26. * @package GNUsocial
  27. * @category CoverPlugin
  28. *
  29. * @author Daniel Brandao <up201705812@fe.up.pt>
  30. * @copyright 2020 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 Cover extends Entity
  34. {
  35. // {{{ Autocode
  36. // @codeCoverageIgnoreStart
  37. private int $gsactor_id;
  38. private int $attachment_id;
  39. private \DateTimeInterface $created;
  40. private \DateTimeInterface $modified;
  41. public function setGSActorId(int $gsactor_id): self
  42. {
  43. $this->gsactor_id = $gsactor_id;
  44. return $this;
  45. }
  46. public function getGSActorId(): int
  47. {
  48. return $this->gsactor_id;
  49. }
  50. public function setAttachmentId(int $attachment_id): self
  51. {
  52. $this->attachment_id = $attachment_id;
  53. return $this;
  54. }
  55. public function getAttachmentId(): int
  56. {
  57. return $this->attachment_id;
  58. }
  59. public function setCreated(DateTimeInterface $created): self
  60. {
  61. $this->created = $created;
  62. return $this;
  63. }
  64. public function getCreated(): DateTimeInterface
  65. {
  66. return $this->created;
  67. }
  68. public function setModified(DateTimeInterface $modified): self
  69. {
  70. $this->modified = $modified;
  71. return $this;
  72. }
  73. public function getModified(): DateTimeInterface
  74. {
  75. return $this->modified;
  76. }
  77. // @codeCoverageIgnoreEnd
  78. // }}} Autocode
  79. private ?Attachment $attachment = null;
  80. /**
  81. * get cover attachment
  82. *
  83. * @return 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. * @return string
  94. */
  95. public function getAttachmentPath(): string
  96. {
  97. return Common::config('cover', 'dir') . $this->getAttachment()->getAttachmentName();
  98. }
  99. /**
  100. * Delete this cover and the corresponding attachment and thumbnails, which this owns
  101. *
  102. * @param bool $flush
  103. * @param bool $delete_attachments_now
  104. * @param bool $cascading
  105. *
  106. * @return array attachments deleted (if delete_attachments_now is true)
  107. */
  108. public function delete(bool $flush = false, bool $delete_attachments_now = false, bool $cascading = false): array
  109. {
  110. // Don't go into a loop if we're deleting from Attachment
  111. if (!$cascading) {
  112. $attachments = $this->getAttachment()->delete($cascade = true, $attachment_flush = false, $delete_attachments_now);
  113. } else {
  114. DB::remove(DB::getReference('cover', ['gsactor_id' => $this->gsactor_id]));
  115. $attachment_path = $this->getAttachmentPath();
  116. $attachments[] = $attachment_path;
  117. if ($flush) {
  118. DB::flush();
  119. }
  120. return $delete_attachments_now ? [] : $attachments;
  121. }
  122. return [];
  123. }
  124. public static function schemaDef(): array
  125. {
  126. return [
  127. 'name' => 'cover',
  128. 'fields' => [
  129. 'gsactor_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'GSActor.id', 'multiplicity' => 'one to one', 'not null' => true, 'description' => 'foreign key to gsactor table'],
  130. 'attachment_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'Attachment.id', 'multiplicity' => 'one to one', 'not null' => true, 'description' => 'foreign key to attachment table'],
  131. 'created' => ['type' => 'datetime', 'not null' => true, 'description' => 'date this record was created', 'default' => 'CURRENT_TIMESTAMP'],
  132. 'modified' => ['type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified', 'default' => 'CURRENT_TIMESTAMP'],
  133. ],
  134. 'primary key' => ['gsactor_id'],
  135. 'indexes' => [
  136. 'cover_attachment_id_idx' => ['attachment_id'],
  137. ],
  138. ];
  139. }
  140. }