Created
January 23, 2017 18:48
-
-
Save georgringer/67badaa7ed1f8d397d037f3fdd3ae166 to your computer and use it in GitHub Desktop.
Persist a TYPO3 powermail form including a sys_file_reference
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
plugin.tx_powermail.settings.setup { | |
finishers { | |
89 { | |
class = GeorgRinger\Example\Hooks\Frontend\SaveFinisher | |
config { | |
# location in fileadmin | |
path = formuploads/ | |
# pid to save | |
pid = 15 | |
# field mapping | |
fieldMapping { | |
# syntax: powermail field = db field | |
firstname = first_name | |
lastname = last_name | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace GeorgRinger\Example\Hooks\Frontend; | |
use In2code\Powermail\Domain\Model\Answer; | |
use In2code\Powermail\Domain\Model\Mail; | |
use In2code\Powermail\Finisher\AbstractFinisher; | |
use TYPO3\CMS\Core\Database\DatabaseConnection; | |
use TYPO3\CMS\Core\Log\Logger; | |
use TYPO3\CMS\Core\Log\LogManager; | |
use TYPO3\CMS\Core\Resource\ResourceFactory; | |
use TYPO3\CMS\Core\Utility\GeneralUtility; | |
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer; | |
/** | |
* Save form in table including a sys_file_reference | |
*/ | |
class SaveFinisher extends AbstractFinisher | |
{ | |
/** @var $logger Logger */ | |
protected $logger; | |
public function __construct(Mail $mail, array $configuration, array $settings, $formSubmitted, $actionMethodName, ContentObjectRenderer $contentObject) | |
{ | |
$this->logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__); | |
parent::__construct($mail, $configuration, $settings, $formSubmitted, $actionMethodName, $contentObject); | |
} | |
/** | |
* Finisher which does all the magic | |
*/ | |
public function talentePoolFinisher() | |
{ | |
$fields = $this->getMail()->getAnswersByFieldMarker(); | |
$insert = [ | |
'tstamp' => $GLOBALS['EXEC_TIME'], | |
'crdate' => $GLOBALS['EXEC_TIME'], | |
'pid' => (int)$this->configuration['pid'], | |
'hidden' => 1 | |
]; | |
foreach ((array)$this->configuration['fieldMapping'] as $from => $to) { | |
$insert[$to] = $this->getFieldValue($fields, $from); | |
} | |
$table = 'tx_example_domain_model_talent'; | |
$db = $this->getDatabaseConnection(); | |
$db->exec_INSERTquery($table, $insert); | |
$uploadFile = $this->getFieldValue($fields, 'upload'); | |
if (!empty($uploadFile)) { | |
$newRecordId = $db->sql_insert_id(); | |
$copiedLocation = $this->handleUpload($uploadFile); | |
if ($copiedLocation) { | |
$fileOrFolderObject = ResourceFactory::getInstance()->retrieveFileOrFolderObject($copiedLocation); | |
if (!is_null($fileOrFolderObject)) { | |
$sysFileId = $fileOrFolderObject->getUid(); | |
$insert = [ | |
'tstamp' => $GLOBALS['EXEC_TIME'], | |
'crdate' => $GLOBALS['EXEC_TIME'], | |
'pid' => (int)$this->configuration['pid'], | |
'uid_local' => $sysFileId, | |
'uid_foreign' => $newRecordId, | |
'tablenames' => $table, | |
'fieldname' => 'foto' | |
]; | |
$db->exec_INSERTquery('sys_file_reference', $insert); | |
$db->exec_UPDATEquery($table, 'uid=' . $newRecordId, ['foto' => 1]); | |
} | |
} | |
} | |
} | |
protected function handleUpload($file = '') | |
{ | |
if (empty($file)) { | |
$this->logger->debug('no file'); | |
return ''; | |
} | |
// this could be nicer for sure | |
$file = trim($file, '[]"'); | |
$path = PATH_site . '/uploads/tx_powermail/' . $file; | |
$target = '/fileadmin/' . trim($this->configuration['path'], '/') . '/' . $file; | |
$targetAbsolute = PATH_site . $target; | |
if (!is_file($path)) { | |
$this->logger->debug('no file for ' . $file); | |
return ''; | |
} | |
@copy($path, $targetAbsolute); | |
GeneralUtility::fixPermissions($targetAbsolute); | |
return $targetAbsolute; | |
} | |
/** | |
* @param array $fields | |
* @param string $name | |
* @return string | |
*/ | |
protected function getFieldValue(array $fields, $name) | |
{ | |
$field = $fields[$name]; | |
if (!is_null($field)) { | |
/** @var Answer $field */ | |
return $field->getRawValue(); | |
} | |
return ''; | |
} | |
/** | |
* @return DatabaseConnection | |
*/ | |
protected function getDatabaseConnection() | |
{ | |
return $GLOBALS['TYPO3_DB']; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thx Georg for sharing this.
It should be possible to use the UploadService class. There is some magic about the upload files for your handleUpload() method.