src/Controller/TeacherController.php line 647

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Task;
  4. use App\Entity\ExamTemplate;
  5. use App\Entity\Attribute;
  6. use App\Entity\AttributeValue;
  7. use App\Entity\City;
  8. use App\Entity\County;
  9. use App\Entity\School;
  10. use App\Entity\Teacher;
  11. use App\Entity\Voivodeship;
  12. use App\Service\Helpers;
  13. use DateTime;
  14. use PHPUnit\TextUI\Help;
  15. use Symfony\Component\Mailer\Transport;
  16. use Symfony\Component\Mailer\Mailer;
  17. use Symfony\Component\Mime\Email;
  18. use Symfony\Component\Mailer\MailerInterface;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  21. use Symfony\Component\Routing\Annotation\Route;
  22. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  23. use Symfony\Component\Security\Core\User\UserInterface;
  24. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  25. use Symfony\Component\HttpFoundation\JsonResponse;
  26. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  27. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  28. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  29. use Doctrine\Persistence\ManagerRegistry;
  30. /** @Route("/nauczyciel", name="teacher_") */
  31. class TeacherController extends AbstractController
  32. {
  33.    
  34.     private $entityManager;
  35.     public function __construct(ManagerRegistry $entityManager)
  36.     {
  37.         $this->em $entityManager;
  38.     }
  39.     /**
  40.      * @Route("/tokeny", name="token_reindex")
  41.      * @IsGranted("ROLE_ADMIN")
  42.      */
  43.     public function tokenReindex(Request $request)
  44.     {
  45.         $criteria = array();
  46.         $teachers $this->em->getRepository(Teacher::class)->findByCriteria($criteria);
  47.         foreach ($teachers as $teacher){
  48.             $token null;
  49.             while($token==null){
  50.                 $tokenTmp $teacher->generateToken();
  51.                 $c = array();
  52.                 $c['token'] = $tokenTmp;
  53.                 $c['single'] = true;
  54.                 $teacherTmp $this->em->getRepository(Teacher::class)->findByCriteria($c);
  55.                 if($teacherTmp == null){
  56.                     $token=$tokenTmp;
  57.                 }
  58.             }
  59.             $teacher->setOption('token',$token);
  60.             $this->em->getManager()->persist($teacher);
  61.             $this->em->getManager()->flush();
  62.         }
  63.     }
  64.     /**
  65.      * @Route("/", name="index")
  66.      * @IsGranted("ROLE_ADMIN")
  67.      */
  68.     public function index(Request $request)
  69.     {
  70.         $criteria = array();
  71.         
  72.         $voivodeships  $this->em->getRepository(Voivodeship::class)->findBy(array(),array('name'=>'asc'));
  73.         $countys=array();
  74.         $cities=array();
  75.         $schools=array();
  76.         if($request->get('voivodeship')){
  77.             $voivodeship $this->em->getRepository(Voivodeship::class)->find($request->get('voivodeship'));
  78.             $countys  $this->em->getRepository(County::class)->findBy(array("voivodeship"=>$voivodeship),array('name'=>'asc'));
  79.             if($request->get('county')){
  80.                 $county $this->em->getRepository(County::class)->find($request->get('county'));
  81.                 $cities  $this->em->getRepository(City::class)->findBy(array("county"=>$county),array('name'=>'asc'));
  82.                 if($request->get('city')){
  83.                     $city $this->em->getRepository(City::class)->find($request->get('city'));
  84.                     $schools  $this->em->getRepository(School::class)->findBy(array("city"=>$city),array('name'=>'asc'));
  85.                 }
  86.             }
  87.         }
  88.         $criteria['voivodeship']=$request->get('voivodeship');
  89.         $criteria['county']=$request->get('county');
  90.         $criteria['city']=$request->get('city');
  91.         $criteria['school']=$request->get('school');
  92.         $criteria['level']=$request->get('level');
  93.         $criteria['subject']=$request->get('subject');
  94.         $criteria['status']=$request->get('status');
  95.         $criteria['email']=$request->get('email');
  96.         $criteria['name']=$request->get('name');
  97.         $criteria['count']=true;
  98.         $count $this->em->getRepository(Teacher::class)->findByCriteria($criteria);
  99.         $criteria['page']=$request->get('page',1);
  100.         unset($criteria['count']);
  101.         $teachers $this->em->getRepository(Teacher::class)->findByCriteria($criteria);
  102.         $attribute  $this->em->getRepository(Attribute::class)->find(1);
  103.         $subjects  $this->em->getRepository(AttributeValue::class)->findBy(array("attribute"=>$attribute));
  104.         return $this->render('teacher/index.html.twig', [
  105.             "teachers"=>$teachers,
  106.             "voivodeships"=>$voivodeships,
  107.             "countys"=>$countys,
  108.             "cities"=>$cities,
  109.             "schools"=>$schools,
  110.             "subjects"=>$subjects,
  111.             "criteria"=>$criteria,
  112.             "count"=>$count
  113.         ]);
  114.     }
  115.     /**
  116.      * @Route("/pobierz-nauczycieli", name="get_teachers")
  117.      * @IsGranted("ROLE_ADMIN")
  118.      */
  119.     public function getTeachers(Request $request)
  120.     {
  121.         $criteria $request->get('criteria');
  122.         $teachers $this->em->getRepository(Teacher::class)->findByCriteria($criteria);
  123.         $attribute  $this->em->getRepository(Attribute::class)->find(1);
  124.         $subjects  $this->em->getRepository(AttributeValue::class)->findBy(array("attribute"=>$attribute));
  125.         return $this->render('teacher/getteachers.html.twig', [
  126.             "teachers"=>$teachers,
  127.         ]);
  128.     }
  129.     /**
  130.      * @Route("/dodaj", name="add")
  131.      * @IsGranted("ROLE_ADMIN")
  132.      */
  133.     public function add()
  134.     {
  135.         return $this->render('teacher/add.html.twig', [
  136.         ]);
  137.     }
  138.     /**
  139.      * @Route("/aktywacja", name="activation")
  140.      *
  141.      */
  142.     public function activation(Request $request)
  143.     {
  144.         $manager $this->em->getManager();
  145.         $teacher $this->getUser();
  146.         if($teacher->getOption('preregistered')){
  147.             $teacher->setRoles(["ROLE_TEACHER"]);
  148.             $teacher->setStatus(2);
  149.             $teacher->setOption("agreement_1",$request->get('agreement_1',0));
  150.             $teacher->setOption("agreement_2",$request->get('agreement_2',0));
  151.             $teacher->setOption("agreement_3",$request->get('agreement_3',0));
  152.             $teacher->setOption("agreement_4",$request->get('agreement_4',0));
  153.             $manager->persist($teacher);
  154.             $manager->flush();
  155. //            $message = (new Email())
  156. //                ->from('rejestracja@generatorklasowek.pl')
  157. //                ->to($teacher->getUsername())
  158. //                ->subject('Aktywacja konta w Generatorze Klasówek')
  159. //                ->html(
  160. //                    $this->renderView(
  161. //                        'emails/activation.html.twig',
  162. //                        ['teacher' => $teacher]
  163. //                    ),
  164. //                    'text/html'
  165. //                )
  166. //
  167. //                // you can remove the following code if you don't define a text version for your emails
  168. //                ->text(
  169. //                    $this->renderView(
  170. //                        'emails/activation.txt.twig',
  171. //                        ['teacher' => $teacher]
  172. //                    ),
  173. //                    'text/plain'
  174. //                );
  175. //            $mailer->send($message);
  176.             $token = new UsernamePasswordToken(
  177.                 $teacher,
  178.                 $teacher->getPassword(),
  179.                 'secured',
  180.                 $teacher->getRoles()
  181.             );
  182.             $this->container->get('security.token_storage')->setToken($token);
  183.         }
  184.         return $this->redirectToRoute('home');
  185.     }
  186.     /**
  187.      * @Route("/edytuj/{id}", name="edit")
  188.      * @IsGranted("ROLE_ADMIN")
  189.      */
  190.     public function edit()
  191.     {
  192.         return $this->render('teacher/edit.html.twig', [
  193.         ]);
  194.     }
  195.     /**
  196.      * @Route("/profil", name="profile")
  197.      * @IsGranted({"ROLE_TEACHER"})
  198.      */
  199.     public function profile(Request $request,UserPasswordEncoderInterface $passwordEncoder)
  200.     {
  201.         $teacher $this->getUser();
  202.         if($request->isMethod('post')){
  203.             $manager $this->em->getManager();
  204.             if($request->get('username')!=$teacher->getUserName()){
  205.                 $history $teacher->getOption('emails_history',array());
  206.                 $history[date('Y-m-d H:i:s')]['old_email']=$teacher->getUserName();
  207.                 $history[date('Y-m-d H:i:s')]['new_email']=$request->get('username');
  208.                 $teacher->setOption('emails_history',$history);
  209.             }
  210.             $teacher->setUsername($request->get('username'));
  211.             $teacher->setFirstName($request->get('first_name'));
  212.             $teacher->setLastName($request->get('last_name'));
  213.             if($request->get('passwd')){
  214.                 $teacher->setPassword($passwordEncoder->encodePassword($teacher,$request->get('passwd')));
  215.             }
  216.             $s $request->get('school');
  217.             foreach($s as $key=>$value){
  218.                 if(!is_array($value)){
  219.                     $s[$key]=trim($value);
  220.                 }
  221.             }
  222.             if($s['name']){
  223.                 $school = new School();
  224.                 $school->setVoivodeship($this->em->getRepository(Voivodeship::class)->find($request->get('voivodeship')));
  225.                 $school->setCounty($this->em->getRepository(County::class)->find($request->get('county')));
  226.                 $school->setCity($this->em->getRepository(City::class)->find($request->get('city')));
  227.                 $school->setName($s['name']);
  228.                 $school->setPostcode($s['postcode']);
  229.                 $school->setStreet($s['street']);
  230.                 $school->setPhone($s['phone']);
  231.                 $school->setEmail($s['email']);
  232.                 $school->setLevel($s['level']);
  233.                 $school->setIsValid(false);
  234.                 $manager->persist($school);
  235.                 $teacher->setSchool($school);
  236.             }
  237.             elseif($request->get('school_id')){
  238.                 $school $this->em->getRepository(School::class)->find($request->get('school_id'));
  239.                 $teacher->setSchool($school);
  240.             }
  241.             $options $teacher->getOptions();
  242.             $optionsPost $request->get('options');
  243.             $options['agreement_1']=isset($optionsPost['agreement_1'])?$optionsPost['agreement_1']:0;
  244.             $options['agreement_2']=isset($optionsPost['agreement_2'])?$optionsPost['agreement_2']:0;
  245.             $options['agreement_3']=isset($optionsPost['agreement_3'])?$optionsPost['agreement_3']:0;
  246.             $teacher->setOptions($options);
  247.             $manager->persist($teacher);
  248.             $manager->flush();
  249.           }
  250.         return $this->render('teacher/profile.html.twig', [
  251.             "teacher"=>$teacher
  252.         ]);
  253.     }
  254.     /**
  255.      * @Route("/usun/{id}", name="remove")
  256.      * @IsGranted("ROLE_ADMIN")
  257.      */
  258.     public function remove(Request $request)
  259.     {
  260.         $manager $this->em->getManager();
  261.         $teacher $this->em->getRepository(Teacher::class)->find($request->get('id'));
  262.         $teacher->setStatus(5);
  263.         $manager->persist($teacher);
  264.         $manager->flush();
  265.         return $this->redirectToRoute('teacher_index');
  266.     }
  267.     /**
  268.      * @Route("/zapisz", name="update")
  269.      * @IsGranted("ROLE_ADMIN")
  270.      */
  271.     public function update(Request $request,UserPasswordEncoderInterface $passwordEncoder)
  272.     {
  273.         $manager $this->em->getManager();
  274.         if($request->get('id')){
  275.             $teacher $this->em->getRepository(Teacher::class)->find($request->get('id'));
  276.         }
  277.         else{
  278.             $passwd Helpers::randomPassword();
  279.             $teacher = new Teacher();
  280.             $teacher->setExpireAt(new DateTime("+12 months"));
  281.             $teacher->setCreatedAt(new DateTime("now"));
  282.             $teacher->setPassword($passwordEncoder->encodePassword($teacher,$passwd));
  283.         }
  284.         $teacher->setFirstName($request->get('first_name'));
  285.         $teacher->setLastName($request->get('last_name'));
  286.         $teacher->setUsername($request->get('username'));
  287.         if($request->get('passwd')){
  288.             $teacher->setPassword($passwordEncoder->encodePassword($teacher,$request->get('passwd')));
  289.         }
  290.         if($request->get('status')==and $request->get('status')!=$teacher->getStatus()){
  291.             // niezweryfikowany
  292.             $teacher->setRoles(["ROLE_GUEST"]);
  293.         }
  294.         elseif($request->get('status')==and $request->get('status')!=$teacher->getStatus()){
  295.             // aktywowany
  296.             $teacher->setRoles(["ROLE_TEACHER"]);
  297. //            $message = (new Email())
  298. //                ->subject('Aktywacja konta w Generatorze Klasówek')
  299. //                ->from('rejestracja@generatorklasowek.pl')
  300. //                ->to($teacher->getUsername())
  301. //                ->html(
  302. //                    $this->renderView(
  303. //                        'emails/activation.html.twig',
  304. //                        ['teacher' => $teacher]
  305. //                    ),
  306. //                    'text/html'
  307. //                )
  308. //
  309. //                // you can remove the following code if you don't define a text version for your emails
  310. //                ->text(
  311. //                    $this->renderView(
  312. //                        'emails/activation.txt.twig',
  313. //                        ['teacher' => $teacher]
  314. //                    ),
  315. //                    'text/plain'
  316. //                )
  317. //            ;
  318. //
  319. //            $mailer->send($message);
  320.         }
  321.         elseif($request->get('status')==and $request->get('status')!=$teacher->getStatus()){
  322.             // zablokowany
  323.             $teacher->setRoles(["ROLE_GUEST"]);
  324.         }
  325.         elseif($request->get('status')==4){
  326.             // oczekujący
  327.             $teacher->setRoles(["ROLE_GUEST"]);
  328.         }
  329.         $teacher->setStatus($request->get('status'));
  330.         
  331.         if($request->get("school_id") && !$request->get('school')['name']){
  332.             $school $this->em->getRepository(School::class)->find($request->get('school_id'));
  333.         } else if($request->get("school_id") == 0) {
  334.             $school $teacher->getSchool();
  335.             if($school->getIsValid() !== 1) {
  336.                 $school->setIsValid(1);    
  337.                 $manager->persist($school);
  338.             }
  339.             
  340.         }
  341.         else{
  342.             $s $request->get('school');
  343.             $school = new School();
  344.             $school->setVoivodeship($this->em->getRepository(Voivodeship::class)->find($request->get('voivodeship')));
  345.             $school->setCounty($this->em->getRepository(County::class)->find($request->get('county')));
  346.             $school->setCity($this->em->getRepository(City::class)->find($request->get('city')));
  347.             $school->setName($s['name']);
  348.             $school->setPostcode($s['postcode']);
  349.             $school->setStreet($s['street']);
  350.             $school->setPhone($s['phone']);
  351.             $school->setEmail($s['email']);
  352.             $school->setLevel($s['level']);
  353.             $school->setIsValid(true);
  354.             $manager->persist($school);
  355.         }
  356.         $teacher->setSchool($school);
  357.         $post=$request->get('options');
  358.         $options=$teacher->getOptions();
  359.         foreach($post['subject'] as $key=>$value){
  360.             $options['subjects'][]=$key;
  361.         }
  362.         $options['note']=$post['note'];
  363.         $options['codes']=$post['codes'];
  364.         $teacher->setOptions($options);
  365.         $manager->persist($teacher);
  366.         $manager->flush();
  367.         return $this->redirectToRoute('teacher_index');
  368.     }
  369.     /**
  370.      * @Route("/przypomnij-haslo", name="remind_password")
  371.      */
  372.     public function remindPassword(Request $request,UserPasswordEncoderInterface $passwordEncoderMailerInterface $mailer){
  373.         $teacher=$this->em->getRepository(Teacher::class)->find($request->get('teacher_id'));
  374.         if($teacher instanceof Teacher){
  375.             $manager $this->em->getManager();
  376.             $passwd Helpers::randomPassword();
  377.             $teacher->setPassword($passwordEncoder->encodePassword($teacher,$passwd));
  378.             $manager->persist($teacher);
  379.             $manager->flush();
  380.             $message = (new Email())
  381.                 ->subject('Zmiana hasła w Generatorze Klasówek')
  382.                 ->from('rejestracja@generatorklasowek.pl')
  383.                 ->to($teacher->getUsername())
  384.                 ->html(
  385.                     $this->renderView(
  386.                     // templates/emails/registration.html.twig
  387.                         'emails/remind.html.twig',
  388.                         ['teacher' => $teacher,'passwd'=> $passwd]
  389.                     ),
  390.                     'text/html'
  391.                 )
  392.                 // you can remove the following code if you don't define a text version for your emails
  393.                 ->text(
  394.                     $this->renderView(
  395.                     // templates/emails/registration.txt.twig
  396.                         'emails/remind.txt.twig',
  397.                         ['teacher' => $teacher,'passwd'=> $passwd]
  398.                     ),
  399.                     'text/plain'
  400.                 )
  401.             ;
  402.             $mailer->send($message);
  403.             return $this->redirectToRoute('teacher_show',array('id'=>$teacher->getId()));
  404.         }
  405.         return $this->redirectToRoute('teacher_index');
  406.     }
  407.     public function form(Request $requestHelpers $helper)
  408.     {
  409.         $teacher null;
  410.         $attribute  $this->em->getRepository(Attribute::class)->find(1);
  411.         $subjects  $this->em->getRepository(AttributeValue::class)->findBy(array("attribute"=>$attribute));
  412.         if($request->get('id')){
  413.             $teacher $this->em->getRepository(Teacher::class)->find($request->get('id'));
  414.         }
  415.         $books $helper->getBooks();
  416.         return $this->render('teacher/form.html.twig', [
  417.             "subjects"=>$subjects,
  418.             "teacher"=>$teacher,
  419.             "books"=>$books
  420.         ]);
  421.     }
  422.     /**
  423.      * @Route("/pokaz/{id}", name="show")
  424.      * @IsGranted("ROLE_ADMIN")
  425.      */
  426.     public function show(Request $request)
  427.     {
  428.         $teacher $this->em->getRepository(Teacher::class)->find($request->get('id'));
  429.         $attribute  $this->em->getRepository(Attribute::class)->find(1);
  430.         $subjects  $this->em->getRepository(AttributeValue::class)->findBy(array("attribute"=>$attribute));
  431.         if($request->get('expire_at')){
  432.             $em $this->em->getManager();
  433.             $teacher->setExpireAt(new DateTime($request->get('expire_at')));
  434.             $em->persist($teacher);
  435.             $em->flush();
  436.         }
  437.         return $this->render('teacher/show.html.twig', [
  438.             "subjects"=>$subjects,
  439.             "teacher"=>$teacher
  440.         ]);
  441.     }
  442.     /**
  443.      * @Route("/hurtowe-przedluzanie-waznosci-kont", name="bulk")
  444.      * @IsGranted("ROLE_ADMIN")
  445.      */
  446.     public function bulk(Request $request)
  447.     {
  448.         $teachers $this->em->getRepository(Teacher::class)->findAll();
  449.         if($request->get('expire_at')){
  450.             $em $this->em->getManager();
  451.             foreach($teachers as $teacher){
  452.                 if($teacher->getStatus()==2){
  453.                     $teacher->setExpireAt(new DateTime($request->get('expire_at')));
  454.                     $em->persist($teacher);
  455.                 }
  456.             }
  457.             $em->flush();
  458.         }
  459.         return $this->render('teacher/bulk.html.twig', [
  460.         ]);
  461.     }
  462.     /**
  463.      * @Route("/wybierz-szkole", name="get_school")
  464.      */
  465.     public function getSchool(Request $request)
  466.     {
  467.         $voivodeships  $this->em->getRepository(Voivodeship::class)->findBy(array(),array('name'=>'asc'));
  468.         $countys=array();
  469.         $cities=array();
  470.         $schools=array();
  471.         $school=null;
  472.         if($request->get('school_id')){
  473.             $school $this->em->getRepository(School::class)->find($request->get('school_id'));
  474.         }
  475.         if($request->get('voivodeship',($school instanceof School)?$school->getVoivodeship()->getId():null)) {
  476.             $voivodeship $this->em->getRepository(Voivodeship::class)->find($request->get('voivodeship',($school instanceof School)?$school->getVoivodeship()->getId():null));
  477.             $countys $this->em->getRepository(County::class)->findBy(array("voivodeship" => $voivodeship), array('name' => 'asc'));
  478.         }
  479.         if($request->get('county',($school instanceof School)?$school->getCounty()->getId():null)) {
  480.             $county $this->em->getRepository(County::class)->find($request->get('county',($school instanceof School)?$school->getCounty()->getId():null));
  481.             $cities $this->em->getRepository(City::class)->findBy(array("county" => $county), array('name' => 'asc'));
  482.         }
  483.         if($request->get('city',($school instanceof School)?$school->getCity()->getId():null)) {
  484.             $city $this->em->getRepository(City::class)->find($request->get('city',($school instanceof School)?$school->getCity()->getId():null));
  485.             $schools $this->em->getRepository(School::class)->findBy(array("city" => $city), array('name' => 'asc'));
  486.         }
  487.         return $this->render('teacher/getschool.html.twig', [
  488.             "voivodeships"=>$voivodeships,
  489.             "countys"=>$countys,
  490.             "cities"=>$cities,
  491.             "schools"=>$schools,
  492.             "school"=>$school
  493.         ]);
  494.     }
  495.     /**
  496.      * @Route("/logowanie", name="login")
  497.      */
  498.     public function login(AuthenticationUtils $authenticationUtils){
  499.         $user $this->getUser();
  500.         if($user) {
  501.             $auth_checker $this->get('security.authorization_checker');
  502.             //$isRoleUser = $auth_checker->isGranted('ROLE_USER');
  503.             if($auth_checker->isGranted('ROLE_TEACHER') and $user->getStatus()==5){
  504.                 return $this->redirectToRoute('logout');
  505.             }
  506.             else{
  507.                 return $this->redirectToRoute('home');
  508.             }
  509.         }
  510.         return $this->render('/widget/login.html.twig',
  511.             [
  512.                 'last_username' => $authenticationUtils->getLastUsername(),
  513.                 'error' => $authenticationUtils->getLastAuthenticationError()
  514.             ]);
  515.     }
  516.     /**
  517.      * @Route("/logout",name="logout")
  518.      */
  519.     public function logout(){
  520.     }
  521.     /**
  522.      * @Route("/przypominanie-hasla", name="password_remind")
  523.      *
  524.      */
  525.     public function passwordRemind(Request $requestMailerInterface $mailer){
  526.         $error null;
  527.         if($request->getMethod()=='POST'){
  528.             $criteria =array();
  529.             $criteria['username']=trim($request->get('email'));
  530.             $teacher=$this->em->getRepository(Teacher::class)->findOneBy($criteria);
  531.             if($teacher instanceof Teacher) {
  532.                 $hash md5($teacher->getSalt() . $teacher->getUsername());
  533.                 $message = (new Email())
  534.                     ->subject('Zmiana hasła w Generatorze Klasówek')
  535.                     ->from('rejestracja@generatorklasowek.pl')
  536.                     ->to(trim($teacher->getUsername()))
  537.                     ->html(
  538.                         $this->renderView(
  539.                         // templates/emails/registration.html.twig
  540.                             'emails/changePassword.html.twig',
  541.                             ['teacher' => $teacher'hash' => $hash]
  542.                         ),
  543.                         'text/html'
  544.                     )
  545.                     ->text(
  546.                         $this->renderView(
  547.                         // templates/emails/registration.txt.twig
  548.                             'emails/changePassword.txt.twig',
  549.                             ['teacher' => $teacher'hash' => $hash]
  550.                         ),
  551.                         'text/plain'
  552.                     );
  553.                 $mailer->send($message);
  554.             }
  555.         }
  556.         return $this->render('/teacher/passwordRemind.html.twig', [
  557.             'error'=>$error
  558.         ]);
  559.     }
  560.     /**
  561.      * @Route("/zmiana-hasla", name="password_change")
  562.      *
  563.      */
  564.     public function passwordChange(Request $requestMailerInterface $mailer,UserPasswordEncoderInterface $passwordEncoder){
  565.         $error null;
  566.         if($request->getMethod()=='POST'){
  567.             $teacher $this->em->getRepository(Teacher::class)->find($request->get('id'));
  568.             $em $this->em->getManager();
  569.             if(!$request->get('passwd')){
  570.                 $error 'Zdefiniuj nowe hasło';
  571.             }
  572.             else{
  573.                 if($teacher instanceof Teacher and md5($teacher->getSalt().$teacher->getUsername())==$request->get('hash')){
  574.                     $teacher->setPassword($passwordEncoder->encodePassword($teacher,$request->get('passwd')));
  575.                     $em->persist($teacher);
  576.                     $em->flush();
  577.                 }
  578.             }
  579.         }
  580.         return $this->render('/teacher/passwordChange.html.twig', [
  581.             'error'=>$error
  582.         ]);
  583.     }
  584.     /**
  585.      * @Route("/rejestracja", name="register")
  586.      *
  587.      */
  588.     public function register(Request $requestUserPasswordEncoderInterface $passwordEncoderMailerInterface $mailer){
  589.         if($request->isXmlHttpRequest()){
  590.             $manager $this->em->getManager();
  591.             // początek weryfikacji
  592.             $errors = array();
  593.             $t $request->get('teacher');
  594.             foreach($t as $key=>$value){
  595.                 if(!is_array($value)){
  596.                     $t[$key]=trim($value);
  597.                 }
  598.             }
  599.             $s $request->get('school');
  600.             foreach($s as $key=>$value){
  601.                 if(!is_array($value)){
  602.                     $s[$key]=trim($value);
  603.                 }
  604.             }
  605.             $t['username'] = strtolower($t['username']);
  606.             if(!$t['first_name']) $errors['first_name']="Podaj imię";
  607.             if(!$t['last_name']) $errors['last_name']="Podaj nazwisko";
  608.             if(!$t['username']) $errors['username']="Podaj adres e-mail";
  609.             if(empty($t['subject'])) $errors['subject']="Wybierz przedmiot";
  610.             $teacher $this->em->getRepository(Teacher::class)->findOneBy(array("username"=>$t['username']));
  611.             if($teacher instanceof Teacher){
  612.                 $errors['username']="Podany adres e-mail został już użyty do rejestracji.";
  613.             }
  614.             if($s['name']){
  615.                 if(!$s['name']) $errors['school_name']="Podaj nazwę szkoły";
  616.                 if(!$s['postcode']) $errors['school_postcode']="Podaj kod pocztowy dla szkoły";
  617.                 if(!$s['street']) $errors['school_street']="Podaj ulicę szkoły";
  618.                 if(!$s['phone']) $errors['school_phone']="Podaj telefon do szkoły";
  619.                 if(!$s['email']) $errors['school_email']="Podaj email do szkoły";
  620.                 if(!$request->get('voivodeship')) $errors['voivodeship']="Wybierz wojeództwo w jakim znajduje się Twoja szkoła";
  621.                 if(!$request->get('county')) $errors['county']="Wybierz powiat w jakim znajduje się Twoja szkoła";
  622.                 if(!$request->get('city')) $errors['city']="Wybierz miejscowość w jakiej znajduje się Twoja szkoła";
  623.             }
  624.             else{
  625.                 if(!$request->get('school_id')) $errors['school_id']="Wybierz szkołę lub dodaj nową";
  626.             }
  627.             if(!$request->get('agreement_4')) $errors['agreement_4']="Zapoznaj się z regulaminem i zaakceptuj jego warunki.";
  628.             if(!$errors){
  629.                 if($s['name']){
  630.                     $school = new School();
  631.                     $school->setVoivodeship($this->em->getRepository(Voivodeship::class)->find($request->get('voivodeship')));
  632.                     $school->setCounty($this->em->getRepository(County::class)->find($request->get('county')));
  633.                     $school->setCity($this->em->getRepository(City::class)->find($request->get('city')));
  634.                     $school->setName($s['name']);
  635.                     $school->setPostcode($s['postcode']);
  636.                     $school->setStreet($s['street']);
  637.                     $school->setPhone($s['phone']);
  638.                     $school->setEmail($s['email']);
  639.                     $school->setLevel($s['level']);
  640.                     $school->setIsValid(false);
  641.                     $manager->persist($school);
  642.                 }
  643.                 else {
  644.                     $school $this->em->getRepository(School::class)->find($request->get("school_id"));
  645.                 }
  646.                 $passwd Helpers::randomPassword();
  647.                 $teacher = new Teacher();
  648.                 $teacher->setExpireAt(new DateTime("+12 months"));
  649.                 $teacher->setCreatedAt(new DateTime("now"));
  650.                 $teacher->setPassword($passwordEncoder->encodePassword($teacher,$passwd));
  651.                 $teacher->setStatus(1);
  652.                 $teacher->setSchool($school);
  653.                 $teacher->setFirstName($t['first_name']);
  654.                 $teacher->setLastName($t['last_name']);
  655.                 $teacher->setUsername($t['username']);
  656.                 $teacher->setRoles(["ROLE_GUEST"]);
  657.                 $options=array();
  658.                 foreach($t['subject'] as $key=>$value){
  659.                     $options['subjects'][]=$key;
  660.                 }
  661.                 $options['agreement_1'] = $request->get('agreement_1',0);
  662.                 $options['agreement_2'] = $request->get('agreement_2',0);
  663.                 $options['agreement_3'] = $request->get('agreement_3',0);
  664.                 $teacher->setOptions($options);
  665.                 $manager->persist($teacher);
  666.                 $manager->flush();
  667.                 //wysłanie e-mail z hasłem
  668.                 $message = (new Email())
  669.                     ->from('rejestracja@generatorklasowek.pl')
  670.                     ->subject('Rejestracja w Generatorze Klasówek')
  671.                     ->to($t['username'])
  672.                     ->html(
  673.                         $this->renderView(
  674.                         // templates/emails/registration.html.twig
  675.                             'emails/registration.html.twig',
  676.                             ['teacher' => $teacher,'passwd'=> $passwd]
  677.                         ),
  678.                         'text/html'
  679.                     )
  680.                     // you can remove the following code if you don't define a text version for your emails
  681.                     ->text(
  682.                         $this->renderView(
  683.                         // templates/emails/registration.txt.twig
  684.                             'emails/registration.txt.twig',
  685.                             ['teacher' => $teacher,'passwd'=> $passwd]
  686.                         ),
  687.                         'text/plain'
  688.                     )
  689.                 ;
  690.                 $mailer->send($message);
  691.                 return $response = new JsonResponse(['success'=>1]);
  692.             }
  693.             else{
  694.                 return $response = new JsonResponse(['errors'=>$errors,'success'=>0]);
  695.             }
  696.             // koniec weryfikacji
  697.         }
  698.         $attribute  $this->em->getRepository(Attribute::class)->find(1);
  699.         $subjects  $this->em->getRepository(AttributeValue::class)->findBy(array("attribute"=>$attribute));
  700.         return $this->render('/teacher/register.html.twig',
  701.             [
  702.                 "subjects"=>$subjects,
  703.                 "attribute"=>$attribute
  704.             ]);
  705.     }
  706.     /**
  707.      * @Route("/dziekujemy-za-rejestracje", name="thank_you")
  708.      *
  709.      */
  710.     public function thankyou(){
  711.         return $this->render('/teacher/thankyou.html.twig',
  712.             [
  713.             ]);
  714.     }
  715.     /**
  716.      * @Route("/import-kodow", name="code_import")
  717.      * @IsGranted("ROLE_ADMIN")
  718.      */
  719.     public function codeimport(Request $request){
  720.         if($request->get('codes') or $request->files->get('file')){
  721.             $em $this->em->getManager();
  722.             if($request->get('codes')){
  723.                 $rows explode("\n",$request->get('codes'));
  724.                 $header explode(";",trim($rows[0]));
  725.             }
  726.             elseif($request->files->get('file')){
  727.                 $file $request->files->get('file');
  728.                 $content file_get_contents($file->getPathname());
  729.                 $rows explode("\n",$content);
  730.                 $header explode(";",trim($rows[0]));
  731.             }
  732.             $i=0;
  733.             foreach($rows as $row){
  734.                 if($i>0){
  735.                     $data explode(";",$row);
  736.                     $email strtolower($data[0]);
  737.                     $teacher $this->getDoctrine()->getRepository(Teacher::class)->findOneBy(array("username"=>$email));
  738.                     if($teacher instanceof Teacher){
  739.                         $codes = array();
  740.                         foreach($data as $key=>$value){
  741.                             $codes[$header[$key]]=$value;
  742.                         }
  743.                         $teacher->setOption('codes',$codes);
  744.                         $em->persist($teacher);
  745.                     }
  746.                 }
  747.                 $i++;
  748.             }
  749.             $em->flush();
  750.         }
  751.         return $this->render('/teacher/codeimport.html.twig',
  752.             [
  753.             ]);
  754.     }
  755. }