-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseven.php
executable file
·166 lines (135 loc) · 5.35 KB
/
seven.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
/**
* NOTICE OF LICENSE
* This file is licenced under the Software License Agreement.
* With the purchase or the installation of the software in your application
* you accept the licence agreement.
* You must not modify, adapt or create derivative works of this source code
* @author seven.io
* @copyright 2019-present seven communications GmbH & Co. KG
* @license LICENSE
*/
if (!defined('_PS_VERSION_')) exit;
$autoloadPath = __DIR__ . '/vendor/autoload.php';
if (file_exists($autoloadPath)) {
require_once $autoloadPath;
}
class Seven extends Module {
public function __construct() {
$this->author = 'seven communications GmbH & Co. KG';
$this->bootstrap = true;
$this->module_key = '597145e6fdfc3580abe1afc34f7f3971';
$this->name = 'seven';
$this->need_instance = 0;
$this->tab = 'emailing';
$this->version = '3.0.0';
parent::__construct();
$this->description = $this->l('seven.io module to programmatically send text messages.');
$this->displayName = 'seven';
$this->ps_version = (bool) version_compare(_PS_VERSION_, '1.7', '>=');
$this->ps_versions_compliancy = [
'min' => '1.7',
'max' => _PS_VERSION_,
];
}
public function getConfig(): array {
$cfg = Constants::CONFIGURATION;
$cfg[Constants::FROM] = Configuration::get('PS_SHOP_NAME');
return $cfg;
}
/**
* @return string
* @throws PrestaShopException
*/
public function getContent() {
$output = '';
if (Tools::isSubmit('submit' . $this->name)) {
foreach (Tools::getValue('config') as $k => $v) {
if (Constants::API_KEY === $k && 0 === Tools::strlen($v))
$output .= $this->displayError(
$this->l('An API key is required in order to send SMS.
Get yours now at https://www.seven.io.')
);
Configuration::updateValue($k, $v);
if (Constants::MSG_ON_INVOICE === $k)
$this->toggleHookRegistration('actionSetInvoice', $v);
if (Constants::MSG_ON_PAYMENT === $k)
$this->toggleHookRegistration('actionPaymentConfirmation', $v);
if (in_array($k, [
Constants::MSG_ON_DELIVERY,
Constants::MSG_ON_REFUND,
Constants::MSG_ON_SHIPMENT,
])) $this->toggleHookRegistration('actionOrderStatusPostUpdate', $v);
}
$output .= $this->displayConfirmation($this->l('Settings updated'));
}
return $output . (new Form($this->name))->generate();
}
/**
* @param string $hook
* @param string $value
*/
private function toggleHookRegistration($hook, $value) {
$isRegistered = $this->isRegisteredInHook($hook);
if ('1' === $value && !$isRegistered) $this->registerHook($hook);
elseif ('0' === $value && $isRegistered) $this->unregisterHook($hook);
}
/**
* @param array $data
* @throws PrestaShopDatabaseException
* @throws \Sms77\Api\Exception\InvalidRequiredArgumentException
*/
public function hookActionSetInvoice(array $data) {
if (!Util::isEventEnabled(Constants::MSG_ON_INVOICE)) return;
SmsUtil::sendEventSMS($data['Order'], Constants::ORDER_ACTION_INVOICE,
['invoice' => $data['OrderInvoice']]);
}
/**
* @param array $data
* @throws PrestaShopDatabaseException
* @throws \Sms77\Api\Exception\InvalidRequiredArgumentException
*/
public function hookActionPaymentConfirmation(array $data) {
if (!Util::isEventEnabled(Constants::MSG_ON_PAYMENT)) return;
SmsUtil::sendEventSMS($data['id_order'], Constants::ORDER_ACTION_PAYMENT);
}
/**
* @param array $data
* @throws PrestaShopDatabaseException
* @throws PrestaShopException
* @throws \Sms77\Api\Exception\InvalidRequiredArgumentException
*/
public function hookActionOrderStatusPostUpdate(array $data) {
/** @var Order|Cart $order */
$order = isset($data['Order']) ? $data['Order'] : $data['cart'];
$order = $order ?: new Order($data['id_order']);
$action = Util::getOrderStateAction($data['newOrderStatus']);
if (!$action || !Util::isEventEnabled('SEVEN_MSG_ON_' . $action)) return;
SmsUtil::sendEventSMS($order, $action);
}
/**
* @return bool
* @throws PrestaShopException
*/
public function install() {
TableWrapper::create();
$tab = new Tab;
$tab->class_name = 'SevenAdmin';
$tab->id_parent = Tab::getIdFromClassName('AdminParentCustomerThreads');
$tab->module = $this->name;
$tab->name[$this->context->language->id] = $this->l('Seven Bulk SMS');
$tab->add();
if (Shop::isFeatureActive()) Shop::setContext(Shop::CONTEXT_ALL);
foreach ($this->getConfig() as $k => $v) Configuration::updateValue($k, $v);
return parent::install();
}
/**
* @return bool
*/
public function uninstall() {
TableWrapper::drop();
foreach (Tab::getCollectionFromModule($this->name) as $tab) $tab->delete();
foreach (array_keys($this->getConfig()) as $k) Configuration::deleteByName($k);
return parent::uninstall();
}
}