-
Notifications
You must be signed in to change notification settings - Fork 0
/
allegro.module
156 lines (144 loc) · 3.96 KB
/
allegro.module
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
<?php
/**
* Implements hook_menu().
*/
function allegro_menu() {
$items['admin/allegro'] = array(
'title' => 'Allegro',
'page callback' => 'drupal_get_form',
'access arguments' => array('administer allegro'),
'page arguments' => array('allegro_settings_form'),
'file' => 'allegro.admin.inc',
);
$items['admin/allegro/settings'] = array(
'title' => 'Settings',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
return $items;
}
/**
* Implements hook_permission().
*/
function allegro_permission() {
return array(
'administer allegro' => array(
'title' => t('Administer Allegro'),
),
);
}
/**
* Connect to WebAPI and returns Allegro object extending SoapClient class.
*
* @param bool $login
* Log into the Soap service.
* @param bool $cache
* Whether to reset the internal allegro_client cache.
*
* @return mixed
* Returns Allegro client object when success, false otherwise.
*/
function allegro_client($login = TRUE, $reset = FALSE) {
if (!$reset) {
$allegro = &drupal_static(__FUNCTION__);
}
if (empty($allegro) || $allegro->logged != $login) {
try {
$allegro = new AllegroAPI();
$allegro->userLogin = variable_get('allegro_login');
$allegro->userPassword = variable_get('allegro_password');
$allegro->webapiKey = variable_get('allegro_webapikey');
$allegro->countryCode = variable_get('allegro_country_code');
$allegro->testMode = variable_get('allegro_test_mode');
if ($login) {
$allegro->login();
}
else {
$allegro->init();
}
}
catch (SoapFault $e) {
if (module_exists('dblog') && user_access('access site reports')) {
drupal_set_message(t('An error occurred while connecting to WebAPI service. Check <a href="@watchdog-url">watchdog</a> for more information.', array('@watchdog-url' => url('admin/reports/dblog'))), 'error');
}
else {
drupal_set_message(t('An error occurred while connecting to WebAPI service. Check logs for more information.'), 'error');
}
throw $e;
}
}
return !empty($allegro) ? $allegro : FALSE;
}
/**
* Test connection to Allegro WebAPI service.
*
* @param string $login
* Service login.
* @param string $password
* Service password.
* @param string $webapikey
* API key to service.
* @param int $countryCode
* Country code of targeted market.
* @param bool $testMode
* Use WebAPI sandbox instead of production environment.
*
* @return bool
* TRUE when connection is successful, FALSE otherwise.
*/
function allegro_test_connection($login = NULL, $password = NULL, $webapikey = NULL, $countryCode = NULL, $testMode = FALSE) {
if (!$login) {
$login = variable_get('allegro_login');
}
if (!$password) {
$password = variable_get('allegro_password');
}
if (!$webapikey) {
$webapikey = variable_get('allegro_webapikey');
}
if (!$countryCode) {
$countryCode = variable_get('allegro_country_code');
}
if (!$testMode) {
$testMode = variable_get('allegro_test_mode');
}
if (!$login || !$password || !$webapikey || !$countryCode) {
return FALSE;
}
try {
AllegroAPI::testConnection($login, $password, $webapikey, $countryCode, $testMode);
return TRUE;
}
catch (SoapFault $e) {
return FALSE;
}
}
/**
* Convert object to array.
*
* @param object $object
*
* @return array
*/
function allegro_object2array($object) {
if (!is_object($object) && !is_array($object)) {
return $object;
}
if (is_object($object)) {
$object = get_object_vars($object);
}
return array_map('allegro_object2array', $object);
}
/**
* Load website by country code.
*
* @param int $country_code
* Load website with this country code.
*
* @return mixed
* Website address if found, FALSE otherwise.
*/
function allegro_load_website_by_country_code($country_code) {
$websites = AllegroAPI::getSupportedWebsites();
return isset($websites[$country_code]) ? $websites[$country_code] : FALSE;
}