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 * Copyright (c) 2009, 2010 by Mailer Developer Team *
22 * For more information visit: http://www.mxchange.org *
24 * This program is free software; you can redistribute it and/or modify *
25 * it under the terms of the GNU General Public License as published by *
26 * the Free Software Foundation; either version 2 of the License, or *
27 * (at your option) any later version. *
29 * This program is distributed in the hope that it will be useful, *
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
32 * GNU General Public License for more details. *
34 * You should have received a copy of the GNU General Public License *
35 * along with this program; if not, write to the Free Software *
36 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
38 ************************************************************************/
40 // Some security stuff...
41 if (!defined('__SECURITY')) {
45 // Auto-start referal rallyes
46 function autostartReferalRallyes ($result) {
47 // Load all rallyes (usally we have only one rallye active per time!
48 list($id, $title, $start, $end, $notify, $min_users, $min_prices) = SQL_FETCHROW($result);
51 SQL_FREERESULT($result);
54 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_rallye_data` SET `notified`='Y' WHERE `id`=%s LIMIT 1",
55 array(bigintval($id)), __FUNCTION__, __LINE__);
57 // Transfer all neccessary data to the $content array
58 $content['userid_cnt'] = countSumTotalData('CONFIRMED','user_data','userid','status',true);
59 $content['start'] = generateDateTime($start, 2);
60 $content['end'] = generateDateTime($end , 2);
61 $content['now_t'] = generateDateTime(time(), 2);
62 $content['title'] = $title;
63 $content['id'] = $id; // id for the rallye details link
65 // Determine min_users and min_prices
66 $content['min_users'] = determineReferalRallyeMinimumUsers($min_users);
67 $content['min_prices'] = determineReferalRallyeMinimumPrices($min_prices);
70 $content['prices'] = addReferalRallyePrices($id);
73 $result_user = SQL_QUERY("SELECT `userid` FROM `{?_MYSQL_PREFIX?}_user_data` WHERE `status`='CONFIRMED' ORDER BY `userid` ASC", __FUNCTION__, __LINE__);
75 // Let's begin with the userids...
76 while ($row = SQL_FETCHARRAY($result_user)) {
78 $content = merge_array($content, $row);
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)",
124 bigintval($content['userid']),
127 ), __FUNCTION__, __LINE__);
131 // Ignored but for the template required refs (made before start of rallye)
132 $content['refs'] = $cnt;
134 // Shall I notify this member?
135 if (($notify == 'Y') && ($un)) {
136 // Load email template and send it to the user
137 $message = loadEmailTemplate('member_rallye_notify', $content, $content['userid']);
138 sendEmail($content['userid'], getMaskedMessage('RALLYE_MEMBER_NOTIFY', $title), $message);
142 // Choose the right admin template
143 $templ = 'admin_rallye_no_notify';
144 if ($notify == 'Y') $templ = 'admin_rallye_notify';
146 // Send email to admin
147 sendAdminNotification(getMaskedMessage('RALLYE_ADMIN_NOTIFY', $title), $templ, $prices);
150 SQL_FREERESULT($result_user);
154 function addReferalRallyePrices ($rallye, $mode='email') {
157 case 'email': $mode = "\n"; break;
158 case 'html' : $mode = "<br />\n"; break;
162 $result_prices = SQL_QUERY_ESC("SELECT
163 `price_level`, `points`, `info`
165 `{?_MYSQL_PREFIX?}_rallye_prices`
170 array($rallye), __FUNCTION__, __LINE__);
173 if (SQL_NUMROWS($result_prices) > 0) {
175 if ($mode == "\n") $prices = "{--RALLYE_MEMBER_PRICES_ADDED--}:".$mode."------------------------------".$mode;
177 while ($content = SQL_FETCHARRAY($result_prices)) {
178 $prices .= $content['price_level']. '{--RALLYE_PRICE--}: ';
179 if (!empty($content['info'])) {
180 $prices .= $content['info'];
182 $prices .= $content['points'] . ' {?POINTS?}';
184 $prices .= '' . $mode;
188 SQL_FREERESULT($result_prices);
191 $prices = getMaskedMessage('RALLYE_MEMBER_NO_PRICES', $mode);
194 // Add last line for email mode
195 if ($mode == "\n") $prices .= '------------------------------';
202 function addReferalRallyeTopUsers ($rallye, $default=0) {
203 // First check how many prices are set
204 $result = SQL_QUERY_ESC("SELECT `id` FROM `{?_MYSQL_PREFIX?}_rallye_prices` WHERE `rallye_id`=%s ORDER BY price_level",
205 array(bigintval($rallye)), __FUNCTION__, __LINE__);
206 $prices = SQL_NUMROWS($result);
207 SQL_FREERESULT($result);
209 // And load only limited users
210 $result = SQL_QUERY_ESC("SELECT
211 u.userid, u.refs, u.curr_points, rd.min_users, rd.min_prices
213 `{?_MYSQL_PREFIX?}_rallye_users` AS u
215 `{?_MYSQL_PREFIX?}_refsystem` AS r
219 `{?_MYSQL_PREFIX?}_rallye_data` AS rd
223 u.`rallye_id`=%s AND r.counter > 0
226 array(bigintval($rallye)), __FUNCTION__, __LINE__);
235 while ($content = SQL_FETCHARRAY($result)) {
237 $cnt = getReferalRallyeRefsCount($content['userid'], $content['refs']);
240 $result_ref = SQL_QUERY_ESC("SELECT
243 `{?_MYSQL_PREFIX?}_user_points` AS p
245 `{?_MYSQL_PREFIX?}_user_data` AS d
250 d.`status`='CONFIRMED' AND
253 d.mails_confirmed >= {?ref_payout?}
255 array(bigintval($content['userid'])), __FUNCTION__, __LINE__);
258 list($refpoints) = SQL_FETCHROW($result_ref);
261 SQL_FREERESULT($result_ref);
263 if (empty($refpoints)) $refpoints = '0';
265 // Init userid for list
268 // List only users with at least one ref!
269 //* DEBUG: */ print("*".$cnt.'/'.$content['userid'].'/'.$content['curr_points'].'/'.$refpoints."*<br />");
270 if (($cnt > 0) && ($refpoints > $content['curr_points'])) { $userid = $content['userid']; } else { $cnt = ''; }
272 // Save values to array
273 $DATA['userid'][] = $userid;
274 $DATA['ref'][] = $cnt;
275 $DATA['cpoints'][] = $content['curr_points'];
276 $min_users = $content['min_users'];
277 $min_prices = $content['min_prices'];
281 SQL_FREERESULT($result);
284 array_pk_sort($DATA, array('ref', 'cpoints'), 0, 1, true);
287 $OUT = loadTemplate('guest_rallye_header', true);
289 for ($idx = '0'; $idx < $prices; $idx++) {
290 if (empty($DATA['userid'][$idx])) $DATA['userid'][$idx] = '---';
291 if (empty($DATA['ref'][$idx])) $DATA['ref'][$idx] = '---';
294 <td class=\"switch_sw".$SW." bottom\">".($idx+1).".</td>
295 <td align=\"center\" class=\"switch_sw".$SW." bottom\">";
296 if (($DATA['userid'][$idx] == $default) && ($default > 0)) $OUT .= '<strong>';
297 $OUT .= $DATA['userid'][$idx];
298 if (($DATA['userid'][$idx] == $default) && ($default > 0)) $OUT .= '</strong>';
300 <td align=\"center\" class=\"switch_sw".$SW." bottom\">";
301 if (($DATA['userid'][$idx] == $default) && ($default > 0)) $OUT .= '<strong>';
302 $OUT .= $DATA['ref'][$idx];
303 if (($DATA['userid'][$idx] == $default) && ($default > 0)) $OUT .= '</strong>';
309 // Add min_prices/users
310 $content['min_prices'] = bigintval($min_prices);
311 $content['min_users'] = bigintval($min_users);
314 $OUT .= loadTemplate('guest_rallye_footer', true, $content);
316 // And finnally return the output
320 // Run this function only when a new member has confirmed his email address!
321 function addUserToReferalRallye ($userid, $content = array()) {
324 // Updated extension?
325 if (isExtensionInstalledAndNewer('rallye', '0.2.0')) {
326 $add .= ", `min_users`, `min_prices`";
329 // Check for an auto-add rallye
330 $result = SQL_QUERY("SELECT
331 `id`, `title`, `start_time`, `end_time`, `send_notify`".$add."
333 `{?_MYSQL_PREFIX?}_rallye_data`
337 `auto_add_new_user`='Y' AND
339 LIMIT 1", __FUNCTION__, __LINE__);
340 if (SQL_NUMROWS($result) == 1) {
342 $min_users = '0'; $min_prices = '0';
345 if (isExtensionInstalledAndNewer('rallye', '0.2.0')) {
346 list($id, $title, $start, $end, $notify, $min_users, $min_prices) = SQL_FETCHROW($result);
348 list($id, $title, $start, $end, $notify) = SQL_FETCHROW($result);
352 SQL_FREERESULT($result);
354 if ($notify == 'Y') {
355 // Transfer all neccessary data to $content array
356 $content['start'] = generateDateTime($start, 2);
357 $content['end'] = generateDateTime($end , 2);
358 $content['now_t'] = generateDateTime(time(), 2);
359 $content['title'] = $title;
360 $content['id'] = $id; // id for the rallye details link
361 $content['ref'] = '0';
362 $content['refs'] = countSumTotalData($userid, 'user_data', 'userid', 'refid', true);
365 $content['prices'] = addReferalRallyePrices($id);
367 // Determine min_users/prices tring
368 $content['min_users'] = determineReferalRallyeMinimumUsers($min_users);
369 $content['min_prices'] = determineReferalRallyeMinimumPrices($min_prices);
371 // Send notification to member
372 $message = loadEmailTemplate('member_rallye_notify', $content, $userid);
373 sendEmail($userid, getMaskedMessage('RALLYE_MEMBER_NOTIFY', $title), $message);
376 // Check if line is already included...
377 $result = SQL_QUERY_ESC("SELECT `id` FROM `{?_MYSQL_PREFIX?}_rallye_users` WHERE `rallye_id`=%s AND `userid`=%s LIMIT 1",
381 ), __FUNCTION__, __LINE__);
383 // Is this user added?
384 if (SQL_NUMROWS($result) < 1) {
385 // Add userid and his ref count to table
386 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_rallye_users` (`rallye_id`, `userid`, `refs`)
387 VALUES ('%s','%s',0)",
391 ), __FUNCTION__, __LINE__);
395 SQL_FREERESULT($result);
400 function markReferalRallyesAsExpired ($result) {
404 list($id, $title, $start, $end, $notify, $min_users, $min_prices) = SQL_FETCHROW($result);
407 SQL_FREERESULT($result);
409 // Load users array (!) with assigned prices
410 $prices = getArrayFromReferalRallyeUsers($id);
413 $DATA = array(); $cnt = '0';
415 $DATA['title'] = $title;
416 $DATA['start'] = generateDateTime($start, 1);
417 $DATA['end'] = generateDateTime($end , 1);
418 $DATA['now_t'] = generateDateTime(time(), 1);
422 foreach($prices['userid'] as $key => $userid) {
424 // active = 1: account is still confirmed
425 // active = '0': account is deleted or locked
426 $result = SQL_QUERY_ESC("SELECT
427 COUNT(`userid`) AS active
429 `{?_MYSQL_PREFIX?}_user_data`
431 `userid`=%s AND `status`='CONFIRMED'
433 array(bigintval($userid)), __FUNCTION__, __LINE__);
434 list($active) = SQL_FETCHROW($result);
435 SQL_FREERESULT($result);
437 $prices['active'][$key] = $active;
439 // Allow valid and active users with at least one ref to get points
440 if (($userid > 0) && ($prices['ref'][$key] > 0) && ($active == 1) && ($prices['cpoints'][$key] > 0)) {
445 if (($total < $min_prices) || ($total == '0')) {
446 // Do not end this rallye!
452 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_rallye_data` SET `expired`='Y' WHERE `id`=%s LIMIT 1",
453 array(bigintval($id)), __FUNCTION__, __LINE__);
455 // Run array through (by userid is the most important 2nd-level-array)
456 foreach($prices['userid'] as $key => $userid) {
457 // Allow valid and active users with at least one ref to get points
458 if (($userid > 0) && ($prices['ref'][$key] > 0) && ($prices['active'][$key] == 1) && ($prices['cpoints'][$key] > 0)) {
459 // Transfer data to array for the mail template
460 $DATA['level'] = $prices['level'][$key];
461 $DATA['points'] = $prices['points'][$key];
462 $DATA['info'] = $prices['info'][$key];
463 $DATA['ref'] = $prices['ref'][$key];
466 $winnerLevel = 'other';
468 // Determine winner level
469 if ($DATA['level'] == 1) {
471 $winnerLevel = '_gold';
472 } elseif ($DATA['level'] == 2) {
474 $winnerLevel = '_silver';
475 } elseif ($DATA['level'] == 3) {
477 $winnerLevel = '_bronce';
480 if ($DATA['points'] > 0) {
481 // Add points directly to user's account
482 addPointsDirectly('rallye_winner' . $winnerLevel, $userid, $DATA['points']);
485 if ($notify == 'Y') {
486 // Prepare infos for the mail template
487 if (!empty($DATA['info'])) {
489 $DATA['infos'] = $DATA['info'];
492 $DATA['infos'] = $DATA['points'] . ' {?POINTS?}';
495 // Add suffix to template name
496 $template = 'member_rallye_expired' . $winnerLevel;
499 $message = loadEmailTemplate($template, $DATA, $userid);
500 sendEmail($userid, getMaskedMessage('RALLYE_MEMBER_EXPIRED_SUBJ', $DATA['level']), $message);
505 $users['userid'][$userid] = $userid;
506 $users['poi'][$userid] = $DATA['infos'];
510 // Select template depending on notfication is switch on / off
511 if ($notify == 'Y') {
512 $templ = 'admin_rallye_expired';
513 } elseif (is_array($users['userid'])) {
514 $templ = 'admin_rallye_expired_no';
515 $cnt = getReferalRallyeUserDataFromArray($users);
518 // Send mail to admin
519 sendAdminNotification(getMaskedMessage('RALLYE_ADMIN_EXPIRED_SUBJ', $title), $templ, $cnt);
522 createNewTask('{--RALLYE_ADMIN_EXPIRED--}: ' . $title, '{--RALLYE_ADMIN_EXPIRED_TEXT--}', 'RALLYE_EXPIRED');
526 function getReferalRallyeUserDataFromArray ($userIds) {
528 $userid_string = implode(',', $userIds['userid']);
530 // Init result string
534 $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",
535 array($userid_string, count($userIds)), __FUNCTION__, __LINE__);
536 while ($content = SQL_FETCHARRAY($result)) {
537 // Construct the message masked and add it
538 $ret .= sprintf("%s %s %s (%s) - %s\n",
539 translateGender($content['gender']),
543 $userIds['poi'][$content['userid']]
548 return substr($ret, 0, -1);
552 function getArrayFromReferalRallyePrices ($rallye) {
561 $result = SQL_QUERY_ESC("SELECT price_level, points, info FROM `{?_MYSQL_PREFIX?}_rallye_prices` WHERE `rallye_id`=%s ORDER BY price_level",
562 array(bigintval($rallye)), __FUNCTION__, __LINE__);
565 while ($content = SQL_FETCHARRAY($result)) {
566 $prices['level'][] = $content['price_level'];
567 $prices['points'][] = $content['points'];
568 $prices['info'][] = $content['info'];
572 SQL_FREERESULT($result);
579 function getArrayFromReferalRallyeUsers ($rallye) {
580 // Fix zero points to 0.00000
581 if (getConfig('ref_payout') == '0') setConfigEntry('ref_payout', '0.00000');
587 'cpoints' => array(),
590 // Load users userid old points earned
591 $result_user = SQL_QUERY_ESC("SELECT `userid`, `refs`, `curr_points` FROM `{?_MYSQL_PREFIX?}_rallye_users` WHERE `rallye_id`=%s ORDER BY `userid` ASC",
592 array(bigintval($rallye)), __FUNCTION__, __LINE__);
593 while ($content = SQL_FETCHARRAY($result_user)) {
594 // Load current ref count
595 $cnt = getReferalRallyeRefsCount($content['userid'], $content['refs']);
598 $result_ref = SQL_QUERY_ESC("SELECT
599 SUM(p.points) AS points
601 `{?_MYSQL_PREFIX?}_user_points` AS p
603 `{?_MYSQL_PREFIX?}_user_data` AS d
607 d.`status`='CONFIRMED' AND
609 d.mails_confirmed >= {?ref_payout?} AND
614 array(bigintval($content['userid'])), __FUNCTION__, __LINE__);
617 list($refpoints) = SQL_FETCHROW($result_ref);
620 SQL_FREERESULT($result_ref);
622 // Fix empty refpoints
623 if (empty($refpoints)) $refpoints = '0';
625 // Store calculated new refs to array
626 $users['userid'][] = $content['userid'];
627 $users['ref'][] = abs($cnt - $content['refs']);
628 $users['cpoints'][] = $refpoints - $content['curr_points'];
632 SQL_FREERESULT($result_user);
634 // Sort array for refs (descending)
635 array_pk_sort($users, array("ref", "cpoints"), 0, 1, true);
637 // Load prices array (!)
638 $prices = getArrayFromReferalRallyePrices($rallye);
640 // Merge users into prices
641 foreach ($prices['level'] as $k => $level) {
642 // We only need to check one element in $users, see above while() block
643 if (isset($users['userid'][$k])) {
644 $prices['userid'][$k] = $users['userid'][$k];
645 if (empty($prices['userid'][$k])) $prices['userid'][$k] = '---';
646 $prices['ref'][$k] = $users['ref'][$k];
647 if (empty($prices['ref'][$k])) $prices['ref'][$k] = '---';
648 $prices['cpoints'][$k] = $users['cpoints'][$k];
652 // Return completed array
657 function addReferalRallyeWinners ($rallye, $default=0) {
658 // First check how many prices are set
659 $result_prices = SQL_QUERY_ESC("SELECT `id` FROM `{?_MYSQL_PREFIX?}_rallye_prices` WHERE `rallye_id`=%s ORDER BY `price_level` ASC",
660 array(bigintval($rallye)), __FUNCTION__, __LINE__);
661 $prices = SQL_NUMROWS($result_prices);
662 SQL_FREERESULT($result_prices);
665 $DATA = getArrayFromReferalRallyeUsers($rallye);
668 $OUT = loadTemplate('guest_rallye_expired_header', true);
670 for ($idx = '0'; $idx < $prices; $idx++) {
672 // active = 1: account is still confirmed
673 // active = '0': account is deleted or locked
674 $active = countSumTotalData($DATA['userid'][$idx], 'user_data', 'userid', 'userid', true, " AND `status`='CONFIRMED'");
676 if (empty($DATA['userid'][$idx])) $DATA['userid'][$idx] = '---';
677 if ((empty($DATA['ref'][$idx])) || ($DATA['ref'][$idx] == '0') || ($active == '0') || ("".round($DATA['cpoints'][$idx])."" == '0') || (empty($DATA['cpoints'][$idx]))) {
678 // Allow valid and active users with at least one ref to get points
679 $DATA['ref'][$idx] = '---';
680 $DATA['userid'][$idx] = '---';
683 if (!empty($DATA['info'][$idx])) {
685 $DATA['infos'][$idx] = $DATA['info'][$idx];
688 $DATA['infos'][$idx] = $DATA['points'][$idx] . ' {?POINTS?}';
694 <td class=\"switch_sw".$SW." bottom".$add."\">".($idx+1).".</td>
695 <td align=\"center\" class=\"switch_sw".$SW." bottom".$add."\">";
696 if (($DATA['userid'][$idx] == $default) && ($default > 0)) $OUT .= '<strong>';
697 $OUT .= $DATA['userid'][$idx];
698 if (($DATA['userid'][$idx] == $default) && ($default > 0)) $OUT .= '</strong>';
700 <td align=\"center\" class=\"switch_sw".$SW." bottom".$add."\">";
701 if (($DATA['userid'][$idx] == $default) && ($default > 0)) $OUT .= '<strong>';
702 $OUT .= $DATA['ref'][$idx];
703 if (($DATA['userid'][$idx] == $default) && ($default > 0)) $OUT .= '</strong>';
705 <td align=\"center\" class=\"switch_sw".$SW." bottom".$add."\">";
706 if (($DATA['userid'][$idx] == $default) && ($default > 0)) $OUT .= '<strong>';
707 $OUT .= $DATA['infos'][$idx];
708 if (($DATA['userid'][$idx] == $default) && ($default > 0)) $OUT .= '</strong>';
715 $OUT .= loadTemplate('guest_rallye_expired_footer', true);
717 // And finnally return the output
722 function purgeExpiredReferalRallyes () {
723 // Check for expired rallyes
724 $EXPIRE = 3; // @TODO The hard-coded value...
725 $result_rallye = SQL_QUERY_ESC("SELECT id, title, start_time, end_time
726 FROM `{?_MYSQL_PREFIX?}_rallye_data`
727 WHERE end_time <= (UNIX_TIMESTAMP() - {?ONE_DAY?} - %s) AND expired='Y'",
728 array($EXPIRE), __FUNCTION__, __LINE__);
730 if (SQL_NUMROWS($result_rallye) > 0) {
734 // Expire found rallyes and notify admin
735 while ($content = SQL_FETCHARRAY($result_rallye)) {
736 // Prepare data for mail template
737 $content['start_time'] = generateDateTime($content['start_time'], 1);
738 $content['end_time'] = generateDateTime($content['end_time'] , 1);
739 $content['now_time'] = generateDateTime(time(), 1);
741 // Send mail to admin
742 sendAdminNotification(getMaskedMessage('RALLYE_ADMIN_PURGED_SUBJ', $content['title']), 'admin_rallye_purged', $content);
744 // Purge whole rallye
745 addSql(SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_rallye_data` WHERE `id`=%s LIMIT 1",
746 array(bigintval($content['id'])), __FUNCTION__, __LINE__, false));
747 addSql(SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_rallye_prices` WHERE `rallye_id`=%s LIMIT 1",
748 array(bigintval($content['id'])), __FUNCTION__, __LINE__, false));
749 addSql(SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_rallye_users` WHERE `rallye_id`=%s LIMIT 1",
750 array(bigintval($content['id'])), __FUNCTION__, __LINE__, false));
753 createNewTask('{--RALLYE_ADMIN_PURGED--}: ' . $content['title'], '{--RALLYE_ADMIN_PURGED_TEXT--}', 'RALLYE_PURGED');
757 runFilterChain('run_sqls');
761 SQL_FREERESULT($result_rallye);
765 function addReferalRallyeTemplateSelection ($name = 'template', $default = '') {
770 // Check templates directory
771 $basePath = sprintf("templates/%s/html/rallye/", getLanguage());
773 // Read the directory
774 $templates = getArrayFromDirectory($basePath, 'rallye_', false, true, array(), '.tpl');
777 foreach ($templates as $read) {
778 // Cut prefix and extension away
779 $read = substr($read, 7, strpos($read, '.') - 7);
781 // Accept only template names between 1 and 255 chars length
782 if ((strlen($read) < 256) && (!empty($read))) {
783 // Valid entry found!
787 logDebugMessage(__FUNCTION__, __LINE__, sprintf("WARNING: Template %s not used.", $read));
791 // Do we have found templates which we can link with the new rallye?
792 if (!empty($ral[0])) {
793 // Generate selection box for all found templates
794 $OUT = '<select name="' . $name . '" size="1" class="admin_select">';
795 $OUT .= generateOptionList('/ARRAY/', array_keys($ral), array_values($ral), $default, '', 'none');
798 // No rallye templates found
799 $OUT = getMessage('RALLYE_NO_TEMPLATES_FOUND');
806 // @TODO Please document this function
807 function getReferalRallyeRefsCount ($currUserid, $old = '0') {
808 // Check current refs
809 if (isExtensionInstalledAndNewer('cache', '0.1.2')) {
810 // Get refs from cache
812 foreach ($GLOBALS['cache_array']['refsystem']['userid'] as $id => $userid) {
813 // Do we have a ref for this user?
814 //* DEBUG: */ print("id={$id},userid={$userid},userid={$userid},old={$old},level={$GLOBALS['cache_array']['refsystem']['level'][$id]}<br />");
815 if (($currUserid == $userid) && ($GLOBALS['cache_array']['refsystem']['level'][$id] == 1)) {
816 //* DEBUG: */ print("userid matches!<br />");
817 foreach ($GLOBALS['cache_array']['refdepths']['level'] as $level) {
818 if (($level == $GLOBALS['cache_array']['refsystem']['level'][$id]) && ($level == 1)) {
819 // Level does exist so abort here
820 $cnt = $GLOBALS['cache_array']['refsystem']['counter'][$id];
821 //* DEBUG: */ print("*".$userid.'/'.$cnt."*<br />");
823 } elseif ($level > 1) {
824 // Not interesting here...
834 //* DEBUG: */ print("<pre>");
835 //* DEBUG: */ print(print_r($GLOBALS['cache_array']['refsystem'], true));
836 //* DEBUG: */ print("</pre>");
837 //* DEBUG: */ shutdown();
841 incrementStatsEntry('cache_hits');
844 //* DEBUG: */ print('+'.$cnt.'/'.$old."+<br />");
848 // Load current refs from database
849 $result_ref = SQL_QUERY_ESC("SELECT
850 SUM(s.counter) AS cnt
852 `{?_MYSQL_PREFIX?}_refsystem` AS s
854 `{?_MYSQL_PREFIX?}_refdepths` AS d
862 ), __FUNCTION__, __LINE__);
864 // Load count @TODO Can't we rewrite this to our API?
865 list($cnt) = SQL_FETCHROW($result_ref);
868 SQL_FREERESULT($result_ref);
877 //* DEBUG: */ print("*".$userid.'/'.$old.'/'.$cnt."*<br />");
881 // Determines the right language string for min_users
882 function determineReferalRallyeMinimumUsers ($min_users) {
883 // Rallye ends without user limitation is the default
884 $return = getMessage('RALLYE_END_NO_USER_LIMITATION');
886 if ($min_users > 0) {
887 // Rallye ends when X members are totally in your exchange
888 $return = getMaskedMessage('RALLYE_END_USERS', $min_users);
895 // Determines the right language string for min_prices
896 function determineReferalRallyeMinimumPrices ($min_prices) {
897 // Rallye ends without user limitation is the default
898 $return = getMessage('RALLYE_END_NO_PRICE_LIMITATION');
900 if ($min_prices > 0) {
901 // Rallye ends when X members are totally in your exchange
902 $return = getMaskedMessage('RALLYE_END_PRICES', $min_prices);
909 // Filter for extra-autpurge
910 function FILTER_RALLYE_EXTRA_AUTOPURGE () {
911 // Check expired rallyes (hard-coded 3 days limit for displaying expired rallyes!)
912 purgeExpiredReferalRallyes();