Renamed ifSqlHasZeroNums() to ifSqlHasZeroNumRows() and improved some queries.
[mailer.git] / inc / modules / member / what-unconfirmed.php
index 16e247338a201aa782239a91d7ecb5a92a5b54de..b1ef76e919167274f062d112ddd6daede52349cb 100644 (file)
@@ -16,7 +16,7 @@
  * $Author::                                                          $ *
  * -------------------------------------------------------------------- *
  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
- * Copyright (c) 2009 - 2012 by Mailer Developer Team                   *
+ * Copyright (c) 2009 - 2015 by Mailer Developer Team                   *
  * For more information visit: http://mxchange.org                      *
  *                                                                      *
  * This program is free software; you can redistribute it and/or modify *
@@ -54,17 +54,26 @@ if (!isExtensionActive('mailid')) {
 // Shall we display a mail?
 if ((isGetRequestElementSet('bonusid')) && (isExtensionActive('bonus'))) {
        // Display bonus mail by loading it's full data
-       $result_data = SQL_QUERY_ESC("SELECT
-       `id`,`subject`,`timestamp`,`cat_id`,`points`,`text`,`is_notify`,`data_type`,`time`,`url`
+       $result_data = sqlQueryEscaped('SELECT
+       `id`,
+       `subject`,
+       `timestamp`,
+       `cat_id`,
+       `points`,
+       `text`,
+       `is_notify`,
+       `data_type`,
+       `time`,
+       `url`
 FROM
        `{?_MYSQL_PREFIX?}_bonus`
 WHERE
        `id`=%s
-LIMIT 1",
+LIMIT 1',
                array(bigintval(getRequestElement('bonusid'))), __FILE__, __LINE__);
 
        // Load data
-       $content = SQL_FETCHARRAY($result_data);
+       $content = sqlFetchArray($result_data);
 
        // Translate some data
        $content['timestamp'] = generateDateTime($content['timestamp'], '2');
@@ -87,35 +96,32 @@ LIMIT 1",
        } // END - if
 
        // Display it depending on mail (data) type
-       loadTemplate('member_mail_bonus_' . strtolower($content['data_type']), false, $content);
+       loadTemplate('member_mail_bonus_' . strtolower($content['data_type']), FALSE, $content);
 
        // Free result
-       SQL_FREERESULT($result_data);
+       sqlFreeResult($result_data);
 } elseif (isGetRequestElementSet('mailid')) {
        // Display regular member mail by loading its full data
-       $result_data = SQL_QUERY_ESC("SELECT
-       s.id, s.subject, p.text, s.timestamp_ordered AS `timestamp`,
-       s.cat_id, pay.price AS `points`, p.sender, pay.time, p.data_type
+       $result_data = sqlQueryEscaped("SELECT
+       `id` AS `stats_id`,
+       `pool_id`,
+       `subject`,
+       `timestamp_ordered`,
+       `cat_id`,
 FROM
-       `{?_MYSQL_PREFIX?}_user_stats` AS s
-LEFT JOIN
-       `{?_MYSQL_PREFIX?}_pool` AS p
-ON
-       s.pool_id=p.id
-LEFT JOIN
-       `{?_MYSQL_PREFIX?}_payments` AS pay
-ON
-       p.payment_id=pay.id
+       `{?_MYSQL_PREFIX?}_user_stats` AS `s`
 WHERE
-       s.id=%s
+       `id`=%s
 LIMIT 1",
                array(bigintval(getRequestElement('mailid'))), __FILE__, __LINE__);
 
        // Load data
-       $content = SQL_FETCHARRAY($result_data);
+       $content = sqlFetchArray($result_data);
+       $content = merge_array($content, getPoolDataFromId($content['pool_id']));
 
        // Translate some data
-       $content['timestamp'] = generateDateTime($content['timestamp'], '2');
+       $content['timestamp_ordered'] = generateDateTime($content['timestamp_ordered'], '2');
+       // @TODO This call cannot be saved as EL code cannot be "framed" in other EL code
        $content['userid'] = getMemberId();
        $content['user_mail_status'] = '{%message,MEMBER_MAIL_NORMAL_CONFIRMED_UNKNOWN=' . $content['id'] . '%}';
 
@@ -135,24 +141,24 @@ LIMIT 1",
        } // END - if
 
        // Display it depending on mail (data) type
-       loadTemplate('member_mail_normal_' . strtolower($content['data_type']), false, $content);
+       loadTemplate('member_mail_normal_' . strtolower($content['data_type']), FALSE, $content);
 
        // Free result
-       SQL_FREERESULT($result_data);
+       sqlFreeResult($result_data);
 }
 
 if (isExtensionActive('bonus')) {
        // Load bonus id
-       $result = SQL_QUERY_ESC("SELECT `stats_id`,`bonus_id`,`link_type` FROM `{?_MYSQL_PREFIX?}_user_links` WHERE `userid`=%s ORDER BY `bonus_id` DESC, stats_id DESC",
+       $result = sqlQueryEscaped("SELECT `stats_id`, `bonus_id`, `link_type` FROM `{?_MYSQL_PREFIX?}_user_links` WHERE `userid`=%s ORDER BY `bonus_id` DESC, stats_id DESC",
                array(getMemberId()), __FILE__, __LINE__);
 } else {
        // Don't load bonus id if ext-bonus is not installed
-       $result = SQL_QUERY_ESC("SELECT `stats_id`,`link_type` FROM `{?_MYSQL_PREFIX?}_user_links` WHERE `userid`=%s ORDER BY `stats_id` DESC",
+       $result = sqlQueryEscaped("SELECT `stats_id`, `link_type` FROM `{?_MYSQL_PREFIX?}_user_links` WHERE `userid`=%s ORDER BY `stats_id` DESC",
                array(getMemberId()), __FILE__, __LINE__);
 }
 
 // Mails left for confirmation?
-if (!SQL_HASZERONUMS($result)) {
+if (!ifSqlHasZeroNumRows($result)) {
        // Please confirm these mails!
        $sum = '0'; $OUT = '';
 
@@ -160,24 +166,28 @@ if (!SQL_HASZERONUMS($result)) {
        $content = array();
 
        // @TODO Try to rewrite this to $content = SQL_FETCHARRAY()
-       while ($row = SQL_FETCHARRAY($result)) {
+       while ($row = sqlFetchArray($result)) {
                // Load data from stats table...
                $cat = '';
-               $result_data = false;
+               $result_data = FALSE;
                $message = '{--MEMBER_GENERAL_MAIL_PROBLEM--}';
                $data = $row['stats_id'] . '/' . $row['bonus_id'] . '/' . $row['link_type'];
                switch ($row['link_type']) {
                        case 'NORMAL':
-                               $result_data = SQL_QUERY_ESC("SELECT
-       s.subject, s.timestamp_ordered, s.cat_id, s.payment_id, p.sender
+                               $result_data = sqlQueryEscaped("SELECT
+       `s`.`subject`,
+       `s`.`timestamp_ordered` AS `timestamp`,
+       `s`.`cat_id`,
+       `s`.`payment_id`,
+       `p`.`sender`
 FROM
-       `{?_MYSQL_PREFIX?}_user_stats` AS s
+       `{?_MYSQL_PREFIX?}_user_stats` AS `s`
 LEFT JOIN
-       `{?_MYSQL_PREFIX?}_pool` AS p
+       `{?_MYSQL_PREFIX?}_pool` AS `p`
 ON
-       s.pool_id=p.id
+       `s`.`pool_id`=`p`.`id`
 WHERE
-       s.id=%s
+       `s`.`id`=%s
 LIMIT 1",
                                        array(bigintval($row['stats_id'])), __FILE__, __LINE__);
                                $row['link_type'] = 'mailid';
@@ -186,7 +196,7 @@ LIMIT 1",
                                break;
 
                        case 'BONUS':
-                               $result_data = SQL_QUERY_ESC("SELECT `subject`,`timestamp`,`cat_id`,`points`, 0 FROM `{?_MYSQL_PREFIX?}_bonus` WHERE `id`=%s LIMIT 1",
+                               $result_data = sqlQueryEscaped("SELECT `subject`, `timestamp`, `cat_id`, `points`, NULL FROM `{?_MYSQL_PREFIX?}_bonus` WHERE `id`=%s LIMIT 1",
                                        array(bigintval($row['bonus_id'])), __FILE__, __LINE__);
                                $row['link_type'] = 'bonusid';
                                $data = $row['bonus_id'];
@@ -196,73 +206,74 @@ LIMIT 1",
                        default: // Unknown type detected!
                                reportBug(__FILE__, __LINE__, sprintf("Unknown mail type %s detected.", $row['link_type']));
                                break;
-               }
+               } // END - switch
 
                // Data found to this mail?
-               if ((SQL_NUMROWS($result_data) == 1) && (($row['link_type'] == 'mailid') || ($row['link_type'] == 'bonusid'))) {
+               if ((sqlNumRows($result_data) == 1) && (($row['link_type'] == 'mailid') || ($row['link_type'] == 'bonusid'))) {
                        // Mail was found
-                       list($subject, $timestamp, $cat, $pay, $sender) = SQL_FETCHROW($result_data);
+                       $rowData = sqlFetchArray($result_data);
 
                        // Subject line found?
-                       if (empty($subject)) {
+                       if (empty($rowData['subject'])) {
                                // No subject line!
-                               $subject = '{--DEFAULT_SUBJECT_LINE--}';
+                               $rowData['subject'] = '{--DEFAULT_SUBJECT--}';
                        } // END - if
 
                        // Prepare sender id
-                       if ((isValidUserId($sender)) && ($row['link_type'] == 'mailid')) {
+                       if ((isValidId($rowData['sender'])) && ($row['link_type'] == 'mailid')) {
                                // Sender id
-                               $sender = bigintval($sender);
+                               $rowData['sender'] = bigintval($rowData['sender']);
                        } elseif ($row['link_type'] == 'bonusid') {
                                // Is admin
-                               $sender = '{--USERNAME_ADMIN_SHORT--}';
+                               $rowData['sender'] = '{--USERNAME_ADMIN_SHORT--}';
                        } else {
                                // Deleted
-                               $sender = '{--EMAIL_STATUS_DELETED--}';
+                               $rowData['sender'] = '{--EMAIL_STATUS_DELETED--}';
                        }
 
                        // Prepare data for template
-                       $content = array(
-                               'data'      => bigintval($data),
+                       // @TODO cat_id is unused
+                       $rowData = array(
+                               'data'              => bigintval($data),
                                // @TODO This 'userid' cannot be saved because of encapsulated EL code
-                               'userid'    => getMemberId(),
-                               'link_type' => $row['link_type'],
-                               'subject'   => $subject,
-                               'sender'    => $sender,
-                               'timestamp' => generateDateTime($timestamp, 2),
-                               'points'    => $pay,
+                               'userid'            => getMemberId(),
+                               'link_type'         => $row['link_type'],
+                               'subject'           => $rowData['subject'],
+                               'sender'            => $rowData['sender'],
+                               'timestamp_ordered' => generateDateTime($rowData['timestamp_ordered'], 2),
+                               'payment_id'        => $rowData['payment_id'],
                        );
 
                        // Load row template
-                       if (getConfig('show_points_unconfirmed') == 'Y') {
-                               $OUT .= loadTemplate('member_unconfirmed_row', true, $content);
+                       if (isShowPointsUnconfirmedEnabled()) {
+                               $OUT .= loadTemplate('member_list_unconfirmed_row', TRUE, $rowData);
                        } else {
-                               $OUT .= loadTemplate('member_unconfirmed_row_nopoints', true, $content);
+                               $OUT .= loadTemplate('member_list_unconfirmed_nopoints_row', TRUE, $rowData);
                        }
 
-                       // Count points
-                       $sum += $pay;
+                       // Count points (price)
+                       $sum += getPaymentPrice($rowData['payment_id']);
                } else {
                        // Prepare data for template
-                       $content = array(
+                       $rowData = array(
                                'data'    => $data,
                                'message' => $message,
                        );
 
                        // Display points or not?
-                       if (getConfig('show_points_unconfirmed') == 'Y') {
-                               $OUT .= loadTemplate('member_unconfirmed_404', true, $content);
+                       if (isShowPointsUnconfirmedEnabled()) {
+                               $OUT .= loadTemplate('member_list_unconfirmed_404_row', TRUE, $rowData);
                        } else {
-                               $OUT .= loadTemplate('member_unconfirmed_404_nopoints', true, $content);
+                               $OUT .= loadTemplate('member_list_unconfirmed_nopoints_404_row', TRUE, $rowData);
                        }
                }
 
                // Free result
-               SQL_FREERESULT($result_data);
+               sqlFreeResult($result_data);
        } // END - while
 
        // Free memory
-       SQL_FREERESULT($result);
+       sqlFreeResult($result);
 
        // Remember total points
        $content['total_points'] = $sum;
@@ -271,10 +282,10 @@ LIMIT 1",
        $content['rows'] = $OUT;
 
        // Load main template
-       if (getConfig('show_points_unconfirmed') == 'Y') {
-               loadTemplate('member_unconfirmed_table', false, $content);
+       if (isShowPointsUnconfirmedEnabled()) {
+               loadTemplate('member_list_unconfirmed', FALSE, $content);
        } else {
-               loadTemplate('member_unconfirmed_table_nopoints', false, $content);
+               loadTemplate('member_list_unconfirmed_nopoints', FALSE, $content);
        }
 } else {
        // No mails left to confirm... :)