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