Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
evsmash/widgets / libs / FormSender.php
Size: Mime:
<?php

namespace Evsmash\Widgets;

use Evsmash\Core\Dec\Dec;
use Evsmash\Core\Files\File;
use Evsmash\Core\Files\Upload;
use Evsmash\Core\Helpers\Sanitize;
use Evsmash\Core\Helpers\Str;
use Evsmash\Core\Helpers\Redirect;
use Evsmash\Core\Input\Post;
use Evsmash\Core\Input\Route;
use Evsmash\Core\Input\Server;
use Evsmash\Core\Services\Mail;

class FormSender {

	private $input;
	
	public $rules;
	public $intro;
	public $fields;
	public $approvals;
	public $title;
	public $form;
	public $recipient;
	public $attachments = false;
	public $redirect = false;

	// construct
	public function __construct() {

		$this->input = Post::all();

	}

	// send
	public function send() {

		// validate
		Post::validate($this->rules);

		// save
		if(cfg('evsmash-widgets-forms-save')) {
			$this->save();
		}

		// email
		$this->email();

		// finish
		if(!$this->redirect) {
			Redirect::msg(t('Your message has been sent. Thank you.').'<br /><a href="'.Server::referer().'">'.t('Back to the previous page').'</a>', 'info');
			Redirect::to(Route::map('evsmash/widgets/forms/sent'));
		} else {
			Redirect::to($this->redirect);
		}

	}

	// import
	private function save() {

		$item = new Form();
		$item->form = Str::slug($this->title);
		$item->url = Server::referer();
		$item->email = Sanitize::email($this->input['email']);
		$item->data = $this->data();
		$item->save();

	}

	// email
	private function email() {

		// init
		$mail = new Mail();

		// reply
		$mail->reply = $this->input['email'];

		// title
		$mail->title = t($this->title).' - '.Route::get('domain');

		// msg
		$mail->msg = $this->msg();

		// attachments
		if($this->attachments) {
			$mail->attachments = $attachments_to_remove = $this->attachments();
		}

		// send
		$mail->send($this->recipient);

		// clear attachments
		if($this->attachments) {
			$this->attachmentsRemove($attachments_to_remove);
		}

	}

	// data
	private function data() {

		$data = [];
		foreach($this->fields as $key => $label) {
			$data[t($label)] = Sanitize::clean($this->input[$key]);
		}
		return serialize($data);

	}

	// msg
	private function msg() {

		// init
		$rows = [];

		// fields
		foreach($this->fields as $key => $label) {
			$rows[] = [
				'<span class="text-sm text-bold">'.t($label).'</span>', 
				'<span class="text-sm">'.Sanitize::clean($this->input[$key]).'</span>'
			];
		}

		// approvals
		if($this->approvals) {
			foreach($this->approvals as $text) {
				$rows[] = [
					'<span class="text-sm text-bold">'.t('Approval').'</span>', 
					'<span class="text-sm">'.$text.'</span>'
				];
			}
		}

		// msg
		$msg = '';
		if($this->intro) {
			$msg .= '<section>'.$this->intro.'</section>';
		}
		$msg .= '<section>';
		$msg .= Dec::table($rows)->class('table-std');
		$msg .= '</section>';

		// output
		return $msg;

	}

	// attachments
	private function attachments() {

		$output = [];
		foreach($this->attachments as $row) {

			// skip if error
			if($_FILES[$row[0]]['error'] == 0) {

				// tmp upload
				$file = Upload::tmp($row[0], $row[1], $row[2]);

				// assign
				$output[] = $file;

			}

		}
		return $output;

	}

	// attachments remove
	private function attachmentsRemove($files) {

		foreach($files as $file) {
			File::delete($file->path);
		}

	}

}