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