Commit d88d33e3 authored by Sebastian Castro's avatar Sebastian Castro

Refactor image file

parent 94f42339
......@@ -94,8 +94,8 @@ services:
- { name: sonata.admin, manager_type: doctrine_mongodb, group: "data", label: Etiquettes }
admin.element_image:
class: Biopen\GeoDirectoryBundle\Admin\ElementImageAdmin
arguments: [~, Biopen\GeoDirectoryBundle\Document\ElementImage, '' ]
class: Biopen\CoreBundle\Admin\ImageAdmin
arguments: [~, Biopen\CoreBundle\Document\Image, '' ]
tags:
- { name: sonata.admin, manager_type: doctrine_mongodb, group: "Hide" }
......
<?php
namespace Biopen\CoreBundle\Admin;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Route\RouteCollection;
use Sonata\AdminBundle\Show\ShowMapper;
use Vich\UploaderBundle\Form\Type\VichImageType;
class ImageAdmin extends AbstractAdmin
{
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('file', 'file', array('label' => 'Fichier à importer', 'required' => false))
->add('externalImageUrl', null, array('label' => 'Lien vers une image externe', 'required' => false))
;
}
protected function configureShowFields(ShowMapper $show)
{
$show->add('fileName');
}
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper
->add('fileName')
;
}
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->addIdentifier('fileName')
->add('_action', 'actions', array(
'actions' => array(
'edit' => array(),
)
))
;
}
}
\ No newline at end of file
......@@ -11,6 +11,10 @@ use Biopen\CoreBundle\Services\UploadDirectoryNamer;
* Represent a common File. Need to be extended by real Document
* You need to implement $vichUploadFileKey and the annotation of the $file to specify the mapping key inside it (defined also in config.yml -> vich_uploader)
*/
/**
* @MongoDB\EmbeddedDocument
* @Vich\Uploadable
*/
class AbstractFile
{
protected $vichUploadFileKey = "default_file";
......@@ -69,7 +73,6 @@ class AbstractFile
$this->file = $file;
if (null !== $file) {
// store the absolute url of the file so we can directly use it in the json conversion
$this->fileUrl = $this->calculateFileUrl();
......@@ -186,4 +189,9 @@ class AbstractFile
{
return $this->fileUrl;
}
public function getVichUploadFileKey()
{
return $this->vichUploadFileKey;
}
}
<?php
namespace Biopen\CoreBundle\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
use Biopen\CoreBundle\Helper\HostResolverHelper;
use Biopen\CoreBundle\Document\AbstractFile;
/**
* @MongoDB\EmbeddedDocument
* @Vich\Uploadable
*/
class Image extends AbstractFile
{
protected $vichUploadFileKey = "element_image";
/**
* @var string
* Instead of uploading a file, we can give an external url to an image
* @MongoDB\Field(type="string")
*/
public $externalImageUrl = "";
public function toJson()
{
$url = $this->fileUrl ? $this->fileUrl : $this->externalImageUrl;
return json_encode($url);
}
/**
* Set externalImageUrl
*
* @param string $externalImageUrl
* @return $this
*/
public function setExternalImageUrl($externalImageUrl)
{
$this->externalImageUrl = $externalImageUrl;
return $this;
}
/**
* Get externalImageUrl
*
* @return string $externalImageUrl
*/
public function getExternalImageUrl()
{
return $this->externalImageUrl;
}
}
......@@ -24,8 +24,7 @@ class UploadDirectoryNamer implements DirectoryNamerInterface
public function directoryName($object, PropertyMapping $mapping)
{
$name = $this->getDirectoryPathFromKey($mapping->getMappingName());
dump($name);
$name = $this->getDirectoryPathFromKey($object->getVichUploadFileKey());
return $name;
}
......
......@@ -7,7 +7,7 @@
*
* @copyright Copyright (c) 2016 Sebastian Castro - 90scastro@gmail.com
* @license MIT License
* @Last Modified time: 2018-06-07 08:07:06
* @Last Modified time: 2018-06-08 20:06:57
*/
namespace Biopen\GeoDirectoryBundle\Document;
......@@ -218,7 +218,7 @@ class Element
/**
* Images, photos, logos, linked to an element
*
* @MongoDB\EmbedMany(targetDocument="Biopen\GeoDirectoryBundle\Document\ElementImage")
* @MongoDB\EmbedMany(targetDocument="Biopen\CoreBundle\Document\Image")
*/
private $images;
......@@ -1429,9 +1429,9 @@ class Element
/**
* Add image
*
* @param Biopen\GeoDirectoryBundle\Document\ElementImage $image
* @param Biopen\CoreBundle\Document\Image $image
*/
public function addImage(\Biopen\GeoDirectoryBundle\Document\ElementImage $image)
public function addImage(\Biopen\CoreBundle\Document\Image $image)
{
$this->images[] = $image;
}
......@@ -1439,9 +1439,9 @@ class Element
/**
* Remove image
*
* @param Biopen\GeoDirectoryBundle\Document\ElementImage $image
* @param Biopen\CoreBundle\Document\Image $image
*/
public function removeImage(\Biopen\GeoDirectoryBundle\Document\ElementImage $image)
public function removeImage(\Biopen\CoreBundle\Document\Image $image)
{
$this->images->removeElement($image);
}
......
......@@ -13,6 +13,8 @@ use Biopen\CoreBundle\Document\AbstractFile;
*/
class Import extends AbstractFile
{
protected $vichUploadFileKey = "import_file";
/**
* @var int
* @MongoDB\Id(strategy="INCREMENT")
......@@ -43,22 +45,9 @@ class Import extends AbstractFile
/**
* @MongoDB\Field(type="bool")
*/
private $geocodeIfNecessary;
protected $vichUploadFileKey = "import_file";
/**
* NOTE: This is not a mapped field of entity metadata, just a simple property.
*
* @Vich\UploadableField(mapping="import_file", fileNameProperty="fileName", size="fileSize")
*
* @var File
*/
protected $file;
private $geocodeIfNecessary;
public function __construct()
{
}
public function __construct() {}
/**
* Get id
......
......@@ -7,7 +7,7 @@
*
* @copyright Copyright (c) 2016 Sebastian Castro - 90scastro@gmail.com
* @license MIT License
* @Last Modified time: 2018-06-06 10:55:49
* @Last Modified time: 2018-06-08 19:41:29
*/
namespace Biopen\GeoDirectoryBundle\Form;
......@@ -17,7 +17,7 @@ use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\FileType;
class ElementImageType extends AbstractType
class ImageType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
......@@ -35,7 +35,7 @@ class ElementImageType extends AbstractType
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Biopen\GeoDirectoryBundle\Document\ElementImage'
'data_class' => 'Biopen\CoreBundle\Document\Image'
));
}
......
......@@ -16,7 +16,7 @@ use Biopen\GeoDirectoryBundle\Document\InteractionType;
use Biopen\GeoDirectoryBundle\Document\UserRoles;
use Biopen\GeoDirectoryBundle\Document\PostalAddress;
use Biopen\GeoDirectoryBundle\Document\ElementUrl;
use Biopen\GeoDirectoryBundle\Document\ElementImage;
use Biopen\CoreBundle\Document\Image;
class ImportCsvService
{
......@@ -95,7 +95,8 @@ class ImportCsvService
$new_element->setEmail($row['email']);
$new_element->setOpenHoursMoreInfos($row['openHoursString']);
$new_element->setSourceKey(strlen($row['source']) > 0 ? $row['source'] : $sourceKey);
if (array_key_exists('publisher', $row)) $new_element->setUserOwnerEmail($row['publisher']);
$lat = 0;
$lng = 0;
......@@ -198,7 +199,7 @@ class ImportCsvService
{
if (strlen($imageUrl) > 5)
{
$elementImage = new ElementImage();
$elementImage = new Image();
$elementImage->setExternalImageUrl($imageUrl);
$element->addImage($elementImage);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment