2025-01-10 07:47:18 +00:00
|
|
|
<?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;
|
2025-01-10 09:34:02 +00:00
|
|
|
use CakeContactUs\CakeContactUsPlugin;
|
2025-01-10 07:47:18 +00:00
|
|
|
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;
|
|
|
|
|
}
|
2025-01-10 09:34:02 +00:00
|
|
|
$now = DateTime::now()->format(CakeContactUsPlugin::CAKE_CONTACT_US_MYSQL_DATETIME);
|
2025-01-10 07:47:18 +00:00
|
|
|
$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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|