Renamed ifSqlHasZeroNums() to ifSqlHasZeroNumRows() and improved some queries.
[mailer.git] / inc / modules / member / what-categories.php
index be91ee3a2cd0366d7c476e9712816a95f549442c..d1ddf06e1feeea0cb8cfed22275d33a37f0765c7 100644 (file)
@@ -16,8 +16,8 @@
  * $Author::                                                          $ *
  * -------------------------------------------------------------------- *
  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
- * Copyright (c) 2009, 2010 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 *
@@ -37,7 +37,7 @@
 
 // Some security stuff...
 if (!defined('__SECURITY')) {
-       die();
+       exit();
 } elseif (!isMember()) {
        redirectToIndexMemberOnlyModule();
 }
@@ -49,20 +49,27 @@ $whereStatement = " WHERE `visible`='Y'";
 if (isAdmin()) $whereStatement = '';
 
 // Get all categories
-$result = SQL_QUERY("SELECT `id`, `cat` FROM `{?_MYSQL_PREFIX?}_cats`".$whereStatement." ORDER BY `sort` ASC", __FILE__, __LINE__);
-
-// Do we have entries?
-if (!SQL_HASZERONUMS($result)) {
-       $LEAST = false;
+$result = sqlQuery('SELECT
+       `id`,
+       `cat`
+FROM
+       `{?_MYSQL_PREFIX?}_cats`
+' . $whereStatement . '
+ORDER BY
+       `sort` ASC', __FILE__, __LINE__);
+
+// Are there entries?
+if (!ifSqlHasZeroNumRows($result)) {
+       $LEAST = FALSE;
        if (isFormSent()) {
                $count = '0';
-               foreach (postRequestParameter('cat') as $cat => $joined) {
+               foreach (postRequestElement('cat') as $categoryId => $joined) {
                        if ($joined != 'Y') $count++;
                } // END - foreach
 
-               if ((SQL_NUMROWS($result) - $count) < getLeastCats()) {
-                       unsetPostRequestParameter('ok');
-                       $LEAST = true;
+               if ((sqlNumRows($result) - $count) < getLeastCats()) {
+                       unsetPostRequestElement('ok');
+                       $LEAST = TRUE;
                } // END - if
        } // END - if
 
@@ -71,99 +78,86 @@ if (!SQL_HASZERONUMS($result)) {
                // Start counting all
                $count = '0';
 
-               // Go through all entries
-               foreach (postRequestParameter('cat') as $cat => $joined) {
-                       // Ini sql here
-                       $sql = '';
+               // Ini SQLs here
+               initSqls();
 
+               // Go through all entries
+               foreach (postRequestElement('cat') as $categoryId => $joined) {
                        // Has the user joined on this category?
                        switch ($joined) {
                                case 'Y':
-                                       $result_user = SQL_QUERY_ESC("SELECT `id` FROM `{?_MYSQL_PREFIX?}_user_cats` WHERE `userid`=%s AND `cat_id`=%s LIMIT 1",
-                                               array(getMemberId(), bigintval($cat)), __FILE__, __LINE__);
-
-                                       // Debug message
-                                       //* DEBUG: */ logDebugMessage(__FILE__, __LINE__, 'cat='.$cat.',joined='.$joined.',getMemberId()='.getMemberId().',SQL_HASZERONUMS()='.intval(SQL_HASZERONUMS($result_user)));
-
-                                       if (SQL_HASZERONUMS($result_user)) {
-                                               $sql = "INSERT INTO `{?_MYSQL_PREFIX?}_user_cats` (`userid`, `cat_id`) VALUES (%s,%s)";
+                                       // Check if this category has an entry
+                                       if (countSumTotalData(getMemberId(), 'user_cats', 'id', 'userid', TRUE, sprintf(' AND `cat_id`=%s', bigintval($categoryId))) == 0) {
+                                               // No, so add it
+                                               addSql(sprintf('INSERT INTO `{?_MYSQL_PREFIX?}_user_cats` (`userid`, `cat_id`) VALUES (%s,%s)', getMemberId(), bigintval($categoryId)));
                                        } // END - if
-
-                                       // Free memory
-                                       SQL_FREERESULT($result_user);
                                        break;
 
                                case 'N':
-                                       $sql = "DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_user_cats` WHERE `userid`=%s AND `cat_id`=%s LIMIT 1";
+                                       addSql(sprintf('DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_user_cats` WHERE `userid`=%s AND `cat_id`=%s LIMIT 1', getMemberId(), bigintval($categoryId)));
                                        break;
                        } // END - switch
-
-                       if (!empty($sql)) {
-                               // Run SQL command
-                               SQL_QUERY_ESC(trim($sql), array(getMemberId(), bigintval($cat)), __FILE__, __LINE__);
-
-                               // Count this row
-                               $count += SQL_AFFECTEDROWS();
-                       } // END - if
                } // END - foreach
 
+               // Are there entries?
+               if (countSqls() > 0) {
+                       // Run SQL commands
+                       $count = runFilterChain('run_sqls');
+               } // END - if
+
                // Categories saved?
                if ($count > 0) {
                        // Output message
-                       loadTemplate('admin_settings_saved', false, '{--MEMBER_CATEGORIES_SAVED--}');
+                       displayMessage('{--MEMBER_CATEGORIES_SAVED--}');
                } else {
                        // None save
-                       loadTemplate('admin_settings_saved', false, '{--MEMBER_CATEGORIES_NOT_SAVED--}');
+                       displayMessage('{--MEMBER_CATEGORIES_NOT_SAVED--}');
                }
        } else {
-               if ($LEAST === true) {
+               if ($LEAST === TRUE) {
                        // Also here we have to secure it... :(
-                       loadTemplate('admin_settings_saved', false, '{--CHOOSE_MORE_CATEGORIES--}');
+                       displayMessage('{--CHOOSE_MORE_CATEGORIES--}');
                } // END - if
 
-               // Put some data into constants for the template
-               $content['rows'] = (SQL_NUMROWS($result) * 2 + 4);
-
-               // Load header template
-               loadTemplate('member_cats_header', false, $content);
-
-               // Start switching colors and load all visible categories
-               // @TODO Rewrite this to use $OUT .= ...
+               // Load all categories of this member
                $OUT = '';
-               while ($content = SQL_FETCHARRAY($result)) {
-                       // Default he has not joined
+               while ($content = sqlFetchArray($result)) {
+                       // Default he has not joined, add color switch
                        $content['jn'] = ' checked="checked"';
                        $content['jy'] = '';
 
                        // When we found an entry don't read it, just change the jx elements
-                       if ((isFormSent()) && (isPostRequestParameterSet('cat'))) {
+                       if ((isFormSent()) && (isPostRequestElementSet('cat'))) {
                                // Form sent?
-                               if (postRequestParameter('cat', $content['id']) =='Y') {
+                               if (postRequestElement('cat', $content['id']) =='Y') {
                                        $content['jy'] = ' checked="checked"';
                                        $content['jn'] = '';
                                } // END - if
                        } else {
                                // Check if he has an entry
-                               if (countSumTotalData(getMemberId(), 'user_cats', 'id', 'userid', true, sprintf(" AND `cat_id`=%s", bigintval($content['id']))) == 1) {
+                               if (countSumTotalData(getMemberId(), 'user_cats', 'id', 'userid', TRUE, sprintf(' AND `cat_id`=%s', bigintval($content['id']))) == 1) {
                                        $content['jn'] = '';
                                        $content['jy'] = ' checked="checked"';
                                } // END - if
                        }
 
-                       // Load row template and switch colors
-                       loadTemplate('member_cat_row', false, $content);
+                       // Load row template
+                       $OUT .= loadTemplate('member_cats_row', TRUE, $content);
                } // END - while
 
+               // Free result
+               sqlFreeResult($result);
+
                // Load footer template
-               loadTemplate('member_cats_footer');
+               loadTemplate('member_cats', FALSE, $OUT);
        }
 } else {
        // No cateogries are defined yet
-       loadTemplate('admin_settings_saved', false, '{--MEMBER_NO_CATEGORIES--}');
+       displayMessage('{--MEMBER_NO_CATEGORIES--}');
 }
 
 // Free result
-SQL_FREERESULT($result);
+sqlFreeResult($result);
 
 // [EOF]
 ?>