Skip to content

Commit

Permalink
feat(admin): add admin controllers for organizational elements
Browse files Browse the repository at this point in the history
  • Loading branch information
sgomez committed Feb 26, 2021
1 parent c16bd1b commit abfe078
Show file tree
Hide file tree
Showing 6 changed files with 221 additions and 1 deletion.
34 changes: 33 additions & 1 deletion src/Controller/Admin/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@

namespace App\Controller\Admin;

use App\Entity\Degree;
use App\Entity\Group;
use App\Entity\User;
use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use EasyCorp\Bundle\EasyAdminBundle\Config\Dashboard;
use EasyCorp\Bundle\EasyAdminBundle\Config\MenuItem;
use EasyCorp\Bundle\EasyAdminBundle\Config\UserMenu;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractDashboardController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\User\UserInterface;

class DashboardController extends AbstractDashboardController
{
Expand All @@ -25,9 +33,33 @@ public function configureDashboard(): Dashboard
->setTitle('Idea');
}

public function configureActions(): Actions
{
return parent::configureActions()
->add(Crud::PAGE_INDEX, Action::DETAIL)
;
}

public function configureMenuItems(): iterable
{
yield MenuItem::linktoDashboard('Dashboard', 'fa fa-home');
// yield MenuItem::linkToCrud('The Label', 'fas fa-list', EntityClass::class);
yield MenuItem::section('Organization');
yield MenuItem::linkToCrud('Users', 'fas fa-user', User::class);
yield MenuItem::linkToCrud('Groups', 'fas fa-users', Group::class);
yield MenuItem::linkToCrud('Degrees', 'fas fa-graduation-cap', Degree::class);
}

public function configureUserMenu(UserInterface $user): UserMenu
{
$userMenu = parent::configureUserMenu($user);

/** @var User $user */
if (!$user->getImage()) {
return $userMenu;
}

return $userMenu
->setAvatarUrl('/images/avatars/'.$user->getImage()->getName())
;
}
}
38 changes: 38 additions & 0 deletions src/Controller/Admin/DegreeCrudController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace App\Controller\Admin;

use App\Entity\Degree;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Field\FormField;
use EasyCorp\Bundle\EasyAdminBundle\Field\IdField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;

class DegreeCrudController extends AbstractCrudController
{
public static function getEntityFqcn(): string
{
return Degree::class;
}

public function configureCrud(Crud $crud): Crud
{
return $crud
->setEntityLabelInPlural('Degrees')
->setEntityLabelInSingular('Degree')
;
}

public function configureFields(string $pageName): iterable
{
yield FormField::addPanel('General');
yield IdField::new('id')
->onlyOnIndex()
;
yield TextField::new('name');
yield TextField::new('slug')
->onlyOnDetail()
;
}
}
39 changes: 39 additions & 0 deletions src/Controller/Admin/GroupCrudController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace App\Controller\Admin;

use App\Entity\Group;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Field\FormField;
use EasyCorp\Bundle\EasyAdminBundle\Field\IdField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;

class GroupCrudController extends AbstractCrudController
{
public static function getEntityFqcn(): string
{
return Group::class;
}

public function configureCrud(Crud $crud): Crud
{
return $crud
->setEntityLabelInPlural('Groups')
->setEntityLabelInSingular('Group')
;
}

public function configureFields(string $pageName): iterable
{
yield FormField::addPanel('General');
yield IdField::new('id')
->onlyOnIndex()
;
yield TextField::new('name');
yield TextField::new('icon');
yield TextField::new('slug')
->onlyOnDetail()
;
}
}
98 changes: 98 additions & 0 deletions src/Controller/Admin/UserCrudController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

namespace App\Controller\Admin;

use App\Entity\User;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Field\ArrayField;
use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
use EasyCorp\Bundle\EasyAdminBundle\Field\BooleanField;
use EasyCorp\Bundle\EasyAdminBundle\Field\ChoiceField;
use EasyCorp\Bundle\EasyAdminBundle\Field\FormField;
use EasyCorp\Bundle\EasyAdminBundle\Field\IdField;
use EasyCorp\Bundle\EasyAdminBundle\Field\ImageField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextareaField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
use Vich\UploaderBundle\Form\Type\VichImageType;

class UserCrudController extends AbstractCrudController
{
public static function getEntityFqcn(): string
{
return User::class;
}


public function configureCrud(Crud $crud): Crud
{
return $crud
->setEntityLabelInPlural('Users')
->setEntityLabelInSingular('User')
;
}

public function configureFields(string $pageName): iterable
{
yield FormField::addPanel('General');
yield TextField::new('username');
yield TextField::new('email')
->hideOnIndex()
;


yield FormField::addPanel('Security');
yield BooleanField::new('enabled');
yield ChoiceField::new('roles')
->setChoices([
'Usuario' => 'ROLE_USER',
'Gestor' => 'ROLE_ADMIN',
'Administrador' => 'ROLE_SUPER_ADMIN',
])
->allowMultipleChoices()
;
yield AssociationField::new('versions')
->setLabel('Policies')
->setTemplatePath('admin/user/policies.html.twig')
->onlyOnDetail();

yield FormField::addPanel('Profile');
yield IdField::new('id')
->onlyOnIndex()
;
yield TextField::new('firstname');
yield TextField::new('lastname');
yield ChoiceField::new('collective')
->setChoices(User::getCollectives())
;
yield TextField::new('nic')
->hideOnIndex()
;
yield AssociationField::new('degree')
->hideOnIndex()
;
yield TextField::new('year')
->hideOnIndex()
;
yield TextareaField::new('imageFile')
->setFormType(VichImageType::class)
->onlyOnForms()
;
yield ImageField::new('image.name')
->setBasePath('/images/avatars')
->setCssClass('ea-vich-image')
->onlyOnDetail()
;
}

/*
public function configureFields(string $pageName): iterable
{
return [
IdField::new('id'),
TextField::new('title'),
TextEditorField::new('description'),
];
}
*/
}
6 changes: 6 additions & 0 deletions src/Entity/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ public function __toString(): string
return (string) $this->name;
}

public function getId(): ?int
{
return $this->id;
}


/**
* @return Idea[]|Collection
*/
Expand Down
7 changes: 7 additions & 0 deletions templates/admin/user/policies.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<ul>
{% for logPolicy in field.value %}
<li>{{ logPolicy.version }} ({{ logPolicy.createAt | date('d-m-Y H:i') }})</li>
{% else %}
<li>No ha aceptado ninguna política de uso</li>
{% endfor %}
</ul>

0 comments on commit abfe078

Please sign in to comment.