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