Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manage newsletter subscription in a modern ajax way #48

Merged
merged 7 commits into from Apr 10, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion config.xml
Expand Up @@ -2,7 +2,7 @@
<module>
<name>ps_emailsubscription</name>
<displayName><![CDATA[E-mail subscription form]]></displayName>
<version><![CDATA[2.5.1]]></version>
<version><![CDATA[2.5.2]]></version>
<description><![CDATA[Adds a block for newsletter subscription.]]></description>
<author><![CDATA[PrestaShop]]></author>
<confirmUninstall><![CDATA[Are you sure that you want to delete all of your contacts?]]></confirmUninstall>
Expand Down
9 changes: 6 additions & 3 deletions controllers/front/subscription.php
Expand Up @@ -36,12 +36,11 @@ class Ps_EmailsubscriptionSubscriptionModuleFrontController extends ModuleFrontC
*/
public function postProcess()
{

$this->variables['value'] = Tools::getValue('email', '');
$this->variables['msg'] = '';
$this->variables['conditions'] = Configuration::get('NW_CONDITIONS', $this->context->language->id);

if (Tools::isSubmit('submitNewsletter')) {
if (Tools::isSubmit('submitNewsletter') || $this->ajax) {
$this->module->newsletterRegistration();
if ($this->module->error) {
$this->variables['msg'] = $this->module->error;
Expand All @@ -50,8 +49,12 @@ public function postProcess()
$this->variables['msg'] = $this->module->valid;
$this->variables['nw_error'] = false;
}
}

if ($this->ajax) {
header('Content-Type: application/json');
$this->ajaxDie(json_encode($this->variables));
}
}
}

/**
Expand Down
23 changes: 16 additions & 7 deletions ps_emailsubscription.php
@@ -1,6 +1,6 @@
<?php
/*
* 2007-2017 PrestaShop
/**
* 2007-2020 PrestaShop
*
* NOTICE OF LICENSE
*
Expand All @@ -19,7 +19,7 @@
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2017 PrestaShop SA
* @copyright 2007-2020 PrestaShop SA
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
Expand Down Expand Up @@ -68,7 +68,7 @@ public function __construct(EntityManager $entity_manager)

$this->entity_manager = $entity_manager;

$this->version = '2.5.1';
$this->version = '2.5.2';
$this->author = 'PrestaShop';
$this->error = false;
$this->valid = false;
Expand Down Expand Up @@ -100,10 +100,10 @@ public function __construct(EntityManager $entity_manager)

public function install()
{
if (
!parent::install()
if (!parent::install()
|| !$this->registerHook(
array(
'actionFrontControllerSetMedia',
'displayFooterBefore',
'actionCustomerAccountAdd',
'additionalCustomerFormFields',
Expand Down Expand Up @@ -207,7 +207,7 @@ public function getContent()
$id = Tools::getValue('id');

if (preg_match('/(^N)/', $id)) {
$id = (int) substr($id, 1);
$id = (int) Tools::substr($id, 1);
$sql = 'UPDATE ' . _DB_PREFIX_ . 'emailsubscription SET active = 0 WHERE id = ' . $id;
Db::getInstance()->execute($sql);
} else {
Expand Down Expand Up @@ -862,6 +862,15 @@ public function getWidgetVariables($hookName = null, array $configuration = [])
return $variables;
}

public function hookActionFrontControllerSetMedia()
{
Media::addJsDef([
'psemailsubscription_subscription' => $this->context->link->getModuleLink($this->name, 'subscription', [], true),
]);

$this->context->controller->registerJavascript('modules-psemailsubscription', 'modules/' . $this->name . '/views/js/ps_emailsubscription.js');
}

/**
* Deletes duplicates email in newsletter table.
*
Expand Down
34 changes: 34 additions & 0 deletions upgrade/install-2.5.2.php
@@ -0,0 +1,34 @@
<?php
/*
* 2007-2020 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2020 PrestaShop SA
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/

if (!defined('_PS_VERSION_')) {
exit;
}

function upgrade_module_2_5_2($object)
{
return $object->registerHook('actionFrontControllerSetMedia');
}
35 changes: 35 additions & 0 deletions views/js/index.php
@@ -0,0 +1,35 @@
<?php
/*
* 2007-2015 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2015 PrestaShop SA
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");

header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

header("Location: ../");
exit;
NeOMakinG marked this conversation as resolved.
Show resolved Hide resolved
55 changes: 55 additions & 0 deletions views/js/ps_emailsubscription.js
@@ -0,0 +1,55 @@
/**
* 2007-2020 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2020 PrestaShop SA
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*
* Don't forget to prefix your containers with your own identifier
* to avoid any conflicts with others containers.
*/

$(document).ready(function () {
$('.block_newsletter form').on("submit", function () {
jf-viguier marked this conversation as resolved.
Show resolved Hide resolved
if (psemailsubscription_subscription !== 'undefined') {
jf-viguier marked this conversation as resolved.
Show resolved Hide resolved
var psemailsubscriptionForm = $(this);
$('.block_newsletter_alert').remove();
$.ajax({
type: 'POST',
dataType: 'JSON',
url: psemailsubscription_subscription,
cache: false,
data: $(this).serialize(),
success: function (data) {
if (data.nw_error) {
psemailsubscriptionForm.prepend('<p class="alert alert-danger block_newsletter_alert">' + data.msg + '</p>');
jf-viguier marked this conversation as resolved.
Show resolved Hide resolved
jf-viguier marked this conversation as resolved.
Show resolved Hide resolved
} else {
psemailsubscriptionForm.prepend('<p class="alert alert-success block_newsletter_alert">' + data.msg + '</p>');
jf-viguier marked this conversation as resolved.
Show resolved Hide resolved

}
},
error: function (err) {
console.log(err);
}
});
return false;
}
});
});
2 changes: 1 addition & 1 deletion views/templates/hook/ps_emailsubscription-column.tpl
Expand Up @@ -23,7 +23,7 @@
* International Registered Trademark & Property of PrestaShop SA
*}

<div class="email_subscription" id="blockEmailSubscription_{$hookName}">
<div class="email_subscription block_newsletter" id="blockEmailSubscription_{$hookName}">
<h4>{l s='Newsletter' d='Modules.Emailsubscription.Shop'}</h4>
{if $msg}
<p class="notification {if $nw_error}notification-error{else}notification-success{/if}">{$msg}</p>
Expand Down
4 changes: 2 additions & 2 deletions views/templates/hook/ps_emailsubscription.tpl
Expand Up @@ -23,13 +23,13 @@
* International Registered Trademark & Property of PrestaShop SA
*}

<div class="email_subscription" id="blockEmailSubscription_{$hookName}">
<div class="email_subscription block_newsletter" id="blockEmailSubscription_{$hookName}">
<h4>{l s='Newsletter' d='Modules.Emailsubscription.Shop'}</h4>
{if $msg}
<p class="notification {if $nw_error}notification-error{else}notification-success{/if}">{$msg}</p>
{/if}
<form action="{$urls.current_url}#blockEmailSubscription_{$hookName}" method="post">
<input type="email" name="email" value="{$value}" placeholder="{l s='Your e-mail' d='Modules.Emailsubscription.Shop'}" required />
<input type="text" name="email" value="{$value}" placeholder="{l s='Your e-mail' d='Modules.Emailsubscription.Shop'}" />
jf-viguier marked this conversation as resolved.
Show resolved Hide resolved
{if $conditions}
<p>{$conditions}</p>
{/if}
Expand Down