Skip to content

Commit

Permalink
Fixes (#114)
Browse files Browse the repository at this point in the history
* Fix not adding subs to maijlet with newsletter form

* Fix for configuration page breaks

* Changed logos

* Config to default after disabling and uninstalling module

* Disable if uninstall

* Disable if uninstall

* Missing space

* Fixed unsub and sub, added select for contact lists, added manual sync button

Co-authored-by: Pawel Wasiluk <pwasi@softserveinc.com>
  • Loading branch information
Pavlico and Pawel Wasiluk committed Dec 9, 2021
1 parent 8ad7772 commit 7792952
Show file tree
Hide file tree
Showing 10 changed files with 184 additions and 14 deletions.
9 changes: 9 additions & 0 deletions classes/hooks/synchronization/Segment.php
Expand Up @@ -358,6 +358,15 @@ private function getExistingMailjetListId($filterId)
return $listId;
}

/**
*
* @return array
*/
public function getAllLists()
{
return $this->getApiOverlay()->getContactsLists();
}

/**
*
* @param string $filterId
Expand Down
3 changes: 3 additions & 0 deletions classes/hooks/synchronization/SynchronizationAbstract.php
Expand Up @@ -99,6 +99,9 @@ public function getDbInstance()
*/
public function getAlreadyCreatedMasterListId()
{
if (Configuration::get('contact_list')) {
$this->masterListId = Configuration::get('contact_list');
}
if (!$this->masterListId) {
$lists = $this->getApiOverlay()->getContactsLists();

Expand Down
Binary file modified logo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
170 changes: 159 additions & 11 deletions mailjet.php
Expand Up @@ -57,7 +57,7 @@

class Mailjet extends Module
{

private const DEFAULT_MAIL_OPTION = 1;
public $errors_list = array();
public $page_name;
public $module_access = array();
Expand Down Expand Up @@ -264,6 +264,9 @@ public function install()
&& $this->registerHook('registerGDPRConsent')
&& $this->registerHook('updateOrderStatus')
&& $this->registerHook('updateQuantity')
&& $this->registerHook('actionNewsletterRegistrationAfter')
&& $this->registerHook('actionNewsletterRegistrationBefore')
&& $this->registerHook('actionControllerInitBefore')
);
}

Expand Down Expand Up @@ -341,6 +344,7 @@ public function uninstall()
Configuration::deleteByName('MAILJET');
Configuration::deleteByName('MJ_TRIGGERS');
Configuration::deleteByName('MJ_ALLEMAILS');
$this->configToDefault();
unset($this->account);
unset($this->triggers);

Expand Down Expand Up @@ -409,8 +413,36 @@ public function hookNewOrder($params)
}
}

public function hookActionControllerInitBefore()
{
if (Tools::isSubmit('subscribedmerged') && $this->isAccountSet()) {
$id = Tools::getValue('id');
if (preg_match('/(^N)/', $id)) {
$id = (int) Tools::substr($id, 1);
if (version_compare(_PS_VERSION_, '1.7', '<')) {
$sql = 'SELECT `email` FROM '._DB_PREFIX_.'newsletter WHERE `id` = \''. pSQL($id).'\'';
} else {
$sql = 'SELECT `email` FROM '._DB_PREFIX_.'emailsubscription WHERE `id` = \''. pSQL($id).'\'';
}
if ($subscriber = Db::getInstance()->getRow($sql)) {
if (!empty($subscriber['email'])) {
$initialSynchronization = new HooksSynchronizationSingleUser(MailjetTemplate::getApi());
$masterListId = $initialSynchronization->getAlreadyCreatedMasterListId();
$initialSynchronization->unsubscribe($subscriber['email'], $masterListId);
}
}
}
}

}

public function hookBackOfficeHeader()
{
if (Tools::getIsset('sync_list')) {
$sync = new HooksSynchronizationInitial(MailjetTemplate::getApi());
$listId = $sync->getAlreadyCreatedMasterListId();
$sync->synchronizeSubscribers($listId);
}
// Process subscribers changed by Admin through the BackOffice
if (Tools::isSubmit('subscribedmerged') && $this->isAccountSet()) {
$id = Tools::getValue('id');
Expand Down Expand Up @@ -630,17 +662,12 @@ public function hookActionObjectCustomerUpdateAfter($params)
{
$customer = $params['object'];
$initialSynchronization = new HooksSynchronizationSingleUser(MailjetTemplate::getApi());

try {
$this->checkAutoAssignment($customer->id);
if ($customer->active == 0) {
if ($customer->deleted == 1) {
$initialSynchronization->removeFromAllLists($customer->email);
} elseif ($customer->active == 1 && $customer->newsletter == 0) {
} elseif ($customer->newsletter == 0) {
$initialSynchronization->unsubscribe($customer->email);

// Remove a customer from the MasterList When he unsubscribe via profil
$masterListId = $initialSynchronization->getAlreadyCreatedMasterListId();
$initialSynchronization->remove($customer->email, $masterListId);
} elseif ($customer->active == 1 && $customer->newsletter == 1) {
$initialSynchronization->subscribe($customer);
}
Expand Down Expand Up @@ -740,6 +767,40 @@ public function hookCreateAccount($params)
}
}

/**
* @param array $params
* @return void
*/
public function hookActionNewsletterRegistrationAfter(array $params)
{
if (!$params['error'] && $params['action'] == '0') {
try {
$initialSynchronization = new HooksSynchronizationSingleUser(MailjetTemplate::getApi());
$masterListId = $initialSynchronization->getAlreadyCreatedMasterListId();
$initialSynchronization->subscribe($params['email'], $masterListId);
} catch (Exception $e) {
$this->errors_list[] = $this->l($e->getMessage());
}
}
}

/**
* @param array $params
* @return void
*/
public function hookActionNewsletterRegistrationBefore(array $params)
{
if (!$params['error'] && $params['action'] == '1') {
try {
$initialSynchronization = new HooksSynchronizationSingleUser(MailjetTemplate::getApi());
$masterListId = $initialSynchronization->getAlreadyCreatedMasterListId();
$initialSynchronization->remove($params['email'], $masterListId);
} catch (Exception $e) {
$this->errors_list[] = $this->l($e->getMessage());
}
}
}

/**
*
* @author atanas
Expand Down Expand Up @@ -1188,6 +1249,24 @@ public function postProcess()
}
}

public function disable($force_all = false)
{
$this->configToDefault();
return parent::disable($force_all);

}

public function enable($force_all = false)
{
$this->activateAllEmailMailjet();
return parent::enable($force_all);
}

public function configToDefault()
{
Configuration::updateValue('PS_MAIL_METHOD', self::DEFAULT_MAIL_OPTION);
}

public function activateAllEmailMailjet()
{
Configuration::updateValue('PS_MAIL_SERVER', $this->mj_mail_server);
Expand Down Expand Up @@ -1254,7 +1333,17 @@ public function getContent()
$this->postProcess();

$this->context->smarty->assign(array('is_landing' => false));
$output = '';
if (Tools::isSubmit('submit' . $this->name)) {
$configValue = (string) Tools::getValue('contact_list');

if (empty($configValue) || !Validate::isGenericName($configValue)) {
$output = $this->displayError($this->l('Invalid Configuration value'));
} else {
Configuration::updateValue('contact_list', $configValue);
$output = $this->displayConfirmation($this->l('Settings updated'));
}
}
switch ($this->page_name) {
case 'SETUP_LANDING':
$mt = new MailjetTemplate();
Expand Down Expand Up @@ -1383,21 +1472,80 @@ public function getContent()
$this->checkTokenValidity();
$this->checkPlanValidity();
}

$tab = ['class_name' => 'AdminModules'];
$link = $this->context->link->getTabLink($tab) . '&configure=' . $this->name . '&conf=4&token=' . Tools::getAdminTokenLite('AdminModules') . '&sync_list=true';
$this->context->smarty->assign(array(
'MJ_templates' => $this->mj_template->getTemplates(),
'MJ_iframes' => $this->mj_template->getIframesURL(),
'MJ_errors' => $this->errors_list,
'MJ_page_name' => $this->page_name,
'MJ_template_name' => $this->mj_pages->getTemplateName($this->page_name),
'MJ_local_path' => $this->module_access['dir'],
'MJ_template_tab_name' => $this->mj_pages->getTemplateTabName($this->page_name),
'MJ_authentication' => $this->isAccountSet(),
'MJ_TOKEN_USER' => isset($this->account->{'TOKEN_' . $this->context->employee->id}) ?
$this->account->{'TOKEN_' . $this->context->employee->id} : null,
'MJ_user_plan' => $this->getPlan(),
'MJ_adminmodules_link' => $this->getAdminModuleLink(array()),
'MJ_REQUEST_PAGE_TYPE' => MailJetPages::REQUEST_PAGE_TYPE,
'MJ_sync_url' => $link,
));

return $this->fetchTemplate('/views/templates/admin/', 'configuration');
if ($this->page_name == 'CONTACTS') {
$this->context->smarty->assign([
'MJ_contact_list_form' => '<div class="center_page">' . $this->displayForm() . '</div>'
]);
}
return $output . $this->fetchTemplate('/views/templates/admin/', 'configuration');
}

public function displayForm()
{
$synchronization = new HooksSynchronizationSegment(MailjetTemplate::getApi());
$lists = $synchronization->getAllLists();
$formatedList = [];
foreach ($lists as $l) {
$formatedList[] = [
'name' => explode('idf', $l->Name)[0],
'list_id' => explode('idf', $l->ID)[0]
];
}
$form = [
'form' => [
'legend' => [
'title' => $this->trans('Settings', [], 'Admin.Global'),
'icon' => 'icon-cogs',
],
'input' => [
[
'type' => 'select',
'label' => $this->l('Contact list:'),
'name' => 'contact_list',
'required' => true,
'options' => [
'query' => $formatedList,
'id' => 'list_id',
'name' => 'name'
]
],
],
'submit' => [
'title' => $this->l('Save'),
'class' => 'btn btn-primary',
],
],
];

$helper = new HelperForm();

$helper->table = $this->table;
$helper->name_controller = $this->name;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->currentIndex = AdminController::$currentIndex . '&' . http_build_query(['configure' => $this->name]);
$helper->submit_action = 'submit' . $this->name;
$helper->fields_value['contact_list'] = Configuration::get('contact_list');
$helper->default_form_language = (int) Configuration::get('PS_LANG_DEFAULT');

return $helper->generateForm([$form]);
}

public function displayAccount()
Expand Down
6 changes: 4 additions & 2 deletions views/css/bo.css
Expand Up @@ -386,14 +386,16 @@ table#vsTable .titleImage {
.logo_mailjet_tiny {
background-image:url(../img/MJSmall.png);
width:164px;
height:58px;
background-size: 164px;
height:48px;
margin-top:10px;
margin-right:4px;
}
.logo_mailjet_center {
background-image:url(../img/MJSmall.png);
width:164px;
height:58px;
background-size: 164px;
height:48px;
margin-top: 10px;
margin-left:auto;
margin-right:auto;
Expand Down
Binary file modified views/img/MJSmall.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified views/img/head_logo_tiny.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion views/templates/admin/home.tpl
Expand Up @@ -170,7 +170,9 @@
<a href="http://www.mailjet.com/pricing" target="_blank" class="btn_tab_home"><span></span><span>{l s='Upgrade' mod='mailjet'}</span></a>
{l s='Click here to change/upgrade your current plan.' mod='mailjet'}
</li>

<li>
{include file="$MJ_local_path/views/templates/admin/list_config.tpl"}
</li>
</ul>
</div>
</div>
3 changes: 3 additions & 0 deletions views/templates/admin/iframes.tpl
Expand Up @@ -31,5 +31,8 @@
{/if}
{/if}
{/if}
{if $MJ_page_name == "CONTACTS"}
{$MJ_contact_list_form}
{/if}
<iframe border="0" id="mj_iframe" width="100%" height="1200px" src="{$MJ_iframes.$MJ_page_name|escape:'htmlall':'UTF-8'}{if !empty($MJ_TOKEN_USER)}{/if}">
</iframe>
3 changes: 3 additions & 0 deletions views/templates/admin/list_config.tpl
@@ -0,0 +1,3 @@
<div class="center_page">
<a href="{$MJ_sync_url}" class="btn_tab_home">SYNC LIST WITH PRESTASHOP DATA</a>
</div>

0 comments on commit 7792952

Please sign in to comment.