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