A lot while() conditions rewritten to SQL_FETCHARRAY(), see bug #107, @TODO tags...
[mailer.git] / inc / modules / admin / what-refbanner.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 10/19/2003 *
4  * ===============                              Last change: 07/13/2004 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : what-refbanner.php                               *
8  * -------------------------------------------------------------------- *
9  * Short description : Manage all referal banner                        *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Alle Banner fuer die Ref-Links verwalten         *
12  * -------------------------------------------------------------------- *
13  *                                                                      *
14  * -------------------------------------------------------------------- *
15  * Copyright (c) 2003 - 2008 by Roland Haeder                           *
16  * For more information visit: http://www.mxchange.org                  *
17  *                                                                      *
18  * This program is free software; you can redistribute it and/or modify *
19  * it under the terms of the GNU General Public License as published by *
20  * the Free Software Foundation; either version 2 of the License, or    *
21  * (at your option) any later version.                                  *
22  *                                                                      *
23  * This program is distributed in the hope that it will be useful,      *
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
26  * GNU General Public License for more details.                         *
27  *                                                                      *
28  * You should have received a copy of the GNU General Public License    *
29  * along with this program; if not, write to the Free Software          *
30  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
31  * MA  02110-1301  USA                                                  *
32  ************************************************************************/
33
34 // Some security stuff...
35 if ((!defined('__SECURITY')) || (!IS_ADMIN())) {
36         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
37         require($INC);
38 }
39
40 // Add description as navigation point
41 ADD_DESCR("admin", __FILE__);
42
43 // Init variable
44 $SEL = 0;
45
46 // Some sanity-check
47 if ((!REQUEST_ISSET_POST(('url'))) || (!REQUEST_ISSET_POST(('alternate')))) {
48         REQUEST_UNSET_POST('ok');
49 }
50
51 // Check selection count
52 if (REQUEST_ISSET_POST('sel')) $SEL = SELECTION_COUNT(REQUEST_POST('sel'));
53
54 if (IS_FORM_SENT()) {
55         // Fix older calls from add-new-banner-form
56         if (!REQUEST_ISSET_GET(('mode'))) REQUEST_SET_GET('mode', "add");
57         $sql = "";
58         switch (REQUEST_GET('mode'))
59         {
60         case "add":
61                 // Check if banner is already added
62                 $result = SQL_QUERY_ESC("SELECT id FROM `{!_MYSQL_PREFIX!}_refbanner` WHERE url='%s' LIMIT 1",
63                         array(REQUEST_POST('url')), __FILE__, __LINE__);
64                 if (SQL_NUMROWS($result) == 0) {
65                         // Add banner
66                         SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_refbanner` (url, alternate, visible)
67 VALUES ('%s','%s','%s')",
68                                 array(REQUEST_POST('url'), REQUEST_POST('alternate'), REQUEST_POST('visible')), __FILE__, __LINE__);
69                 } else {
70                         // Free memory
71                         SQL_FREERESULT($result);
72                 }
73                 break;
74
75         case "edit": // Update banner
76                 foreach (REQUEST_POST('sel') as $id => $sel) {
77                         // Secure ID
78                         $id = bigintval($id);
79
80                         // Update entry
81                         SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_refbanner` SET url='%s', alternate='%s', `visible`='%s' WHERE id=%s LIMIT 1",
82                                 array(
83                                         REQUEST_POST('url', $id),
84                                         REQUEST_POST('alternate', $id),
85                                         REQUEST_POST('visible'),
86                                         $id
87                                 ), __FILE__, __LINE__);
88                 }
89                 break;
90         }
91
92         if (SQL_AFFECTEDROWS() == 1) {
93                 $content = getMessage('SETTINGS_SAVED');
94         } else {
95                 $content = "<span class=\"admin_failed\">{--SETTINGS_NOT_SAVED--}</span>";
96         }
97         LOAD_TEMPLATE("admin_settings_saved", false, $content);
98 } elseif (($SEL > 0) && (REQUEST_ISSET_POST('edit'))) {
99         // Edit banner
100         $SW = ""; $OUT = "";
101         foreach (REQUEST_POST('sel') as $id => $sel) {
102                 // Load data
103                 $result = SQL_QUERY_ESC("SELECT url, alternate, visible FROM `{!_MYSQL_PREFIX!}_refbanner` WHERE id=%s LIMIT 1",
104                         array(bigintval($id)), __FILE__, __LINE__);
105                 list($url, $alt, $vis) = SQL_FETCHROW($result);
106                 SQL_FREERESULT($result);
107
108                 // Preapre data for the row
109                 $content = array(
110                         'sw'  => $SW,
111                         'id'  => $id,
112                         'url' => $url,
113                         'alt' => $alt,
114                         'vis' => ADD_SELECTION("yn", $vis   , "visible"),
115                 );
116
117                 // Load row template and switch color
118                 $OUT .= LOAD_TEMPLATE("admin_refbanner_edit_row", true, $content);
119                 $SW = 3 - $SW;
120         }
121
122         // @TODO Rewrite this constant
123         define('__BANNER_ROWS', $OUT);
124
125         // Load main template
126         LOAD_TEMPLATE("admin_refbanner_edit");
127 } else {
128         if (($SEL > 0) && (REQUEST_ISSET_POST('del'))) {
129                 // Delete banner
130                 foreach (REQUEST_POST('sel') as $id => $sel) {
131                         SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_refbanner` WHERE id=%s LIMIT 1",
132                                 array(bigintval($id)), __FILE__, __LINE__);
133                 }
134         }
135
136         // Referal levels
137         $result = SQL_QUERY("SELECT id, url, alternate, visible, counter, clicks FROM `{!_MYSQL_PREFIX!}_refbanner` ORDER BY url",
138                 __FILE__, __LINE__);
139         if (SQL_NUMROWS($result) > 0) {
140                 // Make referal banner editable and deletable
141                 $OUT = ""; $SW = 2;
142                 while ($content = SQL_FETCHARRAY($result)) {
143                         // Preapre data for the row
144                         // @TODO Rewritings: alt->alternate,cnt->counter,clx->clicks in template
145                         $content = array(
146                                 'sw'  => $SW,
147                                 'id'  => $content['id'],
148                                 'url' => $content['url'],
149                                 'alt' => $content['alternate'],
150                                 'vis' => TRANSLATE_YESNO($content['visible']),
151                                 'cnt' => $content['counter'],
152                                 'clx' => $content['clicks']
153                         );
154
155                         // Load row template and switch color
156                         $OUT .= LOAD_TEMPLATE("admin_refbanner_row", true, $content);
157                         $SW = 3 - $SW;
158                 }
159
160                 // Free memory
161                 SQL_FREERESULT($result);
162
163                 // @TODO Rewrite this constant
164                 define('__BANNER_ROWS', $OUT);
165
166                 // Load main template
167                 LOAD_TEMPLATE("admin_refbanner");
168         }
169
170         // Form for adding new referal levels
171         LOAD_TEMPLATE("admin_add_banner");
172 }
173
174 //
175 ?>