c2791e8293034ecdbd87b17c3de28361efed184b
[gnucomo.git] / src / web / notification.php
1 <?php 
2
3 /**************************************************************************
4 **  (c) Copyright 2003, Andromeda Technology & Automation
5 ** This is free software; you can redistribute it and/or modify it under the
6 ** terms of the GNU General Public License, see the file COPYING.
7 ***************************************************************************
8 ** MODULE INFORMATION *
9 ***********************
10 **      FILE NAME      : notification.php
11 **      SYSTEM NAME    : Gnucomo - Gnu Computer Monitoring
12 **      VERSION NUMBER : $Revision: 1.2 $
13 **
14 **  DESCRIPTION      : Display and handle notifications.
15 **                     There are two major views to this page: either a list of
16 **                     all notifications for an object or the display of a single
17 **                     notification. In the 'single notification' view, actions
18 **                     can be added to the notification and the status can be
19 **                     changed as a result of those actions.
20 **
21 **                     Input: GET['oid']   - If set, list all notifications for object.
22 **                            GET['notid'] - If set, display a single notification.
23 **                            POST['notid']- If set, update the notification.
24 **
25 **  EXPORTED OBJECTS : 
26 **  LOCAL    OBJECTS : 
27 **  MODULES  USED    :
28 ***************************************************************************
29 **  ADMINISTRATIVE INFORMATION *
30 ********************************
31 **      ORIGINAL AUTHOR : Arjen Baart - arjen@andromeda.nl
32 **      CREATION DATE   : Dec 12, 2002
33 **      LAST UPDATE     : Feb 04, 2003
34 **      MODIFICATIONS   : 
35 **************************************************************************/
36
37 /*****************************
38    $Log: notification.php,v $
39    Revision 1.2  2003-02-13 09:01:29  arjen
40    All web interface pages use the page class.
41
42    Revision 1.1  2003/02/05 09:48:14  arjen
43    Added display and handling of notifications
44
45 ******************************/
46
47 // RCSID = "$Id: notification.php,v 1.2 2003-02-13 09:01:29 arjen Exp $";
48
49 ini_set('include_path', '.:./classes:../phpclasses');
50
51 require_once('page.class.php');
52
53 function clientscripts()
54 {
55 ?>
56
57 <script language='JavaScript'>
58
59 function CheckRemark(f)
60 {
61    if (f.remark.value == "")
62    {
63       alert("Your remark is still empty");
64       return false;
65    }
66    return true;
67 }
68
69 </script>
70
71 <?php
72 }
73
74 /*
75  *   Add an action to the notification $note.
76  *   The status of the notification may alter as a result.
77  */
78
79 function add_action($db, $note, $actionid, $remark)
80 {
81    $r = pg_exec($db, "SELECT statuscode FROM action WHERE actionid=" . $actionid);
82    $action = pg_fetch_object($r, 0);
83
84    if ($action->statuscode != "" && $note->statuscode != $action->statuscode)
85    {
86       //  Update the status of the notification.
87
88       $note->statuscode = $action->statuscode;
89
90       pg_exec($db, "UPDATE notification SET statuscode='" . $note->statuscode
91                 . "' WHERE notificationid = " . $note->notificationid);
92    }
93
94    //  Add the action to the action_user table.
95
96    pg_exec($db, "INSERT INTO action_user (actionid, username, notificationid,
97                            timestamp, statuscode, remarks) values ('$actionid', '"
98                          . $_SESSION['username'] . "', '" . $note->notificationid
99                          . "', '" . date('Y-m-d H:i:s') . "', '" . $note->statuscode
100                          . "', '" . $remark . "')");
101
102    return $note->statuscode;
103 }
104
105 class notification_page extends page
106 {
107
108    function display_notification($note)
109    {
110       $res = pg_exec($this->database, "SELECT objectname FROM object WHERE objectid=" . $note->objectid);
111       $obj = pg_fetch_object($res, 0);
112       echo "<h1>Notification " . $note->notificationid . " for " . $obj->objectname . "</h1><hr>";
113
114       $r = pg_exec($this->database, "SELECT description from type_of_issue WHERE type_of_issueid ="
115                        . $note->type_of_issueid);
116       $issue = pg_fetch_object($r, 0);
117
118       $r = pg_exec($this->database, "SELECT statusname FROM status
119                                      WHERE statuscode = '" . $note->statuscode . "'");
120
121       $status = pg_fetch_object($r, 0);
122
123       echo "Issue : " . $issue->description . ", Priority " . $note->priority . "<br>";
124       echo "Creation time : " . $note->timestamp . "<br>";
125       echo "Current status : " . $status->statusname . "<br>";
126
127       //  List all actions that occurred on this notification
128
129       echo "<h2>Action history for notification " . $note->notificationid . "</h2>";
130       echo "<table>";
131       echo "<tr><th>Time</th><th>User</th><th>Remarks</th></tr>";
132
133       $r = pg_exec($this->database, "SELECT actionid, username, timestamp, remarks
134                                      FROM action_user WHERE notificationid=" . $note->notificationid
135                                    ." ORDER BY timestamp");
136       for ($row = 0; $row < pg_numrows($r); $row++)
137       {
138          $action_user = pg_fetch_object($r, $row);
139
140          echo "<tr><td>";
141          echo $action_user->timestamp;
142          echo "</td><td>";
143          echo $action_user->username;
144          echo "</td><td>";
145          echo htmlspecialchars(stripslashes($action_user->remarks));
146          echo "</td></tr>";
147       }
148       echo "</table>";
149
150       //  See if we can list any parameters for this notification
151
152       $r = pg_exec($this->database, "SELECT * FROM parameter WHERE paramid IN
153                                      ( SELECT paramid FROM parameter_notification
154                                      WHERE notificationid = " . $note->notificationid . ")");
155       if (pg_numrows($r) > 0)
156       {
157          echo "<h2>Parameters involved in this notification</h2>";
158          echo "<table>";
159          echo "<tr><th>Class</th><th>Name</th><th>Description</th><th>History</th></tr>";
160
161          for ($row = 0; $row < pg_numrows($r); $row++)
162          {
163             $p = pg_fetch_object($r, $row);
164             $res = pg_exec($this->database, "SELECT * from history WHERE paramid=" . $p->paramid
165                                             . " ORDER BY modified");
166
167             echo "<tr><td>";
168             echo $p->class;
169             echo "</td><td>";
170             echo $p->name;
171             echo "</td><td>";
172             echo $p->description;
173             echo "</td><td>";
174             for ($hrow = 0; $hrow < pg_numrows($res); $hrow++)
175             {
176                $hist = pg_fetch_object($res, $hrow);
177                echo $hist->modified . " ";
178                echo $hist->change_nature . " ";
179                echo $hist->changed_property . " ";
180                echo $hist->new_value;
181                echo "<br>";
182             }
183             echo "</td></tr>";
184
185          }
186          echo "</table>";
187       }
188
189       //  See if we can list any log entries for this notification
190
191       $r = pg_exec($this->database, "SELECT * FROM log WHERE logid IN
192                                      ( SELECT logid FROM log_notification
193                                      WHERE notificationid = " . $note->notificationid . ")");
194       if (pg_numrows($r) > 0)
195       {
196          echo "<h2>Log entries involved in this notification</h2>";
197          echo "<table>";
198          echo "<tr><th>Time</th><th>Service</th><th>Log entry</th></tr>";
199
200          for ($row = 0; $row < pg_numrows($r); $row++)
201          {
202             $p = pg_fetch_object($r, $row);
203
204             echo "<tr><td>";
205             echo $p->object_timestamp;
206             echo "</td><td>";
207             echo $p->servicecode;
208             echo "</td><td>";
209             echo $p->rawdata;
210             echo "</td></tr>";
211
212          }
213          echo "</table>";
214       }
215
216    }
217
218    function notification_form($note)
219    {
220       /*
221        *  Depending on the state of the notification, specific actions
222        *  are possible. One such actions determines a transition to
223        *  the next state. The array $possible_action lists the state transitions
224        *  for each state.
225        */
226
227       $possible_action = array
228       (
229          "opn" => array (3, 6, 7),
230          "pen" => array (3, 6, 7, 8, 11, 18),
231          "inv" => array (3, 9),
232          "vrf" => array (12, 13),
233          "cls" => array (19)
234       );
235
236       echo "<h2>Enter a remark for the next action</h2>";
237       echo "<form method='post' onSubmit='return CheckRemark(this)'>";
238       echo "<textarea name='remark'></textarea><br>";
239
240       /*  Display a list of possible actions */
241
242       echo "<h2>Select on of the actions to take below</h2>";
243
244       $first = true;
245
246       foreach ($possible_action[$note->statuscode] as $act)
247       {
248          $action = pg_fetch_object(pg_exec("SELECT description FROM action WHERE actionid=$act"),0);
249          echo "<input type='radio' name='actionid'";
250          if ($first)
251          {
252             $first = false;
253             echo " checked='on'";
254          }
255          echo " value='$act'>" . $action->description . "<br>";
256       }
257
258       echo "<input type='submit' value='Submit action'>";
259       echo "</form>";
260    }
261
262    function Body()
263    {
264
265       if ($_SERVER['REQUEST_METHOD'] == 'GET' && !empty($_GET['oid']))
266       {
267
268          //  Display a list of all notifications for this object['oid']
269
270          $res = pg_exec($this->database, "SELECT objectname FROM object WHERE objectid=" . $_GET['oid']);
271          $obj = pg_fetch_object($res, 0);
272          echo "<h1>Notifications for " . $obj->objectname . "</h1><hr>";
273
274          $res = pg_exec($this->database, "SELECT notificationid, timestamp, type_of_issueid,
275                                               statuscode, priority
276                                           FROM notification WHERE objectid=" . $_GET['oid']
277                                          . " ORDER BY notificationid");
278
279          echo "<table>";
280          $row = 0;
281          while ($row < pg_numrows($res))
282          {
283             $note = pg_fetch_object($res, $row);
284             $r = pg_exec($this->database, "SELECT name from type_of_issue WHERE type_of_issueid ="
285                           . $note->type_of_issueid);
286             $issue = pg_fetch_object($r, 0);
287             echo "<tr><td align='center'>\n";
288             echo $note->timestamp;
289             echo "</td><td>";
290             echo $note->notificationid;
291             echo "</td><td>";
292             echo "<a href='notification.php?notid=";
293             echo $note->notificationid;
294             echo "'>";
295             echo $issue->name;
296             echo "</a>";
297             echo "</td><td>";
298             echo $note->statuscode;
299             echo "</td><td>";
300             echo $note->priority;
301             echo "</td></tr>\n";
302             $row++;
303          }
304          echo "</table>";
305       }
306       else if ($_SERVER['REQUEST_METHOD'] == 'GET' && !empty($_GET['notid']))
307       {
308          clientscripts();
309
310          //  Display all information about a single notification['notid']
311
312          $res = pg_exec($this->database, "SELECT * from notification
313                                           WHERE notificationid=" . $_GET['notid']);
314          $note = pg_fetch_object($res, 0);
315
316          if ($note->statuscode == 'new')
317          {
318             $note->statuscode = add_action($this->database, $note, 2, "Displayed through web interface");
319          }
320
321          $this->display_notification($note);
322          echo "<hr>";
323          $this->notification_form($note);
324       }
325       else if ($_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_GET['notid']))
326       {
327          clientscripts();
328
329          //  Display all information about a single notification['notid']
330
331          $res = pg_exec($this->database, "SELECT * from notification
332                                           WHERE notificationid=" . $_GET['notid']);
333          $note = pg_fetch_object($res, 0);
334
335          $note->statuscode = add_action($this->database, $note, $_POST['actionid'], $_POST['remark']);
336          $this->display_notification($note);
337          echo "<hr>";
338          $this->notification_form($note);
339       }
340       else
341       {
342          echo "<h1>Insufficient input to create page</h1>";
343          phpinfo();
344       }
345    }
346 }
347
348 $notif_page = new notification_page("Gnucomo Notifications");
349
350 $notif_page->Showpage();
351
352 ?>