-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathmain_oauth.php
More file actions
188 lines (134 loc) · 4.52 KB
/
Copy pathmain_oauth.php
File metadata and controls
188 lines (134 loc) · 4.52 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
require("simpleCache.php");
include('GoogleSpreadsheetAPI.class.php');
function set_google_auth($ini) {
$ga = new GoogleSpreadsheetAPI('service');
$ga->auth->setClientId($ini['google_client_id']);
$ga->auth->setEmail($ini['google_email']);
$ga->auth->setPrivateKey($ini['google_private_key']);
$auth = $ga->auth->getAccessToken();
// Try to get the AccessToken
if ($auth['http_code'] == 200) {
$accessToken = $auth['access_token'];
$tokenExpires = $auth['expires_in'];
$tokenCreated = time();
} else {
// error...
die('AUTH FAILED');
}
$ga->setAccessToken($accessToken);
return $ga;
}
//** fetch the ini config variables **/
$ini = parse_ini_file("app_config.ini");
$timeout = $ini['cachettl'];
$sc = new SimpleCache();
$store_config = $sc->getCache('store_config_cache');
if($store_config == null) {
if (!isset($ga)) {
$ga = set_google_auth($ini);
}
$params = array('gid'=>$ini['google_tab_store_config'], 'format'=> 'csv');
$_config = $ga->getWorksheet($ini['google_worksheet_url'],$params);
$store_config = array();
$_config = (array) $_config;
foreach ($_config as $arr) {
$store_config[$arr['key']] = str_replace("'", "", $arr['value']);
}
$sc->setCache('store_config_cache',$store_config);
$store_config = $sc->getCache('store_config_cache');
}
$products = $sc->getCache("products_cache");
if($products == null) {
if (!isset($ga)) {
$ga = set_google_auth($ini);
}
$params = array('gid'=>$ini['google_tab_product_catalog'], 'format'=> 'csv');
$temp_products = $ga->getWorksheet($ini['google_worksheet_url'],$params);
$products = array();
foreach ($temp_products as $product) {
/** Parent, and Simple with no parentsku **/
if ($product['producttype'] == 'Parent' || ($product['producttype'] == 'Simple' && $product['parentsku'] == '' )) {
$products[] = $product;
}
}
$sc->setCache("products_cache",$products);
$products = $sc->getCache("products_cache"); //must call this line here, keep
}
$language = get_language();
$translations = $sc->getCache("translations_cache_".$language);
if($translations == null) {
if (!isset($ga)) {
$ga = set_google_auth($ini);
}
if ($language == 'es') {
$params = array('gid'=>$ini['google_tab_es'], 'format'=> 'csv');
$_trans = $ga->getWorksheet($ini['google_worksheet_url'],$params);
}
else if ($language == 'zh') {
$params = array('gid'=>$ini['google_tab_zh'], 'format'=> 'csv');
$_trans = $ga->getWorksheet($ini['google_worksheet_url'],$params);
}
else {
$params = array('gid'=>$ini['google_tab_en'], 'format'=> 'csv');
$_trans = $ga->getWorksheet($ini['google_worksheet_url'],$params);
}
$translations = array();
foreach ($_trans as $arr) {
$translations[$arr['key']] = $arr['value'];
}
// Write products to Cache in 10 minutes with same keyword
$sc->setCache("translations_cache_".$language,$translations);
$translations = $sc->getCache("translations_cache_".$language);
}
$types = $sc->getCache("types_cache");
if($types == null) {
if (!isset($ga)) {
$ga = set_google_auth($ini);
}
$params = array('gid'=>$ini['google_tab_types'], 'format'=> 'csv');
$types = $ga->getWorksheet($ini['google_worksheet_url'],$params);
$categories = array();
$product_types = array();
$colors = array();
foreach ($types as $data) {
$categories[$data['categories']] = _l($data['categories']);
if (isset($data['producttype']) && $data['producttype'] != "") {
$product_types[] = $data['producttype'];
}
if (isset($data['colors']) && $data['colors'] != "") {
$colors[] = $data['colors'];
}
}
$types['categories'] = $categories;
$types['product_types'] = $product_types;
$types['colors'] = $colors;
$sc->setCache("types_cache",$types );
$types = $sc->getCache("types_cache");
}
function _m($key) {
global $store_config;
if (isset($store_config->{$key})) {
return $store_config->{$key};
} else {
return $key;
}
}
function _l($key) {
global $translations;
if (isset($translations->{$key})) {
return $translations->{$key};
} else {
return $key;
}
}
function get_language() {
$lang = 'en';
$domain = explode('-',$_SERVER['SERVER_NAME']);
if (isset($domain[1])) {
$parts = explode('.',$domain[1]);
$lang = $parts[0];
}
return $lang;
}
?>