Extension ext-network continued:
[mailer.git] / inc / libs / network_functions.php
index fd779222d06ada3348adce1a9d38e8aa25ed4f08..cca701ce44de85cef493d8412d8f373da4f26507 100644 (file)
@@ -41,8 +41,8 @@ if (!defined('__SECURITY')) {
 } // END - if
 
 // Private setter for current network id
-function setCurrentNetworkId ($id) {
-       $GLOBALS['current_network_id'] = bigintval($id);
+function setCurrentNetworkId ($networkId) {
+       $GLOBALS['current_network_id'] = bigintval($networkId);
 }
 
 // Private getter for current network id
@@ -156,20 +156,20 @@ function isNetworkApiTranslationValid ($key, $type, $networkId) {
 }
 
 // "Getter" for a network's data by provided id number
-function getNetworkDataById ($id, $column = '') {
+function getNetworkDataById ($networkId, $column = '') {
        // Ids lower one are not accepted
-       if ($id < 1) {
+       if ($networkId < 1) {
                // Not good, should be fixed
-               debug_report_bug(__FUNCTION__, __LINE__, 'Network id ' . $id . ' is smaller than 1.');
+               debug_report_bug(__FUNCTION__, __LINE__, 'Network id ' . $networkId . ' is smaller than 1.');
        } // END - if
 
        // Set current network id
-       setCurrentNetworkId($id);
+       setCurrentNetworkId($networkId);
 
        // Is it cached?
-       if (!isset($GLOBALS['network_data'][$id])) {
+       if (!isset($GLOBALS['network_data'][$networkId])) {
                // By default we have no data
-               $GLOBALS['network_data'][$id] = array();
+               $GLOBALS['network_data'][$networkId] = array();
 
                // Query for the network data
                $result = SQL_QUERY_ESC("SELECT
@@ -179,12 +179,12 @@ FROM
 WHERE
        `network_id`=%s
 LIMIT 1",
-                       array(bigintval($id)), __FUNCTION__, __LINE__);
+                       array(bigintval($networkId)), __FUNCTION__, __LINE__);
 
                // Do we have an entry?
                if (SQL_NUMROWS($result) == 1) {
                        // Then get it
-                       $GLOBALS['network_data'][$id] = SQL_FETCHARRAY($result);
+                       $GLOBALS['network_data'][$networkId] = SQL_FETCHARRAY($result);
                } // END - if
 
                // Free result
@@ -194,28 +194,28 @@ LIMIT 1",
        // Return result
        if (empty($column)) {
                // Return array
-               return $GLOBALS['network_data'][$id];
+               return $GLOBALS['network_data'][$networkId];
        } else {
                // Return column
-               return $GLOBALS['network_data'][$id][$column];
+               return $GLOBALS['network_data'][$networkId][$column];
        }
 }
 
 // "Getter" for a network's data by provided type id number
-function getNetworkDataByTypeId ($id, $column = '') {
+function getNetworkDataByTypeId ($networkId, $column = '') {
        // Ids lower one are not accepted
-       if ($id < 1) {
+       if ($networkId < 1) {
                // Not good, should be fixed
-               debug_report_bug(__FUNCTION__, __LINE__, 'Network type id ' . $id . ' is smaller than 1.');
+               debug_report_bug(__FUNCTION__, __LINE__, 'Network type id ' . $networkId . ' is smaller than 1.');
        } // END - if
 
        // Set current network id
-       setCurrentNetworkId($id);
+       setCurrentNetworkId($networkId);
 
        // Is it cached?
-       if (!isset($GLOBALS['network_data'][$id])) {
+       if (!isset($GLOBALS['network_data'][$networkId])) {
                // By default we have no data
-               $GLOBALS['network_data'][$id] = array();
+               $GLOBALS['network_data'][$networkId] = array();
 
                // Query for the network data
                $result = SQL_QUERY_ESC("SELECT
@@ -230,12 +230,12 @@ ON
 WHERE
        t.`network_type_id`=%s
 LIMIT 1",
-                       array(bigintval($id)), __FUNCTION__, __LINE__);
+                       array(bigintval($networkId)), __FUNCTION__, __LINE__);
 
                // Do we have an entry?
                if (SQL_NUMROWS($result) == 1) {
                        // Then get it
-                       $GLOBALS['network_data'][$id] = SQL_FETCHARRAY($result);
+                       $GLOBALS['network_data'][$networkId] = SQL_FETCHARRAY($result);
                } // END - if
 
                // Free result
@@ -245,23 +245,23 @@ LIMIT 1",
        // Return result
        if (empty($column)) {
                // Return array
-               return $GLOBALS['network_data'][$id];
+               return $GLOBALS['network_data'][$networkId];
        } else {
                // Return column
-               return $GLOBALS['network_data'][$id][$column];
+               return $GLOBALS['network_data'][$networkId][$column];
        }
 }
 
 // "Getter" for a network type data by provided id number
-function getNetworkTypeDataById ($id) {
+function getNetworkTypeDataById ($networkId) {
        // Ids lower one are not accepted
-       if ($id < 1) {
+       if ($networkId < 1) {
                // Not good, should be fixed
-               debug_report_bug(__FUNCTION__, __LINE__, 'Network type id ' . $id . ' is smaller than 1.');
+               debug_report_bug(__FUNCTION__, __LINE__, 'Network type id ' . $networkId . ' is smaller than 1.');
        } // END - if
 
        // By default we have no data
-       $GLOBALS['network_type_data'][$id] = array();
+       $GLOBALS['network_type_data'][$networkId] = array();
 
        // Query for the network data
        $result = SQL_QUERY_ESC("SELECT
@@ -271,27 +271,27 @@ FROM
 WHERE
        `network_type_id`=%s
 LIMIT 1",
-               array(bigintval($id)), __FUNCTION__, __LINE__);
+               array(bigintval($networkId)), __FUNCTION__, __LINE__);
 
        // Do we have an entry?
        if (SQL_NUMROWS($result) == 1) {
                // Then get it
-               $GLOBALS['network_type_data'][$id] = SQL_FETCHARRAY($result);
+               $GLOBALS['network_type_data'][$networkId] = SQL_FETCHARRAY($result);
        } // END - if
 
        // Free result
        SQL_FREERESULT($result);
 
        // Return result
-       return $GLOBALS['network_type_data'][$id];
+       return $GLOBALS['network_type_data'][$networkId];
 }
 
 // "Getter" for a network request parameter data by provided id number
-function getNetworkRequestParamsDataById ($id) {
+function getNetworkRequestParamsDataById ($networkId) {
        // Ids lower one are not accepted
-       if ($id < 1) {
+       if ($networkId < 1) {
                // Not good, should be fixed
-               debug_report_bug(__FUNCTION__, __LINE__, 'Network request parameter id ' . $id . ' is smaller than 1.');
+               debug_report_bug(__FUNCTION__, __LINE__, 'Network request parameter id ' . $networkId . ' is smaller than 1.');
        } // END - if
 
        // By default we have no data
@@ -305,7 +305,7 @@ FROM
 WHERE
        `network_param_id`=%s
 LIMIT 1",
-               array(bigintval($id)), __FUNCTION__, __LINE__);
+               array(bigintval($networkId)), __FUNCTION__, __LINE__);
 
        // Do we have an entry?
        if (SQL_NUMROWS($result) == 1) {
@@ -321,46 +321,46 @@ LIMIT 1",
 }
 
 // Updates given network (id) with data from array
-function doNetworkUpdateDataByArray ($id, $networkData) {
+function doNetworkUpdateDataByArray ($networkId, $networkData) {
        // Ids lower one are not accepted
-       if ($id < 1) {
+       if ($networkId < 1) {
                // Not good, should be fixed
-               debug_report_bug(__FUNCTION__, __LINE__, 'Network id ' . $id . ' is smaller than 1.');
+               debug_report_bug(__FUNCTION__, __LINE__, 'Network id ' . $networkId . ' is smaller than 1.');
        } // END - if
 
        // Just call our inner method
-       return adminSaveSettings($networkData, '_network_data', sprintf("`network_id`=%s", bigintval($id)), array(), false, false);
+       return adminSaveSettings($networkData, '_network_data', sprintf("`network_id`=%s", bigintval($networkId)), array(), false, false);
 }
 
 // Updates given network type handler (id) with data from array
-function doNetworkUpdateTypeByArray ($id, $networkTypeData) {
+function doNetworkUpdateTypeByArray ($networkId, $networkTypeData) {
        // Ids lower one are not accepted
-       if ($id < 1) {
+       if ($networkId < 1) {
                // Not good, should be fixed
-               debug_report_bug(__FUNCTION__, __LINE__, 'Network type handler id ' . $id . ' is smaller than 1.');
+               debug_report_bug(__FUNCTION__, __LINE__, 'Network type handler id ' . $networkId . ' is smaller than 1.');
        } // END - if
 
        // Just call our inner method
-       return adminSaveSettings($networkTypeData, '_network_types', sprintf("`network_type_id`=%s", bigintval($id)), array(), false, false);
+       return adminSaveSettings($networkTypeData, '_network_types', sprintf("`network_type_id`=%s", bigintval($networkId)), array(), false, false);
 }
 
 // Updates given network request parameters (id) with data from array
-function doNetworkUpdateParamsByArray ($id, $networkParamData) {
+function doNetworkUpdateParamsByArray ($networkId, $networkParamData) {
        // Ids lower one are not accepted
-       if ($id < 1) {
+       if ($networkId < 1) {
                // Not good, should be fixed
-               debug_report_bug(__FUNCTION__, __LINE__, 'Network request parameter id ' . $id . ' is smaller than 1.');
+               debug_report_bug(__FUNCTION__, __LINE__, 'Network request parameter id ' . $networkId . ' is smaller than 1.');
        } // END - if
 
        // Just call our inner method
-       return adminSaveSettings($networkParamData, '_network_request_params', sprintf("`network_param_id`=%s", bigintval($id)), array(), false, false);
+       return adminSaveSettings($networkParamData, '_network_request_params', sprintf("`network_param_id`=%s", bigintval($networkId)), array(), false, false);
 }
 
 // Removes given network entry
-function doAdminRemoveNetworkEntry ($table, $column, $id, $limit = 1) {
+function doAdminRemoveNetworkEntry ($table, $column, $networkId, $limit = 1) {
        // Remove the entry
        SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_network_%s` WHERE `%s`=%s LIMIT %s",
-               array($table, $column, $id, $limit), __FUNCTION__, __LINE__);
+               array($table, $column, $networkId, $limit), __FUNCTION__, __LINE__);
 
        // Return affected rows
        return SQL_AFFECTEDROWS();
@@ -495,20 +495,20 @@ ORDER BY
 }
 
 // Generator (somewhat getter) for network type options
-function generateNetworkTypeOptions ($id) {
+function generateNetworkTypeOptions ($networkId) {
        // Is this an array, then we just came back from edit/delete actions
-       if (is_array($id)) {
-               $id = '';
+       if (is_array($networkId)) {
+               $networkId = '';
        } // END - if
 
        // Is this cached?
-       if (!isset($GLOBALS[__FUNCTION__][$id])) {
+       if (!isset($GLOBALS[__FUNCTION__][$networkId])) {
                // Generate output and cache it
-               $GLOBALS[__FUNCTION__][$id] = generateOptionList(
+               $GLOBALS[__FUNCTION__][$networkId] = generateOptionList(
                        'network_types',
                        'network_type_id',
                        'network_type_handle',
-                       $id,
+                       $networkId,
                        '',
                        sprintf(
                                "WHERE `network_id`=%s",
@@ -520,7 +520,7 @@ function generateNetworkTypeOptions ($id) {
        } // END - if
 
        // Return content
-       return $GLOBALS[__FUNCTION__][$id];
+       return $GLOBALS[__FUNCTION__][$networkId];
 }
 
 // Generates an options list of all available (hard-coded) handlers
@@ -734,6 +734,42 @@ ORDER BY
        return translateNetworkTranslationName($name);
 }
 
+// Translates network API configuration status (see function isNetworkApiConfigured()) by given id
+function translateNetworkApiConfiguredStatusById ($networkId) {
+       // Do we have cache?
+       if (!isset($GLOBALS[__FUNCTION__][$networkId])) {
+               // By default it is not configured
+               $GLOBALS[__FUNCTION__][$networkId] = '{--ADMIN_NETWORK_API_NOT_CONFIGURED--}';
+
+               // So is it configured?
+               if (isNetworkApiConfigured($networkId)) {
+                       // Yes, it is
+                       $GLOBALS[__FUNCTION__][$networkId] = '{--ADMIN_NETWORK_API_CONFIGURED--}';
+               } // END - if
+       } // END - if
+
+       // Return cache
+       return $GLOBALS[__FUNCTION__][$networkId];
+}
+
+// Checks if the given network is configured by looking its API configuration entry up
+function isNetworkApiConfigured ($networkId) {
+       // Do we have cache?
+       if (!isset($GLOBALS[__FUNCTION__][$networkId])) {
+               // Check for an entry in network_api_config
+               $GLOBALS[__FUNCTION__][$networkId] = (countSumTotalData(
+                       bigintval($networkId),
+                       'network_api_config',
+                       'network_id',
+                       'network_id',
+                       true
+               ) == 1);
+       } // END - if
+
+       // Return cache
+       return $GLOBALS[__FUNCTION__][$networkId];
+}
+
 //------------------------------------------------------------------------------
 //                             Call-back functions
 //------------------------------------------------------------------------------
@@ -778,11 +814,11 @@ function doAdminNetworkProcessHandleNetwork () {
        if (ifPostContainsSelections()) {
                // Something has been selected, so start displaying one by one
                $OUT = '';
-               foreach (postRequestElement('sel') as $id => $sel) {
+               foreach (postRequestElement('sel') as $networkId => $sel) {
                        // Is this selected?
                        if ($sel == 1) {
                                // Load this network's data
-                               $networkData = getNetworkDataById($id);
+                               $networkData = getNetworkDataById($networkId);
 
                                // Do we have found the network?
                                if (count($networkData) > 0) {
@@ -830,11 +866,11 @@ function doAdminNetworkProcessHandleNetworkType () {
 
                // Something has been selected, so start displaying one by one
                $OUT = '';
-               foreach (postRequestElement('sel') as $id => $sel) {
+               foreach (postRequestElement('sel') as $networkId => $sel) {
                        // Is this selected?
                        if ($sel == 1) {
                                // Load this network's data
-                               $networkTypeData = getNetworkTypeDataById($id);
+                               $networkTypeData = getNetworkTypeDataById($networkId);
 
                                // Do we have found the network?
                                if (count($networkTypeData) > 0) {
@@ -885,11 +921,11 @@ function doAdminNetworkProcessHandleRequestParams () {
 
                // Something has been selected, so start displaying one by one
                $OUT = '';
-               foreach (postRequestElement('sel') as $id => $sel) {
+               foreach (postRequestElement('sel') as $networkId => $sel) {
                        // Is this selected?
                        if ($sel == 1) {
                                // Load this network's data
-                               $networkRequestData = getNetworkRequestParamsDataById($id);
+                               $networkRequestData = getNetworkRequestParamsDataById($networkId);
 
                                // Do we have found the network?
                                if (count($networkRequestData) > 0) {
@@ -939,7 +975,7 @@ function doAdminNetworkProcessChangeNetwork () {
                $updated = 0;
 
                // Something has been selected, so start updating them
-               foreach (postRequestElement('sel') as $id => $sel) {
+               foreach (postRequestElement('sel') as $networkId => $sel) {
                        // Update this entry?
                        if ($sel == 1) {
                                // Init data array
@@ -953,17 +989,17 @@ function doAdminNetworkProcessChangeNetwork () {
                                        } // END - if
 
                                        // Do we have this enty?
-                                       if (!isset($entry[$id])) {
+                                       if (!isset($entry[$networkId])) {
                                                // Not found, needs fixing
-                                               debug_report_bug(__FUNCTION__, __LINE__, 'No entry in key=' . $key . ', id=' . $id . ' found.');
+                                               debug_report_bug(__FUNCTION__, __LINE__, 'No entry in key=' . $key . ', id=' . $networkId . ' found.');
                                        } // END - if
 
                                        // Add this entry
-                                       $networkData[$key] = $entry[$id];
+                                       $networkData[$key] = $entry[$networkId];
                                } // END - foreach
 
                                // Update the network data
-                               $updated += doNetworkUpdateDataByArray($id, $networkData);
+                               $updated += doNetworkUpdateDataByArray($networkId, $networkData);
                        } // END - if
                } // END - foreach
 
@@ -986,11 +1022,11 @@ function doAdminNetworkProcessRemoveNetwork () {
                $removed = 0;
 
                // Something has been selected, so start updating them
-               foreach (postRequestElement('sel') as $id => $sel) {
+               foreach (postRequestElement('sel') as $networkId => $sel) {
                        // Update this entry?
                        if ($sel == 1) {
                                // Remove this entry
-                               $removed += doAdminRemoveNetworkEntry('data', 'network_id', $id);
+                               $removed += doAdminRemoveNetworkEntry('data', 'network_id', $networkId);
                        } // END - if
                } // END - foreach
 
@@ -1055,7 +1091,7 @@ function doAdminNetworkProcessChangeNetworkType () {
                $updated = 0;
 
                // Something has been selected, so start updating them
-               foreach (postRequestElement('sel') as $id => $sel) {
+               foreach (postRequestElement('sel') as $networkId => $sel) {
                        // Update this entry?
                        if ($sel == 1) {
                                // Init data array
@@ -1069,23 +1105,23 @@ function doAdminNetworkProcessChangeNetworkType () {
                                        } // END - if
 
                                        // Do we have this enty?
-                                       if (!isset($entry[$id])) {
+                                       if (!isset($entry[$networkId])) {
                                                // Not found, needs fixing
-                                               debug_report_bug(__FUNCTION__, __LINE__, 'No entry in key=' . $key . ', id=' . $id . ' found.');
+                                               debug_report_bug(__FUNCTION__, __LINE__, 'No entry in key=' . $key . ', id=' . $networkId . ' found.');
                                        } // END - if
 
                                        // Fix empty network_type_banner_url to NULL
-                                       if (($key == 'network_type_banner_url') && (trim($entry[$id]) == '')) {
+                                       if (($key == 'network_type_banner_url') && (trim($entry[$networkId]) == '')) {
                                                // Set it to NULL
-                                               $entry[$id] = NULL;
+                                               $entry[$networkId] = NULL;
                                        } // END - if
 
                                        // Add this entry
-                                       $networkTypeData[$key] = $entry[$id];
+                                       $networkTypeData[$key] = $entry[$networkId];
                                } // END - foreach
 
                                // Update the network data
-                               $updated += doNetworkUpdateTypeByArray($id, $networkTypeData);
+                               $updated += doNetworkUpdateTypeByArray($networkId, $networkTypeData);
                        } // END - if
                } // END - foreach
 
@@ -1108,7 +1144,7 @@ function doAdminNetworkProcessChangeNetworkParam () {
                $updated = 0;
 
                // Something has been selected, so start updating them
-               foreach (postRequestElement('sel') as $id => $sel) {
+               foreach (postRequestElement('sel') as $networkId => $sel) {
                        // Update this entry?
                        if ($sel == 1) {
                                // Init data array
@@ -1122,23 +1158,23 @@ function doAdminNetworkProcessChangeNetworkParam () {
                                        } // END - if
 
                                        // Do we have this enty?
-                                       if (!isset($entry[$id])) {
+                                       if (!isset($entry[$networkId])) {
                                                // Not found, needs fixing
-                                               debug_report_bug(__FUNCTION__, __LINE__, 'No entry in key=' . $key . ', id=' . $id . ' found.');
+                                               debug_report_bug(__FUNCTION__, __LINE__, 'No entry in key=' . $key . ', id=' . $networkId . ' found.');
                                        } // END - if
 
                                        // Fix empty request_param_default to NULL
-                                       if (($key == 'request_param_default') && (trim($entry[$id]) == '')) {
+                                       if (($key == 'request_param_default') && (trim($entry[$networkId]) == '')) {
                                                // Set it to NULL
-                                               $entry[$id] = NULL;
+                                               $entry[$networkId] = NULL;
                                        } // END - if
 
                                        // Add this entry
-                                       $networkParamsData[$key] = $entry[$id];
+                                       $networkParamsData[$key] = $entry[$networkId];
                                } // END - foreach
 
                                // Update the network data
-                               $updated += doNetworkUpdateParamsByArray($id, $networkParamsData);
+                               $updated += doNetworkUpdateParamsByArray($networkId, $networkParamsData);
                        } // END - if
                } // END - foreach
 
@@ -1161,11 +1197,11 @@ function doAdminNetworkProcessRemoveNetworkType () {
                $removed = 0;
 
                // Something has been selected, so start updating them
-               foreach (postRequestElement('sel') as $id => $sel) {
+               foreach (postRequestElement('sel') as $networkId => $sel) {
                        // Update this entry?
                        if ($sel == 1) {
                                // Remove this entry
-                               $removed += doAdminRemoveNetworkEntry('types', 'network_type_id', $id);
+                               $removed += doAdminRemoveNetworkEntry('types', 'network_type_id', $networkId);
                        } // END - if
                } // END - foreach
 
@@ -1188,11 +1224,11 @@ function doAdminNetworkProcessRemoveNetworkParam () {
                $removed = 0;
 
                // Something has been selected, so start updating them
-               foreach (postRequestElement('sel') as $id => $sel) {
+               foreach (postRequestElement('sel') as $networkId => $sel) {
                        // Update this entry?
                        if ($sel == 1) {
                                // Remove this entry
-                               $removed += doAdminRemoveNetworkEntry('request_params', 'network_param_id', $id);
+                               $removed += doAdminRemoveNetworkEntry('request_params', 'network_param_id', $networkId);
                        } // END - if
                } // END - foreach
 
@@ -1297,15 +1333,6 @@ function doAdminNetworkProcessAddNetworkApiTranslation () {
 
 // Adds/update network API configuration
 function doAdminNetworkProcessNetworkApiConfig () {
-       // Check for an entry in network_api_config
-       $entryCount = countSumTotalData(
-               bigintval(getRequestElement('network')),
-               'network_api_config',
-               'network_id',
-               'network_id',
-               true
-       );
-
        // Remove the 'ok' part
        unsetPostRequestElement('ok');
 
@@ -1313,7 +1340,7 @@ function doAdminNetworkProcessNetworkApiConfig () {
        setPostRequestElement('network_id', bigintval(getRequestElement('network')));
 
        // Is there already an entry?
-       if ($entryCount == 1) {
+       if (isNetworkApiConfigured(getRequestElement('network'))) {
                // Generate SQL query
                $SQL = getUpdateSqlFromArray(postRequestArray(), 'network_api_config', 'network_id', postRequestElement('network_id'), array('network_id'));
        } else {
@@ -1333,10 +1360,10 @@ function doAdminNetworkProcessNetworkApiConfig () {
        // Output message
        if (!SQL_HASZEROAFFECTED()) {
                // Successfully added
-               displayMessage('{--ADMIN_NETWORK_API_CONFIG_SAVED--}');
+               displayMessage('{--ADMIN_CONFIG_NETWORK_API_SAVED--}');
        } else {
                // Not added
-               loadTemplate('admin_settings_unsaved', false, '{--ADMIN_NETWORK_API_CONFIG_NOT_SAVED--}');
+               loadTemplate('admin_settings_unsaved', false, '{--ADMIN_CONFIG_NETWORK_API_NOT_SAVED--}');
        }
 }