Cover.php 4.9 KB

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