2 /************************************************************************
3 * MXChange v0.2.1 Start: 08/22/2004 *
4 * =============== Last change: 08/24/2004 *
6 * -------------------------------------------------------------------- *
7 * File : rallye_functions.php *
8 * -------------------------------------------------------------------- *
9 * Short description : Special functions for rallye extension *
10 * -------------------------------------------------------------------- *
11 * Kurzbeschreibung : Spezielle Funktion fuer rallye-Erweiterung *
12 * -------------------------------------------------------------------- *
15 * $Tag:: 0.2.1-FINAL $ *
17 * Needs to be in all Files and every File needs "svn propset *
18 * svn:keywords Date Revision" (autoprobset!) at least!!!!!! *
19 * -------------------------------------------------------------------- *
20 * Copyright (c) 2003 - 2008 by Roland Haeder *
21 * For more information visit: http://www.mxchange.org *
23 * This program is free software; you can redistribute it and/or modify *
24 * it under the terms of the GNU General Public License as published by *
25 * the Free Software Foundation; either version 2 of the License, or *
26 * (at your option) any later version. *
28 * This program is distributed in the hope that it will be useful, *
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
31 * GNU General Public License for more details. *
33 * You should have received a copy of the GNU General Public License *
34 * along with this program; if not, write to the Free Software *
35 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
37 ************************************************************************/
39 // Some security stuff...
40 if (!defined('__SECURITY')) {
41 $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), '/inc') + 4) . '/security.php';
46 function RALLYE_AUTOSTART_RALLYES ($result) {
47 // Global data array for LOAD_EMAIL_TEMPLATE()
51 // Load all rallyes (usally we have only one rallye active per time!
52 list($id, $title, $start, $end, $notify, $min_users, $min_prices) = SQL_FETCHROW($result);
53 SQL_FREERESULT($result);
56 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_rallye_data` SET notified='Y' WHERE id=%s LIMIT 1",
57 array(bigintval($id)), __FUNCTION__, __LINE__);
59 // Do a snapshot off all user refs
60 $result_user = SQL_QUERY("SELECT userid FROM `{!_MYSQL_PREFIX!}_user_data` WHERE `status`='CONFIRMED' ORDER BY userid", __FUNCTION__, __LINE__);
62 // Transfer all neccessary data to the global $DATA array
63 $DATA['uid_cnt'] = SQL_NUMROWS($result_user);
64 $DATA['start'] = MAKE_DATETIME($start, "2");
65 $DATA['end'] = MAKE_DATETIME($end , "2");
66 $DATA['now_t'] = MAKE_DATETIME(time(), "2");
67 $DATA['title'] = $title;
68 $DATA['id'] = $id; // ID for the rallye details link
70 // Determine min_users and min_prices
71 $DATA['min_users'] = RALLYE_DETERMINE_MIN_USERS($min_users);
72 $DATA['min_prices'] = RALLYE_DETERMINE_MIN_PRICES($min_prices);
75 $prices = RALLYE_ADD_PRICES($id);
77 // Let's begin with the userids...
78 while ($content = SQL_FETCHARRAY($result_user)) {
82 $cnt = RALLYE_GET_REFCOUNT($content['userid']);
83 if (empty($cnt)) $cnt = 0; // Added prevent some unknown troubles... :-?
85 // Check if line is already included...
86 $result_ref = SQL_QUERY_ESC("SELECT id FROM `{!_MYSQL_PREFIX!}_rallye_users` WHERE rallye_id=%s AND userid=%s LIMIT 1",
87 array(bigintval($id), bigintval($content['userid'])), __FUNCTION__, __LINE__);
88 if (SQL_NUMROWS($result_ref) == 0) {
90 SQL_FREERESULT($result_ref);
92 // Add userid and his ref count to table
93 $result_ref = SQL_QUERY_ESC("SELECT DISTINCT SUM(p.points)
94 FROM `{!_MYSQL_PREFIX!}_user_points` AS p
95 LEFT JOIN `{!_MYSQL_PREFIX!}_user_data` AS d
97 WHERE d.`status`='CONFIRMED' AND d.max_mails > 0 AND d.mails_confirmed >= %s AND p.ref_depth=1 AND p.points > 0 AND d.userid=%s",
98 array(getConfig('ref_payout'), bigintval($content['userid'])), __FUNCTION__, __LINE__);
99 list($cpoints) = SQL_FETCHROW($result_ref);
100 SQL_FREERESULT($result_ref);
102 if (empty($cpoints)) $cpoints = "0.00000";
105 SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_rallye_users` (rallye_id, userid, refs, curr_points)
106 VALUES ('%s','%s','%s','%s')",
107 array(bigintval($id), bigintval($content['userid']), bigintval($cnt), $cpoints), __FUNCTION__, __LINE__);
111 // Ignored but for the template required refs (made before start of rallye)
112 $DATA['refs'] = $cnt;
114 // Shall I notify this member?
115 if (($notify == 'Y') && ($un)) {
116 // Load email template and send it to the user
117 $msg = LOAD_EMAIL_TEMPLATE("member_rallye_notify", array('prices' => $prices), $content['userid']);
118 SEND_EMAIL($content['userid'], sprintf(getMessage('RALLYE_MEMBER_NOTIFY'), $title), $msg);
122 // Choose the right admin template
123 $templ = "admin_rallye_no_notify";
124 if ($notify == 'Y') $templ = "admin_rallye_notify";
126 // Send email to admin
127 SEND_ADMIN_NOTIFICATION(sprintf(getMessage('RALLYE_ADMIN_NOTIFY'), $title), $templ, $prices, "0");
130 SQL_FREERESULT($result_user);
133 function RALLYE_ADD_PRICES ($rallye, $mode='email') {
136 case 'email': $mode = "\n"; break;
137 case 'html' : $mode = "<br />\n"; break;
141 $result_prices = SQL_QUERY("SELECT price_level, points, info FROM `{!_MYSQL_PREFIX!}_rallye_prices` WHERE rallye_id='".$rallye."' ORDER BY price_level", __FUNCTION__, __LINE__);
142 if (SQL_NUMROWS($result_prices) > 0) {
144 if ($mode == "\n") $prices = "{--RALLYE_MEMBER_PRICES_ADDED--}:".$mode."------------------------------".$mode;
146 while ($content = SQL_FETCHARRAY($result_prices)) {
147 $prices .= $content['price_level'].getMessage('RALLYE_PRICE').": ";
148 if (!empty($content['info'])) {
149 $prices .= $content['info'];
151 $prices .= $content['points']." {!POINTS!}";
157 SQL_FREERESULT($result_prices);
160 $prices = sprintf(getMessage('RALLYE_MEMBER_NO_PRICES'), $mode);
163 // Add last line for email mode
164 if ($mode == "\n") $prices .= "------------------------------";
171 function RALLYE_ADD_TOPUSERS ($rallye, $default=0) {
172 // First check how many prices are set
173 $result = SQL_QUERY_ESC("SELECT id FROM `{!_MYSQL_PREFIX!}_rallye_prices` WHERE rallye_id=%s ORDER BY price_level",
174 array(bigintval($rallye)), __FUNCTION__, __LINE__);
175 $prices = SQL_NUMROWS($result);
176 SQL_FREERESULT($result);
178 // And load only limited users
179 $result = SQL_QUERY_ESC("SELECT DISTINCT u.userid, u.refs, u.curr_points FROM `{!_MYSQL_PREFIX!}_rallye_users` AS u
180 LEFT JOIN `{!_MYSQL_PREFIX!}_refsystem` AS r
182 WHERE u.rallye_id=%s AND r.counter > 0 ORDER BY u.refs DESC",
183 array(bigintval($rallye)), __FUNCTION__, __LINE__);
192 while ($content = SQL_FETCHARRAY($result)) {
194 $cnt = RALLYE_GET_REFCOUNT($content['userid'], $content['refs']);
197 $result_ref = SQL_QUERY_ESC("SELECT DISTINCT p.points FROM `{!_MYSQL_PREFIX!}_user_points` AS p
198 LEFT JOIN `{!_MYSQL_PREFIX!}_user_data` AS d
200 WHERE d.userid=%s AND d.`status`='CONFIRMED' AND p.ref_depth=1 AND d.max_mails > 0 AND d.mails_confirmed >= %s
201 LIMIT 1", array(bigintval($content['userid']), getConfig('ref_payout')), __FUNCTION__, __LINE__);
202 list($refpoints) = SQL_FETCHROW($result_ref);
203 SQL_FREERESULT($result_ref);
205 if (empty($refpoints)) $refpoints = 0;
207 // Init userid for list
209 // List only users with at least one ref!
210 //* DEBUG: */ echo "*".$cnt."/".$content['userid']."/".$content['curr_points']."/".$refpoints."*<br />\n";
211 if (($cnt > 0) && ($refpoints > $content['curr_points'])) { $_uid = $content['userid']; } else { $cnt = ''; }
213 // Save values to array
214 $DATA['uid'][] = $_uid;
215 $DATA['ref'][] = $cnt;
216 $DATA['cpoints'][] = $content['curr_points'];
220 SQL_FREERESULT($result);
223 array_pk_sort($DATA, array('ref', 'cpoints'), 0, 1, true);
226 $OUT = LOAD_TEMPLATE("guest_rallye_header", true);
228 for ($idx = 0; $idx < $prices; $idx++) {
229 if (empty($DATA['uid'][$idx])) $DATA['uid'][$idx] = '---';
230 if (empty($DATA['ref'][$idx])) $DATA['ref'][$idx] = '---';
233 <td class=\"switch_sw".$SW." bottom2\"> ".($idx+1).".</td>
234 <td align=\"center\" class=\"switch_sw".$SW." bottom2\">";
235 if (($DATA['uid'][$idx] == $default) && ($default > 0)) $OUT .= "<strong>";
236 $OUT .= $DATA['uid'][$idx];
237 if (($DATA['uid'][$idx] == $default) && ($default > 0)) $OUT .= "</strong>";
239 <td align=\"center\" class=\"switch_sw".$SW." bottom2\">";
240 if (($DATA['uid'][$idx] == $default) && ($default > 0)) $OUT .= "<strong>";
241 $OUT .= $DATA['ref'][$idx];
242 if (($DATA['uid'][$idx] == $default) && ($default > 0)) $OUT .= "</strong>";
249 $OUT .= LOAD_TEMPLATE("guest_rallye_footer", true);
251 // And finnally return the output
254 // Run this function only when a new member has confirmed his email address!
255 function RALLYE_AUTOADD_USER ($uid) {
259 // Updated extension?
260 if (GET_EXT_VERSION('rallye') >= '0.2.0') {
261 $add .= ", min_users, min_prices";
264 // Check for an auto-add rallye
265 $result = SQL_QUERY("SELECT id, title, start_time, end_time, send_notify".$add." FROM `{!_MYSQL_PREFIX!}_rallye_data` WHERE is_active='Y' AND notified='Y' AND auto_add_new_user='Y' AND expired='N' LIMIT 1", __FUNCTION__, __LINE__);
266 if (SQL_NUMROWS($result) == 1) {
268 $min_users = 0; $min_prices = 0;
270 if (GET_EXT_VERSION('rallye') >= '0.2.0') {
271 list($id, $title, $start, $end, $notify, $min_users, $min_prices) = SQL_FETCHROW($result);
273 list($id, $title, $start, $end, $notify) = SQL_FETCHROW($result);
277 SQL_FREERESULT($result);
279 // Check if line is already included...
280 $result = SQL_QUERY_ESC("SELECT id FROM `{!_MYSQL_PREFIX!}_rallye_users` WHERE rallye_id=%s AND userid=%s LIMIT 1",
281 array(bigintval($id), bigintval($uid)), __FUNCTION__, __LINE__);
283 // Is this user added?
284 if (SQL_NUMROWS($result) == 0) {
285 // Add userid and his ref count to table
286 SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_rallye_users` (rallye_id, userid, refs)
287 VALUES ('%s','%s','0')",
288 array(bigintval($id), bigintval($uid)), __FUNCTION__, __LINE__);
292 SQL_FREERESULT($result);
294 if ($notify == 'Y') {
295 // Transfer all neccessary data to the global $DATA array
296 $DATA['start'] = MAKE_DATETIME($start, "2");
297 $DATA['end'] = MAKE_DATETIME($end , "2");
298 $DATA['now_t'] = MAKE_DATETIME(time(), "2");
299 $DATA['title'] = $title;
300 $DATA['id'] = $id; // ID for the rallye details link
302 $DATA['refs'] = GET_TOTAL_DATA($uid, "user_data", 'userid', "refid", true);
305 $prices = RALLYE_ADD_PRICES($id);
307 // Determine min_users/prices tring
308 $DATA['min_users'] = RALLYE_DETERMINE_MIN_USERS($min_users);
309 $DATA['min_prices'] = RALLYE_DETERMINE_MIN_PRICES($min_prices);
311 // Send notification to member
312 $msg = LOAD_EMAIL_TEMPLATE("member_rallye_notify", array('prices' => $prices), $uid);
313 SEND_EMAIL($uid, sprintf(getMessage('RALLYE_MEMBER_NOTIFY'), $title), $msg);
319 function RALLYE_EXPIRE_RALLYES ($result) {
323 list($id, $title, $start, $end, $notify, $min_users, $min_prices) = SQL_FETCHROW($result);
324 SQL_FREERESULT($result);
326 // Load users array (!) with assigned prices
327 $prices = RALLYE_LOAD_USERS_ARRAY($id);
330 $DATA = array(); $cnt = 0;
332 $DATA['title'] = $title;
333 $DATA['start'] = MAKE_DATETIME($start, "1");
334 $DATA['end'] = MAKE_DATETIME($end , "1");
335 $DATA['now_t'] = MAKE_DATETIME(time(), "1");
339 foreach($prices['uid'] as $key => $uid) {
341 // active = 1: account is still confirmed
342 // active = 0: account is deleted or locked
343 $result = SQL_QUERY_ESC("SELECT COUNT(userid) AS active
344 FROM `{!_MYSQL_PREFIX!}_user_data`
345 WHERE userid=%s AND `status`='CONFIRMED'
346 LIMIT 1", array(bigintval($uid)), __FUNCTION__, __LINE__);
347 list($active) = SQL_FETCHROW($result);
348 SQL_FREERESULT($result);
350 $prices['active'][$key] = $active;
352 // Allow valid and active users with at least one ref to get points
353 if (($uid > 0) && ($prices['ref'][$key] > 0) && ($active == 1) && ($prices['cpoints'][$key] > 0)) {
358 if (($total < $min_prices) || ($total == 0)) {
359 // Do not end this rallye!
365 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_rallye_data` SET expired='Y' WHERE id=%s LIMIT 1",
366 array(bigintval($id)), __FUNCTION__, __LINE__);
368 // Run array through (by uid is the most important 2nd-level-array)
369 foreach($prices['uid'] as $key => $uid) {
370 // Allow valid and active users with at least one ref to get points
371 if (($uid > 0) && ($prices['ref'][$key] > 0) && ($prices['active'][$key] == 1) && ($prices['cpoints'][$key] > 0)) {
372 // Transfer data to array for the mail template
373 $DATA['level'] = $prices['level'][$key];
374 $DATA['points'] = $prices['points'][$key];
375 $DATA['info'] = $prices['info'][$key];
376 $DATA['ref'] = $prices['ref'][$key];
379 $winnerLevel = 'other';
381 // Determine winner level
382 if ($DATA['level'] == 1) {
384 $winnerLevel = "_gold";
385 } elseif ($DATA['level'] == 2) {
387 $winnerLevel = "_silver";
388 } elseif ($DATA['level'] == 3) {
390 $winnerLevel = "_bronce";
393 if ($DATA['points'] > 0) {
394 // Add points directly to user's account
395 ADD_POINTS_REFSYSTEM("rallye_winner" . $winnerLevel, $uid, $DATA['points'], false, "0", false, "direct");
398 if ($notify == 'Y') {
399 // Prepare infos for the mail template
400 if (!empty($DATA['info'])) {
402 $DATA['infos'] = $DATA['info'];
405 $DATA['infos'] = $DATA['points']." {!POINTS!}";
408 // Add suffix to template name
409 $template = "member_rallye_expired".$winnerLevel;
412 $msg = LOAD_EMAIL_TEMPLATE($template, $DATA, $uid);
413 SEND_EMAIL($uid, sprintf(getMessage('RALLYE_MEMBER_EXPIRED_SUBJ'), $DATA['level']), $msg);
418 $users['uid'][$uid] = $uid;
419 $users['poi'][$uid] = $DATA['infos'];
423 // Select template depending on notfication is switch on / off
424 if ($notify == 'Y') {
425 $templ = "admin_rallye_expired";
426 } elseif (is_array($users['uid'])) {
427 $templ = "admin_rallye_expired_no";
428 $cnt = RALLYE_LOAD_USER_DATA($users);
431 // Send mail to admin
432 SEND_ADMIN_NOTIFICATION(sprintf(getMessage('RALLYE_ADMIN_EXPIRED_SUBJ'), $title), $templ, $cnt, 0);
435 SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_task_system` (status, task_type, subject, text, task_created)
436 VALUES ('NEW','RALLYE_EXPIRED','{--RALLYE_ADMIN_EXPIRED--}: %s','{--RALLYE_ADMIN_EXPIRED_TEXT--}',UNIX_TIMESTAMP())",
437 array($title), __FUNCTION__, __LINE__);
439 // All work done here...
443 function RALLYE_LOAD_USER_DATA ($uids_array) {
445 $uid_string = implode(",", $uids_array['uid']);
447 // Init result string
451 $result = SQL_QUERY_ESC("SELECT userid, gender, surname, family, email FROM `{!_MYSQL_PREFIX!}_user_data` WHERE userid IN(%s) AND `status`='CONFIRMED' ORDER BY userid LIMIT %s",
452 array($uid_string, count($uids_array)), __FUNCTION__, __LINE__);
453 while ($content = SQL_FETCHARRAY($result)) {
454 // Construct the message masked and add it
455 $ret .= sprintf("%s %s %s (%s) - %s\n",
456 TRANSLATE_GENDER($content['gender']),
460 $uids_array['poi'][$content['userid']]
465 return substr($ret, 0, -1);
469 function RALLYE_LOAD_PRICES_ARRAY ($rallye) {
478 $result = SQL_QUERY_ESC("SELECT price_level, points, info FROM `{!_MYSQL_PREFIX!}_rallye_prices` WHERE rallye_id=%s ORDER BY price_level",
479 array(bigintval($rallye)), __FUNCTION__, __LINE__);
482 while ($content = SQL_FETCHARRAY($result)) {
483 $prices['level'][] = $content['price_level'];
484 $prices['points'][] = $content['points'];
485 $prices['info'][] = $content['info'];
489 SQL_FREERESULT($result);
496 function RALLYE_LOAD_USERS_ARRAY ($rallye) {
497 // Fix zero points to 0.00000
498 if (getConfig('ref_payout') == "0") setConfigEntry('ref_payout', "0.00000");
504 'cpoints' => array(),
507 // Load users uid old points earned
508 $result_user = SQL_QUERY_ESC("SELECT userid, refs, curr_points FROM `{!_MYSQL_PREFIX!}_rallye_users` WHERE rallye_id=%s ORDER BY userid",
509 array(bigintval($rallye)), __FUNCTION__, __LINE__);
510 while ($content = SQL_FETCHARRAY($result_user)) {
511 // Load current ref count
512 $cnt = RALLYE_GET_REFCOUNT($content['userid'], $content['refs']);
515 $result_ref = SQL_QUERY_ESC("SELECT DISTINCT SUM(p.points)
516 FROM `{!_MYSQL_PREFIX!}_user_points` AS p
517 LEFT JOIN `{!_MYSQL_PREFIX!}_user_data` AS d
519 WHERE d.`status`='CONFIRMED' AND d.max_mails > 0 AND d.mails_confirmed >= %s AND p.ref_depth=1 AND p.points > 0 AND d.userid=%s",
520 array(getConfig('ref_payout'), bigintval($content['userid'])), __FUNCTION__, __LINE__);
521 list($refpoints) = SQL_FETCHROW($result_ref);
522 SQL_FREERESULT($result_ref);
524 // Fix empty refpoints
525 if (empty($refpoints)) $refpoints = 0;
527 // Store calculated new refs to array
528 $users['uid'][] = $content['userid'];
529 $users['ref'][] = abs($cnt - $content['refs']);
530 $users['cpoints'][] = $refpoints - $content['curr_points'];
534 SQL_FREERESULT($result_user);
536 // Sort array for refs (descending)
537 array_pk_sort($users, array("ref", "cpoints"), 0, 1, true);
539 // Load prices array (!)
540 $prices = RALLYE_LOAD_PRICES_ARRAY($rallye);
542 // Merge users into prices
543 foreach ($prices['level'] as $k => $lvl) {
544 // We only need to check one element in $users, see above while() block
545 if (isset($users['uid'][$k])) {
546 $prices['uid'][$k] = $users['uid'][$k];
547 if (empty($prices['uid'][$k])) $prices['uid'][$k] = '---';
548 $prices['ref'][$k] = $users['ref'][$k];
549 if (empty($prices['ref'][$k])) $prices['ref'][$k] = '---';
550 $prices['cpoints'][$k] = $users['cpoints'][$k];
554 // Return completed array
559 function RALLYE_LIST_WINNERS ($rallye, $default=0) {
560 // First check how many prices are set
561 $result_prices = SQL_QUERY_ESC("SELECT id FROM `{!_MYSQL_PREFIX!}_rallye_prices` WHERE rallye_id=%s ORDER BY price_level",
562 array(bigintval($rallye)), __FUNCTION__, __LINE__);
563 $prices = SQL_NUMROWS($result_prices);
564 SQL_FREERESULT($result_prices);
567 $DATA = RALLYE_LOAD_USERS_ARRAY($rallye);
570 $OUT = LOAD_TEMPLATE("guest_rallye_expired_header", true);
572 for ($idx = 0; $idx < $prices; $idx++) {
574 // active = 1: account is still confirmed
575 // active = 0: account is deleted or locked
576 $result_active = SQL_QUERY_ESC("SELECT COUNT(userid) FROM `{!_MYSQL_PREFIX!}_user_data` WHERE userid=%s AND `status`='CONFIRMED' LIMIT 1",
577 array(bigintval($DATA['uid'][$idx])), __FUNCTION__, __LINE__);
578 list($active) = SQL_FETCHROW($result_active);
579 SQL_FREERESULT($result_active);
581 if (empty($DATA['uid'][$idx])) $DATA['uid'][$idx] = '---';
582 if ((empty($DATA['ref'][$idx])) || ($DATA['ref'][$idx] == 0) || ($active == 0) || ("".round($DATA['cpoints'][$idx])."" == "0") || (empty($DATA['cpoints'][$idx]))) {
583 // Allow valid and active users with at least one ref to get points
584 $DATA['ref'][$idx] = '---';
585 $DATA['uid'][$idx] = '---';
588 if (!empty($DATA['info'][$idx])) {
590 $DATA['infos'][$idx] = $DATA['info'][$idx];
593 $DATA['infos'][$idx] = $DATA['points'][$idx]." {!POINTS!}";
599 <td class=\"switch_sw".$SW." bottom2".$add."\"> ".($idx+1).".</td>
600 <td align=\"center\" class=\"switch_sw".$SW." bottom2".$add."\">";
601 if (($DATA['uid'][$idx] == $default) && ($default > 0)) $OUT .= "<strong>";
602 $OUT .= $DATA['uid'][$idx];
603 if (($DATA['uid'][$idx] == $default) && ($default > 0)) $OUT .= "</strong>";
605 <td align=\"center\" class=\"switch_sw".$SW." bottom2".$add."\">";
606 if (($DATA['uid'][$idx] == $default) && ($default > 0)) $OUT .= "<strong>";
607 $OUT .= $DATA['ref'][$idx];
608 if (($DATA['uid'][$idx] == $default) && ($default > 0)) $OUT .= "</strong>";
610 <td align=\"center\" class=\"switch_sw".$SW." bottom2".$add."\">";
611 if (($DATA['uid'][$idx] == $default) && ($default > 0)) $OUT .= "<strong>";
612 $OUT .= $DATA['infos'][$idx];
613 if (($DATA['uid'][$idx] == $default) && ($default > 0)) $OUT .= "</strong>";
620 $OUT .= LOAD_TEMPLATE("guest_rallye_expired_footer", true);
622 // And finnally return the output
627 function RALLYE_DELETE_EXPIRED_RALLYES () {
630 // Check for expired rallyes
631 $EXPIRE = getConfig('one_day') * 3; // @TODO The hard-coded value...
632 $result_rallye = SQL_QUERY_ESC("SELECT id, title, start_time, end_time
633 FROM `{!_MYSQL_PREFIX!}_rallye_data`
634 WHERE end_time <= (UNIX_TIMESTAMP() - %s) AND expired='Y'",
635 array($EXPIRE), __FUNCTION__, __LINE__);
637 if (SQL_NUMROWS($result_rallye) > 0) {
641 // Expire found rallyes and notify admin
642 while ($content = SQL_FETCHARRAY($result_rallye)) {
643 // Prepare data for mail template
644 $DATA['title'] = $content['title'];
645 $DATA['start'] = MAKE_DATETIME($content['start_time'], "1");
646 $DATA['end'] = MAKE_DATETIME($content['end_time'] , "1");
647 $DATA['now_t'] = MAKE_DATETIME(time(), "1");
649 // Send mail to admin
650 SEND_ADMIN_NOTIFICATION(sprintf(getMessage('RALLYE_ADMIN_PURGED_SUBJ'), $content['title']), "admin_rallye_purged", '', 0);
652 // Purge whole rallye
653 ADD_SQL(SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_rallye_data` WHERE id=%s LIMIT 1",
654 array(bigintval($content['id'])), __FUNCTION__, __LINE__, false));
655 ADD_SQL(SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_rallye_prices` WHERE rallye_id=%s LIMIT 1",
656 array(bigintval($content['id'])), __FUNCTION__, __LINE__, false));
657 ADD_SQL(SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_rallye_users` WHERE rallye_id=%s LIMIT 1",
658 array(bigintval($content['id'])), __FUNCTION__, __LINE__, false));
661 // @TODO Rewrite this to a function
662 ADD_SQL(SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_task_system` (status, task_type, subject, text, task_created)
663 VALUES ('NEW','RALLYE_PURGED','{--RALLYE_ADMIN_PURGED--}: %s','{--RALLYE_ADMIN_PURGED_TEXT--}',UNIX_TIMESTAMP())",
664 array($content['title']), __FUNCTION__, __LINE__, false));
668 runFilterChain('run_sqls');
672 SQL_FREERESULT($result_rallye);
676 function RALLYE_TEMPLATE_SELECTION ($name="template", $default="") {
677 // Check templates directory
678 $OUT = ''; $ral = array();
679 $basePath = sprintf("%stemplates/%s/html/rallye/", constant('PATH'), GET_LANGUAGE());
680 $dir = opendir($basePath) or mxchange_die(sprintf("Cannot open dir %s.", $basePath));
681 while ($read = readdir($dir)) {
682 // If it is no dir (so a file)
683 if (!isDirectory($basePath.$read)) {
684 // Accept only templates matching with rallye_????.tpl.xx
685 if (eregi("^rallye_.*\.tpl", $read)) {
686 $read = substr($read, 7, strpos($read, ".") - 7);
687 // Accept only template names between 1 and 255 chars length
688 if ((strlen($read) < 256) && (!empty($read))) $ral[] = $read;
694 // Do we have found templates which we can link with the new rallye?
695 if (!empty($ral[0])) {
696 // Generate selection box for all found templates
697 // @TODO Rewrite this to our API function
698 $OUT = "<select name=\"".$name."\" size=\"1\" class=\"admin_select\">
699 <option value=\"\">{--SELECT_NONE--}</option>\n";
700 foreach ($ral as $rallye) {
701 $OUT .= " <option value=\"".$rallye."\"";
702 if ($default == $rallye) $OUT .= ' selected="selected"';
703 $OUT .= ">".$rallye."</option>\n";
705 $OUT .= "</select>\n";
707 // No rallye templates found
708 $OUT = getMessage('RALLYE_NO_TEMPLATES_FOUND');
716 function RALLYE_GET_REFCOUNT ($uid, $old=0) {
717 // Check current refs
718 if (GET_EXT_VERSION('cache') >= '0.1.2') {
719 // Get refs from cache
721 foreach ($GLOBALS['cache_array']['refsystem']['userid'] as $id => $uid) {
722 // Do we have a ref for this user?
723 //* DEBUG: */ echo "id={$id},uid={$uid},uid={$uid},old={$old},level={$GLOBALS['cache_array']['refsystem']['level'][$id]}<br />\n";
724 if (($uid == $uid) && ($GLOBALS['cache_array']['refsystem']['level'][$id] == 1)) {
725 //* DEBUG: */ echo "uid matches!<br />\n";
726 foreach ($GLOBALS['cache_array']['ref_depths']['level'] as $level) {
727 if (($level == $GLOBALS['cache_array']['refsystem']['level'][$id]) && ($level == 1)) {
728 // Level does exist so abort here
729 $cnt = $GLOBALS['cache_array']['refsystem']['counter'][$id];
730 //* DEBUG: */ echo "*".$uid."/".$cnt."*<br />";
732 } elseif ($level > 1) {
733 // Not interesting here...
743 //* DEBUG: */ echo "<pre>";
744 //* DEBUG: */ print_r($GLOBALS['cache_array']['refsystem']);
745 //* DEBUG: */ echo "</pre>";
750 incrementConfigEntry('cache_hits');
753 //* DEBUG: */ echo "+".$cnt."/".$old."+<br />";
757 // Load current refs from database
758 $result_ref = SQL_QUERY_ESC("SELECT DISTINCT SUM(s.counter) AS cnt
759 FROM `{!_MYSQL_PREFIX!}_refsystem` AS s
760 LEFT JOIN `{!_MYSQL_PREFIX!}_refdepths` AS d
762 WHERE s.userid=%s AND s.level=1", array(bigintval($uid)), __FUNCTION__, __LINE__);
763 list($cnt) = SQL_FETCHROW($result_ref);
764 SQL_FREERESULT($result_ref);
773 //* DEBUG: */ echo "*".$uid."/".$old."/".$cnt."*<br />";
777 // Determines the right language string for min_users
778 function RALLYE_DETERMINE_MIN_USERS ($min_users) {
779 // Rallye ends without user limitation is the default
780 $return = getMessage('RALLYE_END_NO_USER_LIMITATION');
782 if ($min_users > 0) {
783 // Rallye ends when X members are totally in your exchange
784 $return = sprintf(getMessage('RALLYE_END_USERS'), $min_users);
791 // Determines the right language string for min_prices
792 function RALLYE_DETERMINE_MIN_PRICES ($min_prices) {
793 // Rallye ends without user limitation is the default
794 $return = getMessage('RALLYE_END_NO_PRICE_LIMITATION');
796 if ($min_prices > 0) {
797 // Rallye ends when X members are totally in your exchange
798 $return = sprintf(getMessage('RALLYE_END_PRICES'), $min_prices);
805 // Filter for extra-autpurge
806 function FILTER_RALLYE_EXTRA_AUTOPURGE () {
807 // Check expired rallyes (hard-coded 3 days limit for displaying expired rallyes!)
808 RALLYE_DELETE_EXPIRED_RALLYES();