prep($request); $bannerQ = $this->main->createQuery('Select b from App\Entity\Main\Banner b where b.status=:normal and b.worldId is null and b.realmId is null order by b.name'); $bannerQ->setParameter('normal','normal'); $data['banners'] = $bannerQ->getResult(); //$data['banners'] = $this->main->getRepository(Banner::class)->findBy(array('status'=>'normal', 'worldId'=>null, 'realmId'=>null)); $data['submittedBanners'] = $this->main->getRepository(Banner::class)->findBy(array('status'=>'submitted', 'worldId'=>$this->world, 'realmId'=>$this->Character->getRealm()->getId())); $activeBanners = $this->main->getRepository(Banner::class)->findBy(array('status'=>'normal', 'worldId'=>$this->world, 'realmId'=>$this->Character->getRealm()->getId())); if (count($activeBanners) > 0) { $data['activeBanner'] = $activeBanners[0]; $data['banners'] = array_merge(array($activeBanners[0]), $data['banners']); } else { $data['activeBanner'] = false; } return $this->render('command/banners.html.twig', $data); } /** * @Route("/command/bannerSelect", name="RulerBannerSelect") */ public function bannerSelect(Request $request) { $bannerId = $request->request->get('bannerId'); $Banner = $this->main->getRepository(Banner::class)->find($bannerId); if (!$this->Character->checkPosition('Ruler')) { return $this->errorRedirect('Only the ruler can change the realm\'s banner.', 'RulerBanners'); } if (!$Banner) { return $this->errorRedirect('No banner with ID '.$bannerId.' found.', 'RulerBanners'); } if ($Banner->getStatus() != 'normal') { return $this->errorRedirect('That banner is not usable by your realm.', 'RulerBanners'); } if ($Banner->getWorldId() && $Banner->getRealmId()) { $Island = new Island($Banner->getWorldId()); $Realm = $this->worlds[$Banner->getWorldId()]->getRepository(Realm::class)->find($Banner->getRealmId()); if ($Island && $Realm) { return $this->errorRedirect('That banner is already in use by '.$Realm->getName().' on '.$Island->Name, 'RulerBanners'); } else { return $this->errorRedirect('That banner is already in use, though I can\'t tell you where.', 'RulerBanners'); } } if ($this->Character->getRealm()->getLastChange() && $this->Character->getRealm()->getLastChange()) $CurrentBanner = $this->main->getRepository(Banner::class)->findOneBy(array('worldId'=>$this->world, 'realmId'=>$this->Character->getRealm()->getId())); if ($CurrentBanner) { $CurrentBanner->setWorldId(null); $CurrentBanner->setRealmId(null); } $Banner->setWorldId($this->world); $Banner->setRealmId($this->Character->getRealm()->getId()); $this->Character->getRealm()->setBanner($Banner->getBannerFile())->setIcon($Banner->getIconFile())->setLastChange(new \DateTime()); $this->main->flush(); $this->worlds[$this->world]->flush(); $this->addFlash('notice', 'The new banner has been set.'); return new RedirectResponse($this->generateUrl('RulerBanners')); } /** * @Route("/command/bannerCreate", name="RulerBannerCreate") */ public function bannerCreate(Request $request, MailerInterface $mailer, $projectDir) { $data = $this->prep($request); $bannerConstraints = new Image(['maxSize'=>'4096k','maxWidth'=>'250','minWidth'=>'50','maxHeight'=>'250','minHeight'=>'50','mimeTypes'=>['image/png'],'mimeTypesMessage'=>'Please ensure that the image is a .png file.']); $iconConstraints = new Image(['maxSize'=>'1024k','maxWidth'=>'24','minWidth'=>'24','maxHeight'=>'24','minHeight'=>'24','mimeTypes'=>['image/png'],'mimeTypesMessage'=>'Please ensure that the image is a .png file.']); $bannerData = array(); $bannerForm = $this->createFormBuilder($bannerData) ->add('bannerFile', FileType::class, ['label'=>'Banner file: ', 'required'=>true, 'mapped'=>false, 'constraints'=>$bannerConstraints]) ->add('iconFile', FileType::class, ['label'=>'Icon file: ', 'required'=>true, 'mapped'=>false, 'constraints'=>$iconConstraints]) ->add('name', TextType::class, ['label'=>'Banner name: ', 'required'=>true, 'mapped'=>false]) ->add('submit', SubmitType::class, ['label'=>'Upload Banner']) ->getForm(); $bannerForm->handleRequest($request); if ($bannerForm->isSubmitted() && $bannerForm->isValid()) { $uploadDir = $projectDir.'/var/upload/'; $name = $bannerForm->get('name')->getData(); $Banner = new Banner(); $Banner->setName($name); $Banner->setStatus('submitted'); $Banner->setWorldId($this->world); $Banner->setRealmId($this->Character->getRealm()->getId()); $safeName = preg_replace('/[^a-zA-Z0-9_-]/', '_', $name); $bannerFile = $bannerForm->get('bannerFile')->getData(); if ($bannerFile) { $imageInfo = getimagesize($bannerFile->getRealPath()); $Banner->setWidth($imageInfo[0]); $Banner->setHeight($imageInfo[1]); $originalFilename = pathinfo($bannerFile->getClientOriginalName(), PATHINFO_FILENAME); $originalExtension = pathinfo($bannerFile->getClientOriginalName(), PATHINFO_EXTENSION); $newFilename = $safeName.'_banner.'.$originalExtension; try { $bannerFile->move($uploadDir, $newFilename); } catch (Exception $e) { $this->addFlash('error', 'An error occurred while trying to upload the banner file: '.$e->getMessage()); return new RedirectResponse($this->generateUrl('RulerBanners')); } $Banner->setBannerFile($newFilename); } $iconFile = $bannerForm->get('iconFile')->getData(); if ($iconFile) { $originalFilename = pathinfo($iconFile->getClientOriginalName(), PATHINFO_FILENAME); $originalExtension = pathinfo($iconFile->getClientOriginalName(), PATHINFO_EXTENSION); $newFilename = $safeName.'_icon.'.$originalExtension; try { $iconFile->move($uploadDir, $newFilename); } catch (Exception $e) { $this->addFlash('error', 'An error occurred while trying to upload the icon file: '.$e->getMessage()); return new RedirectResponse($this->generateUrl('RulerBanners')); } $Banner->setIconFile($newFilename); } $this->main->persist($Banner); $this->main->flush(); $this->addFlash('notice', 'Your banner has now been uploaded for the Admins to review. Please be patient, as they are sometimes very busy. You will be notified when they approve or reject your banner.'); $emailData = array(); $emailData['Character'] = $this->Character; $emailData['Banner'] = $Banner; $emailData['worldId'] = $this->world; $emailData['bannerPath'] = $this->generateUrl('AdminBanners'); $email = (new TemplatedEmail()) ->from('admin@battlemaster.org') ->to('admin@battlemaster.org') ->subject('New Banner for '.$this->Character->getRealm()->getName()) ->textTemplate('email/newBanner.txt.twig') ->context($emailData); $mailer->send($email); } $data['bannerForm'] = $bannerForm->createView(); return $this->render('command/bannerCreate.html.twig', $data); } }