Skip to content

Commit

Permalink
[TASK] Substitude placeholders in name in saveFinisher (#20)
Browse files Browse the repository at this point in the history
* [TASK] Substitude placeholders in name in saveFinisher

* [REFACTOR] Refactor
  • Loading branch information
Pavlo Zaporozkyi authored Aug 23, 2019
1 parent eabdaff commit 2af1905
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
28 changes: 25 additions & 3 deletions Classes/Domain/Finishers/SaveFormFinisher.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,16 @@ protected function executeInternal()
$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->saveForm->setName(
$this->generateName()
);

$this->attachFiles($formRuntime);

Expand Down Expand Up @@ -188,4 +188,26 @@ protected function initializeStandaloneView(FormRuntime $formRuntime): Standalon

return $standaloneView;
}

/**
* @return string
* @throws \TYPO3\CMS\Extbase\Persistence\Generic\Exception\InvalidNumberOfConstraintsException
*/
protected function generateName(): string
{
$name = $this->parseOption('name');

// If name is not found - set default
if (!is_string($name)) {
$name = 'Form';
}

// Add count postfix
$existingNamesCount = $this->formRepository->countByNameSimilarity($this->pid, $name);
if ($existingNamesCount > 0) {
$name = $name . ' #' . ++$existingNamesCount;
}

return $name;
}
}
16 changes: 12 additions & 4 deletions Classes/Domain/Repository/FormRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/

use TYPO3\CMS\Extbase\Persistence\Generic\Query;
use TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings;
use TYPO3\CMS\Extbase\Persistence\Repository;

Expand All @@ -53,18 +54,25 @@ public function initializeObject()
}

/**
* count records
*
* @param int $pid
* @param string $name
* @return int
* @throws \TYPO3\CMS\Extbase\Persistence\Generic\Exception\InvalidNumberOfConstraintsException
*/
public function countByPid($pid)
public function countByNameSimilarity(int $pid, string $name = '')
{
/** @var Query $query */
$query = $this->createQuery();

return $query
->matching(
$query->equals('pid', $pid)
$query->logicalAnd(
$query->equals('pid', $pid),
$query->logicalOr(
$query->equals('name', $name),
$query->like('name', $name . ' #%')
)
)
)
->execute()
->count();
Expand Down
4 changes: 2 additions & 2 deletions ext_emconf.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
'uploadfolder' => '0',
'createDirs' => '',
'clearCacheOnLoad' => 1,
'version' => '3.2.5',
'version' => '3.2.6',
'constraints' => [
'depends' => [
'typo3' => '8.7.0-9.5.99',
Expand All @@ -31,4 +31,4 @@
'suggests' => [
],
]
];
];

0 comments on commit 2af1905

Please sign in to comment.