856454cffb38237950f0b7790baf212cb22210ad
[mailer.git] / inc / modules / admin / overview-inc.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 07/13/2004 *
4  * ===============                              Last change: 08/02/2004 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : overview-inc.php                                 *
8  * -------------------------------------------------------------------- *
9  * Short description : Output standard task management                  *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Standart-Aufgaben-Management ausgeben            *
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 - 2008 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')) || (!IS_ADMIN())) {
41         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), '/inc') + 4) . '/security.php';
42         require($INC);
43 }
44
45 function OUTPUT_STANDARD_OVERVIEW (&$result_tasks) {
46         // First check for solved and not assigned tasks and assign them to current admin
47         SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_task_system` SET `assigned_admin`=%s WHERE assigned_admin < 1 AND status != 'NEW'",
48         array(getCurrentAdminId()), __FILE__, __LINE__);
49
50         // We currently don't want to install an extension so let's find out if we need...
51         $EXT_LOAD_MODE = 'register';
52         $jobsDone = true;
53
54         // Open the extension directory
55         $extensionList = getArrayFromDirectory("inc/extensions/", "ext-", false, false);
56         foreach ($extensionList as $file) {
57                 // Is this file an extension?
58                 if ((substr($file, 0, 4) == "ext-") && (substr($file, -4) == '.php')) {
59                         //* DEBUG: */ echo $file."<br />\n";
60                         // Possible newly installed extension found so we extract extension's name
61                         $ext_name = strtolower(substr($file, 4, -4)); // Keep always extension names on lower case!!!
62
63                         // Init variables
64                         $result = false;
65
66                         // Check if extension is installed or not
67                         $ext_ver = GET_EXT_VERSION($ext_name);
68
69                         // Is the extension not yet installed?
70                         if (empty($ext_ver)) {
71                                 // Generate subject line
72                                 $ext_subj = sprintf("[%s:]", $ext_name);
73
74                                 // We maybe want to install an extension so let's test-drive it...
75                                 if (LOAD_EXTENSION($ext_name, $EXT_LOAD_MODE)) {
76                                         // Create a task for newly installed extension
77                                         CREATE_NEW_EXTENSION_TASK(getCurrentAdminId(), $ext_subj, $ext_name);
78                                 } // END - if
79                         } else {
80                                 // Test-drive extension in update mode
81                                 require(sprintf("%sinc/extensions/ext-%s.php", constant('PATH'), $ext_name));
82
83                                 // Update extension if extension is installed and outdated
84                                 //* DEBUG: */ print "ext={$ext_name},ver={EXT_GET_VERSION()}/".GET_EXT_VERSION($ext_name)."<br />\n";
85                                 if (EXT_GET_VERSION() > $ext_ver) {
86                                         // Update the extension
87                                         EXTENSION_UPDATE($ext_name, $ext_ver);
88                                 } // END - if
89
90                                 if (isset($GLOBALS['cache_array']['active_extensions'][$ext_name])) {
91                                         // Maybe we want to keept the current extension active?
92                                         if (($GLOBALS['cache_array']['active_extensions'][$ext_name] == 'Y') && (!EXT_IS_ACTIVE($ext_name))) {
93                                                 // Reactivate this extension!
94                                                 ACTIVATE_EXTENSION($ext_name);
95                                         } // END - if
96                                 } // END - if
97                         }
98                 } // END - if
99         } // END - foreach
100
101         // At last - but not least - check for own and new unassigned tasks...
102         $result_tasks = SQL_QUERY_ESC("SELECT `id`, `assigned_admin`, `userid`, `task_type`, `subject`, `text`, `task_created`
103 FROM `{!_MYSQL_PREFIX!}_task_system`
104 WHERE `assigned_admin`=%s OR (`assigned_admin`=0 AND `status`='NEW')
105 ORDER BY `userid` DESC, `task_type` DESC, `subject` ASC, `task_created` DESC",
106         array(getCurrentAdminId()), __FILE__, __LINE__);
107
108         if (SQL_NUMROWS($result_tasks) > 0) {
109                 // New jobs found!
110                 $jobsDone = false;
111         } // END - if
112
113         // Free the result
114         SQL_FREERESULT($result);
115
116         // Return status
117         return $jobsDone;
118 }
119
120 // Outputs selected tasks
121 function OUTPUT_SELECTED_TASKS ($POST, $result_tasks) {
122         if ((isset($POST['assign'])) && (count($POST['task']) > 0)) {
123                 // Assign / do tasks
124                 $OUT = ''; $SW = 2;
125                 foreach ($POST['task'] as $id => $sel) {
126                         $result_task = SQL_QUERY_ESC("SELECT `id`, `userid`, `task_type`, `subject`, `text`, `task_created`, `status`, `assigned_admin`
127 FROM `{!_MYSQL_PREFIX!}_task_system`
128 WHERE `id`=%s AND (`assigned_admin`=%s OR (`assigned_admin`=0 AND `status`='NEW'))
129 LIMIT 1",
130                         array(bigintval($id), getCurrentAdminId()), __FILE__, __LINE__);
131                         if (SQL_NUMROWS($result_task) == 1) {
132                                 // Task is valid...
133                                 list($tid, $uid, $type, $subj, $text, $created, $status, $aid) = SQL_FETCHROW($result_task);
134                                 SQL_FREERESULT($result_task);
135
136                                 if ($aid == '0') {
137                                         // Assgin current admin to unassgigned task
138                                         SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_task_system` SET `assigned_admin`=%s WHERE `id`=%s LIMIT 1",
139                                         array(getCurrentAdminId(), bigintval($tid)), __FILE__, __LINE__);
140                                 } // END - if
141
142                                 $add = '';
143                                 // @TODO Rewrite this to a filter
144                                 if ($type == 'SUPPORT_MEMBER') {
145                                         $mode = substr($text, 0, strpos($text, ':'));
146                                         $text = substr($text, strpos($text, ':') + 1);
147                                         $add = "<li>{--ADMIN_TASK_SUPPORT_MODE--}: <strong>".$mode."</strong></li>";
148                                 } // END - if
149
150                                 if ($uid > 0) {
151                                         $result_user = SQL_QUERY_ESC("SELECT gender, surname, family, email FROM `{!_MYSQL_PREFIX!}_user_data` WHERE userid=%s LIMIT 1",
152                                         array(bigintval($uid)), __FILE__, __LINE__);
153                                         if (SQL_NUMROWS($result_user) == 1) {
154                                                 list($gender, $sname, $fname, $email) = SQL_FETCHROW($result_user);
155                                                 SQL_FREERESULT($result_user);
156                                                 $add = "<li>{--ADMIN_MEMBER_UID--}: <strong>".generateUserProfileLink($uid)." (<a href=\"".generateMemberEmailLink($email, "user_data")."\">".translateGender($gender)." ".$sname." ".$fname."</a>)</strong></li>";
157                                         } // END - if
158                                 } // END - if
159
160                                 // Decode entities of the text
161                                 $text = decodeEntities($text);
162
163                                 // Compile and insert text from task into table template
164                                 $text = LOAD_TEMPLATE('admin_extensions_text', true, $text);
165
166                                 // Initialize variables (no title for SQL commands by default)
167                                 $ext_name = ''; $ext_ver = '';
168                                 $title = getMessage('TASK_NO_TITLE');
169
170                                 // Shall I list SQL commands assigned to an extension installation or update task?
171                                 if (((GET_EXT_VERSION('sql_patches') != '') && (getConfig('verbose_sql') == 'Y')) || (!EXT_IS_ACTIVE('sql_patches'))) {
172                                         // Extract extension name from subject
173                                         $ext_name = substr($subj, 1, strpos($subj, ':') - 1);
174
175                                         // Update task or extension task?
176                                         if ($type == 'EXTENSION') {
177                                                 // Load SQL commands for registering
178                                                 REGISTER_EXTENSION($ext_name, $id, true);
179
180                                                 // Add notes to text
181                                                 $text .= EXT_GET_NOTES();
182
183                                                 // Set title
184                                                 $title = getMessage('ADMIN_SQLS_EXECUTED_ON_REGISTER');
185                                         } elseif ($type == 'EXTENSION_UPDATE') {
186                                                 // Prepare extension name and version
187                                                 $ext_name = substr($ext_name, 7);
188                                                 $ext_name = substr($ext_name, 0, strpos($ext_name, '-'));
189                                                 $test = '[UPDATE-' . $ext_name . '-';
190                                                 $ext_ver = substr($subj, strlen($test));
191                                                 $ext_ver = substr($ext_ver, 0, strpos($ext_ver, ':'));
192
193                                                 // Load SQLs from file
194                                                 EXTENSION_UPDATE($ext_name, $ext_ver, true);
195
196                                                 // Add notes to text
197                                                 $text .= EXT_GET_NOTES();
198
199                                                 // Set title
200                                                 $title = getMessage('ADMIN_SQLS_EXECUTED_ON_UPDATE');
201                                         } else {
202                                                 // Remove extension's name
203                                                 $ext_name = '';
204                                         }
205
206                                         // Auto-init SQL handler
207                                         if (!IS_SQLS_VALID()) INIT_SQLS();
208
209                                         // Add SQLs to a table
210                                         if ((!empty($ext_name)) && (GET_EXT_VERSION('sql_patches')) && (getConfig('verbose_sql') == 'Y')) {
211                                                 // Add verbose SQL table
212                                                 $text .= EXTENSION_VERBOSE_TABLE($title, " class=\"admin_table top2 left2 right2\"", true, '100%')."<br />\n";
213                                         } // END - if
214                                 } elseif ((!empty($ext_name)) && (!empty($ext_ver))) {
215                                         // Run SQL commands in dry mode but only return the notes
216                                         EXTENSION_UPDATE($ext_name, $ext_ver, true);
217                                         $text .= EXT_GET_NOTES();
218                                 } else {
219                                         // This should not normally happen!
220                                         debug_report_bug('ext_name(' . $ext_name . ') or ext_ver(' . $ext_ver . ') is empty! sql_patches=' . GET_EXT_VERSION('sql_patches') . '/verbose_sql=' . getConfig('verbose_sql'));
221                                 }
222
223                                 // Prepare array for the template
224                                 $content = array(
225                                         'sw'        => $SW,
226                                         'subj'      => $subj,
227                                         'add'       => $add,
228                                         'text'      => $text,
229                                         'created'   => generateDateTime($created, '1'),
230                                         'extension' => $ext_name
231                                 );
232
233                                 // Load template
234                                 $OUT .= LOAD_TEMPLATE('admin_overview_row', true, $content);
235
236                                 // Which task do we actually have here?
237                                 // @TODO Rewrite this to something with include files
238                                 switch ($type)
239                                 {
240                                         case 'EXTENSION': // Install new extensions
241                                                 $ext_name = substr($subj, 1, strpos($subj, ':') - 1);
242                                                 $result_lines = SQL_QUERY_ESC("SELECT `id` FROM `{!_MYSQL_PREFIX!}_extensions` WHERE `ext_name`='%s' LIMIT 1",
243                                                 array($ext_name), __FILE__, __LINE__);
244                                                 $lines = SQL_NUMROWS($result_lines);
245                                                 SQL_FREERESULT($result_lines);
246                                                 if ($lines == '0') {
247                                                         // New extension found
248                                                         $OUT .= LOAD_TEMPLATE('admin_ext_reg_form', true, array(
249                                                         'id'       => bigintval($id),
250                                                         'ext_name' => $ext_name
251                                                         ));
252                                                 } else {
253                                                         // Task is closed so nothing is todo
254                                                         $OUT .= "<div class=\"admin_failed\">{--ADMIN_EXT_ALREADY_REGISTERED--}</div>\n";
255
256                                                         // Close task but not already closes or deleted or update tasks
257                                                         if (($status != 'CLOSED') && ($status != 'DELETED') && ($type != 'EXTENSION_UPDATE')) {
258                                                                 // Solve the task
259                                                                 runFilterChain('solve_task', $tid);
260                                                         } // END - if
261                                                 }
262                                                 break;
263
264                                         case 'EXTENSION_UPDATE': // Extension update
265                                                 // Extension updates are installed automatically
266                                                 $OUT .= "<div class=\"admin_failed medium\">{--ADMIN_EXTENSION_UPDATED--}</div>\n";
267
268                                                 // Close task
269                                                 if (($status != 'CLOSED') && ($status != 'DELETED')) {
270                                                         // Solve the task
271                                                         runFilterChain('solve_task', $tid);
272                                                 } // END - if
273                                                 break;
274
275                                         case 'SUPPORT_MEMBER': // Assign on member's support request
276                                                 // @TODO This may also be rewritten to include files
277                                                 switch ($mode)
278                                                 {
279                                                         default: // @TODO Unknown support mode
280                                                         DEBUG_LOG(__FILE__, __LINE__, sprintf("Unknown support mode %s detected. This part is under construction!", $mode));
281                                                         $OUT .= "<div class=\"admin_failed medium\">".sprintf(getMessage('ADMIN_UNKNOWN_SUPPORT_MODE'), $mode)."</div>\n";
282                                                         break;
283                                                 }
284                                                 break;
285
286                                                         case 'PAYOUT_REQUEST': // Payout requests
287                                                                 if (EXT_IS_ACTIVE('payout')) {
288                                                                         // Extension is installed so let him send a notification to the user
289                                                                         $result_pay = SQL_QUERY_ESC("SELECT `id` FROM `{!_MYSQL_PREFIX!}_user_payouts` WHERE userid=%s AND payout_timestamp=%s LIMIT 1",
290                                                                         array(bigintval($uid), bigintval($created)), __FILE__, __LINE__);
291                                                                         list($pid) = SQL_FETCHROW($result_pay);
292                                                                         SQL_FREERESULT($result_pay);
293
294                                                                         if ((!empty($pid)) && ($pid > 0)) {
295                                                                                 // Payout ID can be obtained
296                                                                                 $content = array(
297                                                                 'pid' => $pid,
298                                                                 'tid' => $tid,
299                                                                                 );
300                                                                                 $OUT .= LOAD_TEMPLATE('admin_payout_overview_form', true, $content);
301                                                                         } else {
302                                                                                 // Problem obtaining payout ID
303                                                                                 $OUT .= "<div class=\"admin_failed medium\">{--PAYOUT_OBTAIN_ID_FAILED--}</div>\n";
304                                                                         }
305                                                                 } else {
306                                                                         // Extension is not installed
307                                                                         $OUT .= "<div class=\"admin_failed medium\">{--ADMIN_PAYOUT_NOT_INSTALLED--}</div>\n";
308                                                                 }
309                                                                 break;
310
311                                                         case 'WERNIS_REQUEST': // Wernis requests
312                                                                 if (EXT_IS_ACTIVE('wernis')) {
313                                                                         // Extension is installed so let him send a notification to the user
314                                                                         $result_pay = SQL_QUERY_ESC("SELECT `id` FROM `{!_MYSQL_PREFIX!}_user_wernis` WHERE userid=%s AND wernis_timestamp=%s LIMIT 1",
315                                                                         array(bigintval($uid), bigintval($created)), __FILE__, __LINE__);
316                                                                         list($pid) = SQL_FETCHROW($result_pay);
317                                                                         SQL_FREERESULT($result_pay);
318
319                                                                         if ((!empty($pid)) && ($pid > 0)) {
320                                                                                 // Payout ID can be obtained
321                                                                                 $content = array(
322                                                                 'pid' => $pid,
323                                                                 'tid' => $tid,
324                                                                                 );
325                                                                                 $OUT .= LOAD_TEMPLATE('admin_wernis_overview_form', true, $content);
326                                                                         } else {
327                                                                                 // Problem obtaining wernis ID
328                                                                                 $OUT .= "<div class=\"admin_failed medium\">{--WERNIS_OBTAIN_ID_FAILED--}</div>\n";
329                                                                         }
330                                                                 } else {
331                                                                         // Extension is not installed
332                                                                         $OUT .= "<div class=\"admin_failed medium\">{--ADMIN_WERNIS_NOT_INSTALLED--}</div>\n";
333                                                                 }
334                                                                 break;
335
336                                                         case 'HOLIDAY_REQUEST': // Holiday requests
337                                                                 $OUT .= LOAD_TEMPLATE('admin_task_holiday', true, $uid);
338                                                                 break;
339
340                                                         case 'NL_UNSUBSCRIBE': // Newsletter unsubscriptions
341                                                                 $result = SQL_QUERY_ESC("SELECT nl_timespan FROM `{!_MYSQL_PREFIX!}_user_data` WHERE userid=%s LIMIT 1",
342                                                                 array(bigintval($uid)), __FILE__, __LINE__);
343                                                                 list($span) = SQL_FETCHROW($result);
344                                                                 SQL_FREERESULT($result);
345
346                                                                 if ($span > 0) {
347                                                                         // Undone unscubscribe request
348                                                                         $content = array(
349                                                         'uid' => $uid,
350                                                         'id'  => $tid
351                                                                         );
352                                                                         $OUT .= LOAD_TEMPLATE('admin_newsletter_tsk', true, $content);
353                                                                 } else {
354                                                                         // Already unsubscribed
355                                                                         $OUT .= "<div class=\"admin_failed medium\">".ADMIN_NL_UNSUBSCRIBE_ALREADY."</div>\n";
356                                                                 }
357                                                                 break;
358
359                                                         default: // Unknown task type
360                                                                 DEBUG_LOG(__FILE__, __LINE__, sprintf("Unknown task type %s detected.", $type));
361                                                                 $OUT .= "<div class=\"admin_failed medium\">".sprintf(getMessage('ADMIN_UNKNOWN_TASK_TYPE'), $type, $id)."</div>\n";
362                                                                 break;
363                                 }
364                                 $OUT .= "  </td>
365   <td width=\"1%\" class=\"switch_sw".$SW." bottom2 right2\">&nbsp;</td>
366 </tr>\n";
367                         } // END - if
368                         $SW = 3 - $SW;
369                 } // END - foreach
370                 define('__TASK_ROWS', $OUT);
371
372                 // Load final template
373                 LOAD_TEMPLATE('admin_overview_list');
374         } else {
375                 if ((isset($POST['task'])) && ((count($POST['task']) > 0) || ($POST['task'][0] == '1'))) {
376                         // Only unassign / delete tasks when there are selected tasks posted
377                         if (!empty($POST['unassign'])) {
378                                 // Unassign from tasks
379                                 foreach ($POST['task'] as $id => $sel) {
380                                         SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_task_system` SET `assigned_admin`=0 WHERE `id`=%s AND `assigned_admin`=%s LIMIT 1",
381                                         array(bigintval($id), getCurrentAdminId()), __FILE__, __LINE__);
382                                 }
383                         } elseif (isset($POST['del'])) {
384                                 // Delete tasks
385                                 foreach ($POST['task'] as $id => $sel) {
386                                         SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_task_system` WHERE `id`=%s AND assigned_admin IN (%s,0) LIMIT 1",
387                                         array(bigintval($id), getCurrentAdminId()), __FILE__, __LINE__);
388                                 }
389                         }
390
391                         // Update query
392                         $result_tasks = SQL_QUERY_ESC("SELECT id, assigned_admin, userid, task_type, subject, text, task_created FROM `{!_MYSQL_PREFIX!}_task_system` WHERE `assigned_admin`=%s OR (`assigned_admin`=0 AND `status`='NEW') ORDER BY task_created DESC",
393                         array(getCurrentAdminId()), __FILE__, __LINE__);
394                 } // END - if
395
396                 // There are uncompleted jobs!
397                 $OUT = ''; $SW = 2;
398                 while ($content = SQL_FETCHARRAY($result_tasks)) {
399                         // Init infos
400                         $content['infos'] = '---';
401
402                         // Generate assign link
403                         $content['assigned_admin'] = generateAdminLink($content['assigned_admin']);
404
405                         // Generate infos
406                         switch ($content['task_type'])
407                         {
408                                 case 'EXTENSION':
409                                 case 'EXTENSION_UPDATE':
410                                         $content['infos'] = substr($content['subject'], 1, strpos($content['subject'], ':') - 1);
411                                         break;
412                         }
413
414                         // Get task type
415                         $content['task_type_msg'] = getMessage('ADMIN_TASK_IS_'.strtoupper($content['task_type']).'');
416
417                         if ($content['userid'] > 0) {
418                                 // Member found otherwise it's a system task
419                                 $content['userid'] = generateUserProfileLink($content['userid']);
420                         } else {
421                                 $content['userid'] = "<em>{--ADMIN_IS_SYSTEM_TASK--}</em>";
422                         }
423
424                         // Prepare content
425                         // @TODO Rewritings: admin->assigned_admin,uid->userid,type->task_type_msg in template
426                         $content = merge_array($content, array(
427                                 'sw'      => $SW,
428                                 'admin'   => $content['assigned_admin'],
429                                 'uid'     => $content['userid'],
430                                 'type'    => $content['task_type_msg'],
431                                 'created' => generateDateTime($content['task_created'], '2')
432                         ));
433
434                         // Do we have extension task?
435                         if (($content['task_type'] == 'EXTENSION') && (GET_EXT_VERSION($content['infos']) == '')) {
436                                 // Load extension row template
437                                 $OUT .= LOAD_TEMPLATE("admin_overview_list_ext_rows", true, $content);
438                         } else {
439                                 // Load default row template
440                                 $OUT .= LOAD_TEMPLATE("admin_overview_list_rows", true, $content);
441                         }
442
443                         // Switch color
444                         $SW = 3 - $SW;
445                 }
446
447                 // Free memory
448                 SQL_FREERESULT($result_tasks);
449
450                 // Load footer template
451                 LOAD_TEMPLATE("admin_overview_table", false, $OUT);
452         }
453 }
454 //
455 ?>