Skip to content

Commit

Permalink
Add slug on file entity
Browse files Browse the repository at this point in the history
  • Loading branch information
na-ji committed Mar 12, 2017
1 parent 3df68ad commit 4ee15b1
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 2 deletions.
49 changes: 49 additions & 0 deletions src/nk/DocumentBundle/Command/GenerateFileSlugCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace nk\DocumentBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Helper\ProgressBar;

class GenerateFileSlugCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
->setName('nk:document:generate:file:slug')
->setDescription('(re)genère les slugs de tous les fichiers')
;
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$em = $this->getContainer()->get('doctrine.orm.entity_manager');
$files = $em->getRepository('nkDocumentBundle:File')->findAll();

$progress = new ProgressBar($output, count($files));
$progress->setFormat('%current%/%max% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s% %memory:6s% %message%');
$progress->start();

$progress->setMessage('start');

foreach ($files as $file) {
$progress->setMessage($file->getId());
$file->setSlug(null);

if ($progress->getProgress() % 10 === 0) {
$em->flush();
}

$progress->advance();
}

$em->flush();

$progress->setMessage('finish');
$progress->finish();
}
}
2 changes: 1 addition & 1 deletion src/nk/DocumentBundle/Controller/ApiRestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public function getDocumentAction(Document $document)

foreach ($document->getFiles() as $file) {
/* @var $file \nk\DocumentBundle\Entity\File */
$file->setDownloadPath($this->generateUrl('nk_document_file_download', array('id' => $file->getId(), 'inline' => 1)));
$file->setDownloadPath($this->generateUrl('nk_document_file_preview_slug', array('slug' => $file->getSlug())));
}

$data = array(
Expand Down
30 changes: 30 additions & 0 deletions src/nk/DocumentBundle/Entity/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ class File
*/
private $downloadPath;

/**
* @Gedmo\Slug(fields={"id", "name"}, suffix=".pdf")
* @ORM\Column(length=128, nullable=false, options={"default" : ""})
* @Serializer\Groups({"details"})
*/
private $slug;

public function __construct(Document $document = null)
{
if($document !== null)
Expand Down Expand Up @@ -275,4 +282,27 @@ public function setDownloadPath($downloadPath)

return $this;
}

/**
* Set slug
*
* @param string $slug
* @return File
*/
public function setSlug($slug)
{
$this->slug = $slug;

return $this;
}

/**
* Get slug
*
* @return string
*/
public function getSlug()
{
return $this->slug;
}
}
8 changes: 7 additions & 1 deletion src/nk/DocumentBundle/Resources/config/routing/file.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,10 @@ nk_document_file_download:
pattern: /{id}/download
defaults: { _controller: nkDocumentBundle:File:download }
requirements:
id: \d+
id: \d+

nk_document_file_preview_slug:
pattern: /{slug}
defaults: { _controller: nkDocumentBundle:File:download, inline = 1 }
requirements:
id: \d+

0 comments on commit 4ee15b1

Please sign in to comment.