File: /var/www/vhosts/creativefellows.nl/firstbrick.creativefellows.nl/src/Action/Form/FormPostAction.php
<?php
namespace App\Action\Form;
use App\Action\Action;
use App\Domain\Mail\FormMailer;
use App\Domain\Form\Service\FormReader;
use App\Domain\Page\Service\PageReader;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Container\ContainerInterface;
use Slim\Interfaces\RouteParserInterface;
final class FormPostAction extends Action
{
protected $container;
protected $routeParser;
protected $mailer;
protected $form;
protected $pageReader;
protected $html;
protected $fields = [
["first-name","input"],
["last-name","input"],
["email","input"],
["phone-number","input"],
["campany-name","input"],
["country-region","input"],
["inquiry-explanation","text"],
["spacer","spacer"],
["co-branding","checkbox"],
["co-branding-company-name","input"],
["co-branding-company-website","input"],
["co-branding-phone-number","input"],
["co-branding-location-address","text"],
["ucaas-collateral","checkbox"],
["ccaas-collateral","checkbox"],
["files","upload"],
];
protected $subject = "UNIVERGE BLUE Marketing Support Request";
protected $receivers = ['bas@creativefellows.nl','PartnerMarketing@necam.com','Ronald.Schapendonk@EMEA.NEC.COM'];//,'PartnerMarketing@necam.com','Ronald.Schapendonk@EMEA.NEC.COM'];
public function __construct(
ContainerInterface $container,
RouteParserInterface $routeParser,
FormMailer $mailer,
FormReader $form,
PageReader $pageReader
) {
$this->container = $container;
$this->routeParser = $routeParser;
$this->mailer = $mailer;
$this->form = $form;
$this->pageReader = $pageReader;
$this->template_path = $this->container->get('settings')['email_template_path'];
$this->domain_path = $this->container->get('settings')['domain_path'];
$this->email_from = $this->container->get('settings')['email_from'];
}
public function action() : ResponseInterface
{
// check of a post url is set
$id = ($this->args['id'] ?? null);
$name = ($this->args['name'] ?? null);
// Post
$formData = $this->request->getParsedBody();
// Create field HTML
$this->html = $this->getFieldsHtml($formData);
// replace variables
$email = $this->replaceText("template.html",[
"/{domain_path}/" => $this->domain_path,
"/{subject}/" => $this->subject,
"/{body_html}/" => $this->html
]);
// set mail contents
$this->mailer->body( $email );
$this->mailer->subject( $this->subject );
$this->mailer->from( $this->email_from );
$this->mailer->to( $this->receivers );
$this->mailer->send();
// return to complete page
return $this->response
->withHeader('Location', $this->routeParser->urlFor('page.view',["section" => "form-succesfully-sent"]))
->withStatus(200);
}
private function getFieldsHtml($post)
{
$html = "";
foreach($this->fields as $field)
{
$postValue = $post[$field[0]];
$field_type = $field[1];
$field_name = $field[0];
// file uploads
if($field_type == "upload")
{
$uploads = "<table cellpadding='0' cellspacing='0' border='0' style='border-collapse:collapse;border-spacing:0;padding-bottom:0;padding-left:0;padding-right:0;padding-top:0;text-align:left;vertical-align:top;width:100%'>";
foreach($postValue as $file)
{
$name = end(explode("/",$file));
$uploads .= '<tr>';
$uploads .= '<td style=""><a href="'. $this->domain_path ."/download?file=". $name .'">'. urldecode($name) .'</a></td>';
$uploads .= '<td style="width:100px;text-align:right;"><a href="'. $this->domain_path .'/uploadfiles?file='. $name .'&method=DELETE">Delete file</a></td>';
//http://bluepartner.test:8888/uploadfiles?file=CF%20-%20GEOMIL%20portal%20v2%20%281%29.pdf
$uploads .= '</tr>';
}
$uploads .= "</table>";
$html .= $this->htmlRow(" "," ");
$html .= $this->htmlRow(ucfirst(str_replace("-"," ",$field_name)), $uploads);
}
elseif($field_type == "spacer")
{
$html .= $this->htmlRow(" "," ");
}
elseif($field_type == "checkbox")
{
$html .= $this->htmlRow(" "," ");
$html .= $this->htmlRow(ucfirst(str_replace("-"," ",$field_name)),"- ".implode("<br />- ",$postValue));
}
else
{
$html .= $this->htmlRow(ucfirst(str_replace("-"," ",$field_name)), $postValue);
}
}
return $html;
}
private function htmlRow($name,$value)
{
return '<table class="row" style="border-collapse:collapse;border-spacing:0;display:table;padding:0;padding-bottom:0;padding-left:0;padding-right:0;padding-top:0;position:relative;text-align:left;vertical-align:top;width:100%"><tbody><tr style="padding-bottom:0;padding-left:0;padding-right:0;padding-top:0;text-align:left;vertical-align:top"><th class="small-12 large-4 columns first" style="-moz-hyphens:auto;-webkit-hyphens:auto;Margin:0 auto;border-collapse:collapse!important;color:#2b2d33;font-family:Helvetica,Arial,sans-serif;font-size:15px;font-weight:400;hyphens:auto;line-height:1.4;margin:0 auto;padding-bottom:0;padding-left:24px;padding-right:12px;padding-top:0;text-align:left;vertical-align:top;width:216px;word-wrap:break-word"><table style="border-collapse:collapse;border-spacing:0;padding-bottom:0;padding-left:0;padding-right:0;padding-top:0;text-align:left;vertical-align:top;width:100%"><tbody><tr style="padding-bottom:0;padding-left:0;padding-right:0;padding-top:0;text-align:left;vertical-align:top"><th style="-moz-hyphens:auto;-webkit-hyphens:auto;Margin:0;border-collapse:collapse!important;color:#2b2d33;font-family:Helvetica,Arial,sans-serif;font-size:15px;font-weight:400;hyphens:auto;line-height:1.4;margin:0;padding-bottom:0;padding-left:0;padding-right:0;padding-top:0;text-align:left;vertical-align:top;word-wrap:break-word;font-weight:bold">'. $name.'</th></tr></tbody></table></th><th class="small-12 large-8 columns last" style="-moz-hyphens:auto;-webkit-hyphens:auto;Margin:0 auto;border-collapse:collapse!important;color:#2b2d33;font-family:Helvetica,Arial,sans-serif;font-size:15px;font-weight:400;hyphens:auto;line-height:1.4;margin:0 auto;padding-bottom:0;padding-left:12px;padding-right:24px;padding-top:0;text-align:left;vertical-align:top;width:456px;word-wrap:break-word"><table style="border-collapse:collapse;border-spacing:0;padding-bottom:0;padding-left:0;padding-right:0;padding-top:0;text-align:left;vertical-align:top;width:100%"><tbody><tr style="padding-bottom:0;padding-left:0;padding-right:0;padding-top:0;text-align:left;vertical-align:top"><th style="-moz-hyphens:auto;-webkit-hyphens:auto;Margin:0;border-collapse:collapse!important;color:#2b2d33;font-family:Helvetica,Arial,sans-serif;font-size:15px;font-weight:400;hyphens:auto;line-height:1.4;margin:0;padding-bottom:0;padding-left:0;padding-right:0;padding-top:0;text-align:left;vertical-align:top;word-wrap:break-word">'. $value .'</th></tr></tbody></table></th></tr></tbody></table>';
}
private function replaceText($template_email_name, $replacements)
{
$text_email = file_get_contents($this->template_path . $template_email_name);
$keys = array_keys($replacements);
$values = array_values($replacements);
return preg_replace($keys,$values,$text_email);
}
}