Damn typo fixed... ;-)
[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(GET_CURRENT_ADMIN_ID()), __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         $JOBS_DONE = true;
53
54         // Open the extension directory
55         $handle = opendir(constant('PATH')."inc/extensions/") or mxchange_die("Cannot read extension directory!");
56         while ($file = readdir($handle)) {
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(GET_CURRENT_ADMIN_ID(), $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 - while
100
101         // Close directory handle
102         closedir($handle);
103
104         // At last - but not least - check for own and new unassigned tasks...
105         $result_tasks = SQL_QUERY_ESC("SELECT id, assigned_admin, userid, task_type, subject, text, task_created
106 FROM `{!_MYSQL_PREFIX!}_task_system`
107 WHERE assigned_admin='%s' OR (assigned_admin='0' AND `status`='NEW')
108 ORDER BY userid DESC, task_type DESC, subject, task_created DESC",
109                 array(GET_CURRENT_ADMIN_ID()), __FILE__, __LINE__);
110
111         if (SQL_NUMROWS($result_tasks) > 0) {
112                 // New jobs found!
113                 $JOBS_DONE = false;
114         } // END - if
115
116         // Free the result
117         SQL_FREERESULT($result);
118
119         // Return status
120         return $JOBS_DONE;
121 }
122
123 // Outputs selected tasks
124 function OUTPUT_SELECTED_TASKS ($POST, $result_tasks) {
125         global $NOTES;
126         if ((isset($POST['assign'])) && (count($POST['task']) > 0)) {
127                 // Assign / do tasks
128                 $OUT = ""; $SW = 2;
129                 foreach ($POST['task'] as $id => $sel) {
130                         $result_task = SQL_QUERY_ESC("SELECT id, userid, task_type, subject, text, task_created, status, assigned_admin FROM `{!_MYSQL_PREFIX!}_task_system` WHERE id=%s AND (assigned_admin='%s' OR (assigned_admin='0' AND `status`='NEW')) LIMIT 1",
131                                 array(bigintval($id), GET_CURRENT_ADMIN_ID()), __FILE__, __LINE__);
132                         if (SQL_NUMROWS($result_task) == 1) {
133                                 // Task is valid...
134                                 list($tid, $uid, $type, $subj, $text, $created, $status, $aid) = SQL_FETCHROW($result_task);
135                                 SQL_FREERESULT($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(GET_CURRENT_ADMIN_ID(), bigintval($tid)), __FILE__, __LINE__);
141                                 } // END - if
142
143                                 $ADD = "";
144                                 if ($type == "SUPPORT_MEMBER") {
145                                         $mode = substr($text, 0, strpos($text, ":"));
146                                         $text = substr($text, strpos($text, ":") + 1);
147                                         $ADD = "<li>{--ADMIN_TASK_SUPPORT_MODE--}: <strong>".$mode."</strong></li>";
148                                 } // END - if
149
150                                 if ($uid > 0) {
151                                         $result_user = SQL_QUERY_ESC("SELECT gender, surname, family, email FROM `{!_MYSQL_PREFIX!}_user_data` WHERE userid=%s LIMIT 1",
152                                          array(bigintval($uid)), __FILE__, __LINE__);
153                                         if (SQL_NUMROWS($result_user) == 1) {
154                                                 list($gender, $sname, $fname, $email) = SQL_FETCHROW($result_user);
155                                                 SQL_FREERESULT($result_user);
156                                                 $ADD = "<li>{--ADMIN_MEMBER_UID--}: <strong>".ADMIN_USER_PROFILE_LINK($uid)." (<a href=\"".CREATE_EMAIL_LINK($email, "user_data")."\">".TRANSLATE_GENDER($gender)." ".$sname." ".$fname."</a>)</strong></li>";
157                                         } // END - if
158                                 } // END - if
159
160                                 // Decode entities of the text
161                                 $text = decodeEntities($text);
162
163                                 // Compile and insert text from task into table template
164                                 $text = LOAD_TEMPLATE("admin_extensions_text", true, $text);
165
166                                 // Initialize variables (no title for SQL commands by default)
167                                 $ext_name = "";
168                                 $title = getMessage('TASK_NO_TITLE');
169
170                                 // Shall I list SQL commands assigned to an extension installation or update task?
171                                 if (((GET_EXT_VERSION("sql_patches") != '') && (getConfig('verbose_sql') == "Y")) || (!EXT_IS_ACTIVE("sql_patches"))) {
172                                         $ext_name = substr($subj, 1, strpos($subj, ":") - 1);
173                                         if ($type == "EXTENSION") {
174                                                 // Load SQL commands for registering
175                                                 SET_SQLS(EXTENSION_REGISTER($ext_name, $id, true));
176
177                                                 // Add notes to text
178                                                 $text .= $NOTES;
179
180                                                 // Set title
181                                                 $title = getMessage('ADMIN_SQLS_EXECUTED_ON_REGISTER');
182                                         } elseif ($type == "EXTENSION_UPDATE") {
183                                                 // Prepare extension name and version
184                                                 $ext_name = substr($ext_name, 7);
185                                                 $ext_name = substr($ext_name, 0, strpos($ext_name, "-"));
186                                                 $test = "[UPDATE-".$ext_name."-";
187                                                 $ext_ver = substr($subj, strlen($test));
188                                                 $ext_ver = substr($ext_ver, 0, strpos($ext_ver, ":"));
189
190                                                 // Load SQLs from file
191                                                 EXTENSION_UPDATE($ext_name, $ext_ver, true);
192
193                                                 // Add notes to text
194                                                 $text .= $NOTES;
195
196                                                 // Set title
197                                                 $title = getMessage('ADMIN_SQLS_EXECUTED_ON_UPDATE');
198                                         } else {
199                                                 // Remove extension's name
200                                                 $ext_name = "";
201                                         }
202
203                                         // Add SQLs to a table
204                                         if (!IS_SQLS_VALID()) INIT_SQLS();
205                                         if (empty($title)) $title = "";
206                                         if ((!empty($ext_name)) && (GET_EXT_VERSION("sql_patches")) && (getConfig('verbose_sql') == "Y")) {
207                                                 // Add verbose SQL table
208                                                 $text .= EXTENSION_VERBOSE_TABLE($title, " class=\"admin_table top2 left2 right2\"", true, "100%")."<br />\n";
209                                         } // END - if
210                                 } else {
211                                         // Run SQL commands in dry mode but only return the notes
212                                         EXTENSION_UPDATE($ext_name, $ext_ver, true);
213                                         $text .= $NOTES;
214                                 }
215
216                                 // Prepare array for the template
217                                 $content = array(
218                                         'sw'        => $SW,
219                                         'subj'      => $subj,
220                                         'add'       => $ADD,
221                                         'text'      => $text,
222                                         'created'   => MAKE_DATETIME($created, "1"),
223                                         'extension' => $ext_name
224                                 );
225
226                                 // Load template
227                                 $OUT .= LOAD_TEMPLATE("admin_overview_row", true, $content);
228
229                                 // Which task do we actually have here?
230                                 // @TODO Rewrite this to something with include files
231                                 switch ($type)
232                                 {
233                                 case "EXTENSION": // Install new extensions
234                                         $ext_name = substr($subj, 1, strpos($subj, ":") - 1);
235                                         $result_lines = SQL_QUERY_ESC("SELECT id FROM `{!_MYSQL_PREFIX!}_extensions` WHERE ext_name='%s' LIMIT 1",
236                                                 array($ext_name), __FILE__, __LINE__);
237                                         $lines = SQL_NUMROWS($result_lines);
238                                         SQL_FREERESULT($result_lines);
239                                         if ($lines == "0") {
240                                                 // New extension found
241                                                 $OUT .= LOAD_TEMPLATE("admin_ext_reg_form", true, array(
242                                                         'id'       => bigintval($id),
243                                                         'ext_name' => $ext_name
244                                                 ));
245                                         } else {
246                                                 // Task is closed so nothing is todo
247                                                 $OUT .= "<div class=\"admin_failed\">{--ADMIN_EXT_ALREADY_REGISTERED--}</div>\n";
248
249                                                 // Close task but not already closes or deleted or update tasks
250                                                 if (($status != "CLOSED") && ($status != "DELETED") && ($type != "EXTENSION_UPDATE")) {
251                                                         // Solve the task
252                                                         runFilterChain('solve_task', $tid);
253                                                 } // END - if
254                                         }
255                                         break;
256
257                                 case "EXTENSION_UPDATE": // Extension update
258                                         // Extension updates are installed automatically
259                                         $OUT .= "<div class=\"admin_failed medium\">{--ADMIN_EXTENSION_UPDATED--}</div>\n";
260
261                                         // Close task
262                                         if (($status != "CLOSED") && ($status != "DELETED")) {
263                                                 // Solve the task
264                                                 runFilterChain('solve_task', $tid);
265                                         } // END - if
266                                         break;
267
268                                 case "SUPPORT_MEMBER": // Assign on member's support request
269                                         // @TODO This may also be rewritten to include files
270                                         switch ($mode)
271                                         {
272                                         default: // @TODO Unknown support mode
273                                                 DEBUG_LOG(__FILE__, __LINE__, sprintf("Unknown support mode %s detected. This part is under construction!", $mode));
274                                                 $OUT .= "<div class=\"admin_failed medium\">".ADMIN_UNKNOWN_SUPPORT_MODE_1.$mode.ADMIN_UNKNOWN_SUPPORT_MODE_2."</div>\n";
275                                                 break;
276                                         }
277                                         break;
278
279                                 case "PAYOUT_REQUEST": // Payout requests
280                                         if (EXT_IS_ACTIVE("payout")) {
281                                                 // Extension is installed so let him send a notification to the user
282                                                 $result_pay = SQL_QUERY_ESC("SELECT id FROM `{!_MYSQL_PREFIX!}_user_payouts` WHERE userid=%s AND payout_timestamp=%s LIMIT 1",
283                                                         array(bigintval($uid), bigintval($created)), __FILE__, __LINE__);
284                                                 list($pid) = SQL_FETCHROW($result_pay);
285                                                 SQL_FREERESULT($result_pay);
286
287                                                 if ((!empty($pid)) && ($pid > 0)) {
288                                                         // Payout ID can be obtained
289                                                         $content = array(
290                                                                 'pid' => $pid,
291                                                                 'tid' => $tid,
292                                                         );
293                                                         $OUT .= LOAD_TEMPLATE("admin_payout_overview_form", true, $content);
294                                                 } else {
295                                                         // Problem obtaining payout ID
296                                                         $OUT .= "<div class=\"admin_failed medium\">".PAYOUT_OBTAIN_ID_FAILED."</div>\n";
297                                                 }
298                                         } else {
299                                                 // Extension is not installed
300                                                 $OUT .= "<div class=\"admin_failed medium\">{--ADMIN_PAYOUT_NOT_INSTALLED--}</div>\n";
301                                         }
302                                         break;
303
304                                 case "WERNIS_REQUEST": // Wernis requests
305                                         if (EXT_IS_ACTIVE("wernis")) {
306                                                 // Extension is installed so let him send a notification to the user
307                                                 $result_pay = SQL_QUERY_ESC("SELECT id FROM `{!_MYSQL_PREFIX!}_user_wernis` WHERE userid=%s AND wernis_timestamp=%s LIMIT 1",
308                                                         array(bigintval($uid), bigintval($created)), __FILE__, __LINE__);
309                                                 list($pid) = SQL_FETCHROW($result_pay);
310                                                 SQL_FREERESULT($result_pay);
311
312                                                 if ((!empty($pid)) && ($pid > 0)) {
313                                                         // Payout ID can be obtained
314                                                         $content = array(
315                                                                 'pid' => $pid,
316                                                                 'tid' => $tid,
317                                                         );
318                                                         $OUT .= LOAD_TEMPLATE("admin_wernis_overview_form", true, $content);
319                                                 } else {
320                                                         // Problem obtaining wernis ID
321                                                         $OUT .= "<div class=\"admin_failed medium\">{--WERNIS_OBTAIN_ID_FAILED--}</div>\n";
322                                                 }
323                                         } else {
324                                                 // Extension is not installed
325                                                 $OUT .= "<div class=\"admin_failed medium\">{--ADMIN_WERNIS_NOT_INSTALLED--}</div>\n";
326                                         }
327                                         break;
328
329                                 case "HOLIDAY_REQUEST": // Holiday requests
330                                         $OUT .= LOAD_TEMPLATE("admin_task_holiday", true, $uid);
331                                         break;
332
333                                 case "NL_UNSUBSCRIBE": // Newsletter unsubscriptions
334                                         $result = SQL_QUERY_ESC("SELECT nl_timespan FROM `{!_MYSQL_PREFIX!}_user_data` WHERE userid=%s LIMIT 1",
335                                                 array(bigintval($uid)), __FILE__, __LINE__);
336                                         list($span) = SQL_FETCHROW($result);
337                                         SQL_FREERESULT($result);
338
339                                         if ($span > 0) {
340                                                 // Undone unscubscribe request
341                                                 $content = array(
342                                                         'uid' => $uid,
343                                                         'id'  => $tid
344                                                 );
345                                                 $OUT .= LOAD_TEMPLATE("admin_newsletter_tsk", true, $content);
346                                         } else {
347                                                 // Already unsubscribed
348                                                 $OUT .= "<div class=\"admin_failed medium\">".ADMIN_NL_UNSUBSCRIBE_ALREADY."</div>\n";
349                                         }
350                                         break;
351
352                                 default: // Unknown task type
353                                         DEBUG_LOG(__FILE__, __LINE__, sprintf("Unknown task type %s detected.", $type));
354                                         $OUT .= "<div class=\"admin_failed medium\">".ADMIN_UNKNOWN_TASK_TYPE_1.$type.ADMIN_UNKNOWN_TASK_TYPE_2.$id.ADMIN_UNKNOWN_TASK_TYPE_3."</div>\n";
355                                         break;
356                                 }
357                                 $OUT .= "  </td>
358   <td width=\"1%\" class=\"switch_sw".$SW." bottom2 right2\">&nbsp;</td>
359 </tr>\n";
360                         } // END - if
361                         $SW = 3 - $SW;
362                 } // END - foreach
363                 define('__TASK_ROWS', $OUT);
364
365                 // Load final template
366                 LOAD_TEMPLATE("admin_overview_list");
367         } else {
368                 if ((isset($POST['task'])) && ((count($POST['task']) > 0) || ($POST['task'][0] == "1"))) {
369                         // Only unassign / delete tasks when there are selected tasks posted
370                         if (!empty($POST['unassign'])) {
371                                 // Unassign from tasks
372                                 foreach ($POST['task'] as $id => $sel) {
373                                         SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_task_system` SET assigned_admin=0 WHERE id=%s AND assigned_admin=%s LIMIT 1",
374                                                 array(bigintval($id), GET_CURRENT_ADMIN_ID()), __FILE__, __LINE__);
375                                 }
376                         } elseif (isset($POST['del'])) {
377                                 // Delete tasks
378                                 foreach ($POST['task'] as $id => $sel) {
379                                         SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_task_system` WHERE id=%s AND assigned_admin IN (%s,0) LIMIT 1",
380                                                 array(bigintval($id), GET_CURRENT_ADMIN_ID()), __FILE__, __LINE__);
381                                 }
382                         }
383
384                         // Update query
385                         $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",
386                                 array(GET_CURRENT_ADMIN_ID()), __FILE__, __LINE__);
387                 } // END - if
388
389                 // There are uncompleted jobs!
390                 // @TODO Rewrite this to one template and use $OUT = ...
391                 LOAD_TEMPLATE("admin_overview_header");
392                 $OUT = ""; $SW = 2;
393                 while ($content = SQL_FETCHARRAY($result_tasks)) {
394                         // Init infos
395                         $content['infos'] = "---";
396
397                         // Generate assign link
398                         $content['assigned_admin'] = GENERATE_AID_LINK($content['assigned_admin']);
399
400                         // Generate infos
401                         switch ($content['task_type'])
402                         {
403                         case "EXTENSION":
404                         case "EXTENSION_UPDATE":
405                                 $content['infos'] = substr($content['subject'], 1, strpos($content['subject'], ":") - 1);
406                                 break;
407                         }
408
409                         // Get task type
410                         $content['task_type_msg'] = getMessage('ADMIN_TASK_IS_'.strtoupper($content['task_type']).'');
411
412                         if ($content['userid'] > 0) {
413                                 // Member found otherwise it's a system task
414                                 $content['userid'] = ADMIN_USER_PROFILE_LINK($content['userid']);
415                         } else {
416                                 $content['userid'] = "<em>{--ADMIN_IS_SYSTEM_TASK--}</em>";
417                         }
418
419                         // Prepare content
420                         // @TODO Rewritings: admin->assigned_admin,uid->userid,type->task_type_msg in template
421                         $content = merge_array($content, array(
422                                 'sw'      => $SW,
423                                 'admin'   => $content['assigned_admin'],
424                                 'uid'     => $content['userid'],
425                                 'type'    => $content['task_type_msg'],
426                                 'created' => MAKE_DATETIME($content['task_created'], "2")
427                         ));
428
429                         // Do we have extension task?
430                         if (($content['task_type'] == "EXTENSION") && (GET_EXT_VERSION($content['infos']) == "")) {
431                                 // Load extension row template
432                                 // @TODO Rewrite this to $OUT .= ..., true, ...
433                                 LOAD_TEMPLATE("admin_overview_list_ext_rows", false, $content);
434                         } else {
435                                 // Load default row template
436                                 // @TODO Rewrite this to $OUT .= ..., true, ...
437                                 LOAD_TEMPLATE("admin_overview_list_rows", false, $content);
438                         }
439
440                         // Switch color
441                         $SW = 3 - $SW;
442                 }
443
444                 // Free memory
445                 SQL_FREERESULT($result_tasks);
446
447                 // Load footer template
448                 LOAD_TEMPLATE("admin_overview_footer");
449         }
450 }
451 //
452 ?>