Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).

## Unreleased
- Add statuses to Submission elements: the status indicates whether the submission is spam (fixes [#58](https://github.com/hybridinteractive/craft-contact-form-extensions/issues/58))
- Use unified element editor

## [4.2.2] - 2022-11-14
Fixed an issue where the settings tabs were not showing [#164](https://github.com/hybridinteractive/craft-contact-form-extensions/issues/164)

Expand Down
40 changes: 22 additions & 18 deletions src/ContactFormExtensions.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@
use craft\web\View;
use hybridinteractive\contactformextensions\base\Routes;
use hybridinteractive\contactformextensions\models\Settings;
use hybridinteractive\contactformextensions\services\ContactFormExtensionsService;
use hybridinteractive\contactformextensions\variables\ContactFormExtensionsVariable;
use yii\base\Event;

/**
* Class ContactFormExtensions.
*
* @property-read Settings $settings
* @property-read ContactFormExtensionsService $contactFormExtensionsService
*/
class ContactFormExtensions extends Plugin
{
Expand Down Expand Up @@ -173,25 +177,21 @@ private function _registerContactFormEventListeners(): void
{
// Capture Before Send Event from Craft Contact Form plugin
Event::on(CraftContactFormMailer::class, CraftContactFormMailer::EVENT_BEFORE_SEND, function (CraftContactFormSendEvent $e) {
if ($e->isSpam) {
return;
}

// Disable Recaptcha
$disableRecaptcha = false;
if (is_array($e->submission->message) && array_key_exists('disableRecaptcha', $e->submission->message)) {
$disableRecaptcha = filter_var($e->submission->message['disableRecaptcha'], FILTER_VALIDATE_BOOLEAN);
}

if ($this->settings->recaptcha && $disableRecaptcha != true) {
$recaptcha = $this->contactFormExtensionsService->getRecaptcha();
$captchaResponse = Craft::$app->request->getParam('g-recaptcha-response');
if (!$e->isSpam) {
// Disable Recaptcha
$disableRecaptcha = false;
if (is_array($e->submission->message) && array_key_exists('disableRecaptcha', $e->submission->message)) {
$disableRecaptcha = filter_var($e->submission->message['disableRecaptcha'], FILTER_VALIDATE_BOOLEAN);
}

if (!$recaptcha->verifyResponse($captchaResponse, $_SERVER['REMOTE_ADDR'])) {
$e->isSpam = true;
$e->handled = true;
if ($this->settings->recaptcha && $disableRecaptcha != true) {
$recaptcha = $this->contactFormExtensionsService->getRecaptcha();
$captchaResponse = Craft::$app->request->getParam('g-recaptcha-response');

return;
if (!$recaptcha->verifyResponse($captchaResponse, $_SERVER['REMOTE_ADDR'])) {
$e->isSpam = true;
$e->handled = true;
}
}
}

Expand All @@ -203,7 +203,11 @@ private function _registerContactFormEventListeners(): void

$submission = $e->submission;
if ($this->settings->enableDatabase && $disableSaveSubmission != true) {
$this->contactFormExtensionsService->saveSubmission($submission);
$this->contactFormExtensionsService->saveSubmission($submission, $e->isSpam);
}

if ($e->isSpam) {
return;
}

// Override toEmail setting
Expand Down
4 changes: 2 additions & 2 deletions src/base/Routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ trait Routes
public function _registerCpRoutes(): void
{
Event::on(UrlManager::class, UrlManager::EVENT_REGISTER_CP_URL_RULES, function (RegisterUrlRulesEvent $event) {
$event->rules['contact-form-extensions/submissions/<submissionId:\d+>'] = 'contact-form-extensions/submissions/show-submission';
$event->rules['contact-form-extensions/submissions/<submissionId:\d+>/<siteHandle:{handle}>'] = 'contact-form-extensions/submissions/show-submission';
$event->rules['contact-form-extensions/submissions/<elementId:\d+>'] = 'elements/edit';
$event->rules['contact-form-extensions/submissions/<elementId:\d+>/<siteHandle:{handle}>'] = 'elements/edit';
});
}
}
46 changes: 0 additions & 46 deletions src/controllers/SubmissionsController.php

This file was deleted.

41 changes: 41 additions & 0 deletions src/elements/Submission.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,19 @@
use craft\elements\db\ElementQueryInterface;
use craft\helpers\StringHelper;
use craft\helpers\UrlHelper;
use craft\web\CpScreenResponseBehavior;
use hybridinteractive\contactformextensions\ContactFormExtensions;
use hybridinteractive\contactformextensions\elements\db\SubmissionQuery;
use yii\web\Response;

/**
* @method SubmissionQuery find()
*/
class Submission extends Element
{
public const STATUS_IS_SPAM = 'spam';
public const STATUS_IS_NOT_SPAM = 'not-spam';

// Public Properties
// =========================================================================

Expand All @@ -25,6 +34,7 @@ class Submission extends Element
public ?string $fromEmail;
public ?string $subject;
public $message;
public $isSpam;

// Static Methods
// =========================================================================
Expand Down Expand Up @@ -70,6 +80,17 @@ public function getCpEditUrl(): ?string
return UrlHelper::cpUrl('contact-form-extensions/submissions/'.$this->id);
}

public function prepareEditScreen(Response $response, string $containerId): void
{
/** @var CpScreenResponseBehavior $response */
$response->addCrumb('Contact form submissions', '/contact-form-extensions');
$response->title($this->id);
$response->contentTemplate('contact-form-extensions/submissions/_show', [
'submission' => $this,
'messageObject' => ContactFormExtensions::$plugin->contactFormExtensionsService->utf8AllTheThings(json_decode($this->message, true)),
]);
}

/**
* @inheritDoc
*/
Expand Down Expand Up @@ -116,6 +137,24 @@ protected static function defineActions(string $source = null): array
return $actions;
}

public static function hasStatuses(): bool
{
return true;
}

public function getStatus(): ?string
{
return $this->isSpam ? self::STATUS_IS_SPAM : self::STATUS_IS_NOT_SPAM;
}

public static function statuses(): array
{
return [
self::STATUS_IS_SPAM => ['label' => Craft::t('contact-form-extensions', 'Spam'), 'color' => 'red'],
self::STATUS_IS_NOT_SPAM => ['label' => Craft::t('contact-form-extensions', 'Not spam'), 'color' => 'green'],
];
}

/**
* @inheritDoc
*/
Expand Down Expand Up @@ -198,6 +237,7 @@ public function afterSave(bool $isNew): void
'fromName' => $this->fromName,
'fromEmail' => $this->fromEmail,
'message' => $this->message,
'isSpam' => $this->isSpam,
])
->execute();
} else {
Expand All @@ -208,6 +248,7 @@ public function afterSave(bool $isNew): void
'fromName' => $this->fromName,
'fromEmail' => $this->fromEmail,
'message' => $this->message,
'isSpam' => $this->isSpam,
], ['id' => $this->id])
->execute();
}
Expand Down
22 changes: 22 additions & 0 deletions src/elements/db/SubmissionQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use craft\elements\db\ElementQuery;
use craft\helpers\Db;
use hybridinteractive\contactformextensions\elements\Submission;

class SubmissionQuery extends ElementQuery
{
Expand All @@ -12,6 +13,7 @@ class SubmissionQuery extends ElementQuery
public $fromName;
public $fromEmail;
public $message;
public $isSpam;

public function form($value)
{
Expand Down Expand Up @@ -48,6 +50,25 @@ public function message($value)
return $this;
}

public function isSpam($value)
{
$this->isSpam = $value;

return $this;
}

protected function statusCondition(string $status): mixed
{
switch ($status) {
case Submission::STATUS_IS_SPAM:
return ['isSpam' => true];
case Submission::STATUS_IS_NOT_SPAM:
return ['isSpam' => false];
default:
return parent::statusCondition($status);
}
}

protected function beforePrepare(): bool
{
// join in the products table
Expand All @@ -60,6 +81,7 @@ protected function beforePrepare(): bool
'contactform_submissions.fromName',
'contactform_submissions.fromEmail',
'contactform_submissions.message',
'contactform_submissions.isSpam',
]);

if ($this->form) {
Expand Down
33 changes: 33 additions & 0 deletions src/migrations/m240110_190349_add_isSpam_column.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace hybridinteractive\contactformextensions\migrations;

use craft\db\Migration;

/**
* m240110_190349_add_isSpam_column migration.
*/
class m240110_190349_add_isSpam_column extends Migration
{
public const COLUMN_IS_SPAM = 'isSpam';

/**
* @inheritdoc
*/
public function safeUp(): bool
{
$this->addColumn('{{%contactform_submissions}}', self::COLUMN_IS_SPAM, $this->boolean()->defaultValue(false));

return true;
}

/**
* @inheritdoc
*/
public function safeDown(): bool
{
$this->dropColumn('{{%contactform_submissions}}', self::COLUMN_IS_SPAM);

return false;
}
}
3 changes: 2 additions & 1 deletion src/services/ContactFormExtensionsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,14 @@ class ContactFormExtensionsService extends Component
*
* @return mixed
*/
public function saveSubmission(CraftContactFormSubmission $submission)
public function saveSubmission(CraftContactFormSubmission $submission, bool $isSpam = false)
{
$contactFormSubmission = new Submission();
$contactFormSubmission->form = $submission->message['formName'] ?? 'contact';
$contactFormSubmission->fromName = $submission->fromName;
$contactFormSubmission->fromEmail = $submission->fromEmail;
$contactFormSubmission->subject = $submission->subject;
$contactFormSubmission->isSpam = $isSpam;

if (!is_array($submission->message)) {
$submission->message = ['message' => $this->utf8Value($submission->message)];
Expand Down
Loading