New method generateExtensionInactiveMessage() introduced
[mailer.git] / inc / modules / admin / overview-inc.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 07/13/2004 *
4  * ===============                              Last change: 08/02/2004 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : overview-inc.php                                 *
8  * -------------------------------------------------------------------- *
9  * Short description : Output standard task management                  *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Standart-Aufgaben-Management ausgeben            *
12  * -------------------------------------------------------------------- *
13  * $Revision::                                                        $ *
14  * $Date::                                                            $ *
15  * $Tag:: 0.2.1-FINAL                                                 $ *
16  * $Author::                                                          $ *
17  * Needs to be in all Files and every File needs "svn propset           *
18  * svn:keywords Date Revision" (autoprobset!) at least!!!!!!            *
19  * -------------------------------------------------------------------- *
20  * Copyright (c) 2003 - 2008 by Roland Haeder                           *
21  * For more information visit: http://www.mxchange.org                  *
22  *                                                                      *
23  * This program is free software; you can redistribute it and/or modify *
24  * it under the terms of the GNU General Public License as published by *
25  * the Free Software Foundation; either version 2 of the License, or    *
26  * (at your option) any later version.                                  *
27  *                                                                      *
28  * This program is distributed in the hope that it will be useful,      *
29  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
31  * GNU General Public License for more details.                         *
32  *                                                                      *
33  * You should have received a copy of the GNU General Public License    *
34  * along with this program; if not, write to the Free Software          *
35  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
36  * MA  02110-1301  USA                                                  *
37  ************************************************************************/
38
39 // Some security stuff...
40 if ((!defined('__SECURITY')) || (!IS_ADMIN())) {
41         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), '/inc') + 4) . '/security.php';
42         require($INC);
43 }
44
45 function OUTPUT_STANDARD_OVERVIEW (&$result_tasks) {
46         // First check for solved and not assigned tasks and assign them to current admin
47         SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_task_system` SET `assigned_admin`=%s WHERE assigned_admin < 1 AND status != 'NEW'",
48         array(getCurrentAdminId()), __FILE__, __LINE__);
49
50         // We currently don't want to install an extension so let's find out if we need...
51         $EXT_LOAD_MODE = 'register';
52         $jobsDone = true;
53
54         // Open the extension directory
55         $extensionList = getArrayFromDirectory("inc/extensions/", "ext-", false, false);
56         foreach ($extensionList as $file) {
57                 // Is this file an extension?
58                 if ((substr($file, 0, 4) == "ext-") && (substr($file, -4) == '.php')) {
59                         //* DEBUG: */ echo $file."<br />\n";
60                         // Possible newly installed extension found so we extract extension's name
61                         $ext_name = strtolower(substr($file, 4, -4)); // Keep always extension names on lower case!!!
62
63                         // Init variables
64                         $result = false;
65
66                         // Check if extension is installed or not
67                         $ext_ver = GET_EXT_VERSION($ext_name);
68
69                         // Is the extension not yet installed?
70                         if (empty($ext_ver)) {
71                                 // Generate subject line
72                                 $ext_subj = sprintf("[%s:]", $ext_name);
73
74                                 // We maybe want to install an extension so let's test-drive it...
75                                 if (LOAD_EXTENSION($ext_name, $EXT_LOAD_MODE)) {
76                                         // Create a task for newly installed extension
77                                         CREATE_NEW_EXTENSION_TASK(getCurrentAdminId(), $ext_subj, $ext_name);
78                                 } // END - if
79                         } else {
80                                 // Test-drive extension in update mode
81                                 require(sprintf("%sinc/extensions/ext-%s.php", constant('PATH'), $ext_name));
82
83                                 // Update extension if extension is installed and outdated
84                                 //* DEBUG: */ print "ext={$ext_name},ver={EXT_GET_VERSION()}/".GET_EXT_VERSION($ext_name)."<br />\n";
85                                 if (EXT_GET_VERSION() > $ext_ver) {
86                                         // Update the extension
87                                         EXTENSION_UPDATE($ext_name, $ext_ver);
88                                 } // END - if
89
90                                 if (isset($GLOBALS['cache_array']['active_extensions'][$ext_name])) {
91                                         // Maybe we want to keept the current extension active?
92                                         if (($GLOBALS['cache_array']['active_extensions'][$ext_name] == 'Y') && (!EXT_IS_ACTIVE($ext_name))) {
93                                                 // Reactivate this extension!
94                                                 ACTIVATE_EXTENSION($ext_name);
95                                         } // END - if
96                                 } // END - if
97                         }
98                 } // END - if
99         } // END - foreach
100
101         // At last - but not least - check for own and new unassigned tasks...
102         $result_tasks = SQL_QUERY_ESC("SELECT `id`, `assigned_admin`, `userid`, `task_type`, `subject`, `text`, `task_created`
103 FROM `{!_MYSQL_PREFIX!}_task_system`
104 WHERE `assigned_admin`=%s OR (`assigned_admin`=0 AND `status`='NEW')
105 ORDER BY `userid` DESC, `task_type` DESC, `subject` ASC, `task_created` DESC",
106         array(getCurrentAdminId()), __FILE__, __LINE__);
107
108         if (SQL_NUMROWS($result_tasks) > 0) {
109                 // New jobs found!
110                 $jobsDone = false;
111         } // END - if
112
113         // Free the result
114         SQL_FREERESULT($result);
115
116         // Return status
117         return $jobsDone;
118 }
119
120 // Outputs selected tasks
121 function OUTPUT_SELECTED_TASKS ($POST, $result_tasks) {
122         if ((isset($POST['assign'])) && (count($POST['task']) > 0)) {
123                 // Assign / do tasks
124                 $OUT = ''; $SW = 2;
125                 foreach ($POST['task'] as $id => $sel) {
126                         $result_task = SQL_QUERY_ESC("SELECT `id`, `userid`, `task_type`, `subject`, `text`, `task_created`, `status`, `assigned_admin`
127 FROM `{!_MYSQL_PREFIX!}_task_system`
128 WHERE `id`=%s AND (`assigned_admin`=%s OR (`assigned_admin`=0 AND `status`='NEW'))
129 LIMIT 1",
130                                 array(bigintval($id), getCurrentAdminId()), __FILE__, __LINE__);
131
132                         // Task is found?
133                         if (SQL_NUMROWS($result_task) == 1) {
134                                 // Task is valid...
135                                 list($tid, $uid, $type, $subj, $text, $created, $status, $aid) = SQL_FETCHROW($result_task);
136
137                                 if ($aid == '0') {
138                                         // Assgin current admin to unassgigned task
139                                         SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_task_system` SET `assigned_admin`=%s WHERE `id`=%s LIMIT 1",
140                                         array(getCurrentAdminId(), bigintval($tid)), __FILE__, __LINE__);
141                                 } // END - if
142
143                                 $add = '';
144                                 // @TODO Rewrite this to a filter
145                                 if ($type == 'SUPPORT_MEMBER') {
146                                         $mode = substr($text, 0, strpos($text, ':'));
147                                         $text = substr($text, strpos($text, ':') + 1);
148                                         $add = "<li>{--ADMIN_TASK_SUPPORT_MODE--}: <strong>".$mode."</strong></li>";
149                                 } // END - if
150
151                                 // Is a userid assign?
152                                 if ($uid > 0) {
153                                         // Then load his data!
154                                         // @TODO Can this SQL be encapsulated in a function, so all similar queries can be rewritten?
155                                         $result_user = SQL_QUERY_ESC("SELECT gender, surname, family, email FROM `{!_MYSQL_PREFIX!}_user_data` WHERE userid=%s LIMIT 1",
156                                                 array(bigintval($uid)), __FILE__, __LINE__);
157
158                                         // Entry found?
159                                         if (SQL_NUMROWS($result_user) == 1) {
160                                                 list($gender, $sname, $fname, $email) = SQL_FETCHROW($result_user);
161                                                 $add = "<li>{--ADMIN_MEMBER_UID--}: <strong>".generateUserProfileLink($uid)." (<a href=\"".generateMemberEmailLink($email, "user_data")."\">".translateGender($gender)." ".$sname." ".$fname."</a>)</strong></li>";
162                                         } else {
163                                                 // Invalid userid, so log and zero it
164                                                 DEBUG_LOG(__FUNCTION__, __LINE__, 'Invalid userid=' . $uid . '-> Not found!');
165                                                 $uid = 0;
166                                         }
167
168                                         // Free result
169                                         SQL_FREERESULT($result_user);
170                                 } // END - if
171
172                                 // Decode entities of the text
173                                 $text = decodeEntities($text);
174
175                                 // Compile and insert text from task into table template
176                                 $text = LOAD_TEMPLATE('admin_extensions_text', true, $text);
177
178                                 // Initialize variables (no title for SQL commands by default)
179                                 $ext_name = ''; $ext_ver = '';
180                                 $title = getMessage('TASK_NO_TITLE');
181
182                                 // Shall I list SQL commands assigned to an extension installation or update task?
183                                 // OLD WAY: if (((GET_EXT_VERSION('sql_patches') != '') && (getConfig('verbose_sql') == 'Y')) || (!EXT_IS_ACTIVE('sql_patches'))) {
184                                 if ((getConfig('verbose_sql') == 'Y')) {
185                                         // Extract extension name from subject
186                                         $ext_name = substr($subj, 1, strpos($subj, ':') - 1);
187
188                                         // Update task or extension task?
189                                         if ($type == 'EXTENSION') {
190                                                 // @TODO Should we init sql_notes here or inside REGISTER_EXTENSION()?
191                                                 EXT_INIT_NOTES();
192
193                                                 // Load SQL commands for registering
194                                                 REGISTER_EXTENSION($ext_name, $id, true);
195
196                                                 // Add notes to text
197                                                 $text .= EXT_GET_NOTES();
198
199                                                 // Set title
200                                                 $title = getMessage('ADMIN_SQLS_EXECUTED_ON_REGISTER');
201                                         } elseif ($type == 'EXTENSION_UPDATE') {
202                                                 // Prepare extension name and version
203                                                 $ext_name = substr($ext_name, 7);
204                                                 $ext_name = substr($ext_name, 0, strpos($ext_name, '-'));
205                                                 $test = '[UPDATE-' . $ext_name . '-';
206                                                 $ext_ver = substr($subj, strlen($test));
207                                                 $ext_ver = substr($ext_ver, 0, strpos($ext_ver, ':'));
208
209                                                 // Load SQLs from file
210                                                 EXTENSION_UPDATE($ext_name, $ext_ver, true);
211
212                                                 // Add notes to text
213                                                 $text .= EXT_GET_NOTES();
214
215                                                 // Set title
216                                                 $title = getMessage('ADMIN_SQLS_EXECUTED_ON_UPDATE');
217                                         } else {
218                                                 // Remove extension's name
219                                                 $ext_name = '';
220                                         }
221
222                                         // Auto-init SQL handler
223                                         if (!IS_SQLS_VALID()) INIT_SQLS();
224
225                                         // Add SQLs to a table
226                                         if ((!empty($ext_name)) && (GET_EXT_VERSION('sql_patches')) && (getConfig('verbose_sql') == 'Y')) {
227                                                 // Add verbose SQL table
228                                                 $text .= EXTENSION_VERBOSE_TABLE($title, " class=\"admin_table top2 left2 right2\"", true, '100%')."<br />\n";
229                                         } // END - if
230                                 } elseif ((!empty($ext_name)) && (!empty($ext_ver))) {
231                                         // Run SQL commands in dry mode but only return the notes
232                                         EXTENSION_UPDATE($ext_name, $ext_ver, true);
233                                         $text .= EXT_GET_NOTES();
234                                 } else {
235                                         // This should not normally happen!
236                                         debug_report_bug('ext_name(' . $ext_name . ') or ext_ver(' . $ext_ver . ') is empty! sql_patches=' . GET_EXT_VERSION('sql_patches') . '/verbose_sql=' . getConfig('verbose_sql'));
237                                 }
238
239                                 // Prepare array for the template
240                                 $content = array(
241                                         'sw'        => $SW,
242                                         'subj'      => $subj,
243                                         'add'       => $add,
244                                         'text'      => $text,
245                                         'created'   => generateDateTime($created, '1'),
246                                         'extension' => $ext_name
247                                 );
248
249                                 // Load template
250                                 $OUT .= LOAD_TEMPLATE('admin_overview_row', true, $content);
251
252                                 // Which task do we actually have here?
253                                 // @TODO Rewrite this to something with include files
254                                 switch ($type) {
255                                         case 'EXTENSION': // Install new extensions
256                                                 $ext_name = substr($subj, 1, strpos($subj, ':') - 1);
257                                                 $result_lines = SQL_QUERY_ESC("SELECT `id` FROM `{!_MYSQL_PREFIX!}_extensions` WHERE `ext_name`='%s' LIMIT 1",
258                                                 array($ext_name), __FILE__, __LINE__);
259                                                 $lines = SQL_NUMROWS($result_lines);
260                                                 SQL_FREERESULT($result_lines);
261                                                 if ($lines == '0') {
262                                                         // New extension found
263                                                         $OUT .= LOAD_TEMPLATE('admin_ext_reg_form', true, array(
264                                                         'id'       => bigintval($id),
265                                                         'ext_name' => $ext_name
266                                                         ));
267                                                 } else {
268                                                         // Task is closed so nothing is todo
269                                                         $OUT .= "<div class=\"admin_failed\">{--ADMIN_EXT_ALREADY_REGISTERED--}</div>\n";
270
271                                                         // Close task but not already closes or deleted or update tasks
272                                                         if (($status != 'CLOSED') && ($status != 'DELETED') && ($type != 'EXTENSION_UPDATE')) {
273                                                                 // Solve the task
274                                                                 runFilterChain('solve_task', $tid);
275                                                         } // END - if
276                                                 }
277                                                 break;
278
279                                         case 'EXTENSION_UPDATE': // Extension update
280                                                 // Extension updates are installed automatically
281                                                 $OUT .= "<div class=\"admin_failed medium\">{--ADMIN_EXTENSION_UPDATED--}</div>\n";
282
283                                                 // Close task
284                                                 if (($status != 'CLOSED') && ($status != 'DELETED')) {
285                                                         // Solve the task
286                                                         runFilterChain('solve_task', $tid);
287                                                 } // END - if
288                                                 break;
289
290                                         case 'SUPPORT_MEMBER': // Assign on member's support request
291                                                 // @TODO This may also be rewritten to include files
292                                                 switch ($mode) {
293                                                         default: // @TODO Unknown support mode
294                                                         DEBUG_LOG(__FILE__, __LINE__, sprintf("Unknown support mode %s detected. This part is under construction!", $mode));
295                                                         $OUT .= "<div class=\"admin_failed medium\">".sprintf(getMessage('ADMIN_UNKNOWN_SUPPORT_MODE'), $mode)."</div>\n";
296                                                         break;
297                                                 }
298                                                 break;
299
300                                                         case 'PAYOUT_REQUEST': // Payout requests
301                                                                 if (EXT_IS_ACTIVE('payout')) {
302                                                                         // Extension is installed so let him send a notification to the user
303                                                                         $result_pay = SQL_QUERY_ESC("SELECT `id` FROM `{!_MYSQL_PREFIX!}_user_payouts` WHERE userid=%s AND payout_timestamp=%s LIMIT 1",
304                                                                         array(bigintval($uid), bigintval($created)), __FILE__, __LINE__);
305                                                                         list($pid) = SQL_FETCHROW($result_pay);
306                                                                         SQL_FREERESULT($result_pay);
307
308                                                                         if ((!empty($pid)) && ($pid > 0)) {
309                                                                                 // Payout ID can be obtained
310                                                                                 $content = array(
311                                                                 'pid' => $pid,
312                                                                 'tid' => $tid,
313                                                                                 );
314                                                                                 $OUT .= LOAD_TEMPLATE('admin_payout_overview_form', true, $content);
315                                                                         } else {
316                                                                                 // Problem obtaining payout ID
317                                                                                 $OUT .= "<div class=\"admin_failed medium\">{--PAYOUT_OBTAIN_ID_FAILED--}</div>\n";
318                                                                         }
319                                                                 } else {
320                                                                         // Extension is not installed
321                                                                         $OUT .= "<div class=\"admin_failed medium\">{--ADMIN_PAYOUT_NOT_INSTALLED--}</div>\n";
322                                                                 }
323                                                                 break;
324
325                                                         case 'WERNIS_REQUEST': // Wernis requests
326                                                                 if (EXT_IS_ACTIVE('wernis')) {
327                                                                         // Extension is installed so let him send a notification to the user
328                                                                         $result_pay = SQL_QUERY_ESC("SELECT `id` FROM `{!_MYSQL_PREFIX!}_user_wernis` WHERE userid=%s AND wernis_timestamp=%s LIMIT 1",
329                                                                         array(bigintval($uid), bigintval($created)), __FILE__, __LINE__);
330                                                                         list($pid) = SQL_FETCHROW($result_pay);
331                                                                         SQL_FREERESULT($result_pay);
332
333                                                                         if ((!empty($pid)) && ($pid > 0)) {
334                                                                                 // Payout ID can be obtained
335                                                                                 $content = array(
336                                                                 'pid' => $pid,
337                                                                 'tid' => $tid,
338                                                                                 );
339                                                                                 $OUT .= LOAD_TEMPLATE('admin_wernis_overview_form', true, $content);
340                                                                         } else {
341                                                                                 // Problem obtaining wernis ID
342                                                                                 $OUT .= "<div class=\"admin_failed medium\">{--WERNIS_OBTAIN_ID_FAILED--}</div>\n";
343                                                                         }
344                                                                 } else {
345                                                                         // Extension is not installed
346                                                                         $OUT .= "<div class=\"admin_failed medium\">{--ADMIN_WERNIS_NOT_INSTALLED--}</div>\n";
347                                                                 }
348                                                                 break;
349
350                                                         case 'HOLIDAY_REQUEST': // Holiday requests
351                                                                 $OUT .= LOAD_TEMPLATE('admin_task_holiday', true, $uid);
352                                                                 break;
353
354                                                         case 'NL_UNSUBSCRIBE': // Newsletter unsubscriptions
355                                                                 $result = SQL_QUERY_ESC("SELECT nl_timespan FROM `{!_MYSQL_PREFIX!}_user_data` WHERE userid=%s LIMIT 1",
356                                                                 array(bigintval($uid)), __FILE__, __LINE__);
357                                                                 list($span) = SQL_FETCHROW($result);
358                                                                 SQL_FREERESULT($result);
359
360                                                                 if ($span > 0) {
361                                                                         // Undone unscubscribe request
362                                                                         $content = array(
363                                                         'uid' => $uid,
364                                                         'id'  => $tid
365                                                                         );
366                                                                         $OUT .= LOAD_TEMPLATE('admin_newsletter_tsk', true, $content);
367                                                                 } else {
368                                                                         // Already unsubscribed
369                                                                         $OUT .= "<div class=\"admin_failed medium\">".ADMIN_NL_UNSUBSCRIBE_ALREADY."</div>\n";
370                                                                 }
371                                                                 break;
372
373                                                         default: // Unknown task type
374                                                                 DEBUG_LOG(__FILE__, __LINE__, sprintf("Unknown task type %s detected.", $type));
375                                                                 $OUT .= "<div class=\"admin_failed medium\">".sprintf(getMessage('ADMIN_UNKNOWN_TASK_TYPE'), $type, $id)."</div>\n";
376                                                                 break;
377                                 }
378                                 $OUT .= "  </td>
379   <td width=\"1%\" class=\"switch_sw".$SW." bottom2 right2\">&nbsp;</td>
380 </tr>\n";
381                         } // END - if
382
383                         // Free result
384                         SQL_FREERESULT($result_task);
385
386                         // Switch colors
387                         $SW = 3 - $SW;
388                 } // END - foreach
389                 define('__TASK_ROWS', $OUT);
390
391                 // Load final template
392                 LOAD_TEMPLATE('admin_overview_list');
393         } else {
394                 if ((isset($POST['task'])) && ((count($POST['task']) > 0) || ($POST['task'][0] == '1'))) {
395                         // Only unassign / delete tasks when there are selected tasks posted
396                         if (!empty($POST['unassign'])) {
397                                 // Unassign from tasks
398                                 foreach ($POST['task'] as $id => $sel) {
399                                         SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_task_system` SET `assigned_admin`=0 WHERE `id`=%s AND `assigned_admin`=%s LIMIT 1",
400                                         array(bigintval($id), getCurrentAdminId()), __FILE__, __LINE__);
401                                 }
402                         } elseif (isset($POST['del'])) {
403                                 // Delete tasks
404                                 foreach ($POST['task'] as $id => $sel) {
405                                         SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_task_system` WHERE `id`=%s AND assigned_admin IN (%s,0) LIMIT 1",
406                                         array(bigintval($id), getCurrentAdminId()), __FILE__, __LINE__);
407                                 }
408                         }
409
410                         // Update query
411                         $result_tasks = SQL_QUERY_ESC("SELECT id, assigned_admin, userid, task_type, subject, text, task_created FROM `{!_MYSQL_PREFIX!}_task_system` WHERE `assigned_admin`=%s OR (`assigned_admin`=0 AND `status`='NEW') ORDER BY task_created DESC",
412                         array(getCurrentAdminId()), __FILE__, __LINE__);
413                 } // END - if
414
415                 // There are uncompleted jobs!
416                 $OUT = ''; $SW = 2;
417                 while ($content = SQL_FETCHARRAY($result_tasks)) {
418                         // Init infos
419                         $content['infos'] = '---';
420
421                         // Generate assign link
422                         $content['assigned_admin'] = generateAdminLink($content['assigned_admin']);
423
424                         // Generate infos
425                         switch ($content['task_type'])
426                         {
427                                 case 'EXTENSION':
428                                 case 'EXTENSION_UPDATE':
429                                         $content['infos'] = substr($content['subject'], 1, strpos($content['subject'], ':') - 1);
430                                         break;
431                         }
432
433                         // Get task type
434                         $content['task_type_msg'] = getMessage('ADMIN_TASK_IS_'.strtoupper($content['task_type']).'');
435
436                         if ($content['userid'] > 0) {
437                                 // Member found otherwise it's a system task
438                                 $content['userid'] = generateUserProfileLink($content['userid']);
439                         } else {
440                                 $content['userid'] = "<em>{--ADMIN_IS_SYSTEM_TASK--}</em>";
441                         }
442
443                         // Prepare content
444                         // @TODO Rewritings: admin->assigned_admin,uid->userid,type->task_type_msg in template
445                         $content = merge_array($content, array(
446                                 'sw'      => $SW,
447                                 'admin'   => $content['assigned_admin'],
448                                 'uid'     => $content['userid'],
449                                 'type'    => $content['task_type_msg'],
450                                 'created' => generateDateTime($content['task_created'], '2')
451                         ));
452
453                         // Do we have extension task?
454                         if (($content['task_type'] == 'EXTENSION') && (GET_EXT_VERSION($content['infos']) == '')) {
455                                 // Load extension row template
456                                 $OUT .= LOAD_TEMPLATE("admin_overview_list_ext_rows", true, $content);
457                         } else {
458                                 // Load default row template
459                                 $OUT .= LOAD_TEMPLATE("admin_overview_list_rows", true, $content);
460                         }
461
462                         // Switch color
463                         $SW = 3 - $SW;
464                 }
465
466                 // Free memory
467                 SQL_FREERESULT($result_tasks);
468
469                 // Load footer template
470                 LOAD_TEMPLATE("admin_overview_table", false, $OUT);
471         }
472 }
473 //
474 ?>