A lot while() conditions rewritten to SQL_FETCHARRAY(), see bug #107, @TODO tags...
[mailer.git] / inc / modules / member / what-categories.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 10/19/2003 *
4  * ===============                              Last change: 06/30/2004 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : what-categories.php                              *
8  * -------------------------------------------------------------------- *
9  * Short description : Select or unselect categories                    *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Kategorien ab- bzw. anwaehlen                    *
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')) {
36         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
37         require($INC);
38 } elseif (!IS_MEMBER()) {
39         LOAD_URL("modules.php[13~?module=index");
40 }
41
42 // Add description as navigation point
43 ADD_DESCR("member", __FILE__);
44
45 $UID = $GLOBALS['userid'];
46 $whereStatement = " WHERE `visible`='Y'";
47 if (IS_ADMIN()) $whereStatement = "";
48
49 $result = SQL_QUERY("SELECT id, cat FROM `{!_MYSQL_PREFIX!}_cats`".$whereStatement." ORDER BY `sort`", __FILE__, __LINE__);
50 $cats = SQL_NUMROWS($result);
51
52 if ($cats > 0) {
53         $LEAST = false;
54         if (IS_FORM_SENT()) {
55                 $cnt = 0;
56                 foreach (REQUEST_POST('cat') as $cat => $joined) {
57                         if ($joined == "N") $cnt++;
58                 }
59
60                 if (($cats - $cnt) < getConfig('least_cats')) {
61                         REQUEST_UNSET_POST('ok');
62                         $LEAST = true;
63                 }
64         }
65
66         if (IS_FORM_SENT()) {
67                 foreach (REQUEST_POST('cat') as $cat => $joined) {
68                         switch ($joined) {
69                                 case "Y":
70                                         $sql = "";
71                                         $result_user = SQL_QUERY_ESC("SELECT id FROM `{!_MYSQL_PREFIX!}_user_cats` WHERE userid=%s AND cat_id=%s LIMIT 1",
72                                          array($UID, bigintval($cat)), __FILE__, __LINE__);
73
74                                         if (SQL_NUMROWS($result_user) == 0) {
75                                                 $sql = "INSERT INTO `{!_MYSQL_PREFIX!}_user_cats` (userid, cat_id) VALUES ('%s','%s')";
76                                         } else {
77                                                 // Free memory
78                                                 SQL_FREERESULT($result_user);
79                                         }
80                                         break;
81
82                                 case "N":
83                                         $sql = "DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_user_cats` WHERE userid=%s AND cat_id=%s LIMIT 1";
84                                         break;
85                         }
86
87                         if (!empty($sql)) {
88                                 // Run SQL command
89                                 $result = SQL_QUERY_ESC(trim($sql), array($UID, bigintval($cat)), __FILE__, __LINE__);
90                         }
91                 }
92
93                 // Categories saved...
94                 LOAD_TEMPLATE("admin_settings_saved", true, getMessage('MEMBER_CATS_SAVED'));
95         } else {
96                 if ($LEAST) {
97                         // Also here we have to secure it... :(
98                         LOAD_TEMPLATE("admin_settings_saved", false, sprintf(getMessage('CATS_LEAST'), getConfig('least_cats')));
99                 }
100                 // Put some data into constants for the template
101                 define('__ROWS', ($cats*2+4));
102
103                 // Load header template
104                 LOAD_TEMPLATE("member_cats_header");
105
106                 // Start switching colors and load all visible categories
107                 // @TODO Rewrite this to use $OUT .= ...
108                 $OUT = ""; $SW = 2;
109                 while ($content = SQL_FETCHARRAY($result)) {
110                         // Default he has not joined
111                         $content['jn'] = " checked=\"checked\"";
112                         $content['jy'] = "";
113
114                         // When we found an entry don't read it, just change the jx elements
115                         if (REQUEST_ISSET_POST(('cat'))) {
116                                 // Form sent?
117                                 if (REQUEST_POST('cat', $content['id']) =='Y') {
118                                         $content['jy'] = " checked=\"checked\"";
119                                         $content['jn'] = "";
120                                 }
121                         } else {
122                                 // Check if he has an entry
123                                 if (GET_TOTAL_DATA($UID, "user_cats", "id", "userid", true, sprintf(" AND cat_id=%s", bigintval($content['id']))) == 1) {
124                                         $content['jn'] = "";
125                                         $content['jy'] = " checked=\"checked\"";
126                                 }
127                         }
128
129                         // Add some more
130                         $content['sw'] = $SW;
131
132                         // Load row template and switch colors
133                         LOAD_TEMPLATE("member_cat_row", false, $content);
134                         $SW = 3 - $SW;
135                 }
136
137                 // Load footer template
138                 LOAD_TEMPLATE("member_cats_footer");
139         }
140 } else {
141         // No cateogries are defined yet
142         LOAD_TEMPLATE("admin_settings_saved", true, getMessage('MEMBER_NO_CATS'));
143 }
144
145 // Free result
146 SQL_FREERESULT($result);
147
148 //
149 ?>