2020-11-25 21:43:55 +00:00
< ? php
// {{{ 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/>.
// }}}
namespace App\Entity ;
2020-11-27 22:25:37 +00:00
use App\Core\DB\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 ;
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
private int $gsactor_id ;
2021-05-01 13:48:08 +01:00
private int $attachment_id ;
2021-04-27 22:24:48 +01:00
private \DateTimeInterface $created ;
private \DateTimeInterface $modified ;
2020-11-25 21:43:55 +00:00
2021-04-15 17:58:13 +01:00
public function setGSActorId ( int $gsactor_id ) : self
2020-11-25 21:43:55 +00:00
{
$this -> gsactor_id = $gsactor_id ;
return $this ;
}
2021-04-15 17:58:13 +01:00
public function getGSActorId () : int
2020-11-25 21:43:55 +00:00
{
return $this -> gsactor_id ;
}
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
}
public function setCreated ( DateTimeInterface $created ) : self
{
$this -> created = $created ;
return $this ;
}
public function getCreated () : DateTimeInterface
{
return $this -> created ;
}
public function setModified ( DateTimeInterface $modified ) : self
{
$this -> modified = $modified ;
return $this ;
}
public function getModified () : DateTimeInterface
{
return $this -> modified ;
}
// }}} 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
* @ return 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
*
* @ return string
*/
2021-05-01 13:48:08 +01:00
public function getAttachmentPath () : string
2020-11-27 22:25:37 +00:00
{
2021-05-01 13:48:08 +01:00
return Common :: config ( 'cover' , 'dir' ) . $this -> getAttachment () -> getAttachmentName ();
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
*
* @ param bool $flush
2021-05-01 13:48:08 +01:00
* @ param bool $delete_attachments_now
2020-11-29 16:54:29 +00:00
* @ param bool $cascading
*
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-05-01 13:48:08 +01:00
$attachments = $this -> getAttachment () -> delete ( $cascade = true , $attachment_flush = false , $delete_attachments_now );
2020-11-27 22:25:37 +00:00
} else {
DB :: remove ( DB :: getReference ( 'cover' , [ 'gsactor_id' => $this -> gsactor_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-05-01 13:48:08 +01:00
'gsactor_id' => [ 'type' => 'int' , 'foreign key' => true , 'target' => 'GSActor.id' , 'multiplicity' => 'onne to one' , 'not null' => true , 'description' => 'foreign key to gsactor table' ],
'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-05-01 13:48:08 +01:00
'primary key' => [ 'gsactor_id' ],
'indexes' => [
'cover_attachment_id_idx' => [ 'attachment_id' ],
2020-11-25 21:43:55 +00:00
],
];
}
}