Maslosoft Staple API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
<?php
/**
* This software package is licensed under AGPL or Commercial license.
*
* @package maslosoft/staple
* @licence AGPL or Commercial
* @copyright Copyright (c) Piotr Masełkowski <pmaselkowski@gmail.com>
* @copyright Copyright (c) Maslosoft
* @link http://maslosoft.com/staple/
*/
namespace Maslosoft\Staple\Widgets;
use Exception;
use Maslosoft\EmbeDi\EmbeDi;
use Maslosoft\MiniView\MiniView;
/**
* ContactForm
*
* @author Piotr Maselkowski <pmaselkowski at gmail.com>
*/
class ContactForm
{
const DefaultLang = 'en';
/**
* Language
* @var string
*/
public $lang = self::DefaultLang;
public $email = '';
public $to = '';
public $fields = ['name', 'email', 'subject', 'body'];
public $options = [
'lang' => self::DefaultLang,
'email' => '',
'fields' => ['name', 'email', 'subject', 'body']
];
public $error = [];
/**
* View
* @var MiniView
*/
private $mv = null;
private $success = false;
public function __construct($options = [])
{
if (is_string($options))
{
$this->options['email'] = $options;
unset($options);
}
if (!empty($options))
{
$this->options = array_merge($this->options, $options);
}
if (empty($this->options['lang']))
{
$this->options['lang'] = self::DefaultLang;
}
// Apply configuration
EmbeDi::fly()->apply($this->options, $this);
// Setup view
$this->mv = new MiniView($this);
if (!empty($_POST['ContactForm']))
{
if(!empty($_POST['email']))
{
$this->success = true;
return;
}
$data = (object) $_POST['ContactForm'];
if (!isset($data->name))
{
$data->name = $data->email;
}
if (!isset($data->subject))
{
$data->subject = substr($data->body, 0, 32);
}
if ($this->validate($data))
{
$email = $this->mv->render(sprintf('%s/%s', $this->lang, 'contact-email'), ['data' => $data], true);
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= "From: <$this->email>" . "\r\n";
$headers .= "Reply-to: $data->name <$data->email>" . "\r\n";
$this->success = mail($this->email, $data->subject, $email, $headers);
}
}
}
public function hasfield($field)
{
return in_array($field, $this->fields);
}
public function isSuccess()
{
return $this->success;
}
protected function validate($data)
{
$this->error = [];
foreach ($this->fields as $field)
{
if (empty($data->$field))
{
array_push($this->error, $field);
}
}
if (!filter_var($data->email, FILTER_VALIDATE_EMAIL))
{
array_push($this->error, 'emailFormat');
}
if (empty($this->error))
{
return true;
}
return false;
}
public function __toString()
{
try
{
return $this->mv->render(sprintf('%s/%s', $this->lang, 'contact-form'), [], true);
}
catch (Exception $exc)
{
echo $exc->getTraceAsString();
}
}
}
API documentation generated by ApiGen