Mailer project continued:
[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 - 2012 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 $moreColumns = '';
48
49 // Add lock reason?
50 if (isExtensionInstalledAndNewer('user', '0.3.5')) {
51         // Add them...
52         $moreColumns .= ', 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         $moreColumns .= ',`country_code`';
59 } // END - if
60
61 // Init unset data
62 if (!isGetRequestElementSet('letter')) { setGetRequestElement('letter', '');       }
63 if (!isGetRequestElementSet('sortby')) { setGetRequestElement('sortby', 'userid'); }
64 if (!isGetRequestElementSet('page'))   { setGetRequestElement('page'  , 1);        }
65
66 // Set base HTML
67 $base = '[<a href="{%url=modules.php?module=admin';
68
69 if (isGetRequestElementSet('userid')) {
70         // Secure the user id
71         $userid = bigintval(getRequestElement('userid'));
72
73         // Does the account exists?
74         $result_user = SQL_QUERY_ESC('SELECT
75         `userid`,
76         `country`,
77         `email`,
78         `birth_day`,
79         `birth_month`,
80         `birth_year`,
81         `last_online`,
82         `used_points`,
83         `emails_sent`,
84         `joined`,
85         `last_update`,
86         `last_profile_sent`,
87         `notified`,
88         `emails_received`,
89         `mails_confirmed`
90         ' . $moreColumns . '
91 FROM
92         `{?_MYSQL_PREFIX?}_user_data`
93 WHERE
94         `userid`=%s
95 LIMIT 1',
96                 array($userid), __FILE__, __LINE__);
97
98         // User found?
99         if (SQL_NUMROWS($result_user) == 1) {
100                 // Account found
101                 $content  = SQL_FETCHARRAY($result_user);
102
103                 // Get count/sum of refs, selected categories, unconfirmed mails
104                 // @TODO Refacture these old link generation to functions
105                 $content['refs']  = countSumTotalData($userid, 'refsystem', 'counter');
106                 $content['cats']  = countSumTotalData($userid, 'user_cats', 'id', 'userid', true);
107                 $content['links'] = getTotalUnconfirmedMails($userid);
108
109                 // Add links to the numbers
110                 if ($content['links'] > 0) {
111                         $content['links'] = $base . '&amp;what=list_links&amp;userid=' . $userid . '%}">' . $content['links'] . '</a>]';
112                 } // END - if
113                 if ($content['refs']  > 0) {
114                         $content['refs']  = $base . '&amp;what=list_refs&amp;userid=' . $userid . '%}">{%pipe,translateComma=' . $content['refs'] . '%}</a>]';
115                 } // END - if
116                 if ($content['cats']  > 0) {
117                         $content['cats']  = $base . '&amp;what=list_cats&amp;userid=' . $userid . '%}">{%pipe,translateComma=' . $content['cats'] . '%}</a>]';
118                 } // END - if
119
120                 // Calculate timestamp for birthday
121                 // @TODO Refacture birth_foo to avoid uni* timetstamps as older members cannot be greeded correctly
122                 $stamp = mktime(0, 0, 0, $content['birth_month'], $content['birth_day'], $content['birth_year']);
123
124                 // Is this above zero?
125                 if ($stamp > 0) {
126                         // Then use it
127                         $content['birthday'] = generateDateTime($stamp, '2');
128                 } else {
129                         // Zero or below so set zero!
130                         $content['birthday'] = generateDateTime(0, '2');
131                 }
132
133                 // Prepare data for template
134                 $content['email_link']        = generateEmailLink($content['email'], 'user_data');
135                 $content['last_online']       = generateDateTime($content['last_online'], '2');
136                 $content['joined']            = generateDateTime($content['joined'], '2');
137                 $content['last_update']       = generateDateTime($content['last_update'], '2');
138                 $content['last_profile_sent'] = generateDateTime($content['last_profile_sent'], '2');
139                 $content['locked_points']     = countSumTotalData($content['userid'], 'user_points', 'locked_points') + countSumTotalData($content['userid'], 'user_points', 'locked_order_points');
140
141                 // Prepare array for filter
142                 $data = array(
143                         'userid' => $content['userid'],
144                         'points' => 0
145                 );
146
147                 // Run the filter chain
148                 $data = runFilterChain('get_own_points', $data);
149
150                 // Add more data
151                 $content['own_points']     = $data['points'];
152                 $content['lock_timestamp'] = generateDateTime($content['lock_timestamp'], '2');
153
154                 // Link to email_details if some are sent
155                 if ($content['emails_sent'] > 0) {
156                         $content['emails_sent'] = $base . '&amp;what=email_details&amp;userid=' . $content['userid'] . '%}">{%pipe,translateComma=' . $content['emails_sent'] . '%}</a>]';
157                 } // END - if
158
159                 // Clickrate
160                 $content['click_rate'] = '0';
161                 if ($content['emails_received'] > 0) {
162                         $content['click_rate'] = ($content['mails_confirmed'] / $content['emails_received'] * 100);
163                 } // END - if
164
165                 // Is the extension 'country' installed?
166                 if (isExtensionActive('country')) {
167                         // Then overwrite country information
168                         $content['country'] = generateCountryInfo($content['country_code']);
169                 } elseif ($content['country'] == '0') {
170                         // Zero id???
171                         $content['country'] = '???';
172                 }
173
174                 // Add userid
175                 $content['userid'] = $userid;
176
177                 // Load user-details template
178                 loadTemplate('admin_user_details', false, $content);
179         } else {
180                 // Account does not exist!
181                 displayMessage('{%message,ADMIN_MEMBER_404=' . $userid . '%}');
182         }
183
184         // Free the result
185         SQL_FREERESULT($result_user);
186 } else {
187         $whereStatement = '';
188         if (isGetRequestElementSet('letter')) {
189                 // List only persons w
190                 $whereStatement = " WHERE `family` LIKE '".getRequestElement('letter') . "%%'";
191         } // END - if
192
193         // Parse the status or mode parameter
194         if (isGetRequestElementSet('status')) {
195                 // Is a WHERE statement already there?
196                 if (!empty($whereStatement)) {
197                         // Then append the status column
198                         $whereStatement .= sprintf(" AND `status`='%s'", SQL_ESCAPE(strtoupper(getRequestElement('status'))));
199                 } else {
200                         // Start a new one
201                         $whereStatement = sprintf(" WHERE `status`='%s'", SQL_ESCAPE(strtoupper(getRequestElement('status'))));
202                 }
203
204                 // Exclude other users
205                 $whereStatement = runFilterChain('user_exclusion_sql', $whereStatement);
206         } elseif (isGetRequestElementSet('do')) {
207                 // Choose what we need to list
208                 switch (getRequestElement('do')) {
209                         case 'norefs': // Users w/o refs
210                                 if (!empty($whereStatement)) {
211                                         // Add AND statement
212                                         $whereStatement .= ' AND (`refid`=0 OR `refid` IS NULL)';
213                                 } else {
214                                         // Add WHERE statement
215                                         $whereStatement = ' WHERE (`refid`=0 OR `refid` IS NULL)';
216                                 }
217                                 break;
218
219                         case 'random_refid': // Users available for random referral id
220                                 if (!empty($whereStatement)) {
221                                         // Add AND statement
222                                         $whereStatement .= ' AND `rand_confirmed` >= {?user_min_confirmed?}';
223                                 } else {
224                                         // Add WHERE statement
225                                         $whereStatement = ' WHERE `rand_confirmed` >= {?user_min_confirmed?}';
226                                 }
227                                 break;
228
229                         case 'testers': // Test accounts
230                                 // Add statement through filter
231                                 $whereStatement = runFilterChain('user_inclusion_sql', $whereStatement);
232                                 break;
233
234                         default: // Invalid list mode
235                                 reportBug(__FILE__, __LINE__, sprintf('Invalid do %s detected.', SQL_ESCAPE(secureString(getRequestElement('do')))));
236                                 break;
237                 } // END - switch
238
239                 // If it is not 'testers', add exclusion SQL
240                 if (getRequestElement('do') != 'testers') {
241                         // Exclude tester users
242                         $whereStatement = runFilterChain('user_exclusion_sql', $whereStatement);
243                 } // END - if
244         } // END - if
245
246         // Generate master query string
247         $sql = sprintf('SELECT
248         `userid`,
249         `email`,
250         `emails_sent`,
251         `mails_confirmed`,
252         `emails_received`
253         ' . $moreColumns . '
254 FROM
255         `{?_MYSQL_PREFIX?}_user_data`
256 ' . $whereStatement . '
257 ORDER BY
258         `%s` ASC',
259                 getRequestElement('sortby')
260         );
261
262         // Prepare SQL and run it
263         $result_master = SQL_QUERY($sql, __FILE__, __LINE__);
264
265         // Is the user limit zero?
266         if (getUserLimit() == '0') {
267                 // Then set it to default ...
268                 setConfigEntry('user_limit', 100);
269
270                 // ... but output warning
271                 displayMessage('{--EXTENSION_WARNING_USER_LIMIT--}');
272         } // END - if
273
274         // Calculate page count (0.5 fixes a bug with page count)
275         $numPages = round(SQL_NUMROWS($result_master) / getUserLimit() + 0.5);
276
277         if (!isGetRequestElementSet('page'))   setGetRequestElement('page'  , 1);
278         if (!isGetRequestElementSet('offset')) setGetRequestElement('offset', getUserLimit());
279
280         // Add limitation to SQL string and run him again
281         $sql .= ' LIMIT ' . (getRequestElement('offset') * getRequestElement('page') - getRequestElement('offset')) . ', ' . getRequestElement('offset');
282         $result = SQL_QUERY($sql, __FILE__, __LINE__);
283
284         // Are there some entries?
285         if (!SQL_HASZERONUMS($result_master)) {
286                 // Sorting links and number of pages
287                 $templateContent['sort_links'] = addSortLinks(getRequestElement('letter'), getRequestElement('sortby'));
288                 $templateContent['num_pages'] = $numPages;
289
290                 // Column with nickname when nickname extension is present
291                 // @TODO Rewrite this into a filter
292                 $templateContent['nickname_th'] = '';
293                 if (isExtensionActive('nickname')) {
294                         // Nickname extension found
295                         $templateContent['nickname_th'] = '<td class="header_column bottom" align="center">{--NICKNAME--}</td>';
296                 } // END - if
297
298                 // Load all users
299                 $OUT = '';
300                 while ($content = SQL_FETCHARRAY($result)) {
301                         // Merge more data in
302                         $content = merge_array($content, $templateContent);
303
304                         // Get number of unconfirmed mails
305                         $content['links'] = getTotalUnconfirmedMails($content['userid']);
306                         if ($content['links'] > 0) {
307                                 $content['links'] = $base . '&amp;what=list_links&amp;userid=' . $content['userid'] . '%}">{%pipe,translateComma=' . $content['links'] . '%}</a>]';
308                         } // END - if
309
310                         // Set link to sent mails if present
311                         if ($content['emails_sent'] > 0) {
312                                 $content['emails_sent'] = $base . '&amp;what=email_details&amp;userid=' . $content['userid'] . '%}">{%pipe,translateComma=' . $content['emails_sent'] . '%}</a>]';
313                         } // END - if
314
315                         // Click rate
316                         $content['click_rate'] = '0';
317                         if ($content['emails_received'] > 0) {
318                                 $content['click_rate'] = $content['mails_confirmed'] / $content['emails_received'] * 100;
319                         } // END - if
320
321                         // Transfer data to array
322                         $content['email']          = '[<a href="' . generateEmailLink($content['email'], 'user_data') . '">' . $content['email'] . '</a>]';
323                         $content['lock_timestamp'] = generateDateTime($content['lock_timestamp'], '2');
324                         $content['refs']           = countSumTotalData($content['userid'], 'user_refs', 'id', 'userid', true);
325
326                         // Prepare array for filter
327                         $data = array(
328                                 'userid' => $content['userid'],
329                                 'points' => 0
330                         );
331
332                         // Run the filter chain
333                         $data = runFilterChain('get_own_points', $data);
334
335                         // Copy points
336                         $content['own_points'] = $data['points'];
337
338                         // Prepare array for filter again
339                         $data = array(
340                                 'userid' => $content['userid'],
341                                 'points' => 0
342                         );
343
344                         // Run the filter chain
345                         $data = runFilterChain('get_locked_points', $data);
346
347                         // Copy points
348                         $content['locked_points']  = $data['points'];
349
350                         // If we have at least one referral, make it clickable to referral list
351                         if ($content['refs'] > 0) {
352                                 $content['refs'] = $base . '&amp;what=list_refs&amp;userid=' . $content['userid'] . '%}">{%pipe,translateComma=' . $content['refs'] . '%}</a>]';
353                         } // END - if
354
355                         // Is the extension 'country' installed?
356                         // @TODO Rewrite this into a filter
357                         if (isExtensionActive('country')) {
358                                 // Then overwrite country information
359                                 $content['country'] = generateCountryInfo($content['country_code']);
360                         } elseif ($content['country'] == '') {
361                                 // Zero id???
362                                 $content['country'] = '???';
363                         }
364
365                         // Load row template and switch colors
366                         $OUT .= loadTemplate('admin_list_user_row', true, $content);
367                 } // END - while
368
369                 // Free memory
370                 SQL_FREERESULT($result);
371
372                 // Free some memory
373                 SQL_FREERESULT($result_master);
374
375                 // Remember all rows
376                 $content['rows'] = $OUT;
377
378                 // Merge more data again
379                 $content = merge_array($content, $templateContent);
380
381                 // Load main template
382                 loadTemplate('admin_list_user', false, $content);
383         } else {
384                 // No one as registered so far! :-(
385                 displayMessage('{--ADMIN_NO_NONE_REGISTERED--}');
386         }
387
388         // Free memory
389         SQL_FREERESULT($result_master);
390 }
391
392 // [EOF]
393 ?>