Skip to content

Commit 26d7235

Browse files
committed
BulkSMS: added country filter
1 parent e3a791d commit 26d7235

File tree

3 files changed

+69
-52
lines changed

3 files changed

+69
-52
lines changed

Constants.php

+28-13
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,38 @@
1515

1616
class Constants
1717
{
18+
const API_KEY = 'SMS77_API_KEY';
19+
const BULK = 'SMS77_BULK';
20+
const BULK_COUNTRIES = 'SMS77_BULK_COUNTRIES';
21+
const FROM = 'SMS77_FROM';
22+
const MSG_ON_DELIVERY = 'SMS77_MSG_ON_DELIVERY';
23+
const MSG_ON_INVOICE = 'SMS77_MSG_ON_INVOICE';
24+
const MSG_ON_PAYMENT = 'SMS77_MSG_ON_PAYMENT';
25+
const MSG_ON_SHIPMENT = 'SMS77_MSG_ON_SHIPMENT';
26+
const ON_DELIVERY = 'SMS77_ON_DELIVERY';
27+
const ON_INVOICE = 'SMS77_ON_INVOICE';
28+
const ON_PAYMENT = 'SMS77_ON_PAYMENT';
29+
const ON_SHIPMENT = 'SMS77_ON_SHIPMENT';
30+
const SIGNATURE = 'SMS77_SIGNATURE';
31+
const SIGNATURE_POSITION = 'SMS77_SIGNATURE_POSITION';
32+
1833
public static $configuration = [
19-
'SMS77_API_KEY' => '',
20-
'SMS77_BULK' => '',
21-
'SMS77_FROM' => '',
22-
'SMS77_MSG_ON_DELIVERY' => false,
23-
'SMS77_MSG_ON_INVOICE' => false,
24-
'SMS77_MSG_ON_PAYMENT' => false,
25-
'SMS77_MSG_ON_SHIPMENT' => false,
26-
'SMS77_ON_DELIVERY' => 'Dear {0} {1}. Your order #{2} has been delivered. Enjoy your goods!',
27-
'SMS77_ON_INVOICE' => 'Dear {0} {1}. An invoice has been generated for your order #{2}.
34+
self::API_KEY => '',
35+
self::BULK => '',
36+
self::FROM => '',
37+
self::MSG_ON_DELIVERY => false,
38+
self::MSG_ON_INVOICE => false,
39+
self::MSG_ON_PAYMENT => false,
40+
self::MSG_ON_SHIPMENT => false,
41+
self::ON_DELIVERY => 'Dear {0} {1}. Your order #{2} has been delivered. Enjoy your goods!',
42+
self::ON_INVOICE => 'Dear {0} {1}. An invoice has been generated for your order #{2}.
2843
Log in to your account in order to have a look at it. Best regards!',
29-
'SMS77_ON_PAYMENT' => 'Dear {0} {1}. A payment has been made for your order #{2}.
44+
self::ON_PAYMENT => 'Dear {0} {1}. A payment has been made for your order #{2}.
3045
Log in to your account for more information. Best regards!',
31-
'SMS77_ON_SHIPMENT' => 'Dear {0} {1}. Your order #{2} has been shipped.
46+
self::ON_SHIPMENT => 'Dear {0} {1}. Your order #{2} has been shipped.
3247
Log in to your customer account for more information. Best regards!',
33-
'SMS77_SIGNATURE' => '',
34-
'SMS77_SIGNATURE_POSITION' => 'append',
48+
self::SIGNATURE => '',
49+
self::SIGNATURE_POSITION => 'append',
3550
];
3651

3752
public static $signature_positions = ['append', 'prepend',];

Form.php

+14-14
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,14 @@ class Form extends HelperForm
1818
public function __construct($name)
1919
{
2020
parent::__construct();
21-
2221
$defaultLang = (int)Configuration::get('PS_LANG_DEFAULT');
2322
$this->allow_employee_form_lang = $defaultLang;
2423
$this->currentIndex = AdminController::$currentIndex . "&configure=$name";
2524
$this->default_form_language = $defaultLang;
26-
27-
$configuration = Configuration::getMultiple(array_keys(Constants::$configuration));
28-
29-
foreach ($configuration as $k => $v) {
30-
$configuration["config[$k]"] = $v;
31-
32-
unset($configuration[$k]);
25+
foreach (Configuration::getMultiple(array_keys(Constants::$configuration)) as $k => $v) {
26+
$this->fields_value["config[$k]"] = $v;
3327
}
3428

35-
$this->fields_value = $configuration;
36-
3729
$this->fields_form = [
3830
[
3931
'form' => [
@@ -119,6 +111,18 @@ public function __construct($name)
119111
];
120112
}, Constants::$signature_positions),
121113
],
114+
[
115+
'label' => $this->l('Countries'),
116+
'multiple' => true,
117+
'name' => 'config[SMS77_BULK_COUNTRIES]',
118+
'options' => [
119+
'query' => Country::getCountries($this->context->language->id),
120+
'id' => 'id_country',
121+
'name' => 'name'
122+
],
123+
'tab' => 'bulk',
124+
'type' => 'select',
125+
],
122126
$this->makeTextarea(
123127
'BULK',
124128
'Send out any message to all of your customers.',
@@ -137,12 +141,9 @@ public function __construct($name)
137141
$this->name = $name;
138142
$this->name_controller = $name;
139143
$this->title = $name;
140-
141144
$this->token = Tools::getAdminTokenLite('AdminModules');
142-
143145
$this->show_toolbar = true;
144146
$this->submit_action = "submit$name";
145-
146147
$this->toolbar_btn = [
147148
'save' =>
148149
[
@@ -155,7 +156,6 @@ public function __construct($name)
155156
'desc' => $this->l('Back to list'),
156157
],
157158
];
158-
159159
$this->toolbar_scroll = true;
160160
}
161161

sms77.php

+27-25
Original file line numberDiff line numberDiff line change
@@ -13,41 +13,40 @@
1313
* @license LICENSE
1414
*/
1515

16+
use libphonenumber\NumberParseException;
1617
use libphonenumber\PhoneNumberUtil;
1718

1819
require_once dirname(__FILE__) . "/Constants.php";
20+
require_once dirname(__FILE__) . "/Form.php";
1921

22+
/**
23+
* @property PhoneNumberUtil phoneNumberUtil
24+
*/
2025
class Sms77 extends Module
2126
{
2227
protected $config;
2328

2429
public function __construct()
2530
{
2631
$this->config = Constants::$configuration;
27-
2832
$this->name = 'sms77';
2933
$this->version = '1.4.0';
3034
$this->author = 'sms77 e.K.';
3135
$this->need_instance = 0;
3236
$this->module_key = '7c33461cc60fc57e9746c6d288b6487e';
33-
3437
parent::__construct();
3538

3639
$this->__moduleDir = dirname(__FILE__);
3740
$this->bootstrap = true;
3841
$this->displayName = 'sms77';
39-
4042
$this->description =
4143
$this->l('sms77.io module to programmatically send text messages.');
42-
4344
$this->tab = 'advertising_marketing';
4445
$this->ps_versions_compliancy = [
4546
'min' => '1.6',
4647
'max' => _PS_VERSION_,
4748
];
48-
4949
$this->config['SMS77_FROM'] = Configuration::get('PS_SHOP_NAME'); // defaults to the shop name
50-
5150
$this->phoneNumberUtil = PhoneNumberUtil::getInstance();
5251
}
5352

@@ -64,26 +63,31 @@ public function getContent()
6463
{
6564
$output = null;
6665

66+
$missingApiKey = function() use($output) {
67+
return $this->displayError(
68+
$this->l('An API key is required in order to send SMS. Get yours at http://sms77.io.')
69+
);
70+
};
71+
6772
if (Tools::isSubmit('submit' . $this->name)) {
68-
foreach (Tools::getValue('config') as $k => $v) {
73+
$config = Tools::getValue('config');
74+
75+
foreach ($config as $k => $v) {
6976
if ('SMS77_API_KEY' === $k && 0 === Tools::strlen($v)) {
70-
$output .=
71-
$this->displayError(
72-
$this->l('An API key is required in order to send SMS. Get yours at http://sms77.io.')
73-
);
77+
$output .= $missingApiKey(); //TODO add back
7478
}
7579

7680
if ('SMS77_BULK' === $k && Tools::strlen($v)) {
7781
if (0 === Tools::strlen($this->getSetting('API_KEY'))) {
78-
$output .= $this->displayError(
79-
$this->l('An API key is required in order to send SMS. Get yours at http://sms77.io.')
80-
);
82+
$output .= $missingApiKey();
8183
} else {
84+
$where = "q.active = 1 AND q.deleted = 0 AND q.id_customer != 0 AND q.phone_mobile<>'0000000000'";
85+
if (isset($config['SMS77_BULK_COUNTRIES'])) {
86+
$countries = implode(',', $config['SMS77_BULK_COUNTRIES']);
87+
$where .= " AND q.id_country IN ($countries)";
88+
}
8289
$addresses = self::dbQuery(
83-
'id_country, id_customer, phone, phone_mobile',
84-
'address',
85-
"q.active = 1 AND q.deleted = 0 AND q.id_customer != 0 AND q.phone_mobile<>'0000000000'"
86-
);
90+
'id_country, id_customer, phone, phone_mobile','address', $where);
8791

8892
$merged = array_map(static function ($address) {
8993
$customer = self::dbQuery(
@@ -94,9 +98,7 @@ public function getContent()
9498
return $address + array_shift($customer);
9599
}, $addresses);
96100

97-
$phoneNumberUtil = $this->phoneNumberUtil;
98-
99-
$valids = array_filter($merged, function ($d) use ($phoneNumberUtil) {
101+
$valids = array_filter($merged, function ($d) {
100102
$numbers = [];
101103
if (isset($d['phone'])) {
102104
$numbers[] = $d['phone'];
@@ -105,16 +107,16 @@ public function getContent()
105107
$numbers[] = $d['phone_mobile'];
106108
}
107109

108-
$numbers = array_filter($numbers, function ($number) use ($d, $phoneNumberUtil) {
110+
$numbers = array_filter($numbers, function ($number) use ($d) {
109111
try {
110112
$isoCode = self::dbQuery(
111113
'iso_code',
112114
'country',
113115
'q.id_country = ' . $d['id_country']
114116
);
115117
$isoCode = array_shift($isoCode)['iso_code'];
116-
$numberProto = $phoneNumberUtil->parse($number, $isoCode);
117-
return $phoneNumberUtil->isValidNumber($numberProto);
118+
$numberProto = $this->phoneNumberUtil->parse($number, $isoCode);
119+
return $this->phoneNumberUtil->isValidNumber($numberProto);
118120
} catch (NumberParseException $e) {
119121
return false;
120122
}
@@ -141,7 +143,7 @@ public function getContent()
141143
$this->validateAndSend($k, implode(',', array_unique($phoneNumbers)));
142144
}
143145
}
144-
} else {
146+
} elseif (!in_array($k, [Constants::BULK_COUNTRIES])) {
145147
Configuration::updateValue($k, $v);
146148
}
147149
}

0 commit comments

Comments
 (0)