Skip to content

Instantly share code, notes, and snippets.

@georgringer
Created January 23, 2017 18:48
Show Gist options
  • Select an option

  • Save georgringer/67badaa7ed1f8d397d037f3fdd3ae166 to your computer and use it in GitHub Desktop.

Select an option

Save georgringer/67badaa7ed1f8d397d037f3fdd3ae166 to your computer and use it in GitHub Desktop.

Revisions

  1. georgringer created this gist Jan 23, 2017.
    22 changes: 22 additions & 0 deletions Configuration.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    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
    }
    }
    }
    }
    }
    119 changes: 119 additions & 0 deletions SaveFinisher.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,119 @@
    <?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'];
    }
    }