Deleting of network type handler partialy finished
[mailer.git] / inc / libs / network_functions.php
index af436af..b97060e 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /************************************************************************
- * MXChange v0.2.1                                    Start: 11/04/2009 *
- * ===============                              Last change: 11/04/2009 *
+ * Mailer v0.2.1-FINAL                                Start: 11/04/2009 *
+ * ===================                          Last change: 11/04/2009 *
  *                                                                      *
  * -------------------------------------------------------------------- *
  * File              : network_functions.php                            *
  * -------------------------------------------------------------------- *
  * Kurzbeschreibung  : Funktionen fuer ext-network                      *
  * -------------------------------------------------------------------- *
- * $Revision:: 1194                                                   $ *
- * $Date:: 2009-10-27 18:24:18 +0100 (Tue, 27 Oct 2009)               $ *
+ * $Revision::                                                        $ *
+ * $Date::                                                            $ *
  * $Tag:: 0.2.1-FINAL                                                 $ *
- * $Author:: quix0r                                                   $ *
+ * $Author::                                                          $ *
  * Needs to be in all Files and every File needs "svn propset           *
  * svn:keywords Date Revision" (autoprobset!) at least!!!!!!            *
  * -------------------------------------------------------------------- *
@@ -41,10 +41,25 @@ 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?
-       if ((!isFormSent()) && (!isPostRequestElementSet('edit')) && (!isPostRequestElementSet('del'))) {
+       if ((!isFormSent()) && (!isPostRequestElementSet('edit')) && (!isPostRequestElementSet('del')) && (!isPostRequestElementSet('change')) && (!isPostRequestElementSet('remove'))) {
                // Abort here
                loadTemplate('admin_settings_saved', false, getMessage('ADMIN_NETWORK_FORM_NOT_SENT'));
                return;
@@ -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,98 @@ 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
+       if ($id < 1) {
+               // Not good, should be fixed
+               debug_report_bug('Network id ' . $id . ' 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);
+}
+
+// Removes given network entry
+function doAdminRemoveNetworkEntry ($table, $column, $id, $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), __FILE__, __LINE__);
+
+       // Return affected rows
+       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
 //------------------------------------------------------------------------------
@@ -126,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
@@ -143,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) {
@@ -151,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')));
        }
 }
 
@@ -161,7 +284,7 @@ function doAdminNetworkProcessHandlenetworkForm () {
        if (countPostSelection() > 0) {
                // Something has been selected, so start displaying one by one
                $SW = 2; $OUT = '';
-               foreach (postRequestElement('sel') as $id=>$sel) {
+               foreach (postRequestElement('sel') as $id => $sel) {
                        // Is this selected?
                        if ($sel == 1) {
                                // Load this network's data
@@ -172,11 +295,32 @@ function doAdminNetworkProcessHandlenetworkForm () {
                                        // Add color
                                        $networkData['sw'] = $SW;
 
-                                       // Make selection box for network_request_type
-                                       $networkData['network_request_type'] = addSelectionBox('network_request_type', $networkData['network_request_type'], '', 0, 'admin_select');
+                                       if (isPostRequestElementSet('edit')) {
+                                               // Make selection box for network_request_type
+                                               $networkData['network_request_type'] = generateOptionList(
+                                                       '/ARRAY/',
+                                                       array('GET','POST'),
+                                                       array(
+                                                               getMessage('ADMIN_NETWORK_REQUEST_TYPE_GET'),
+                                                               getMessage('ADMIN_NETWORK_REQUEST_TYPE_POST')
+                                                       ),
+                                                       $networkData['network_request_type']
+                                               );
+
+                                               // Add row template for editing
+                                               $OUT .= loadTemplate('admin_edit_networks_row', true, $networkData);
+                                       } elseif (isPostRequestElementSet('del')) {
+                                               // Translate the request type
+                                               $networkData['network_request_type'] = getMessage('ADMIN_NETWORK_REQUEST_TYPE_' . $networkData['network_request_type']);
 
-                                       // Add row template and switch color
-                                       $OUT .= loadTemplate('admin_edit_networks_row', true, $networkData);
+                                               // Add row template for deleting
+                                               $OUT .= loadTemplate('admin_del_networks_row', true, $networkData);
+                                       } else {
+                                               // Problem!
+                                               debug_report_bug('Cannot detect edit/del.');
+                                       }
+
+                                       // Switch colors
                                        $SW = 3 - $SW;
                                } // END - if
                        } // END - if
@@ -185,7 +329,14 @@ function doAdminNetworkProcessHandlenetworkForm () {
                // If we have no rows, we don't need to display the edit form
                if (!empty($OUT)) {
                        // Output main template
-                       loadTemplate('admin_edit_networks', false, $OUT);
+                       if (isPostRequestElementSet('edit')) {
+                               loadTemplate('admin_edit_networks', false, $OUT);
+                       } elseif (isPostRequestElementSet('del')) {
+                               loadTemplate('admin_del_networks', false, $OUT);
+                       } else {
+                               // Problem!
+                               debug_report_bug('Cannot detect edit/del.');
+                       }
 
                        // Don't display the list/add new form
                        $GLOBALS['network_display'] = false;
@@ -196,5 +347,180 @@ 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?
+       if (countPostSelection() > 0) {
+               // By default nothing is updated
+               $updated = 0;
+
+               // Something has been selected, so start updating them
+               foreach (postRequestElement('sel') as $id => $sel) {
+                       // Update this entry?
+                       if ($sel == 1) {
+                               // Init data array
+                               $networkData = array();
+
+                               // Transfer whole array, except 'sel'
+                               foreach (postRequestArray() as $key => $entry) {
+                                       // Skip 'sel' and submit button
+                                       if (in_array($key, array('sel', 'change'))) continue;
+
+                                       // Do we have this enty?
+                                       if (!isset($entry[$id])) {
+                                               // Not found, needs fixing
+                                               debug_report_bug('No entry in key=' . $key . ', id=' . $id . ' found.');
+                                       } // END - if
+
+                                       // Add this entry
+                                       $networkData[$key] = $entry[$id];
+                               } // END - foreach
+
+                               // Update the network data
+                               $updated += doNetworkUpdateDataByArray($id, $networkData);
+                       } // END - if
+               } // END - foreach
+
+               // Do we have updates?
+               if ($updated > 0) {
+                       // Updates done
+                       loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_NETWORK_UPDATED', $updated));
+               } else {
+                       // Nothing changed
+                       loadTemplate('admin_settings_saved', false, getMessage('ADMIN_NETWORK_NOTHING_CHANGED'));
+               }
+       } // END - if
+}
+
+// Removes given networks
+function doAdminNetworkProcessRemovenetworkForm () {
+       // Do we have selections?
+       if (countPostSelection() > 0) {
+               // By default nothing is removed
+               $removed = 0;
+
+               // Something has been selected, so start updating them
+               foreach (postRequestElement('sel') as $id => $sel) {
+                       // Update this entry?
+                       if ($sel == 1) {
+                               // Remove this entry
+                               $removed += doAdminRemoveNetworkEntry('data', 'network_id', $id);
+                       } // END - if
+               } // END - foreach
+
+               // Do we have removes?
+               if ($removed > 0) {
+                       // Removals done
+                       loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_NETWORK_REMOVED', $removed));
+               } else {
+                       // Nothing removed
+                       loadTemplate('admin_settings_saved', false, getMessage('ADMIN_NETWORK_NOTHING_REMOVED'));
+               }
+       } // 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]
 ?>