Padhie

Untitled

Jan 1st, 2018
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.99 KB | None | 0 0
  1. class Admin extends BaseUser
  2. {
  3.     /**
  4.      * @ORM\Id()
  5.      * @ORM\Column(name="idAdmin", type="integer")
  6.      * @ORM\GeneratedValue(strategy="AUTO")
  7.      */
  8.     protected $id;
  9.  
  10.     /**
  11.      * @var string
  12.      * @ORM\Column(type="string", length=255, nullable=true, options={"default":NULL})
  13.      */
  14.     protected $style;
  15.  
  16.     /**
  17.      * @ORM\ManyToMany(targetEntity="AdminBundle\Entity\AdminGroup")
  18.      * @ORM\JoinTable(
  19.      *     name="admin_has_group",
  20.      *     joinColumns={
  21.      *         @ORM\JoinColumn(name="idAdmin", referencedColumnName="idAdmin")
  22.      *     },
  23.      *     inverseJoinColumns={
  24.      *         @ORM\JoinColumn(name="idGroup", referencedColumnName="idGroup")
  25.      *     }
  26.      * )
  27.      */
  28.     protected $groups;
  29.  
  30.     /**
  31.      * Admin constructor.
  32.      */
  33.     public function __construct()
  34.     {
  35.         parent::__construct();
  36.         $this->groups = new ArrayCollection();
  37.     }
  38.  
  39.     /**
  40.      * @return string
  41.      */
  42.     public function getStyle()
  43.     {
  44.         return $this->style;
  45.     }
  46.  
  47.     /**
  48.      * @param string $style
  49.      * @return Admin
  50.      */
  51.     public function setStyle($style)
  52.     {
  53.         $this->style = $style;
  54.  
  55.         return $this;
  56.     }
  57.  
  58.     /**
  59.      * @param ArrayCollection
  60.      *
  61.      * @return Admin
  62.      */
  63.     public function setGroups($groups)
  64.     {
  65.  
  66.         $this->groups = $groups;
  67.  
  68.         return $this;
  69.     }
  70.  
  71.     /**
  72.      * @return ArrayCollection
  73.      */
  74.     public function getGroups()
  75.     {
  76.         return $this->groups;
  77.     }
  78.  
  79.     /**
  80.      * @param GroupInterface $group
  81.      *
  82.      * @return Admin
  83.      */
  84.     public function addGroup(GroupInterface $group)
  85.     {
  86.         if (empty($this->getGroups())) {
  87.             $this->groups = new ArrayCollection();
  88.         }
  89.  
  90.         if (!$this->getGroups()->contains($group)) {
  91.             $this->getGroups()->add($group);
  92.         }
  93.  
  94.         return $this;
  95.     }
  96.  
  97.     /**
  98.      * @param string $name
  99.      *
  100.      * @return bool
  101.      */
  102.     public function hasGroup($name="") {
  103.         dump($name);
  104.         if (!empty($name)) {
  105.             return parent::hasGroup($name);
  106.         }
  107.         return false;
  108.     }
  109. }
  110.  
  111. --------------------------------------------------------------------------------
  112.  
  113. class AdminGroup extends BaseGroup
  114. {
  115.     /**
  116.      * @var int
  117.      * @ORM\Id
  118.      * @ORM\Column(name="idGroup", type="integer")
  119.      * @ORM\GeneratedValue(strategy="AUTO")
  120.      */
  121.     protected $id;
  122.  
  123.     /**
  124.      * Group constructor.
  125.      *
  126.      * @param string $name
  127.      * @param array  $roles
  128.      */
  129.     public function __construct($name = '', $roles = array())
  130.     {
  131.         parent::__construct($name, $roles);
  132.     }
  133. }
  134.  
  135. --------------------------------------------------------------------------------
  136.  
  137.     public function newAction(Request $request)
  138.     {
  139.         $em    = $this->getDoctrine()->getManager();
  140.         $admin = new Admin();
  141.         $form  = $this->buildForm($admin, true);
  142.  
  143.         $form->handleRequest($request);
  144.         if ($form->isSubmitted() && $form->isValid()) {
  145.  
  146.             /** @var Admin $admin */
  147.             $admin = $form->getData();
  148.  
  149.             $admin->setEmail($admin->getUsername());
  150.             $admin->setEnabled(true);
  151.  
  152.             $em->persist($admin);
  153.             $em->flush();
  154.  
  155.             return $this->redirectToRoute('backend_admin_list');
  156.         }
  157.  
  158.         return $this->render(
  159.             'edit.html.twig', [
  160.             'form' => $form->createView(),
  161.         ]);
  162.  
  163.     }
  164.  
  165.     public function editAction($id, Request $request)
  166.     {
  167.         $em        = $this->getDoctrine()->getManager();
  168.         $admin     = $this->getDoctrine()->getRepository(Admin::class)->find($id);
  169.         $form      = $this->buildForm($admin);
  170.  
  171.         $form->handleRequest($request);
  172.         if ($form->isSubmitted() && $form->isValid()) {
  173.  
  174.             /** @var Admin $admin */
  175.             $admin = $form->getData();
  176.  
  177.             $em->persist($admin);
  178.             $em->flush();
  179.  
  180.             return $this->redirectToRoute('index');
  181.         }
  182.  
  183.         return $this->render(
  184.             'edit.html.twig', [
  185.             'form' => $form->createView(),
  186.         ]);
  187.     }
  188.  
  189.     protected function buildForm(Admin $admin)
  190.     {
  191.         return $this->createFormBuilder($admin)
  192.             ->add("username", TextType::class, [
  193.                 'required' => true,
  194.             ])
  195.             ->add('plainPassword', PasswordType::class, [
  196.                 'label'    => 'Passwort',
  197.                 'required' => true,
  198.             ])
  199.             ->add(
  200.                 'style', ChoiceType::class, [
  201.                 'required' => false,
  202.                 'choices'  => [
  203.                     'System'   => '',
  204.                     'AdminLTE' => 'adminlte',
  205.                 ],
  206.             ])
  207.             ->add('groups', EntityType::class, [
  208.                 'class'        => AdminGroup::class,
  209.                 'multiple'     => true,
  210.                 'choice_label' => 'name',
  211.             ])
  212.             ->add('save', SubmitType::class, ['label' => 'Save'])
  213.             ->getForm();
  214.     }
Advertisement
Add Comment
Please, Sign In to add comment