/**
  * Add category image when not already present
  *
  * @param Category $category
  * @param $image
  */
 protected function setFileRelationFromImage($category, $image)
 {
     // get fileObject by given identifier (file UID, combined identifier or path/filename)
     try {
         $newImage = $this->getResourceFactory()->retrieveFileOrFolderObject($image);
     } catch (\TYPO3\CMS\Core\Resource\Exception\ResourceDoesNotExistException $exception) {
         $newImage = false;
     }
     // only proceed if image is found
     if (!$newImage instanceof \TYPO3\CMS\Core\Resource\File) {
         return;
     }
     // new image found check if this isn't already
     $existingImages = $category->getImages();
     if (!is_null($existingImages) && $existingImages->count() !== 0) {
         /** @var $item FileReference */
         foreach ($existingImages as $item) {
             // only check already persisted items
             if ($item->getFileUid() === (int) $newImage->getUid() || $item->getUid() && $item->getOriginalResource()->getName() === $newImage->getName() && $item->getOriginalResource()->getSize() === (int) $newImage->getSize()) {
                 $newImage = false;
                 break;
             }
         }
     }
     if ($newImage) {
         // file not inside a storage then search for existing file or copy the one form storage 0 to the import folder
         if ($newImage->getStorage()->getUid() === 0) {
             // search DB for same file based on hash (to prevent duplicates)
             $existingFile = $this->findFileByHash($newImage->getSha1());
             // no exciting file then copy file to import folder
             if ($existingFile === null) {
                 $newImage = $this->getResourceStorage()->copyFile($newImage, $this->getImportFolder());
             } else {
                 $newImage = $existingFile;
             }
         }
         /** @var FileReference $fileReference */
         $fileReference = $this->objectManager->get(\GeorgRinger\News\Domain\Model\FileReference::class);
         $fileReference->setFileUid($newImage->getUid());
         $fileReference->setPid($category->getPid());
         $category->addImage($fileReference);
     }
 }
Example #2
0
 /**
  * Test if importSource can be set
  *
  * @test
  * @return void
  */
 public function importSourceCanBeSet()
 {
     $value = 'something';
     $this->instance->setImportSource($value);
     $this->assertEquals($value, $this->instance->getImportSource());
 }
Example #3
0
 /**
  * Test if category can be set
  *
  * @test
  * @return void
  */
 public function categoryCanBeSet()
 {
     $category = new Category();
     $category->setTitle('fo');
     $categories = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
     $categories->attach($category);
     $this->newsDomainModelInstance->setCategories($categories);
     $this->assertEquals($categories, $this->newsDomainModelInstance->getCategories());
 }
 /**
  * @test
  * @return void
  */
 public function getDetailPidFromCategoriesReturnsCorrectValue()
 {
     $viewHelper = $this->getAccessibleMock('GeorgRinger\\News\\ViewHelpers\\LinkViewHelper', ['dummy']);
     $newsItem = new \GeorgRinger\News\Domain\Model\News();
     $categories = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
     $category1 = new Category();
     $categories->attach($category1);
     $category2 = new Category();
     $category2->setSinglePid('123');
     $categories->attach($category2);
     $category3 = new Category();
     $category3->setSinglePid('456');
     $categories->attach($category3);
     $newsItem->setCategories($categories);
     $result = $viewHelper->_call('getDetailPidFromCategories', [], $newsItem);
     $this->assertEquals(123, $result);
 }