first real commit in standalone repo
This commit is contained in:
160
src/Controller/Component/ContactUsComponent.php
Normal file
160
src/Controller/Component/ContactUsComponent.php
Normal file
@@ -0,0 +1,160 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CakeContactUs\Controller\Component;
|
||||
|
||||
use Cake\Controller\Component;
|
||||
use Cake\Controller\ComponentRegistry;
|
||||
use Cake\Core\Configure;
|
||||
use Cake\Datasource\EntityInterface;
|
||||
use Cake\Http\Response;
|
||||
use Cake\I18n\DateTime;
|
||||
use Cake\Log\Log;
|
||||
use Cake\ORM\Table;
|
||||
use Cake\ORM\TableRegistry;
|
||||
use CakeContactUs\Model\Entity\ContactUsFormSubmission;
|
||||
use CakeContactUs\Model\Table\ContactUsFormSubmissionsTable;
|
||||
use Exception;
|
||||
use CakeContactUs\CakeContactUsPlugin;
|
||||
|
||||
/**
|
||||
* ContactUs component
|
||||
*/
|
||||
class ContactUsComponent extends Component
|
||||
{
|
||||
/**
|
||||
* @var ContactUsFormSubmissionsTable|Table
|
||||
*/
|
||||
protected ContactUsFormSubmissionsTable|Table $ContactUsFormSubmissions;
|
||||
|
||||
/**
|
||||
* Default configuration.
|
||||
*
|
||||
* @var array<string, mixed>
|
||||
*/
|
||||
protected array $_defaultConfig = [];
|
||||
|
||||
/**
|
||||
* @var array|string[]
|
||||
*/
|
||||
protected array $components = ['Flash'];
|
||||
|
||||
/**
|
||||
* @param array $config
|
||||
* @return void
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public function initialize(array $config): void
|
||||
{
|
||||
parent::initialize($config); // TODO: Change the autogenerated stub
|
||||
|
||||
$this->ContactUsFormSubmissions = TableRegistry::getTableLocator()->get('CakeContactUs.ContactUsFormSubmissions');
|
||||
|
||||
$requireCaptcha = Configure::readOrFail('ContactUs.fields.captcha');
|
||||
$this->setConfig('redirectUrl', Configure::read('ContactUs.redirectUrl', '/'));
|
||||
$this->setConfig('addIdToRedirect', Configure::read('ContactUs.addIdToRedirect', false));
|
||||
$this->setConfig('requireEmail', Configure::readOrFail('ContactUs.fields.email'));
|
||||
$this->setConfig('requireCaptcha', $requireCaptcha);
|
||||
if ($requireCaptcha) {
|
||||
$this->_registry->load('Captcha.Captcha');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return EntityInterface|ContactUsFormSubmission
|
||||
*/
|
||||
public function newContactUsForm()
|
||||
{
|
||||
if ($this->getConfig('requireCaptcha')) {
|
||||
$this->ContactUsFormSubmissions->addBehavior('Captcha.Captcha');
|
||||
}
|
||||
return $this->ContactUsFormSubmissions->newEmptyEntity();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return EntityInterface|ContactUsFormSubmission
|
||||
*/
|
||||
public function processContactUsForm(EntityInterface $contactUsFormSubmission, array $postData = null)
|
||||
{
|
||||
if (!isset($postData)) {
|
||||
$postData = $this->getController()->getRequest()->getData();
|
||||
}
|
||||
$postData['client_ip'] = array_key_exists('client_ip', $postData) && $postData['client_ip'] ? $postData['client_ip'] : $this->getController()->getRequest()->clientIp();
|
||||
$postData['submitted_at'] = DateTime::now();
|
||||
|
||||
$event = $this->getController()->dispatchEvent(CakeContactUsPlugin::EVENT_BEFORE_CONTACT_US_FORM_SAVED, [
|
||||
'contactUsFormSubmission' => $contactUsFormSubmission,
|
||||
], $this->getController());
|
||||
$result = $event->getResult();
|
||||
|
||||
Log::debug(print_r('$result', true));
|
||||
Log::debug(print_r($result, true));
|
||||
if ($result instanceof EntityInterface) {
|
||||
$postData = $result->toArray();
|
||||
$contactUsFormSubmission = $this->ContactUsFormSubmissions->patchEntity($contactUsFormSubmission, $postData);
|
||||
if ($contactUsFormSubmission->getErrors()) {
|
||||
Log::debug(print_r('$contactUsFormSubmission->getErrors()', true));
|
||||
Log::debug(print_r($contactUsFormSubmission->getErrors(), true));
|
||||
}
|
||||
$contactUsFormSubmissionSaved = $this->ContactUsFormSubmissions->save($contactUsFormSubmission);
|
||||
if ($contactUsFormSubmissionSaved) {
|
||||
return $this->_afterFormSaved($contactUsFormSubmissionSaved);
|
||||
}
|
||||
|
||||
// @TODO contact us form submission failed - handle here
|
||||
}
|
||||
|
||||
if ($event->isStopped()) {
|
||||
return $this->getController()->redirect($event->getResult());
|
||||
}
|
||||
|
||||
if (!$this->getController()->getRequest()->is('post')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$contactUsFormSubmission = $this->ContactUsFormSubmissions->patchEntity($contactUsFormSubmission, $postData);
|
||||
if ($contactUsFormSubmission->getErrors()) {
|
||||
Log::debug(print_r('$contactUsFormSubmission->getErrors()', true));
|
||||
Log::debug(print_r($contactUsFormSubmission->getErrors(), true));
|
||||
}
|
||||
$contactUsFormSubmissionSaved = $this->ContactUsFormSubmissions->save($contactUsFormSubmission);
|
||||
if ($contactUsFormSubmissionSaved) {
|
||||
return $this->_afterFormSaved($contactUsFormSubmissionSaved);
|
||||
}
|
||||
$message = __d('cake_contact_us', 'Something doesn\'t look quite right. Please try again.');
|
||||
|
||||
$this->Flash->error($message);
|
||||
|
||||
// @TODO contact us form submission failed - handle here
|
||||
$this->getController()->set(['contactUsFormSubmission' => $contactUsFormSubmission]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare flash messages after registration, and dispatch afterRegister event
|
||||
*
|
||||
* @param \Cake\Datasource\EntityInterface|ContactUsFormSubmission $contactUsFormSubmissionSaved Contact us form submission entity
|
||||
* @return \Cake\Http\Response
|
||||
*/
|
||||
protected function _afterFormSaved(EntityInterface $contactUsFormSubmissionSaved)
|
||||
{
|
||||
$message = __d('cake_contact_us', 'Message received, thank you. We will be in touch soon.');
|
||||
|
||||
$event = $this->getController()->dispatchEvent(CakeContactUsPlugin::EVENT_AFTER_CONTACT_US_FORM_SAVED, [
|
||||
'contactUsFormSubmission' => $contactUsFormSubmissionSaved,
|
||||
], $this->getController());
|
||||
$result = $event->getResult();
|
||||
if ($result instanceof Response) {
|
||||
return $result;
|
||||
}
|
||||
$this->Flash->success($message);
|
||||
|
||||
$redirectUrl = $this->getConfig('redirectUrl');
|
||||
Log::debug(print_r('$contactUsFormSubmissionSaved after save', true));
|
||||
Log::debug(print_r($contactUsFormSubmissionSaved, true));
|
||||
if ($this->getConfig('addIdToRedirect')) {
|
||||
$redirectUrl[] = $contactUsFormSubmissionSaved->get($this->ContactUsFormSubmissions->getPrimaryKey());
|
||||
}
|
||||
return $this->getController()->redirect($redirectUrl);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user