File: D:/HostingSpaces/SBogers10/topswtw.komma.pro/app/controllers/ContactController.php
<?php
use Komma\Kms\Products\Models\Product;
use KommaApp\Shop\Customers\CustomerNotifierService;
use KommaApp\Shop\Mailers\CustomerMailer;
use KommaApp\Shop\Orders\Order;
use KommaApp\Shop\Pages\PageService;
use KommaApp\Shop\Mailers\AdminMailer;
class ContactController extends BaseController
{
/**
* @var PageService
*/
private $pageService;
/**
* @var\ AdminMailer
*/
private $adminMailer;
/**
* @var CustomerMailer $customerMailer
*/
private $customerMailer;
/**
* @var CustomerNotifierService
*/
private $customerNotifierService;
/**
* @param PageService $pageService
* @param AdminMailer $adminMailer
* @param CustomerMailer $customerMailer
* @param CustomerNotifierService $customerNotifierService
*/
public function __construct(PageService $pageService, AdminMailer $adminMailer, CustomerMailer $customerMailer, CustomerNotifierService $customerNotifierService)
{
$this->pageService = $pageService;
$this->adminMailer = $adminMailer;
$this->customerMailer = $customerMailer;
$this->customerNotifierService = $customerNotifierService;
}
public function index()
{
$pageId = $this->routeData->routable->page_id;
$node = $this->pageService->getNodeById($pageId);
$images = $this->pageService->getImagesByPageId($pageId);
return View::make( viewPrefix() . 'pages.contact')
->with([
'entity' => $node,
'images' => $images
]);
}
public function sendFirstOpenPaymentNotifications($order)
{
return $this->customerNotifierService->notify($order);
}
/***
* This method handles the post method of the form
*
*/
public function submit()
{
// Setup shop languages from URI, for the validation
\Shop::getLanguageService()->bootFromUri(\Shop::getId());
$input = \Input::all();
//Validation
$validator = \Validator::make(
\Input::all(),
array(
'name' => 'required',
'email' => 'required|email',
'message' => 'required',
),
['required_without' => \Lang::get('validation.custom.phone_or_email')]
);
if ($validator->fails()) {
return \Redirect::back()->withErrors($validator)->withInput();
}
//Check if the birthday field if filled in, this is not visible so then it is Spam
if (\Input::has('birthday')) return \Redirect::to(\Input::get('route') . '?status=faulty');
//All is well, so we are going to send the information to the admin
$this->adminMailer->sendContactForm(\Input::all());
return \Redirect::to(\Input::get('route') . '?status=success');
}
public function returningIndex()
{
$pageId = $this->routeData->routable->page_id;
$node = $this->pageService->getNodeById($pageId);
$images = $this->pageService->getImagesByPageId($pageId);
return View::make( viewPrefix() . 'pages.return')
->with([
'entity' => $node,
'images' => $images
]);
}
public function returningSubmit()
{
// Setup shop languages from URI, for the validation
\Shop::getLanguageService()->bootFromUri(\Shop::getId());
//No validation here. frontend handles this.
//Check if the birthday field if filled in, this is not visible so then it is Spam
if (\Input::has('birthday')) return \Redirect::to(\Input::get('route') . '?status=faulty');
//All is well, so we are going to send the information to the admin
$this->adminMailer->sendRetourFormToAdminAndCustomer(\Input::all());
return \Redirect::to(\Input::get('route') . '?status=success');
}
public function askHelp()
{
$pageId = $this->routeData->routable->page_id;
$node = $this->pageService->getNodeById($pageId);
$images = $this->pageService->getImagesByPageId($pageId);
return View::make( viewPrefix() . 'pages.askHelp')
->with([
'entity' => $node,
'images' => $images
]);
}
public function askHelpSubmit()
{
// Setup shop languages from URI, for the validation
\Shop::getLanguageService()->bootFromUri(\Shop::getId());
//Validation
$validator = \Validator::make(
\Input::all(),
array(
'name' => 'required',
'email' => 'required|email',
'question' => 'required',
'image' => 'mimes:jpg,jpeg,png'
)
);
if ($validator->fails()) {
return \Redirect::back()->withErrors($validator)->withInput();
}
//Check if the birthday field if filled in, this is not visible so then it is Spam
if (\Input::has('birthday')) return \Redirect::to(\Input::get('route') . '?status=faulty');
$short_path = '/uploads/contact';
$file_path = public_path().$short_path;
$file_name = 'ask_help_'.uniqid().'.jpg';
//Upload image
$data = \Input::all();
$data['image_location'] = '';
if (\Input::file('image')) {
\Input::file('image')->move($file_path, $file_name);
$data['image_location'] = helper_fullDomain() . $short_path . '/' . $file_name;
}
//All is well, so we are going to send the information to the admin
$this->adminMailer->sendAskHelpForm($data);
return \Redirect::to(\Input::get('route') . '?status=success');
}
/**
* For ajax calls. Needs an order number, postal code and home number.
* Then checks if the there is an order that matches those details.
* If not returns false. else returns true
*
* @return string
*/
public function returningValidateOrder()
{
if(!Request::ajax()) $this::abort('nl'); //only allow ajax requests
$valid = false;
$orderNumber = Input::get('ordernumber');
$postalcode = Input::get('postalcode');
$housenumber = Input::get('housenumber'); //May also contain suffix
$order = Order::where('order_number', '=', $orderNumber)->first();
$orderNumberValid = ($order) ? true : false;
$postalCodeValidForShipping = ($order && $order->shipping_postal && $this->formatForCheck($order->shipping_postal) == $this->formatForCheck($postalcode)) ? true : false;
$postalCodeValidForInvoice = ($order && $order->invoice_postal && $this->formatForCheck($order->invoice_postal) == $this->formatForCheck($postalcode)) ? true : false;
$houseNumberValidForShipping = (
$order &&
$order->shipping_house_number &&
(
($this->formatForCheck($order->shipping_house_number) == $this->formatForCheck($housenumber) && $this->formatForCheck($order->shipping_house_number_suffix) == "") ||
$this->formatForCheck($order->shipping_house_number.$order->shipping_house_number_suffix) == $this->formatForCheck($housenumber)
)
) ? true : false;
$houseNumberValidForInvoice = (
$order &&
$order->invoice_house_number &&
(
($this->formatForCheck($order->invoice_house_number) == $this->formatForCheck($housenumber) && $this->formatForCheck($order->invoice_house_number_suffix) == "") ||
$this->formatForCheck($order->invoice_house_number.$order->invoice_house_number_suffix) == $this->formatForCheck($housenumber)
)
) ? true : false;
$shippingAddressValid = $postalCodeValidForShipping == true && $houseNumberValidForShipping == true;
$invoiceAddressValid = $postalCodeValidForInvoice == true && $houseNumberValidForInvoice == true;
$valid = ($orderNumberValid && ($shippingAddressValid || $invoiceAddressValid));
$products = [];
if($valid) $products = $order->products()->get();
return json_encode([
'valid' => $valid,
'ordernumber' => $orderNumber,
'ordernumbervalid' => $orderNumberValid,
'postalcode' => $postalcode,
'postalcodevalid' => ($postalCodeValidForShipping || $postalCodeValidForInvoice) ? true : false,
'housenumber' => $housenumber, //May also contain suffix
'housenumbervalid' => ($houseNumberValidForShipping || $houseNumberValidForInvoice) ? true: false,
'invoice_adress_valid' => $invoiceAddressValid,
'shipping_adress_valid' => $shippingAddressValid,
'products' => $products,
//Debugging helpers
// 'shipping_postal_code_valid' => $postalCodeValidForShipping,
// 'shipping_housenumber_valid' => $houseNumberValidForShipping,
// 'invoice_postal_code_valid' => $postalCodeValidForInvoice,
// 'invoice_housenumber_valid' => $houseNumberValidForInvoice,
]);
}
/**
* For ajax calls. Needs an tops article / product number.
* Then checks if the there is an product that has that article number.
* If not returns false. else returns true
*
* @return string
*/
public function returningValidateArticleNumber()
{
if(!Request::ajax()) $this::abort('nl'); //only allow ajax requests
$articleNumber = Input::get('articlenumber');
$product = Product::where('internal_article_number', '=', $articleNumber)->first();
$valid = ($product) ? true : false;
return json_encode([
'valid' => $valid,
]);
}
/**
* makes a string lowercase, trimmed and removes spaces in it so that you can compare it to another string.
*
* For example a postal code like this "6021 PW" or "6021PW" or "6021 PW " or "6021pw" would all match "6021pw"
*
* @param $data
* @return string
*/
public function formatForCheck($data)
{
return strtolower(str_replace(' ', '', trim($data)));
}
}