e42defec891ae0beb0c347b09742845d312724b8
[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 - 2013 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         sqlQueryEscaped("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                                         // Shall the current extension kept activated?
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 = sqlQueryEscaped("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 (!ifSqlHasZeroNums($result_tasks)) {
125                 // New jobs found
126                 $jobsDone = FALSE;
127         } // END - if
128
129         // Free the result
130         sqlFreeResult($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 = sqlQueryEscaped("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 (sqlNumRows($result_task) == 1) {
163                                 // Task is valid so load it's data
164                                 $taskData = sqlFetchArray($result_task);
165
166                                 if ($taskData['assigned_admin'] == '0') {
167                                         // Assgin current admin to unassgigned task
168                                         sqlQueryEscaped("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 (isValidId($taskData['userid'])) {
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_ASSIGNED_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_ver  = '';
207                                 $ext_name = '';
208                                 $title    = '{--ADMIN_TASK_NO_SQL_TITLE--}';
209
210                                 // Extract extension name from subject (for testing)
211                                 $ext_name = substr($taskData['subject'], 1, strpos($taskData['subject'], ':') - 1);
212
213                                 // Update task or extension task?
214                                 if (($taskData['task_type'] == 'EXTENSION') && (!isExtensionInstalled($ext_name))) {
215                                         // Load SQL commands for registering in dry-run
216                                         registerExtension($ext_name, $taskId, TRUE);
217
218                                         // Get current extension's version
219                                         $ext_ver = getCurrentExtensionVersion();
220
221                                         // Is this non-productive?
222                                         if (isExtensionProductive() === FALSE) {
223                                                 // Issue warning
224                                                 $taskData['text'] = displayMessage('{%message,ADMIN_EXTENSION_IS_NON_PRODUCTIVE=' . $ext_name . '%}', TRUE) . $taskData['text'];
225                                         } // END - if
226
227                                         // Set current extension name
228                                         setCurrentExtensionName($ext_name);
229
230                                         // Add notes to text
231                                         $taskData['text'] .= getExtensionNotes();
232
233                                         // Set title
234                                         $title = '{--ADMIN_SQLS_EXECUTED_ON_REGISTER--}';
235                                 } elseif ($taskData['task_type'] == 'EXTENSION_UPDATE') {
236                                         // Prepare extension name and version
237                                         $ext_name = substr($ext_name, 7);
238                                         $ext_name = substr($ext_name, 0, strpos($ext_name, '-'));
239                                         $test = '[UPDATE-' . $ext_name . '-';
240                                         $ext_ver = substr($taskData['subject'], strlen($test));
241                                         $ext_ver = substr($ext_ver, 0, strpos($ext_ver, ':'));
242
243                                         // Load SQLs from file
244                                         updateExtension($ext_name, $ext_ver, TRUE);
245
246                                         // Set current extension name
247                                         setCurrentExtensionName($ext_name);
248
249                                         // Add notes to text
250                                         $taskData['text'] .= getExtensionNotes();
251
252                                         // Set title
253                                         $title = '{--ADMIN_SQLS_EXECUTED_ON_UPDATE--}';
254                                 } else {
255                                         // Remove extension's name
256                                         $ext_name = '';
257                                 }
258
259                                 // Add SQLs to a table
260                                 if ((!empty($ext_name)) && (isVerboseSqlEnabled())) {
261                                         // Add verbose SQL table
262                                         $taskData['text'] .= addExtensionVerboseSqlTable($title);
263                                 } // END - if
264
265                                 // Version+extension name must be set
266                                 if (((!empty($ext_name)) && (empty($ext_ver))) || ((empty($ext_name)) && (!empty($ext_ver)))) {
267                                         // This should not normally happen!
268                                         reportBug(__FILE__, __LINE__, 'ext_name(' . $ext_name . ') or ext_ver(' . $ext_ver . ') is empty! isVerboseSqlEnabled=' . intval(isVerboseSqlEnabled()));
269                                 } // END - if
270
271                                 // Prepare array for the template
272                                 $content = array(
273                                         'subject'      => $taskData['subject'],
274                                         'add'          => $add,
275                                         'text'         => $taskData['text'],
276                                         'task_created' => generateDateTime($taskData['task_created'], '1'),
277                                         'ext_name'     => $ext_name,
278                                         'ext_version'  => $ext_ver
279                                 );
280
281                                 // Load template
282                                 $OUT .= loadTemplate('admin_overview_row', TRUE, $content);
283
284                                 // Which task do we actually have here?
285                                 // @TODO Rewrite this to something with include files and/or filter
286                                 switch ($taskData['task_type']) {
287                                         case 'EXTENSION': // Install new extensions
288                                                 $ext_name = substr($taskData['subject'], 1, strpos($taskData['subject'], ':') - 1);
289                                                 if (!isExtensionInstalled($ext_name)) {
290                                                         // New (not yet installed) extension found
291                                                         $OUT .= loadTemplate('admin_extension_reg_form', TRUE, array(
292                                                                 'id'       => bigintval($taskId),
293                                                                 'ext_name' => $ext_name
294                                                         ));
295                                                 } else {
296                                                         // Extension is already installed
297                                                         $OUT .= '<div class="notice">{--ADMIN_EXTENSION_ALREADY_REGISTERED--}</div>';
298
299                                                         // Close task but not already closed, solved, deleted or update tasks
300                                                         if ((!in_array($taskData['status'], array('CLOSED', 'DELETED', 'SOLVED'))) && ($taskData['task_type'] != 'EXTENSION_UPDATE')) {
301                                                                 // Solve the task
302                                                                 runFilterChain('solve_task', $taskData['id']);
303                                                         } // END - if
304                                                 }
305                                                 break;
306
307                                         case 'EXTENSION_UPDATE': // Extension update
308                                                 // Extension updates are installed automatically
309                                                 $OUT .= '<div class="notice medium">{--ADMIN_EXTENSION_UPDATED--}</div>';
310
311                                                 // Close task if not closed or deleted
312                                                 if (!in_array($taskData['status'], array('CLOSED', 'DELETED'))) {
313                                                         // Solve the task
314                                                         runFilterChain('solve_task', $taskData['id']);
315                                                 } // END - if
316                                                 break;
317
318                                         case 'MEMBER_SUPPORT': // Assign on member's support request
319                                                 // @TODO This may also be rewritten to include files
320                                                 switch ($mode) {
321                                                         default: // @TODO Unknown support mode
322                                                         logDebugMessage(__FILE__, __LINE__, sprintf('Unknown support mode %s detected. This part is under construction.', $mode));
323                                                         $OUT .= '<div class="notice medium">{%message,ADMIN_UNKNOWN_SUPPORT_MODE=' . $mode . '%}</div>';
324                                                         break;
325                                                 } // END - switch
326                                                 break;
327
328                                         case 'PAYOUT_REQUEST': // Payout requests
329                                                 if (isExtensionActive('payout')) {
330                                                         // Extension is installed so let him send a notification to the user
331                                                         $result_pay = sqlQueryEscaped("SELECT `id` FROM `{?_MYSQL_PREFIX?}_user_payouts` WHERE `userid`=%s AND `payout_timestamp`=%s LIMIT 1",
332                                                                 array(bigintval($taskData['userid']), bigintval($taskData['task_created'])), __FILE__, __LINE__);
333                                                         list($pid) = sqlFetchRow($result_pay);
334                                                         sqlFreeResult($result_pay);
335
336                                                         if (isValidId($pid)) {
337                                                                 // Payout id can be obtained
338                                                                 $content = array(
339                                                                         'pid' => $pid,
340                                                                         'tid' => $taskData['id'],
341                                                                 );
342
343                                                                 // Load template
344                                                                 $OUT .= loadTemplate('admin_payout_overview_form', TRUE, $content);
345                                                         } else {
346                                                                 // Problem obtaining payout id
347                                                                 $OUT .= '<div class="notice medium">{--ADMIN_PAYOUT_OBTAIN_ID_FAILED--}</div>';
348                                                         }
349                                                 } else {
350                                                         // Extension is not installed
351                                                         $OUT .= '<div class="notice medium">{--ADMIN_PAYOUT_NOT_INSTALLED--}</div>';
352                                                 }
353                                                 break;
354
355                                         case 'WERNIS_REQUEST': // Wernis requests
356                                                 if (isExtensionActive('wernis')) {
357                                                         // Extension is installed so let him send a notification to the user
358                                                         $result_pay = sqlQueryEscaped("SELECT `id` FROM `{?_MYSQL_PREFIX?}_user_wernis` WHERE `userid`=%s AND wernis_timestamp=%s LIMIT 1",
359                                                                 array(bigintval($taskData['userid']), bigintval($taskData['task_created'])), __FILE__, __LINE__);
360                                                         list($pid) = sqlFetchRow($result_pay);
361                                                         sqlFreeResult($result_pay);
362
363                                                         if (isValidId($pid)) {
364                                                                 // Payout id can be obtained
365                                                                 $content = array(
366                                                                         'pid' => $pid,
367                                                                         'tid' => $taskData['id'],
368                                                                 );
369                                                                 $OUT .= loadTemplate('admin_wernis_overview_form', TRUE, $content);
370                                                         } else {
371                                                                 // Problem obtaining wernis id
372                                                                 $OUT .= '<div class="notice medium">{--WERNIS_OBTAIN_ID_FAILED--}</div>';
373                                                         }
374                                                 } else {
375                                                         // Extension is not installed
376                                                         $OUT .= '<div class="notice medium">{--ADMIN_WERNIS_NOT_INSTALLED--}</div>';
377                                                 }
378                                                 break;
379
380                                         case 'HOLIDAY_REQUEST': // Holiday requests
381                                                 $OUT .= loadTemplate('admin_task_holiday', TRUE, $taskData['userid']);
382                                                 break;
383
384                                         case 'MEMBER_ORDER': // Member mail orders
385                                                 $OUT .= loadTemplate('admin_task_order', TRUE, $taskId);
386                                                 break;
387
388                                         default: // Unknown task type
389                                                 logDebugMessage(__FILE__, __LINE__, sprintf('Unknown task type %s detected.', $taskData['task_type']));
390                                                 $OUT .= '<div class="notice medium">' . sprintf(getMessage('ADMIN_UNKNOWN_TASK_TYPE'), $taskData['task_type'], $taskId) . '</div>';
391                                                 break;
392                                 }
393                                 $OUT .= '
394   </td>
395 </tr>';
396                         } // END - if
397
398                         // Free result
399                         sqlFreeResult($result_task);
400                 } // END - foreach
401
402                 // Load final template
403                 loadTemplate('admin_overview_list', FALSE, $OUT);
404         } else {
405                 if ((isset($postData['sel'])) && ((count($postData['sel']) > 0) || ($postData['sel'][0] == 1))) {
406                         // Only unassign / delete tasks when there are selected tasks posted
407                         if (!empty($postData['unassign'])) {
408                                 // Unassign from tasks
409                                 foreach ($postData['sel'] as $taskId => $sel) {
410                                         sqlQueryEscaped("UPDATE `{?_MYSQL_PREFIX?}_task_system` SET `assigned_admin`=NULL WHERE `id`=%s AND `assigned_admin`=%s LIMIT 1",
411                                                 array(bigintval($taskId), getCurrentAdminId()), __FILE__, __LINE__);
412                                 } // END - foreach
413                         } elseif (!empty($postData['delete'])) {
414                                 // Delete tasks
415                                 foreach ($postData['sel'] as $taskId => $sel) {
416                                         sqlQueryEscaped("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_task_system` WHERE `id`=%s AND `assigned_admin` IN (%s,0,NULL) LIMIT 1",
417                                                 array(bigintval($taskId), getCurrentAdminId()), __FILE__, __LINE__);
418                                 } // END - foreach
419                         } else {
420                                 // Unknown action
421                                 reportBug(__FILE__, __LINE__, sprintf('Unknown task action performed. data=<pre>%s</pre>', print_r($postData, TRUE)));
422                         }
423
424                         // Update query
425                         $result_tasks = sqlQueryEscaped("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",
426                                 array(getCurrentAdminId()), __FILE__, __LINE__);
427                 } // END - if
428
429                 // There are uncompleted jobs!
430                 $OUT = '';
431                 while ($content = sqlFetchArray($result_tasks)) {
432                         // Init infos
433                         $content['infos'] = '';
434
435                         // Generate infos
436                         // @TODO Try to move this in includes
437                         switch ($content['task_type']) {
438                                 case 'EXTENSION':
439                                 case 'EXTENSION_UPDATE':
440                                         $content['infos'] = substr($content['subject'], 1, strpos($content['subject'], ':') - 1);
441                                         break;
442                         } // END - switch
443
444                         if (isValidId($content['userid'])) {
445                                 // Member found otherwise it's a system task
446                                 $content['userid'] = '{%pipe,generateUserProfileLink=' . $content['userid'] . '%}';
447                         } else {
448                                 $content['userid'] = '{--ADMIN_IS_SYSTEM_TASK--}';
449                         }
450
451                         // Add/translate some content
452                         $content['task_created'] = generateDateTime($content['task_created'], 2);
453
454                         // Is there extension task?
455                         if (isExtensionTask($content)) {
456                                 // Load extension row template
457                                 $OUT .= loadTemplate('admin_overview_list_ext_rows', TRUE, $content);
458                         } else {
459                                 // Load default row template
460                                 $OUT .= loadTemplate('admin_overview_list_rows', TRUE, $content);
461                         }
462                 } // END - while
463
464                 // Free memory
465                 sqlFreeResult($result_tasks);
466
467                 // Load footer template
468                 loadTemplate('admin_overview_table', FALSE, $OUT);
469         }
470 }
471
472 // [EOF]
473 ?>