[Cover] Remove of cover form

This commit is contained in:
Daniel 2020-12-29 18:27:02 +00:00 committed by Hugo Sales
parent 485607169f
commit 35d2bdfd5e
2 changed files with 29 additions and 4 deletions

View File

@ -27,6 +27,7 @@ use function App\Core\I18n\_m;
use App\Entity\Cover as CoverEntity; use App\Entity\Cover as CoverEntity;
use App\Util\Common; use App\Util\Common;
use App\Util\Exception\ClientException; use App\Util\Exception\ClientException;
use App\Util\Exception\RedirectException;
use App\Util\Exception\ServerException; use App\Util\Exception\ServerException;
use Component\Media\Media; use Component\Media\Media;
use Component\Media\Media as M; use Component\Media\Media as M;
@ -62,6 +63,9 @@ class Cover
*/ */
public function coverSettings(Request $request) public function coverSettings(Request $request)
{ {
$user = Common::user();
$actor_id = $user->getId();
$form = Form::create([ $form = Form::create([
['cover', FileType::class, ['label' => _m('Cover'), 'help' => _m('You can upload your personal cover. The maximum file size is 2MB.'), ['cover', FileType::class, ['label' => _m('Cover'), 'help' => _m('You can upload your personal cover. The maximum file size is 2MB.'),
'constraints' => [ 'constraints' => [
@ -93,8 +97,6 @@ class Cover
if (explode('/',$sfile->getMimeType())[0] != 'image') { if (explode('/',$sfile->getMimeType())[0] != 'image') {
throw new ServerException('Invalid file type'); throw new ServerException('Invalid file type');
} }
$user = Common::user();
$actor_id = $user->getId();
$file = Media::validateAndStoreFile($sfile, Common::config('cover', 'dir'), $title = null, $is_local = true, $use_unique = $actor_id); $file = Media::validateAndStoreFile($sfile, Common::config('cover', 'dir'), $title = null, $is_local = true, $use_unique = $actor_id);
$old_file = null; $old_file = null;
$cover = DB::find('cover', ['gsactor_id' => $actor_id]); $cover = DB::find('cover', ['gsactor_id' => $actor_id]);
@ -113,9 +115,26 @@ class Cover
if ($old_file != null) { if ($old_file != null) {
@unlink($old_file); @unlink($old_file);
} }
throw new RedirectException();
} }
return ['_template' => 'cover/cover.html.twig', 'form' => $form->createView()]; $removeForm = null;
$cover = DB::find('cover', ['gsactor_id' => $actor_id]);
if ($cover != null) {
$form2 = Form::create([
['remove', SubmitType::class, ['label' => _m('Remove')]],
]);
$form2->handleRequest($request);
if ($form2->isSubmitted() && $form2->isValid()) {
$old_file = $cover->delete();
DB::remove($cover);
DB::flush();
@unlink($old_file);
throw new RedirectException();
}
$removeForm = $form2->createView();
}
return ['_template' => 'cover/cover.html.twig', 'form' => $form->createView(), 'remove_form' => $removeForm];
} }
/** /**

View File

@ -7,7 +7,13 @@
{% endblock body %} {% endblock body %}
{% block form %} {% block form %}
<div class='form'> <div class='form-single'>
{{ form(form) }} {{ form(form) }}
</div> </div>
{% if remove_form is not null %}
<div class='form-single'>
{{ form(remove_form) }}
</div>
{% endif %}
{% endblock form %} {% endblock form %}