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