From 5da1cea4d68dfb848ff0969e4d3cd4fa85ef173b Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Thu, 11 Aug 2011 23:28:51 +0000 Subject: [PATCH] Extension ext-network continued: - Saving of network API configuration data basicly finished - Function getUpdateSqlFromArray() introduced which "converts" an array (both one- and two-dimensional) into an SQL query for updating database tables - TODOs.txt updated --- DOCS/TODOs.txt | 2 +- inc/language/network_de.php | 2 + inc/libs/admins_functions.php | 32 ++----------- inc/libs/network_functions.php | 47 ++++++++++++++++++- inc/mysql-manager.php | 8 ++-- inc/sql-functions.php | 47 +++++++++++++++++++ .../de/html/admin/admin_config_network.tpl | 2 +- 7 files changed, 106 insertions(+), 34 deletions(-) diff --git a/DOCS/TODOs.txt b/DOCS/TODOs.txt index 01cc2f6b5b..3bcfe9778d 100644 --- a/DOCS/TODOs.txt +++ b/DOCS/TODOs.txt @@ -63,7 +63,7 @@ ./inc/language/rallye_de.php:13: * @TODO Naming convention not applied for language strings * ./inc/language/refback_de.php:53: // @TODO Rewrite these constants to one ./inc/language/sponsor_de.php:117:// @TODO Rewrite these four constants to one and use sprintf() -./inc/libs/admins_functions.php:508: // @TODO This can be, somehow, rewritten +./inc/libs/admins_functions.php:486: // @TODO This can be, somehow, rewritten ./inc/libs/bonus_functions.php:194: // @TODO Move this HTML to a template ./inc/libs/doubler_functions.php:44:// @TODO Lame description ./inc/libs/doubler_functions.php:93: // @TODO Can't this be moved into EL? diff --git a/inc/language/network_de.php b/inc/language/network_de.php index 7ce05cb335..6d1b077da8 100644 --- a/inc/language/network_de.php +++ b/inc/language/network_de.php @@ -210,6 +210,8 @@ addMessages(array( 'ADMIN_CONFIG_NETWORK_TITLE' => "Einrichtung des API-Zugangs zum Werbenetzwerk %s:", 'ADMIN_CONFIG_NETWORK_SAVE_BUTTON' => "API-Konfiguration speichern", 'ADMIN_CONFIG_NETWORK_NOTE' => "Tragen Sie hier Ihre Interface-Zugangsdaten zum angzeigten Werbenetzwerk ein.", + 'ADMIN_NETWORK_API_CONFIG_SAVED' => "API-Konfiguration gespeichert.", + 'ADMIN_NETWORK_API_CONFIG_NOT_SAVED' => "API-Konfiguration NICHT gespeichert (keine Änderungen).", // Error codes 'ADMIN_NETWORK_AFF_ID_PASS_WRONG_ERROR_CODE' => "Affiliate-Id oder -Passwort falsch", 'ADMIN_NETWORK_SITE_ID_NOT_ASSIGNED_ERROR_CODE' => "Seiten-Id nicht zugewiesen/ungültig", diff --git a/inc/libs/admins_functions.php b/inc/libs/admins_functions.php index 407308222a..0fad9b6657 100644 --- a/inc/libs/admins_functions.php +++ b/inc/libs/admins_functions.php @@ -277,28 +277,7 @@ LIMIT 1", } } else { // Update whole array - $SQL = 'UPDATE `{?_MYSQL_PREFIX?}_admins` SET '; - foreach ($postData as $entry => $value) { - // Skip login/id entry - if (in_array($entry, array('login', 'id'))) { - continue; - } // END - if - - // Do we have a non-string (e.g. number, NULL, NOW() or back-tick at the beginning? - if (is_null($value[$id])) { - // NULL detected - $SQL .= '`' . $entry . '`=NULL, '; - } elseif ((bigintval($value[$id], true, false) === $value[$id]) || ($value[$id] == 'NOW()') || (substr($value[$id], 0, 1) == '`')) { - // No need for ticks (') - $SQL .= '`' . $entry . '`=' . $value[$id] . ', '; - } else { - // Strings need ticks (') around them - $SQL .= '`' . $entry . "`='" . SQL_ESCAPE($value[$id]) . "', "; - } - } // END - foreach - - // Remove last 2 chars and finish query - $SQL = substr($SQL, 0, -2) . ' WHERE `id`=%s LIMIT 1'; + $SQL = getUpdateSqlFromArray($postData, 'admins', 'id', '%s', array('login', 'id'), $id); // Run it SQL_QUERY_ESC($SQL, array(bigintval($id)), __FUNCTION__, __LINE__); @@ -314,11 +293,10 @@ LIMIT 1", } } // END - foreach - // Display message - if (!empty($message)) { - if ($displayMessage === true) { - displayMessage($message); - } // END - if + // Display message if not empty and allowed + if ((!empty($message)) && ($displayMessage === true)) { + // Display it + displayMessage($message); } // END - if // Remove cache file diff --git a/inc/libs/network_functions.php b/inc/libs/network_functions.php index f57c5831a7..60e4740d89 100644 --- a/inc/libs/network_functions.php +++ b/inc/libs/network_functions.php @@ -1268,7 +1268,7 @@ function doAdminNetworkProcessAddNetworkApiTranslation () { // Add sorting setPostRequestElement('sort', (countSumTotalData( - postRequestElement('network_id'), + bigintval(postRequestElement('network_id')), 'network_api_translation', 'network_api_id', 'network_id', @@ -1295,6 +1295,51 @@ function doAdminNetworkProcessAddNetworkApiTranslation () { } } +// Adds/update network API configuration +function doAdminNetworkProcessConfigNetwork () { + // Check for an entry in network_config + $entryCount = countSumTotalData( + bigintval(getRequestElement('network')), + 'network_config', + 'network_id', + 'network_id', + true + ); + + // Remove the 'ok' part + unsetPostRequestElement('ok'); + + // Add id + setPostRequestElement('network_id', bigintval(getRequestElement('network'))); + + // Is there already an entry? + if ($entryCount == 1) { + // Generate SQL query + $SQL = getUpdateSqlFromArray(postRequestArray(), 'network_config', 'network_id', postRequestElement('network_id'), array('network_id')); + } else { + // Insert new entry + $SQL = 'INSERT INTO + `{?_MYSQL_PREFIX?}_network_config` +( + `' . implode('`,`', array_keys(postRequestArray())) . "` +) VALUES ( + '" . implode("','", array_values(postRequestArray())) . "' +)"; + } + + // Run the query + SQL_QUERY($SQL, __FUNCTION__, __LINE__); + + // Output message + if (!SQL_HASZEROAFFECTED()) { + // Successfully added + displayMessage('{--ADMIN_NETWORK_API_CONFIG_SAVED--}'); + } else { + // Not added + loadTemplate('admin_settings_unsaved', false, '{--ADMIN_NETWORK_API_CONFIG_NOT_SAVED--}'); + } +} + // Do expression code for this extension function doExpressionNetwork ($data) { // Construct replacer diff --git a/inc/mysql-manager.php b/inc/mysql-manager.php index a9733b4ec1..ea9b130671 100644 --- a/inc/mysql-manager.php +++ b/inc/mysql-manager.php @@ -959,14 +959,14 @@ function countSumTotalData ($search, $tableName, $lookFor = 'id', $whereStatemen // Count or sum whole table? if ($countRows === true) { // Count whole table - $result = SQL_QUERY_ESC("SELECT COUNT(`%s`) AS `res` FROM `{?_MYSQL_PREFIX?}_%s`".$add, + $result = SQL_QUERY_ESC("SELECT COUNT(`%s`) AS `res` FROM `{?_MYSQL_PREFIX?}_%s`" . $add . ' LIMIT 1', array( $lookFor, $tableName ), __FUNCTION__, __LINE__); } else { // Sum whole table - $result = SQL_QUERY_ESC("SELECT SUM(`%s`) AS `res` FROM `{?_MYSQL_PREFIX?}_%s`".$add, + $result = SQL_QUERY_ESC("SELECT SUM(`%s`) AS `res` FROM `{?_MYSQL_PREFIX?}_%s`" . $add . ' LIMIT 1', array( $lookFor, $tableName @@ -975,7 +975,7 @@ function countSumTotalData ($search, $tableName, $lookFor = 'id', $whereStatemen } elseif (($countRows === true) || ($lookFor == 'userid')) { // Count rows //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'COUNT!'); - $result = SQL_QUERY_ESC("SELECT COUNT(`%s`) AS `res` FROM `{?_MYSQL_PREFIX?}_%s` WHERE `%s`%s'%s'".$add, + $result = SQL_QUERY_ESC("SELECT COUNT(`%s`) AS `res` FROM `{?_MYSQL_PREFIX?}_%s` WHERE `%s`%s'%s'" . $add . ' LIMIT 1', array( $lookFor, $tableName, @@ -986,7 +986,7 @@ function countSumTotalData ($search, $tableName, $lookFor = 'id', $whereStatemen } else { // Add all rows //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'SUM!'); - $result = SQL_QUERY_ESC("SELECT SUM(`%s`) AS `res` FROM `{?_MYSQL_PREFIX?}_%s` WHERE `%s`%s'%s'".$add, + $result = SQL_QUERY_ESC("SELECT SUM(`%s`) AS `res` FROM `{?_MYSQL_PREFIX?}_%s` WHERE `%s`%s'%s'" . $add . ' LIMIT 1', array( $lookFor, $tableName, diff --git a/inc/sql-functions.php b/inc/sql-functions.php index 6e6e3bd28d..6298ed43f4 100644 --- a/inc/sql-functions.php +++ b/inc/sql-functions.php @@ -124,5 +124,52 @@ function isSqlsValid () { ); } +// Generates an updating SQL query from given array +function getUpdateSqlFromArray ($array, $tableName, $whereColumn, $whereData, $excludedFields, $multiDimId = NULL) { + // Begin SQL query + $SQL = 'UPDATE `{?_MYSQL_PREFIX?}_' . $tableName . '` SET '; + + // Insert all data + foreach ($array as $entry => $value) { + // Skip login/id entry + if (in_array($entry, $excludedFields)) { + continue; + } // END - if + + // Do we have a non-string (e.g. number, NULL, NOW() or back-tick at the beginning? + if (is_null($multiDimId)) { + // Handle one-dimensional data + if (is_null($value)) { + // NULL detected + $SQL .= '`' . $entry . '`=NULL, '; + } elseif ((bigintval($value, true, false) === $value) || ($value == 'NOW()') || (substr($value, 0, 1) == '`')) { + // No need for ticks (') + $SQL .= '`' . $entry . '`=' . $value . ', '; + } else { + // Strings need ticks (') around them + $SQL .= '`' . $entry . "`='" . SQL_ESCAPE($value) . "', "; + } + } else { + // Handle multi-dimensional data + if (is_null($value[$multiDimId])) { + // NULL detected + $SQL .= '`' . $entry . '`=NULL, '; + } elseif ((bigintval($value[$multiDimId], true, false) === $value[$multiDimId]) || ($value[$multiDimId] == 'NOW()') || (substr($value[$multiDimId], 0, 1) == '`')) { + // No need for ticks (') + $SQL .= '`' . $entry . '`=' . $value[$multiDimId] . ', '; + } else { + // Strings need ticks (') around them + $SQL .= '`' . $entry . "`='" . SQL_ESCAPE($value[$multiDimId]) . "', "; + } + } + } // END - foreach + + // Remove last 2 chars and finish query + $SQL = substr($SQL, 0, -2) . ' WHERE `' . $whereColumn . '`=' . $whereData . ' LIMIT 1'; + + // Return SQL query + return $SQL; +} + // [EOF] ?> diff --git a/templates/de/html/admin/admin_config_network.tpl b/templates/de/html/admin/admin_config_network.tpl index 7e5997d0ef..c959ac5bb7 100644 --- a/templates/de/html/admin/admin_config_network.tpl +++ b/templates/de/html/admin/admin_config_network.tpl @@ -1,5 +1,5 @@
-
+
-- 2.39.5