Skip to content

Commit

Permalink
Migrate to use FileStructureLink
Browse files Browse the repository at this point in the history
  • Loading branch information
Salamek committed Oct 6, 2019
1 parent 0a59b94 commit 24b69ac
Show file tree
Hide file tree
Showing 10 changed files with 175 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

use Dravencms\Components\BaseControl\BaseControl;
use Dravencms\Components\BaseForm\BaseFormFactory;
use Dravencms\Model\File\Entities\StructureFileLink;
use Dravencms\Model\File\Repository\StructureFileRepository;
use Dravencms\Model\File\Repository\StructureRepository;
use Dravencms\Model\Gallery\Entities\Gallery;
Expand Down Expand Up @@ -176,8 +177,10 @@ public function editFormSucceeded(Form $form)

$name = $structureFile->getId().'-'.$structureFile->getName();
$identifier = md5($name.microtime().rand().$structureFile->getFile()->getSum());

$picture = new Picture($this->gallery, $structureFile, $identifier, $values->isActive, false);

$structureFileLink = new StructureFileLink(\Dravencms\Gallery\Gallery::PLUGIN_NAME, $structureFile, true, true);
$this->entityManager->persist($structureFileLink);
$picture = new Picture($this->gallery, $structureFileLink, $identifier, $values->isActive, false);
$picture->setTags($tags);

$this->entityManager->persist($picture);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

use Dravencms\File\File;
use Dravencms\Model\File\Entities\Structure;
use Dravencms\Model\File\Entities\StructureFileLink;
use Dravencms\Model\File\Repository\StructureFileRepository;
use Dravencms\Model\File\Repository\StructureRepository;
use Dravencms\Model\Gallery\Entities\Gallery;
Expand Down Expand Up @@ -249,9 +250,20 @@ public function editFormSucceeded(Form $form)
$picture->setIsActive($values->isActive);
$picture->setIsPrimary($values->isPrimary);
$picture->setPosition($values->position);
$picture->setStructureFile($structureFile);

if ($picture->getStructureFileLink()) {
$existingStructureFile = $picture->getStructureFileLink();
$existingStructureFile->setStructureFile($structureFile);

} else {
$existingStructureFile = new StructureFileLink(\Dravencms\Gallery\Gallery::PLUGIN_NAME, $structureFile, true, true);
}

$this->entityManager->persist($existingStructureFile);
} else {
$picture = new Picture($this->gallery, $structureFile, $values->identifier, $values->isActive, $values->isPrimary);
$structureFileLink = new StructureFileLink(\Dravencms\Gallery\Gallery::PLUGIN_NAME, $structureFile, true, true);
$this->entityManager->persist($structureFileLink);
$picture = new Picture($this->gallery, $structureFileLink, $values->identifier, $values->isActive, $values->isPrimary);
}
$picture->setTags($tags);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ public function createComponentGrid($name)
->setAlign('center')
->setRenderer(function ($row) use($grid){
/** @var Picture $row */
if ($haveImage = $row->getStructureFile()) {
if ($haveImage = $row->getStructureFileLink()) {
$img = Html::el('img');
$img->src = $this->imagePipe->request($haveImage->getFile(), '200x');
$img->src = $this->imagePipe->request($haveImage->getStructureFile()->getFile(), '200x');
} else {
$img = '';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
<div class="row page-row">
<div n:foreach="$pictureTranslations as $pictureTranslation" class="col-sm-6 col-xs-12 col-md-3">
{var $image = $pictureTranslation->getPicture()}
<a class="thumbnail text-center" href="{img $image->getStructureFile()->getFile(), 1024x}" title="{$pictureTranslation->getName()}" data-gallery>
<img class="img-responsive" n:img="$image->getStructureFile()->getFile(), 263x150, fit_exact" alt="{$pictureTranslation->getName()}" title="{$pictureTranslation->getName()}">
{var $structureFile = ($image->getStructureFileLink() ? $image->getStructureFileLink()->getStructureFile(): null)}
<a class="thumbnail text-center" href="{img $structureFile->getFile(), 1024x}" title="{$pictureTranslation->getName()}" data-gallery>
<img class="img-responsive" n:img="$structureFile->getFile(), 263x150, fit_exact" alt="{$pictureTranslation->getName()}" title="{$pictureTranslation->getName()}">
</a>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
<div class="album-cover">
<a n:cmsLink="Gallery\Gallery\Detail, 'id' => $gallery->getId()">
{if $gallery->getPrimaryPicture()->count()}
{var $picture = $gallery->getPrimaryPicture()[0]->getStructureFile()}
{var $picture = $gallery->getPrimaryPicture()[0]}
{var $structureFile = ($picture->getStructureFileLink() ? $picture->getStructureFileLink()->getStructureFile(): null)}
{elseif $gallery->getPictures()->count()}
{var $picture = $gallery->getPictures()[0]->getStructureFile()}
{var $picture = $gallery->getPictures()[0]}
{var $structureFile = ($picture->getStructureFileLink() ? $picture->getStructureFileLink()->getStructureFile(): null)}
{else}
{var $picture = null}
{var $structureFile = null}
{/if}
<img class="img-responsive" n:img="$picture, '553x306', 'exact'" alt="{$galleryTranslation->getName()}">

<img class="img-responsive" n:img="$structureFile, '553x306', 'exact'" alt="{$galleryTranslation->getName()}">
</a>
<div class="desc">
<h4><small><a n:cmsLink="Gallery\Gallery\Detail, 'id' => $gallery->getId()">{$galleryTranslation->getName()}</a></small></h4>
Expand Down
84 changes: 84 additions & 0 deletions src/Dravencms/Gallery/Console/MigrateLinkGalleryCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

declare(strict_types=1);

namespace Dravencms\Gallery\Console;

use Dravencms\Gallery\Gallery;
use Dravencms\Model\File\Entities\StructureFileLink;
use Dravencms\Model\Gallery\Repository\GalleryRepository;
use Dravencms\Model\Gallery\Repository\PictureRepository;
use Kdyby\Doctrine\EntityManager;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Class MigrateLinkGalleryCommand
* @package Dravencms\Gallery\Console
*/
class MigrateLinkGalleryCommand extends Command
{
/** @var EntityManager */
private $entityManager;

/** @var PictureRepository */
private $pictureRepository;

/**
* CleanGalleryCommand constructor.
* @param EntityManager $entityManager
* @param GalleryRepository $galleryRepository
*/
public function __construct(
EntityManager $entityManager,
PictureRepository $pictureRepository
)
{
parent::__construct(null);

$this->entityManager = $entityManager;
$this->pictureRepository = $pictureRepository;
}

/**
*
*/
protected function configure(): void
{
$this->setName('gallery:gallery:migrate-link')
->setDescription('Migrate from direct fileStructure usage to fileStructureLink');
}

/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int|null
* @throws \GuzzleHttp\Exception\GuzzleException
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
try {
$migrated = 0;
foreach ($this->pictureRepository->getAll() AS $picture) {
if ($picture->getStructureFile() && !$picture->getStructureFileLink()) {
$structureFileLink = new StructureFileLink(Gallery::PLUGIN_NAME, $picture->getStructureFile(), true, true);
$picture->setStructureFileLink($structureFileLink);
$picture->setStructureFile(null);
$this->entityManager->persist($structureFileLink);
$this->entityManager->persist($picture);
$migrated++;
}
}

$this->entityManager->flush();
$output->writeLn(sprintf('%s pictures has been migrated!', $migrated));
return 0; // zero return code means everything is ok

} catch (\Exception $e) {
$output->writeLn('<error>' . $e->getMessage() . '</error>');
return 1; // non-zero return code means error
}
}
}
3 changes: 2 additions & 1 deletion src/Dravencms/Gallery/DI/console.neon
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
- Dravencms\Gallery\Console\CleanGalleryCommand
- Dravencms\Gallery\Console\CleanGalleryCommand
- Dravencms\Gallery\Console\MigrateLinkGalleryCommand
3 changes: 3 additions & 0 deletions src/Dravencms/Gallery/Gallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
class Gallery
{
use Nette\SmartObject;

const PLUGIN_NAME = 'gallery';

public function __construct()
{
}
Expand Down
63 changes: 44 additions & 19 deletions src/Dravencms/Model/Gallery/Entities/Picture.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use Doctrine\Common\Collections\Criteria;
use Dravencms\Model\File\Entities\StructureFile;
use Dravencms\Model\File\Entities\StructureFileLink;
use Dravencms\Model\Locale\Entities\ILocale;
use Dravencms\Model\Tag\Entities\Tag;
use Doctrine\Common\Collections\ArrayCollection;
Expand Down Expand Up @@ -58,6 +59,13 @@ class Picture
*/
private $structureFile;

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

/**
* @var Gallery
* @Gedmo\SortableGroup
Expand Down Expand Up @@ -91,18 +99,18 @@ class Picture
/**
* Picture constructor.
* @param Gallery $gallery
* @param StructureFile $structureFile
* @param StructureFileLink $structureFileLink
* @param $identifier
* @param bool $isActive
* @param bool $isPrimary
*/
public function __construct(Gallery $gallery, StructureFile $structureFile, $identifier, $isActive = true, $isPrimary = false)
public function __construct(Gallery $gallery, StructureFileLink $structureFileLink, string $identifier, bool $isActive = true, bool $isPrimary = false)
{
$this->identifier = $identifier;
$this->gallery = $gallery;
$this->isActive = $isActive;
$this->isPrimary = $isPrimary;
$this->structureFile = $structureFile;
$this->structureFileLink = $structureFileLink;

$this->tags = new ArrayCollection();
$this->translations = new ArrayCollection();
Expand All @@ -111,47 +119,55 @@ public function __construct(Gallery $gallery, StructureFile $structureFile, $ide
/**
* @param string $identifier
*/
public function setIdentifier($identifier)
public function setIdentifier(string $identifier): void
{
$this->identifier = $identifier;
}

/**
* @param boolean $isActive
*/
public function setIsActive($isActive)
public function setIsActive(bool $isActive): void
{
$this->isActive = $isActive;
}

/**
* @param mixed $position
* @param int $position
*/
public function setPosition($position)
public function setPosition(int $position): void
{
$this->position = $position;
}

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

/**
* @param StructureFileLink $structureFileLink
*/
public function setStructureFileLink(StructureFileLink $structureFileLink): void
{
$this->structureFileLink = $structureFileLink;
}

/**
* @param boolean $isPrimary
*/
public function setIsPrimary($isPrimary)
public function setIsPrimary(bool $isPrimary): void
{
$this->isPrimary = $isPrimary;
}

/**
* @param Tag $tag
*/
public function addTag(Tag $tag)
public function addTag(Tag $tag): void
{
if ($this->tags->contains($tag))
{
Expand All @@ -164,7 +180,7 @@ public function addTag(Tag $tag)
/**
* @param Tag $tag
*/
public function removeTag(Tag $tag)
public function removeTag(Tag $tag): void
{
if (!$this->tags->contains($tag))
{
Expand All @@ -178,7 +194,7 @@ public function removeTag(Tag $tag)
*
* @param ArrayCollection $tags
*/
public function setTags(ArrayCollection $tags)
public function setTags(ArrayCollection $tags): void
{
//Remove all not in
foreach($this->tags AS $tag)
Expand All @@ -202,33 +218,42 @@ public function setTags(ArrayCollection $tags)
/**
* @return boolean
*/
public function isActive()
public function isActive(): bool
{
return $this->isActive;
}

/**
* @return boolean
*/
public function isPrimary()
public function isPrimary(): bool
{
return $this->isPrimary;
}

/**
* @return mixed
* @return int
*/
public function getPosition()
public function getPosition(): int
{
return $this->position;
}

/**
* @return StructureFile
* @deprecated Use getStructureFileLink()->getStructureFile()
*/
public function getStructureFile()
{
return $this->structureFile;
return ($this->structureFileLink ? $this->structureFileLink->getStructureFile() : $this->structureFile);
}

/**
* @return StructureFileLink
*/
public function getStructureFileLink()
{
return $this->structureFileLink;
}

/**
Expand All @@ -242,7 +267,7 @@ public function getTags()
/**
* @return Gallery
*/
public function getGallery()
public function getGallery(): Gallery
{
return $this->gallery;
}
Expand All @@ -258,7 +283,7 @@ public function getTranslations()
/**
* @return string
*/
public function getIdentifier()
public function getIdentifier(): string
{
return $this->identifier;
}
Expand Down
Loading

0 comments on commit 24b69ac

Please sign in to comment.