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