File: D:/HostingSpaces/SBogers10/rentman.komma.pro/app/Komma/Contact/ContactController.php
<?php
/**
* Short description for the file.
*
* @author Komma <support@komma.pro>
* @copyright (c) 2012-2015, Komma Mediadesign
*/
namespace Komma\Contact;
use Ixudra\Curl\Facades\Curl;
use Komma\Blocks\BlockService;
use Komma\LanguageService;
use Komma\Mailers\MailService;
use Komma\Pages\PageService;
use Komma\Tables\TableService;
class ContactController extends \BaseController
{
protected $contactService;
protected $mailService;
protected $pageService;
protected $blockService;
public $languageService;
protected $tableService;
/**
* ContactController constructor.
* @param ContactService $contactService
* @param MailService $mailService
* @param PageService $pageService
*/
public function __construct(ContactService $contactService, BlockService $blockService, MailService $mailService, PageService $pageService, LanguageService $languageService, TableService $tableService)
{
parent::__construct();
$this->pageService = $pageService;
$this->data = (object)[];
$this->mailService = $mailService;
$this->contactService = $contactService;
$this->blockService = $blockService;
$this->languageService = $languageService;
$this->tableService = $tableService;
}
/**
* This method returns the contact form view
*
* @param bool $sent
* @return mixed
*/
public function contactForm($sent = false)
{
//Load the info from the contact page
$this->languageService->checkRouteWithSetLanguage();
$page = $this->pageService->getPageByCodeName('contact');
$this->data->links = $this->pageService->getAllRoutes();
$page->blocks = $this->blockService->getBlocksWhere('type', $page->code_name);
$page->blocks = $page->blocks->keyBy('code_name');
//Set the data
$this->data->content = $page;
$this->data->id = $page->id;
$otherLanguages = $this->pageService->getOtherLanguageRoutes($this->data->id);
//Return the view
return \View::make('layouts.pages.contact')
->with('data', $this->data)
->with('blocks', $page->blocks)
->with('sent', $sent)
->with('otherLanguages', $otherLanguages->allTranslations);
}
public function contactSuccess()
{
return $this->contactForm(true);
}
/**
* This method processes the contact form
*
* @return null
*/
public function contactFormProcess()
{
//Validate the input
$validation = $this->contactService->validateContactForm(\Input::all());
//If validation is not null and the request is via ajax, return the validation messages
if($validation !== null && \Request::ajax()) return $validation;
//\Log::info($validation);
//If The validation is not null, redirect back to the form with the errors.
if($validation !== null) return \Redirect::back()->withErrors($validation)->withInput();
$input = \Input::all();
$input['lang'] = $this->languageService->getCurrentLanguageIso();
$this->mailService->saveMailToDatabase($input);
$this->mailService->sendContactForm($input);
//If the request ia via ajax, return the string true
//For the normal request show the contactForm page. with the send variable set to true
return \Redirect::to(route('contact.success'));
}
/**
* This method returns the contact form view
*
* @param bool $sent
* @return mixed
*/
public function freeTrialForm($apiCall = null)
{
//By default sent is false
$preSet = '';
//If the request has a status and the status is success set sent to true
if(\Request::has('email') && \Request::get('email') != 'success') $preSet = \Request::get('email');
$this->languageService->checkRouteWithSetLanguage();
//Load the info from the contact page
$page = $this->pageService->getPageByCodeName('trial');
$this->data->links = $this->pageService->getAllRoutes();
$page->blocks = $this->blockService->getBlocksWhere('type', $page->code_name);
$page->blocks = $page->blocks->keyBy('code_name');
//Sety the data
$this->data->content = $page;
$this->data->id = $page->id;
$otherLanguages = $this->pageService->getOtherLanguageRoutes($this->data->id);
// $packages = $this->tableService->getTablesWhere('type', 'compare-packages');
// $selectPackages = [];
// foreach ($packages as $package){
// if($package->code_name == 'enterprice') continue;
//
// $selectPackages[$package->code_name] = $package->translation->name;
// }
//Return the view
return \View::make('layouts.pages.trial')
->with('data', $this->data)
->with('blocks', $page->blocks)
->with('api', $apiCall)
->with('preSet', $preSet)
// ->with('packages', $selectPackages)
->with('otherLanguages', $otherLanguages->allTranslations);
}
public function freeTrialSuccess($data = null)
{
if($data == null) $data = \Session::get('succesData');
if($data == null) return \Redirect::to(route('trial.page'));
return $this->freeTrialForm($data);
}
/**
* This method processes the contact form
*
* @return null
*/
public function freeTrialProcess()
{
//Validate the input
$validation = $this->contactService->validateTrialForm(\Input::all());
if($validation !== null) return \Redirect::back()->withErrors($validation)->withInput();
$inputs = \Input::all();
$ip = \Request::ip();
if (array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER)){
$ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
$inputs['HTTP_X_FORWARDED_FOR'] = $_SERVER["HTTP_X_FORWARDED_FOR"];
}
$sendData = [
'apiKey' => 'a999240b13f84f25474be6c6b17c3ccd',
'firstname' => $inputs['first_name'],
'middlename' => '',
'lastname' => $inputs['last_name'],
'company' => $inputs['company'],
'account' => $inputs['username'],
'email' => $inputs['email'],
'phone' => $inputs['phone'],
'ip' => $ip,
'language' => \App::getLocale(),
'voucher' => $inputs['voucher'],
'basictype' => $inputs['basictype'],
'passwordmethod' => 'generate',
//'country' => 'nl',
];
//custom encode because of bugs in Curl encoding
$sendData = json_encode($sendData);
$inputs['lang'] = \App::getLocale();
$inputs['json'] = $sendData;
$this->mailService->saveMailToDatabase($inputs, 'trial-form');
//If validation is not null and the request is via ajax, return the validation messages
$curlURL = 'https://admin.rentman.nl/admin/rm4/create.php';
// $response = Curl::to($curlURL)
// ->withData($sendData)
// ->withContentType('application/json')
// ->asJsonResponse()
// ->post();
if($sendData != '0')
{
$curlData = Curl::to($curlURL)
->withData($sendData)
->withContentType('application/json')
->withTimeout(300)
->returnResponseObject()
->post();
$response = json_decode($curlData->content);
}
if( ! isset($response))
{
$data = (object)[
'success' => false,
'error' => 0
];
$mailer = \App::make('Komma\Mailers\Mailer');
$mailer->sendTo(\Config::get('business.admin_emails'), 'Demo Form Error No Response', 'emails/errorApi',
[
'ip' => $ip,
'firstname' => $inputs['first_name'],
'lastname' => $inputs['last_name'],
'company' => $inputs['company'],
'account' => $inputs['username'],
'email' => $inputs['email'],
'phone' => $inputs['phone'],
'voucher' => $inputs['voucher'],
'jsonSend' => $sendData,
'curlResponse' => json_encode($curlData),
],
['support@rentman.nl', 'simon@rentman.nl']);
return \Redirect::to(route('trial.error'))->with('succesData', $data);
}
switch ($response->code)
{
case 0:
$data = (object)[
'success' => true,
'account' => (object)[
'fullname' => $response->account->fullname,
'email' => $response->account->email,
'accountname' => $response->account->username,
'password' => $response->account->password,
'company' => $response->account->company,
'account' => $response->account->url,
'expire' => $response->account->expire,
]
];
$this->clearFormSession();
return \Redirect::to(route('trial.success').'?account='.$response->account->id)->with('succesData', $data);
case 21:
return \Redirect::back()->with('options', $response->extra)->withInput(\Input::all());
default:
\Log::warning(json_encode($curlData));
$code = $response->code;
if( ! in_array($code, [22, 23]))
{
$mailer = \App::make('Komma\Mailers\Mailer');
$mailer->sendTo(\Config::get('business.admin_emails'), 'Demo Form Unknown Error', 'emails/errorApi',
[
'jsonSend' => $sendData,
'curlResponse' => json_encode($curlData),
]);
$code = 0;
}
$data = (object)[
'success' => false,
'error' => $response->code
];
if($code == 22) return \Redirect::to(route('trial.ipExist'))->with('succesData', $data);
else return \Redirect::to(route('trial.error'))->with('succesData', $data);
}
}
function clearFormSession(){
\Session::forget('trialform_email');
\Session::forget('trialform_company');
\Session::forget('trialform_username');
\Session::forget('trialform_first_name');
\Session::forget('trialform_last_name');
\Session::forget('trialform_phone');
}
function storeToSession(){
//dd(\Request::all());
\Session::put('trialform_'.\Request::get('name'), \Request::get('val'));
}
}