2025-01-09 23:47:18 -08:00
|
|
|
<?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) {
|
2025-01-10 01:34:02 -08:00
|
|
|
// $this->_registry->load('Captcha.Captcha');
|
|
|
|
|
$this->getController()->viewBuilder()->addHelpers(['Captcha.Captcha']);
|
2025-01-09 23:47:18 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return EntityInterface|ContactUsFormSubmission
|
|
|
|
|
*/
|
|
|
|
|
public function newContactUsForm()
|
|
|
|
|
{
|
|
|
|
|
if ($this->getConfig('requireCaptcha')) {
|
|
|
|
|
$this->ContactUsFormSubmissions->addBehavior('Captcha.Captcha');
|
|
|
|
|
}
|
2025-11-10 22:26:52 -08:00
|
|
|
|
2025-01-09 23:47:18 -08:00
|
|
|
return $this->ContactUsFormSubmissions->newEmptyEntity();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return EntityInterface|ContactUsFormSubmission
|
|
|
|
|
*/
|
|
|
|
|
public function processContactUsForm(EntityInterface $contactUsFormSubmission, array $postData = null)
|
|
|
|
|
{
|
|
|
|
|
if (!isset($postData)) {
|
|
|
|
|
$postData = $this->getController()->getRequest()->getData();
|
|
|
|
|
}
|
2025-11-10 22:26:52 -08:00
|
|
|
$postData['client_ip'] = array_key_exists('client_ip', $postData) && $postData['client_ip'] ?
|
|
|
|
|
$postData['client_ip'] :
|
|
|
|
|
($this->getConfig('clientIpHeader') ?
|
|
|
|
|
$this->getController()->getRequest()->getHeaderLine($this->getConfig('clientIpHeader')) :
|
|
|
|
|
$this->getController()->getRequest()->clientIp()
|
|
|
|
|
);
|
2025-01-09 23:47:18 -08:00
|
|
|
$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));
|
|
|
|
|
}
|
2025-11-10 22:26:52 -08:00
|
|
|
|
|
|
|
|
|
2025-01-09 23:47:18 -08:00
|
|
|
$contactUsFormSubmissionSaved = $this->ContactUsFormSubmissions->save($contactUsFormSubmission);
|
2025-11-10 22:26:52 -08:00
|
|
|
|
2025-01-09 23:47:18 -08:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|