Deleting of network type handler partialy finished
[mailer.git] / inc / libs / network_functions.php
index e90f634..b97060e 100644 (file)
@@ -41,6 +41,21 @@ if (!defined('__SECURITY')) {
        die();
 } // END - if
 
+// Handle a (maybe) sent form here
+function doNetworkHandleForm () {
+       // Was the form sent?
+       if ((isFormSent()) || (isPostRequestElementSet('edit')) || (isPostRequestElementSet('del')) || (isPostRequestElementSet('change')) || (isPostRequestElementSet('remove'))) {
+               // Do we have a 'do'?
+               if (isGetRequestElementSet('do')) {
+                       // Process the request
+                       doAdminNetworkProcessForm();
+               } else {
+                       // No 'do' found
+                       loadTemplate('admin_settings_saved', false, getMessage('ADMIN_NETWORK_DO_404'));
+               }
+       } // END - if
+}
+
 // Processes an admin form
 function doAdminNetworkProcessForm () {
        // Form really sent?
@@ -60,7 +75,7 @@ function doAdminNetworkProcessForm () {
        // Is the function valid?
        if (!function_exists($functionName)) {
                // Invalid function name
-               debug_report_bug('Invalid do ' . getRequestElement('do') . ', function ' . $functionName .' does not exist.');
+               debug_report_bug('Invalid do ' . getRequestElement('do') . ', function ' . $functionName .' does not exist.', false);
        } // END - if
 
        // Call-back the method handling our request
@@ -83,6 +98,22 @@ function isNetworkNameValid ($name) {
        return $isValid;
 }
 
+// Checks wether the given network type is already used (valid)
+function isNetworkTypeHandleValid ($type, $networkId) {
+       // Query for it
+       $result = SQL_QUERY_ESC("SELECT `network_type_id` FROM `{?_MYSQL_PREFIX?}_network_types` WHERE `network_id`=%s AND `network_type_handle`='%s' LIMIT 1",
+               array($networkId, $type), __FUNCTION__, __LINE__);
+
+       // Does it exist?
+       $isValid = (SQL_NUMROWS($result) == 1);
+
+       // Free result
+       SQL_FREERESULT($result);
+
+       // Return result
+       return $isValid;
+}
+
 // "Getter" for a network's data by provided id number
 function getNetworkDataById ($id) {
        // Ids lower one are not accepted
@@ -117,6 +148,40 @@ LIMIT 1",
        return $networkData;
 }
 
+// "Getter" for a network type data by provided id number
+function getNetworkTypeDataById ($id) {
+       // Ids lower one are not accepted
+       if ($id < 1) {
+               // Not good, should be fixed
+               debug_report_bug('Network type id ' . $id . ' is smaller than 1.');
+       } // END - if
+
+       // By default we have no data
+       $networkTypeData = array();
+
+       // Query for the network data
+       $result = SQL_QUERY_ESC("SELECT
+       `network_type_id`, `network_id`, `network_type_handle`, `network_type_api_url`, `network_type_click_url`, `network_type_banner_url`
+FROM
+       `{?_MYSQL_PREFIX?}_network_types`
+WHERE
+       `network_type_id`=%s
+LIMIT 1",
+               array(bigintval($id)), __FUNCTION__, __LINE__);
+
+       // Do we have an entry?
+       if (SQL_NUMROWS($result) == 1) {
+               // Then get it
+               $networkTypeData = SQL_FETCHARRAY($result);
+       } // END - if
+
+       // Free result
+       SQL_FREERESULT($result);
+
+       // Return result
+       return $networkTypeData;
+}
+
 // Updates given network (id) with data from array
 function doNetworkUpdateDataByArray ($id, $networkData) {
        // Ids lower one are not accepted
@@ -139,6 +204,42 @@ function doAdminRemoveNetworkEntry ($table, $column, $id, $limit = 1) {
        return SQL_AFFECTEDROWS();
 }
 
+// Generates a list of networks for given script and returns it
+function generateAdminNetworkList () {
+       // Init output
+       $content = '';
+
+       // Query for all networks
+       $result = SQL_QUERY("SELECT
+       `network_id`, `network_title`
+FROM
+       `{?_MYSQL_PREFIX?}_network_data`
+ORDER BY
+       `network_title` ASC", __FILE__, __LINE__);
+
+       // Do we have entries?
+       if (SQL_NUMROWS($result) > 0) {
+               // List all entries
+               $rows = array();
+               while ($row = SQL_FETCHARRAY($result)) {
+                       // Is this valid, then add it
+                       if ((is_array($row)) && (isset($row['network_id']))) $rows[] = $row;
+               } // END - while
+
+               // Generate the selection box
+               $content = generateSelectionBoxFromArray($rows, 'network', 'network_id', 'network_title');
+       } else {
+               // Nothing selected
+               $content = loadTemplate('admin_settings_saved', false, getMessage('ADMIN_ENTRIES_404'));
+       }
+
+       // Free the result
+       SQL_FREERESULT($result);
+
+       // Return the list
+       return $content;
+}
+
 //------------------------------------------------------------------------------
 //                             Call-back functions
 //------------------------------------------------------------------------------
@@ -148,8 +249,8 @@ function doAdminNetworkProcessAddnetworkForm () {
        // We can say here, the form is sent, so check if the network is already added
        if (isNetworkNameValid(postRequestElement('network_short_name'))) {
                // Already there
-               loadTemplate('admin_settings_saved', false, sprintf(getMessage('ADMIN_NETWORK_ALREADY_ADDED'), postRequestElement('network_short_name')));
-               return;
+               loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_NETWORK_ALREADY_ADDED', postRequestElement('network_short_name')));
+               return false;
        } // END - if
 
        // Remove the 'ok' part
@@ -165,7 +266,7 @@ function doAdminNetworkProcessAddnetworkForm () {
 )", __FUNCTION__, __LINE__);
 
        // Add the id for output only
-       setRequestPostElement('network_id', SQL_INSERTID());
+       setPostRequestElement('network_id', SQL_INSERTID());
 
        // Output message
        if (SQL_AFFECTEDROWS() == 1) {
@@ -173,7 +274,7 @@ function doAdminNetworkProcessAddnetworkForm () {
                loadTemplate('admin_network_added', false, postRequestArray());
        } else {
                // Not added
-               loadTemplate('admin_settings_saved', false, sprintf(getMessage('ADMIN_NETWORK_DATA_NOT_ADDED'), postRequestElement('network_short_name')));
+               loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_NETWORK_DATA_NOT_ADDED', postRequestElement('network_short_name')));
        }
 }
 
@@ -199,7 +300,10 @@ function doAdminNetworkProcessHandlenetworkForm () {
                                                $networkData['network_request_type'] = generateOptionList(
                                                        '/ARRAY/',
                                                        array('GET','POST'),
-                                                       array(getMessage('ADMIN_NETWORK_REQUEST_TYPE_GET'), getMessage('ADMIN_NETWORK_REQUEST_TYPE_POST')),
+                                                       array(
+                                                               getMessage('ADMIN_NETWORK_REQUEST_TYPE_GET'),
+                                                               getMessage('ADMIN_NETWORK_REQUEST_TYPE_POST')
+                                                       ),
                                                        $networkData['network_request_type']
                                                );
 
@@ -243,6 +347,73 @@ function doAdminNetworkProcessHandlenetworkForm () {
        } // END - if
 }
 
+// Handle network type form
+function doAdminNetworkProcessHandlenetworktypeForm () {
+       // Do we have selections?
+       if (countPostSelection() > 0) {
+               // Load network data
+               $networkData = getNetworkDataById(getRequestElement('network'));
+
+               // Something has been selected, so start displaying one by one
+               $SW = 2; $OUT = '';
+               foreach (postRequestElement('sel') as $id => $sel) {
+                       // Is this selected?
+                       if ($sel == 1) {
+                               // Load this network's data
+                               $networkTypeData = getNetworkTypeDataById($id);
+
+                               // Do we have found the network?
+                               if (count($networkTypeData) > 0) {
+                                       // Add color
+                                       $networkTypeData['sw'] = $SW;
+
+                                       if (isPostRequestElementSet('edit')) {
+                                               // Add row template for deleting
+                                               $OUT .= loadTemplate('admin_edit_network_types_row', true, $networkTypeData);
+                                       } elseif (isPostRequestElementSet('del')) {
+                                               // Fix empty banner URL
+                                               if (trim($networkTypeData['network_type_banner_url']) == '') $networkTypeData['network_type_banner_url'] = '---';
+
+                                               // Add row template for deleting
+                                               $OUT .= loadTemplate('admin_del_network_types_row', true, $networkTypeData);
+                                       } else {
+                                               // Problem!
+                                               debug_report_bug('Cannot detect edit/del.');
+                                       }
+
+                                       // Switch colors
+                                       $SW = 3 - $SW;
+                               } // END - if
+                       } // END - if
+               } // END - foreach
+
+               // If we have no rows, we don't need to display the edit form
+               if (!empty($OUT)) {
+                       // Prepare content for template
+                       $content = array(
+                               'rows' => $OUT,
+                               'network_data' => getNetworkDataById(getRequestElement('network'))
+                       );
+
+                       // Output main template
+                       if (isPostRequestElementSet('edit')) {
+                               loadTemplate('admin_edit_network_types', false, $content);
+                       } elseif (isPostRequestElementSet('del')) {
+                               loadTemplate('admin_del_network_types', false, $content);
+                       } else {
+                               // Problem!
+                               debug_report_bug('Cannot detect edit/del.');
+                       }
+
+                       // Don't display the list/add new form
+                       $GLOBALS['network_display'] = false;
+               } else {
+                       // Nothing selected/found
+                       loadTemplate('admin_settings_saved', false, getMessage('ADMIN_NETWORK_TYPE_NOTHING_FOUND'));
+               }
+       } // END - if
+}
+
 // Changes given networks
 function doAdminNetworkProcessChangenetworkForm () {
        // Do we have selections?
@@ -280,7 +451,7 @@ function doAdminNetworkProcessChangenetworkForm () {
                // Do we have updates?
                if ($updated > 0) {
                        // Updates done
-                       loadTemplate('admin_settings_saved', false, sprintf(getMessage('ADMIN_NETWORK_UPDATED'), $updated));
+                       loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_NETWORK_UPDATED', $updated));
                } else {
                        // Nothing changed
                        loadTemplate('admin_settings_saved', false, getMessage('ADMIN_NETWORK_NOTHING_CHANGED'));
@@ -307,7 +478,7 @@ function doAdminNetworkProcessRemovenetworkForm () {
                // Do we have removes?
                if ($removed > 0) {
                        // Removals done
-                       loadTemplate('admin_settings_saved', false, sprintf(getMessage('ADMIN_NETWORK_REMOVED'), $removed));
+                       loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_NETWORK_REMOVED', $removed));
                } else {
                        // Nothing removed
                        loadTemplate('admin_settings_saved', false, getMessage('ADMIN_NETWORK_NOTHING_REMOVED'));
@@ -315,5 +486,41 @@ function doAdminNetworkProcessRemovenetworkForm () {
        } // END - if
 }
 
+// Add a network type if not yet found
+function doAdminNetworkProcessAddnetworktypeForm () {
+       // Is the network type handle already used with given network?
+       if (isNetworkTypeHandleValid(postRequestElement('network_type_handle'), getRequestElement('network'))) {
+               // Already added
+               loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_NETWORK_TYPE_HANDLE_ALREADY_ADDED', postRequestElement('network_type_handle')));
+
+               // ... so abort here
+               return false;
+       } // END - if
+
+       // Remove the 'ok' part
+       unsetPostRequestElement('ok');
+
+       // Add id
+       setPostRequestElement('network_id', getRequestElement('network'));
+
+       // Add the whole request to database
+       SQL_QUERY("INSERT INTO
+       `{?_MYSQL_PREFIX?}_network_types`
+(
+       `" . implode('`,`', array_keys(postRequestArray())) . "`
+) VALUES (
+       '" . implode("','", array_values(postRequestArray())) . "'
+)", __FUNCTION__, __LINE__);
+
+       // Output message
+       if (SQL_AFFECTEDROWS() == 1) {
+               // Successfully added
+               loadTemplate('admin_network_type_added', false, postRequestArray());
+       } else {
+               // Not added
+               loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_NETWORK_TYPE_NOT_ADDED', postRequestElement('network_type_handle')));
+       }
+}
+
 // [EOF]
 ?>