<commit_before><?php
/**
 * Created by PhpStorm.
 * User: michi
 * Date: 07.01.17
 * Time: 20:31
 */

namespace AppBundle\Controller\Account;


use AppBundle\Entity\Organization;
use AppBundle\Entity\User;
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\HttpFoundation\Request;



class Account extends Controller
{
    /**
     * @Route("/account", name="account")
     */
    public function indexAction(Request $request)
    {
        $rep = $this->getDoctrine()->getRepository('AppBundle:Screen');
        $screens = $rep->findAll();


        return $this->render('account/account.html.twig', [

        ]);
    }

    /**
     * @Route("/account/edit", name="account-edit")
     */
    public function editAction(Request $request)
    {
        /** @var User $user */
        $user = $this->get('security.token_storage')->getToken()->getUser();
        $userManager = $this->container->get('fos_user.user_manager');

        $form = $this->createFormBuilder($user)
            ->add('email', EmailType::class)
            #->add('password', PasswordType::class)
            ->add('save', SubmitType::class, array('label' => 'Save'))
            ->getForm();

        $form->handleRequest($request);

        if ($form->isSubmitted()) {
            if ($form->isValid()) {
                $userManager->updateUser($user, true);
                $this->addFlash('success', 'Die Änderungen wurden gespeichert.');
            }
            else {
                $this->addFlash('error', 'Die Änderungen konnten leider nicht gespeichert werden. Evtl. sind Eingaben nicht korrekt.');
            }
        }

        return $this->render('account/user.html.twig', [
            'form' => $form->createView()
        ]);
    }


    /**
     * @Route("/account/password", name="account-password")
     */
    public function passwordAction(Request $request)
    {
        /** @var User $user */
        $user = $this->get('security.token_storage')->getToken()->getUser();
        $em = $this->getDoctrine()->getManager();
        $userManager = $this->container->get('fos_user.user_manager');

        if (empty($request->get('pw'))) {
            $this->addFlash('error', 'Das Passwort darf nicht leer sein.');
            return $this->redirectToRoute('account-edit');
        }

        // TODO weitere Passwort-Checks (nicht zu einfach!)

        if ($request->get('pw') != $request->get('pw-again')) {
            $this->addFlash('error', 'Die eingegebenen Passwörter stimmen nicht überein.');
            return $this->redirectToRoute('account-edit');
        }

        $user->setPlainPassword($request->get('pw'));
        $userManager->updateUser($user, true);


        $this->addFlash('success', 'Das Passwort wurde geändert.');
        return $this->redirectToRoute('account-edit');
    }

    /**
     * @Route("/account/organizations", name="account-organizations")
     */
    public function orgaAction(Request $request)
    {
        /** @var User $user */
        $user = $this->get('security.token_storage')->getToken()->getUser();
        $em = $this->getDoctrine()->getManager();

        $orga_new = new Organization();

        $form_create = $this->createFormBuilder($orga_new)
            ->add('Name', TextType::class, array('trim' => true))
            ->add('save', SubmitType::class, array('label' => 'Save'))
            ->getForm();
        $form_create->handleRequest($request);
        if ($form_create->isSubmitted()) {
            if ($form_create->isValid()) {
                $em->persist($orga_new);

                try {
                    $em->flush();
                    $user->addOrganization($orga_new);
                    $em->persist($user);
                    $em->flush();
                    $this->addFlash('success', 'Die neue Organisation wurde gespeichert.');
                } catch (UniqueConstraintViolationException $ex) {
                    $this->addFlash('error', 'Der gewählte Name wird bereits für eine Organisation verwendet. Bitte wähle einen anderen, eindeutigen Namen.');
                    $em = $this->getDoctrine()->resetManager();
                }
            }
            else {
                $this->addFlash('error', 'Die Organisation konnte leider nicht angelegt werden.');
            }
        }


        $orgas = $user->getOrganizations();
        return $this->render('account/organizations.html.twig', [
            'form_create' => $form_create->createView(),
            'organizations' => $orgas
        ]);<commit_msg>bugfix: add organization: user was not shown as member after submit<commit_after><?php
/**
 * Created by PhpStorm.
 * User: michi
 * Date: 07.01.17
 * Time: 20:31
 */

namespace AppBundle\Controller\Account;


use AppBundle\Entity\Organization;
use AppBundle\Entity\User;
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\HttpFoundation\Request;



class Account extends Controller
{
    /**
     * @Route("/account", name="account")
     */
    public function indexAction(Request $request)
    {
        $rep = $this->getDoctrine()->getRepository('AppBundle:Screen');
        $screens = $rep->findAll();


        return $this->render('account/account.html.twig', [

        ]);
    }

    /**
     * @Route("/account/edit", name="account-edit")
     */
    public function editAction(Request $request)
    {
        /** @var User $user */
        $user = $this->get('security.token_storage')->getToken()->getUser();
        $userManager = $this->container->get('fos_user.user_manager');

        $form = $this->createFormBuilder($user)
            ->add('email', EmailType::class)
            #->add('password', PasswordType::class)
            ->add('save', SubmitType::class, array('label' => 'Save'))
            ->getForm();

        $form->handleRequest($request);

        if ($form->isSubmitted()) {
            if ($form->isValid()) {
                $userManager->updateUser($user, true);
                $this->addFlash('success', 'Die Änderungen wurden gespeichert.');
            }
            else {
                $this->addFlash('error', 'Die Änderungen konnten leider nicht gespeichert werden. Evtl. sind Eingaben nicht korrekt.');
            }
        }

        return $this->render('account/user.html.twig', [
            'form' => $form->createView()
        ]);
    }


    /**
     * @Route("/account/password", name="account-password")
     */
    public function passwordAction(Request $request)
    {
        /** @var User $user */
        $user = $this->get('security.token_storage')->getToken()->getUser();
        $em = $this->getDoctrine()->getManager();
        $userManager = $this->container->get('fos_user.user_manager');

        if (empty($request->get('pw'))) {
            $this->addFlash('error', 'Das Passwort darf nicht leer sein.');
            return $this->redirectToRoute('account-edit');
        }

        // TODO weitere Passwort-Checks (nicht zu einfach!)

        if ($request->get('pw') != $request->get('pw-again')) {
            $this->addFlash('error', 'Die eingegebenen Passwörter stimmen nicht überein.');
            return $this->redirectToRoute('account-edit');
        }

        $user->setPlainPassword($request->get('pw'));
        $userManager->updateUser($user, true);


        $this->addFlash('success', 'Das Passwort wurde geändert.');
        return $this->redirectToRoute('account-edit');
    }

    /**
     * @Route("/account/organizations", name="account-organizations")
     */
    public function orgaAction(Request $request)
    {
        /** @var User $user */
        $user = $this->get('security.token_storage')->getToken()->getUser();
        $em = $this->getDoctrine()->getManager();

        $orga_new = new Organization();

        $form_create = $this->createFormBuilder($orga_new)
            ->add('Name', TextType::class, array('trim' => true))
            ->add('save', SubmitType::class, array('label' => 'Save'))
            ->getForm();
        $form_create->handleRequest($request);
        if ($form_create->isSubmitted()) {
            if ($form_create->isValid()) {
                $em->persist($orga_new);

                try {
                    $em->flush();
                    $user->addOrganization($orga_new);
                    $em->persist($user);
                    $em->flush();
                    $em->refresh($orga_new); // needed to notify $user that he is in a new organization
                    $this->addFlash('success', 'Die neue Organisation wurde gespeichert.');
                } catch (UniqueConstraintViolationException $ex) {
                    $this->addFlash('error', 'Der gewählte Name wird bereits für eine Organisation verwendet. Bitte wähle einen anderen, eindeutigen Namen.');
                    $em = $this->getDoctrine()->resetManager();
                }
            }
            else {
                $this->addFlash('error', 'Die Organisation konnte leider nicht angelegt werden.');
            }
        }


        $orgas = $user->getOrganizations();
        return $this->render('account/organizations.html.twig', [
            'form_create' => $form_create->createView(),
            'organizations' => $orgas
        ]);
{'fraction_non_alphanumeric': 0.1608433734939759, 'fraction_numerical': 0.002008032128514056, 'mean_word_length': 2.3823429541595926, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 148, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6710205', 'n_tokens_mistral': 2856, 'n_tokens_neox': 2790, 'n_words': 498}