Extension ext-network continued:
[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  * -------------------------------------------------------------------- *
18  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
19  * Copyright (c) 2009 - 2011 by Mailer Developer Team                   *
20  * For more information visit: http://mxchange.org                      *
21  *                                                                      *
22  * This program is free software; you can redistribute it and/or modify *
23  * it under the terms of the GNU General Public License as published by *
24  * the Free Software Foundation; either version 2 of the License, or    *
25  * (at your option) any later version.                                  *
26  *                                                                      *
27  * This program is distributed in the hope that it will be useful,      *
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
30  * GNU General Public License for more details.                         *
31  *                                                                      *
32  * You should have received a copy of the GNU General Public License    *
33  * along with this program; if not, write to the Free Software          *
34  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
35  * MA  02110-1301  USA                                                  *
36  ************************************************************************/
37
38 // Some security stuff...
39 if (!defined('__SECURITY')) {
40         die();
41 } // END - if
42
43 // Private setter for current network id
44 function setCurrentNetworkId ($networkId) {
45         $GLOBALS['current_network_id'] = bigintval($networkId);
46 }
47
48 // Private getter for current network id
49 function getCurrentNetworkId () {
50         return $GLOBALS['current_network_id'];
51 }
52
53 // Handle a (maybe) sent form here
54 function doNetworkHandleForm () {
55         // Was the form sent?
56         if ((isFormSent()) || (isFormSent('edit')) || (isFormSent('delete')) || (isFormSent('do_edit')) || (isFormSent('do_delete'))) {
57                 // Do we have a 'do'?
58                 if (isGetRequestElementSet('do')) {
59                         // Process the request
60                         doAdminNetworkProcessForm();
61                 } else {
62                         // No 'do' found
63                         loadTemplate('admin_settings_unsaved', false, '{--ADMIN_NETWORK_DO_404--}');
64                 }
65         } // END - if
66 }
67
68 // Processes an admin form
69 function doAdminNetworkProcessForm () {
70         // Form really sent?
71         if ((!isFormSent()) && (!isFormSent('edit')) && (!isFormSent('delete')) && (!isFormSent('do_edit')) && (!isFormSent('do_delete'))) {
72                 // Abort here
73                 loadTemplate('admin_settings_unsaved', false, '{--ADMIN_NETWORK_FORM_NOT_SENT--}');
74                 return;
75         } elseif (!isGetRequestElementSet('do')) {
76                 // No 'do' found
77                 loadTemplate('admin_settings_unsaved', false, '{--ADMIN_NETWORK_DO_404--}');
78                 return;
79         }
80
81         // Create function name
82         $functionName = sprintf("doAdminNetworkProcess%s", capitalizeUnderscoreString(getRequestElement('do')));
83
84         // Is the function valid?
85         if (!function_exists($functionName)) {
86                 // Invalid function name
87                 debug_report_bug(__FUNCTION__, __LINE__, 'Invalid do ' . getRequestElement('do') . ', function ' . $functionName .' does not exist.', false);
88         } // END - if
89
90         // Call-back the method handling our request
91         call_user_func($functionName);
92 }
93
94 // Checks wether the (short) network name is already used (valid)
95 function isNetworkNameValid ($name) {
96         // Query for it
97         $result = SQL_QUERY_ESC("SELECT `network_id` FROM `{?_MYSQL_PREFIX?}_network_data` WHERE `network_short_name`='%s' LIMIT 1",
98                 array($name), __FUNCTION__, __LINE__);
99
100         // Does it exist?
101         $isValid = (SQL_NUMROWS($result) == 1);
102
103         // Free result
104         SQL_FREERESULT($result);
105
106         // Return result
107         return $isValid;
108 }
109
110 // Checks wether the given network type is already used (valid)
111 function isNetworkTypeHandleValid ($type, $networkId) {
112         // Query for it
113         $result = SQL_QUERY_ESC("SELECT `network_type_id` FROM `{?_MYSQL_PREFIX?}_network_types` WHERE `network_id`=%s AND `network_type_handle`='%s' LIMIT 1",
114                 array($networkId, $type), __FUNCTION__, __LINE__);
115
116         // Does it exist?
117         $isValid = (SQL_NUMROWS($result) == 1);
118
119         // Free result
120         SQL_FREERESULT($result);
121
122         // Return result
123         return $isValid;
124 }
125
126 // Checks wether the given network request parameter is already used (valid)
127 function isNetworkRequestElementValid ($key, $type, $networkId) {
128         // Query for it
129         $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",
130                 array($networkId, $type, $key), __FUNCTION__, __LINE__);
131
132         // Does it exist?
133         $isValid = (SQL_NUMROWS($result) == 1);
134
135         // Free result
136         SQL_FREERESULT($result);
137
138         // Return result
139         return $isValid;
140 }
141
142 // Checks wether the given network API array translation
143 function isNetworkApiTranslationValid ($key, $type, $networkId) {
144         // Query for it
145         $result = SQL_QUERY_ESC("SELECT `network_api_id` FROM `{?_MYSQL_PREFIX?}_network_api_translation` WHERE `network_id`=%s AND `network_type_id`=%s AND `network_api_index`='%s' LIMIT 1",
146                 array($networkId, $type, $key), __FUNCTION__, __LINE__);
147
148         // Does it exist?
149         $isValid = (SQL_NUMROWS($result) == 1);
150
151         // Free result
152         SQL_FREERESULT($result);
153
154         // Return result
155         return $isValid;
156 }
157
158 // "Getter" for a network's data by provided id number
159 function getNetworkDataById ($networkId, $column = '') {
160         // Ids lower one are not accepted
161         if ($networkId < 1) {
162                 // Not good, should be fixed
163                 debug_report_bug(__FUNCTION__, __LINE__, 'Network id ' . $networkId . ' is smaller than 1.');
164         } // END - if
165
166         // Set current network id
167         setCurrentNetworkId($networkId);
168
169         // Is it cached?
170         if (!isset($GLOBALS['network_data'][$networkId])) {
171                 // By default we have no data
172                 $GLOBALS['network_data'][$networkId] = array();
173
174                 // Query for the network data
175                 $result = SQL_QUERY_ESC("SELECT
176         `network_id`,`network_short_name`,`network_title`,`network_reflink`,`network_data_seperator`,`network_row_seperator`,`network_request_type`,`network_charset`
177 FROM
178         `{?_MYSQL_PREFIX?}_network_data`
179 WHERE
180         `network_id`=%s
181 LIMIT 1",
182                         array(bigintval($networkId)), __FUNCTION__, __LINE__);
183
184                 // Do we have an entry?
185                 if (SQL_NUMROWS($result) == 1) {
186                         // Then get it
187                         $GLOBALS['network_data'][$networkId] = SQL_FETCHARRAY($result);
188                 } // END - if
189
190                 // Free result
191                 SQL_FREERESULT($result);
192         } // END - if
193
194         // Return result
195         if (empty($column)) {
196                 // Return array
197                 return $GLOBALS['network_data'][$networkId];
198         } else {
199                 // Return column
200                 return $GLOBALS['network_data'][$networkId][$column];
201         }
202 }
203
204 // "Getter" for a network's data by provided type id number
205 function getNetworkDataByTypeId ($networkId, $column = '') {
206         // Ids lower one are not accepted
207         if ($networkId < 1) {
208                 // Not good, should be fixed
209                 debug_report_bug(__FUNCTION__, __LINE__, 'Network type id ' . $networkId . ' is smaller than 1.');
210         } // END - if
211
212         // Set current network id
213         setCurrentNetworkId($networkId);
214
215         // Is it cached?
216         if (!isset($GLOBALS['network_data'][$networkId])) {
217                 // By default we have no data
218                 $GLOBALS['network_data'][$networkId] = array();
219
220                 // Query for the network data
221                 $result = SQL_QUERY_ESC("SELECT
222         d.`network_id`, d.`network_short_name`, d.`network_title`, d.`network_reflink`, d.`network_data_seperator`, d.`network_row_seperator`, d.`network_request_type`, d.`network_charset`,
223         t.`network_type_handle`, t.`network_type_api_url`, t.`network_type_click_url`, t.`network_type_banner_url`
224 FROM
225         `{?_MYSQL_PREFIX?}_network_data` AS d
226 LEFT JOIN
227         `{?_MYSQL_PREFIX?}_network_types` AS t
228 ON
229         d.`network_id`=t.`network_id`
230 WHERE
231         t.`network_type_id`=%s
232 LIMIT 1",
233                         array(bigintval($networkId)), __FUNCTION__, __LINE__);
234
235                 // Do we have an entry?
236                 if (SQL_NUMROWS($result) == 1) {
237                         // Then get it
238                         $GLOBALS['network_data'][$networkId] = SQL_FETCHARRAY($result);
239                 } // END - if
240
241                 // Free result
242                 SQL_FREERESULT($result);
243         } // END - if
244
245         // Return result
246         if (empty($column)) {
247                 // Return array
248                 return $GLOBALS['network_data'][$networkId];
249         } else {
250                 // Return column
251                 return $GLOBALS['network_data'][$networkId][$column];
252         }
253 }
254
255 // "Getter" for a network type data by provided id number
256 function getNetworkTypeDataById ($networkId) {
257         // Ids lower one are not accepted
258         if ($networkId < 1) {
259                 // Not good, should be fixed
260                 debug_report_bug(__FUNCTION__, __LINE__, 'Network type id ' . $networkId . ' is smaller than 1.');
261         } // END - if
262
263         // By default we have no data
264         $GLOBALS['network_type_data'][$networkId] = array();
265
266         // Query for the network data
267         $result = SQL_QUERY_ESC("SELECT
268         `network_type_id`,`network_id`,`network_type_handle`,`network_type_api_url`,`network_type_click_url`,`network_type_banner_url`
269 FROM
270         `{?_MYSQL_PREFIX?}_network_types`
271 WHERE
272         `network_type_id`=%s
273 LIMIT 1",
274                 array(bigintval($networkId)), __FUNCTION__, __LINE__);
275
276         // Do we have an entry?
277         if (SQL_NUMROWS($result) == 1) {
278                 // Then get it
279                 $GLOBALS['network_type_data'][$networkId] = SQL_FETCHARRAY($result);
280         } // END - if
281
282         // Free result
283         SQL_FREERESULT($result);
284
285         // Return result
286         return $GLOBALS['network_type_data'][$networkId];
287 }
288
289 // "Getter" for a network request parameter data by provided id number
290 function getNetworkRequestParamsDataById ($networkId) {
291         // Ids lower one are not accepted
292         if ($networkId < 1) {
293                 // Not good, should be fixed
294                 debug_report_bug(__FUNCTION__, __LINE__, 'Network request parameter id ' . $networkId . ' is smaller than 1.');
295         } // END - if
296
297         // By default we have no data
298         $networkRequestData = array();
299
300         // Query for the network data
301         $result = SQL_QUERY_ESC("SELECT
302         `network_param_id`,`network_id`,`network_type_id`,`request_param_key`,`request_param_value`,`request_param_default`
303 FROM
304         `{?_MYSQL_PREFIX?}_network_request_params`
305 WHERE
306         `network_param_id`=%s
307 LIMIT 1",
308                 array(bigintval($networkId)), __FUNCTION__, __LINE__);
309
310         // Do we have an entry?
311         if (SQL_NUMROWS($result) == 1) {
312                 // Then get it
313                 $networkRequestData = SQL_FETCHARRAY($result);
314         } // END - if
315
316         // Free result
317         SQL_FREERESULT($result);
318
319         // Return result
320         return $networkRequestData;
321 }
322
323 // Updates given network (id) with data from array
324 function doNetworkUpdateDataByArray ($networkId, $networkData) {
325         // Ids lower one are not accepted
326         if ($networkId < 1) {
327                 // Not good, should be fixed
328                 debug_report_bug(__FUNCTION__, __LINE__, 'Network id ' . $networkId . ' is smaller than 1.');
329         } // END - if
330
331         // Just call our inner method
332         return adminSaveSettings($networkData, '_network_data', sprintf("`network_id`=%s", bigintval($networkId)), array(), false, false);
333 }
334
335 // Updates given network type handler (id) with data from array
336 function doNetworkUpdateTypeByArray ($networkId, $networkTypeData) {
337         // Ids lower one are not accepted
338         if ($networkId < 1) {
339                 // Not good, should be fixed
340                 debug_report_bug(__FUNCTION__, __LINE__, 'Network type handler id ' . $networkId . ' is smaller than 1.');
341         } // END - if
342
343         // Just call our inner method
344         return adminSaveSettings($networkTypeData, '_network_types', sprintf("`network_type_id`=%s", bigintval($networkId)), array(), false, false);
345 }
346
347 // Updates given network request parameters (id) with data from array
348 function doNetworkUpdateParamsByArray ($networkId, $networkParamData) {
349         // Ids lower one are not accepted
350         if ($networkId < 1) {
351                 // Not good, should be fixed
352                 debug_report_bug(__FUNCTION__, __LINE__, 'Network request parameter id ' . $networkId . ' is smaller than 1.');
353         } // END - if
354
355         // Just call our inner method
356         return adminSaveSettings($networkParamData, '_network_request_params', sprintf("`network_param_id`=%s", bigintval($networkId)), array(), false, false);
357 }
358
359 // Removes given network entry
360 function doAdminRemoveNetworkEntry ($table, $column, $networkId, $limit = 1) {
361         // Remove the entry
362         SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_network_%s` WHERE `%s`=%s LIMIT %s",
363                 array($table, $column, $networkId, $limit), __FUNCTION__, __LINE__);
364
365         // Return affected rows
366         return SQL_AFFECTEDROWS();
367 }
368
369 // Generates a list of networks for given script and returns it
370 function generateAdminNetworkList () {
371         // Init output
372         $content = '';
373
374         // Query for all networks
375         $result = SQL_QUERY('SELECT
376         `network_id`,`network_short_name`,`network_title`
377 FROM
378         `{?_MYSQL_PREFIX?}_network_data`
379 ORDER BY
380         `network_short_name` ASC', __FUNCTION__, __LINE__);
381
382         // Do we have entries?
383         if (!SQL_HASZERONUMS($result)) {
384                 // List all entries
385                 $rows = array();
386                 while ($row = SQL_FETCHARRAY($result)) {
387                         // Is this valid, then add it
388                         if ((is_array($row)) && (isset($row['network_id']))) {
389                                 // Add entry
390                                 $rows[$row['network_id']] = $row;
391                         } // END - if
392                 } // END - while
393
394                 // Generate the selection box
395                 $content = generateSelectionBoxFromArray($rows, 'network', 'network_id');
396         } else {
397                 // Nothing selected
398                 $content = loadTemplate('admin_settings_unsaved', false, '{--ADMIN_ENTRIES_404--}');
399         }
400
401         // Free the result
402         SQL_FREERESULT($result);
403
404         // Return the list
405         return $content;
406 }
407
408 // Generator (somewhat getter) for a list of network types for given network id
409 function generateAdminNetworkTypeList ($networkId) {
410         // Init content
411         $content = '';
412
413         // Query all types of this network
414         $result = SQL_QUERY_ESC("SELECT
415         `network_type_id`,`network_type_handle`
416 FROM
417         `{?_MYSQL_PREFIX?}_network_types`
418 WHERE
419         `network_id`=%s
420 ORDER BY
421         `network_type_handle` ASC",
422                 array(
423                         bigintval($networkId)
424                 ), __FUNCTION__, __LINE__);
425
426         // Do we have entries?
427         if (!SQL_HASZERONUMS($result)) {
428                 // List all entries
429                 $rows = array();
430                 while ($row = SQL_FETCHARRAY($result)) {
431                         // Is this valid, then add it
432                         if ((is_array($row)) && (isset($row['network_type_id']))) {
433                                 // Add entry
434                                 $rows[$row['network_type_id']] = $row;
435                         } // END - if
436                 } // END - while
437
438                 // Generate the selection box
439                 $content = generateSelectionBoxFromArray($rows, 'network_type', 'network_type_id');
440         } else {
441                 // Nothing selected
442                 $content = loadTemplate('admin_settings_unsaved', false, '{--ADMIN_ENTRIES_404--}');
443         }
444
445         // Free the result
446         SQL_FREERESULT($result);
447
448         // Return content
449         return $content;
450 }
451
452 // Generator (somewhat getter) for a list of network types for all types
453 function generateAdminDistinctNetworkTypeList () {
454         // Init content
455         $content = '';
456
457         // Query all types of this network
458         $result = SQL_QUERY('SELECT
459         t.`network_type_id`, t.`network_type_handle`, d.`network_title`
460 FROM
461         `{?_MYSQL_PREFIX?}_network_types` AS t
462 LEFT JOIN
463         `{?_MYSQL_PREFIX?}_network_data` AS d
464 ON
465         t.`network_id`=d.`network_id`
466 ORDER BY
467         d.`network_short_name` ASC,
468         t.`network_type_handle` ASC', __FUNCTION__, __LINE__);
469
470         // Do we have entries?
471         if (!SQL_HASZERONUMS($result)) {
472                 // List all entries
473                 $rows = array();
474                 while ($row = SQL_FETCHARRAY($result)) {
475                         // Is this valid, then add it
476                         if ((is_array($row)) && (isset($row['network_type_id']))) {
477                                 // Add entry
478                                 $rows[$row['network_type_id']] = $row;
479                         } // END - if
480                 } // END - while
481
482                 // Generate the selection box
483                 $content = generateSelectionBoxFromArray($rows, 'network_type', 'network_type_id', '', '_title');
484         } else {
485                 // Nothing selected
486                 $content = loadTemplate('admin_settings_unsaved', false, '{--ADMIN_ENTRIES_404--}');
487         }
488
489         // Free the result
490         SQL_FREERESULT($result);
491         //* DEBUG: */ die('<pre>'.encodeEntities($content).'</pre>');
492
493         // Return content
494         return $content;
495 }
496
497 // Generator (somewhat getter) for network type options
498 function generateNetworkTypeOptions ($networkId) {
499         // Is this an array, then we just came back from edit/delete actions
500         if (is_array($networkId)) {
501                 $networkId = '';
502         } // END - if
503
504         // Is this cached?
505         if (!isset($GLOBALS[__FUNCTION__][$networkId])) {
506                 // Generate output and cache it
507                 $GLOBALS[__FUNCTION__][$networkId] = generateOptionList(
508                         'network_types',
509                         'network_type_id',
510                         'network_type_handle',
511                         $networkId,
512                         '',
513                         sprintf(
514                                 "WHERE `network_id`=%s",
515                                 bigintval(getRequestElement('network'))
516                         ),
517                         '',
518                         'translateNetworkTypeHandler'
519                 );
520         } // END - if
521
522         // Return content
523         return $GLOBALS[__FUNCTION__][$networkId];
524 }
525
526 // Generates an options list of all available (hard-coded) handlers
527 function generateNetworkTypesAvailableOptions () {
528         // Is it cached?
529         if (!isset($GLOBALS[__FUNCTION__])) {
530                 // Generate list
531                 $GLOBALS[__FUNCTION__] = generateOptionList(
532                         '/ARRAY/',
533                         array(
534                                 'banner',
535                                 'banner_click',
536                                 'banner_view',
537                                 'button',
538                                 'button_click',
539                                 'button_view',
540                                 'surfbar',
541                                 'surfbar_click',
542                                 'surfbar_view',
543                                 'forcedbanner',
544                                 'forcedtextlink',
545                                 'textlink',
546                                 'textlink_click',
547                                 'textlink_view',
548                                 'skybanner',
549                                 'skybanner_click',
550                                 'skybanner_view',
551                                 'layer',
552                                 'layer_click',
553                                 'layer_view',
554                                 'popup',
555                                 'popdown',
556                                 'textmail',
557                                 'htmlmail',
558                                 'lead',
559                                 'sale',
560                                 'payperactive',
561                                 'pagepeel',
562                                 'traffic'
563                         ),
564                         array(),
565                         '',
566                         '', '',
567                         $GLOBALS['network_types_disabled'],
568                         'translateNetworkTypeHandler'
569                 );
570         } // END - if
571
572         // Return content
573         return $GLOBALS[__FUNCTION__];
574 }
575
576 // Generates an options list (somewhat getter) ofr request keys
577 function generateNetworkRequestKeyOptions () {
578         // Is it cached?
579         if (!isset($GLOBALS[__FUNCTION__])) {
580                 // Generate and cache it
581                 $GLOBALS[__FUNCTION__] = generateOptionList(
582                         '/ARRAY/',
583                         array(
584                                 'id',
585                                 'sid',
586                                 'hash',
587                                 'password',
588                                 'reload',
589                                 'maximum_stay',
590                                 'minimum_stay',
591                                 'currency',
592                                 'type',
593                                 'remain',
594                                 'reward',
595                                 'size',
596                                 'erotic',
597                                 'extra',
598                                 'country'
599                         ),
600                         array(),
601                         '',
602                         '', '',
603                         $GLOBALS['network_params_disabled'],
604                         'translateNetworkRequestElement'
605                 );
606         } // END - if
607
608         // Return content
609         return $GLOBALS[__FUNCTION__];
610 }
611
612 // Generator (somewhat getter) for (return) array translation
613 function generateNetworkTranslationOptions ($default = '') {
614         // Is it cached?
615         if (!isset($GLOBALS[__FUNCTION__][$default])) {
616                 // Generate and cache it
617                 $GLOBALS[__FUNCTION__][$default] = generateOptionList(
618                         'network_translations',
619                         'network_translation_id',
620                         'network_translation_name',
621                         $default,
622                         '',
623                         '',
624                         $GLOBALS['network_translation_disabled'],
625                         'translateNetworkTranslationName'
626                 );
627         } // END - if
628
629         // Return content
630         return $GLOBALS[__FUNCTION__][$default];
631 }
632
633 // Generates an option list of request types
634 function generateNetworkRequestTypeOptions ($default = '') {
635         // Do we have cache?
636         if (!isset($GLOBALS[__FUNCTION__][$default])) {
637                 // Generate the list
638                 $GLOBALS[__FUNCTION__][$default] = generateOptionList(
639                         '/ARRAY/',
640                         array(
641                                 'GET',
642                                 'POST'
643                         ),
644                         array(
645                                 '{--ADMIN_NETWORK_REQUEST_TYPE_GET--}',
646                                 '{--ADMIN_NETWORK_REQUEST_TYPE_POST--}'
647                         ),
648                         $default
649                 );
650         } // END - if
651
652         // Return cache
653         return $GLOBALS[__FUNCTION__][$default];
654 }
655
656 // Generates an option list of network_api_active
657 function generateNetworkApiActiveOptions ($default = '') {
658         // Do we have cache?
659         if (!isset($GLOBALS[__FUNCTION__][$default])) {
660                 // Generate the list
661                 $GLOBALS[__FUNCTION__][$default] = generateYesNoOptionList($default);
662         } // END - if
663
664         // Return cache
665         return $GLOBALS[__FUNCTION__][$default];
666 }
667
668 // Translates 'translate_name' for e.g. templates
669 function translateNetworkTranslationName ($name) {
670         // Get the message id
671         return '{--ADMIN_NETWORK_TRANSLATE_' . strtoupper($name) . '_NAME--}';
672 }
673
674 // Translates the network type handler (e.g. banner, paidmail) for templates
675 function translateNetworkTypeHandler ($type) {
676         // Get the message id
677         return '{--ADMIN_NETWORK_TYPES_' . strtoupper($type) . '--}';
678 }
679
680 // Translates request type
681 function translateNetworkRequestType ($type) {
682         // Get the message id
683         return '{--ADMIN_NETWORK_REQUEST_TYPE_' . strtoupper($type) . '--}';
684 }
685
686 // Translates request parameter
687 function translateNetworkRequestElement ($param) {
688         // Get the message id
689         return '{--ADMIN_NETWORK_REQUEST_PARAMETER_' . strtoupper($param) . '--}';
690 }
691
692 // Translates API index
693 function translateNetworkApiIndex ($index) {
694         // Do we have cache?
695         if (!isset($GLOBALS['network_api_index'])) {
696                 // Get an array of all API array indexes
697                 $GLOBALS['network_api_index'] = array();
698
699                 // Get all entries
700                 $result = SQL_QUERY('SELECT
701         `network_api_id`,`network_api_index`,`network_translation_name`
702 FROM
703         `{?_MYSQL_PREFIX?}_network_api_translation`
704 INNER JOIN
705         `{?_MYSQL_PREFIX?}_network_translations`
706 ON
707         `network_api_index`=`network_translation_id`
708 ORDER BY
709         `sort` ASC', __FUNCTION__, __LINE__);
710
711                 // Do we have entries?
712                 if (!SQL_HASZERONUMS($result)) {
713                         // Get all entries
714                         while ($row = SQL_FETCHARRAY($result)) {
715                                 // Add it to our global array
716                                 $GLOBALS['network_api_index'][$row['network_api_index']] = $row;
717                         } // END - while
718                 } // END - if
719
720                 // Free result
721                 SQL_FREERESULT($result);
722         } // END - if
723
724         // Default name is unknown
725         $name = 'unknown';
726
727         // Is the entry there?
728         if (isset($GLOBALS['network_api_index'][$index])) {
729                 // Then get the name
730                 $name = $GLOBALS['network_api_index'][$index]['network_translation_name'];
731         } // END - if
732
733         // Return translation
734         return translateNetworkTranslationName($name);
735 }
736
737 // Translates network API configuration status (see function isNetworkApiConfigured()) by given id
738 function translateNetworkApiConfiguredStatusById ($networkId) {
739         // Do we have cache?
740         if (!isset($GLOBALS[__FUNCTION__][$networkId])) {
741                 // By default it is not configured
742                 $GLOBALS[__FUNCTION__][$networkId] = '{--ADMIN_NETWORK_API_NOT_CONFIGURED--}';
743
744                 // So is it configured?
745                 if (isNetworkApiConfigured($networkId)) {
746                         // Yes, it is
747                         $GLOBALS[__FUNCTION__][$networkId] = '{--ADMIN_NETWORK_API_CONFIGURED--}';
748                 } // END - if
749         } // END - if
750
751         // Return cache
752         return $GLOBALS[__FUNCTION__][$networkId];
753 }
754
755 // Checks if the given network is configured by looking its API configuration entry up
756 function isNetworkApiConfigured ($networkId) {
757         // Do we have cache?
758         if (!isset($GLOBALS[__FUNCTION__][$networkId])) {
759                 // Check for an entry in network_api_config
760                 $GLOBALS[__FUNCTION__][$networkId] = (countSumTotalData(
761                         bigintval($networkId),
762                         'network_api_config',
763                         'network_id',
764                         'network_id',
765                         true
766                 ) == 1);
767         } // END - if
768
769         // Return cache
770         return $GLOBALS[__FUNCTION__][$networkId];
771 }
772
773 //------------------------------------------------------------------------------
774 //                             Call-back functions
775 //------------------------------------------------------------------------------
776
777 // Callback function to add new network
778 function doAdminNetworkProcessAddNetwork () {
779         // We can say here, the form is sent, so check if the network is already added
780         if (isNetworkNameValid(postRequestElement('network_short_name'))) {
781                 // Already there
782                 loadTemplate('admin_settings_unsaved', false, '{%message,ADMIN_NETWORK_ALREADY_ADDED=' . postRequestElement('network_short_name') . '%}');
783                 return false;
784         } // END - if
785
786         // Remove the 'ok' part
787         unsetPostRequestElement('ok');
788
789         // Add the whole request to database
790         SQL_QUERY('INSERT INTO
791         `{?_MYSQL_PREFIX?}_network_data`
792 (
793         `' . implode('`,`', array_keys(postRequestArray())) . "`
794 ) VALUES (
795         '" . implode("','", array_values(postRequestArray())) . "'
796 )", __FUNCTION__, __LINE__);
797
798         // Add the id for output only
799         setPostRequestElement('network_id', SQL_INSERTID());
800
801         // Output message
802         if (!SQL_HASZEROAFFECTED()) {
803                 // Successfully added
804                 loadTemplate('admin_network_added', false, postRequestArray());
805         } else {
806                 // Not added
807                 loadTemplate('admin_settings_unsaved', false, '{%message,ADMIN_NETWORK_DATA_NOT_ADDED=' . postRequestElement('network_short_name') . '%}');
808         }
809 }
810
811 // Displays selected networks for editing
812 function doAdminNetworkProcessHandleNetwork () {
813         // Do we have selections?
814         if (ifPostContainsSelections()) {
815                 // Something has been selected, so start displaying one by one
816                 $OUT = '';
817                 foreach (postRequestElement('sel') as $networkId => $sel) {
818                         // Is this selected?
819                         if ($sel == 1) {
820                                 // Load this network's data
821                                 $networkData = getNetworkDataById($networkId);
822
823                                 // Do we have found the network?
824                                 if (count($networkData) > 0) {
825                                         if (isFormSent('edit')) {
826                                                 // Add row template for editing
827                                                 $OUT .= loadTemplate('admin_edit_networks_row', true, $networkData);
828                                         } elseif (isFormSent('delete')) {
829                                                 // Add row template for deleting
830                                                 $OUT .= loadTemplate('admin_delete_networks_row', true, $networkData);
831                                         } else {
832                                                 // Problem!
833                                                 debug_report_bug(__FUNCTION__, __LINE__, 'Cannot detect edit/del.');
834                                         }
835                                 } // END - if
836                         } // END - if
837                 } // END - foreach
838
839                 // If we have no rows, we don't need to display the edit form
840                 if (!empty($OUT)) {
841                         // Output main template
842                         if (isFormSent('edit')) {
843                                 loadTemplate('admin_edit_networks', false, $OUT);
844                         } elseif (isFormSent('delete')) {
845                                 loadTemplate('admin_delete_networks', false, $OUT);
846                         } else {
847                                 // Problem!
848                                 debug_report_bug(__FUNCTION__, __LINE__, 'Cannot detect edit/del.');
849                         }
850
851                         // Don't display the list/add new form
852                         $GLOBALS['network_display'] = false;
853                 } else {
854                         // Nothing selected/found
855                         loadTemplate('admin_settings_unsaved', false, '{--ADMIN_NETWORK_NOTHING_FOUND--}');
856                 }
857         } // END - if
858 }
859
860 // Handle network type form
861 function doAdminNetworkProcessHandleNetworkType () {
862         // Do we have selections?
863         if (ifPostContainsSelections()) {
864                 // Load network data
865                 $networkData = getNetworkDataById(getRequestElement('network'));
866
867                 // Something has been selected, so start displaying one by one
868                 $OUT = '';
869                 foreach (postRequestElement('sel') as $networkId => $sel) {
870                         // Is this selected?
871                         if ($sel == 1) {
872                                 // Load this network's data
873                                 $networkTypeData = getNetworkTypeDataById($networkId);
874
875                                 // Do we have found the network?
876                                 if (count($networkTypeData) > 0) {
877                                         if (isFormSent('edit')) {
878                                                 // Add row template for deleting
879                                                 $OUT .= loadTemplate('admin_edit_network_types_row', true, $networkTypeData);
880                                         } elseif (isFormSent('delete')) {
881                                                 // Add row template for deleting
882                                                 $OUT .= loadTemplate('admin_delete_network_types_row', true, $networkTypeData);
883                                         } else {
884                                                 // Problem!
885                                                 debug_report_bug(__FUNCTION__, __LINE__, 'Cannot detect edit/del.');
886                                         }
887                                 } // END - if
888                         } // END - if
889                 } // END - foreach
890
891                 // If we have no rows, we don't need to display the edit form
892                 if (!empty($OUT)) {
893                         // Output main template
894                         if (isFormSent('edit')) {
895                                 loadTemplate('admin_edit_network_types', false, $OUT);
896                         } elseif (isFormSent('delete')) {
897                                 loadTemplate('admin_delete_network_types', false, $OUT);
898                         } else {
899                                 // Problem!
900                                 debug_report_bug(__FUNCTION__, __LINE__, 'Cannot detect edit/del.');
901                         }
902
903                         // Don't display the list/add new form
904                         $GLOBALS['network_display'] = false;
905                 } else {
906                         // Nothing selected/found
907                         loadTemplate('admin_settings_unsaved', false, '{--ADMIN_NETWORK_TYPES_NOTHING_FOUND--}');
908                 }
909         } // END - if
910 }
911
912 // Handle network request parameter form
913 function doAdminNetworkProcessHandleRequestParams () {
914         // Do we have selections?
915         if (ifPostContainsSelections()) {
916                 // Init cache array
917                 $GLOBALS['network_params_disabled'] = array();
918
919                 // Load network data
920                 $networkData = getNetworkDataById(getRequestElement('network'));
921
922                 // Something has been selected, so start displaying one by one
923                 $OUT = '';
924                 foreach (postRequestElement('sel') as $networkId => $sel) {
925                         // Is this selected?
926                         if ($sel == 1) {
927                                 // Load this network's data
928                                 $networkRequestData = getNetworkRequestParamsDataById($networkId);
929
930                                 // Do we have found the network?
931                                 if (count($networkRequestData) > 0) {
932                                         if (isFormSent('edit')) {
933                                                 // Add row template for deleting
934                                                 $OUT .= loadTemplate('admin_edit_network_params_row', true, $networkRequestData);
935                                         } elseif (isFormSent('delete')) {
936                                                 // Get type data
937                                                 $networkRequestData['network_type_data'] = getNetworkTypeDataById($networkRequestData['network_type_id']);
938
939                                                 // Add row template for deleting
940                                                 $OUT .= loadTemplate('admin_delete_network_params_row', true, $networkRequestData);
941                                         } else {
942                                                 // Problem!
943                                                 debug_report_bug(__FUNCTION__, __LINE__, 'Cannot detect edit/del.');
944                                         }
945                                 } // END - if
946                         } // END - if
947                 } // END - foreach
948
949                 // If we have no rows, we don't need to display the edit form
950                 if (!empty($OUT)) {
951                         // Output main template
952                         if (isFormSent('edit')) {
953                                 loadTemplate('admin_edit_network_params', false, $OUT);
954                         } elseif (isFormSent('delete')) {
955                                 loadTemplate('admin_delete_network_params', false, $OUT);
956                         } else {
957                                 // Problem!
958                                 debug_report_bug(__FUNCTION__, __LINE__, 'Cannot detect edit/del.');
959                         }
960
961                         // Don't display the list/add new form
962                         $GLOBALS['network_display'] = false;
963                 } else {
964                         // Nothing selected/found
965                         loadTemplate('admin_settings_unsaved', false, '{--ADMIN_NETWORK_REQUEST_PARAMETER_NOTHING_FOUND--}');
966                 }
967         } // END - if
968 }
969
970 // Changes given networks
971 function doAdminNetworkProcessChangeNetwork () {
972         // Do we have selections?
973         if (ifPostContainsSelections()) {
974                 // By default nothing is updated
975                 $updated = 0;
976
977                 // Something has been selected, so start updating them
978                 foreach (postRequestElement('sel') as $networkId => $sel) {
979                         // Update this entry?
980                         if ($sel == 1) {
981                                 // Init data array
982                                 $networkData = array();
983
984                                 // Transfer whole array, except 'sel'
985                                 foreach (postRequestArray() as $key => $entry) {
986                                         // Skip 'sel' and submit button
987                                         if (in_array($key, array('sel', 'do_edit'))) {
988                                                 continue;
989                                         } // END - if
990
991                                         // Do we have this enty?
992                                         if (!isset($entry[$networkId])) {
993                                                 // Not found, needs fixing
994                                                 debug_report_bug(__FUNCTION__, __LINE__, 'No entry in key=' . $key . ', id=' . $networkId . ' found.');
995                                         } // END - if
996
997                                         // Add this entry
998                                         $networkData[$key] = $entry[$networkId];
999                                 } // END - foreach
1000
1001                                 // Update the network data
1002                                 $updated += doNetworkUpdateDataByArray($networkId, $networkData);
1003                         } // END - if
1004                 } // END - foreach
1005
1006                 // Do we have updates?
1007                 if ($updated > 0) {
1008                         // Updates done
1009                         displayMessage('{%message,ADMIN_NETWORK_UPDATED=' . $updated . '%}');
1010                 } else {
1011                         // Nothing changed
1012                         loadTemplate('admin_settings_unsaved', false, '{--ADMIN_NETWORK_NOTHING_CHANGED--}');
1013                 }
1014         } // END - if
1015 }
1016
1017 // Removes given networks
1018 function doAdminNetworkProcessRemoveNetwork () {
1019         // Do we have selections?
1020         if (ifPostContainsSelections()) {
1021                 // By default nothing is removed
1022                 $removed = 0;
1023
1024                 // Something has been selected, so start updating them
1025                 foreach (postRequestElement('sel') as $networkId => $sel) {
1026                         // Update this entry?
1027                         if ($sel == 1) {
1028                                 // Remove this entry
1029                                 $removed += doAdminRemoveNetworkEntry('data', 'network_id', $networkId);
1030                         } // END - if
1031                 } // END - foreach
1032
1033                 // Do we have removes?
1034                 if ($removed > 0) {
1035                         // Removals done
1036                         displayMessage('{%message,ADMIN_NETWORK_REMOVED=' . $removed . '%}');
1037                 } else {
1038                         // Nothing removed
1039                         loadTemplate('admin_settings_unsaved', false, '{--ADMIN_NETWORK_NOTHING_REMOVED--}');
1040                 }
1041         } // END - if
1042 }
1043
1044 // Add a network type handler if not yet found
1045 function doAdminNetworkProcessAddNetworkType () {
1046         // Is the network type handle already used with given network?
1047         if (isNetworkTypeHandleValid(postRequestElement('network_type_handle'), getRequestElement('network'))) {
1048                 // Already added
1049                 loadTemplate('admin_settings_unsaved', false, '{%message,ADMIN_NETWORK_TYPES_HANDLE_ALREADY_ADDED=' . postRequestElement('network_type_handle') . '%}');
1050
1051                 // ... so abort here
1052                 return false;
1053         } // END - if
1054
1055         // Remove the 'ok' part
1056         unsetPostRequestElement('ok');
1057
1058         // Add id
1059         setPostRequestElement('network_id', bigintval(getRequestElement('network')));
1060
1061         // Is network_type_banner_url set?
1062         if (postRequestElement('network_type_banner_url') == '') {
1063                 // Remove empty value to get a NULL for an optional entry
1064                 unsetPostRequestElement('network_type_banner_url');
1065         } // END - if
1066
1067         // Add the whole request to database
1068         SQL_QUERY('INSERT INTO
1069         `{?_MYSQL_PREFIX?}_network_types`
1070 (
1071         `' . implode('`,`', array_keys(postRequestArray())) . "`
1072 ) VALUES (
1073         '" . implode("','", array_values(postRequestArray())) . "'
1074 )", __FUNCTION__, __LINE__);
1075
1076         // Output message
1077         if (!SQL_HASZEROAFFECTED()) {
1078                 // Successfully added
1079                 loadTemplate('admin_network_type_added', false, postRequestArray());
1080         } else {
1081                 // Not added
1082                 loadTemplate('admin_settings_unsaved', false, '{%message,ADMIN_NETWORK_TYPES_NOT_ADDED=' . postRequestElement('network_type_handle') . '%}');
1083         }
1084 }
1085
1086 // Changes given network type handlers
1087 function doAdminNetworkProcessChangeNetworkType () {
1088         // Do we have selections?
1089         if (ifPostContainsSelections()) {
1090                 // By default nothing is updated
1091                 $updated = 0;
1092
1093                 // Something has been selected, so start updating them
1094                 foreach (postRequestElement('sel') as $networkId => $sel) {
1095                         // Update this entry?
1096                         if ($sel == 1) {
1097                                 // Init data array
1098                                 $networkTypeData = array();
1099
1100                                 // Transfer whole array, except 'sel'
1101                                 foreach (postRequestArray() as $key => $entry) {
1102                                         // Skip 'sel' and submit button
1103                                         if (in_array($key, array('sel', 'do_edit'))) {
1104                                                 continue;
1105                                         } // END - if
1106
1107                                         // Do we have this enty?
1108                                         if (!isset($entry[$networkId])) {
1109                                                 // Not found, needs fixing
1110                                                 debug_report_bug(__FUNCTION__, __LINE__, 'No entry in key=' . $key . ', id=' . $networkId . ' found.');
1111                                         } // END - if
1112
1113                                         // Fix empty network_type_banner_url to NULL
1114                                         if (($key == 'network_type_banner_url') && (trim($entry[$networkId]) == '')) {
1115                                                 // Set it to NULL
1116                                                 $entry[$networkId] = NULL;
1117                                         } // END - if
1118
1119                                         // Add this entry
1120                                         $networkTypeData[$key] = $entry[$networkId];
1121                                 } // END - foreach
1122
1123                                 // Update the network data
1124                                 $updated += doNetworkUpdateTypeByArray($networkId, $networkTypeData);
1125                         } // END - if
1126                 } // END - foreach
1127
1128                 // Do we have updates?
1129                 if ($updated > 0) {
1130                         // Updates done
1131                         displayMessage('{%message,ADMIN_NETWORK_TYPES_UPDATED=' . $updated . '%}');
1132                 } else {
1133                         // Nothing changed
1134                         loadTemplate('admin_settings_unsaved', false, '{--ADMIN_NETWORK_TYPES_NOTHING_CHANGED--}');
1135                 }
1136         } // END - if
1137 }
1138
1139 // Changes given network request parameters
1140 function doAdminNetworkProcessChangeNetworkParam () {
1141         // Do we have selections?
1142         if (ifPostContainsSelections()) {
1143                 // By default nothing is updated
1144                 $updated = 0;
1145
1146                 // Something has been selected, so start updating them
1147                 foreach (postRequestElement('sel') as $networkId => $sel) {
1148                         // Update this entry?
1149                         if ($sel == 1) {
1150                                 // Init data array
1151                                 $networkParamsData = array();
1152
1153                                 // Transfer whole array, except 'sel'
1154                                 foreach (postRequestArray() as $key => $entry) {
1155                                         // Skip 'sel' and submit button
1156                                         if (in_array($key, array('sel', 'do_edit'))) {
1157                                                 continue;
1158                                         } // END - if
1159
1160                                         // Do we have this enty?
1161                                         if (!isset($entry[$networkId])) {
1162                                                 // Not found, needs fixing
1163                                                 debug_report_bug(__FUNCTION__, __LINE__, 'No entry in key=' . $key . ', id=' . $networkId . ' found.');
1164                                         } // END - if
1165
1166                                         // Fix empty request_param_default to NULL
1167                                         if (($key == 'request_param_default') && (trim($entry[$networkId]) == '')) {
1168                                                 // Set it to NULL
1169                                                 $entry[$networkId] = NULL;
1170                                         } // END - if
1171
1172                                         // Add this entry
1173                                         $networkParamsData[$key] = $entry[$networkId];
1174                                 } // END - foreach
1175
1176                                 // Update the network data
1177                                 $updated += doNetworkUpdateParamsByArray($networkId, $networkParamsData);
1178                         } // END - if
1179                 } // END - foreach
1180
1181                 // Do we have updates?
1182                 if ($updated > 0) {
1183                         // Updates done
1184                         displayMessage('{%message,ADMIN_NETWORK_REQUEST_PARAMETER_UPDATED=' . $updated . '%}');
1185                 } else {
1186                         // Nothing changed
1187                         loadTemplate('admin_settings_unsaved', false, '{--ADMIN_NETWORK_REQUEST_PARAMETER_NOTHING_CHANGED--}');
1188                 }
1189         } // END - if
1190 }
1191
1192 // Removes given network type handlers
1193 function doAdminNetworkProcessRemoveNetworkType () {
1194         // Do we have selections?
1195         if (ifPostContainsSelections()) {
1196                 // By default nothing is removed
1197                 $removed = 0;
1198
1199                 // Something has been selected, so start updating them
1200                 foreach (postRequestElement('sel') as $networkId => $sel) {
1201                         // Update this entry?
1202                         if ($sel == 1) {
1203                                 // Remove this entry
1204                                 $removed += doAdminRemoveNetworkEntry('types', 'network_type_id', $networkId);
1205                         } // END - if
1206                 } // END - foreach
1207
1208                 // Do we have removes?
1209                 if ($removed > 0) {
1210                         // Removals done
1211                         displayMessage('{%message,ADMIN_NETWORK_TYPES_REMOVED=' . $removed . '%}');
1212                 } else {
1213                         // Nothing removed
1214                         loadTemplate('admin_settings_unsaved', false, '{--ADMIN_NETWORK_TYPES_NOTHING_REMOVED--}');
1215                 }
1216         } // END - if
1217 }
1218
1219 // Removes given network request parameters
1220 function doAdminNetworkProcessRemoveNetworkParam () {
1221         // Do we have selections?
1222         if (ifPostContainsSelections()) {
1223                 // By default nothing is removed
1224                 $removed = 0;
1225
1226                 // Something has been selected, so start updating them
1227                 foreach (postRequestElement('sel') as $networkId => $sel) {
1228                         // Update this entry?
1229                         if ($sel == 1) {
1230                                 // Remove this entry
1231                                 $removed += doAdminRemoveNetworkEntry('request_params', 'network_param_id', $networkId);
1232                         } // END - if
1233                 } // END - foreach
1234
1235                 // Do we have removes?
1236                 if ($removed > 0) {
1237                         // Removals done
1238                         displayMessage('{%message,ADMIN_NETWORK_REQUEST_PARAMETER_REMOVED=' . $removed . '%}');
1239                 } else {
1240                         // Nothing removed
1241                         loadTemplate('admin_settings_unsaved', false, '{--ADMIN_NETWORK_REQUEST_PARAMETER_NOTHING_REMOVED--}');
1242                 }
1243         } // END - if
1244 }
1245
1246 // Adds a request parameter to given network and type
1247 function doAdminNetworkProcessAddNetworkParam () {
1248         // Is the request parameter already used with given network?
1249         if (isNetworkRequestElementValid(postRequestElement('request_param_key'), postRequestElement('network_type_id'), getRequestElement('network'))) {
1250                 // Already added
1251                 loadTemplate('admin_settings_unsaved', false, '{%message,ADMIN_NETWORK_REQUEST_PARAMETER_ALREADY_ADDED=' . postRequestElement('request_param_key') . '%}');
1252
1253                 // ... so abort here
1254                 return false;
1255         } // END - if
1256
1257         // Remove the 'ok' part
1258         unsetPostRequestElement('ok');
1259
1260         // Add id
1261         setPostRequestElement('network_id', bigintval(getRequestElement('network')));
1262
1263         // Is request_param_default set?
1264         if (postRequestElement('request_param_default') == '') {
1265                 // Remove empty value to get a NULL for an optional entry
1266                 unsetPostRequestElement('request_param_default');
1267         } // END - if
1268
1269         // Add the whole request to database
1270         SQL_QUERY('INSERT INTO
1271         `{?_MYSQL_PREFIX?}_network_request_params`
1272 (
1273         `' . implode('`,`', array_keys(postRequestArray())) . "`
1274 ) VALUES (
1275         '" . implode("','", array_values(postRequestArray())) . "'
1276 )", __FUNCTION__, __LINE__);
1277
1278         // Output message
1279         if (!SQL_HASZEROAFFECTED()) {
1280                 // Successfully added
1281                 loadTemplate('admin_network_request_param_added', false, postRequestArray());
1282         } else {
1283                 // Not added
1284                 loadTemplate('admin_settings_unsaved', false, '{%message,ADMIN_NETWORK_REQUEST_PARAMETER_NOT_ADDED=' . postRequestElement('request_param_key') . '%}');
1285         }
1286 }
1287
1288 // Adds a API response array entry
1289 function doAdminNetworkProcessAddNetworkApiTranslation () {
1290         // Is the request parameter already used with given network?
1291         if (isNetworkApiTranslationValid(postRequestElement('network_api_index'), postRequestElement('network_type_id'), getRequestElement('network'))) {
1292                 // Already added
1293                 loadTemplate('admin_settings_unsaved', false, '{%message,ADMIN_NETWORK_API_TRANSLATION_ALREADY_ADDED=' . postRequestElement('network_api_index') . '%}');
1294
1295                 // ... so abort here
1296                 return false;
1297         } // END - if
1298
1299         // Remove the 'ok' part
1300         unsetPostRequestElement('ok');
1301
1302         // Add id
1303         setPostRequestElement('network_id', bigintval(getRequestElement('network')));
1304
1305         // Add sorting
1306         setPostRequestElement('sort', (countSumTotalData(
1307                 bigintval(postRequestElement('network_id')),
1308                 'network_api_translation',
1309                 'network_api_id',
1310                 'network_id',
1311                 true,
1312                 sprintf(" AND `network_type_id`=%s", bigintval(postRequestElement('network_type_id')))
1313         ) + 1));
1314
1315         // Add the whole request to database
1316         SQL_QUERY('INSERT INTO
1317         `{?_MYSQL_PREFIX?}_network_api_translation`
1318 (
1319         `' . implode('`,`', array_keys(postRequestArray())) . "`
1320 ) VALUES (
1321         '" . implode("','", array_values(postRequestArray())) . "'
1322 )", __FUNCTION__, __LINE__);
1323
1324         // Output message
1325         if (!SQL_HASZEROAFFECTED()) {
1326                 // Successfully added
1327                 loadTemplate('admin_network_api_translation_added', false, postRequestArray());
1328         } else {
1329                 // Not added
1330                 loadTemplate('admin_settings_unsaved', false, '{%message,ADMIN_NETWORK_API_TRANSLATION_NOT_ADDED=' . postRequestElement('network_api_index') . '%}');
1331         }
1332 }
1333
1334 // Adds/update network API configuration
1335 function doAdminNetworkProcessNetworkApiConfig () {
1336         // Remove the 'ok' part
1337         unsetPostRequestElement('ok');
1338
1339         // Add id
1340         setPostRequestElement('network_id', bigintval(getRequestElement('network')));
1341
1342         // Is there already an entry?
1343         if (isNetworkApiConfigured(getRequestElement('network'))) {
1344                 // Generate SQL query
1345                 $SQL = getUpdateSqlFromArray(postRequestArray(), 'network_api_config', 'network_id', postRequestElement('network_id'), array('network_id'));
1346         } else {
1347                 // Insert new entry
1348                 $SQL = 'INSERT INTO
1349         `{?_MYSQL_PREFIX?}_network_api_config`
1350 (
1351         `' . implode('`,`', array_keys(postRequestArray())) . "`
1352 ) VALUES (
1353         '" . implode("','", array_values(postRequestArray())) . "'
1354 )";
1355         }
1356
1357         // Run the query
1358         SQL_QUERY($SQL, __FUNCTION__, __LINE__);
1359
1360         // Output message
1361         if (!SQL_HASZEROAFFECTED()) {
1362                 // Successfully added
1363                 displayMessage('{--ADMIN_CONFIG_NETWORK_API_SAVED--}');
1364         } else {
1365                 // Not added
1366                 loadTemplate('admin_settings_unsaved', false, '{--ADMIN_CONFIG_NETWORK_API_NOT_SAVED--}');
1367         }
1368 }
1369
1370 // Do expression code for this extension
1371 function doExpressionNetwork ($data) {
1372         // Construct replacer
1373         $replacer = sprintf(
1374                 "{DQUOTE} . %s(%s, '%s') . {DQUOTE}",
1375                 $data['callback'],
1376                 $data['matches'][4][$data['key']],
1377                 $data['extra_func']
1378         );
1379
1380         // Replace %network% with the current network id
1381         $replacer = str_replace('%network%', getCurrentNetworkId(), $replacer);
1382
1383         // Replace the code
1384         $code = replaceExpressionCode($data, $replacer);
1385
1386         // Return it
1387         return $code;
1388 }
1389
1390 // [EOF]
1391 ?>