Skip to content

Instantly share code, notes, and snippets.

@PhongGCS
Created June 12, 2020 06:40
Show Gist options
  • Save PhongGCS/fe1989cb1e3f0d3ee58ed36f1beca24a to your computer and use it in GitHub Desktop.
Save PhongGCS/fe1989cb1e3f0d3ee58ed36f1beca24a to your computer and use it in GitHub Desktop.
handleUploadFile by Symfomy
<?php
namespace ABC\Controllers;
use Stem\Controllers\PageController;
use Stem\Core\Context;
use Stem\Core\Response;
use ABC\Traits\Content\HasContent;
use ABC\Traits\Content\HasContentInterface;
use ABC\Traits\Content\HasHero;
use ABC\Traits\Content\HasOptions;
use ABC\Traits\Content\HasPageScripts;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use ABC\Traits\Content\HasThemeSettings;
use ABC\Models\Core\MailTemplate;
use ByJG\Mail\Envelope;
/**
* Class FormController
*
* Controller for all contact form actions.
*
* @package ABC\Controllers
*/
// ini_set('display_errors','Off');
// ini_set('error_reporting', E_ALL );
class FormController extends PageController {
use HasThemeSettings;
public $whiteList = [];
public $emailData = [];
public $cv = null;
public $name = null;
public $email = null;
public $message = null;
public $token = null;
public function __construct(Context $context, $template=null) {
parent::__construct($context, $template);
}
public function postCareerForm(Request $request) {
status_header(200);
$this->data = json_decode($request->getContent(), true);
// vomit($this->data);
$this->whiteList = [
"name", "message", "token", "email"
];
$file = $request->files->get("file");
$this->getPostData();
$this->emailData['file'] = $this->handleUploadFile($file);
// vomit($this->emailData);
$this->sendEmail($this->emailData, false);
wp_send_json(["status" => "1", "message" => "Thank for your submission"]);
}
public function sendEmail($mailData = [], $isTour = true) {
// MailTemplate::sendTemplate($this->context, 'contact-confirm', $this->email, []);
MailTemplate::sendTemplate($this->context, 'contact-notice', null, $mailData);
}
public function getPostData() {
if(!empty ($this->whiteList) && is_array($this->whiteList) && count($this->whiteList) ){
foreach($this->whiteList as $item){
if(!empty($this->data[$item])) {
if(is_array($this->data[$item])) {
$this->$item = implode("," , $this->data[$item]);
}else {
$this->$item = filter_var($this->data[$item], FILTER_SANITIZE_STRING);
}
$this->emailData[$item] = $this->$item;
}
}
$this->email = filter_var($this->data['email'], FILTER_VALIDATE_EMAIL);
}
}
public function handleUploadFile($file) {
$whiteType = [
// "image/png",
// "image/jpg",
// "image/jpeg",
// "audio/mp3",
"application/pdf",
];
if ($file) {
$nameFile = filter_var($file->getClientOriginalName(), FILTER_SANITIZE_STRING);
$typeMimeFile = filter_var($file->getClientMimeType(), FILTER_SANITIZE_STRING);
if ($file->getSize() > 10 * 1024 * 1024) {
wp_send_json(["status" => "023", "message" => "Exceeding file size limit. No more than 10 MB."]);
}
if ( !in_array($typeMimeFile, $whiteType) ){
wp_send_json(["status" => "021", "message" => "Sorry, File is invalid, allowed extensions is PDF"]);
}
$typeFile = filter_var($file->getClientOriginalExtension(), FILTER_SANITIZE_STRING);
$generateFile = $this->phone .'-'.$nameFile."-".date("dmYhms").".".$typeFile;
try {
$file->move(
"app/storage/",
$generateFile
);
return get_home_url()."/app/storage/".$generateFile;
} catch (FileException $e) {
wp_send_json(["status" => "022", "message" => "Sorry, Uploaded file could not working."]);
}
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment