Skip to content

Commit

Permalink
Merge pull request #1 from oskardydo/TASK/Initial-setup
Browse files Browse the repository at this point in the history
Task/initial setup
  • Loading branch information
lukaszuznanski authored Jul 10, 2020
2 parents 9b9ccdc + 9613df1 commit b7186ca
Show file tree
Hide file tree
Showing 33 changed files with 1,222 additions and 2 deletions.
55 changes: 55 additions & 0 deletions Classes/ViewHelpers/Form/GenerateNameViewHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
namespace FriendsOfTYPO3\HeadlessPowermail\ViewHelpers\Form;

/*
* 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!
*/

/**
* ViewHelper which creates a text field :html:`<input type="text">`.
*
* Examples
* ========
*
* Example::
*
* <f:form.textfield name="myTextBox" value="default value" />
*
* Output::
*
* <input type="text" name="myTextBox" value="default value" />
*/
class GenerateNameViewHelper extends \TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper
{
/**
* Initialize the arguments.
*
* @throws \TYPO3Fluid\Fluid\Core\ViewHelper\Exception
*/
public function initializeArguments()
{
$this->registerArgument(
'property',
'string',
'Name of Object Property. If used in conjunction with <f:form object="...">, "name" and "value" properties will be ignored.'
);
$this->registerArgument('name', 'string', 'Name of input tag');
}

/**
* @return string|void
*/
public function render()
{
return $this->getName();
}
}
65 changes: 65 additions & 0 deletions Classes/ViewHelpers/Form/MultiUploadViewHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

namespace FriendsOfTYPO3\HeadlessPowermail\ViewHelpers\Form;

use TYPO3\CMS\Fluid\ViewHelpers\Form\UploadViewHelper;

/**
* Class MultiUploadViewHelper
*/
class MultiUploadViewHelper extends UploadViewHelper
{

/**
* Initialize the arguments.
*
* @api
*/
public function initializeArguments()
{
parent::initializeArguments();
}

/**
* Renders the upload field.
*
* @return string
*/
public function render(): string
{
$name = $this->getName();
$allowedFields = ['name', 'type', 'tmp_name', 'error', 'size'];
foreach ($allowedFields as $fieldName) {
$this->registerFieldNameForFormTokenGeneration($name . '[' . $fieldName . '][]');
}
$this->tag->addAttribute('type', 'file');
$name .= '[]';
$this->tag->addAttribute('name', $name);
$this->setErrorClassAttribute();
return $this->renderInput();
}

/**
* Renders and returns the tag
*
* @return string
* @api
*/
public function renderInput()
{
$data = [];
if (empty($this->tagName)) {
return '';
}
foreach ($this->tag->getAttributes() as $attributeName => $attributeValue) {
$data[$attributeName] = $attributeValue;
}
$data['name'] = $this->tag->getTagName();
if ($this->tag->hasContent()) {
$data['content'] = $this->tag->getContent();
}
return json_encode($data);
}
}
71 changes: 71 additions & 0 deletions Classes/ViewHelpers/Form/RegisterFieldViewHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
namespace FriendsOfTYPO3\HeadlessPowermail\ViewHelpers\Form;

/*
* 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!
*/

/**
* Registers field for generating hidden fields
*/
class RegisterFieldViewHelper extends \TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper
{

/**
* Initialize the arguments.
*
* @throws \TYPO3Fluid\Fluid\Core\ViewHelper\Exception
*/
public function initializeArguments()
{
$this->registerArgument('name', 'string', 'Name of input tag');
$this->registerArgument('value', 'mixed', 'Value of input tag');
$this->registerArgument(
'property',
'string',
'Name of Object Property. If used in conjunction with <f:form object="...">, "name" and "value" properties will be ignored.'
);
$this->registerArgument('additionalAttributes', 'array', 'Additional tag attributes. They will be added directly to the resulting HTML tag.', false);
$this->registerArgument('checked', 'bool', 'Specifies that the input element should be preselected');
$this->registerArgument('multiple', 'bool', 'Specifies whether this checkbox belongs to a multivalue (is part of a checkbox group)', false, false);
}

/**
* @return string|void
*/
public function render()
{
$nameAttribute = $this->getName();

$checked = $this->arguments['checked'];
$multiple = $this->arguments['multiple'];

$propertyValue = null;
if ($this->hasMappingErrorOccurred()) {
$propertyValue = $this->getLastSubmittedFormData();
}
if ($checked === null && $propertyValue === null) {
$propertyValue = $this->getPropertyValue();
}

if ($propertyValue instanceof \Traversable) {
$propertyValue = iterator_to_array($propertyValue);
}
if (is_array($propertyValue)) {
$nameAttribute .= '[]';
} elseif ($multiple === true) {
$nameAttribute .= '[]';
}

$this->registerFieldNameForFormTokenGeneration($nameAttribute);
}
}
130 changes: 130 additions & 0 deletions Classes/ViewHelpers/Form/SelectFieldViewHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php

declare(strict_types=1);

namespace FriendsOfTYPO3\HeadlessPowermail\ViewHelpers\Form;

/**
* Class SelectFieldViewHelper
*/
class SelectFieldViewHelper extends SelectViewHelper
{

/**
* @var array
*/
protected $originalOptions = [];

/**
* Render the tag.
*
* @return string rendered tag.
* @api
*/
public function render()
{
$this->originalOptions = $this->arguments['options'];
$this->setOptions();
return json_encode(parent::render());
}

/**
* Set options with key and value from $field->getModifiedOptions()
* convert:
* array(
* array(
* 'label' => 'Red shoes',
* 'value' => 'red',
* 'selected' => 0
* )
* )
*
* to:
* array(
* 'red' => 'Red shoes'
* )
*/
protected function setOptions(): void
{
$optionArray = [];
foreach ($this->arguments['options'] as $option) {
$optionArray[$option['value']] = $option['label'];
}
$this->arguments['options'] = $optionArray;
}

/**
* Render one option tag
*
* @param string $value value attribute of the option tag (will be escaped)
* @param string $label content of the option tag (will be escaped)
* @param bool $isSelected specifies wether or not to add selected attribute
* @return string the rendered option tag
*/
protected function renderOptionTag($value, $label, $isSelected = false)
{
unset($isSelected);
return parent::renderOptionTag(
$value,
$label,
$this->isSelectedAlternative($this->getOptionFromOriginalOptionsByValue((string)$value))
);
}

/**
* @param string $value
* @return array
*/
protected function getOptionFromOriginalOptionsByValue(string $value): array
{
foreach ($this->originalOptions as $option) {
if ((string)$value === $option['value'] || (string)$value === $option['label']) {
return $option;
}
}
return [];
}

/**
* Check if option is selected
*
* @param array $option Current option
* @return bool TRUE if the value marked a s selected; FALSE otherwise
*/
protected function isSelectedAlternative(array $option): bool
{
if (is_array($this->getValueAttribute())) {
return $this->isSelectedAlternativeForArray($option);
}
return $this->isSelectedAlternativeForString($option);
}

/**
* @param array $option
* @return bool
*/
protected function isSelectedAlternativeForString(array $option): bool
{
if (($option['selected'] && !$this->getValueAttribute()) ||
($this->getValueAttribute() &&
($option['value'] === $this->getValueAttribute() || $option['label'] === $this->getValueAttribute()))
) {
return true;
}
return false;
}

/**
* @param array $option
* @return bool
*/
protected function isSelectedAlternativeForArray(array $option): bool
{
foreach ($this->getValueAttribute() as $singleValue) {
if (!empty($singleValue) && ($option['value'] === $singleValue || $option['label'] === $singleValue)) {
return true;
}
}
return false;
}
}
Loading

0 comments on commit b7186ca

Please sign in to comment.