2025-01-09 23:47:18 -08:00
< ? php
declare ( strict_types = 1 );
namespace CakeContactUs\Controller\Component ;
use Cake\Controller\Component ;
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\CakeContactUsPlugin ;
2026-01-24 01:20:17 -08:00
use CakeContactUs\Model\Table\ContactUsFormSubmissionsTable ;
2025-01-09 23:47:18 -08:00
/**
* ContactUs component
2026-01-24 01:20:17 -08:00
*
* @property \Cake\Controller\Component\FlashComponent $Flash
2025-01-09 23:47:18 -08:00
*/
2026-01-24 01:20:17 -08:00
class ContactUsComponent extends Component {
/**
* @var \CakeContactUs\Model\Table\ContactUsFormSubmissionsTable|\Cake\ORM\Table
2025-01-09 23:47:18 -08:00
*/
2026-01-24 01:20:17 -08:00
protected ContactUsFormSubmissionsTable | Table $ContactUsFormSubmissions ;
2025-01-09 23:47:18 -08:00
2026-01-24 01:20:17 -08:00
/**
2025-01-09 23:47:18 -08:00
* Default configuration.
*
* @var array<string, mixed>
*/
2026-01-24 01:20:17 -08:00
protected array $_defaultConfig = [];
2025-01-09 23:47:18 -08:00
2026-01-24 01:20:17 -08:00
/**
2025-01-09 23:47:18 -08:00
* @var array|string[]
*/
2026-01-24 01:20:17 -08:00
protected array $components = [ 'Flash' ];
2025-01-09 23:47:18 -08:00
2026-01-24 01:20:17 -08:00
/**
2025-01-09 23:47:18 -08:00
* @param array $config
2026-01-24 01:20:17 -08:00
* @throws \Exception
2025-01-09 23:47:18 -08:00
* @return void
*
*/
2026-01-24 01:20:17 -08:00
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');
2026-01-24 01:20:17 -08:00
$this -> getController () -> viewBuilder () -> addHelpers ([ 'Captcha.Captcha' ]);
}
}
2025-01-09 23:47:18 -08:00
2026-01-24 01:20:17 -08:00
/**
* @return \Cake\Datasource\EntityInterface|\CakeContactUs\Model\Entity\ContactUsFormSubmission
2025-01-09 23:47:18 -08:00
*/
2026-01-24 01:20:17 -08:00
public function newContactUsForm () {
if ( $this -> getConfig ( 'requireCaptcha' )) {
$this -> ContactUsFormSubmissions -> addBehavior ( 'Captcha.Captcha' );
}
2025-11-10 22:26:52 -08:00
2026-01-24 01:20:17 -08:00
return $this -> ContactUsFormSubmissions -> newEmptyEntity ();
}
2025-01-09 23:47:18 -08:00
2026-01-24 01:20:17 -08:00
/**
* @return \Cake\Http\Response|null|void
2025-01-09 23:47:18 -08:00
*/
2026-01-24 01:20:17 -08:00
public function processContactUsForm ( EntityInterface $contactUsFormSubmission , array | null $postData ) {
if ( ! isset ( $postData )) {
$postData = $this -> getController () -> getRequest () -> getData ();
}
$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 ()
);
$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 ());
}
2026-01-26 23:31:29 -08:00
if ( ! $postData ) {
2026-01-24 01:20:17 -08:00
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 ]);
}
/**
2025-01-09 23:47:18 -08:00
* Prepare flash messages after registration, and dispatch afterRegister event
*
2026-01-24 01:20:17 -08:00
* @param \Cake\Datasource\EntityInterface|\CakeContactUs\Model\Entity\ContactUsFormSubmission $contactUsFormSubmissionSaved Contact us form submission entity
2025-01-09 23:47:18 -08:00
* @return \Cake\Http\Response
*/
2026-01-24 01:20:17 -08:00
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 );
}
2025-01-09 23:47:18 -08:00
}