コード例 #1
0
 /**
  * @param string $text    The text to slugify
  * @param string $default The default alternative
  * @param string $result  The slug it should generate
  *
  * @dataProvider getSlugifyData
  */
 public function testSlugify($text, $default, $result)
 {
     if (!is_null($default)) {
         $this->assertEquals($result, $this->slugifier->slugify($text, $default));
     } else {
         $this->assertEquals($result, $this->slugifier->slugify($text));
     }
 }
 public function isUnderPage(AbstractPage $page, $name)
 {
     $slug = $this->slugifier->slugify($name);
     $breadcrumbs = $this->categoryService->getBreadcrumbs($page, true);
     return array_reduce($breadcrumbs, function ($match, Category $parent) use($slug) {
         if ($match) {
             return true;
         }
         return substr($parent->getSlug(), 0, -strlen($slug)) === $slug || $this->slugifier->slugify($parent->getTitle()) === $slug;
     });
 }
コード例 #3
0
 /**
  * {@inheritdoc}
  */
 public function buildView(FormView $view, FormInterface $form, array $options)
 {
     $nodeTranslation = $form->getParent()->getData();
     $view->vars['reset'] = $this->slugifier->slugify($nodeTranslation->getTitle(), '');
     $parentNode = $nodeTranslation->getNode()->getParent();
     if ($parentNode !== null) {
         $nodeTranslation = $parentNode->getNodeTranslation($nodeTranslation->getLang(), true);
         $slug = $nodeTranslation->getSlugPart();
         if (!empty($slug)) {
             $slug .= '/';
         }
         $view->vars['prefix'] = $slug;
     }
 }
コード例 #4
0
 public function prepareMedia(Media $media)
 {
     $url = $media->getUrl();
     $media->setUuid(uniqid());
     parent::prepareMedia($media);
     if ($media->getContent() instanceof File) {
         // if media already has it’s local path ($url) then i don’t want parent to overwrite it
         if ($url && "." !== $url[strlen($url) - 1] && $url === parse_url($url, PHP_URL_PATH)) {
             $media->setUrl($url);
         }
         $dirname = dirname($media->getUrl());
         $ext = pathinfo($media->getUrl(), PATHINFO_EXTENSION);
         $filename = $this->slugifier->slugify(basename($media->getUrl(), $ext)) . ($ext ? ".{$ext}" : "");
         $url = implode('/', [$dirname, $filename]);
         $this->setMediaUrl($media, $url);
     }
 }
コード例 #5
0
 /**
  *
  *
  * @param Media $media
  * @return string
  */
 private function getFilePath(Media $media)
 {
     $filename = $media->getOriginalFilename();
     $filename = str_replace(array('/', '\\', '%'), '', $filename);
     if (!empty($this->blacklistedExtensions)) {
         $filename = preg_replace('/\\.(' . join('|', $this->blacklistedExtensions) . ')$/', '.txt', $filename);
     }
     $parts = pathinfo($filename);
     $filename = $this->slugifier->slugify($parts['filename']);
     $filename .= '.' . strtolower($parts['extension']);
     return sprintf('%s/%s', $media->getUuid(), $filename);
 }
コード例 #6
0
 /**
  * @param NodeTranslation $translation The node translation
  * @param EntityManager   $em          The entity manager
  * @param array           $flashes     Flashes
  *
  * A function that checks the URL and sees if it's unique.
  * It's allowed to be the same when the node is a StructureNode.
  * When a node is deleted it needs to be ignored in the check.
  * Offline nodes need to be included as well.
  *
  * It sluggifies the slug, updates the URL
  * and checks all existing NodeTranslations ([1]), excluding itself. If a URL existsthat has the same url.
  * If an existing one is found the slug is modified, the URL is updated and the check is repeated
  * until no prior urls exist.
  *
  * NOTE: We need a way to tell if the slug has been modified or not.
  * NOTE: Would be cool if we could increment a number after the slug. Like check if it matches -v#
  *       and increment the number.
  *
  * [1] For all languages for now. The issue is that we need a way to know if a node's URL is prepended with the
  * language or not. For now both scenarios are possible so we check for all languages.
  *
  * @param NodeTranslation &$translation Reference to the NodeTranslation. This is modified in place.
  * @param EntityManager   $em           The entity manager
  * @param array           $flashes      The flash messages array
  *
  * @return bool
  *
  * @return boolean
  */
 private function ensureUniqueUrl(NodeTranslation &$translation, EntityManager $em, $flashes = array())
 {
     // Can't use GetRef here yet since the NodeVersions aren't loaded yet for some reason.
     $pnv = $translation->getPublicNodeVersion();
     $page = $em->getRepository($pnv->getRefEntityName())->find($pnv->getRefId());
     $isStructureNode = $page->isStructureNode();
     // If it's a StructureNode the slug and url should be empty.
     if ($isStructureNode) {
         $translation->setSlug('');
         $translation->setUrl($translation->getFullSlug());
         return true;
     }
     /* @var Kunstmaan\NodeBundle\Entity\NodeTranslation $nodeTranslationRepository */
     $nodeTranslationRepository = $em->getRepository('KunstmaanNodeBundle:NodeTranslation');
     if ($translation->getUrl() == $translation->getFullSlug()) {
         $this->logger->addDebug('Evaluating URL for NT ' . $translation->getId() . ' getUrl: \'' . $translation->getUrl() . '\' getFullSlug: \'' . $translation->getFullSlug() . '\'');
         return false;
     }
     // Adjust the URL.
     $translation->setUrl($translation->getFullSlug());
     // Find all translations with this new URL, whose nodes are not deleted.
     $translations = $nodeTranslationRepository->getNodeTranslationForUrl($translation->getUrl(), $translation->getLang(), false, $translation);
     $this->logger->addDebug('Found ' . count($translations) . ' node(s) that match url \'' . $translation->getUrl() . '\'');
     if (count($translations) > 0) {
         $oldUrl = $translation->getFullSlug();
         $translation->setSlug($this->slugifier->slugify($this->IncrementString($translation->getSlug())));
         $newUrl = $translation->getFullSlug();
         $message = 'The URL of the page has been changed from ' . $oldUrl . ' to ' . $newUrl . ' since another page already uses this URL.';
         $this->logger->addInfo($message);
         $flashes[] = $message;
         $this->ensureUniqueUrl($translation, $em, $flashes);
     } elseif (count($flashes) > 0) {
         // No translations found so we're certain we can show this message.
         $flash = end($flashes);
         $flash = current(array_slice($flashes, -1));
         $this->session->getFlashBag()->add('warning', $flash);
     }
     return true;
 }
コード例 #7
0
 /**
  * @param string $text
  *
  * @return string
  */
 public function slugify($text)
 {
     return $this->slugifier->slugify($text, '');
 }