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 - 2009 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')) {
44 // Auto-start referal rallyes
45 function autostartReferalRallyes ($result) {
46 // Global data array for loadEmailTemplate()
50 // Load all rallyes (usally we have only one rallye active per time!
51 list($id, $title, $start, $end, $notify, $min_users, $min_prices) = SQL_FETCHROW($result);
54 SQL_FREERESULT($result);
57 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_rallye_data` SET `notified`='Y' WHERE `id`=%s LIMIT 1",
58 array(bigintval($id)), __FUNCTION__, __LINE__);
60 // Transfer all neccessary data to the $DATA array
61 $DATA['userid_cnt'] = countSumTotalData('CONFIRMED','user_data','userid','status',true);
62 $DATA['start'] = generateDateTime($start, 2);
63 $DATA['end'] = generateDateTime($end , 2);
64 $DATA['now_t'] = generateDateTime(time(), 2);
65 $DATA['title'] = $title;
66 $DATA['id'] = $id; // ID for the rallye details link
68 // Determine min_users and min_prices
69 $DATA['min_users'] = determineReferalRallyeMinimumUsers($min_users);
70 $DATA['min_prices'] = determineReferalRallyeMinimumPrices($min_prices);
73 $prices = addReferalRallyePrices($id);
76 $result_user = SQL_QUERY("SELECT `userid` FROM `{?_MYSQL_PREFIX?}_user_data` WHERE `status`='CONFIRMED' ORDER BY `userid` ASC", __FUNCTION__, __LINE__);
78 // Let's begin with the userids...
79 while ($content = SQL_FETCHARRAY($result_user)) {
83 $cnt = getReferalRallyeRefsCount($content['userid']);
84 if (empty($cnt)) $cnt = 0; // Added prevent some unknown troubles... :-?
86 // Check if line is already included...
87 $result_ref = SQL_QUERY_ESC("SELECT `id` FROM `{?_MYSQL_PREFIX?}_rallye_users` WHERE rallye_id=%s AND `userid`=%s LIMIT 1",
88 array(bigintval($id), bigintval($content['userid'])), __FUNCTION__, __LINE__);
89 if (SQL_NUMROWS($result_ref) == 0) {
91 SQL_FREERESULT($result_ref);
93 // Add userid and his ref count to table
94 $result_ref = SQL_QUERY_ESC("SELECT
95 SUM(p.points) AS points
97 `{?_MYSQL_PREFIX?}_user_points` AS p
99 `{?_MYSQL_PREFIX?}_user_data` AS d
103 d.`status`='CONFIRMED' AND
105 d.mails_confirmed >= {?ref_payout?} AND
109 array(bigintval($content['userid'])), __FUNCTION__, __LINE__);
112 list($cpoints) = SQL_FETCHROW($result_ref);
115 SQL_FREERESULT($result_ref);
117 if (empty($cpoints)) $cpoints = '0.00000';
120 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_rallye_users` (rallye_id, userid, refs, curr_points)
121 VALUES ('%s','%s','%s','%s')",
122 array(bigintval($id), bigintval($content['userid']), bigintval($cnt), $cpoints), __FUNCTION__, __LINE__);
126 // Ignored but for the template required refs (made before start of rallye)
127 $DATA['refs'] = $cnt;
129 // Shall I notify this member?
130 if (($notify == 'Y') && ($un)) {
131 // Load email template and send it to the user
132 $message = loadEmailTemplate('member_rallye_notify', array('prices' => $prices), $content['userid']);
133 sendEmail($content['userid'], sprintf(getMessage('RALLYE_MEMBER_NOTIFY'), $title), $message);
137 // Choose the right admin template
138 $templ = 'admin_rallye_no_notify';
139 if ($notify == 'Y') $templ = 'admin_rallye_notify';
141 // Send email to admin
142 sendAdminNotification(sprintf(getMessage('RALLYE_ADMIN_NOTIFY'), $title), $templ, $prices, 0);
145 SQL_FREERESULT($result_user);
149 function addReferalRallyePrices ($rallye, $mode='email') {
152 case 'email': $mode = "\n"; break;
153 case 'html' : $mode = "<br />\n"; break;
157 $result_prices = SQL_QUERY("SELECT
158 `price_level`, `points`, `info`
160 `{?_MYSQL_PREFIX?}_rallye_prices`
162 `rallye_id`='".$rallye."'
165 __FUNCTION__, __LINE__);
166 if (SQL_NUMROWS($result_prices) > 0) {
168 if ($mode == "\n") $prices = "{--RALLYE_MEMBER_PRICES_ADDED--}:".$mode."------------------------------".$mode;
170 while ($content = SQL_FETCHARRAY($result_prices)) {
171 $prices .= $content['price_level'].getMessage('RALLYE_PRICE').": ";
172 if (!empty($content['info'])) {
173 $prices .= $content['info'];
175 $prices .= $content['points'] . ' {?POINTS?}';
177 $prices .= '' . $mode;
181 SQL_FREERESULT($result_prices);
184 $prices = sprintf(getMessage('RALLYE_MEMBER_NO_PRICES'), $mode);
187 // Add last line for email mode
188 if ($mode == "\n") $prices .= '------------------------------';
195 function addReferalRallyeTopUsers ($rallye, $default=0) {
196 // First check how many prices are set
197 $result = SQL_QUERY_ESC("SELECT `id` FROM `{?_MYSQL_PREFIX?}_rallye_prices` WHERE rallye_id=%s ORDER BY price_level",
198 array(bigintval($rallye)), __FUNCTION__, __LINE__);
199 $prices = SQL_NUMROWS($result);
200 SQL_FREERESULT($result);
202 // And load only limited users
203 $result = SQL_QUERY_ESC("SELECT
204 u.userid, u.refs, u.curr_points FROM `{?_MYSQL_PREFIX?}_rallye_users` AS u
206 `{?_MYSQL_PREFIX?}_refsystem` AS r
210 u.rallye_id=%s AND r.counter > 0
213 array(bigintval($rallye)), __FUNCTION__, __LINE__);
222 while ($content = SQL_FETCHARRAY($result)) {
224 $cnt = getReferalRallyeRefsCount($content['userid'], $content['refs']);
227 $result_ref = SQL_QUERY_ESC("SELECT
230 `{?_MYSQL_PREFIX?}_user_points` AS p
232 `{?_MYSQL_PREFIX?}_user_data` AS d
237 d.`status`='CONFIRMED' AND
240 d.mails_confirmed >= %s
242 array(bigintval($content['userid']), getConfig('ref_payout')), __FUNCTION__, __LINE__);
245 list($refpoints) = SQL_FETCHROW($result_ref);
248 SQL_FREERESULT($result_ref);
250 if (empty($refpoints)) $refpoints = 0;
252 // Init userid for list
255 // List only users with at least one ref!
256 //* DEBUG: */ print("*".$cnt.'/'.$content['userid'].'/'.$content['curr_points'].'/'.$refpoints."*<br />");
257 if (($cnt > 0) && ($refpoints > $content['curr_points'])) { $_userid = $content['userid']; } else { $cnt = ''; }
259 // Save values to array
260 $DATA['userid'][] = $_userid;
261 $DATA['ref'][] = $cnt;
262 $DATA['cpoints'][] = $content['curr_points'];
266 SQL_FREERESULT($result);
269 array_pk_sort($DATA, array('ref', 'cpoints'), 0, 1, true);
272 $OUT = loadTemplate('guest_rallye_header', true);
274 for ($idx = 0; $idx < $prices; $idx++) {
275 if (empty($DATA['userid'][$idx])) $DATA['userid'][$idx] = '---';
276 if (empty($DATA['ref'][$idx])) $DATA['ref'][$idx] = '---';
279 <td class=\"switch_sw".$SW." bottom2\"> ".($idx+1).".</td>
280 <td align=\"center\" class=\"switch_sw".$SW." bottom2\">";
281 if (($DATA['userid'][$idx] == $default) && ($default > 0)) $OUT .= "<strong>";
282 $OUT .= $DATA['userid'][$idx];
283 if (($DATA['userid'][$idx] == $default) && ($default > 0)) $OUT .= "</strong>";
285 <td align=\"center\" class=\"switch_sw".$SW." bottom2\">";
286 if (($DATA['userid'][$idx] == $default) && ($default > 0)) $OUT .= "<strong>";
287 $OUT .= $DATA['ref'][$idx];
288 if (($DATA['userid'][$idx] == $default) && ($default > 0)) $OUT .= "</strong>";
295 $OUT .= loadTemplate('guest_rallye_footer', true);
297 // And finnally return the output
301 // Run this function only when a new member has confirmed his email address!
302 function addUserToReferalRallye ($userid) {
306 // Updated extension?
307 if (getExtensionVersion('rallye') >= '0.2.0') {
308 $add .= ", `min_users`, `min_prices`";
311 // Check for an auto-add rallye
312 $result = SQL_QUERY("SELECT
313 `id`, `title`, `start_time`, `end_time`, `send_notify`".$add."
315 `{?_MYSQL_PREFIX?}_rallye_data`
319 `auto_add_new_user`='Y' AND
321 LIMIT 1", __FUNCTION__, __LINE__);
322 if (SQL_NUMROWS($result) == 1) {
324 $min_users = 0; $min_prices = 0;
326 if (getExtensionVersion('rallye') >= '0.2.0') {
327 list($id, $title, $start, $end, $notify, $min_users, $min_prices) = SQL_FETCHROW($result);
329 list($id, $title, $start, $end, $notify) = SQL_FETCHROW($result);
333 SQL_FREERESULT($result);
335 // Check if line is already included...
336 $result = SQL_QUERY_ESC("SELECT `id` FROM `{?_MYSQL_PREFIX?}_rallye_users` WHERE `rallye_id`=%s AND `userid`=%s LIMIT 1",
337 array(bigintval($id), bigintval($userid)), __FUNCTION__, __LINE__);
339 // Is this user added?
340 if (SQL_NUMROWS($result) == 0) {
341 // Add userid and his ref count to table
342 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_rallye_users` (`rallye_id`, `userid`, `refs`)
343 VALUES ('%s','%s',0)",
344 array(bigintval($id), bigintval($userid)), __FUNCTION__, __LINE__);
348 SQL_FREERESULT($result);
350 if ($notify == 'Y') {
351 // Transfer all neccessary data to the global $DATA array
352 $DATA['start'] = generateDateTime($start, 2);
353 $DATA['end'] = generateDateTime($end , 2);
354 $DATA['now_t'] = generateDateTime(time(), 2);
355 $DATA['title'] = $title;
356 $DATA['id'] = $id; // ID for the rallye details link
358 $DATA['refs'] = countSumTotalData($userid, 'user_data', 'userid', "refid", true);
361 $prices = addReferalRallyePrices($id);
363 // Determine min_users/prices tring
364 $DATA['min_users'] = determineReferalRallyeMinimumUsers($min_users);
365 $DATA['min_prices'] = determineReferalRallyeMinimumPrices($min_prices);
367 // Send notification to member
368 $message = loadEmailTemplate('member_rallye_notify', array('prices' => $prices), $userid);
369 sendEmail($userid, sprintf(getMessage('RALLYE_MEMBER_NOTIFY'), $title), $message);
375 function markReferalRallyesAsExpired ($result) {
379 list($id, $title, $start, $end, $notify, $min_users, $min_prices) = SQL_FETCHROW($result);
382 SQL_FREERESULT($result);
384 // Load users array (!) with assigned prices
385 $prices = getArrayFromReferalRallyeUsers($id);
388 $DATA = array(); $cnt = 0;
390 $DATA['title'] = $title;
391 $DATA['start'] = generateDateTime($start, 1);
392 $DATA['end'] = generateDateTime($end , 1);
393 $DATA['now_t'] = generateDateTime(time(), 1);
397 foreach($prices['userid'] as $key => $userid) {
399 // active = 1: account is still confirmed
400 // active = 0: account is deleted or locked
401 $result = SQL_QUERY_ESC("SELECT
402 COUNT(`userid`) AS active
404 `{?_MYSQL_PREFIX?}_user_data`
406 `userid`=%s AND `status`='CONFIRMED'
408 array(bigintval($userid)), __FUNCTION__, __LINE__);
409 list($active) = SQL_FETCHROW($result);
410 SQL_FREERESULT($result);
412 $prices['active'][$key] = $active;
414 // Allow valid and active users with at least one ref to get points
415 if (($userid > 0) && ($prices['ref'][$key] > 0) && ($active == 1) && ($prices['cpoints'][$key] > 0)) {
420 if (($total < $min_prices) || ($total == 0)) {
421 // Do not end this rallye!
427 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_rallye_data` SET `expired`='Y' WHERE `id`=%s LIMIT 1",
428 array(bigintval($id)), __FUNCTION__, __LINE__);
430 // Run array through (by userid is the most important 2nd-level-array)
431 foreach($prices['userid'] as $key => $userid) {
432 // Allow valid and active users with at least one ref to get points
433 if (($userid > 0) && ($prices['ref'][$key] > 0) && ($prices['active'][$key] == 1) && ($prices['cpoints'][$key] > 0)) {
434 // Transfer data to array for the mail template
435 $DATA['level'] = $prices['level'][$key];
436 $DATA['points'] = $prices['points'][$key];
437 $DATA['info'] = $prices['info'][$key];
438 $DATA['ref'] = $prices['ref'][$key];
441 $winnerLevel = 'other';
443 // Determine winner level
444 if ($DATA['level'] == 1) {
446 $winnerLevel = '_gold';
447 } elseif ($DATA['level'] == 2) {
449 $winnerLevel = '_silver';
450 } elseif ($DATA['level'] == 3) {
452 $winnerLevel = '_bronce';
455 if ($DATA['points'] > 0) {
456 // Add points directly to user's account
457 addPointsDirectly('rallye_winner' . $winnerLevel, $userid, $DATA['points']);
460 if ($notify == 'Y') {
461 // Prepare infos for the mail template
462 if (!empty($DATA['info'])) {
464 $DATA['infos'] = $DATA['info'];
467 $DATA['infos'] = $DATA['points'] . ' {?POINTS?}';
470 // Add suffix to template name
471 $template = 'member_rallye_expired' . $winnerLevel;
474 $message = loadEmailTemplate($template, $DATA, $userid);
475 sendEmail($userid, sprintf(getMessage('RALLYE_MEMBER_EXPIRED_SUBJ'), $DATA['level']), $message);
480 $users['userid'][$userid] = $userid;
481 $users['poi'][$userid] = $DATA['infos'];
485 // Select template depending on notfication is switch on / off
486 if ($notify == 'Y') {
487 $templ = "admin_rallye_expired";
488 } elseif (is_array($users['userid'])) {
489 $templ = "admin_rallye_expired_no";
490 $cnt = getReferalRallyeUserDataFromArray($users);
493 // Send mail to admin
494 sendAdminNotification(sprintf(getMessage('RALLYE_ADMIN_EXPIRED_SUBJ'), $title), $templ, $cnt, 0);
497 createNewTask('{--RALLYE_ADMIN_EXPIRED--}: ' . $title, '{--RALLYE_ADMIN_EXPIRED_TEXT--}', 'RALLYE_EXPIRED');
501 function getReferalRallyeUserDataFromArray ($userIds) {
503 $userid_string = implode(',', $userIds['userid']);
505 // Init result string
509 $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` ASC LIMIT %s",
510 array($userid_string, count($userIds)), __FUNCTION__, __LINE__);
511 while ($content = SQL_FETCHARRAY($result)) {
512 // Construct the message masked and add it
513 $ret .= sprintf("%s %s %s (%s) - %s\n",
514 translateGender($content['gender']),
518 $userIds['poi'][$content['userid']]
523 return substr($ret, 0, -1);
527 function getArrayFromReferalRallyePrices ($rallye) {
536 $result = SQL_QUERY_ESC("SELECT price_level, points, info FROM `{?_MYSQL_PREFIX?}_rallye_prices` WHERE rallye_id=%s ORDER BY price_level",
537 array(bigintval($rallye)), __FUNCTION__, __LINE__);
540 while ($content = SQL_FETCHARRAY($result)) {
541 $prices['level'][] = $content['price_level'];
542 $prices['points'][] = $content['points'];
543 $prices['info'][] = $content['info'];
547 SQL_FREERESULT($result);
554 function getArrayFromReferalRallyeUsers ($rallye) {
555 // Fix zero points to 0.00000
556 if (getConfig('ref_payout') == 0) setConfigEntry('ref_payout', '0.00000');
562 'cpoints' => array(),
565 // Load users userid old points earned
566 $result_user = SQL_QUERY_ESC("SELECT `userid`, `refs`, `curr_points` FROM `{?_MYSQL_PREFIX?}_rallye_users` WHERE `rallye_id`=%s ORDER BY `userid` ASC",
567 array(bigintval($rallye)), __FUNCTION__, __LINE__);
568 while ($content = SQL_FETCHARRAY($result_user)) {
569 // Load current ref count
570 $cnt = getReferalRallyeRefsCount($content['userid'], $content['refs']);
573 $result_ref = SQL_QUERY_ESC("SELECT
574 SUM(p.points) AS points
576 `{?_MYSQL_PREFIX?}_user_points` AS p
578 `{?_MYSQL_PREFIX?}_user_data` AS d
582 d.`status`='CONFIRMED' AND
584 d.mails_confirmed >= {?ref_payout?} AND
589 array(bigintval($content['userid'])), __FUNCTION__, __LINE__);
592 list($refpoints) = SQL_FETCHROW($result_ref);
595 SQL_FREERESULT($result_ref);
597 // Fix empty refpoints
598 if (empty($refpoints)) $refpoints = 0;
600 // Store calculated new refs to array
601 $users['userid'][] = $content['userid'];
602 $users['ref'][] = abs($cnt - $content['refs']);
603 $users['cpoints'][] = $refpoints - $content['curr_points'];
607 SQL_FREERESULT($result_user);
609 // Sort array for refs (descending)
610 array_pk_sort($users, array("ref", "cpoints"), 0, 1, true);
612 // Load prices array (!)
613 $prices = getArrayFromReferalRallyePrices($rallye);
615 // Merge users into prices
616 foreach ($prices['level'] as $k => $level) {
617 // We only need to check one element in $users, see above while() block
618 if (isset($users['userid'][$k])) {
619 $prices['userid'][$k] = $users['userid'][$k];
620 if (empty($prices['userid'][$k])) $prices['userid'][$k] = '---';
621 $prices['ref'][$k] = $users['ref'][$k];
622 if (empty($prices['ref'][$k])) $prices['ref'][$k] = '---';
623 $prices['cpoints'][$k] = $users['cpoints'][$k];
627 // Return completed array
632 function addReferalRallyeWinners ($rallye, $default=0) {
633 // First check how many prices are set
634 $result_prices = SQL_QUERY_ESC("SELECT `id` FROM `{?_MYSQL_PREFIX?}_rallye_prices` WHERE `rallye_id`=%s ORDER BY `price_level` ASC",
635 array(bigintval($rallye)), __FUNCTION__, __LINE__);
636 $prices = SQL_NUMROWS($result_prices);
637 SQL_FREERESULT($result_prices);
640 $DATA = getArrayFromReferalRallyeUsers($rallye);
643 $OUT = loadTemplate('guest_rallye_expired_header', true);
645 for ($idx = 0; $idx < $prices; $idx++) {
647 // active = 1: account is still confirmed
648 // active = 0: account is deleted or locked
649 $active = countSumTotalData($DATA['userid'][$idx], 'user_data', 'userid', 'userid', true, " AND `status`='CONFIRMED'");
651 if (empty($DATA['userid'][$idx])) $DATA['userid'][$idx] = '---';
652 if ((empty($DATA['ref'][$idx])) || ($DATA['ref'][$idx] == 0) || ($active == 0) || ("".round($DATA['cpoints'][$idx])."" == 0) || (empty($DATA['cpoints'][$idx]))) {
653 // Allow valid and active users with at least one ref to get points
654 $DATA['ref'][$idx] = '---';
655 $DATA['userid'][$idx] = '---';
658 if (!empty($DATA['info'][$idx])) {
660 $DATA['infos'][$idx] = $DATA['info'][$idx];
663 $DATA['infos'][$idx] = $DATA['points'][$idx] . ' {?POINTS?}';
669 <td class=\"switch_sw".$SW." bottom2".$add."\"> ".($idx+1).".</td>
670 <td align=\"center\" class=\"switch_sw".$SW." bottom2".$add."\">";
671 if (($DATA['userid'][$idx] == $default) && ($default > 0)) $OUT .= "<strong>";
672 $OUT .= $DATA['userid'][$idx];
673 if (($DATA['userid'][$idx] == $default) && ($default > 0)) $OUT .= "</strong>";
675 <td align=\"center\" class=\"switch_sw".$SW." bottom2".$add."\">";
676 if (($DATA['userid'][$idx] == $default) && ($default > 0)) $OUT .= "<strong>";
677 $OUT .= $DATA['ref'][$idx];
678 if (($DATA['userid'][$idx] == $default) && ($default > 0)) $OUT .= "</strong>";
680 <td align=\"center\" class=\"switch_sw".$SW." bottom2".$add."\">";
681 if (($DATA['userid'][$idx] == $default) && ($default > 0)) $OUT .= "<strong>";
682 $OUT .= $DATA['infos'][$idx];
683 if (($DATA['userid'][$idx] == $default) && ($default > 0)) $OUT .= "</strong>";
690 $OUT .= loadTemplate('guest_rallye_expired_footer', true);
692 // And finnally return the output
697 function purgeExpiredReferalRallyes () {
698 // Check for expired rallyes
699 $EXPIRE = 3; // @TODO The hard-coded value...
700 $result_rallye = SQL_QUERY_ESC("SELECT id, title, start_time, end_time
701 FROM `{?_MYSQL_PREFIX?}_rallye_data`
702 WHERE end_time <= (UNIX_TIMESTAMP() - {?ONE_DAY?} - %s) AND expired='Y'",
703 array($EXPIRE), __FUNCTION__, __LINE__);
705 if (SQL_NUMROWS($result_rallye) > 0) {
709 // Expire found rallyes and notify admin
710 while ($content = SQL_FETCHARRAY($result_rallye)) {
711 // Prepare data for mail template
712 $content['start_time'] = generateDateTime($content['start_time'], 1);
713 $content['end_time'] = generateDateTime($content['end_time'] , 1);
714 $content['now_time'] = generateDateTime(time(), 1);
716 // Send mail to admin
717 sendAdminNotification(sprintf(getMessage('RALLYE_ADMIN_PURGED_SUBJ'), $content['title']), 'admin_rallye_purged', $content, 0);
719 // Purge whole rallye
720 addSql(SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_rallye_data` WHERE `id`=%s LIMIT 1",
721 array(bigintval($content['id'])), __FUNCTION__, __LINE__, false));
722 addSql(SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_rallye_prices` WHERE rallye_id=%s LIMIT 1",
723 array(bigintval($content['id'])), __FUNCTION__, __LINE__, false));
724 addSql(SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_rallye_users` WHERE rallye_id=%s LIMIT 1",
725 array(bigintval($content['id'])), __FUNCTION__, __LINE__, false));
728 createNewTask('{--RALLYE_ADMIN_PURGED--}: ' . $content['title'], '{--RALLYE_ADMIN_PURGED_TEXT--}', 'RALLYE_PURGED');
732 runFilterChain('run_sqls');
736 SQL_FREERESULT($result_rallye);
740 function addReferalRallyeTemplateSelection ($name = 'template', $default = '') {
745 // Check templates directory
746 $basePath = sprintf("templates/%s/html/rallye/", getLanguage());
748 // Read the directory
749 $templates = getArrayFromDirectory($basePath, 'rallye_', false, true, array(), '.tpl');
752 foreach ($templates as $read) {
753 // Cut prefix and extension away
754 $read = substr($read, 7, strpos($read, '.') - 7);
756 // Accept only template names between 1 and 255 chars length
757 if ((strlen($read) < 256) && (!empty($read))) $ral[] = $read;
760 // Do we have found templates which we can link with the new rallye?
761 if (!empty($ral[0])) {
762 // Generate selection box for all found templates
763 // @TODO Rewrite this to our API function
764 $OUT = "<select name=\"".$name."\" size=\"1\" class=\"admin_select\">
765 <option value=\"\">{--SELECT_NONE--}</option>\n";
766 foreach ($ral as $rallye) {
767 $OUT .= " <option value=\"".$rallye."\"";
768 if ($default == $rallye) $OUT .= ' selected="selected"';
769 $OUT .= ">".$rallye."</option>\n";
771 $OUT .= "</select>\n";
773 // No rallye templates found
774 $OUT = getMessage('RALLYE_NO_TEMPLATES_FOUND');
782 function getReferalRallyeRefsCount ($userid, $old = 0) {
783 // Check current refs
784 if (getExtensionVersion('cache') >= '0.1.2') {
785 // Get refs from cache
787 foreach ($GLOBALS['cache_array']['refsystem']['userid'] as $id => $userid) {
788 // Do we have a ref for this user?
789 //* DEBUG: */ print("id={$id},userid={$userid},userid={$userid},old={$old},level={$GLOBALS['cache_array']['refsystem']['level'][$id]}<br />");
790 if (($userid == $userid) && ($GLOBALS['cache_array']['refsystem']['level'][$id] == 1)) {
791 //* DEBUG: */ print("userid matches!<br />");
792 foreach ($GLOBALS['cache_array']['refdepths']['level'] as $level) {
793 if (($level == $GLOBALS['cache_array']['refsystem']['level'][$id]) && ($level == 1)) {
794 // Level does exist so abort here
795 $cnt = $GLOBALS['cache_array']['refsystem']['counter'][$id];
796 //* DEBUG: */ print("*".$userid.'/'.$cnt."*<br />");
798 } elseif ($level > 1) {
799 // Not interesting here...
809 //* DEBUG: */ print("<pre>");
810 //* DEBUG: */ print(print_r($GLOBALS['cache_array']['refsystem'], true));
811 //* DEBUG: */ print("</pre>");
812 //* DEBUG: */ shutdown();
816 incrementStatsEntry('cache_hits');
819 //* DEBUG: */ print('+'.$cnt.'/'.$old."+<br />");
823 // Load current refs from database
824 $result_ref = SQL_QUERY_ESC("SELECT SUM(s.counter) AS cnt
825 FROM `{?_MYSQL_PREFIX?}_refsystem` AS s
826 LEFT JOIN `{?_MYSQL_PREFIX?}_refdepths` AS d
828 WHERE s.userid=%s AND s.level=1", array(bigintval($userid)), __FUNCTION__, __LINE__);
829 list($cnt) = SQL_FETCHROW($result_ref);
830 SQL_FREERESULT($result_ref);
839 //* DEBUG: */ print("*".$userid.'/'.$old.'/'.$cnt."*<br />");
843 // Determines the right language string for min_users
844 function determineReferalRallyeMinimumUsers ($min_users) {
845 // Rallye ends without user limitation is the default
846 $return = getMessage('RALLYE_END_NO_USER_LIMITATION');
848 if ($min_users > 0) {
849 // Rallye ends when X members are totally in your exchange
850 $return = sprintf(getMessage('RALLYE_END_USERS'), $min_users);
857 // Determines the right language string for min_prices
858 function determineReferalRallyeMinimumPrices ($min_prices) {
859 // Rallye ends without user limitation is the default
860 $return = getMessage('RALLYE_END_NO_PRICE_LIMITATION');
862 if ($min_prices > 0) {
863 // Rallye ends when X members are totally in your exchange
864 $return = sprintf(getMessage('RALLYE_END_PRICES'), $min_prices);
871 // Filter for extra-autpurge
872 function FILTER_RALLYE_EXTRA_AUTOPURGE () {
873 // Check expired rallyes (hard-coded 3 days limit for displaying expired rallyes!)
874 purgeExpiredReferalRallyes();