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