2020-11-25 21:43:55 +00:00
< ? php
2021-10-10 09:26:18 +01:00
declare ( strict_types = 1 );
2020-11-25 21:43:55 +00:00
// {{{ License
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
// }}}
2021-08-16 17:17:05 +01:00
namespace Plugin\Cover\Entity ;
2020-11-25 21:43:55 +00:00
2022-03-27 15:19:09 +01:00
use App\Core\DB ;
2020-11-25 21:43:55 +00:00
use App\Core\Entity ;
2020-11-27 22:25:37 +00:00
use App\Util\Common ;
2021-12-26 09:48:16 +00:00
use Component\Attachment\Entity\Attachment ;
2020-11-25 21:43:55 +00:00
use DateTimeInterface ;
2020-11-29 16:54:29 +00:00
/**
* For storing a cover
*
* @ package GNUsocial
* @ category CoverPlugin
*
* @ author Daniel Brandao < up201705812 @ fe . up . pt >
* @ copyright 2020 Free Software Foundation , Inc http :// www . fsf . org
* @ license https :// www . gnu . org / licenses / agpl . html GNU AGPL v3 or later
*/
2020-11-25 21:43:55 +00:00
class Cover extends Entity
{
// {{{ Autocode
2021-05-05 17:03:03 +01:00
// @codeCoverageIgnoreStart
2021-09-18 03:22:27 +01:00
private int $actor_id ;
2021-05-01 13:48:08 +01:00
private int $attachment_id ;
2021-10-10 09:26:18 +01:00
private DateTimeInterface $created ;
private DateTimeInterface $modified ;
2020-11-25 21:43:55 +00:00
2021-09-18 03:22:27 +01:00
public function setActorId ( int $actor_id ) : self
2020-11-25 21:43:55 +00:00
{
2021-09-18 03:22:27 +01:00
$this -> actor_id = $actor_id ;
2020-11-25 21:43:55 +00:00
return $this ;
}
2021-09-18 03:22:27 +01:00
public function getActorId () : int
2020-11-25 21:43:55 +00:00
{
2021-09-18 03:22:27 +01:00
return $this -> actor_id ;
2020-11-25 21:43:55 +00:00
}
2021-05-01 13:48:08 +01:00
public function setAttachmentId ( int $attachment_id ) : self
2020-11-25 21:43:55 +00:00
{
2021-05-01 13:48:08 +01:00
$this -> attachment_id = $attachment_id ;
2020-11-25 21:43:55 +00:00
return $this ;
}
2021-05-01 13:48:08 +01:00
public function getAttachmentId () : int
2020-11-25 21:43:55 +00:00
{
2021-05-01 13:48:08 +01:00
return $this -> attachment_id ;
2020-11-25 21:43:55 +00:00
}
2021-05-05 17:03:03 +01:00
public function setCreated ( DateTimeInterface $created ) : self
2020-11-25 21:43:55 +00:00
{
$this -> created = $created ;
return $this ;
}
2021-05-05 17:03:03 +01:00
public function getCreated () : DateTimeInterface
2020-11-25 21:43:55 +00:00
{
return $this -> created ;
}
2021-05-05 17:03:03 +01:00
public function setModified ( DateTimeInterface $modified ) : self
2020-11-25 21:43:55 +00:00
{
$this -> modified = $modified ;
return $this ;
}
2021-05-05 17:03:03 +01:00
public function getModified () : DateTimeInterface
2020-11-25 21:43:55 +00:00
{
return $this -> modified ;
}
2021-05-05 17:03:03 +01:00
// @codeCoverageIgnoreEnd
2020-11-25 21:43:55 +00:00
// }}} Autocode
2021-05-01 13:48:08 +01:00
private ? Attachment $attachment = null ;
2020-11-27 22:25:37 +00:00
2020-11-29 16:54:29 +00:00
/**
2021-05-01 13:48:08 +01:00
* get cover attachment
2020-11-29 16:54:29 +00:00
*/
2021-05-01 13:48:08 +01:00
public function getAttachment () : Attachment
2020-11-27 22:25:37 +00:00
{
2021-05-01 13:48:08 +01:00
$this -> attachment = $this -> attachment ? : DB :: find ( 'attachment' , [ 'id' => $this -> attachment_id ]);
return $this -> attachment ;
2020-11-27 22:25:37 +00:00
}
2020-11-29 16:54:29 +00:00
/**
2021-05-01 13:48:08 +01:00
* get cover attachment path
2020-11-29 16:54:29 +00:00
*/
2021-05-01 13:48:08 +01:00
public function getAttachmentPath () : string
2020-11-27 22:25:37 +00:00
{
2021-09-06 23:47:28 +01:00
return Common :: config ( 'cover' , 'dir' ) . $this -> getAttachment () -> getBestTitle ();
2020-11-27 22:25:37 +00:00
}
/**
2021-05-01 13:48:08 +01:00
* Delete this cover and the corresponding attachment and thumbnails , which this owns
2020-11-29 16:54:29 +00:00
*
2021-05-01 13:48:08 +01:00
* @ return array attachments deleted ( if delete_attachments_now is true )
2020-11-27 22:25:37 +00:00
*/
2021-05-01 13:48:08 +01:00
public function delete ( bool $flush = false , bool $delete_attachments_now = false , bool $cascading = false ) : array
2020-11-27 22:25:37 +00:00
{
2021-05-01 13:48:08 +01:00
// Don't go into a loop if we're deleting from Attachment
2020-11-27 22:25:37 +00:00
if ( ! $cascading ) {
2021-09-06 23:47:28 +01:00
$attachments = $this -> getAttachment () -> kill ();
2020-11-27 22:25:37 +00:00
} else {
2021-09-18 03:22:27 +01:00
DB :: remove ( DB :: getReference ( 'cover' , [ 'actor_id' => $this -> actor_id ]));
2021-05-01 13:48:08 +01:00
$attachment_path = $this -> getAttachmentPath ();
$attachments [] = $attachment_path ;
2020-11-27 22:25:37 +00:00
if ( $flush ) {
DB :: flush ();
}
2021-05-01 13:48:08 +01:00
return $delete_attachments_now ? [] : $attachments ;
2020-11-27 22:25:37 +00:00
}
return [];
}
2020-11-25 21:43:55 +00:00
public static function schemaDef () : array
{
return [
'name' => 'cover' ,
'fields' => [
2021-09-18 03:22:27 +01:00
'actor_id' => [ 'type' => 'int' , 'foreign key' => true , 'target' => 'Actor.id' , 'multiplicity' => 'one to one' , 'not null' => true , 'description' => 'foreign key to actor table' ],
2021-05-01 13:48:08 +01:00
'attachment_id' => [ 'type' => 'int' , 'foreign key' => true , 'target' => 'Attachment.id' , 'multiplicity' => 'one to one' , 'not null' => true , 'description' => 'foreign key to attachment table' ],
'created' => [ 'type' => 'datetime' , 'not null' => true , 'description' => 'date this record was created' , 'default' => 'CURRENT_TIMESTAMP' ],
'modified' => [ 'type' => 'timestamp' , 'not null' => true , 'description' => 'date this record was modified' , 'default' => 'CURRENT_TIMESTAMP' ],
2020-11-25 21:43:55 +00:00
],
2021-09-18 03:22:27 +01:00
'primary key' => [ 'actor_id' ],
2021-05-01 13:48:08 +01:00
'indexes' => [
'cover_attachment_id_idx' => [ 'attachment_id' ],
2020-11-25 21:43:55 +00:00
],
];
}
}