first real commit in standalone repo
This commit is contained in:
98
src/CakeContactUsPlugin.php
Normal file
98
src/CakeContactUsPlugin.php
Normal file
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CakeContactUs;
|
||||
|
||||
use Cake\Console\CommandCollection;
|
||||
use Cake\Core\BasePlugin;
|
||||
use Cake\Core\ContainerInterface;
|
||||
use Cake\Core\PluginApplicationInterface;
|
||||
use Cake\Http\MiddlewareQueue;
|
||||
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';
|
||||
|
||||
/**
|
||||
* Load all the plugin configuration and bootstrap logic.
|
||||
*
|
||||
* The host application is provided as an argument. This allows you to load
|
||||
* additional plugin dependencies, or attach events.
|
||||
*
|
||||
* @param \Cake\Core\PluginApplicationInterface $app The host application
|
||||
* @return 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,
|
||||
* you can create `$plugin/config/routes.php` and delete this method.
|
||||
*
|
||||
* @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']);
|
||||
|
||||
$routes->fallbacks();
|
||||
});
|
||||
});
|
||||
$routes->plugin('CakeContactUs', ['path' => '/contact-us'], function (RouteBuilder $routes): void {
|
||||
$routes->connect('/', ['controller' => 'ContactUsFormSubmissions', 'action' => 'add']);
|
||||
});
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
$commands = parent::console($commands);
|
||||
|
||||
return $commands;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register application container services.
|
||||
*
|
||||
* @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
|
||||
}
|
||||
}
|
||||
88
src/Controller/Admin/ContactUsFormSubmissionsController.php
Normal file
88
src/Controller/Admin/ContactUsFormSubmissionsController.php
Normal file
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CakeContactUs\Controller\Admin;
|
||||
|
||||
use App\Controller\AppController;
|
||||
|
||||
/**
|
||||
* ContactUsFormSubmissions Controller
|
||||
*
|
||||
* @property \CakeContactUs\Model\Table\ContactUsFormSubmissionsTable $ContactUsFormSubmissions
|
||||
*/
|
||||
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);
|
||||
|
||||
$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.
|
||||
*/
|
||||
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.
|
||||
*/
|
||||
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'));
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
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']);
|
||||
}
|
||||
}
|
||||
10
src/Controller/AppController.php
Normal file
10
src/Controller/AppController.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CakeContactUs\Controller;
|
||||
|
||||
use App\Controller\AppController as BaseController;
|
||||
|
||||
class AppController extends BaseController
|
||||
{
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
34
src/Controller/ContactUsFormSubmissionsController.php
Normal file
34
src/Controller/ContactUsFormSubmissionsController.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CakeContactUs\Controller;
|
||||
|
||||
use App\Controller\AppController;
|
||||
|
||||
/**
|
||||
* ContactUsFormSubmissions Controller
|
||||
*
|
||||
* @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');
|
||||
}
|
||||
|
||||
/**
|
||||
* 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'));
|
||||
}
|
||||
}
|
||||
65
src/Mailer/ContactUsFormSubmissionsMailer.php
Normal file
65
src/Mailer/ContactUsFormSubmissionsMailer.php
Normal file
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CakeContactUs\Mailer;
|
||||
|
||||
use Cake\Core\Configure;
|
||||
use Cake\Datasource\EntityInterface;
|
||||
use Cake\Mailer\Mailer;
|
||||
use Cake\Mailer\Message;
|
||||
|
||||
/**
|
||||
* User Mailer
|
||||
*/
|
||||
class ContactUsFormSubmissionsMailer extends Mailer
|
||||
{
|
||||
/**
|
||||
* Send the templated email to the user
|
||||
*
|
||||
* @param \Cake\Datasource\EntityInterface $user User entity
|
||||
* @return void
|
||||
*/
|
||||
protected function confirmation(EntityInterface $contactUsFormSubmission, array $options = [])
|
||||
{
|
||||
$name = isset($contactUsFormSubmission['name']) ? $contactUsFormSubmission['name'] . ', ' : '';
|
||||
// un-hide the token to be able to send it in the email content
|
||||
$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->viewBuilder()
|
||||
->setTemplate('CakeContactUs.confirmation');
|
||||
}
|
||||
|
||||
/**
|
||||
* Send to backoffice to take action
|
||||
*
|
||||
* @param \Cake\Datasource\EntityInterface $user User entity
|
||||
* @return void
|
||||
*/
|
||||
protected function backend(EntityInterface $contactUsFormSubmission, array $options = [])
|
||||
{
|
||||
$name = isset($contactUsFormSubmission['name']) ? ' by ' . $contactUsFormSubmission['name'] : '';
|
||||
// un-hide the token to be able to send it in the email content
|
||||
$subject = __d('cake_contact_us', 'Contact Us Form Submitted');
|
||||
|
||||
|
||||
$this
|
||||
->setTo(Configure::readOrFail('ContactUs.email.backend.to'))
|
||||
->setSubject($subject . $name)
|
||||
->setEmailFormat(Message::MESSAGE_BOTH)
|
||||
->setViewVars([
|
||||
'contactUsFormSubmission' => $contactUsFormSubmission,
|
||||
]);
|
||||
|
||||
$this->viewBuilder()
|
||||
->setTemplate('CakeContactUs.backend');
|
||||
}
|
||||
}
|
||||
42
src/Model/Entity/ContactUsFormSubmission.php
Normal file
42
src/Model/Entity/ContactUsFormSubmission.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CakeContactUs\Model\Entity;
|
||||
|
||||
use Cake\ORM\Entity;
|
||||
|
||||
/**
|
||||
* ContactUsFormSubmission Entity
|
||||
*
|
||||
* @property string $id
|
||||
* @property \Cake\I18n\DateTime $submitted_at
|
||||
* @property string $client_ip
|
||||
* @property string $name
|
||||
* @property string|null $email
|
||||
* @property string|null $subject
|
||||
* @property string $message
|
||||
* @property \Cake\I18n\DateTime|null $confirm_email_sent
|
||||
* @property \Cake\I18n\DateTime|null $backend_email_sent
|
||||
*/
|
||||
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
|
||||
* be mass assigned. For security purposes, it is advised to set '*' to false
|
||||
* (or remove it), and explicitly make individual fields accessible as needed.
|
||||
*
|
||||
* @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,
|
||||
];
|
||||
}
|
||||
157
src/Model/Table/ContactUsFormSubmissionsTable.php
Normal file
157
src/Model/Table/ContactUsFormSubmissionsTable.php
Normal file
@@ -0,0 +1,157 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CakeContactUs\Model\Table;
|
||||
|
||||
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\Model\Entity\ContactUsFormSubmission;
|
||||
use Closure;
|
||||
use Psr\SimpleCache\CacheInterface;
|
||||
|
||||
/**
|
||||
* ContactUsFormSubmissions Model
|
||||
*
|
||||
* @method ContactUsFormSubmission newEmptyEntity()
|
||||
* @method 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 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 = [])
|
||||
*/
|
||||
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);
|
||||
|
||||
$this->setTable('contact_us_form_submissions');
|
||||
$this->setDisplayField('name');
|
||||
$this->setPrimaryKey('id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Default validation rules.
|
||||
*
|
||||
* @param Validator $validator Validator instance.
|
||||
* @return Validator
|
||||
*/
|
||||
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)
|
||||
->requirePresence('client_ip', 'create')
|
||||
->notEmptyString('client_ip');
|
||||
|
||||
$validator
|
||||
->scalar('name')
|
||||
->maxLength('name', 255)
|
||||
->requirePresence('name', 'create')
|
||||
->notEmptyString('name');
|
||||
|
||||
// email
|
||||
$validator->email('email');
|
||||
if ($fields['email']) {
|
||||
$validator->notEmptyString('email');
|
||||
} else {
|
||||
$validator->allowEmptyString('email');
|
||||
}
|
||||
|
||||
// subject
|
||||
$validator
|
||||
->scalar('subject')
|
||||
->maxLength('subject', 255);
|
||||
if ($fields['subject']) {
|
||||
$validator->notEmptyString('subject');
|
||||
} else {
|
||||
$validator->allowEmptyString('subject');
|
||||
}
|
||||
|
||||
$validator
|
||||
->scalar('message')
|
||||
->requirePresence('message', 'create')
|
||||
->notEmptyString('message');
|
||||
|
||||
$validator
|
||||
->dateTime('confirm_email_sent')
|
||||
->allowEmptyDateTime('confirm_email_sent');
|
||||
|
||||
$validator
|
||||
->dateTime('backend_email_sent')
|
||||
->allowEmptyDateTime('backend_email_sent');
|
||||
|
||||
return $validator;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param EventInterface $event
|
||||
* @param EntityInterface|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(DATETIME_MYSQL);
|
||||
$updateData = [];
|
||||
if (Configure::read('ContactUs.email.confirmation.enabled') && 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::readOrFail('ContactUs.email.backend.enabled')) {
|
||||
$mailer = Configure::read('ContactUs.email.mailerClass', 'CakeContactUs.ContactUsFormSubmissions');
|
||||
try {
|
||||
$this->getMailer($mailer)->send('backend', [$contactUsFormSubmission]);
|
||||
$updateData['backend_email_sent'] = $now;
|
||||
} catch (CakeException $exception) {
|
||||
|
||||
}
|
||||
}
|
||||
Log::debug(print_r('$updateData', true));
|
||||
Log::debug(print_r($updateData, true));
|
||||
if ($updateData) {
|
||||
$contactUsFormSubmission = $this->patchEntity($contactUsFormSubmission, $updateData, ['validate' => false]);
|
||||
$this->saveOrFail($contactUsFormSubmission);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user