TYPO3: sendy-Integration mit formhandler

Da ich jedoch für ein Projekt kurz zuvor eine kleine Extension geschrieben habe, welche sendy (siehe Beitrag „Newsletterversand mit sendy“) in Typo3 integriert, möchte ich diese Lösung dennoch hier vorstellen.

Hinweis: Die Entwicklung von formhandler wurde die Tage (Stand Oktober 2016) eingestellt.

Ziel ist die Erstellung eines Formulares mit formhandler und Speichern der eingegebenen Daten in der sendy-Datenbank, um später darüber sowohl die Abonnenten als auch den Newsletterversand zu verwalten.

  1. Erstellung eines Formulares
  2. Erstellung eines Plugins „formhandler_sendy“
  3. Einbindung ins Formular

Erstellung eines Formulares

Zunächst erstellt man mit formhandler ein simples Formular. Bei einer Newsletteranmeldung reicht das Feld Email für den Anfang vollkommen aus (auch aus datenschutzrechtlichen Gründen). Ich habe in meinem Projekt den Beispielcode von Formhandler mit sr_freecap genutzt, um Spam zu vermeiden.

Erstellung eines Plugins für sendy

Als nächster Punkt muss ein neues Plugin erstellt werden. Als Name habe ich formhandler_sendy gewählt.

Struktur:

formhandler_sendy

  • Classes
    • Validator
      • Sendy.php
      • sendy_signup.php
  • Configuration
    • TypoScript
      • setup.txt
  • ext_emconf.php

ext_emconf.php:

$EM_CONF[$_EXTKEY] = array (
  'title' => 'Formhandler Sendy Integration',
  'description' => '',
  'category' => '',
  'version' => '0.0.1',
  'state' => 'stable',
  'uploadfolder' => false,
  'createDirs' => '',
  'clearcacheonload' => true,
  'author' => 'Florian Freiburg',
  'author_email' => ' ',
  'author_company' => 'Schreiber & Freunde GmbH & Co. KG',
  'constraints' => 
  array (
    'depends' => 
    array (
      'typo3' => '6.2.0-8.0.999',
      'php' => '5.3.2-7.0.999',
      'realurl' => '2.0.14-3.0.0',
      'formhandler' => '',
    ),
    'conflicts' =>
        array(),
    'suggests' => 
    array (
    ),
  ),
);

Classes/Validator/Sendy.php:

<?php

namespace Vendor\Formhandler_Sendy\Validator;
/*                                                                        *
 * This script is part of the TYPO3 project - inspiring people to share!  *
 *                                                                        *
 * TYPO3 is free software; you can redistribute it and/or modify it under *
 * the terms of the GNU General Public License version 2 as published by  *
 * the Free Software Foundation.                                          *
 *                                                                        *
 * This script is distributed in the hope that it will be useful, but     *
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General      *
 * Public License for more details.                                       *
 *
 *                                                                        */

/**
 * This finisher sends submitted form data to sendy (sendy.co).
 *
 * @author	Florian Freiburg
 * @package	Tx_Formhandler
 * @subpackage	Finisher
 */
class Sendy extends \Typoheads\Formhandler\Validator\AbstractValidator {

	/**
	 * Validates the submitted values using given settings
	 *
	 * @param array &$errors Reference to the errors array to store the errors occurred
	 * @return boolean
	 */
	public function validate(&$errors) {

		//First validator returned errors, do nothing
		if(!empty($errors)) {
			return TRUE;
		}

		$params = array(
			'sendyUrl' => $this->settings['sendyUrl'],
			'listId' => $this->settings['listId']
		);

		require_once('sendy_signup.php');
		$api = new sendy_signup();
		$result = $api->add_subscription($params);

		return $result;
	}
}
?>

Classes/Validator/sendy_signup.php

Sollten im Formular mehr Felder als nur die Emailadresse vorhanden sein, sollten diese natürlich mit aufgenommen werden. Weiterhin müssen diese Felder in der Empfängerliste im Sendy als Custom-Fields angelegt werden. Sendy kennt von sich aus nur die Felder Email und Name.

<?php

namespace Vendor\Formhandler_Sendy\Validator;

class sendy_signup {

    public function __construct() {

    }

   public function add_subscription($params) {

        //------------------- Edit here --------------------//
       $sendy_url = $params['sendyUrl'];
       $list = $params['listId'];

       //------------------ /Edit here --------------------//

       //--------------------------------------------------//
       //POST variables
       $email = $_POST['sr_freecap']['email'];

       //subscribe
       $postdata = http_build_query(
           array(
               'email' => $email,
               'list' => $list,
               'boolean' => 'true'
           )
       );
       $opts = array('http' => array('method'  => 'POST', 'header'  => 'Content-type: application/x-www-form-urlencoded', 'content' => $postdata));
       $context  = stream_context_create($opts);
       $result = file_get_contents($sendy_url.'/subscribe', false, $context);
       //--------------------------------------------------//

       return $result;

   }
}

?>

Configuration/TypoScript/setup.txt

plugin.tx_formhandlersendy {

}

 

Einbindung ins Formular

Die Einbindung ins Formular ist denkbar einfach. Hier wird einfach ein neuer Validator im Setup des Formulares hinzugefügt, welcher die sendy-Klasse anspricht. Die Werte in der Config des Validators für sendy sind natürlich anzupassen.

plugin.Tx_Formhandler.settings.myExampleForm {

	name = Newsletter registration with sendy.co

	... your definitions ...
	
	# This block defines the error checks performed when the user hits submit.
	validators {
		1.class = Validator_Default
		1.config.fieldConf {
		
			email.errorCheck.1 = required
			email.errorCheck.2 = email
			email.errorCheck.4 = emailExists
			email.errorCheck.5 = betweenLength
            		email.errorCheck.5 {
		                minValue = 8
                		maxValue = 70
			}
			email.errorCheck.6 = containsAll
			email.errorCheck.6 {
			    words = @,.
			}
			freecapfield.errorCheck.1 = required
			freecapfield.errorCheck.2 = srFreecap
		}

		# This special Validator will take care of adding the new subscriber. In case of an error, the according message will be displayed.
	        2.class = Vendor\Formhandler_Sendy\Validator\Sendy
        	2.config {
	            sendyUrl = http://path-to-your-sendy.com
        	    listId = sendy-subscriber-list-id
	        }
	}

	... your finishers and loggers ...

    }
}

 

Da die Abmeldung über den versendeten Newsletter läuft (Abmeldelink in Email), wird an dieser Stelle auf ein weiteres Formular zu Abmeldung über Typo3 verzichtet. Prinzipiell ist dies ebenfalls über die Sendy-API möglich.


Beitrag veröffentlicht

in

von

Schlagwörter: