[ProfileColor] Added Profile Color entity, color form and db store/load to color settings controler

This commit is contained in:
Daniel 2020-12-16 23:19:52 +00:00 committed by Hugo Sales
parent 20a5005e1d
commit afae038cff
Signed by untrusted user: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
3 changed files with 138 additions and 18 deletions

View File

@ -21,20 +21,23 @@
namespace Plugin\ProfileColor\Controller; namespace Plugin\ProfileColor\Controller;
use App\Core\DB\DB;
use App\Core\Form; use App\Core\Form;
use function App\Core\I18n\_m; use function App\Core\I18n\_m;
use App\Entity\ProfileColor as PColor;
use App\Util\Common;
use App\Util\Exception\ClientException; use App\Util\Exception\ClientException;
use App\Util\Exception\ServerException; use App\Util\Exception\ServerException;
use Symfony\Component\Form\Extension\Core\Type\ColorType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType; use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
/** /**
* Cover controller * Profile Color controller
* *
* @package GNUsocial * @package GNUsocial
* @category CoverPlugin * @category ProfileColor
* *
* @author Daniel Brandao <up201705812@fe.up.pt> * @author Daniel Brandao <up201705812@fe.up.pt>
* @copyright 2020 Free Software Foundation, Inc http://www.fsf.org * @copyright 2020 Free Software Foundation, Inc http://www.fsf.org
@ -54,8 +57,19 @@ class ProfileColor
*/ */
public function profileColorSettings(Request $request) public function profileColorSettings(Request $request)
{ {
$user = Common::user();
$actor_id = $user->getId();
$pcolor = DB::find('profile_color', ['gsactor_id' => $actor_id]);
$color = '#000000';
if ($pcolor != null) {
$color = $pcolor->getColor();
}
//print_r("STORED: " );
//var_dump($color);
$form = Form::create([ $form = Form::create([
['color', TextType::class, ['label' => _m('Color')]], ['color', ColorType::class, ['data' => $color, 'label' => _m('Profile Color')]],
['hidden', HiddenType::class, []], ['hidden', HiddenType::class, []],
['save', SubmitType::class, ['label' => _m('Submit')]], ['save', SubmitType::class, ['label' => _m('Submit')]],
]); ]);
@ -63,6 +77,17 @@ class ProfileColor
$form->handleRequest($request); $form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) { if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData(); $data = $form->getData();
//var_dump($data['color']);
// Must get old id before inserting another one
if ($pcolor != null) {
DB::remove($pcolor);
DB::flush();
}
$pcolor = PColor::create(['gsactor_id' => $actor_id, 'color' => $data['color']]);
DB::persist($pcolor);
DB::flush();
} }
return ['_template' => 'profilecolor/profilecolor.html.twig', 'form' => $form->createView()]; return ['_template' => 'profilecolor/profilecolor.html.twig', 'form' => $form->createView()];

View File

@ -20,17 +20,15 @@
namespace Plugin\ProfileColor; namespace Plugin\ProfileColor;
use App\Core\DB\DB;
use App\Core\Event; use App\Core\Event;
use App\Core\Module; use App\Core\Module;
use App\Core\Router\RouteLoader; use App\Core\Router\RouteLoader;
use App\Util\Common;
/** /**
* Profile Color plugin main class * Profile Color plugin main class
* *
* @package GNUsocial * @package GNUsocial
* @category CoverPlugin * @category ProfileColor
* *
* @author Daniel Brandao <up201705812@fe.up.pt> * @author Daniel Brandao <up201705812@fe.up.pt>
* @copyright 2020 Free Software Foundation, Inc http://www.fsf.org * @copyright 2020 Free Software Foundation, Inc http://www.fsf.org
@ -63,16 +61,6 @@ class ProfileColor extends Module
$vars['profile_tabs'][] = ['title' => 'Color', $vars['profile_tabs'][] = ['title' => 'Color',
'route' => 'settings_profile_color', 'route' => 'settings_profile_color',
]; ];
/*
if (Common::user() != null) {
$cover = DB::find('cover', ['gsactor_id' => Common::user()->getId()]);
if ($cover != null) {
$vars['profile_extras'][] = ['name' => 'cover', 'vars' => ['img' => '/cover']];
} else {
$vars['profile_extras'][] = ['name' => 'cover', 'vars' => []];
}
}
*/
return Event::next; return Event::next;
} }
@ -86,7 +74,7 @@ class ProfileColor extends Module
public function onStartShowStyles(array &$styles): bool public function onStartShowStyles(array &$styles): bool
{ {
//$styles[] = 'cover/cover.css'; //$styles[] = 'profilecolor/profilecolor.css';
return Event::next; return Event::next;
} }
} }

107
src/Entity/ProfileColor.php Normal file
View File

@ -0,0 +1,107 @@
<?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;
use App\Core\Entity;
use DateTimeInterface;
/**
* For storing a profile Color
*
* @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
*/
class ProfileColor extends Entity
{
// {{{ Autocode
private int $gsactor_id;
private string $color;
private DateTimeInterface $created;
private DateTimeInterface $modified;
public function setGsactorId(int $gsactor_id): self
{
$this->gsactor_id = $gsactor_id;
return $this;
}
public function getGsactorId(): int
{
return $this->gsactor_id;
}
public function setColor(string $color): self
{
$this->color = $color;
return $this;
}
public function getColor(): string
{
return $this->color;
}
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
public static function schemaDef(): array
{
return [
'name' => 'profile_color',
'fields' => [
'gsactor_id' => ['type' => 'int', 'not null' => true, 'description' => 'foreign key to gsactor table'],
'color' => ['type' => 'text', 'not null' => true, 'description' => 'color hex code'],
'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'],
'foreign keys' => [
'cover_gsactor_id_fkey' => ['gsactor', ['gsactor_id' => 'id']],
],
];
}
}