Skip to content

Commit

Permalink
Allow localized files for download
Browse files Browse the repository at this point in the history
  • Loading branch information
Salamek committed Jul 25, 2017
1 parent 59a3367 commit e596b8d
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 87 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@
</div>
</div><!-- col-sm-12 -->
</div><!-- row -->

{include $fileSelectorPath, filetype => null, form => $form, selectorName => structureFile, uploadName => file, id => $form[$activeLocale->getLanguageCode()]['structureFile']->htmlId}

</fieldset>
{/formContainer}
</div>
</div>

{include $fileSelectorPath, filetype => null, form => $form, selectorName => structureFile, uploadName => file}

<div class="row">
<div class="col-sm-6">
<div class="form-group no-margin-hr">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class DownloadFileForm extends BaseControl
* @param StructureRepository $structureRepository
* @param LocaleRepository $localeRepository
* @param FileStorage $fileStorage
* @param File $file
* @param File $fileFile
* @param Download $download
* @param DownloadFile|null $file
*/
Expand Down Expand Up @@ -127,14 +127,14 @@ public function __construct(

$defaults = [
'position' => $this->file->getPosition(),
'structureFile' => $this->file->getStructureFile()->getId(),
'identifier' => $this->file->getIdentifier()
];

foreach ($this->file->getTranslations() AS $translation)
{
$defaults[$translation->getLocale()->getLanguageCode()]['name'] = $translation->getName();
$defaults[$translation->getLocale()->getLanguageCode()]['description'] = $translation->getDescription();
$defaults[$translation->getLocale()->getLanguageCode()]['structureFile'] = ($translation->getStructureFile() ? $translation->getStructureFile()->getId() : null);
}

}
Expand All @@ -159,13 +159,13 @@ protected function createComponentForm()
->addRule(Form::MAX_LENGTH, 'File name is too long.', 255);

$container->addTextArea('description');
}

$form->addText('structureFile')
->setType('number');

$form->addUpload('file');
$container->addText('structureFile')
->setType('number');

$container->addUpload('file');
}

$form->addText('identifier')
->setRequired('Please fill in unique identifier');

Expand Down Expand Up @@ -203,47 +203,50 @@ public function editFormValidate(Form $form)
public function editFormSucceeded(Form $form)
{
$values = $form->getValues();

$structureFile = $this->structureFileRepository->getOneById($values->structureFile);

/** @var FileUpload $fileUpload */
$fileUpload = $values->file;
if ($fileUpload->isOk()) {
$structureName = 'Download';
if (!$structure = $this->structureRepository->getOneByName($structureName)) {
$structure = new Structure($structureName);
$this->entityManager->persist($structure);
$this->entityManager->flush();
}
$structureFile = $this->fileStorage->processFile($fileUpload, $structure);
}


if ($this->file) {
$file = $this->file;
$file->setPosition($values->position);
$file->setStructureFile($structureFile);
$file->setIdentifier($values->identifier);
} else {
$file = new DownloadFile($structureFile, $this->download, $values->identifier);
$file = new DownloadFile($this->download, $values->identifier);
}

$this->entityManager->persist($file);

$this->entityManager->flush();

foreach ($this->localeRepository->getActive() AS $activeLocale) {

$structureFile = $this->structureFileRepository->getOneById($values->{$activeLocale->getLanguageCode()}->structureFile);

/** @var FileUpload $fileUpload */
$fileUpload = $values->{$activeLocale->getLanguageCode()}->file;
if ($fileUpload->isOk()) {
$structureName = 'Download';
if (!$structure = $this->structureRepository->getOneByName($structureName)) {
$structure = new Structure($structureName);
$this->entityManager->persist($structure);
$this->entityManager->flush();
}
$structureFile = $this->fileStorage->processFile($fileUpload, $structure);
}


if ($downloadFileTranslation = $this->downloadFileTranslationRepository->getTranslation($file, $activeLocale))
{
$downloadFileTranslation->setName($values->{$activeLocale->getLanguageCode()}->name);
$downloadFileTranslation->setDescription($values->{$activeLocale->getLanguageCode()}->description);
$downloadFileTranslation->setStructureFile($structureFile);
}
else
{
$downloadFileTranslation = new DownloadFileTranslation(
$file,
$activeLocale,
$values->{$activeLocale->getLanguageCode()}->name,
$values->{$activeLocale->getLanguageCode()}->description
$values->{$activeLocale->getLanguageCode()}->description,
$structureFile
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,6 @@ public function createComponentGrid($name)
->setFilterText()
->setSuggestion();

$grid->addColumnText('structureFile.name', 'File')
->setCustomRender(function($row){
return $row->getStructureFile()->getBasename();
})
->setFilterText()
->setSuggestion();

$grid->getColumn('identifier')->cellPrototype->class[] = 'center';

$grid->addColumnNumber('downloadCount', 'Download count')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,34 +68,29 @@ public function render()
$paginator->itemsPerPage = 10;
$paginator->itemCount = $download->getDownloadFiles()->count();

$template->downloadTranslation = $this->downloadTranslationRepository->getTranslation($download, $this->currentLocale);
$template->download = $download;
$template->currentLocale = $this->currentLocale;

$fileTranslations = [];
foreach ($this->downloadFileRepository->getByDownload($download, $paginator->itemsPerPage, $paginator->offset) AS $item)
{
$fileTranslations[] = $this->downloadFileTranslationRepository->getTranslation($item, $this->currentLocale);
}

$template->fileTranslations = $fileTranslations;
$template->downloadFiles = $this->downloadFileRepository->getByDownload($download, $paginator->itemsPerPage, $paginator->offset);

$template->setFile($this->cmsActionOption->getTemplatePath(__DIR__.'/detail.latte'));
$template->render();
}

/**
* @param $downloadFileId
* @param $downloadFileTranslationId
*/
public function handleDownload($downloadFileId)
public function handleDownload($downloadFileTranslationId)
{
$downloadFile = $this->downloadFileRepository->getOneById($downloadFileId);
if (!$downloadFile)
$downloadFileTranslation = $this->downloadFileTranslationRepository->getOneById($downloadFileTranslationId);
if (!$downloadFileTranslation)
{
$this->presenter->flashMessage('File not found!', 'alert-danger');
}
else
{
$this->downloadFileRepository->logDownload($downloadFile);
$response = $this->fileStorage->downloadFile($downloadFile->getStructureFile());
$this->downloadFileRepository->logDownload($downloadFileTranslation->getDownloadFile());
$response = $this->fileStorage->downloadFile($downloadFileTranslation->getStructureFile());
$this->presenter->sendResponse($response);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
<h3 n:if="$downloadTranslation->getDownload()->isShowName()" class="heading-title">{$downloadTranslation->getName()}</h3>
{var $downloadTranslation = $download->getTranslation($currentLocale)}

<h3 n:if="$download->isShowName()" class="heading-title">{$downloadTranslation->getName()}</h3>
<div class="news-wrapper" n:snippet="detail">
<article n:foreach="$fileTranslations AS $fileTranslation" class="news-item page-row has-divider clearfix row">
<article n:foreach="$downloadFiles AS $downloadFile" class="news-item page-row has-divider clearfix row">
{var $fileTranslation = $downloadFile->getTranslation($currentLocale)}
<figure class="thumb file col-md-2 col-sm-3 col-xs-4">
{if \Salamek\Files\Models\IFile::TYPE_IMAGE == $fileTranslation->getDownloadFile()->getStructureFile()->getFile()->getType()}
<img class="img-responsive" n:img="$fileTranslation->getDownloadFile()->getStructureFile()->getFile(), 122x91" alt="{$fileTranslation->getName()}">
{if \Salamek\Files\Models\IFile::TYPE_IMAGE == $fileTranslation->getStructureFile()->getFile()->getType()}
<img class="img-responsive" n:img="$fileTranslation->getStructureFile()->getFile(), 122x91" alt="{$fileTranslation->getName()}">
{else}
<img class="img-responsive" src="{$fileTranslation->getDownloadFile()->getStructureFile()->getFile()|fileIcon}" alt="{$fileTranslation->getName()}">
<img class="img-responsive" src="{$fileTranslation->getStructureFile()->getFile()|fileIcon}" alt="{$fileTranslation->getName()}">
{/if}
<div class="file-name">
<h4>{$fileTranslation->getDownloadFile()->getStructureFile()->getBasename()}</h4>
<h4>{$fileTranslation->getStructureFile()->getBasename()}</h4>
</div>
</figure>
<div class="details col-md-10 col-sm-9 col-xs-8">
<h3 class="title"><a href="">{$fileTranslation->getName()}</a></h3>
<p>{$fileTranslation->getDescription()|noescape}</p>
<a class="btn btn-theme read-more" n:href="download!, $fileTranslation->getDownloadFile()->getId()">Stáhnout <small>{$fileTranslation->getDownloadFile()->getStructureFile()->getFile()->getSize()|bytes}</small><i class="fa fa-download"></i></a>
<a class="btn btn-theme read-more" n:href="download!, $fileTranslation->getId()">Stáhnout <small>{$fileTranslation->getStructureFile()->getFile()->getSize()|bytes}</small><i class="fa fa-download"></i></a>
</div>
</article><!--//news-item-->

Expand Down
12 changes: 12 additions & 0 deletions src/Dravencms/Model/FileDownload/Entities/Download.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
namespace Dravencms\Model\FileDownload\Entities;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\Mapping as ORM;
use Dravencms\Model\Locale\Entities\ILocale;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use Kdyby\Doctrine\Entities\Attributes\Identifier;
use Nette;
Expand Down Expand Up @@ -102,6 +104,16 @@ public function getTranslations()
return $this->translations;
}

/**
* @param ILocale $locale
* @return DownloadTranslation
*/
public function getTranslation(ILocale $locale)
{
$criteria = Criteria::create()->where(Criteria::expr()->eq("locale", $locale));
return $this->getTranslations()->matching($criteria)->first();
}

/**
* @return string
*/
Expand Down
41 changes: 14 additions & 27 deletions src/Dravencms/Model/FileDownload/Entities/DownloadFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
namespace Dravencms\Model\FileDownload\Entities;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\Mapping as ORM;
use Dravencms\Model\File\Entities\StructureFile;
use Dravencms\Model\Locale\Entities\ILocale;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use Kdyby\Doctrine\Entities\Attributes\Identifier;
use Nette;
Expand All @@ -30,14 +32,7 @@ class DownloadFile extends Nette\Object
* @ORM\Column(type="string",length=255,nullable=false, unique=true)
*/
private $identifier;

/**
* @var StructureFile
* @ORM\ManyToOne(targetEntity="\Dravencms\Model\File\Entities\StructureFile")
* @ORM\JoinColumn(name="structure_file_id", referencedColumnName="id")
*/
private $structureFile;


/**
* @var Download
* @Gedmo\SortableGroup
Expand Down Expand Up @@ -67,26 +62,16 @@ class DownloadFile extends Nette\Object

/**
* DownloadFile constructor.
* @param StructureFile $structureFile
* @param Download $download
* @param $identifier
*/
public function __construct(StructureFile $structureFile, Download $download, $identifier)
public function __construct(Download $download, $identifier)
{
$this->structureFile = $structureFile;
$this->download = $download;
$this->identifier = $identifier;
$this->downloadCount = 0;
$this->translations = new ArrayCollection();
}

/**
* @param StructureFile $structureFile
*/
public function setStructureFile($structureFile)
{
$this->structureFile = $structureFile;
}

/**
* @param int $downloadCount
Expand All @@ -112,14 +97,6 @@ public function setPosition($position)
$this->position = $position;
}

/**
* @return StructureFile
*/
public function getStructureFile()
{
return $this->structureFile;
}

/**
* @return int
*/
Expand Down Expand Up @@ -152,6 +129,16 @@ public function getTranslations()
return $this->translations;
}

/**
* @param ILocale $locale
* @return DownloadFileTranslation
*/
public function getTranslation(ILocale $locale)
{
$criteria = Criteria::create()->where(Criteria::expr()->eq("locale", $locale));
return $this->getTranslations()->matching($criteria)->first();
}

/**
* @return string
*/
Expand Down
Loading

0 comments on commit e596b8d

Please sign in to comment.