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
|
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Plugins\MobileMessaging;
use Piwik\Common;
use Piwik\Intl\Data\Provider\RegionDataProvider;
use Piwik\IP;
use Piwik\Piwik;
use Piwik\Plugin\ControllerAdmin;
use Piwik\Plugins\LanguagesManager\LanguagesManager;
use Piwik\Plugins\MobileMessaging\SMSProvider;
use Piwik\Translation\Translator;
use Piwik\View;
require_once PIWIK_INCLUDE_PATH . '/plugins/UserCountry/functions.php';
class Controller extends ControllerAdmin
{
/**
* @var RegionDataProvider
*/
private $regionDataProvider;
/**
* @var Translator
*/
private $translator;
public function __construct(RegionDataProvider $regionDataProvider, Translator $translator)
{
$this->regionDataProvider = $regionDataProvider;
$this->translator = $translator;
parent::__construct();
}
/**
* Mobile Messaging Settings tab :
* - set delegated management
* - provide & validate SMS API credential
* - add & activate phone numbers
* - check remaining credits
*/
public function index()
{
Piwik::checkUserIsNotAnonymous();
$view = new View('@MobileMessaging/index');
$this->setManageVariables($view);
return $view->render();
}
private function setManageVariables(View $view)
{
$view->isSuperUser = Piwik::hasUserSuperUserAccess();
$mobileMessagingAPI = API::getInstance();
$model = new Model();
$view->delegatedManagement = $mobileMessagingAPI->getDelegatedManagement();
$view->credentialSupplied = $mobileMessagingAPI->areSMSAPICredentialProvided();
$view->accountManagedByCurrentUser = $view->isSuperUser || $view->delegatedManagement;
$view->strHelpAddPhone = $this->translator->translate('MobileMessaging_Settings_PhoneNumbers_HelpAdd', array(
$this->translator->translate('General_Settings'),
$this->translator->translate('MobileMessaging_SettingsMenu')
));
$view->credentialError = null;
$view->creditLeft = 0;
$currentProvider = '';
if ($view->credentialSupplied && $view->accountManagedByCurrentUser) {
$currentProvider = $mobileMessagingAPI->getSMSProvider();
try {
$view->creditLeft = $mobileMessagingAPI->getCreditLeft();
} catch (\Exception $e) {
$view->credentialError = $e->getMessage();
}
}
$view->delegateManagementOptions = array(
array('key' => '0',
'value' => Piwik::translate('General_No'),
'description' => Piwik::translate('General_Default') . '. ' .
Piwik::translate('MobileMessaging_Settings_LetUsersManageAPICredential_No_Help')),
array('key' => '1',
'value' => Piwik::translate('General_Yes'),
'description' => Piwik::translate('MobileMessaging_Settings_LetUsersManageAPICredential_Yes_Help'))
);
$providers = array();
$providerOptions = array();
foreach (SMSProvider::findAvailableSmsProviders() as $provider) {
if (empty($currentProvider)) {
$currentProvider = $provider->getId();
}
$providers[$provider->getId()] = $provider->getDescription();
$providerOptions[$provider->getId()] = $provider->getId();
}
$view->provider = $currentProvider;
$view->smsProviders = $providers;
$view->smsProviderOptions = $providerOptions;
$defaultCountry = Common::getCountry(
LanguagesManager::getLanguageCodeForCurrentUser(),
true,
IP::getIpFromHeader()
);
$view->defaultCallingCode = '';
// construct the list of countries from the lang files
$countries = array(array('key' => '', 'value' => ''));
foreach ($this->regionDataProvider->getCountryList() as $countryCode => $continentCode) {
if (isset(CountryCallingCodes::$countryCallingCodes[$countryCode])) {
if ($countryCode == $defaultCountry) {
$view->defaultCallingCode = CountryCallingCodes::$countryCallingCodes[$countryCode];
}
$countries[] = array(
'key' => CountryCallingCodes::$countryCallingCodes[$countryCode],
'value' => \Piwik\Plugins\UserCountry\countryTranslate($countryCode)
);
}
}
$view->countries = $countries;
$view->phoneNumbers = $model->getPhoneNumbers(Piwik::getCurrentUserLogin());
$this->setBasicVariablesView($view);
}
public function getCredentialFields()
{
$provider = Common::getRequestVar('provider', '');
$credentialFields = array();
foreach (SMSProvider::findAvailableSmsProviders() as $availableSmsProvider) {
if ($availableSmsProvider->getId() == $provider) {
$credentialFields = $availableSmsProvider->getCredentialFields();
break;
}
}
$view = new View('@MobileMessaging/credentials');
$view->credentialfields = $credentialFields;
return $view->render();
}
}
|