You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

126 lines
4.1 KiB

<?php
namespace Zoomyboy\Stepper;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;
use Twig\TwigFilter as Filter;
use Twig\TwigFunction as Func;
class Stepper {
private $twig;
public $url;
public function __construct() {
$loader = new FilesystemLoader(__DIR__.'/../views');
$this->twig = new Environment($loader);
$this->twig->addFilter(new Filter('svg', [$this, 'svgTag']));
$this->twig->addFilter(new Filter('prop', [$this, 'alpineProp']));
$this->twig->addFunction(new Func('cprop', function ($aprop, $modifier) {
return $this->alpineProp($aprop, $modifier);
}));
$this->twig->addFunction(new Func('sliderelement', function ($aprop, $modifier) {
return '$refs.slider.scrollLeft = $refs.slider.scrollWidth / slides.length * active';
}));
}
public function initFrontend() {
add_action('wp_enqueue_scripts', [ $this, 'enqueue' ]);
add_action('wp_ajax_nopriv_stepper_submit', [ $this, 'onSubmit' ]);
add_action('wp_ajax_stepper_submit', [ $this, 'onSubmit' ]);
if (!is_admin()) {
add_shortcode('stepper', [ $this, 'handle' ]);
}
}
public function enqueue() {
wp_enqueue_script('stepper-js', $this->url.'assets/public/app.js');
wp_enqueue_style('stepper-css', $this->url.'assets/public/app.css');
}
public function handle() {
if (array_key_exists('kind', $_GET)) {
$data = [
'kaufpreis' => $_GET['kaufpreis'],
'eigenk' => $_GET['eigenkapital'],
'kind' => $_GET['kind'],
'step' => 1
];
} else {
$data = [
'kaufpreis' => 300000,
'eigenk' => 0,
'kind' => null,
'step' => 0
];
}
return $this->twig->render('stepper.twig.htm', [
'sprite' => $this->url.'assets/public/sprite.svg',
'data' => $data,
]);
}
public function svgTag($e, $class = '') {
return '<svg class="'.$class.'"><use xlink:href="'.$sprite.'#'.$e.'"></use></svg>';
}
public function alpineProp($prop, $modifier) {
$v = '$event.target.value';
$bv = '($event.target || $event.originalTarget).value';
return ':value="units.'.$modifier.'.to('.$prop.')"
@focus="$event.target.value = '.$prop.'"
@blur="console.log($event); '.$bv.' = units.'.$modifier.'.to('.$prop.');"
@change="console.log(\'change\'); '.$prop.' = units.'.$modifier.'.from('.$v.');"
onblur="console.log(\'RRRR\');"';
}
public function validate($payload) {
$errors = [];
if (!$payload['datenschutz']) {
$errors['datenschutz'] = 'Bitte akzeptieren Sie die Datenschutzerklärung.';
}
if (!$payload['firstname']) {
$errors['firstname'] = 'Bitte füllen Sie dieses Feld aus.';
}
if (!$payload['lastname']) {
$errors['lastname'] = 'Bitte füllen Sie dieses Feld aus.';
}
if (!$payload['phone']) {
$errors['phone'] = 'Bitte füllen Sie dieses Feld aus.';
}
if (!$payload['email']) {
$errors['email'] = 'Bitte füllen Sie dieses Feld aus.';
}
if ($payload['email'] && !filter_var($payload['email'], FILTER_VALIDATE_EMAIL)) {
$errors['email'] = 'Dies ist keine richtige E-Mail-Adresse';
}
return $errors;
}
public function onSubmit() {
$payload = json_decode(file_get_contents('php://input'), true);
$errors = $this->validate($payload);
if (count($errors)) {
echo json_encode(['errors' => $errors]);
die();
}
wp_mail('info@jonas-ruettgers.de', 'Neue Finanzierungs-Anfrage', $this->twig->render('mail.twig.htm', $payload, true), [
]);
wp_mail($payload['email'], 'Vielen Dank für Ihre Finanzierungs-Anfrage', $this->twig->render('client.twig.htm', $payload, true), [
]);
die();
}
}