/**
* Method to process the form
*
* @return self
*/
public function process()
{
$fields = $this->getFields();
$submission = new Table\FormSubmissions(['form_id' => $this->id, 'timestamp' => date('Y-m-d H:i:s'), 'ip_address' => $_SERVER['REMOTE_ADDR']]);
$submission->save();
unset($fields['csrf']);
unset($fields['captcha']);
unset($fields['id']);
unset($fields['submit']);
$files = [];
if ($_FILES) {
foreach ($_FILES as $key => $value) {
if (isset($value['tmp_name']) && !empty($value['tmp_name']) && class_exists('Phire\\Fields\\Model\\Field')) {
$upload = new Upload($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/files');
$filename = $upload->checkFilename($value['name'], $_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/files');
$fields[$key] = $filename;
$files[] = $filename;
$upload->upload($value);
unset($_FILES[$key]);
}
}
}
$fv = new \Phire\Fields\Model\FieldValue();
$values = $fv->save($fields, $submission->id, 'Phire\\Forms\\Model\\FormSubmission');
$form = Table\Forms::findById($this->id);
// If the form action is set
if (!empty($form->action)) {
$scheme = $form->force_ssl ? 'https://' : 'http://';
$action = substr($form->action, 0, 4) == 'http' ? $form->action : $scheme . $_SERVER['HTTP_HOST'] . BASE_PATH . $form->action;
if ($form->method == 'post') {
$options = [CURLOPT_POST => true, CURLOPT_POSTFIELDS => $values, CURLOPT_HEADER => false, CURLOPT_RETURNTRANSFER => true];
} else {
$action .= '?' . http_build_query($values);
$options = [CURLOPT_HEADER => false, CURLOPT_RETURNTRANSFER => true];
}
$curl = new \Pop\Http\Client\Curl($action, $options);
$curl->send();
unset($curl);
}
// Send the submission if the form "to" field is set
if (!empty($form->to)) {
$domain = str_replace('www.', '', $_SERVER['HTTP_HOST']);
$subject = $form->name . ' : ' . $domain;
// Set the recipient
$rcpt = ['email' => $form->to];
$message = '';
foreach ($values as $key => $value) {
$message .= ucwords(str_replace('_', ' ', $key)) . ': ' . (is_array($value) ? implode(', ', $value) : $value) . PHP_EOL;
}
// Send form submission
$mail = new Mail($subject, $rcpt);
if (!empty($form->from)) {
if (!empty($form->reply_to)) {
$mail->from($form->from, null, false)->replyTo($form->reply_to, null, false);
} else {
$mail->from($form->from);
}
} else {
if (!empty($form->reply_to)) {
$mail->replyTo($form->from);
} else {
$mail->from('noreply@' . $domain);
}
}
$mail->setText($message);
if (count($files) > 0) {
foreach ($files as $file) {
if (file_exists($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/files/' . $file)) {
$mail->attachFile($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/files/' . $file);
}
}
}
$mail->send();
}
$this->clear();
if (!empty($form->redirect)) {
if (substr($form->redirect, 0, 4) == 'http' || substr($form->redirect, 0, 1) == '/') {
$this->redirect = true;
$redirect = substr($form->redirect, 0, 4) == 'http' ? $form->redirect : BASE_PATH . $form->redirect;
header('Location: ' . $redirect);
exit;
} else {
$this->message = $form->redirect;
}
}
return $this;
}