Skip to content

Commit

Permalink
Merge pull request sulu#54 from alexander-schranz/develop
Browse files Browse the repository at this point in the history
Release: 1.0.0-RC1
  • Loading branch information
alexander-schranz authored Aug 4, 2016
2 parents cae205d + f54d291 commit 94613b0
Show file tree
Hide file tree
Showing 100 changed files with 6,776 additions and 900 deletions.
50 changes: 47 additions & 3 deletions Admin/FormAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,70 @@
use Sulu\Bundle\AdminBundle\Admin\Admin;
use Sulu\Bundle\AdminBundle\Navigation\Navigation;
use Sulu\Bundle\AdminBundle\Navigation\NavigationItem;
use Sulu\Component\Security\Authorization\SecurityCheckerInterface;

/**
* Generated by https://github.com/alexander-schranz/sulu-backend-bundle.
*/
class FormAdmin extends Admin
{
/**
* FormAdmin constructor.
*
* @param $title
* @param SecurityCheckerInterface $securityChecker
* @param string $title
*/
public function __construct($title)
{
public function __construct(
SecurityCheckerInterface $securityChecker,
$title
) {
// set root navigation
$rootNavigationItem = new NavigationItem($title);

// parent navigation
$section = new NavigationItem('navigation.modules');

// create section
if ($securityChecker->hasPermission('l91.sulu.form.forms', 'view')) {
$navigationItem = new NavigationItem('l91_sulu_form.form');
$navigationItem->setIcon('magic');
$navigationItem->setAction('l91/forms');
$navigationItem->setPosition(10);
$section->addChild($navigationItem);
$rootNavigationItem->addChild($section);
}

// set navigation
$this->setNavigation(new Navigation($rootNavigationItem));
}

/**
* {@inheritdoc}
*/
public function getCommands()
{
return [];
}

/**
* {@inheritdoc}
*/
public function getJsBundleName()
{
return 'l91suluform';
}

/**
* {@inheritdoc}
*/
public function getSecurityContexts()
{
return [
'Sulu' => [
'Form' => [
'l91.sulu.form.forms',
],
],
];
}
}
30 changes: 30 additions & 0 deletions Admin/FormNavigationProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace L91\Sulu\Bundle\FormBundle\Admin;

use Sulu\Bundle\AdminBundle\Navigation\ContentNavigationItem;
use Sulu\Bundle\AdminBundle\Navigation\ContentNavigationProviderInterface;

/**
* Generated by https://github.com/alexander-schranz/sulu-backend-bundle.
*/
class FormNavigationProvider implements ContentNavigationProviderInterface
{
/**
* {@inheritdoc}
*/
public function getNavigationItems(array $options = [])
{
$navigationItems = [];

$navigationItems['detail'] = new ContentNavigationItem('l91.sulu.form.navigation.details');
$navigationItems['detail']->setAction('general');
$navigationItems['detail']->setComponent('forms@l91suluform');
$navigationItems['detail']->setComponentOptions([
'display' => 'form',
'content' => 'general',
]);

return $navigationItems;
}
}
10 changes: 5 additions & 5 deletions Admin/TemplateNavigationProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@ public function __construct(
*
* @return array
*/
public function getNavigationItems(array $options = array())
public function getNavigationItems(array $options = [])
{
$items = [];

foreach ($this->listProviderRegistry->getProviders() as $name => $provider) {
$item = new ContentNavigationItem('Formular');
$item->setAction('form-list');
$item->setDisplay(array('edit'));
$item->setDisplay(['edit']);
$item->setComponent('content/list@l91suluform');
$item->setComponentOptions(array(
'template' => $name
));
$item->setComponentOptions([
'template' => $name,
]);

$item->setDisplayConditions(
[
Expand Down
190 changes: 190 additions & 0 deletions Content/Types/FormSelect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
<?php

namespace L91\Sulu\Bundle\FormBundle\Content\Types;

use Doctrine\ORM\NoResultException;
use L91\Sulu\Bundle\FormBundle\Entity\Dynamic;
use L91\Sulu\Bundle\FormBundle\Form\HandlerInterface;
use L91\Sulu\Bundle\FormBundle\Form\Type\DynamicFormType;
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;

/**
* ContentType for selecting a form.
*/
class FormSelect extends SimpleContentType
{
/**
* @var string
*/
private $template;

/**
* @var FormRepository
*/
private $formRepository;

/**
* @var RequestStack
*/
private $requestStack;

/**
* @var FormFactoryInterface
*/
private $formFactory;

/**
* @var HandlerInterface
*/
private $formHandler;

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

/**
* FormSelect constructor.
*
* @param string $template
* @param FormRepository $formRepository
* @param RequestStack $requestStack
* @param FormFactoryInterface $formFactory
* @param HandlerInterface $formHandler
* @param SystemCollectionManagerInterface $systemCollectionManager
*/
public function __construct(
$template,
FormRepository $formRepository,
RequestStack $requestStack,
FormFactoryInterface $formFactory,
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;
}

/**
* {@inheritdoc}
*/
public function getContentData(PropertyInterface $property)
{
$id = (int) $property->getValue();

if (!$id) {
return;
}

$request = $this->requestStack->getCurrentRequest();

$form = null;

try {
// Create Dynamic Data
$uuid = $property->getStructure()->getUuid();
$webspaceKey = $property->getStructure()->getWebspaceKey();
$locale = $property->getStructure()->getLanguageCode();
$formEntity = $this->formRepository->findById($id, $locale);

// set Defaults
$defaults = [];
foreach ($formEntity->getFields() as $field) {
$translation = $field->getTranslation($locale);

if ($translation && $translation->getDefaultValue()) {
$value = $translation->getDefaultValue();

// handle special types
switch ($field->getType()) {
case Dynamic::TYPE_DATE:
$value = new \DateTime($value);
break;
case Dynamic::TYPE_DROPDOWN_MULTIPLE:
case Dynamic::TYPE_CHECKBOX_MULTIPLE:
$value = preg_split('/\r\n|\r|\n/', $value, -1, PREG_SPLIT_NO_EMPTY);
break;
}

$defaults[$field->getKey()] = $value;
}
}

// Create Form Type
$formType = new DynamicFormType(
$formEntity,
$locale,
$property->getName(),
$property->getStructure()->getView(),
$this->systemCollectionManager->getSystemCollection('l91_sulu_form.attachments')
);

$form = $this->formFactory->create(
$formType,
new Dynamic($uuid, $locale, $id, $webspaceKey, $defaults)
);

// handle request
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
// save
$this->formHandler->handle(
$form,
[
'_form_type' => $formType,
'formEntity' => $formEntity->serializeForLocale($locale, $form->getData()),
]
);

// Do redirect after success
throw new HttpException(302, null, null, ['Location' => '?send=true']);
}

return $form->createView();
} catch (NoResultException $e) {
// do nothing
}

return;
}

/**
* {@inheritdoc}
*/
public function getViewData(PropertyInterface $property)
{
$id = (int) $property->getValue();

if (!$id) {
return [];
}

$locale = $property->getStructure()->getLanguageCode();

$formEntity = $this->formRepository->findById($id, $locale);

return [
'entity' => $formEntity->serializeForLocale($locale),
];
}

/**
* {@inheritdoc}
*/
public function getTemplate()
{
return $this->template;
}
}
Loading

0 comments on commit 94613b0

Please sign in to comment.