Permalink
Browse files

added image document, type, tranformer and template

  • Loading branch information...
1 parent b6f9700 commit 55175f377f62e81e0c9396e769bcfde614fc51ae @elHornair elHornair committed Feb 22, 2013
View
61 Document/Image.php
@@ -0,0 +1,61 @@
+<?php
+
+namespace Doctrine\Bundle\PHPCRBundle\Document;
+
+use Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCRODM;
+
+/**
+ * @PHPCRODM\Document(referenceable=true)
+ */
+class Image
+{
+
+ /**
+ * @PHPCRODM\Id
+ */
+ protected $path;
+
+ /**
+ * Image file child
+ *
+ * @PHPCRODM\Child(name="file", cascade="persist")
+ */
+ protected $file;
+
+ /**
+ * @PHPCRODM\String
+ */
+ protected $mimeType;
+
+
+ public function setFile($file)
+ {
+ $this->file = $file;
+ }
+
+ public function getFile()
+ {
+ return $this->file;
+ }
+
+ public function setMimeType($mimeType)
+ {
+ $this->mimeType = $mimeType;
+ }
+
+ public function getMimeType()
+ {
+ return $this->mimeType;
+ }
+
+ public function getContent()
+ {
+ return $this->file->getFileContentAsStream();
+ }
+
+ public function __toString()
+ {
+ return $this->path;
+ }
+
+}
View
38 Form/DataTransformer/ModelToFileTransformer.php
@@ -0,0 +1,38 @@
+<?php
+
+namespace Doctrine\Bundle\PHPCRBundle\Form\DataTransformer;
+
+use Symfony\Component\Form\DataTransformerInterface;
+use Symfony\Component\HttpFoundation\File\UploadedFile;
+use Doctrine\ODM\PHPCR\Document\File as PHPCRFile;
+use Doctrine\Common\Util\Debug;
+use Doctrine\Bundle\PHPCRBundle\Document\Image;
+
+class ModelToFileTransformer implements DataTransformerInterface
+{
+
+ /**
+ * {@inheritdoc}
+ */
+ public function reverseTransform($uploadedFile)
+ {
+ if (! $uploadedFile instanceof UploadedFile) {
+ return $uploadedFile;
+ }
+
+ /** @var $uploadedFile UploadedFile */
+ $fileObj = new PHPCRFile();
+ $fileObj->setFileContentFromFilesystem($uploadedFile->getPathname());
+ $image = new Image();
+ $image->setFile($fileObj);
+ return $image;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function transform($image)
+ {
+ return $image;
+ }
+}
View
28 Form/Type/ImageType.php
@@ -0,0 +1,28 @@
+<?php
+
+namespace Doctrine\Bundle\PHPCRBundle\Form\Type;
+
+use Symfony\Component\Form\AbstractType;
+use Symfony\Component\Form\FormBuilderInterface;
+use Doctrine\Bundle\PHPCRBundle\Form\DataTransformer\ModelToFileTransformer;
+
+class ImageType extends AbstractType
+{
+
+ public function getParent()
+ {
+ return 'file';
+ }
+
+ public function getName()
+ {
+ return 'phpcr_image';
+ }
+
+ public function buildForm(FormBuilderInterface $builder, array $options)
+ {
+ $transformer = new ModelToFileTransformer();
+ $builder->addModelTransformer($transformer);
+ }
+
+}
View
4 Resources/config/odm.xml
@@ -60,6 +60,10 @@
<argument type="service" id="service_container"/>
</service>
+ <service id="form.type.phpcr.image" class="Doctrine\Bundle\PHPCRBundle\Form\Type\ImageType">
+ <tag name="form.type" alias="phpcr_image"/>
+ </service>
+
<service id="form.type.phpcr.document" class="Doctrine\Bundle\PHPCRBundle\Form\Type\DocumentType">
<tag name="form.type" alias="phpcr_document"/>
<argument type="service" id="doctrine_phpcr"/>
View
4 Resources/views/Form/fields.html.twig
@@ -0,0 +1,4 @@
+{% block phpcr_image_widget %}
+ {{ form_widget(form) }}
+ <img src="{{ form.vars.data | imagine_filter('phpcr_test') }}" alt="" />
+{% endblock %}

0 comments on commit 55175f3

Please sign in to comment.