WIP - phpstan and phpcs - tests failing currently

This commit is contained in:
2026-01-24 01:20:17 -08:00
parent 31650e55c2
commit 37b7bca3be
17 changed files with 888 additions and 822 deletions

View File

@@ -13,12 +13,23 @@ use Cake\Routing\RouteBuilder;
/**
* Plugin for CakeContactUs
*/
class CakeContactUsPlugin extends BasePlugin
{
public const EVENT_BEFORE_CONTACT_US_FORM_SAVED = 'ContactUs.Global.beforeRegister';
public const EVENT_AFTER_CONTACT_US_FORM_SAVED = 'ContactUs.Global.afterRegister';
public const CAKE_CONTACT_US_MYSQL_DATETIME = 'Y-m-d H:i:s';
/**
class CakeContactUsPlugin extends BasePlugin {
/**
* @var string
*/
public const EVENT_BEFORE_CONTACT_US_FORM_SAVED = 'ContactUs.Global.beforeRegister';
/**
* @var string
*/
public const EVENT_AFTER_CONTACT_US_FORM_SAVED = 'ContactUs.Global.afterRegister';
/**
* @var string
*/
public const CAKE_CONTACT_US_MYSQL_DATETIME = 'Y-m-d H:i:s';
/**
* Load all the plugin configuration and bootstrap logic.
*
* The host application is provided as an argument. This allows you to load
@@ -27,11 +38,10 @@ class CakeContactUsPlugin extends BasePlugin
* @param \Cake\Core\PluginApplicationInterface $app The host application
* @return void
*/
public function bootstrap(PluginApplicationInterface $app): void
{
}
public function bootstrap(PluginApplicationInterface $app): void {
}
/**
/**
* Add routes for the plugin.
*
* If your plugin has many routes and you would like to isolate them into a separate file,
@@ -40,59 +50,56 @@ class CakeContactUsPlugin extends BasePlugin
* @param \Cake\Routing\RouteBuilder $routes The route builder to update.
* @return void
*/
public function routes(RouteBuilder $routes): void
{
$routes->prefix('Admin', function (RouteBuilder $routes): void {
$routes->plugin('CakeContactUs', function (RouteBuilder $routes): void {
$routes->connect('/', ['controller' => 'ContactUsFormSubmissions', 'action' => 'index']);
public function routes(RouteBuilder $routes): void {
$routes->prefix('Admin', function (RouteBuilder $routes): void {
$routes->plugin('CakeContactUs', function (RouteBuilder $routes): void {
$routes->connect('/', ['controller' => 'ContactUsFormSubmissions', 'action' => 'index']);
$routes->fallbacks();
});
});
$routes->plugin('CakeContactUs', ['path' => '/contact-us'], function (RouteBuilder $routes): void {
$routes->connect('/', ['controller' => 'ContactUsFormSubmissions', 'action' => 'add']);
});
$routes->fallbacks();
});
});
$routes->plugin('CakeContactUs', ['path' => '/contact-us'], function (RouteBuilder $routes): void {
$routes->connect('/', ['controller' => 'ContactUsFormSubmissions', 'action' => 'add']);
});
parent::routes($routes);
}
parent::routes($routes);
}
/**
/**
* Add middleware for the plugin.
*
* @param \Cake\Http\MiddlewareQueue $middlewareQueue The middleware queue to update.
* @return \Cake\Http\MiddlewareQueue
*/
public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue
{
// Add your middlewares here
public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue {
// Add your middlewares here
return $middlewareQueue;
}
return $middlewareQueue;
}
/**
/**
* Add commands for the plugin.
*
* @param \Cake\Console\CommandCollection $commands The command collection to update.
* @return \Cake\Console\CommandCollection
*/
public function console(CommandCollection $commands): CommandCollection
{
// Add your commands here
public function console(CommandCollection $commands): CommandCollection {
// Add your commands here
$commands = parent::console($commands);
$commands = parent::console($commands);
return $commands;
}
return $commands;
}
/**
/**
* Register application container services.
*
* @link https://book.cakephp.org/4/en/development/dependency-injection.html#dependency-injection
* @param \Cake\Core\ContainerInterface $container The Container to update.
* @return void
* @link https://book.cakephp.org/4/en/development/dependency-injection.html#dependency-injection
*/
public function services(ContainerInterface $container): void
{
// Add your services here
}
public function services(ContainerInterface $container): void {
// Add your services here
}
}

View File

@@ -10,79 +10,74 @@ use App\Controller\AppController;
*
* @property \CakeContactUs\Model\Table\ContactUsFormSubmissionsTable $ContactUsFormSubmissions
*/
class ContactUsFormSubmissionsController extends AppController
{
public function initialize(): void
{
parent::initialize(); // TODO: Change the autogenerated stub
}
class ContactUsFormSubmissionsController extends AppController {
/**
public function initialize(): void {
parent::initialize(); // TODO: Change the autogenerated stub
}
/**
* Index method
*
* @return \Cake\Http\Response|null|void Renders view
*/
public function index()
{
$query = $this->ContactUsFormSubmissions->find();
$contactUsFormSubmissions = $this->paginate($query);
public function index() {
$query = $this->ContactUsFormSubmissions->find();
$contactUsFormSubmissions = $this->paginate($query);
$this->set(compact('contactUsFormSubmissions'));
}
$this->set(compact('contactUsFormSubmissions'));
}
/**
/**
* View method
*
* @param string|null $id Contact Us Form Submission id.
* @return \Cake\Http\Response|null|void Renders view
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
* @return \Cake\Http\Response|null|void Renders view
*/
public function view($id = null)
{
$contactUsFormSubmission = $this->ContactUsFormSubmissions->get($id, contain: []);
$this->set(compact('contactUsFormSubmission'));
}
public function view($id = null) {
$contactUsFormSubmission = $this->ContactUsFormSubmissions->get($id, contain: []);
$this->set(compact('contactUsFormSubmission'));
}
/**
/**
* Edit method
*
* @param string|null $id Contact Us Form Submission id.
* @return \Cake\Http\Response|null|void Redirects on successful edit, renders view otherwise.
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
* @return \Cake\Http\Response|null|void Redirects on successful edit, renders view otherwise.
*/
public function edit($id = null)
{
$contactUsFormSubmission = $this->ContactUsFormSubmissions->get($id, contain: []);
if ($this->request->is(['patch', 'post', 'put'])) {
$contactUsFormSubmission = $this->ContactUsFormSubmissions->patchEntity($contactUsFormSubmission, $this->request->getData());
if ($this->ContactUsFormSubmissions->save($contactUsFormSubmission)) {
$this->Flash->success(__('The contact us form submission has been saved.'));
public function edit($id = null) {
$contactUsFormSubmission = $this->ContactUsFormSubmissions->get($id, contain: []);
if ($this->request->is(['patch', 'post', 'put'])) {
$contactUsFormSubmission = $this->ContactUsFormSubmissions->patchEntity($contactUsFormSubmission, $this->request->getData());
if ($this->ContactUsFormSubmissions->save($contactUsFormSubmission)) {
$this->Flash->success(__('The contact us form submission has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The contact us form submission could not be saved. Please, try again.'));
}
$this->set(compact('contactUsFormSubmission'));
}
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The contact us form submission could not be saved. Please, try again.'));
}
$this->set(compact('contactUsFormSubmission'));
}
/**
/**
* Delete method
*
* @param string|null $id Contact Us Form Submission id.
* @return \Cake\Http\Response|null Redirects to index.
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
* @return \Cake\Http\Response|null Redirects to index.
*/
public function delete($id = null)
{
$this->request->allowMethod(['post', 'delete']);
$contactUsFormSubmission = $this->ContactUsFormSubmissions->get($id);
if ($this->ContactUsFormSubmissions->delete($contactUsFormSubmission)) {
$this->Flash->success(__('The contact us form submission has been deleted.'));
} else {
$this->Flash->error(__('The contact us form submission could not be deleted. Please, try again.'));
}
public function delete($id = null) {
$this->request->allowMethod(['post', 'delete']);
$contactUsFormSubmission = $this->ContactUsFormSubmissions->get($id);
if ($this->ContactUsFormSubmissions->delete($contactUsFormSubmission)) {
$this->Flash->success(__('The contact us form submission has been deleted.'));
} else {
$this->Flash->error(__('The contact us form submission could not be deleted. Please, try again.'));
}
return $this->redirect(['action' => 'index']);
}
return $this->redirect(['action' => 'index']);
}
}

View File

@@ -4,7 +4,6 @@ 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;
@@ -12,158 +11,155 @@ 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;
use CakeContactUs\Model\Table\ContactUsFormSubmissionsTable;
/**
* ContactUs component
*
* @property \Cake\Controller\Component\FlashComponent $Flash
*/
class ContactUsComponent extends Component
{
/**
* @var ContactUsFormSubmissionsTable|Table
*/
protected ContactUsFormSubmissionsTable|Table $ContactUsFormSubmissions;
class ContactUsComponent extends Component {
/**
/**
* @var \CakeContactUs\Model\Table\ContactUsFormSubmissionsTable|\Cake\ORM\Table
*/
protected ContactUsFormSubmissionsTable|Table $ContactUsFormSubmissions;
/**
* Default configuration.
*
* @var array<string, mixed>
*/
protected array $_defaultConfig = [];
protected array $_defaultConfig = [];
/**
/**
* @var array|string[]
*/
protected array $components = ['Flash'];
protected array $components = ['Flash'];
/**
/**
* @param array $config
* @throws \Exception
* @return void
*
* @throws Exception
*/
public function initialize(array $config): void
{
parent::initialize($config); // TODO: Change the autogenerated stub
public function initialize(array $config): void {
parent::initialize($config); // TODO: Change the autogenerated stub
$this->ContactUsFormSubmissions = TableRegistry::getTableLocator()->get('CakeContactUs.ContactUsFormSubmissions');
$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) {
$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');
$this->getController()->viewBuilder()->addHelpers(['Captcha.Captcha']);
}
}
$this->getController()->viewBuilder()->addHelpers(['Captcha.Captcha']);
}
}
/**
* @return EntityInterface|ContactUsFormSubmission
/**
* @return \Cake\Datasource\EntityInterface|\CakeContactUs\Model\Entity\ContactUsFormSubmission
*/
public function newContactUsForm()
{
if ($this->getConfig('requireCaptcha')) {
$this->ContactUsFormSubmissions->addBehavior('Captcha.Captcha');
}
public function newContactUsForm() {
if ($this->getConfig('requireCaptcha')) {
$this->ContactUsFormSubmissions->addBehavior('Captcha.Captcha');
}
return $this->ContactUsFormSubmissions->newEmptyEntity();
}
return $this->ContactUsFormSubmissions->newEmptyEntity();
}
/**
* @return EntityInterface|ContactUsFormSubmission
/**
* @return \Cake\Http\Response|null|void
*/
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();
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();
$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);
}
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
}
// @TODO contact us form submission failed - handle here
}
if ($event->isStopped()) {
return $this->getController()->redirect($event->getResult());
}
if ($event->isStopped()) {
return $this->getController()->redirect($event->getResult());
}
if (!$this->getController()->getRequest()->is('post')) {
return;
}
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));
}
$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);
$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.');
if ($contactUsFormSubmissionSaved) {
return $this->_afterFormSaved($contactUsFormSubmissionSaved);
}
$message = __d('cake_contact_us', 'Something doesn\'t look quite right. Please try again.');
$this->Flash->error($message);
$this->Flash->error($message);
// @TODO contact us form submission failed - handle here
$this->getController()->set(['contactUsFormSubmission' => $contactUsFormSubmission]);
}
// @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
* @param \Cake\Datasource\EntityInterface|\CakeContactUs\Model\Entity\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);
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);
}
$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);
}
}

View File

@@ -8,27 +8,27 @@ use App\Controller\AppController;
/**
* ContactUsFormSubmissions Controller
*
* @property \CakeContactUs\Controller\Component\ContactUsComponent $ContactUs
* @property \CakeContactUs\Model\Table\ContactUsFormSubmissionsTable $ContactUsFormSubmissions
*/
class ContactUsFormSubmissionsController extends AppController
{
public function initialize(): void
{
parent::initialize(); // TODO: Change the autogenerated stub
$this->loadComponent('CakeContactUs.ContactUs');
}
class ContactUsFormSubmissionsController extends AppController {
/**
public function initialize(): void {
parent::initialize(); // TODO: Change the autogenerated stub
$this->loadComponent('CakeContactUs.ContactUs');
}
/**
* Add method
*
* @return \Cake\Http\Response|null|void Redirects on successful add, renders view otherwise.
*/
public function add()
{
$contactUsFormSubmission = $this->ContactUs->newContactUsForm();
if ($this->request->is('post')) {
return $this->ContactUs->processContactUsForm($contactUsFormSubmission, $this->request->getData());
}
$this->set(compact('contactUsFormSubmission'));
}
public function add() {
$contactUsFormSubmission = $this->ContactUs->newContactUsForm();
if ($this->request->is('post')) {
return $this->ContactUs->processContactUsForm($contactUsFormSubmission, $this->request->getData());
}
$this->set(compact('contactUsFormSubmission'));
}
}

View File

@@ -4,72 +4,73 @@ declare(strict_types=1);
namespace CakeContactUs\Mailer;
use Cake\Core\Configure;
use Cake\Datasource\EntityInterface;
use Cake\Mailer\Mailer;
use Cake\Mailer\Message;
use CakeContactUs\Model\Entity\ContactUsFormSubmission;
/**
* User Mailer
*/
class ContactUsFormSubmissionsMailer extends Mailer
{
/**
* Send the templated email to the user
class ContactUsFormSubmissionsMailer extends Mailer {
/**
* Send the confirmation email to the user
*
* @param \Cake\Datasource\EntityInterface $user User entity
* @param \CakeContactUs\Model\Entity\ContactUsFormSubmission $contactUsFormSubmission contact us form submission entity
* @param array $options
* @return void
*/
protected function confirmation(EntityInterface $contactUsFormSubmission, array $options = [])
{
$name = isset($contactUsFormSubmission['name']) ? $contactUsFormSubmission['name'] . ', ' : '';
$subject = __d('cake_contact_us', 'We have received your message');
protected function confirmation(ContactUsFormSubmission $contactUsFormSubmission, array $options = []) {
$name = isset($contactUsFormSubmission->name) ? $contactUsFormSubmission->name . ', ' : '';
$subject = __d('cake_contact_us', 'We have received your message');
$this
->setTo($contactUsFormSubmission['email'])
->setSubject($name . $subject)
->setEmailFormat(Message::MESSAGE_BOTH)
->setViewVars([
'contactUsFormSubmission' => $contactUsFormSubmission,
]);
$this
->setTo($contactUsFormSubmission->email)
->setSubject($name . $subject)
->setEmailFormat(Message::MESSAGE_BOTH)
->setViewVars([
'contactUsFormSubmission' => $contactUsFormSubmission,
]);
$this->viewBuilder()
->setTemplate('CakeContactUs.confirmation');
}
$this->viewBuilder()
->setTemplate('CakeContactUs.confirmation');
}
/**
/**
* Send to backoffice to take action
*
* @param \Cake\Datasource\EntityInterface $user User entity
* @param \CakeContactUs\Model\Entity\ContactUsFormSubmission $contactUsFormSubmission contact us form submission entity
* @param array $options
* @return void
*/
protected function backend(EntityInterface $contactUsFormSubmission, array $options = [])
{
$subject = __d('cake_contact_us', 'Contact Us Form Submitted');
$name = isset($contactUsFormSubmission['name']) ? ' by ' . $contactUsFormSubmission['name'] : '';
protected function backend(ContactUsFormSubmission $contactUsFormSubmission, array $options = []) {
$subject = __d('cake_contact_us', 'Contact Us Form Submitted');
$name = isset($contactUsFormSubmission->name) ? ' by ' . $contactUsFormSubmission->name : '';
$to = Configure::readOrFail('ContactUs.email.backend.to');
$cc = Configure::read('ContactUs.email.backend.cc', []);
$bcc = Configure::read('ContactUs.email.backend.bcc', []);
$to = !is_array($to) ? [$to] : $to;
$to = Configure::readOrFail('ContactUs.email.backend.to');
$cc = Configure::read('ContactUs.email.backend.cc', []);
$bcc = Configure::read('ContactUs.email.backend.bcc', []);
$to = !is_array($to) ? [$to] : $to;
$this
->setTo($to)
->setSubject($subject . $name)
->setEmailFormat(Message::MESSAGE_BOTH)
->setViewVars([
'contactUsFormSubmission' => $contactUsFormSubmission,
]);
$this
->setTo($to)
->setSubject($subject . $name)
->setEmailFormat(Message::MESSAGE_BOTH)
->setViewVars([
'contactUsFormSubmission' => $contactUsFormSubmission,
]);
if ($cc) {
$cc = !is_array($cc) ? [$cc] : $cc;
$this->setCc($to);
}
if ($bcc) {
$bcc = !is_array($bcc) ? [$bcc] : $bcc;
$this->setBcc($bcc);
}
if ($cc) {
$cc = !is_array($cc) ? [$cc] : $cc;
$this->setCc($to);
}
if ($bcc) {
$bcc = !is_array($bcc) ? [$bcc] : $bcc;
$this->setBcc($bcc);
}
$this->viewBuilder()
->setTemplate('CakeContactUs.backend');
}
$this->viewBuilder()
->setTemplate('CakeContactUs.backend');
}
}

View File

@@ -18,9 +18,9 @@ use Cake\ORM\Entity;
* @property \Cake\I18n\DateTime|null $confirm_email_sent
* @property \Cake\I18n\DateTime|null $backend_email_sent
*/
class ContactUsFormSubmission extends Entity
{
/**
class ContactUsFormSubmission extends Entity {
/**
* Fields that can be mass assigned using newEntity() or patchEntity().
*
* Note that when '*' is set to true, this allows all unspecified fields to
@@ -29,14 +29,15 @@ class ContactUsFormSubmission extends Entity
*
* @var array<string, bool>
*/
protected array $_accessible = [
'submitted_at' => true,
'client_ip' => true,
'name' => true,
'email' => true,
'subject' => true,
'message' => true,
'confirm_email_sent' => true,
'backend_email_sent' => true,
];
protected array $_accessible = [
'submitted_at' => true,
'client_ip' => true,
'name' => true,
'email' => true,
'subject' => true,
'message' => true,
'confirm_email_sent' => true,
'backend_email_sent' => true,
];
}

View File

@@ -3,151 +3,144 @@ declare(strict_types=1);
namespace CakeContactUs\Model\Table;
use ArrayObject;
use Cake\Core\Configure;
use Cake\Core\Exception\CakeException;
use Cake\Datasource\EntityInterface;
use Cake\Datasource\ResultSetInterface;
use Cake\Event\EventInterface;
use Cake\I18n\DateTime;
use Cake\Log\Log;
use Cake\Mailer\MailerAwareTrait;
use Cake\ORM\Query\SelectQuery;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
use CakeContactUs\CakeContactUsPlugin;
use CakeContactUs\Model\Entity\ContactUsFormSubmission;
use Closure;
use Psr\SimpleCache\CacheInterface;
/**
* ContactUsFormSubmissions Model
*
* @method ContactUsFormSubmission newEmptyEntity()
* @method ContactUsFormSubmission newEntity(array $data, array $options = [])
* @method \CakeContactUs\Model\Entity\ContactUsFormSubmission newEmptyEntity()
* @method \CakeContactUs\Model\Entity\ContactUsFormSubmission newEntity(array $data, array $options = [])
* @method array<ContactUsFormSubmission> newEntities(array $data, array $options = [])
* @method ContactUsFormSubmission get(mixed $primaryKey, array|string $finder = 'all', CacheInterface|string|null $cache = null, Closure|string|null $cacheKey = null, mixed ...$args)
* @method ContactUsFormSubmission findOrCreate($search, ?callable $callback = null, array $options = [])
* @method ContactUsFormSubmission patchEntity(EntityInterface $entity, array $data, array $options = [])
* @method \CakeContactUs\Model\Entity\ContactUsFormSubmission get(mixed $primaryKey, array|string $finder = 'all', \Psr\SimpleCache\CacheInterface|string|null $cache = null, \Closure|string|null $cacheKey = null, mixed ...$args)
* @method \CakeContactUs\Model\Entity\ContactUsFormSubmission findOrCreate($search, ?callable $callback = null, array $options = [])
* @method \CakeContactUs\Model\Entity\ContactUsFormSubmission patchEntity(EntityInterface $entity, array $data, array $options = [])
* @method array<ContactUsFormSubmission> patchEntities(iterable $entities, array $data, array $options = [])
* @method ContactUsFormSubmission|false save(EntityInterface $entity, array $options = [])
* @method ContactUsFormSubmission saveOrFail(EntityInterface $entity, array $options = [])
* @method iterable<ContactUsFormSubmission>|ResultSetInterface<ContactUsFormSubmission>|false saveMany(iterable $entities, array $options = [])
* @method iterable<ContactUsFormSubmission>|ResultSetInterface<ContactUsFormSubmission> saveManyOrFail(iterable $entities, array $options = [])
* @method iterable<ContactUsFormSubmission>|ResultSetInterface<ContactUsFormSubmission>|false deleteMany(iterable $entities, array $options = [])
* @method iterable<ContactUsFormSubmission>|ResultSetInterface<ContactUsFormSubmission> deleteManyOrFail(iterable $entities, array $options = [])
* @method \CakeContactUs\Model\Entity\ContactUsFormSubmission saveOrFail(\Cake\Datasource\EntityInterface $entity, array $options = [])
* @method iterable<ContactUsFormSubmission>|\Cake\Datasource\ResultSetInterface<ContactUsFormSubmission>|false saveMany(iterable $entities, array $options = [])
* @method iterable<ContactUsFormSubmission>|\Cake\Datasource\ResultSetInterface<ContactUsFormSubmission> saveManyOrFail(iterable $entities, array $options = [])
* @method iterable<ContactUsFormSubmission>|\Cake\Datasource\ResultSetInterface<ContactUsFormSubmission>|false deleteMany(iterable $entities, array $options = [])
* @method iterable<ContactUsFormSubmission>|\Cake\Datasource\ResultSetInterface<ContactUsFormSubmission> deleteManyOrFail(iterable $entities, array $options = [])
*/
class ContactUsFormSubmissionsTable extends Table
{
use MailerAwareTrait;
class ContactUsFormSubmissionsTable extends Table {
/**
use MailerAwareTrait;
/**
* Initialize method
*
* @param array<string, mixed> $config The configuration for the Table.
* @return void
*/
public function initialize(array $config): void
{
parent::initialize($config);
public function initialize(array $config): void {
parent::initialize($config);
$this->setTable('contact_us_form_submissions');
$this->setDisplayField('name');
$this->setPrimaryKey('id');
}
$this->setTable('contact_us_form_submissions');
$this->setDisplayField('name');
$this->setPrimaryKey('id');
}
/**
/**
* Default validation rules.
*
* @param Validator $validator Validator instance.
* @return Validator
* @param \Cake\Validation\Validator $validator Validator instance.
* @return \Cake\Validation\Validator
*/
public function validationDefault(Validator $validator): Validator
{
$fields = Configure::readOrFail('ContactUs.fields');
$validator
->dateTime('submitted_at')
->requirePresence('submitted_at', 'create')
->notEmptyDateTime('submitted_at');
public function validationDefault(Validator $validator): Validator {
$fields = Configure::readOrFail('ContactUs.fields');
$validator
->dateTime('submitted_at')
->requirePresence('submitted_at', 'create')
->notEmptyDateTime('submitted_at');
$validator
->scalar('client_ip')
->maxLength('client_ip', 45)
->allowEmptyString('client_ip');
$validator
->scalar('client_ip')
->maxLength('client_ip', 45)
->allowEmptyString('client_ip');
$validator
->scalar('name')
->maxLength('name', 255)
->requirePresence('name', 'create')
->notEmptyString('name');
$validator
->scalar('name')
->maxLength('name', 255)
->requirePresence('name', 'create')
->notEmptyString('name');
// email
$validator->email('email');
if ($fields['email'] ?? false) {
$validator->notEmptyString('email');
} else {
$validator->allowEmptyString('email');
}
// email
$validator->email('email');
if ($fields['email'] ?? false) {
$validator->notEmptyString('email');
} else {
$validator->allowEmptyString('email');
}
// subject
$validator
->scalar('subject')
->maxLength('subject', 255);
if ($fields['subject'] ?? false) {
$validator->notEmptyString('subject');
} else {
$validator->allowEmptyString('subject');
}
// subject
$validator
->scalar('subject')
->maxLength('subject', 255);
if ($fields['subject'] ?? false) {
$validator->notEmptyString('subject');
} else {
$validator->allowEmptyString('subject');
}
$validator
->scalar('message')
->requirePresence('message', 'create')
->notEmptyString('message');
$validator
->scalar('message')
->requirePresence('message', 'create')
->notEmptyString('message');
$validator
->dateTime('confirm_email_sent')
->allowEmptyDateTime('confirm_email_sent');
$validator
->dateTime('confirm_email_sent')
->allowEmptyDateTime('confirm_email_sent');
$validator
->dateTime('backend_email_sent')
->allowEmptyDateTime('backend_email_sent');
$validator
->dateTime('backend_email_sent')
->allowEmptyDateTime('backend_email_sent');
return $validator;
}
return $validator;
}
/**
* @param EventInterface $event
* @param EntityInterface|ContactUsFormSubmission $contactUsFormSubmission
/**
* @param \Cake\Event\EventInterface $event
* @param \Cake\Datasource\EntityInterface|\CakeContactUs\Model\Entity\ContactUsFormSubmission $contactUsFormSubmission
* @param \ArrayObject $options
* @return void
*/
public function afterSave(EventInterface $event, EntityInterface|ContactUsFormSubmission $contactUsFormSubmission, \ArrayObject $options)
{
if (!$contactUsFormSubmission->isNew()) {
return;
}
$now = DateTime::now()->format(CakeContactUsPlugin::CAKE_CONTACT_US_MYSQL_DATETIME);
$updateData = [];
if (Configure::read('ContactUs.email.confirmation', false) && isset($contactUsFormSubmission->email)) {
$mailer = Configure::read('ContactUs.email.mailerClass', 'CakeContactUs.ContactUsFormSubmissions');
try {
$this->getMailer($mailer)->send('confirmation', [$contactUsFormSubmission]);
$updateData['confirm_email_sent'] = $now;
} catch (CakeException $exception) {
}
}
public function afterSave(EventInterface $event, EntityInterface|ContactUsFormSubmission $contactUsFormSubmission, ArrayObject $options) {
if (!$contactUsFormSubmission->isNew()) {
return;
}
$now = DateTime::now()->format(CakeContactUsPlugin::CAKE_CONTACT_US_MYSQL_DATETIME);
$updateData = [];
if (Configure::read('ContactUs.email.confirmation', false) && isset($contactUsFormSubmission->email)) {
$mailer = Configure::read('ContactUs.email.mailerClass', 'CakeContactUs.ContactUsFormSubmissions');
try {
$this->getMailer($mailer)->send('confirmation', [$contactUsFormSubmission]);
$updateData['confirm_email_sent'] = $now;
} catch (CakeException $exception) {
}
}
if (Configure::read('ContactUs.email.backend.enabled', false)) {
$mailer = Configure::read('ContactUs.email.mailerClass', 'CakeContactUs.ContactUsFormSubmissions');
try {
$this->getMailer($mailer)->send('backend', [$contactUsFormSubmission]);
$updateData['backend_email_sent'] = $now;
} catch (CakeException $exception) {
}
}
if ($updateData) {
$contactUsFormSubmission = $this->patchEntity($contactUsFormSubmission, $updateData, ['validate' => false]);
$this->saveOrFail($contactUsFormSubmission);
}
}
if (Configure::read('ContactUs.email.backend.enabled', false)) {
$mailer = Configure::read('ContactUs.email.mailerClass', 'CakeContactUs.ContactUsFormSubmissions');
try {
$this->getMailer($mailer)->send('backend', [$contactUsFormSubmission]);
$updateData['backend_email_sent'] = $now;
} catch (CakeException $exception) {
}
}
if ($updateData) {
$contactUsFormSubmission = $this->patchEntity($contactUsFormSubmission, $updateData, ['validate' => false]);
$this->saveOrFail($contactUsFormSubmission);
}
}
}