'what' files should be loaded only once
[mailer.git] / inc / modules / admin / what-list_user.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                Start: 09/28/2003 *
4  * ===================                          Last change: 08/18/2004 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : what-list_user.php                               *
8  * -------------------------------------------------------------------- *
9  * Short description : List all or partial members                      *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Alle oder nur bestimmte Mitglieder auflisten     *
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')) || (!isAdmin())) {
40         die();
41 } // END - if
42
43 // Add description as navigation point
44 addYouAreHereLink('admin', __FILE__);
45
46 // Init variables
47 $MORE = '';
48
49 // Add lock reason?
50 if (isExtensionInstalledAndNewer('user', '0.3.5')) {
51         // Add them...
52         $MORE .= ', UNIX_TIMESTAMP(`lock_timestamp`) AS `lock_timestamp`';
53 } // END - if
54
55 // Is the extension 'country' installed?
56 if (isExtensionActive('country')) {
57         // Add country code
58         $MORE .= ', `country_code`';
59 } else {
60         // Add direct value
61         $MORE .= ', `country`';
62 }
63
64 // Init unset data
65 if (!isGetRequestElementSet('letter')) { setGetRequestElement('letter', '');       }
66 if (!isGetRequestElementSet('sortby')) { setGetRequestElement('sortby', 'userid'); }
67 if (!isGetRequestElementSet('page'))   { setGetRequestElement('page'  , 1);        }
68
69 // Set base URL
70 $base = '[<a href="{%url=modules.php?module=admin';
71
72 if (isGetRequestElementSet('userid')) {
73         // Secure the user id
74         $userid = bigintval(getRequestElement('userid'));
75
76         // Does the account exists?
77         $result_user = SQL_QUERY_ESC("SELECT
78         `userid`,`country`,`email`,`birth_day`,`birth_month`,`birth_year`,`last_online`,`used_points`,`emails_sent`,`joined`,`last_update`,`last_profile_sent`,`notified`,`emails_received`,`mails_confirmed`".$MORE."
79 FROM
80         `{?_MYSQL_PREFIX?}_user_data`
81 WHERE
82         `userid`=%s
83 LIMIT 1",
84                 array($userid), __FILE__, __LINE__);
85
86         // User found?
87         if (SQL_NUMROWS($result_user) == 1) {
88                 // Account found
89                 $content  = SQL_FETCHARRAY($result_user);
90
91                 // Get count/sum of refs, selected categories, unconfirmed mails
92                 $content['refs']  = countSumTotalData($userid, 'refsystem', 'counter');
93                 $content['cats']  = countSumTotalData($userid, 'user_cats', 'id', 'userid', true);
94                 $content['links'] = getTotalUnconfirmedMails($userid);
95
96                 // Add links to the numbers
97                 if ($content['links'] > 0) $content['links'] = $base . '&amp;what=list_links&amp;userid=' . $userid . '%}">' . $content['links'] . '</a>]';
98                 if ($content['refs']  > 0) $content['refs']  = $base . '&amp;what=list_refs&amp;userid=' . $userid . '%}">{%pipe,translateComma=' . $content['refs'] . '%}</a>]';
99                 if ($content['cats']  > 0) $content['cats']  = $base . '&amp;what=list_cats&amp;userid=' . $userid . '%}">{%pipe,translateComma=' . $content['cats'] . '%}</a>]';
100
101                 // Calculate timestamp for birthday
102                 $stamp = mktime(0, 0, 0, $content['birth_month'], $content['birth_day'], $content['birth_year']);
103
104                 // Is this above zero?
105                 if ($stamp > 0) {
106                         // Then use it
107                         $content['birthday'] = generateDateTime($stamp, '2');
108                 } else {
109                         // Zero or below so set zero!
110                         $content['birthday'] = generateDateTime(0, '2');
111                 }
112
113                 // Prepare data for template
114                 $content['email_link']        = generateEmailLink($content['email'], 'user_data');
115                 $content['last_online']       = generateDateTime($content['last_online'], '2');
116                 $content['joined']            = generateDateTime($content['joined'], '2');
117                 $content['last_update']       = generateDateTime($content['last_update'], '2');
118                 $content['last_profile_sent'] = generateDateTime($content['last_profile_sent'], '2');
119                 $content['total_points']      = getTotalPoints($content['userid']);
120                 $content['locked_points']     = countSumTotalData($content['userid'], 'user_points', 'locked_points') + countSumTotalData($content['userid'], 'user_points', 'locked_order_points');
121
122                 // Prepare array for filter
123                 $data = array(
124                         'userid' => $content['userid'],
125                         'points' => 0
126                 );
127
128                 // Run the filter chain
129                 $data = runFilterChain('get_own_points', $data);
130
131                 // Add more data
132                 $content['own_points']     = $data['points'];
133                 $content['lock_timestamp'] = generateDateTime($content['lock_timestamp'], '2');
134
135                 // Link to email_details if some are sent
136                 if ($content['emails_sent'] > 0) {
137                         $content['emails_sent'] = $base . '&amp;what=email_details&amp;userid=' . $content['userid'] . '%}">{%pipe,translateComma=' . $content['emails_sent'] . '%}</a>]';
138                 } // END - if
139
140                 // Clickrate
141                 $content['click_rate'] = '0';
142                 if ($content['emails_received'] > 0) {
143                         $content['click_rate'] = ($content['mails_confirmed'] / $content['emails_received'] * 100);
144                 } // END - if
145
146                 // Is the extension 'country' installed?
147                 if (isExtensionActive('country')) {
148                         // Then overwrite country information
149                         $content['country'] = generateCountryInfo($content['country_code']);
150                 } elseif ($content['country'] == '0') {
151                         // Zero id???
152                         $content['country'] = '???';
153                 }
154
155                 // Add userid
156                 $content['userid'] = $userid;
157
158                 // Load user-details template
159                 loadTemplate('admin_user_details', false, $content);
160         } else {
161                 // Account does not exist!
162                 displayMessage('{%message,ADMIN_MEMBER_404=' . $userid . '%}');
163         }
164
165         // Free the result
166         SQL_FREERESULT($result_user);
167 } else {
168         $whereStatement = '';
169         if (isGetRequestElementSet('letter')) {
170                 // List only persons w
171                 $whereStatement = " WHERE `family` LIKE '".getRequestElement('letter') . "%%'";
172         } // END - if
173
174         // Parse the status or mode parameter
175         if (isGetRequestElementSet('status')) {
176                 // Is a WHERE statement already there?
177                 if (!empty($whereStatement)) {
178                         // Then append the status column
179                         $whereStatement .= sprintf(" AND `status`='%s'", SQL_ESCAPE(secureString(strtoupper(getRequestElement('status')))));
180                 } else {
181                         // Start a new one
182                         $whereStatement = sprintf(" WHERE `status`='%s'", SQL_ESCAPE(secureString(strtoupper(getRequestElement('status')))));
183                 }
184         } elseif (isGetRequestElementSet('mode')) {
185                 // Choose what we need to list
186                 switch (getRequestElement('mode')) {
187                         case 'norefs': // Users w/o refs
188                                 if (!empty($whereStatement)) {
189                                         // Add AND statement
190                                         $whereStatement .= ' AND (`refid`=0 OR `refid` IS NULL)';
191                                 } else {
192                                         // Add WHERE statement
193                                         $whereStatement = ' WHERE (`refid`=0 OR `refid` IS NULL)';
194                                 }
195                                 break;
196
197                         case 'random_refid': // Users available for random referal id
198                                 if (!empty($whereStatement)) {
199                                         // Add AND statement
200                                         $whereStatement .= ' AND `rand_confirmed` >= {?user_min_confirmed?}';
201                                 } else {
202                                         // Add WHERE statement
203                                         $whereStatement = ' WHERE `rand_confirmed` >= {?user_min_confirmed?}';
204                                 }
205                                 break;
206
207                         default: // Invalid list mode
208                                 debug_report_bug(__FILE__, __LINE__, sprintf("Invalid list mode %s detected.", getRequestElement('mode')));
209                                 break;
210                 } // END - switch
211         } // END - if
212
213         // Generate master query string
214         $sql = sprintf("SELECT `userid`,`email`,`emails_sent`,`mails_confirmed`,`emails_received`" . $MORE . " FROM `{?_MYSQL_PREFIX?}_user_data`" . $whereStatement . " ORDER BY `%s` ASC",
215                 getRequestElement('sortby')
216         );
217
218         // Prepare SQL and run it
219         $result_master = SQL_QUERY($sql, __FILE__, __LINE__);
220
221         // Calculate page count (0.5 fixes a bug with page count)
222         if (getConfig('user_limit') == '0') {
223                 setConfigEntry('user_limit', 100);
224                 displayMessage('{--EXTENSION_WARNING_USER_LIMIT--}');
225         } // END - if
226
227         // Activate the extension please!
228         $numPages = round(SQL_NUMROWS($result_master) / getConfig('user_limit') + 0.5);
229
230         if (!isGetRequestElementSet('page'))   setGetRequestElement('page'  , 1);
231         if (!isGetRequestElementSet('offset')) setGetRequestElement('offset', getConfig('user_limit'));
232
233         // Add limitation to SQL string and run him again
234         $sql .= ' LIMIT ' . (getRequestElement('offset') * getRequestElement('page') - getRequestElement('offset')) . ', ' . getRequestElement('offset');
235         $result = SQL_QUERY($sql, __FILE__, __LINE__);
236
237         // Are there some entries?
238         if (!SQL_HASZERONUMS($result_master)) {
239                 // We have some (new?) registrations!
240                 $templateContent['user_count'] = getTotalConfirmedUser();
241
242                 // Sorting links and number of pages
243                 $templateContent['sort_links'] = addSortLinks(getRequestElement('letter'), getRequestElement('sortby'));
244                 $templateContent['num_pages'] = $numPages;
245
246                 // Column with nickname when nickname extension is present
247                 // @TODO Rewrite this into a filter
248                 $templateContent['nickname_th'] = '';
249                 if (isExtensionActive('nickname')) {
250                         // Nickname extension found
251                         $templateContent['nickname_th'] = '<td class="header_column bottom" align="center">{--NICKNAME--}</td>';
252                 } // END - if
253
254                 // Load all users
255                 $OUT = '';
256                 while ($content = SQL_FETCHARRAY($result)) {
257                         // Merge more data in
258                         $content = merge_array($content, $templateContent);
259
260                         // Get number of unconfirmed mails
261                         $content['links'] = getTotalUnconfirmedMails($content['userid']);
262                         if ($content['links'] > 0) {
263                                 $content['links'] = $base . '&amp;what=list_links&amp;userid=' . $content['userid'] . '%}">{%pipe,translateComma=' . $content['links'] . '%}</a>]';
264                         } // END - if
265
266                         // Set link to sent mails if present
267                         if ($content['emails_sent'] > 0) {
268                                 $content['emails_sent'] = $base . '&amp;what=email_details&amp;userid=' . $content['userid'] . '%}">{%pipe,translateComma=' . $content['emails_sent'] . '%}</a>]';
269                         } // END - if
270
271                         // Click rate
272                         $content['click_rate'] = '0';
273                         if ($content['emails_received'] > 0) {
274                                 $content['click_rate'] = $content['mails_confirmed'] / $content['emails_received'] * 100;
275                         } // END - if
276
277                         // Transfer data to array
278                         $content['email']          = '[<a href="' . generateEmailLink($content['email'], 'user_data') . '">' . $content['email'] . '</a>]';
279                         $content['lock_timestamp'] = generateDateTime($content['lock_timestamp'], '2');
280                         $content['refs']           = countSumTotalData($content['userid'], 'user_refs', 'id', 'userid', true);
281                         $content['own_points']     = countSumTotalData($content['userid'], 'user_points', 'points') + countSumTotalData($content['userid'], 'user_points', 'order_points');
282                         $content['locked_points']  = countSumTotalData($content['userid'], 'user_points', 'locked_points') + countSumTotalData($content['userid'], 'user_points', 'locked_order_points');
283
284                         // If we have at least one referal, make it clickable to referal list
285                         if ($content['refs'] > 0) {
286                                 $content['refs'] = $base . '&amp;what=list_refs&amp;userid=' . $content['userid'] . '%}">{%pipe,translateComma=' . $content['refs'] . '%}</a>]';
287                         } // END - if
288
289                         // Is the extension 'country' installed?
290                         // @TODO Rewrite this into a filter
291                         if (isExtensionActive('country')) {
292                                 // Then overwrite country information
293                                 $content['country'] = generateCountryInfo($content['country_code']);
294                         } elseif ($content['country'] == '') {
295                                 // Zero id???
296                                 $content['country'] = '???';
297                         }
298
299                         // Load row template and switch colors
300                         $OUT .= loadTemplate('admin_list_user_row', true, $content);
301                 } // END - while
302
303                 // Free memory
304                 SQL_FREERESULT($result);
305
306                 // Free some memory
307                 SQL_FREERESULT($result_master);
308
309                 // Remember all rows
310                 $content['rows'] = $OUT;
311
312                 // Merge more data again
313                 $content = merge_array($content, $templateContent);
314
315                 // Load main template
316                 loadTemplate('admin_list_user', false, $content);
317         } else {
318                 // No one as registered so far! :-(
319                 displayMessage('{--ADMIN_NO_NONE_REGISTERED--}');
320         }
321
322         // Free memory
323         SQL_FREERESULT($result_master);
324 }
325
326 // [EOF]
327 ?>