Skip to content

Commit

Permalink
Merge pull request sulu#47 from alexander-schranz/feature/dynamic-for…
Browse files Browse the repository at this point in the history
…m-extensions

Finish File Upload
  • Loading branch information
thomasduenser authored Jul 26, 2016
2 parents 4e171ad + be75c93 commit 50e83a0
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 14 deletions.
15 changes: 12 additions & 3 deletions Content/Types/FormSelect.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use L91\Sulu\Bundle\FormBundle\Repository\FormRepository;
use Sulu\Component\Content\Compat\PropertyInterface;
use Sulu\Component\Content\SimpleContentType;
use Sulu\Component\Media\SystemCollections\SystemCollectionManagerInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Exception\HttpException;
Expand Down Expand Up @@ -43,6 +44,11 @@ class FormSelect extends SimpleContentType
*/
private $formHandler;

/**
* @var SystemCollectionManagerInterface
*/
private $systemCollectionManager;

/**
* FormSelect constructor.
*
Expand All @@ -51,20 +57,23 @@ class FormSelect extends SimpleContentType
* @param RequestStack $requestStack
* @param FormFactoryInterface $formFactory
* @param HandlerInterface $formHandler
* @param SystemCollectionManagerInterface $systemCollectionManager
*/
public function __construct(
$template,
FormRepository $formRepository,
RequestStack $requestStack,
FormFactoryInterface $formFactory,
HandlerInterface $formHandler
HandlerInterface $formHandler,
SystemCollectionManagerInterface $systemCollectionManager
) {
parent::__construct('FormSelect', '');
$this->template = $template;
$this->formRepository = $formRepository;
$this->requestStack = $requestStack;
$this->formFactory = $formFactory;
$this->formHandler = $formHandler;
$this->systemCollectionManager = $systemCollectionManager;
}

/**
Expand Down Expand Up @@ -104,8 +113,8 @@ public function getContentData(PropertyInterface $property)
$formEntity,
$locale,
$property->getName(),
$property->getStructure()->getView()
// TODO collection id of systemCollection
$property->getStructure()->getView(),
$this->systemCollectionManager->getSystemCollection()
);

$form = $this->formFactory->create(
Expand Down
29 changes: 28 additions & 1 deletion DependencyInjection/L91SuluFormExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

Expand All @@ -12,8 +13,34 @@
*
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
*/
class L91SuluFormExtension extends Extension
class L91SuluFormExtension extends Extension implements PrependExtensionInterface
{
/**
* Allow an extension to prepend the extension configurations.
*
* @param ContainerBuilder $container
*/
public function prepend(ContainerBuilder $container)
{
if ($container->hasExtension('sulu_media')) {
$container->prependExtensionConfig(
'sulu_media',
[
'system_collections' => [
'l91_sulu_form' => [
'meta_title' => ['en' => 'Sulu forms', 'de' => 'Sulu Formulare'],
'collections' => [
'attachments' => [
'meta_title' => ['en' => 'Attachments', 'de' => 'Anhänge'],
],
],
],
],
]
);
}
}

/**
* {@inheritdoc}
*/
Expand Down
36 changes: 28 additions & 8 deletions Form/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,28 +142,27 @@ public function handle(FormInterface $form, $attributes = [])

if ($type instanceof TypeInterface) {
foreach ($type->getFileFields() as $field) {
if (!$form->has($field)) {
if (!$form->has($field) || !count($form[$field]->getData())) {
continue;
}

$files = $form[$field]->getData();

if (!count($files)) {
continue;
}

$type = $this->formExtension->getType($form->getName());
$collectionId = $type->getCollectionId();
$ids = [];

// convert $files to array
if (!is_array($files)) {
$files = [$files];
}

/** @var UploadedFile $file */
foreach ($files as $file) {
if ($file instanceof UploadedFile) {
$media = $this->mediaManager->save(
$file,
[
'collection' => $collectionId,
'locale' => $form->get('locale')->getData(),
'locale' => $this->getFormLocale($form),
'title' => $file->getClientOriginalName(),
],
null
Expand Down Expand Up @@ -263,6 +262,27 @@ protected function saveForm(FormInterface $form, $attributes = [], $mediaIds = [
);
}

/**
* @description Returns the correct form locale.
* TODO What's the correct way to handle both types?
*
* @param FormInterface $form
*
* @return string
*/
public function getFormLocale($form)
{
$locale = 'de';

if ($form->has('locale')) {
$locale = $form->get('locale')->getData();
} elseif ($form->getData()->locale) {
$locale = $form->getData()->locale;
}

return $locale;
}

/**
* @param $name
*
Expand Down
32 changes: 30 additions & 2 deletions Form/Type/DynamicFormType.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\CountryType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
Expand Down Expand Up @@ -38,20 +39,27 @@ class DynamicFormType extends AbstractType
*/
private $name;

/**
* @var int
*/
private $systemCollectionId;

/**
* DynamicFormType constructor.
*
* @param Form $formEntity
* @param string $locale
* @param string $name
* @param string $structureView
* @param int $systemCollectionId
*/
public function __construct($formEntity, $locale, $name, $structureView)
public function __construct($formEntity, $locale, $name, $structureView, $systemCollectionId)
{
$this->formEntity = $formEntity;
$this->locale = $locale;
$this->name = $name;
$this->structureView = $structureView;
$this->systemCollectionId = $systemCollectionId;
}

/**
Expand Down Expand Up @@ -115,6 +123,10 @@ public function buildForm(FormBuilderInterface $builder, array $options)
case 'country':
$type = CountryType::class;
break;
case 'date':
$type = DateType::class;
$options['widget'] = 'single_text';
break;
case 'attachment':
$type = FileType::class;
break;
Expand Down Expand Up @@ -266,7 +278,23 @@ public function getNotifySendAttachments($formData = [])
*/
public function getCollectionId()
{
// TODO
return $this->systemCollectionId;
}

/**
* {@inheritdoc}
*/
public function getFileFields()
{
$fileFields = [];

foreach ($this->formEntity->getFields() as $field) {
if ('attachment' === $field->getType()) {
$fileFields[] = $field->getKey();
}
}

return $fileFields;
}

/**
Expand Down
1 change: 1 addition & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
<argument id="request_stack" type="service" />
<argument id="form.factory" type="service" />
<argument id="l91.sulu.form.handler" type="service" />
<argument type="service" id="sulu_media.system_collections.manager"/>

<tag name="sulu.content.type" alias="form_select"/>
</service>
Expand Down

0 comments on commit 50e83a0

Please sign in to comment.