Deleting of network type handler partialy finished
[mailer.git] / inc / libs / network_functions.php
index e173bad283bed37d2914c517f99cf0b91bee4d90..b97060e9d45387c3f771c52848e1861c5b6b6f07 100644 (file)
@@ -98,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
@@ -132,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
@@ -200,7 +250,7 @@ function doAdminNetworkProcessAddnetworkForm () {
        if (isNetworkNameValid(postRequestElement('network_short_name'))) {
                // Already there
                loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_NETWORK_ALREADY_ADDED', postRequestElement('network_short_name')));
-               return;
+               return false;
        } // END - if
 
        // Remove the 'ok' part
@@ -216,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) {
@@ -250,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']
                                                );
 
@@ -294,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?
@@ -366,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]
 ?>