Renamed ifSqlHasZeroNums() to ifSqlHasZeroNumRows() and improved some queries.
[mailer.git] / inc / modules / admin / what-list_links.php
index bd642d78185bb896a65b75555276c8ff41b12063..b0b5309e31be1d841e47d399626315a526407ecf 100644 (file)
@@ -16,8 +16,8 @@
  * $Author::                                                          $ *
  * -------------------------------------------------------------------- *
  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
- * Copyright (c) 2009 - 2011 by Mailer Developer Team                   *
- * For more information visit: http://www.mxchange.org                  *
+ * 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 *
  * it under the terms of the GNU General Public License as published by *
@@ -44,36 +44,40 @@ if ((!defined('__SECURITY')) || (!isAdmin())) {
 addYouAreHereLink('admin', __FILE__);
 
 if (!isExtensionActive('mailid')) {
-       displayMessage(generateExtensionInactiveNotInstalledMessage('mailid'));
+       displayMessage('{%pipe,generateExtensionInactiveNotInstalledMessage=mailid%}');
        return;
 } // END - if
 
-if (isGetRequestParameterSet('userid')) {
+if (isGetRequestElementSet('userid')) {
        // Check if the user already exists
-       if (fetchUserData(getRequestParameter('userid'))) {
-               // Grab user's all unconfirmed mails
-               // @TODO Try to rewrite this to a filter
-               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 `id` ASC",
-                               array(bigintval(getRequestParameter('userid'))), __FILE__, __LINE__);
-               } else {
-                       // Load stats id (2nd will be ignored later! But it is needed for the same fetchrow command)
-                       $result = SQL_QUERY_ESC("SELECT `stats_id`, `stats_id`, `link_type` FROM `{?_MYSQL_PREFIX?}_user_links` WHERE `userid`=%s ORDER BY `id` ASC",
-                               array(bigintval(getRequestParameter('userid'))), __FILE__, __LINE__);
-               }
+       if (fetchUserData(getRequestElement('userid'))) {
+               // Default is ext-bonus is not installed
+               $rowName = 'stats_id';
 
-               // Do we have some entries?
-               if (!SQL_HASZERONUMS($result)) {
+               // Is ext-bonus installed?
+               if (isExtensionActive('bonus')) {
+                       // Use bonus_id
+                       $linkDataName = 'bonus_id';
+               } // END - if
+
+               // Load user links data
+               $result = sqlQueryEscaped("SELECT `userid`, `stats_id`, `%s`, `link_type` FROM `{?_MYSQL_PREFIX?}_user_links` WHERE `userid`=%s ORDER BY `id` ASC",
+                       array(
+                               $linkDataName,
+                               bigintval(getRequestElement('userid'))
+                       ), __FILE__, __LINE__);
+
+               // Are there some entries?
+               if (!ifSqlHasZeroNumRows($result)) {
                        // Some unconfirmed mails left
-                       if (getRequestParameter('delete') == "all") {
+                       if (getRequestElement('delete') == 'all') {
                                // Delete all unconfirmed mails by this user
-                               SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_user_links` WHERE `userid`=%s LIMIT %s",
-                                       array(bigintval(getRequestParameter('userid')), SQL_NUMROWS($result)), __FILE__, __LINE__);
+                               sqlQueryEscaped("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_user_links` WHERE `userid`=%s LIMIT %s",
+                                       array(bigintval(getRequestElement('userid')), sqlNumRows($result)), __FILE__, __LINE__);
 
                                // Prepare mail and send it away
-                               $message = loadEmailTemplate('member_delete_links', SQL_NUMROWS($result), bigintval(getRequestParameter('userid')));
-                               sendEmail(getUserData('email'), '{--ADMIN_DELETE_LINK_SUBJECT--}', $message);
+                               $message = loadEmailTemplate('member_delete_links', sqlNumRows($result), bigintval(getRequestElement('userid')));
+                               sendEmail(getUserData('userid'), '{--ADMIN_DELETE_LINK_SUBJECT--}', $message);
 
                                // Display message
                                displayMessage('{--ADMIN_LINKS_DELETED--}');
@@ -82,89 +86,82 @@ if (isGetRequestParameterSet('userid')) {
                                $OUT = '';
 
                                // List all unconfirmed mails
-                               // @TODO Find a way to rewrite this. See some lines above for different queries
-                               while (list($id, $id2, $type) = SQL_FETCHROW($result)) {
-                                       // Initializes all variables
-                                       $DATA = '';
-                                       $PROBLEM = '{--ADMIN_GENERAL_MAIL_PROBLEM--}';
-                                       $result_data = false;
+                               while ($linkData = sqlFetchArray($result)) {
+                                       // Initializes all other elements
+                                       $linkData['mail_id'] = '';
+                                       $linkData['problem'] = '{--ADMIN_GENERAL_MAIL_PROBLEM--}';
+                                       $result_data = FALSE;
 
                                        // Load data from stats table...
                                        // @TODO Rewrite this to includes/filter
-                                       switch ($type) {
+                                       switch ($linkData['link_type']) {
                                                case 'NORMAL':
-                                                       $result_data = SQL_QUERY_ESC("SELECT `subject`, `timestamp_ordered` AS `timestamp`, `cat_id` FROM `{?_MYSQL_PREFIX?}_user_stats` WHERE `id`=%s LIMIT 1",
-                                                               array(bigintval($id)), __FILE__, __LINE__);
-                                                       $type = 'mailid'; $DATA = $id; $PROBLEM = '{--NORMAL_MAIL_PROBLEM--}';
-                                                       $LINK = '<a href="{%url=mailid.php?userid=' . getRequestParameter('userid') . '&amp;mailid=' . $id . '%}" target="_blank">' . $id . '</a>';
+                                                       $result_data = sqlQueryEscaped("SELECT `subject`, `timestamp_ordered` AS `timestamp`, `cat_id` FROM `{?_MYSQL_PREFIX?}_user_stats` WHERE `id`=%s LIMIT 1",
+                                                               array(bigintval($linkData['stats_id'])), __FILE__, __LINE__);
+                                                       $linkData['link_type'] = 'mailid';
+                                                       $linkData['mail_id']   = $linkData['stats_id'];
+                                                       $linkData['problem']   = '{--NORMAL_MAIL_PROBLEM--}';
                                                        break;
 
                                                case 'BONUS':
-                                                       $result_data = SQL_QUERY_ESC("SELECT `subject`, `timestamp`, `cat_id` FROM `{?_MYSQL_PREFIX?}_bonus` WHERE `id`=%s LIMIT 1",
-                                                               array(bigintval($id2)), __FILE__, __LINE__);
-                                                       $type = 'bonusid'; $DATA = $id2; $PROBLEM = '{--BONUS_MAIL_PROBLEM--}';
-                                                       $LINK = '<a href="{%url=mailid.php?userid=' . getRequestParameter('userid') . '&amp;bonusid=' . $id2 . '%}" target="_blank">' . $id2 . '</a>';
+                                                       $result_data = sqlQueryEscaped("SELECT `subject`, `timestamp`, `cat_id` FROM `{?_MYSQL_PREFIX?}_bonus` WHERE `id`=%s LIMIT 1",
+                                                               array(bigintval($linkData['bonus_id'])), __FILE__, __LINE__);
+                                                       $linkData['link_type'] = 'bonusid';
+                                                       $linkData['mail_id']   = $linkData['bonus_id'];
+                                                       $linkData['problem']   = '{--BONUS_MAIL_PROBLEM--}';
                                                        break;
 
                                                default: // Problem in application detected!
-                                                       // Log the error
-                                                       logDebugMessage(__FILE__, __LINE__, sprintf("Invalid email type %s detected.", $type));
+                                                       reportBug(__FILE__, __LINE__, sprintf('Invalid email type %s detected.', $linkData['link_type']));
                                                        break;
                                        } // END - switch
 
-                                       // Do we have an entry?
-                                       if (SQL_NUMROWS($result_data) == 1) {
+                                       // Is there an entry?
+                                       if (sqlNumRows($result_data) == 1) {
                                                // Mail was found
-                                               $content = SQL_FETCHARRAY($result_data);
+                                               $mailData = merge_array($linkData, sqlFetchArray($result_data));
 
                                                // Fix empty subject
-                                               if (empty($content['subject'])) $content['subject'] = '{--DEFAULT_SUBJECT_LINE--}';
+                                               if (empty($mailData['subject'])) {
+                                                       // The subject line is empty so we use the default
+                                                       $mailData['subject'] = '{--DEFAULT_SUBJECT--}';
+                                               } // END - if
 
                                                // Prepare data for the row template
-                                               $content = array(
-                                                       'link'      => $LINK,
-                                                       'subject'   => $content['subject'],
-                                                       'timestamp' => generateDateTime($content['timestamp'], 0),
-                                                       'cat_id'    => $content['cat_id'],
-                                               );
+                                               $mailData['timestamp'] = generateDateTime($mailData['timestamp'], 0);
 
                                                // Load row template
-                                               $OUT .= loadTemplate('admin_list_links_row', true, $content);
+                                               $OUT .= loadTemplate('admin_list_links_row', TRUE, $mailData);
                                        } else {
                                                // Load template for error
-                                               $OUT .= loadTemplate('admin_list_links_problem', true,
-                                                       array(
-                                                               'problem'       => $PROBLEM,
-                                                               'data'          => $DATA
-                                                       )
-                                               );
+                                               $OUT .= loadTemplate('admin_list_links_problem', TRUE, $linkData);
                                        }
 
                                        // Free result
-                                       SQL_FREERESULT($result_data);
+                                       sqlFreeResult($result_data);
                                } // END - while
 
                                // Remember list in constant for the template
-                               $content['surname'] = getUserData('surname');
-                               $content['family']  = getUserData('family');
-                               $content['email']   = '<a href="' . generateEmailLink(getUserData('email'), 'user_data') . '">' . getUserData('email') . '</a>';
-                               $content['rows']    = $OUT;
-                               $content['nums']    = SQL_NUMROWS($result);
-                               $content['userid']  = bigintval(getRequestParameter('userid'));
+                               $content = array(
+                                       'email'   => '<a href="' . generateEmailLink(getUserData('email'), 'user_data') . '">' . getUserData('email') . '</a>',
+                                       'rows'    => $OUT,
+                                       'nums'    => sqlNumRows($result),
+                                       'userid'  => bigintval(getRequestElement('userid')),
+                               );
 
                                // Free memory
-                               SQL_FREERESULT($result);
+                               sqlFreeResult($result);
 
                                // Load final template
-                               loadTemplate('admin_list_links', false, $content);
+                               loadTemplate('admin_list_links', FALSE, $content);
                        }
                } else {
                        // No mails left to confirm
-                       displayMessage('{%message,ADMIN_MEMBER_LINKS=' . bigintval(getRequestParameter('userid')) . '%}');
+                       displayMessage('{%message,ADMIN_NO_UNCONFIRMED_MAILS_LEFT=' . bigintval(getRequestElement('userid')) . '%}');
                }
        } else {
                // User not found
-               displayMessage('{%message,ADMIN_MEMBER_404=' . bigintval(getRequestParameter('userid')) . '%}');
+               displayMessage('{%message,ADMIN_MEMBER_404=' . bigintval(getRequestElement('userid')) . '%}');
        }
 } else {
        // Output selection form with all confirmed user accounts listed