13
13
* @license LICENSE
14
14
*/
15
15
16
+ use libphonenumber \NumberParseException ;
16
17
use libphonenumber \PhoneNumberUtil ;
17
18
18
19
require_once dirname (__FILE__ ) . "/Constants.php " ;
20
+ require_once dirname (__FILE__ ) . "/Form.php " ;
19
21
22
+ /**
23
+ * @property PhoneNumberUtil phoneNumberUtil
24
+ */
20
25
class Sms77 extends Module
21
26
{
22
27
protected $ config ;
23
28
24
29
public function __construct ()
25
30
{
26
31
$ this ->config = Constants::$ configuration ;
27
-
28
32
$ this ->name = 'sms77 ' ;
29
33
$ this ->version = '1.4.0 ' ;
30
34
$ this ->author = 'sms77 e.K. ' ;
31
35
$ this ->need_instance = 0 ;
32
36
$ this ->module_key = '7c33461cc60fc57e9746c6d288b6487e ' ;
33
-
34
37
parent ::__construct ();
35
38
36
39
$ this ->__moduleDir = dirname (__FILE__ );
37
40
$ this ->bootstrap = true ;
38
41
$ this ->displayName = 'sms77 ' ;
39
-
40
42
$ this ->description =
41
43
$ this ->l ('sms77.io module to programmatically send text messages. ' );
42
-
43
44
$ this ->tab = 'advertising_marketing ' ;
44
45
$ this ->ps_versions_compliancy = [
45
46
'min ' => '1.6 ' ,
46
47
'max ' => _PS_VERSION_ ,
47
48
];
48
-
49
49
$ this ->config ['SMS77_FROM ' ] = Configuration::get ('PS_SHOP_NAME ' ); // defaults to the shop name
50
-
51
50
$ this ->phoneNumberUtil = PhoneNumberUtil::getInstance ();
52
51
}
53
52
@@ -64,26 +63,31 @@ public function getContent()
64
63
{
65
64
$ output = null ;
66
65
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
+
67
72
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 ) {
69
76
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
74
78
}
75
79
76
80
if ('SMS77_BULK ' === $ k && Tools::strlen ($ v )) {
77
81
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 ();
81
83
} 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
+ }
82
89
$ 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 );
87
91
88
92
$ merged = array_map (static function ($ address ) {
89
93
$ customer = self ::dbQuery (
@@ -94,9 +98,7 @@ public function getContent()
94
98
return $ address + array_shift ($ customer );
95
99
}, $ addresses );
96
100
97
- $ phoneNumberUtil = $ this ->phoneNumberUtil ;
98
-
99
- $ valids = array_filter ($ merged , function ($ d ) use ($ phoneNumberUtil ) {
101
+ $ valids = array_filter ($ merged , function ($ d ) {
100
102
$ numbers = [];
101
103
if (isset ($ d ['phone ' ])) {
102
104
$ numbers [] = $ d ['phone ' ];
@@ -105,16 +107,16 @@ public function getContent()
105
107
$ numbers [] = $ d ['phone_mobile ' ];
106
108
}
107
109
108
- $ numbers = array_filter ($ numbers , function ($ number ) use ($ d, $ phoneNumberUtil ) {
110
+ $ numbers = array_filter ($ numbers , function ($ number ) use ($ d ) {
109
111
try {
110
112
$ isoCode = self ::dbQuery (
111
113
'iso_code ' ,
112
114
'country ' ,
113
115
'q.id_country = ' . $ d ['id_country ' ]
114
116
);
115
117
$ 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 );
118
120
} catch (NumberParseException $ e ) {
119
121
return false ;
120
122
}
@@ -141,7 +143,7 @@ public function getContent()
141
143
$ this ->validateAndSend ($ k , implode (', ' , array_unique ($ phoneNumbers )));
142
144
}
143
145
}
144
- } else {
146
+ } elseif (! in_array ( $ k , [Constants:: BULK_COUNTRIES ])) {
145
147
Configuration::updateValue ($ k , $ v );
146
148
}
147
149
}
0 commit comments