[Avatar] Move entity from core to component
This commit is contained in:
parent
fb6aa78ae8
commit
3334aca7b9
@ -34,25 +34,26 @@ use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
class Avatar extends Component
|
||||
{
|
||||
public function onAddRoute($r)
|
||||
public function onAddRoute($r): bool
|
||||
{
|
||||
$r->connect('avatar', '/{gsactor_id<\d+>}/avatar/{size<full|big|medium|small>?full}', [Controller\Avatar::class, 'avatar_view']);
|
||||
$r->connect('settings_avatar', '/settings/avatar', [Controller\Avatar::class, 'settings_avatar']);
|
||||
return Event::next;
|
||||
}
|
||||
|
||||
public function onPopulateProfileSettingsTabs(Request $request, &$tabs)
|
||||
public function onPopulateProfileSettingsTabs(Request $request, &$tabs): bool
|
||||
{
|
||||
// TODO avatar template shouldn't be on settings folder
|
||||
$tabs[] = ['title' => 'Avatar',
|
||||
'desc' => 'Change your avatar.',
|
||||
'controller' => C\Avatar::settings_avatar($request),
|
||||
$tabs[] = [
|
||||
'title' => 'Avatar',
|
||||
'desc' => 'Change your avatar.',
|
||||
'controller' => C\Avatar::settings_avatar($request),
|
||||
];
|
||||
|
||||
return Event::next;
|
||||
}
|
||||
|
||||
public function onStartTwigPopulateVars(array &$vars)
|
||||
public function onStartTwigPopulateVars(array &$vars): bool
|
||||
{
|
||||
if (Common::user() != null) {
|
||||
$vars['user_avatar'] = self::getAvatarUrl();
|
||||
@ -60,7 +61,7 @@ class Avatar extends Component
|
||||
return Event::next;
|
||||
}
|
||||
|
||||
public function onGetAvatarUrl(int $gsactor_id, ?string &$url)
|
||||
public function onGetAvatarUrl(int $gsactor_id, ?string &$url): bool
|
||||
{
|
||||
$url = self::getAvatarUrl($gsactor_id);
|
||||
return Event::next;
|
||||
@ -78,14 +79,14 @@ class Avatar extends Component
|
||||
/**
|
||||
* Get the avatar associated with the given nickname
|
||||
*/
|
||||
public static function getAvatar(?int $gsactor_id = null): \App\Entity\Avatar
|
||||
public static function getAvatar(?int $gsactor_id = null): Entity\Avatar
|
||||
{
|
||||
$gsactor_id = $gsactor_id ?: Common::userNickname();
|
||||
return GSFile::error(NoAvatarException::class,
|
||||
$gsactor_id,
|
||||
Cache::get("avatar-{$gsactor_id}",
|
||||
function () use ($gsactor_id) {
|
||||
return DB::dql('select a from App\Entity\Avatar a ' .
|
||||
return DB::dql('select a from Component\Avatar\Entity\Avatar a ' .
|
||||
'where a.gsactor_id = :gsactor_id',
|
||||
['gsactor_id' => $gsactor_id]);
|
||||
}));
|
||||
@ -122,11 +123,11 @@ class Avatar extends Component
|
||||
function () use ($gsactor_id) {
|
||||
return DB::dql('select f.file_hash, f.mimetype, f.title ' .
|
||||
'from App\Entity\Attachment f ' .
|
||||
'join App\Entity\Avatar a with f.id = a.attachment_id ' .
|
||||
'join Component\Avatar\Entity\Avatar a with f.id = a.attachment_id ' .
|
||||
'where a.gsactor_id = :gsactor_id',
|
||||
['gsactor_id' => $gsactor_id]);
|
||||
}));
|
||||
$res['file_path'] = \App\Entity\Avatar::getFilePathStatic($res['file_hash']);
|
||||
$res['file_path'] = Entity\Avatar::getFilePathStatic($res['file_hash']);
|
||||
return $res;
|
||||
} catch (Exception $e) {
|
||||
$filepath = INSTALLDIR . '/public/assets/default-avatar.svg';
|
||||
|
@ -28,10 +28,12 @@ use App\Core\Form;
|
||||
use App\Core\GSFile;
|
||||
use App\Core\GSFile as M;
|
||||
use function App\Core\I18n\_m;
|
||||
use App\Entity\Avatar as AvatarEntity;
|
||||
use App\Core\Log;
|
||||
use App\Util\Common;
|
||||
use App\Util\Exception\ClientException;
|
||||
use App\Util\Exception\NotFoundException;
|
||||
use App\Util\TemporaryFile;
|
||||
use Component\Avatar\Entity\Avatar as AvatarEntity;
|
||||
use Exception;
|
||||
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\FileType;
|
||||
@ -39,13 +41,14 @@ use Symfony\Component\Form\Extension\Core\Type\HiddenType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
||||
use Symfony\Component\Form\FormError;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class Avatar extends Controller
|
||||
{
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function avatar_view(Request $request, int $gsactor_id, string $size)
|
||||
public function avatar_view(Request $request, int $gsactor_id, string $size): Response
|
||||
{
|
||||
switch ($size) {
|
||||
case 'full':
|
||||
@ -59,7 +62,7 @@ class Avatar extends Controller
|
||||
/**
|
||||
* Local user avatar panel
|
||||
*/
|
||||
public static function settings_avatar(Request $request)
|
||||
public static function settings_avatar(Request $request): array
|
||||
{
|
||||
$form = Form::create([
|
||||
['avatar', FileType::class, ['label' => _m('Avatar'), 'help' => _m('You can upload your personal avatar. The maximum file size is 2MB.'), 'multiple' => false, 'required' => false]],
|
||||
@ -97,7 +100,7 @@ class Avatar extends Controller
|
||||
}
|
||||
}
|
||||
} elseif (isset($data['avatar'])) {
|
||||
// Cropping failed (e.g. disabled js), have file as uploaded
|
||||
// Cropping failed (e.g. disabled js), use file as uploaded
|
||||
$file = $data['avatar'];
|
||||
} else {
|
||||
throw new ClientException('Invalid form');
|
||||
@ -112,9 +115,7 @@ class Avatar extends Controller
|
||||
// Must get old id before inserting another one
|
||||
$old_attachment = null;
|
||||
$avatar = DB::find('avatar', ['gsactor_id' => $gsactor_id]);
|
||||
if ($avatar != null) {
|
||||
$old_attachment = $avatar->delete();
|
||||
}
|
||||
$old_attachment = $avatar?->delete();
|
||||
DB::persist($attachment);
|
||||
// Can only get new id after inserting
|
||||
DB::flush();
|
||||
|
@ -19,11 +19,13 @@
|
||||
|
||||
// }}}
|
||||
|
||||
namespace App\Entity;
|
||||
namespace Component\Avatar\Entity;
|
||||
|
||||
use App\Core\DB\DB;
|
||||
use App\Core\Entity;
|
||||
use App\Core\Log;
|
||||
use App\Core\Router\Router;
|
||||
use App\Entity\Attachment;
|
||||
use App\Util\Common;
|
||||
use DateTimeInterface;
|
||||
|
||||
@ -47,8 +49,8 @@ class Avatar extends Entity
|
||||
// @codeCoverageIgnoreStart
|
||||
private int $gsactor_id;
|
||||
private int $attachment_id;
|
||||
private \DateTimeInterface $created;
|
||||
private \DateTimeInterface $modified;
|
||||
private DateTimeInterface $created;
|
||||
private DateTimeInterface $modified;
|
||||
|
||||
public function setGSActorId(int $gsactor_id): self
|
||||
{
|
||||
@ -134,7 +136,7 @@ class Avatar extends Entity
|
||||
$filepath = $this->getPath();
|
||||
if (file_exists($filepath)) {
|
||||
if (@unlink($filepath) === false) {
|
||||
Log::warning("Failed deleting attachment for avatar with id={$id} at {$filepath}");
|
||||
Log::warning("Failed deleting attachment for avatar with id={$this->attachment_id} at {$filepath}");
|
||||
}
|
||||
}
|
||||
$this->attachment->delete(cascade: true, flush: false);
|
||||
@ -152,7 +154,7 @@ class Avatar extends Entity
|
||||
'fields' => [
|
||||
'gsactor_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'GSActor.id', 'multiplicity' => 'one 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'],
|
||||
'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'],
|
||||
],
|
||||
'primary key' => ['gsactor_id'],
|
@ -24,6 +24,7 @@ namespace App\Entity;
|
||||
use App\Core\DB\DB;
|
||||
use App\Core\Entity;
|
||||
use App\Core\GSFile;
|
||||
use App\Core\Log;
|
||||
use App\Util\Common;
|
||||
use DateTimeInterface;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user