public function handleUploadImageFile(WebsiteOptions $options, UploadedFile $uploadedFile, $imageStr) { if ($uploadedFile->getMimeType() == 'image/png' || $uploadedFile->getMimeType() == 'image/jpg' || $uploadedFile->getMimeType() == 'image/jpeg') { $newFileName = sha1(uniqid(mt_rand(), true)) . '.' . $uploadedFile->guessExtension(); $oldFileName = null; $getImageValue = 'get' . ucfirst($imageStr); $setImageValue = 'set' . ucfirst($imageStr); $uploadDir = $this->webDir . DIRECTORY_SEPARATOR . $options->getUploadDir(); if (!file_exists($uploadDir)) { mkdir($uploadDir, 0777, true); } $realpathUploadRootDir = realpath($uploadDir); if (false === $realpathUploadRootDir) { throw new \Exception(sprintf("Invalid upload root dir '%s'for uploading website images.", $uploadDir)); } if ($options->{$getImageValue}() !== $newFileName) { $oldFileName = $options->{$getImageValue}(); $options->{$setImageValue}($newFileName); try { $uploadedFile->move($realpathUploadRootDir, $newFileName); $this->om->persist($options); $this->om->flush(); } catch (\Exception $e) { if (file_exists($realpathUploadRootDir . DIRECTORY_SEPARATOR . $newFileName)) { unlink($realpathUploadRootDir . DIRECTORY_SEPARATOR . $newFileName); } $options->{$setImageValue}($oldFileName); throw new \InvalidArgumentException($e->getMessage()); } if (null !== $oldFileName && !filter_var($oldFileName, FILTER_VALIDATE_URL) && file_exists($realpathUploadRootDir . DIRECTORY_SEPARATOR . $oldFileName)) { unlink($realpathUploadRootDir . DIRECTORY_SEPARATOR . $oldFileName); } } return array($imageStr => $options->getWebPath($imageStr)); } else { throw new \InvalidArgumentException(); } }
/** * @ORM\PrePersist */ public function createOptionsAndRoot(LifecycleEventArgs $event) { $em = $event->getEntityManager(); $rootPage = $this->getRoot(); $options = $this->getOptions(); if ($rootPage == null) { $rootPage = new WebsitePage(); $rootPage->setWebsite($this); $rootPage->setIsSection(true); $rootPage->setTitle($this->getResourceNode()->getName()); $rootPage->setType(WebsitePageTypeEnum::ROOT_PAGE); $this->setRoot($rootPage); } if ($rootPage->getId() == null) { $em->getRepository('IcapWebsiteBundle:WebsitePage')->persistAsFirstChild($rootPage); } if ($options == null) { $options = new WebsiteOptions(); $options->setWebsite($this); $this->setOptions($options); } if ($options->getId() == null) { $em->persist($options); } /*if ($rootPage != null || $options != null) { $em->flush(); }*/ }
/** * Imports website object from array * (see WebsiteImporter for structure and description). * * @param array $data * @param $rootPath * @param $test * * @return Website */ public function importWebsite(array $data, $rootPath, $test = false) { $website = new Website($test); if (isset($data['data'])) { $websiteData = $data['data']; $websiteOptions = new WebsiteOptions(); $websiteOptions->setWebsite($website); $website->setOptions($websiteOptions); $websitePagesMap = array(); foreach ($websiteData['pages'] as $websitePage) { $entityWebsitePage = new WebsitePage(); $entityWebsitePage->setWebsite($website); $entityWebsitePage->importFromArray($websitePage, $rootPath); if ($websitePage['is_root']) { $website->setRoot($entityWebsitePage); $this->om->persist($website); //$this->websitePageRepository->persistAsFirstChild($entityWebsitePage); } else { $entityWebsitePageParent = $websitePagesMap[$websitePage['parent_id']]; $entityWebsitePage->setParent($entityWebsitePageParent); $this->websitePageRepository->persistAsLastChildOf($entityWebsitePage, $entityWebsitePageParent); } if ($websitePage['is_homepage']) { $website->setHomePage($entityWebsitePage); } $websitePagesMap[$websitePage['id']] = $entityWebsitePage; } $this->om->flush(); $websiteOptions->importFromArray($this->webDir, $websiteData['options'], $rootPath); } return $website; }