2 /************************************************************************
3 * Mailer v0.2.1-FINAL 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 // Load all rallyes (usally we have only one rallye active per time!
47 list($id, $title, $start, $end, $notify, $min_users, $min_prices) = SQL_FETCHROW($result);
50 SQL_FREERESULT($result);
53 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_rallye_data` SET `notified`='Y' WHERE `id`=%s LIMIT 1",
54 array(bigintval($id)), __FUNCTION__, __LINE__);
56 // Transfer all neccessary data to the $content array
57 $content['userid_cnt'] = countSumTotalData('CONFIRMED','user_data','userid','status',true);
58 $content['start'] = generateDateTime($start, 2);
59 $content['end'] = generateDateTime($end , 2);
60 $content['now_t'] = generateDateTime(time(), 2);
61 $content['title'] = $title;
62 $content['id'] = $id; // id for the rallye details link
64 // Determine min_users and min_prices
65 $content['min_users'] = determineReferalRallyeMinimumUsers($min_users);
66 $content['min_prices'] = determineReferalRallyeMinimumPrices($min_prices);
69 $content['prices'] = addReferalRallyePrices($id);
72 $result_user = SQL_QUERY("SELECT `userid` FROM `{?_MYSQL_PREFIX?}_user_data` WHERE `status`='CONFIRMED' ORDER BY `userid` ASC", __FUNCTION__, __LINE__);
74 // Let's begin with the userids...
75 while ($content = merge_array($content, SQL_FETCHARRAY($result_user))) {
79 $cnt = getReferalRallyeRefsCount($content['userid']);
80 if (empty($cnt)) $cnt = '0'; // Added prevent some unknown troubles... :-?
82 // Check if line is already included...
83 $result_ref = SQL_QUERY_ESC("SELECT `id` FROM `{?_MYSQL_PREFIX?}_rallye_users` WHERE `rallye_id`=%s AND `userid`=%s LIMIT 1",
84 array(bigintval($id), bigintval($content['userid'])), __FUNCTION__, __LINE__);
85 if (SQL_NUMROWS($result_ref) == '0') {
87 SQL_FREERESULT($result_ref);
89 // Add userid and his ref count to table
90 $result_ref = SQL_QUERY_ESC("SELECT
91 SUM(p.points) AS points
93 `{?_MYSQL_PREFIX?}_user_points` AS p
95 `{?_MYSQL_PREFIX?}_user_data` AS d
99 d.`status`='CONFIRMED' AND
101 d.mails_confirmed >= {?ref_payout?} AND
105 array(bigintval($content['userid'])), __FUNCTION__, __LINE__);
108 list($cpoints) = SQL_FETCHROW($result_ref);
111 SQL_FREERESULT($result_ref);
113 if (empty($cpoints)) $cpoints = '0.00000';
116 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_rallye_users` (`rallye_id`, `userid`, `refs`, `curr_points`)
117 VALUES (%s, %s, %s, %s)",
120 bigintval($content['userid']),
123 ), __FUNCTION__, __LINE__);
127 // Ignored but for the template required refs (made before start of rallye)
128 $content['refs'] = $cnt;
130 // Shall I notify this member?
131 if (($notify == 'Y') && ($un)) {
132 // Load email template and send it to the user
133 $message = loadEmailTemplate('member_rallye_notify', $content, $content['userid']);
134 sendEmail($content['userid'], getMaskedMessage('RALLYE_MEMBER_NOTIFY', $title), $message);
138 // Choose the right admin template
139 $templ = 'admin_rallye_no_notify';
140 if ($notify == 'Y') $templ = 'admin_rallye_notify';
142 // Send email to admin
143 sendAdminNotification(getMaskedMessage('RALLYE_ADMIN_NOTIFY', $title), $templ, $prices, 0);
146 SQL_FREERESULT($result_user);
150 function addReferalRallyePrices ($rallye, $mode='email') {
153 case 'email': $mode = "\n"; break;
154 case 'html' : $mode = "<br />\n"; break;
158 $result_prices = SQL_QUERY("SELECT
159 `price_level`, `points`, `info`
161 `{?_MYSQL_PREFIX?}_rallye_prices`
163 `rallye_id`='".$rallye."'
166 __FUNCTION__, __LINE__);
167 if (SQL_NUMROWS($result_prices) > 0) {
169 if ($mode == "\n") $prices = "{--RALLYE_MEMBER_PRICES_ADDED--}:".$mode."------------------------------".$mode;
171 while ($content = SQL_FETCHARRAY($result_prices)) {
172 $prices .= $content['price_level'].getMessage('RALLYE_PRICE').": ";
173 if (!empty($content['info'])) {
174 $prices .= $content['info'];
176 $prices .= $content['points'] . ' {?POINTS?}';
178 $prices .= '' . $mode;
182 SQL_FREERESULT($result_prices);
185 $prices = getMaskedMessage('RALLYE_MEMBER_NO_PRICES', $mode);
188 // Add last line for email mode
189 if ($mode == "\n") $prices .= '------------------------------';
196 function addReferalRallyeTopUsers ($rallye, $default=0) {
197 // First check how many prices are set
198 $result = SQL_QUERY_ESC("SELECT `id` FROM `{?_MYSQL_PREFIX?}_rallye_prices` WHERE `rallye_id`=%s ORDER BY price_level",
199 array(bigintval($rallye)), __FUNCTION__, __LINE__);
200 $prices = SQL_NUMROWS($result);
201 SQL_FREERESULT($result);
203 // And load only limited users
204 $result = SQL_QUERY_ESC("SELECT
205 u.userid, u.refs, u.curr_points, rd.min_users, rd.min_prices
207 `{?_MYSQL_PREFIX?}_rallye_users` AS u
209 `{?_MYSQL_PREFIX?}_refsystem` AS r
213 `{?_MYSQL_PREFIX?}_rallye_data` AS rd
217 u.`rallye_id`=%s AND r.counter > 0
220 array(bigintval($rallye)), __FUNCTION__, __LINE__);
229 while ($content = SQL_FETCHARRAY($result)) {
231 $cnt = getReferalRallyeRefsCount($content['userid'], $content['refs']);
234 $result_ref = SQL_QUERY_ESC("SELECT
237 `{?_MYSQL_PREFIX?}_user_points` AS p
239 `{?_MYSQL_PREFIX?}_user_data` AS d
244 d.`status`='CONFIRMED' AND
247 d.mails_confirmed >= {?ref_payout?}
249 array(bigintval($content['userid'])), __FUNCTION__, __LINE__);
252 list($refpoints) = SQL_FETCHROW($result_ref);
255 SQL_FREERESULT($result_ref);
257 if (empty($refpoints)) $refpoints = '0';
259 // Init userid for list
262 // List only users with at least one ref!
263 //* DEBUG: */ print("*".$cnt.'/'.$content['userid'].'/'.$content['curr_points'].'/'.$refpoints."*<br />");
264 if (($cnt > 0) && ($refpoints > $content['curr_points'])) { $userid = $content['userid']; } else { $cnt = ''; }
266 // Save values to array
267 $DATA['userid'][] = $userid;
268 $DATA['ref'][] = $cnt;
269 $DATA['cpoints'][] = $content['curr_points'];
270 $min_users = $content['min_users'];
271 $min_prices = $content['min_prices'];
275 SQL_FREERESULT($result);
278 array_pk_sort($DATA, array('ref', 'cpoints'), 0, 1, true);
281 $OUT = loadTemplate('guest_rallye_header', true);
283 for ($idx = '0'; $idx < $prices; $idx++) {
284 if (empty($DATA['userid'][$idx])) $DATA['userid'][$idx] = '---';
285 if (empty($DATA['ref'][$idx])) $DATA['ref'][$idx] = '---';
288 <td class=\"switch_sw".$SW." bottom\"> ".($idx+1).".</td>
289 <td align=\"center\" class=\"switch_sw".$SW." bottom\">";
290 if (($DATA['userid'][$idx] == $default) && ($default > 0)) $OUT .= "<strong>";
291 $OUT .= $DATA['userid'][$idx];
292 if (($DATA['userid'][$idx] == $default) && ($default > 0)) $OUT .= "</strong>";
294 <td align=\"center\" class=\"switch_sw".$SW." bottom\">";
295 if (($DATA['userid'][$idx] == $default) && ($default > 0)) $OUT .= "<strong>";
296 $OUT .= $DATA['ref'][$idx];
297 if (($DATA['userid'][$idx] == $default) && ($default > 0)) $OUT .= "</strong>";
303 // Translate min_prices/users
304 $content['min_prices'] = determineReferalRallyeMinimumPrices($min_prices);
305 $content['min_users'] = determineReferalRallyeMinimumUsers($min_users);
308 $OUT .= loadTemplate('guest_rallye_footer', true, $content);
310 // And finnally return the output
314 // Run this function only when a new member has confirmed his email address!
315 function addUserToReferalRallye ($userid, $content = array()) {
318 // Updated extension?
319 if (getExtensionVersion('rallye') >= '0.2.0') {
320 $add .= ", `min_users`, `min_prices`";
323 // Check for an auto-add rallye
324 $result = SQL_QUERY("SELECT
325 `id`, `title`, `start_time`, `end_time`, `send_notify`".$add."
327 `{?_MYSQL_PREFIX?}_rallye_data`
331 `auto_add_new_user`='Y' AND
333 LIMIT 1", __FUNCTION__, __LINE__);
334 if (SQL_NUMROWS($result) == 1) {
336 $min_users = '0'; $min_prices = '0';
339 if (getExtensionVersion('rallye') >= '0.2.0') {
340 list($id, $title, $start, $end, $notify, $min_users, $min_prices) = SQL_FETCHROW($result);
342 list($id, $title, $start, $end, $notify) = SQL_FETCHROW($result);
346 SQL_FREERESULT($result);
348 if ($notify == 'Y') {
349 // Transfer all neccessary data to the global $content array
350 $content['start'] = generateDateTime($start, 2);
351 $content['end'] = generateDateTime($end , 2);
352 $content['now_t'] = generateDateTime(time(), 2);
353 $content['title'] = $title;
354 $content['id'] = $id; // id for the rallye details link
355 $content['ref'] = '0';
356 $content['refs'] = countSumTotalData($userid, 'user_data', 'userid', 'refid', true);
359 $content['prices'] = addReferalRallyePrices($id);
361 // Determine min_users/prices tring
362 $content['min_users'] = determineReferalRallyeMinimumUsers($min_users);
363 $content['min_prices'] = determineReferalRallyeMinimumPrices($min_prices);
365 // Send notification to member
366 $message = loadEmailTemplate('member_rallye_notify', $content, $userid);
367 sendEmail($userid, getMaskedMessage('RALLYE_MEMBER_NOTIFY', $title), $message);
370 // Check if line is already included...
371 $result = SQL_QUERY_ESC("SELECT `id` FROM `{?_MYSQL_PREFIX?}_rallye_users` WHERE `rallye_id`=%s AND `userid`=%s LIMIT 1",
375 ), __FUNCTION__, __LINE__);
377 // Is this user added?
378 if (SQL_NUMROWS($result) < 1) {
379 // Add userid and his ref count to table
380 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_rallye_users` (`rallye_id`, `userid`, `refs`)
381 VALUES ('%s','%s',0)",
385 ), __FUNCTION__, __LINE__);
389 SQL_FREERESULT($result);
394 function markReferalRallyesAsExpired ($result) {
398 list($id, $title, $start, $end, $notify, $min_users, $min_prices) = SQL_FETCHROW($result);
401 SQL_FREERESULT($result);
403 // Load users array (!) with assigned prices
404 $prices = getArrayFromReferalRallyeUsers($id);
407 $DATA = array(); $cnt = '0';
409 $DATA['title'] = $title;
410 $DATA['start'] = generateDateTime($start, 1);
411 $DATA['end'] = generateDateTime($end , 1);
412 $DATA['now_t'] = generateDateTime(time(), 1);
416 foreach($prices['userid'] as $key => $userid) {
418 // active = 1: account is still confirmed
419 // active = '0': account is deleted or locked
420 $result = SQL_QUERY_ESC("SELECT
421 COUNT(`userid`) AS active
423 `{?_MYSQL_PREFIX?}_user_data`
425 `userid`=%s AND `status`='CONFIRMED'
427 array(bigintval($userid)), __FUNCTION__, __LINE__);
428 list($active) = SQL_FETCHROW($result);
429 SQL_FREERESULT($result);
431 $prices['active'][$key] = $active;
433 // Allow valid and active users with at least one ref to get points
434 if (($userid > 0) && ($prices['ref'][$key] > 0) && ($active == 1) && ($prices['cpoints'][$key] > 0)) {
439 if (($total < $min_prices) || ($total == '0')) {
440 // Do not end this rallye!
446 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_rallye_data` SET `expired`='Y' WHERE `id`=%s LIMIT 1",
447 array(bigintval($id)), __FUNCTION__, __LINE__);
449 // Run array through (by userid is the most important 2nd-level-array)
450 foreach($prices['userid'] as $key => $userid) {
451 // Allow valid and active users with at least one ref to get points
452 if (($userid > 0) && ($prices['ref'][$key] > 0) && ($prices['active'][$key] == 1) && ($prices['cpoints'][$key] > 0)) {
453 // Transfer data to array for the mail template
454 $DATA['level'] = $prices['level'][$key];
455 $DATA['points'] = $prices['points'][$key];
456 $DATA['info'] = $prices['info'][$key];
457 $DATA['ref'] = $prices['ref'][$key];
460 $winnerLevel = 'other';
462 // Determine winner level
463 if ($DATA['level'] == 1) {
465 $winnerLevel = '_gold';
466 } elseif ($DATA['level'] == 2) {
468 $winnerLevel = '_silver';
469 } elseif ($DATA['level'] == 3) {
471 $winnerLevel = '_bronce';
474 if ($DATA['points'] > 0) {
475 // Add points directly to user's account
476 addPointsDirectly('rallye_winner' . $winnerLevel, $userid, $DATA['points']);
479 if ($notify == 'Y') {
480 // Prepare infos for the mail template
481 if (!empty($DATA['info'])) {
483 $DATA['infos'] = $DATA['info'];
486 $DATA['infos'] = $DATA['points'] . ' {?POINTS?}';
489 // Add suffix to template name
490 $template = 'member_rallye_expired' . $winnerLevel;
493 $message = loadEmailTemplate($template, $DATA, $userid);
494 sendEmail($userid, getMaskedMessage('RALLYE_MEMBER_EXPIRED_SUBJ', $DATA['level']), $message);
499 $users['userid'][$userid] = $userid;
500 $users['poi'][$userid] = $DATA['infos'];
504 // Select template depending on notfication is switch on / off
505 if ($notify == 'Y') {
506 $templ = 'admin_rallye_expired';
507 } elseif (is_array($users['userid'])) {
508 $templ = 'admin_rallye_expired_no';
509 $cnt = getReferalRallyeUserDataFromArray($users);
512 // Send mail to admin
513 sendAdminNotification(getMaskedMessage('RALLYE_ADMIN_EXPIRED_SUBJ', $title), $templ, $cnt, 0);
516 createNewTask('{--RALLYE_ADMIN_EXPIRED--}: ' . $title, '{--RALLYE_ADMIN_EXPIRED_TEXT--}', 'RALLYE_EXPIRED');
520 function getReferalRallyeUserDataFromArray ($userIds) {
522 $userid_string = implode(',', $userIds['userid']);
524 // Init result string
528 $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",
529 array($userid_string, count($userIds)), __FUNCTION__, __LINE__);
530 while ($content = SQL_FETCHARRAY($result)) {
531 // Construct the message masked and add it
532 $ret .= sprintf("%s %s %s (%s) - %s\n",
533 translateGender($content['gender']),
537 $userIds['poi'][$content['userid']]
542 return substr($ret, 0, -1);
546 function getArrayFromReferalRallyePrices ($rallye) {
555 $result = SQL_QUERY_ESC("SELECT price_level, points, info FROM `{?_MYSQL_PREFIX?}_rallye_prices` WHERE `rallye_id`=%s ORDER BY price_level",
556 array(bigintval($rallye)), __FUNCTION__, __LINE__);
559 while ($content = SQL_FETCHARRAY($result)) {
560 $prices['level'][] = $content['price_level'];
561 $prices['points'][] = $content['points'];
562 $prices['info'][] = $content['info'];
566 SQL_FREERESULT($result);
573 function getArrayFromReferalRallyeUsers ($rallye) {
574 // Fix zero points to 0.00000
575 if (getConfig('ref_payout') == '0') setConfigEntry('ref_payout', '0.00000');
581 'cpoints' => array(),
584 // Load users userid old points earned
585 $result_user = SQL_QUERY_ESC("SELECT `userid`, `refs`, `curr_points` FROM `{?_MYSQL_PREFIX?}_rallye_users` WHERE `rallye_id`=%s ORDER BY `userid` ASC",
586 array(bigintval($rallye)), __FUNCTION__, __LINE__);
587 while ($content = SQL_FETCHARRAY($result_user)) {
588 // Load current ref count
589 $cnt = getReferalRallyeRefsCount($content['userid'], $content['refs']);
592 $result_ref = SQL_QUERY_ESC("SELECT
593 SUM(p.points) AS points
595 `{?_MYSQL_PREFIX?}_user_points` AS p
597 `{?_MYSQL_PREFIX?}_user_data` AS d
601 d.`status`='CONFIRMED' AND
603 d.mails_confirmed >= {?ref_payout?} AND
608 array(bigintval($content['userid'])), __FUNCTION__, __LINE__);
611 list($refpoints) = SQL_FETCHROW($result_ref);
614 SQL_FREERESULT($result_ref);
616 // Fix empty refpoints
617 if (empty($refpoints)) $refpoints = '0';
619 // Store calculated new refs to array
620 $users['userid'][] = $content['userid'];
621 $users['ref'][] = abs($cnt - $content['refs']);
622 $users['cpoints'][] = $refpoints - $content['curr_points'];
626 SQL_FREERESULT($result_user);
628 // Sort array for refs (descending)
629 array_pk_sort($users, array("ref", "cpoints"), 0, 1, true);
631 // Load prices array (!)
632 $prices = getArrayFromReferalRallyePrices($rallye);
634 // Merge users into prices
635 foreach ($prices['level'] as $k => $level) {
636 // We only need to check one element in $users, see above while() block
637 if (isset($users['userid'][$k])) {
638 $prices['userid'][$k] = $users['userid'][$k];
639 if (empty($prices['userid'][$k])) $prices['userid'][$k] = '---';
640 $prices['ref'][$k] = $users['ref'][$k];
641 if (empty($prices['ref'][$k])) $prices['ref'][$k] = '---';
642 $prices['cpoints'][$k] = $users['cpoints'][$k];
646 // Return completed array
651 function addReferalRallyeWinners ($rallye, $default=0) {
652 // First check how many prices are set
653 $result_prices = SQL_QUERY_ESC("SELECT `id` FROM `{?_MYSQL_PREFIX?}_rallye_prices` WHERE `rallye_id`=%s ORDER BY `price_level` ASC",
654 array(bigintval($rallye)), __FUNCTION__, __LINE__);
655 $prices = SQL_NUMROWS($result_prices);
656 SQL_FREERESULT($result_prices);
659 $DATA = getArrayFromReferalRallyeUsers($rallye);
662 $OUT = loadTemplate('guest_rallye_expired_header', true);
664 for ($idx = '0'; $idx < $prices; $idx++) {
666 // active = 1: account is still confirmed
667 // active = '0': account is deleted or locked
668 $active = countSumTotalData($DATA['userid'][$idx], 'user_data', 'userid', 'userid', true, " AND `status`='CONFIRMED'");
670 if (empty($DATA['userid'][$idx])) $DATA['userid'][$idx] = '---';
671 if ((empty($DATA['ref'][$idx])) || ($DATA['ref'][$idx] == '0') || ($active == '0') || ("".round($DATA['cpoints'][$idx])."" == '0') || (empty($DATA['cpoints'][$idx]))) {
672 // Allow valid and active users with at least one ref to get points
673 $DATA['ref'][$idx] = '---';
674 $DATA['userid'][$idx] = '---';
677 if (!empty($DATA['info'][$idx])) {
679 $DATA['infos'][$idx] = $DATA['info'][$idx];
682 $DATA['infos'][$idx] = $DATA['points'][$idx] . ' {?POINTS?}';
688 <td class=\"switch_sw".$SW." bottom".$add."\"> ".($idx+1).".</td>
689 <td align=\"center\" class=\"switch_sw".$SW." bottom".$add."\">";
690 if (($DATA['userid'][$idx] == $default) && ($default > 0)) $OUT .= "<strong>";
691 $OUT .= $DATA['userid'][$idx];
692 if (($DATA['userid'][$idx] == $default) && ($default > 0)) $OUT .= "</strong>";
694 <td align=\"center\" class=\"switch_sw".$SW." bottom".$add."\">";
695 if (($DATA['userid'][$idx] == $default) && ($default > 0)) $OUT .= "<strong>";
696 $OUT .= $DATA['ref'][$idx];
697 if (($DATA['userid'][$idx] == $default) && ($default > 0)) $OUT .= "</strong>";
699 <td align=\"center\" class=\"switch_sw".$SW." bottom".$add."\">";
700 if (($DATA['userid'][$idx] == $default) && ($default > 0)) $OUT .= "<strong>";
701 $OUT .= $DATA['infos'][$idx];
702 if (($DATA['userid'][$idx] == $default) && ($default > 0)) $OUT .= "</strong>";
709 $OUT .= loadTemplate('guest_rallye_expired_footer', true);
711 // And finnally return the output
716 function purgeExpiredReferalRallyes () {
717 // Check for expired rallyes
718 $EXPIRE = 3; // @TODO The hard-coded value...
719 $result_rallye = SQL_QUERY_ESC("SELECT id, title, start_time, end_time
720 FROM `{?_MYSQL_PREFIX?}_rallye_data`
721 WHERE end_time <= (UNIX_TIMESTAMP() - {?ONE_DAY?} - %s) AND expired='Y'",
722 array($EXPIRE), __FUNCTION__, __LINE__);
724 if (SQL_NUMROWS($result_rallye) > 0) {
728 // Expire found rallyes and notify admin
729 while ($content = SQL_FETCHARRAY($result_rallye)) {
730 // Prepare data for mail template
731 $content['start_time'] = generateDateTime($content['start_time'], 1);
732 $content['end_time'] = generateDateTime($content['end_time'] , 1);
733 $content['now_time'] = generateDateTime(time(), 1);
735 // Send mail to admin
736 sendAdminNotification(getMaskedMessage('RALLYE_ADMIN_PURGED_SUBJ', $content['title']), 'admin_rallye_purged', $content, 0);
738 // Purge whole rallye
739 addSql(SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_rallye_data` WHERE `id`=%s LIMIT 1",
740 array(bigintval($content['id'])), __FUNCTION__, __LINE__, false));
741 addSql(SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_rallye_prices` WHERE `rallye_id`=%s LIMIT 1",
742 array(bigintval($content['id'])), __FUNCTION__, __LINE__, false));
743 addSql(SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_rallye_users` WHERE `rallye_id`=%s LIMIT 1",
744 array(bigintval($content['id'])), __FUNCTION__, __LINE__, false));
747 createNewTask('{--RALLYE_ADMIN_PURGED--}: ' . $content['title'], '{--RALLYE_ADMIN_PURGED_TEXT--}', 'RALLYE_PURGED');
751 runFilterChain('run_sqls');
755 SQL_FREERESULT($result_rallye);
759 function addReferalRallyeTemplateSelection ($name = 'template', $default = '') {
764 // Check templates directory
765 $basePath = sprintf("templates/%s/html/rallye/", getLanguage());
767 // Read the directory
768 $templates = getArrayFromDirectory($basePath, 'rallye_', false, true, array(), '.tpl');
771 foreach ($templates as $read) {
772 // Cut prefix and extension away
773 $read = substr($read, 7, strpos($read, '.') - 7);
775 // Accept only template names between 1 and 255 chars length
776 if ((strlen($read) < 256) && (!empty($read))) $ral[] = $read;
779 // Do we have found templates which we can link with the new rallye?
780 if (!empty($ral[0])) {
781 // Generate selection box for all found templates
782 // @TODO Rewrite this to our API function
783 $OUT = "<select name=\"".$name."\" size=\"1\" class=\"admin_select\">
784 <option value=\"\">{--SELECT_NONE--}</option>\n";
785 foreach ($ral as $rallye) {
786 $OUT .= " <option value=\"".$rallye."\"";
787 if ($default == $rallye) $OUT .= ' selected="selected"';
788 $OUT .= ">".$rallye."</option>\n";
790 $OUT .= "</select>\n";
792 // No rallye templates found
793 $OUT = getMessage('RALLYE_NO_TEMPLATES_FOUND');
801 function getReferalRallyeRefsCount ($userid, $old = '0') {
802 // Check current refs
803 if (getExtensionVersion('cache') >= '0.1.2') {
804 // Get refs from cache
806 foreach ($GLOBALS['cache_array']['refsystem']['userid'] as $id => $userid) {
807 // Do we have a ref for this user?
808 //* DEBUG: */ print("id={$id},userid={$userid},userid={$userid},old={$old},level={$GLOBALS['cache_array']['refsystem']['level'][$id]}<br />");
809 if (($userid == $userid) && ($GLOBALS['cache_array']['refsystem']['level'][$id] == 1)) {
810 //* DEBUG: */ print("userid matches!<br />");
811 foreach ($GLOBALS['cache_array']['refdepths']['level'] as $level) {
812 if (($level == $GLOBALS['cache_array']['refsystem']['level'][$id]) && ($level == 1)) {
813 // Level does exist so abort here
814 $cnt = $GLOBALS['cache_array']['refsystem']['counter'][$id];
815 //* DEBUG: */ print("*".$userid.'/'.$cnt."*<br />");
817 } elseif ($level > 1) {
818 // Not interesting here...
828 //* DEBUG: */ print("<pre>");
829 //* DEBUG: */ print(print_r($GLOBALS['cache_array']['refsystem'], true));
830 //* DEBUG: */ print("</pre>");
831 //* DEBUG: */ shutdown();
835 incrementStatsEntry('cache_hits');
838 //* DEBUG: */ print('+'.$cnt.'/'.$old."+<br />");
842 // Load current refs from database
843 $result_ref = SQL_QUERY_ESC("SELECT SUM(s.counter) AS cnt
844 FROM `{?_MYSQL_PREFIX?}_refsystem` AS s
845 LEFT JOIN `{?_MYSQL_PREFIX?}_refdepths` AS d
847 WHERE s.userid=%s AND s.level=1", array(bigintval($userid)), __FUNCTION__, __LINE__);
848 list($cnt) = SQL_FETCHROW($result_ref);
849 SQL_FREERESULT($result_ref);
858 //* DEBUG: */ print("*".$userid.'/'.$old.'/'.$cnt."*<br />");
862 // Determines the right language string for min_users
863 function determineReferalRallyeMinimumUsers ($min_users) {
864 // Rallye ends without user limitation is the default
865 $return = getMessage('RALLYE_END_NO_USER_LIMITATION');
867 if ($min_users > 0) {
868 // Rallye ends when X members are totally in your exchange
869 $return = getMaskedMessage('RALLYE_END_USERS', $min_users);
876 // Determines the right language string for min_prices
877 function determineReferalRallyeMinimumPrices ($min_prices) {
878 // Rallye ends without user limitation is the default
879 $return = getMessage('RALLYE_END_NO_PRICE_LIMITATION');
881 if ($min_prices > 0) {
882 // Rallye ends when X members are totally in your exchange
883 $return = getMaskedMessage('RALLYE_END_PRICES', $min_prices);
890 // Filter for extra-autpurge
891 function FILTER_RALLYE_EXTRA_AUTOPURGE () {
892 // Check expired rallyes (hard-coded 3 days limit for displaying expired rallyes!)
893 purgeExpiredReferalRallyes();