Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding new 'cmf_media_file' form type #137

Merged
merged 1 commit into from
Jul 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions Form/Type/FileType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

/*
* This file is part of the Symfony CMF package.
*
* (c) 2011-2014 Symfony CMF
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Cmf\Bundle\MediaBundle\Form\Type;

use Symfony\Cmf\Bundle\MediaBundle\File\UploadFileHelperInterface;
use Symfony\Cmf\Bundle\MediaBundle\Form\DataTransformer\ModelToFileTransformer;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

/**
* Form type which transforms an uploaded file to an object implementing the
* Symfony\Cmf\Bundle\MediaBundle\FileInterface
*
* It renders as a file upload button with a link for downloading the existing
* file, if any.
*
* Usage: you need to supply the object class to which the file will be
* transformed (which should implement FileInterface) and an UploadFileHelper,
* which will handle the UploadedFile and create the transformed object.
*/
class FileType extends AbstractType
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a docblock for the class, explaining what this is (mainly why we need this on top of the standard file type). i know that the existing code is not setting a good example, but lets try to improve things.

{
/**
* @var string
*/
protected $dataClass;

/**
* @var UploadFileHelperInterface
*/
protected $uploadFileHelper;

/**
* @param string $class
* @param UploadFileHelperInterface $uploadFileHelper
*/
public function __construct($class, UploadFileHelperInterface $uploadFileHelper)
{
$this->dataClass = $class;
$this->uploadFileHelper = $uploadFileHelper;
}

/**
* {@inheritdoc}
*/
public function getParent()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add /** {@inheritDoc} */ to methods that are inherited and need no further comment

{
return 'file';
}

/**
* {@inheritdoc}
*/
public function getName()
{
return 'cmf_media_file';
}

/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$transformer = new ModelToFileTransformer($this->uploadFileHelper, $options['data_class']);
$builder->addModelTransformer($transformer);
}

/**
* {@inheritdoc}
*/
public function setDefaultOptions(OptionsResolverInterface $options)
{
$this->configureOptions($options);
}

/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $options)
{
$options->setDefaults(array('data_class' => $this->dataClass));
}
}
66 changes: 37 additions & 29 deletions Form/Type/ImageType.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,37 @@
namespace Symfony\Cmf\Bundle\MediaBundle\Form\Type;

use Symfony\Cmf\Bundle\MediaBundle\File\UploadFileHelperInterface;
use Symfony\Cmf\Bundle\MediaBundle\Form\DataTransformer\ModelToFileTransformer;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class ImageType extends AbstractType
/**
* Form type which transforms an uploaded file to an object implementing the
* Symfony\Cmf\Bundle\MediaBundle\ImageInterface
*
* It renders as a file upload button and provides a preview of the uploaded
* image, if any.
* To see the preview you can use the twig template provided by this bundle.
*
* Usage: you need to supply the object class to which the file will be
* transformed (which should implement ImageInterface) and an UploadFileHelper,
* which will handle the UploadedFile and create the transformed object.
*
* If the LiipImagineBundle is used in your project, you can configure the imagine
* filter to use for the preview, as well as additional filters to remove from cache
* when the image is replaced. If the filter is not specified, it defaults to
* image_upload_thumbnail.
*/
class ImageType extends FileType
{
private $dataClass;
private $uploadFileHelper;
/**
* @var bool
*/
private $useImagine;

/**
* @var bool
*/
private $defaultFilter;

/**
Expand All @@ -35,43 +53,33 @@ class ImageType extends AbstractType
*/
public function __construct($class, UploadFileHelperInterface $uploadFileHelper, $useImagine = false, $defaultFilter = false)
{
$this->dataClass = $class;
$this->uploadFileHelper = $uploadFileHelper;
parent::__construct($class, $uploadFileHelper);
$this->useImagine = $useImagine;
$this->defaultFilter = $this->useImagine ? $defaultFilter : false;
}

public function getParent()
{
return 'file';
}

/**
* {@inheritdoc}
*/
public function getName()
{
return 'cmf_media_image';
}

public function buildForm(FormBuilderInterface $builder, array $options)
{
$transformer = new ModelToFileTransformer($this->uploadFileHelper, $options['data_class']);
$builder->addModelTransformer($transformer);
}

/**
* {@inheritdoc}
*/
public function buildView(FormView $view, FormInterface $form, array $options)
{
$view->vars['imagine_filter'] = $this->useImagine ? $options['imagine_filter'] : false;
}

public function setDefaultOptions(OptionsResolverInterface $options)
{
$this->configureOptions($options);
}

/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $options)
{
$options->setDefaults(array(
'data_class' => $this->dataClass,
'imagine_filter' => $this->defaultFilter,
));
parent::configureOptions($options);
$options->setDefaults(array('imagine_filter' => $this->defaultFilter));
}
}
7 changes: 7 additions & 0 deletions Resources/config/persistence-phpcr.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<parameter key="cmf_media.upload_editor_helper.default.class">Symfony\Cmf\Bundle\MediaBundle\Editor\Helper\UploadDefaultHelper</parameter>
<parameter key="cmf_media.upload_editor_helper.ckeditor.class">Symfony\Cmf\Bundle\MediaBundle\Editor\Helper\UploadCkeditorHelper</parameter>

<parameter key="cmf_media.form.file.class">Symfony\Cmf\Bundle\MediaBundle\Form\Type\FileType</parameter>
<parameter key="cmf_media.form.image.class">Symfony\Cmf\Bundle\MediaBundle\Form\Type\ImageType</parameter>

<parameter key="cmf_media.persistence.phpcr.subscriber.stream_rewind.class">Symfony\Cmf\Bundle\MediaBundle\Doctrine\DoctrineStreamRewindSubscriber</parameter>
Expand Down Expand Up @@ -120,6 +121,12 @@
<argument type="service" id="router" />
</service>

<service id="cmf_media.form.type.file" class="%cmf_media.form.file.class%">
<tag name="form.type" alias="cmf_media_file" />
<argument>%cmf_media.persistence.phpcr.file.class%</argument>
<argument type="service" id="cmf_media.upload_file_helper" />
</service>

<service id="cmf_media.form.type.image" class="%cmf_media.form.image.class%">
<tag name="form.type" alias="cmf_media_image" />
<argument>%cmf_media.persistence.phpcr.image.class%</argument>
Expand Down
9 changes: 9 additions & 0 deletions Resources/views/Form/fields.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,12 @@
{% endif %}
{% endblock %}
{% endblock %}

{% block cmf_media_file_widget %}
{{ form_widget(form) }}
{% block cmf_media_file_widget_download %}
{% if form.vars.data and form.vars.data.id is defined and form.vars.data.id %}
<a href="{{ cmf_media_download_url(form.vars.data) }}">{{ form.vars.data.name }}</a>
{% endif %}
{% endblock %}
{% endblock %}
117 changes: 114 additions & 3 deletions Tests/Resources/Controller/PhpcrFileTestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@

namespace Symfony\Cmf\Bundle\MediaBundle\Tests\Resources\Controller;

use Doctrine\ODM\PHPCR\Document\Generic;
use PHPCR\Util\PathHelper;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Cmf\Bundle\MediaBundle\File\UploadFileHelperInterface;
use Symfony\Cmf\Bundle\MediaBundle\Tests\Resources\Document\Content;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class PhpcrFileTestController extends Controller
{
Expand All @@ -25,6 +29,32 @@ public function getUploadForm()
;
}

protected function getContentForm(Content $contentObject = null)
{
$is_new = is_null($contentObject);
if ($is_new) {
$contentObject = new Content();
}

return $this->createFormBuilder($contentObject)
->add('name')
->add('title')
->add('file', 'cmf_media_file', array('required' => $is_new))
->getForm()
;
}

protected function getUrlSafePath($object)
{
return ltrim($object->getId(), '/');
}

protected function mapUrlSafePathToId($path)
{
// The path is being the id
return PathHelper::absolutizePath($path, '/');
}

public function indexAction(Request $request)
{
$fileClass = 'Symfony\Cmf\Bundle\MediaBundle\Doctrine\Phpcr\File';
Expand All @@ -34,13 +64,65 @@ public function indexAction(Request $request)
$uploadForm = $this->getUploadForm();
$editorUploadForm = $this->getUploadForm();

// get a content object
$contentClass = 'Symfony\Cmf\Bundle\MediaBundle\Tests\Resources\Document\Content';
$contentObject = $dm->getRepository($contentClass)->findOneBy(array());

// Form - content object with file embedded
$newContentForm = $this->getContentForm();
$editContentForm = $this->getContentForm($contentObject);

// action url for editContentForm
if ($contentObject) {
$editContentFormAction = $this->generateUrl('phpcr_file_test_content_edit', array(
'path' => $this->getUrlSafePath($contentObject),
));
} else {
$editContentFormAction = false;
}

return $this->render('::tests/file.html.twig', array(
'upload_form' => $uploadForm->createView(),
'editor_form' => $editorUploadForm->createView(),
'files' => $files,
'upload_form' => $uploadForm->createView(),
'editor_form' => $editorUploadForm->createView(),
'content_form_new' => $newContentForm->createView(),
'content_form_edit' => $editContentForm->createView(),
'content_form_edit_action' => $editContentFormAction,
'files' => $files,
));
}

public function newAction(Request $request)
{
$dm = $this->get('doctrine_phpcr')->getManager('default');
$contentRoot = $dm->find(null, '/test/content');

if (!$contentRoot) {
$root = $dm->find(null, '/test');
$contentRoot = new Generic();
$contentRoot->setNodename('content');
$contentRoot->setParent($root);
$dm->persist($contentRoot);
}

$contentObject = new Content();
$contentObject->setParent($contentRoot);

$form = $this->getContentForm($contentObject);

if ($request->isMethod('POST')) {
$form->bind($request);

if ($form->isValid()) {
// persist
$dm = $this->get('doctrine_phpcr')->getManager('default');
$dm->persist($contentObject);
$dm->flush();
}
}

return $this->redirect($this->generateUrl('phpcr_file_test'));
}

public function uploadAction(Request $request)
{
$form = $this->getUploadForm();
Expand All @@ -63,4 +145,33 @@ public function uploadAction(Request $request)

return $this->redirect($this->generateUrl('phpcr_file_test'));
}

public function editAction(Request $request, $path)
{
$dm = $this->get('doctrine_phpcr')->getManager('default');

$contentObject = $dm->find(null, $this->mapUrlSafePathToId($path));

if (!$contentObject || !$contentObject instanceof Content) {
throw new NotFoundHttpException(sprintf(
'Object with identifier %s cannot be resolved to a valid instance of Symfony\Cmf\Bundle\MediaBundle\Tests\Resources\Document\Content',
$path
));
}

$form = $this->getContentForm($contentObject);

if ($request->isMethod('POST')) {
$form->bind($request);

if ($form->isValid()) {
// persist
$dm = $this->get('doctrine_phpcr')->getManager('default');
$dm->persist($contentObject);
$dm->flush();
}
}

return $this->redirect($this->generateUrl('phpcr_file_test'));
}
}
Loading