Adding of network advert types partly finished, hard-coded table type fixed to dynamic
[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  * For more information visit: http://www.mxchange.org                  *
22  *                                                                      *
23  * This program is free software; you can redistribute it and/or modify *
24  * it under the terms of the GNU General Public License as published by *
25  * the Free Software Foundation; either version 2 of the License, or    *
26  * (at your option) any later version.                                  *
27  *                                                                      *
28  * This program is distributed in the hope that it will be useful,      *
29  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
31  * GNU General Public License for more details.                         *
32  *                                                                      *
33  * You should have received a copy of the GNU General Public License    *
34  * along with this program; if not, write to the Free Software          *
35  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
36  * MA  02110-1301  USA                                                  *
37  ************************************************************************/
38
39 // Some security stuff...
40 if (!defined('__SECURITY')) {
41         die();
42 } // END - if
43
44 // Handle a (maybe) sent form here
45 function doNetworkHandleForm () {
46         // Was the form sent?
47         if ((isFormSent()) || (isPostRequestElementSet('edit')) || (isPostRequestElementSet('del')) || (isPostRequestElementSet('change')) || (isPostRequestElementSet('remove'))) {
48                 // Do we have a 'do'?
49                 if (isGetRequestElementSet('do')) {
50                         // Process the request
51                         doAdminNetworkProcessForm();
52                 } else {
53                         // No 'do' found
54                         loadTemplate('admin_settings_saved', false, getMessage('ADMIN_NETWORK_DO_404'));
55                 }
56         } // END - if
57 }
58
59 // Processes an admin form
60 function doAdminNetworkProcessForm () {
61         // Form really sent?
62         if ((!isFormSent()) && (!isPostRequestElementSet('edit')) && (!isPostRequestElementSet('del')) && (!isPostRequestElementSet('change')) && (!isPostRequestElementSet('remove'))) {
63                 // Abort here
64                 loadTemplate('admin_settings_saved', false, getMessage('ADMIN_NETWORK_FORM_NOT_SENT'));
65                 return;
66         } elseif (!isGetRequestElementSet('do')) {
67                 // No 'do' found
68                 loadTemplate('admin_settings_saved', false, getMessage('ADMIN_NETWORK_DO_404'));
69                 return;
70         }
71
72         // Create function name
73         $functionName = sprintf("doAdminNetworkProcess%sForm", ucfirst(strtolower(getRequestElement('do'))));
74
75         // Is the function valid?
76         if (!function_exists($functionName)) {
77                 // Invalid function name
78                 debug_report_bug('Invalid do ' . getRequestElement('do') . ', function ' . $functionName .' does not exist.', false);
79         } // END - if
80
81         // Call-back the method handling our request
82         call_user_func($functionName);
83 }
84
85 // Checks wether the (short) network name is already used (valid)
86 function isNetworkNameValid ($name) {
87         // Query for it
88         $result = SQL_QUERY_ESC("SELECT `network_id` FROM `{?_MYSQL_PREFIX?}_network_data` WHERE `network_short_name`='%s' LIMIT 1",
89                 array($name), __FUNCTION__, __LINE__);
90
91         // Does it exist?
92         $isValid = (SQL_NUMROWS($result) == 1);
93
94         // Free result
95         SQL_FREERESULT($result);
96
97         // Return result
98         return $isValid;
99 }
100
101 // "Getter" for a network's data by provided id number
102 function getNetworkDataById ($id) {
103         // Ids lower one are not accepted
104         if ($id < 1) {
105                 // Not good, should be fixed
106                 debug_report_bug('Network id ' . $id . ' is smaller than 1.');
107         } // END - if
108
109         // By default we have no data
110         $networkData = array();
111
112         // Query for the network data
113         $result = SQL_QUERY_ESC("SELECT
114         `network_id`, `network_short_name`, `network_title`, `network_reflink`, `network_data_seperator`, `network_row_seperator`, `network_request_type`, `network_charset`
115 FROM
116         `{?_MYSQL_PREFIX?}_network_data`
117 WHERE
118         `network_id`=%s
119 LIMIT 1",
120                 array(bigintval($id)), __FUNCTION__, __LINE__);
121
122         // Do we have an entry?
123         if (SQL_NUMROWS($result) == 1) {
124                 // Then get it
125                 $networkData = SQL_FETCHARRAY($result);
126         } // END - if
127
128         // Free result
129         SQL_FREERESULT($result);
130
131         // Return result
132         return $networkData;
133 }
134
135 // Updates given network (id) with data from array
136 function doNetworkUpdateDataByArray ($id, $networkData) {
137         // Ids lower one are not accepted
138         if ($id < 1) {
139                 // Not good, should be fixed
140                 debug_report_bug('Network id ' . $id . ' is smaller than 1.');
141         } // END - if
142
143         // Just call our inner method
144         return adminSaveSettings($networkData, '_network_data', sprintf("`network_id`=%s", bigintval($id)), array(), false, false);
145 }
146
147 // Removes given network entry
148 function doAdminRemoveNetworkEntry ($table, $column, $id, $limit = 1) {
149         // Remove the entry
150         SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_network_%s` WHERE `%s`=%s LIMIT %s",
151                 array($table, $column, $id, $limit), __FILE__, __LINE__);
152
153         // Return affected rows
154         return SQL_AFFECTEDROWS();
155 }
156
157 // Generates a list of networks for given script and returns it
158 function generateAdminNetworkList () {
159         // Init output
160         $content = '';
161
162         // Query for all networks
163         $result = SQL_QUERY("SELECT
164         `network_id`, `network_title`
165 FROM
166         `{?_MYSQL_PREFIX?}_network_data`
167 ORDER BY
168         `network_title` ASC", __FILE__, __LINE__);
169
170         // Do we have entries?
171         if (SQL_NUMROWS($result) > 0) {
172                 // List all entries
173                 $rows = array();
174                 while ($row = SQL_FETCHARRAY($result)) {
175                         // Is this valid, then add it
176                         if ((is_array($row)) && (isset($row['network_id']))) $rows[] = $row;
177                 } // END - while
178
179                 // Generate the selection box
180                 $content = generateSelectionBoxFromArray($rows, 'network', 'network_id', 'network_title');
181         } else {
182                 // Nothing selected
183                 $content = loadTemplate('admin_settings_saved', false, getMessage('ADMIN_ENTRIES_404'));
184         }
185
186         // Free the result
187         SQL_FREERESULT($result);
188
189         // Return the list
190         return $content;
191 }
192
193 //------------------------------------------------------------------------------
194 //                             Call-back functions
195 //------------------------------------------------------------------------------
196
197 // Callback function to add new network
198 function doAdminNetworkProcessAddnetworkForm () {
199         // We can say here, the form is sent, so check if the network is already added
200         if (isNetworkNameValid(postRequestElement('network_short_name'))) {
201                 // Already there
202                 loadTemplate('admin_settings_saved', false, sprintf(getMessage('ADMIN_NETWORK_ALREADY_ADDED'), postRequestElement('network_short_name')));
203                 return;
204         } // END - if
205
206         // Remove the 'ok' part
207         unsetPostRequestElement('ok');
208
209         // Add the whole request to database
210         SQL_QUERY("INSERT INTO
211         `{?_MYSQL_PREFIX?}_network_data`
212 (
213         `" . implode('`,`', array_keys(postRequestArray())) . "`
214 ) VALUES (
215         '" . implode("','", array_values(postRequestArray())) . "'
216 )", __FUNCTION__, __LINE__);
217
218         // Add the id for output only
219         setRequestPostElement('network_id', SQL_INSERTID());
220
221         // Output message
222         if (SQL_AFFECTEDROWS() == 1) {
223                 // Successfully added
224                 loadTemplate('admin_network_added', false, postRequestArray());
225         } else {
226                 // Not added
227                 loadTemplate('admin_settings_saved', false, sprintf(getMessage('ADMIN_NETWORK_DATA_NOT_ADDED'), postRequestElement('network_short_name')));
228         }
229 }
230
231 // Displays selected networks for editing
232 function doAdminNetworkProcessHandlenetworkForm () {
233         // Do we have selections?
234         if (countPostSelection() > 0) {
235                 // Something has been selected, so start displaying one by one
236                 $SW = 2; $OUT = '';
237                 foreach (postRequestElement('sel') as $id => $sel) {
238                         // Is this selected?
239                         if ($sel == 1) {
240                                 // Load this network's data
241                                 $networkData = getNetworkDataById($id);
242
243                                 // Do we have found the network?
244                                 if (count($networkData) > 0) {
245                                         // Add color
246                                         $networkData['sw'] = $SW;
247
248                                         if (isPostRequestElementSet('edit')) {
249                                                 // Make selection box for network_request_type
250                                                 $networkData['network_request_type'] = generateOptionList(
251                                                         '/ARRAY/',
252                                                         array('GET','POST'),
253                                                         array(getMessage('ADMIN_NETWORK_REQUEST_TYPE_GET'), getMessage('ADMIN_NETWORK_REQUEST_TYPE_POST')),
254                                                         $networkData['network_request_type']
255                                                 );
256
257                                                 // Add row template for editing
258                                                 $OUT .= loadTemplate('admin_edit_networks_row', true, $networkData);
259                                         } elseif (isPostRequestElementSet('del')) {
260                                                 // Translate the request type
261                                                 $networkData['network_request_type'] = getMessage('ADMIN_NETWORK_REQUEST_TYPE_' . $networkData['network_request_type']);
262
263                                                 // Add row template for deleting
264                                                 $OUT .= loadTemplate('admin_del_networks_row', true, $networkData);
265                                         } else {
266                                                 // Problem!
267                                                 debug_report_bug('Cannot detect edit/del.');
268                                         }
269
270                                         // Switch colors
271                                         $SW = 3 - $SW;
272                                 } // END - if
273                         } // END - if
274                 } // END - foreach
275
276                 // If we have no rows, we don't need to display the edit form
277                 if (!empty($OUT)) {
278                         // Output main template
279                         if (isPostRequestElementSet('edit')) {
280                                 loadTemplate('admin_edit_networks', false, $OUT);
281                         } elseif (isPostRequestElementSet('del')) {
282                                 loadTemplate('admin_del_networks', false, $OUT);
283                         } else {
284                                 // Problem!
285                                 debug_report_bug('Cannot detect edit/del.');
286                         }
287
288                         // Don't display the list/add new form
289                         $GLOBALS['network_display'] = false;
290                 } else {
291                         // Nothing selected/found
292                         loadTemplate('admin_settings_saved', false, getMessage('ADMIN_NETWORK_NOTHING_FOUND'));
293                 }
294         } // END - if
295 }
296
297 // Changes given networks
298 function doAdminNetworkProcessChangenetworkForm () {
299         // Do we have selections?
300         if (countPostSelection() > 0) {
301                 // By default nothing is updated
302                 $updated = 0;
303
304                 // Something has been selected, so start updating them
305                 foreach (postRequestElement('sel') as $id => $sel) {
306                         // Update this entry?
307                         if ($sel == 1) {
308                                 // Init data array
309                                 $networkData = array();
310
311                                 // Transfer whole array, except 'sel'
312                                 foreach (postRequestArray() as $key => $entry) {
313                                         // Skip 'sel' and submit button
314                                         if (in_array($key, array('sel', 'change'))) continue;
315
316                                         // Do we have this enty?
317                                         if (!isset($entry[$id])) {
318                                                 // Not found, needs fixing
319                                                 debug_report_bug('No entry in key=' . $key . ', id=' . $id . ' found.');
320                                         } // END - if
321
322                                         // Add this entry
323                                         $networkData[$key] = $entry[$id];
324                                 } // END - foreach
325
326                                 // Update the network data
327                                 $updated += doNetworkUpdateDataByArray($id, $networkData);
328                         } // END - if
329                 } // END - foreach
330
331                 // Do we have updates?
332                 if ($updated > 0) {
333                         // Updates done
334                         loadTemplate('admin_settings_saved', false, sprintf(getMessage('ADMIN_NETWORK_UPDATED'), $updated));
335                 } else {
336                         // Nothing changed
337                         loadTemplate('admin_settings_saved', false, getMessage('ADMIN_NETWORK_NOTHING_CHANGED'));
338                 }
339         } // END - if
340 }
341
342 // Removes given networks
343 function doAdminNetworkProcessRemovenetworkForm () {
344         // Do we have selections?
345         if (countPostSelection() > 0) {
346                 // By default nothing is removed
347                 $removed = 0;
348
349                 // Something has been selected, so start updating them
350                 foreach (postRequestElement('sel') as $id => $sel) {
351                         // Update this entry?
352                         if ($sel == 1) {
353                                 // Remove this entry
354                                 $removed += doAdminRemoveNetworkEntry('data', 'network_id', $id);
355                         } // END - if
356                 } // END - foreach
357
358                 // Do we have removes?
359                 if ($removed > 0) {
360                         // Removals done
361                         loadTemplate('admin_settings_saved', false, sprintf(getMessage('ADMIN_NETWORK_REMOVED'), $removed));
362                 } else {
363                         // Nothing removed
364                         loadTemplate('admin_settings_saved', false, getMessage('ADMIN_NETWORK_NOTHING_REMOVED'));
365                 }
366         } // END - if
367 }
368
369 // [EOF]
370 ?>