Skip to content

Commit

Permalink
Merge pull request #5 from pixelant/development
Browse files Browse the repository at this point in the history
[TASK] compatibility with TYPO3 8.7
  • Loading branch information
MattiasNilsson authored Apr 7, 2017
2 parents 504de14 + 4a434a8 commit 4db95ee
Show file tree
Hide file tree
Showing 59 changed files with 1,181 additions and 2,246 deletions.
191 changes: 191 additions & 0 deletions Classes/Domain/Finishers/SaveFormFinisher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
<?php
declare(strict_types=1);

namespace Pixelant\PxaFormEnhancement\Domain\Finishers;

/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/

use Pixelant\PxaFormEnhancement\Domain\Model\FileReference as AttachFileReference;
use Pixelant\PxaFormEnhancement\Domain\Model\Form;
use Pixelant\PxaFormEnhancement\Domain\Repository\FormRepository;
use TYPO3\CMS\Core\Resource\ResourceFactory;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager;
use TYPO3\CMS\Form\Domain\Finishers\AbstractFinisher;
use TYPO3\CMS\Extbase\Domain\Model\FileReference;
use TYPO3\CMS\Fluid\View\StandaloneView;
use TYPO3\CMS\Form\Domain\Finishers\Exception\FinisherException;
use TYPO3\CMS\Form\Domain\Model\FormElements\FileUpload;
use TYPO3\CMS\Form\Domain\Runtime\FormRuntime;
use TYPO3\CMS\Form\ViewHelpers\RenderRenderableViewHelper;

/**
* This finisher redirects to another Controller.
*
* Scope: frontend
*/
class SaveFormFinisher extends AbstractFinisher
{

/**
* @var array
*/
protected $defaultOptions = [
'pageUid' => 1,
'name' => '',
];

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

/**
* @var ResourceFactory
*/
protected $resourceFactory;

/**
* @var Form
*/
protected $saveForm;

/**
* Storage for records
*
* @var int
*/
protected $pid = 0;

/**
* Executes this finisher
* @see AbstractFinisher::execute()
*/
protected function executeInternal()
{
$this->formRepository = $this->objectManager->get(FormRepository::class);
$this->resourceFactory = $this->objectManager->get(ResourceFactory::class);
$this->saveForm = $this->objectManager->get(Form::class);
$this->pid = $this->getPid();

$count = $this->formRepository->countByPid($this->pid);

$formRuntime = $this->finisherContext->getFormRuntime();
$standaloneView = $this->initializeStandaloneView($formRuntime);

$message = trim($standaloneView->render());

$this->saveForm->setFormData($message);
$this->saveForm->setPid($this->pid);
$this->saveForm->setName($this->options['name'] . ' #' . ++$count);

$this->attachFiles($formRuntime);

$this->formRepository->add($this->saveForm);

$this->objectManager->get(PersistenceManager::class)->persistAll();
}

/**
* Attach files
*
* @param FormRuntime $formRuntime
*/
protected function attachFiles(FormRuntime $formRuntime)
{
$elements = $formRuntime->getFormDefinition()->getRenderablesRecursively();

foreach ($elements as $element) {
if ($element instanceof FileUpload) {
$file = $formRuntime[$element->getIdentifier()];

if ($file) {
/** @var AttachFileReference $attachment */
$attachment = $this->objectManager->get(AttachFileReference::class);

if ($file instanceof FileReference) {
$file = $file->getOriginalResource();
}

$newFileReferenceObject = $this->resourceFactory->createFileReferenceObject(
[
'uid_local' => $file->getOriginalFile()->getUid(),
'uid_foreign' => uniqid('NEW_'),
'uid' => uniqid('NEW_')
]
);

$attachment->setOriginalResource($newFileReferenceObject);
$attachment->setPid($this->pid);

$this->saveForm->addAttachment($attachment);
}
}
}
}

/**
* Get pid as storage
*
* @return int
*/
protected function getPid(): int
{
if (GeneralUtility::isFirstPartOfStr($this->options['pageUid'], 'pages_')) {
$pid = (int)substr($this->options['pageUid'], 6);
} else {
$pid = (int)$this->options['pageUid'];
}

return $pid;
}
/**
* @param FormRuntime $formRuntime
* @return StandaloneView
* @throws FinisherException
*/
protected function initializeStandaloneView(FormRuntime $formRuntime): StandaloneView
{
if (!isset($this->options['templatePathAndFilename'])) {
throw new FinisherException(
'The option "templatePathAndFilename" must be set for the EmailFinisher.',
1327058829
);
}

/** @var StandaloneView $standaloneView */
$standaloneView = $this->objectManager->get(StandaloneView::class);
$standaloneView->setTemplatePathAndFilename($this->options['templatePathAndFilename']);
$standaloneView->assign('finisherVariableProvider', $this->finisherContext->getFinisherVariableProvider());

if (isset($this->options['partialRootPaths']) && is_array($this->options['partialRootPaths'])) {
$standaloneView->setPartialRootPaths($this->options['partialRootPaths']);
}

if (isset($this->options['layoutRootPaths']) && is_array($this->options['layoutRootPaths'])) {
$standaloneView->setLayoutRootPaths($this->options['layoutRootPaths']);
}

if (isset($this->options['variables'])) {
$standaloneView->assignMultiple($this->options['variables']);
}

$standaloneView->assign('form', $formRuntime);
$standaloneView->getRenderingContext()
->getViewHelperVariableContainer()
->addOrUpdate(RenderRenderableViewHelper::class, 'formRuntime', $formRuntime);

return $standaloneView;
}
}
68 changes: 37 additions & 31 deletions Classes/Domain/Model/FileReference.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Pixelant\PxaFormEnhancement\Domain\Model;

/***************************************************************
Expand All @@ -25,38 +26,43 @@
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/

use TYPO3\CMS\Extbase\Domain\Model\FileReference as AbstractFileReference;

/**
* Class FileReference
* @package Pixelant\PxaFormEnhancement\Domain\Model
*/
class FileReference extends \TYPO3\CMS\Extbase\Domain\Model\FileReference {

/**
* Uid of a sys_file
*
* @var integer
*/
protected $uidLocal;

/**
* tablenames
*
* @var string
*/
protected $tablenames = 'tx_pxaformenhancement_domain_model_form';

/**
* tableLocal
*
* @var string
*/
protected $tableLocal = 'sys_file';

/**
* @param \TYPO3\CMS\Core\Resource\ResourceInterface $originalResource
*/
public function setOriginalResource(\TYPO3\CMS\Core\Resource\ResourceInterface $originalResource) {
$this->originalResource = $originalResource;
$this->uidLocal = (int)$originalResource->getOriginalFile()->getUid();
}
}
class FileReference extends AbstractFileReference
{

/**
* Uid of a sys_file
*
* @var integer
*/
protected $uidLocal;

/**
* tablenames
*
* @var string
*/
protected $tablenames = 'tx_pxaformenhancement_domain_model_form';

/**
* tableLocal
*
* @var string
*/
protected $tableLocal = 'sys_file';

/**
* @param \TYPO3\CMS\Core\Resource\ResourceInterface $originalResource
*/
public function setOriginalResource(\TYPO3\CMS\Core\Resource\ResourceInterface $originalResource)
{
$this->originalResource = $originalResource;
$this->uidLocal = (int)$originalResource->getOriginalFile()->getUid();
}
}
Loading

0 comments on commit 4db95ee

Please sign in to comment.