[PLUGINS][ProfileColor] Settings page render problem fixed. ColorType given data wasn't a string.

This commit is contained in:
Eliseu Amaro 2021-09-25 12:51:25 +01:00
parent 57b94af9f6
commit 808da203ad
7 changed files with 44 additions and 31 deletions

View File

@ -133,7 +133,7 @@ class Cover
} }
$removeForm = $form2->createView(); $removeForm = $form2->createView();
} }
return ['_template' => 'cover/cover.html.twig', 'form' => $form->createView(), 'remove_form' => $removeForm]; return ['_template' => 'cover/cover.html.twig', 'cover' => $form->createView(), 'cover_remove_form' => $removeForm];
} }
/** /**

View File

@ -1,9 +1,9 @@
<div class='form'> <div class='form'>
{{ form(form) }} {{ form(cover) }}
</div> </div>
{% if remove_form is not null %} {% if cover_remove_form is not null %}
<div class='form'> <div class='form'>
{{ form(remove_form) }} {{ form(cover_remove_form) }}
</div> </div>
{% endif %} {% endif %}

View File

@ -23,6 +23,7 @@ namespace Plugin\ProfileColor\Controller;
use App\Core\DB\DB; use App\Core\DB\DB;
use App\Core\Form; use App\Core\Form;
use App\Util\Exception\ServerException;
use function App\Core\I18n\_m; use function App\Core\I18n\_m;
use App\Util\Common; use App\Util\Common;
use App\Util\Exception\RedirectException; use App\Util\Exception\RedirectException;
@ -39,51 +40,54 @@ use Symfony\Component\HttpFoundation\Request;
* @category ProfileColor * @category ProfileColor
* *
* @author Daniel Brandao <up201705812@fe.up.pt> * @author Daniel Brandao <up201705812@fe.up.pt>
* @author Eliseu Amaro <mail@eliseuama.ro>
* @copyright 2020 Free Software Foundation, Inc http://www.fsf.org * @copyright 2020 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/ */
class ProfileColor class ProfileColor
{ {
/** /**
* Add/change profile color * Change Profile color background
*
* @param Request $request * @param Request $request
* * @return array
* @throws RedirectException * @throws RedirectException
* * @throws ServerException
* @return array template
*/ */
public static function profileColorSettings(Request $request) public static function profileColorSettings(Request $request)
{ {
$user = Common::user(); $user = Common::user();
$actor_id = $user->getId(); $actor_id = $user->getId();
$pcolor = DB::find('profile_color', ['actor_id' => $actor_id]); $pcolor = DB::findOneBy('profile_color', ['actor_id' => $actor_id]);
$color = '#000000';
if ($pcolor != null) {
$color = $pcolor;
}
$form = Form::create([ $form = Form::create([
['color', ColorType::class, ['data' => $color, 'label' => _m('Profile Color'), 'help' => _m('Choose your Profile Color')] ], ['color', ColorType::class, [
'html5' => true,
'data' => "#000000",
'label' => _m('Profile Color'),
'help' => _m('Choose your Profile Color')]
],
['hidden', HiddenType::class, []], ['hidden', HiddenType::class, []],
['save_profile_color', SubmitType::class, ['label' => _m('Submit')]], ['save_profile_color', SubmitType::class, ['label' => _m('Submit')]],
]); ]);
$form->handleRequest($request); $form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) { if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
if ($pcolor !== null) { if ($pcolor !== null) {
DB::remove($pcolor); DB::remove($pcolor);
DB::flush(); DB::flush();
} }
$pcolor = Entity\ProfileColor::create(['actor_id' => $actor_id, 'profile_color' => $data['color']]); $data = $form->getData();
$pcolor = Entity\ProfileColor::create(['actor_id' => $actor_id, 'color' => $data['color']]);
DB::persist($pcolor); DB::persist($pcolor);
DB::flush(); DB::flush();
throw new RedirectException(); throw new RedirectException();
} }
return ['_template' => 'profileColor/profileColorSettings.html.twig', 'form' => $form->createView()]; return ['_template' => 'profileColor/profileColorSettings.html.twig', 'profile_color' => $form->createView()];
} }
} }

View File

@ -25,6 +25,8 @@ use App\Core\Event;
use App\Core\Modules\Plugin; use App\Core\Modules\Plugin;
use App\Core\Router\RouteLoader; use App\Core\Router\RouteLoader;
use App\Util\Common; use App\Util\Common;
use App\Util\Exception\RedirectException;
use App\Util\Exception\ServerException;
use App\Util\Formatting; use App\Util\Formatting;
use Plugin\ProfileColor\Controller as C; use Plugin\ProfileColor\Controller as C;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
@ -50,10 +52,17 @@ class ProfileColor extends Plugin
*/ */
public function onAddRoute(RouteLoader $r): bool public function onAddRoute(RouteLoader $r): bool
{ {
$r->connect('settings_profile_color', 'settings/color', [Controller\ProfileColor::class, 'profilecolorsettings']); $r->connect('settings_profile_color', 'settings/color', [Controller\ProfileColor::class, 'profileColorSettings']);
return Event::next; return Event::next;
} }
/**
* @param Request $request
* @param $tabs
* @return bool
* @throws RedirectException
* @throws ServerException
*/
public function onPopulateProfileSettingsTabs(Request $request, &$tabs) public function onPopulateProfileSettingsTabs(Request $request, &$tabs)
{ {
// TODO avatar template shouldn't be on settings folder // TODO avatar template shouldn't be on settings folder
@ -74,15 +83,15 @@ class ProfileColor extends Plugin
* @param $res * @param $res
* @return bool * @return bool
*/ */
public function onAppendCardProfile(&$res): bool public function onAppendCardProfile($vars, &$res): bool
{ {
$user = Common::user(); $actor = $vars['actor'];
if ($user !== null) { if ($actor !== null) {
$actor_id = $user->getId(); $actor_id = $actor->getId();
$color = DB::find('profile_color', ['actor_id' => $actor_id]); $color = DB::find('profile_color', ['actor_id' => $actor_id]);
if ($color !== null) { if ($color !== null) {
$res[] = Formatting::twigRenderFile('/profileColor/profileColorView.html.twig', ['profile' => $color]); $res[] = Formatting::twigRenderFile('/profileColor/profileColorView.html.twig', ['profile_color' => $color, 'actor' => $actor_id]);
} }
} }

View File

@ -1,3 +1,3 @@
<div class='form'> <div class='form'>
{{ form(form) }} {{ form(profile_color) }}
</div> </div>

View File

@ -1,7 +1,7 @@
{% block stylesheets %} {% block stylesheets %}
<style> <style>
.profile { #profile-{{ actor }} {
background: {{ profile.color }} !important; background: {{ profile_color.color }} !important;
} }
</style> </style>
{% endblock stylesheets %} {% endblock stylesheets %}

View File

@ -4,7 +4,7 @@
{% set actor_bio = actor.getBio() %} {% set actor_bio = actor.getBio() %}
{% block profile_view %} {% block profile_view %}
<section class='profile' title="{{ actor_nickname }}'s {{ 'profile information.' | trans }}"> <section id='profile-{{ actor.id }}' class='profile' title="{{ actor_nickname }}'s {{ 'profile information.' | trans }}">
<a href="{{ path('actor_view_nickname', {'nickname' : actor_nickname}) }}"> <a href="{{ path('actor_view_nickname', {'nickname' : actor_nickname}) }}">
<div class="profile-info"> <div class="profile-info">
<img src='{{ actor_avatar }}' class="profile-avatar" alt="{{ actor_nickname }}{{ '\'s avatar.' | trans }}"> <img src='{{ actor_avatar }}' class="profile-avatar" alt="{{ actor_nickname }}{{ '\'s avatar.' | trans }}">
@ -35,7 +35,7 @@
{% endif %} {% endif %}
</nav> </nav>
{% for block in handle_event('AppendCardProfile') %} {% for block in handle_event('AppendCardProfile', {'actor': actor}) %}
{{ block | raw }} {{ block | raw }}
{% endfor %} {% endfor %}
</section> </section>