forked from vponomarev/ngcms-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasket.php
257 lines (235 loc) · 9.64 KB
/
basket.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<?php
// Protect against hack attempts
if (!defined('NGCMS')) die ('HAL');
LoadPluginLibrary('xfields', 'common');
LoadPluginLibrary('feedback', 'common');
register_htmlvar('js', admin_url . '/plugins/basket/js/basket.js');
//
// Отображение общей информации/остатков в корзине
function plugin_basket_total() {
global $mysql, $twig, $userROW, $template;
// Определяем условия выборки
$filter = array();
if (is_array($userROW)) {
$filter [] = '(user_id = ' . db_squote($userROW['id']) . ')';
}
if (isset($_COOKIE['ngTrackID']) && ($_COOKIE['ngTrackID'] != '')) {
$filter [] = '(cookie = ' . db_squote($_COOKIE['ngTrackID']) . ')';
}
// Считаем итоги
$tCount = 0;
$tPrice = 0;
if (count($filter) && is_array($res = $mysql->record("select count(*) as count, sum(price*count) as price from " . prefix . "_basket where " . join(" or ", $filter), 1))) {
$tCount = $res['count'];
$tPrice = $res['price'];
}
// Готовим переменные
$tVars = array(
'count' => $tCount,
'price' => $tPrice,
);
// Выводим шаблон
$tpath = locatePluginTemplates(array('total'), 'basket', pluginGetVariable('basket', 'localsource'));
$xt = $twig->loadTemplate($tpath['total'] . '/total.tpl');
$template['vars']['plugin_basket'] = $xt->render($tVars);
}
//
// Показать содержимое корзины
function plugin_basket_list() {
global $mysql, $twig, $userROW, $template;
// Определяем условия выборки
$filter = array();
if (is_array($userROW)) {
$filter [] = '(user_id = ' . db_squote($userROW['id']) . ')';
}
if (isset($_COOKIE['ngTrackID']) && ($_COOKIE['ngTrackID'] != '')) {
$filter [] = '(cookie = ' . db_squote($_COOKIE['ngTrackID']) . ')';
}
// Выполняем выборку
$recs = array();
$total = 0;
if (count($filter)) {
foreach ($mysql->select("select * from " . prefix . "_basket where " . join(" or ", $filter), 1) as $rec) {
$total += round($rec['price'] * $rec['count'], 2);
$rec['sum'] = sprintf('%9.2f', round($rec['price'] * $rec['count'], 2));
$rec['xfields'] = unserialize($rec['linked_fld']);
unset($rec['linked_fld']);
$recs [] = $rec;
}
}
$tVars = array(
'recs' => count($recs),
'entries' => $recs,
'total' => sprintf('%9.2f', $total),
'form_url' => generatePluginLink('feedback', null, array(), array('id' => intval(pluginGetVariable('basket', 'feedback_form')))),
);
// Выводим шаблон
$xt = $twig->loadTemplate('plugins/basket/list.tpl');
$template['vars']['mainblock'] = $xt->render($tVars);
}
// Update basket content/counters
function plugin_basket_update() {
global $mysql, $twig, $userROW, $template, $SUPRESS_TEMPLATE_SHOW;
// Определяем условия выборки
$filter = array();
if (is_array($userROW)) {
$filter [] = '(user_id = ' . db_squote($userROW['id']) . ')';
}
if (isset($_COOKIE['ngTrackID']) && ($_COOKIE['ngTrackID'] != '')) {
$filter [] = '(cookie = ' . db_squote($_COOKIE['ngTrackID']) . ')';
}
// Scan POST params
if (count($filter)) {
foreach ($_POST as $k => $v) {
if (preg_match('#^count_(\d+)$#', $k, $m)) {
if (intval($v) < 1) {
$mysql->query("delete from " . prefix . "_basket where (id = " . db_squote($m[1]) . ") and (" . join(" or ", $filter) . ")");
} else {
$mysql->query("update " . prefix . "_basket set count = " . db_squote(intval($v)) . "where (id = " . db_squote($m[1]) . ") and (" . join(" or ", $filter) . ")");
}
}
}
}
// Redirect to basket page
$SUPRESS_TEMPLATE_SHOW = true;
@header("Location: " . generatePluginLink('basket', null, array(), array(), false, true));
}
// XFields filter
if (class_exists('XFieldsFilter') && class_exists('FeedbackFilter')) {
class BasketXFieldsFilter extends XFieldsFilter {
function showTableEntry($newsID, $SQLnews, $rowData, &$rowVars) {
global $DSlist;
// Определяем - работаем ли мы внутри строк таблиц
if (!pluginGetVariable('basket', 'ntable_flag'))
return;
// Работаем. Определяем режим работы - по всем строкам или по условию "поле из xfields не равно нулю"
if (pluginGetVariable('basket', 'ntable_activated')) {
if (!$rowData['xfields_' . pluginGetVariable('basket', 'ntable_xfield')])
return;
}
$rowVars['flags']['basket_allow'] = true;
$rowVars['basket_link'] = generatePluginLink('basket', 'add', array('ds' => $DSlist['#xfields:tdata'], 'id' => $rowData['id']), array(), false, true);
// Строку можно добавлять в корзину
//print "rowData <pre>(".var_export($rowVars, true).")</pre><br/>\n";
}
}
// Feedback filter
class BasketFeedbackFilter extends FeedbackFilter {
// Action executed when form is showed
function onShow($formID, $formStruct, $formData, &$tvars) {
global $userROW, $mysql, $twig;
// Проверяем ID формы - данные корзины отображаются только в конкретной форме
if (pluginGetVariable('basket', 'feedback_form') != $formID)
return;
// Определяем условия выборки
$filter = array();
if (is_array($userROW)) {
$filter [] = '(user_id = ' . db_squote($userROW['id']) . ')';
}
if (isset($_COOKIE['ngTrackID']) && ($_COOKIE['ngTrackID'] != '')) {
$filter [] = '(cookie = ' . db_squote($_COOKIE['ngTrackID']) . ')';
}
// Выполняем выборку
$recs = array();
$total = 0;
if (count($filter)) {
foreach ($mysql->select("select * from " . prefix . "_basket where " . join(" or ", $filter)) as $rec) {
$total += round($rec['price'] * $rec['count'], 2);
$rec['sum'] = sprintf('%9.2f', round($rec['price'] * $rec['count'], 2));
$rec['xfields'] = unserialize($rec['linked_fld']);
unset($rec['linked_fld']);
$recs [] = $rec;
}
}
$tVars = array(
'recs' => count($recs),
'entries' => $recs,
'total' => sprintf('%9.2f', $total),
);
// Выводим шаблон
$xt = $twig->loadTemplate('plugins/basket/lfeedback.tpl');
$tvars['plugin_basket'] .= $xt->render($tVars);
}
function onProcess($formID, $formStruct, $formData, $flagHTML, &$tvars) {
global $userROW, $mysql, $twig;
// Проверяем ID формы - данные корзины отображаются только в конкретной форме
if (pluginGetVariable('basket', 'feedback_form') != $formID)
return 1;
// Определяем условия выборки
$filter = array();
if (is_array($userROW)) {
$filter [] = '(user_id = ' . db_squote($userROW['id']) . ')';
}
if (isset($_COOKIE['ngTrackID']) && ($_COOKIE['ngTrackID'] != '')) {
$filter [] = '(cookie = ' . db_squote($_COOKIE['ngTrackID']) . ')';
}
// Выполняем выборку
$recs = array();
$total = 0;
if (count($filter)) {
foreach ($mysql->select("select * from " . prefix . "_basket where " . join(" or ", $filter)) as $rec) {
$total += round($rec['price'] * $rec['count'], 2);
$rec['sum'] = sprintf('%9.2f', round($rec['price'] * $rec['count'], 2));
$rec['xfields'] = unserialize($rec['linked_fld']);
unset($rec['linked_fld']);
$recs [] = $rec;
}
}
$bVars = array(
'recs' => count($recs),
'entries' => $recs,
'total' => sprintf('%9.2f', $total),
);
// Выводим шаблон
$xt = $twig->loadTemplate('plugins/basket/lfeedback.tpl');
$tvars['plugin_basket'] = $xt->render($bVars);
}
// Action executed when post request is completed
function onProcessNotify($formID) {
global $mysql, $userROW;
// Определяем условия выборки
$filter = array();
if (is_array($userROW)) {
$filter [] = '(user_id = ' . db_squote($userROW['id']) . ')';
}
if (isset($_COOKIE['ngTrackID']) && ($_COOKIE['ngTrackID'] != '')) {
$filter [] = '(cookie = ' . db_squote($_COOKIE['ngTrackID']) . ')';
}
// Выполняем выборку
if (count($filter)) {
$mysql->query("delete from " . prefix . "_basket where " . join(" or ", $filter));
}
}
}
register_plugin_page('basket', '', 'plugin_basket_list', 0);
register_plugin_page('basket', 'update', 'plugin_basket_update', 0);
register_filter('xfields', 'basket', new BasketXFieldsFilter);
register_filter('feedback', 'basket', new BasketFeedbackFilter);
} else {
//print "Basket error: XFields and Feedback plugins must be activated";
}
// Perform replacements while showing news
class BasketNewsFilter extends NewsFilter {
// Show news call :: processor (call after all processing is finished and before show)
function showNews($newsID, $SQLnews, &$tvars, $mode = array()) {
global $DSlist;
// Определяем - работаем ли мы внутри строк таблиц
if (!pluginGetVariable('basket', 'news_flag')) {
$tvars['regx']['#\[basket\](.*?)\[\/basket\]#is'] = '';
return;
}
// Работаем. Определяем режим работы - по всем строкам или по условию "поле из xfields не равно нулю"
if (pluginGetVariable('basket', 'news_activated')) {
if (!$SQLnews['xfields_' . pluginGetVariable('basket', 'news_xfield')]) {
$tvars['regx']['#\[basket\](.*?)\[\/basket\]#is'] = '';
return;
}
}
$tvars['regx']['#\[basket\](.*?)\[\/basket\]#is'] = '$1';
$tvars['vars']['basket_link'] = generatePluginLink('basket', 'add', array('ds' => $DSlist['news'], 'id' => $SQLnews['id']), array(), false, true);
}
}
register_filter('news', 'basket', new BasketNewsFilter);
//
// Вызов обработчика
add_act('index', 'plugin_basket_total');