Extension ext-network continued, saving NULL entries fixed:
[mailer.git] / inc / libs / network_functions.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                Start: 11/04/2009 *
4  * ===================                          Last change: 11/04/2009 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : network_functions.php                            *
8  * -------------------------------------------------------------------- *
9  * Short description : Functions for ext-network                        *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Funktionen fuer ext-network                      *
12  * -------------------------------------------------------------------- *
13  * $Revision::                                                        $ *
14  * $Date::                                                            $ *
15  * $Tag:: 0.2.1-FINAL                                                 $ *
16  * $Author::                                                          $ *
17  * Needs to be in all Files and every File needs "svn propset           *
18  * svn:keywords Date Revision" (autoprobset!) at least!!!!!!            *
19  * -------------------------------------------------------------------- *
20  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
21  * Copyright (c) 2009, 2010 by Mailer Developer Team                    *
22  * For more information visit: http://www.mxchange.org                  *
23  *                                                                      *
24  * This program is free software; you can redistribute it and/or modify *
25  * it under the terms of the GNU General Public License as published by *
26  * the Free Software Foundation; either version 2 of the License, or    *
27  * (at your option) any later version.                                  *
28  *                                                                      *
29  * This program is distributed in the hope that it will be useful,      *
30  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
32  * GNU General Public License for more details.                         *
33  *                                                                      *
34  * You should have received a copy of the GNU General Public License    *
35  * along with this program; if not, write to the Free Software          *
36  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
37  * MA  02110-1301  USA                                                  *
38  ************************************************************************/
39
40 // Some security stuff...
41 if (!defined('__SECURITY')) {
42         die();
43 } // END - if
44
45 // Handle a (maybe) sent form here
46 function doNetworkHandleForm () {
47         // Was the form sent?
48         if ((isFormSent()) || (isPostRequestParameterSet('edit')) || (isPostRequestParameterSet('del')) || (isPostRequestParameterSet('change')) || (isPostRequestParameterSet('remove'))) {
49                 // Do we have a 'do'?
50                 if (isGetRequestParameterSet('do')) {
51                         // Process the request
52                         doAdminNetworkProcessForm();
53                 } else {
54                         // No 'do' found
55                         loadTemplate('admin_settings_unsaved', false, getMessage('ADMIN_NETWORK_DO_404'));
56                 }
57         } // END - if
58 }
59
60 // Processes an admin form
61 function doAdminNetworkProcessForm () {
62         // Form really sent?
63         if ((!isFormSent()) && (!isPostRequestParameterSet('edit')) && (!isPostRequestParameterSet('del')) && (!isPostRequestParameterSet('change')) && (!isPostRequestParameterSet('remove'))) {
64                 // Abort here
65                 loadTemplate('admin_settings_unsaved', false, getMessage('ADMIN_NETWORK_FORM_NOT_SENT'));
66                 return;
67         } elseif (!isGetRequestParameterSet('do')) {
68                 // No 'do' found
69                 loadTemplate('admin_settings_unsaved', false, getMessage('ADMIN_NETWORK_DO_404'));
70                 return;
71         }
72
73         // Create function name
74         $functionName = sprintf("doAdminNetworkProcess%sForm", ucfirst(strtolower(getRequestParameter('do'))));
75
76         // Is the function valid?
77         if (!function_exists($functionName)) {
78                 // Invalid function name
79                 debug_report_bug('Invalid do ' . getRequestParameter('do') . ', function ' . $functionName .' does not exist.', false);
80         } // END - if
81
82         // Call-back the method handling our request
83         call_user_func($functionName);
84 }
85
86 // Checks wether the (short) network name is already used (valid)
87 function isNetworkNameValid ($name) {
88         // Query for it
89         $result = SQL_QUERY_ESC("SELECT `network_id` FROM `{?_MYSQL_PREFIX?}_network_data` WHERE `network_short_name`='%s' LIMIT 1",
90                 array($name), __FUNCTION__, __LINE__);
91
92         // Does it exist?
93         $isValid = (SQL_NUMROWS($result) == 1);
94
95         // Free result
96         SQL_FREERESULT($result);
97
98         // Return result
99         return $isValid;
100 }
101
102 // Checks wether the given network type is already used (valid)
103 function isNetworkTypeHandleValid ($type, $networkId) {
104         // Query for it
105         $result = SQL_QUERY_ESC("SELECT `network_type_id` FROM `{?_MYSQL_PREFIX?}_network_types` WHERE `network_id`=%s AND `network_type_handle`='%s' LIMIT 1",
106                 array($networkId, $type), __FUNCTION__, __LINE__);
107
108         // Does it exist?
109         $isValid = (SQL_NUMROWS($result) == 1);
110
111         // Free result
112         SQL_FREERESULT($result);
113
114         // Return result
115         return $isValid;
116 }
117
118 // Checks wether the given network request parameter is already used (valid)
119 function isNetworkRequestParameterValid ($key, $type, $networkId) {
120         // Query for it
121         $result = SQL_QUERY_ESC("SELECT `network_param_id` FROM `{?_MYSQL_PREFIX?}_network_request_params` WHERE `network_id`=%s AND `network_type_id`=%s AND `request_param_key`='%s' LIMIT 1",
122                 array($networkId, $type, $key), __FUNCTION__, __LINE__);
123
124         // Does it exist?
125         $isValid = (SQL_NUMROWS($result) == 1);
126
127         // Free result
128         SQL_FREERESULT($result);
129
130         // Return result
131         return $isValid;
132 }
133
134 // "Getter" for a network's data by provided id number
135 function getNetworkDataById ($id) {
136         // Ids lower one are not accepted
137         if ($id < 1) {
138                 // Not good, should be fixed
139                 debug_report_bug('Network id ' . $id . ' is smaller than 1.');
140         } // END - if
141
142         // By default we have no data
143         $networkData = array();
144
145         // Query for the network data
146         $result = SQL_QUERY_ESC("SELECT
147         `network_id`, `network_short_name`, `network_title`, `network_reflink`, `network_data_seperator`, `network_row_seperator`, `network_request_type`, `network_charset`
148 FROM
149         `{?_MYSQL_PREFIX?}_network_data`
150 WHERE
151         `network_id`=%s
152 LIMIT 1",
153                 array(bigintval($id)), __FUNCTION__, __LINE__);
154
155         // Do we have an entry?
156         if (SQL_NUMROWS($result) == 1) {
157                 // Then get it
158                 $networkData = SQL_FETCHARRAY($result);
159
160                 // Solve disabled
161         } // END - if
162
163         // Free result
164         SQL_FREERESULT($result);
165
166         // Return result
167         return $networkData;
168 }
169
170 // "Getter" for a network type data by provided id number
171 function getNetworkTypeDataById ($id) {
172         // Ids lower one are not accepted
173         if ($id < 1) {
174                 // Not good, should be fixed
175                 debug_report_bug('Network type id ' . $id . ' is smaller than 1.');
176         } // END - if
177
178         // By default we have no data
179         $networkTypeData = array();
180
181         // Query for the network data
182         $result = SQL_QUERY_ESC("SELECT
183         `network_type_id`, `network_id`, `network_type_handle`, `network_type_api_url`, `network_type_click_url`, `network_type_banner_url`
184 FROM
185         `{?_MYSQL_PREFIX?}_network_types`
186 WHERE
187         `network_type_id`=%s
188 LIMIT 1",
189                 array(bigintval($id)), __FUNCTION__, __LINE__);
190
191         // Do we have an entry?
192         if (SQL_NUMROWS($result) == 1) {
193                 // Then get it
194                 $networkTypeData = SQL_FETCHARRAY($result);
195         } // END - if
196
197         // Free result
198         SQL_FREERESULT($result);
199
200         // Return result
201         return $networkTypeData;
202 }
203
204 // "Getter" for a network request parameter data by provided id number
205 function getNetworkRequestParamsDataById ($id) {
206         // Ids lower one are not accepted
207         if ($id < 1) {
208                 // Not good, should be fixed
209                 debug_report_bug('Network request parameter id ' . $id . ' is smaller than 1.');
210         } // END - if
211
212         // By default we have no data
213         $networkRequestData = array();
214
215         // Query for the network data
216         $result = SQL_QUERY_ESC("SELECT
217         `network_param_id`, `network_id`, `network_type_id`, `request_param_key`, `request_param_value`, `request_param_default`
218 FROM
219         `{?_MYSQL_PREFIX?}_network_request_params`
220 WHERE
221         `network_param_id`=%s
222 LIMIT 1",
223                 array(bigintval($id)), __FUNCTION__, __LINE__);
224
225         // Do we have an entry?
226         if (SQL_NUMROWS($result) == 1) {
227                 // Then get it
228                 $networkRequestData = SQL_FETCHARRAY($result);
229         } // END - if
230
231         // Free result
232         SQL_FREERESULT($result);
233
234         // Return result
235         return $networkRequestData;
236 }
237
238 // Updates given network (id) with data from array
239 function doNetworkUpdateDataByArray ($id, $networkData) {
240         // Ids lower one are not accepted
241         if ($id < 1) {
242                 // Not good, should be fixed
243                 debug_report_bug('Network id ' . $id . ' is smaller than 1.');
244         } // END - if
245
246         // Just call our inner method
247         return adminSaveSettings($networkData, '_network_data', sprintf("`network_id`=%s", bigintval($id)), array(), false, false);
248 }
249
250 // Updates given network type handler (id) with data from array
251 function doNetworkUpdateTypeByArray ($id, $networkTypeData) {
252         // Ids lower one are not accepted
253         if ($id < 1) {
254                 // Not good, should be fixed
255                 debug_report_bug('Network type handler id ' . $id . ' is smaller than 1.');
256         } // END - if
257
258         // Just call our inner method
259         return adminSaveSettings($networkTypeData, '_network_types', sprintf("`network_type_id`=%s", bigintval($id)), array(), false, false);
260 }
261
262 // Updates given network request parameters (id) with data from array
263 function doNetworkUpdateParamsByArray ($id, $networkParamData) {
264         // Ids lower one are not accepted
265         if ($id < 1) {
266                 // Not good, should be fixed
267                 debug_report_bug('Network request parameter id ' . $id . ' is smaller than 1.');
268         } // END - if
269
270         // Just call our inner method
271         return adminSaveSettings($networkParamData, '_network_request_params', sprintf("`network_param_id`=%s", bigintval($id)), array(), false, false);
272 }
273
274 // Removes given network entry
275 function doAdminRemoveNetworkEntry ($table, $column, $id, $limit = 1) {
276         // Remove the entry
277         SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_network_%s` WHERE `%s`=%s LIMIT %s",
278                 array($table, $column, $id, $limit), __FILE__, __LINE__);
279
280         // Return affected rows
281         return SQL_AFFECTEDROWS();
282 }
283
284 // Generates a list of networks for given script and returns it
285 function generateAdminNetworkList () {
286         // Init output
287         $content = '';
288
289         // Query for all networks
290         $result = SQL_QUERY("SELECT
291         `network_id`, `network_short_name`, `network_title`
292 FROM
293         `{?_MYSQL_PREFIX?}_network_data`
294 ORDER BY
295         `network_short_name` ASC", __FILE__, __LINE__);
296
297         // Do we have entries?
298         if (SQL_NUMROWS($result) > 0) {
299                 // List all entries
300                 $rows = array();
301                 while ($row = SQL_FETCHARRAY($result)) {
302                         // Is this valid, then add it
303                         if ((is_array($row)) && (isset($row['network_id']))) $rows[] = $row;
304                 } // END - while
305
306                 // Generate the selection box
307                 $content = generateSelectionBoxFromArray($rows, 'network', 'network_id');
308         } else {
309                 // Nothing selected
310                 $content = loadTemplate('admin_settings_unsaved', false, getMessage('ADMIN_ENTRIES_404'));
311         }
312
313         // Free the result
314         SQL_FREERESULT($result);
315
316         // Return the list
317         return $content;
318 }
319
320 // Generator (somewhat getter) for network type options
321 function generateNetworkTypeOptions ($id) {
322         // Is this an array, then we just came back from edit/delete actions
323         if (is_array($id)) $id = '';
324
325         // Is this cached?
326         if (!isset($GLOBALS['network_type_options'][$id])) {
327                 // Generate output and cache it
328                 $GLOBALS['network_type_options'][$id] = generateOptionList(
329                         'network_types',
330                         'network_type_id',
331                         'network_type_handle',
332                         $id,
333                         '',
334                         sprintf("WHERE `network_id`=%s", getRequestParameter('network'))
335                 );
336         } // END - if
337
338         // Return content
339         return $GLOBALS['network_type_options'][$id];
340 }
341
342 // Generator (somewhat getter) for request key options
343 function generateNetworkRequestKeyOptions ($key) {
344         // Is it cached?
345         if (!isset($GLOBALS['network_request_param_key'][$key])) {
346                 // Generate and cache it
347                 $GLOBALS['network_request_param_key'][$key] = generateOptionList(
348                         '/ARRAY/',
349                         array(
350                                 'id',
351                                 'sid',
352                                 'hash',
353                                 'password',
354                                 'reload',
355                                 'max_stay',
356                                 'min_stay',
357                                 'currency',
358                                 'type',
359                                 'remain',
360                                 'reward',
361                                 'size',
362                                 'erotic',
363                                 'extra'
364                         ),
365                         array(
366                                 '{--ADMIN_NETWORK_REQUEST_PARAMS_ID--}',
367                                 '{--ADMIN_NETWORK_REQUEST_PARAMS_SID--}',
368                                 '{--ADMIN_NETWORK_REQUEST_PARAMS_HASH--}',
369                                 '{--ADMIN_NETWORK_REQUEST_PARAMS_PASSWORD--}',
370                                 '{--ADMIN_NETWORK_REQUEST_PARAMS_RELOAD--}',
371                                 '{--ADMIN_NETWORK_REQUEST_PARAMS_MAXIMUM_STAY--}',
372                                 '{--ADMIN_NETWORK_REQUEST_PARAMS_MINIMUM_STAY--}',
373                                 '{--ADMIN_NETWORK_REQUEST_PARAMS_CURRENCY--}',
374                                 '{--ADMIN_NETWORK_REQUEST_PARAMS_TYPE--}',
375                                 '{--ADMIN_NETWORK_REQUEST_PARAMS_REMAIN--}',
376                                 '{--ADMIN_NETWORK_REQUEST_PARAMS_REWARD--}',
377                                 '{--ADMIN_NETWORK_REQUEST_PARAMS_SIZE--}',
378                                 '{--ADMIN_NETWORK_REQUEST_PARAMS_EROTIC--}',
379                                 '{--ADMIN_NETWORK_REQUEST_PARAMS_EXTRA--}'
380                         ),
381                         $key,
382                         '', '',
383                         $GLOBALS['network_params_disabled']
384                 );
385         } // END - if
386
387         // Return content
388         return $GLOBALS['network_request_param_key'][$key];
389 }
390
391 //------------------------------------------------------------------------------
392 //                             Call-back functions
393 //------------------------------------------------------------------------------
394
395 // Callback function to add new network
396 function doAdminNetworkProcessAddnetworkForm () {
397         // We can say here, the form is sent, so check if the network is already added
398         if (isNetworkNameValid(postRequestParameter('network_short_name'))) {
399                 // Already there
400                 loadTemplate('admin_settings_unsaved', false, getMaskedMessage('ADMIN_NETWORK_ALREADY_ADDED', postRequestParameter('network_short_name')));
401                 return false;
402         } // END - if
403
404         // Remove the 'ok' part
405         unsetPostRequestParameter('ok');
406
407         // Add the whole request to database
408         SQL_QUERY("INSERT INTO
409         `{?_MYSQL_PREFIX?}_network_data`
410 (
411         `" . implode('`,`', array_keys(postRequestArray())) . "`
412 ) VALUES (
413         '" . implode("','", array_values(postRequestArray())) . "'
414 )", __FUNCTION__, __LINE__);
415
416         // Add the id for output only
417         setPostRequestParameter('network_id', SQL_INSERTID());
418
419         // Output message
420         if (SQL_AFFECTEDROWS() == 1) {
421                 // Successfully added
422                 loadTemplate('admin_network_added', false, postRequestArray());
423         } else {
424                 // Not added
425                 loadTemplate('admin_settings_unsaved', false, getMaskedMessage('ADMIN_NETWORK_DATA_NOT_ADDED', postRequestParameter('network_short_name')));
426         }
427 }
428
429 // Displays selected networks for editing
430 function doAdminNetworkProcessHandlenetworkForm () {
431         // Do we have selections?
432         if (countPostSelection() > 0) {
433                 // Something has been selected, so start displaying one by one
434                 $SW = 2; $OUT = '';
435                 foreach (postRequestParameter('sel') as $id => $sel) {
436                         // Is this selected?
437                         if ($sel == 1) {
438                                 // Load this network's data
439                                 $networkData = getNetworkDataById($id);
440
441                                 // Do we have found the network?
442                                 if (count($networkData) > 0) {
443                                         // Add color
444                                         $networkData['sw'] = $SW;
445
446                                         if (isPostRequestParameterSet('edit')) {
447                                                 // Make selection box for network_request_type
448                                                 $networkData['network_request_type'] = generateOptionList(
449                                                         '/ARRAY/',
450                                                         array('GET','POST'),
451                                                         array(
452                                                                 getMessage('ADMIN_NETWORK_REQUEST_TYPE_GET'),
453                                                                 getMessage('ADMIN_NETWORK_REQUEST_TYPE_POST')
454                                                         ),
455                                                         $networkData['network_request_type']
456                                                 );
457
458                                                 // Add row template for editing
459                                                 $OUT .= loadTemplate('admin_edit_networks_row', true, $networkData);
460                                         } elseif (isPostRequestParameterSet('del')) {
461                                                 // Translate the request type
462                                                 $networkData['network_request_type'] = getMessage('ADMIN_NETWORK_REQUEST_TYPE_' . $networkData['network_request_type']);
463
464                                                 // Add row template for deleting
465                                                 $OUT .= loadTemplate('admin_del_networks_row', true, $networkData);
466                                         } else {
467                                                 // Problem!
468                                                 debug_report_bug('Cannot detect edit/del.');
469                                         }
470
471                                         // Switch colors
472                                         $SW = 3 - $SW;
473                                 } // END - if
474                         } // END - if
475                 } // END - foreach
476
477                 // If we have no rows, we don't need to display the edit form
478                 if (!empty($OUT)) {
479                         // Output main template
480                         if (isPostRequestParameterSet('edit')) {
481                                 loadTemplate('admin_edit_networks', false, $OUT);
482                         } elseif (isPostRequestParameterSet('del')) {
483                                 loadTemplate('admin_del_networks', false, $OUT);
484                         } else {
485                                 // Problem!
486                                 debug_report_bug('Cannot detect edit/del.');
487                         }
488
489                         // Don't display the list/add new form
490                         $GLOBALS['network_display'] = false;
491                 } else {
492                         // Nothing selected/found
493                         loadTemplate('admin_settings_unsaved', false, getMessage('ADMIN_NETWORK_NOTHING_FOUND'));
494                 }
495         } // END - if
496 }
497
498 // Handle network type form
499 function doAdminNetworkProcessHandlenetworktypeForm () {
500         // Do we have selections?
501         if (countPostSelection() > 0) {
502                 // Load network data
503                 $networkData = getNetworkDataById(getRequestParameter('network'));
504
505                 // Something has been selected, so start displaying one by one
506                 $SW = 2; $OUT = '';
507                 foreach (postRequestParameter('sel') as $id => $sel) {
508                         // Is this selected?
509                         if ($sel == 1) {
510                                 // Load this network's data
511                                 $networkTypeData = getNetworkTypeDataById($id);
512
513                                 // Do we have found the network?
514                                 if (count($networkTypeData) > 0) {
515                                         // Add color
516                                         $networkTypeData['sw'] = $SW;
517
518                                         if (isPostRequestParameterSet('edit')) {
519                                                 // Add row template for deleting
520                                                 $OUT .= loadTemplate('admin_edit_network_types_row', true, $networkTypeData);
521                                         } elseif (isPostRequestParameterSet('del')) {
522                                                 // Fix empty banner URL
523                                                 if (trim($networkTypeData['network_type_banner_url']) == '') $networkTypeData['network_type_banner_url'] = '---';
524
525                                                 // Add row template for deleting
526                                                 $OUT .= loadTemplate('admin_del_network_types_row', true, $networkTypeData);
527                                         } else {
528                                                 // Problem!
529                                                 debug_report_bug('Cannot detect edit/del.');
530                                         }
531
532                                         // Switch colors
533                                         $SW = 3 - $SW;
534                                 } // END - if
535                         } // END - if
536                 } // END - foreach
537
538                 // If we have no rows, we don't need to display the edit form
539                 if (!empty($OUT)) {
540                         // Prepare content for template
541                         $content = array(
542                                 'rows'         => $OUT,
543                                 'network_data' => getNetworkDataById(getRequestParameter('network'))
544                         );
545
546                         // Output main template
547                         if (isPostRequestParameterSet('edit')) {
548                                 loadTemplate('admin_edit_network_types', false, $content);
549                         } elseif (isPostRequestParameterSet('del')) {
550                                 loadTemplate('admin_del_network_types', false, $content);
551                         } else {
552                                 // Problem!
553                                 debug_report_bug('Cannot detect edit/del.');
554                         }
555
556                         // Don't display the list/add new form
557                         $GLOBALS['network_display'] = false;
558                 } else {
559                         // Nothing selected/found
560                         loadTemplate('admin_settings_unsaved', false, getMessage('ADMIN_NETWORK_TYPES_NOTHING_FOUND'));
561                 }
562         } // END - if
563 }
564
565 // Handle network request parameter form
566 function doAdminNetworkProcessHandlerequestparamsForm () {
567         // Do we have selections?
568         if (countPostSelection() > 0) {
569                 // Init cache array
570                 $GLOBALS['network_params_disabled'] = array();
571
572                 // Load network data
573                 $networkData = getNetworkDataById(getRequestParameter('network'));
574
575                 // Something has been selected, so start displaying one by one
576                 $SW = 2; $OUT = '';
577                 foreach (postRequestParameter('sel') as $id => $sel) {
578                         // Is this selected?
579                         if ($sel == 1) {
580                                 // Load this network's data
581                                 $networkRequestData = getNetworkRequestParamsDataById($id);
582
583                                 // Do we have found the network?
584                                 if (count($networkRequestData) > 0) {
585                                         // Add color
586                                         $networkRequestData['sw'] = $SW;
587
588                                         if (isPostRequestParameterSet('edit')) {
589                                                 // Add options list for network type
590                                                 $networkRequestData['type_options'] = generateNetworkTypeOptions($networkRequestData['network_type_id']);
591
592                                                 // Add options list for request key
593                                                 $networkRequestData['key_options'] = generateNetworkRequestKeyOptions($networkRequestData['request_param_key']);
594
595                                                 // Add row template for deleting
596                                                 $OUT .= loadTemplate('admin_edit_network_params_row', true, $networkRequestData);
597                                         } elseif (isPostRequestParameterSet('del')) {
598                                                 // Fix empty banner URL
599                                                 if (trim($networkRequestData['request_param_default']) == '') $networkRequestData['request_param_default'] = '---';
600
601                                                 // Get type data
602                                                 $networkRequestData['network_type_data'] = getNetworkTypeDataById($networkRequestData['network_type_id']);
603
604                                                 // Add row template for deleting
605                                                 $OUT .= loadTemplate('admin_del_network_params_row', true, $networkRequestData);
606                                         } else {
607                                                 // Problem!
608                                                 debug_report_bug('Cannot detect edit/del.');
609                                         }
610
611                                         // Switch colors
612                                         $SW = 3 - $SW;
613                                 } // END - if
614                         } // END - if
615                 } // END - foreach
616
617                 // If we have no rows, we don't need to display the edit form
618                 if (!empty($OUT)) {
619                         // Prepare content for template
620                         $content = array(
621                                 'rows'         => $OUT,
622                                 'network_data' => getNetworkDataById(getRequestParameter('network'))
623                         );
624
625                         // Output main template
626                         if (isPostRequestParameterSet('edit')) {
627                                 loadTemplate('admin_edit_network_params', false, $content);
628                         } elseif (isPostRequestParameterSet('del')) {
629                                 loadTemplate('admin_del_network_params', false, $content);
630                         } else {
631                                 // Problem!
632                                 debug_report_bug('Cannot detect edit/del.');
633                         }
634
635                         // Don't display the list/add new form
636                         $GLOBALS['network_display'] = false;
637                 } else {
638                         // Nothing selected/found
639                         loadTemplate('admin_settings_unsaved', false, getMessage('ADMIN_NETWORK_REQUEST_PARAMS_NOTHING_FOUND'));
640                 }
641         } // END - if
642 }
643
644 // Changes given networks
645 function doAdminNetworkProcessChangenetworkForm () {
646         // Do we have selections?
647         if (countPostSelection() > 0) {
648                 // By default nothing is updated
649                 $updated = 0;
650
651                 // Something has been selected, so start updating them
652                 foreach (postRequestParameter('sel') as $id => $sel) {
653                         // Update this entry?
654                         if ($sel == 1) {
655                                 // Init data array
656                                 $networkData = array();
657
658                                 // Transfer whole array, except 'sel'
659                                 foreach (postRequestArray() as $key => $entry) {
660                                         // Skip 'sel' and submit button
661                                         if (in_array($key, array('sel', 'change'))) continue;
662
663                                         // Do we have this enty?
664                                         if (!isset($entry[$id])) {
665                                                 // Not found, needs fixing
666                                                 debug_report_bug('No entry in key=' . $key . ', id=' . $id . ' found.');
667                                         } // END - if
668
669                                         // Add this entry
670                                         $networkData[$key] = $entry[$id];
671                                 } // END - foreach
672
673                                 // Update the network data
674                                 $updated += doNetworkUpdateDataByArray($id, $networkData);
675                         } // END - if
676                 } // END - foreach
677
678                 // Do we have updates?
679                 if ($updated > 0) {
680                         // Updates done
681                         loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_NETWORK_UPDATED', $updated));
682                 } else {
683                         // Nothing changed
684                         loadTemplate('admin_settings_unsaved', false, getMessage('ADMIN_NETWORK_NOTHING_CHANGED'));
685                 }
686         } // END - if
687 }
688
689 // Removes given networks
690 function doAdminNetworkProcessRemovenetworkForm () {
691         // Do we have selections?
692         if (countPostSelection() > 0) {
693                 // By default nothing is removed
694                 $removed = 0;
695
696                 // Something has been selected, so start updating them
697                 foreach (postRequestParameter('sel') as $id => $sel) {
698                         // Update this entry?
699                         if ($sel == 1) {
700                                 // Remove this entry
701                                 $removed += doAdminRemoveNetworkEntry('data', 'network_id', $id);
702                         } // END - if
703                 } // END - foreach
704
705                 // Do we have removes?
706                 if ($removed > 0) {
707                         // Removals done
708                         loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_NETWORK_REMOVED', $removed));
709                 } else {
710                         // Nothing removed
711                         loadTemplate('admin_settings_unsaved', false, getMessage('ADMIN_NETWORK_NOTHING_REMOVED'));
712                 }
713         } // END - if
714 }
715
716 // Add a network type handler if not yet found
717 function doAdminNetworkProcessAddnetworktypeForm () {
718         // Is the network type handle already used with given network?
719         if (isNetworkTypeHandleValid(postRequestParameter('network_type_handle'), getRequestParameter('network'))) {
720                 // Already added
721                 loadTemplate('admin_settings_unsaved', false, getMaskedMessage('ADMIN_NETWORK_TYPES_HANDLE_ALREADY_ADDED', postRequestParameter('network_type_handle')));
722
723                 // ... so abort here
724                 return false;
725         } // END - if
726
727         // Remove the 'ok' part
728         unsetPostRequestParameter('ok');
729
730         // Add id
731         setPostRequestParameter('network_id', getRequestParameter('network'));
732
733         // Is network_type_banner_url set?
734         if (postRequestParameter('network_type_banner_url') == '') {
735                 // Remove empty value to get a NULL for an optional entry
736                 unsetPostRequestParameter('network_type_banner_url');
737         } // END - if
738
739         // Add the whole request to database
740         SQL_QUERY("INSERT INTO
741         `{?_MYSQL_PREFIX?}_network_types`
742 (
743         `" . implode('`,`', array_keys(postRequestArray())) . "`
744 ) VALUES (
745         '" . implode("','", array_values(postRequestArray())) . "'
746 )", __FUNCTION__, __LINE__);
747
748         // Output message
749         if (SQL_AFFECTEDROWS() == 1) {
750                 // Successfully added
751                 loadTemplate('admin_network_type_added', false, postRequestArray());
752         } else {
753                 // Not added
754                 loadTemplate('admin_settings_unsaved', false, getMaskedMessage('ADMIN_NETWORK_TYPES_NOT_ADDED', postRequestParameter('network_type_handle')));
755         }
756 }
757
758 // Changes given network type handlers
759 function doAdminNetworkProcessChangenetworktypeForm () {
760         // Do we have selections?
761         if (countPostSelection() > 0) {
762                 // By default nothing is updated
763                 $updated = 0;
764
765                 // Something has been selected, so start updating them
766                 foreach (postRequestParameter('sel') as $id => $sel) {
767                         // Update this entry?
768                         if ($sel == 1) {
769                                 // Init data array
770                                 $networkTypeData = array();
771
772                                 // Transfer whole array, except 'sel'
773                                 foreach (postRequestArray() as $key => $entry) {
774                                         // Skip 'sel' and submit button
775                                         if (in_array($key, array('sel', 'change'))) continue;
776
777                                         // Do we have this enty?
778                                         if (!isset($entry[$id])) {
779                                                 // Not found, needs fixing
780                                                 debug_report_bug('No entry in key=' . $key . ', id=' . $id . ' found.');
781                                         } // END - if
782
783                                         // Fix empty network_type_banner_url to NULL
784                                         if (($key == 'network_type_banner_url') && (trim($entry[$id]) == '')) {
785                                                 // Set it to NULL
786                                                 $entry[$id] = null;
787                                         } // END - if
788
789                                         // Add this entry
790                                         $networkTypeData[$key] = $entry[$id];
791                                 } // END - foreach
792
793                                 // Update the network data
794                                 $updated += doNetworkUpdateTypeByArray($id, $networkTypeData);
795                         } // END - if
796                 } // END - foreach
797
798                 // Do we have updates?
799                 if ($updated > 0) {
800                         // Updates done
801                         loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_NETWORK_TYPES_UPDATED', $updated));
802                 } else {
803                         // Nothing changed
804                         loadTemplate('admin_settings_unsaved', false, getMessage('ADMIN_NETWORK_TYPES_NOTHING_CHANGED'));
805                 }
806         } // END - if
807 }
808
809 // Changes given network request parameters
810 function doAdminNetworkProcessChangenetworkparamForm () {
811         // Do we have selections?
812         if (countPostSelection() > 0) {
813                 // By default nothing is updated
814                 $updated = 0;
815
816                 // Something has been selected, so start updating them
817                 foreach (postRequestParameter('sel') as $id => $sel) {
818                         // Update this entry?
819                         if ($sel == 1) {
820                                 // Init data array
821                                 $networkParamsData = array();
822
823                                 // Transfer whole array, except 'sel'
824                                 foreach (postRequestArray() as $key => $entry) {
825                                         // Skip 'sel' and submit button
826                                         if (in_array($key, array('sel', 'change'))) continue;
827
828                                         // Do we have this enty?
829                                         if (!isset($entry[$id])) {
830                                                 // Not found, needs fixing
831                                                 debug_report_bug('No entry in key=' . $key . ', id=' . $id . ' found.');
832                                         } // END - if
833
834                                         // Fix empty request_param_default to NULL
835                                         if (($key == 'request_param_default') && (trim($entry[$id]) == '')) {
836                                                 // Set it to NULL
837                                                 $entry[$id] = null;
838                                         } // END - if
839
840                                         // Add this entry
841                                         $networkParamsData[$key] = $entry[$id];
842                                 } // END - foreach
843
844                                 // Update the network data
845                                 $updated += doNetworkUpdateParamsByArray($id, $networkParamsData);
846                         } // END - if
847                 } // END - foreach
848
849                 // Do we have updates?
850                 if ($updated > 0) {
851                         // Updates done
852                         loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_NETWORK_REQUEST_PARAMS_UPDATED', $updated));
853                 } else {
854                         // Nothing changed
855                         loadTemplate('admin_settings_unsaved', false, getMessage('ADMIN_NETWORK_REQUEST_PARAMS_NOTHING_CHANGED'));
856                 }
857         } // END - if
858 }
859
860 // Removes given network type handlers
861 function doAdminNetworkProcessRemovenetworktypeForm () {
862         // Do we have selections?
863         if (countPostSelection() > 0) {
864                 // By default nothing is removed
865                 $removed = 0;
866
867                 // Something has been selected, so start updating them
868                 foreach (postRequestParameter('sel') as $id => $sel) {
869                         // Update this entry?
870                         if ($sel == 1) {
871                                 // Remove this entry
872                                 $removed += doAdminRemoveNetworkEntry('types', 'network_type_id', $id);
873                         } // END - if
874                 } // END - foreach
875
876                 // Do we have removes?
877                 if ($removed > 0) {
878                         // Removals done
879                         loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_NETWORK_TYPES_REMOVED', $removed));
880                 } else {
881                         // Nothing removed
882                         loadTemplate('admin_settings_unsaved', false, getMessage('ADMIN_NETWORK_TYPES_NOTHING_REMOVED'));
883                 }
884         } // END - if
885 }
886
887 // Removes given network request parameters
888 function doAdminNetworkProcessRemovenetworkparamForm () {
889         // Do we have selections?
890         if (countPostSelection() > 0) {
891                 // By default nothing is removed
892                 $removed = 0;
893
894                 // Something has been selected, so start updating them
895                 foreach (postRequestParameter('sel') as $id => $sel) {
896                         // Update this entry?
897                         if ($sel == 1) {
898                                 // Remove this entry
899                                 $removed += doAdminRemoveNetworkEntry('request_params', 'network_param_id', $id);
900                         } // END - if
901                 } // END - foreach
902
903                 // Do we have removes?
904                 if ($removed > 0) {
905                         // Removals done
906                         loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_NETWORK_REQUEST_PARAMS_REMOVED', $removed));
907                 } else {
908                         // Nothing removed
909                         loadTemplate('admin_settings_unsaved', false, getMessage('ADMIN_NETWORK_REQUEST_PARAMS_NOTHING_REMOVED'));
910                 }
911         } // END - if
912 }
913
914 // Adds a request parameter to given network and type
915 function doAdminNetworkProcessAddnetworkparamForm () {
916         // Is the request parameter already used with given network?
917         if (isNetworkRequestParameterValid(postRequestParameter('request_param_key'), postRequestParameter('network_type_id'), getRequestParameter('network'))) {
918                 // Already added
919                 loadTemplate('admin_settings_unsaved', false, getMaskedMessage('ADMIN_NETWORK_REQUEST_PARAMETER_ALREADY_ADDED', postRequestParameter('request_param_key')));
920
921                 // ... so abort here
922                 return false;
923         } // END - if
924
925         // Remove the 'ok' part
926         unsetPostRequestParameter('ok');
927
928         // Add id
929         setPostRequestParameter('network_id', getRequestParameter('network'));
930
931         // Is request_param_default set?
932         if (postRequestParameter('request_param_default') == '') {
933                 // Remove empty value to get a NULL for an optional entry
934                 unsetPostRequestParameter('request_param_default');
935         } // END - if
936
937         // Add the whole request to database
938         SQL_QUERY("INSERT INTO
939         `{?_MYSQL_PREFIX?}_network_request_params`
940 (
941         `" . implode('`,`', array_keys(postRequestArray())) . "`
942 ) VALUES (
943         '" . implode("','", array_values(postRequestArray())) . "'
944 )", __FUNCTION__, __LINE__);
945
946         // Output message
947         if (SQL_AFFECTEDROWS() == 1) {
948                 // Successfully added
949                 loadTemplate('admin_network_request_param_added', false, postRequestArray());
950         } else {
951                 // Not added
952                 loadTemplate('admin_settings_unsaved', false, getMaskedMessage('ADMIN_NETWORK_REQUEST_PARAMETER_NOT_ADDED', postRequestParameter('request_param_key')));
953         }
954 }
955
956 // [EOF]
957 ?>