ea2e25847ece2918efdd6670e1ddb36a75fe9032
[gnucomo.git] / src / web / parameter_compare.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      : parameter_compare.php
11 **      SYSTEM NAME    : Gnucomo - Gnu Computer Monitoring
12 **      VERSION NUMBER : $Revision: 1.2 $
13 **
14 **  DESCRIPTION      : 
15 **
16 **  EXPORTED OBJECTS : 
17 **  LOCAL    OBJECTS : 
18 **  MODULES  USED    :
19 ***************************************************************************
20 **  ADMINISTRATIVE INFORMATION *
21 ********************************
22 **      ORIGINAL AUTHOR : Arjen Baart - arjen@andromeda.nl
23 **      CREATION DATE   : Dec 04, 2002
24 **      LAST UPDATE     : Feb 03, 2003
25 **      MODIFICATIONS   : 
26 **************************************************************************/
27
28 /*****************************
29    $Log: parameter_compare.php,v $
30    Revision 1.2  2003-02-05 09:47:39  arjen
31    Display the difference of all package class parameters for two objects
32
33 ******************************/
34
35 // RCSID = "$Id: parameter_compare.php,v 1.2 2003-02-05 09:47:39 arjen Exp $";
36
37 session_start();
38 require_once('classes/gnucomo_config.php');
39 ?>
40
41 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
42 <html>
43 <head>
44 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
45 <link rel='stylesheet' href='gnucomo.css' type='text/css'>
46 <title>Parameter comparison</title>
47
48
49 </head>
50 <body>
51 <?php
52
53 /*  Returns an associative array with all properties of a parameter (pid) */
54
55 function param_properties($db, $pid)
56 {
57    $properties = NULL;
58    $r = pg_exec($db, "SELECT name, value FROM property WHERE paramid='" . $pid . "'");
59    for ($p = 0; $p < pg_numrows($r); $p++)
60    {
61       $prop = pg_fetch_object($r, $p);
62       $properties[$prop->name] = $prop->value;
63    }
64    return $properties;
65 }
66
67 /*  Return true if both associative arrays are identical */
68
69 function property_compare($prop, $comp)
70 {
71    $equal = true;
72
73    if (empty($prop) || empty($comp))
74    {
75       $equal = empty($prop) && empty($comp);
76    }
77    else
78    {
79       reset($comp);
80       foreach ($prop as $name => $val)
81       {
82          if ($equal)
83          {
84             $to_compare = each($comp);
85             $equal = $to_compare[0] == $name && $to_compare[1] == $val;
86          }
87       }
88    }
89    return $equal;
90 }
91
92 /*   Display a parameter in two adjecent table cells  */
93
94 function display_parameter($name, $properties, $css_class = "")
95 {
96    echo "<td";
97    if ($css_class != "")
98    {
99       echo " class='$css_class'";
100    }
101    echo ">";
102    echo $name;
103    echo "</td><td";
104    if ($css_class != "")
105    {
106       echo " class='$css_class'";
107    }
108    echo ">";
109    foreach ($properties as $p_name => $p_value)
110    {
111       echo " $p_name=$p_value";
112    }
113    echo "</td>";
114 }
115
116 if (empty($_SESSION['username']))
117 {
118    echo "Please log in first.";
119 }
120 else
121 {
122
123    $config = new gnucomo_config;
124
125    $config->read("gnucomo");
126
127    //  Connect to the database
128    $conn = pg_connect($config->Database($_SESSION['username'], $_SESSION['password']));
129
130    if (!empty($_POST['oid']))
131    {
132       $res = pg_exec("SELECT objectid, objectname FROM object WHERE objectid=" . $_POST['oid']);
133       $obj = pg_fetch_object($res, 0);
134       echo "<h1>" . $_POST['class'] . " parameters for " . $obj->objectname;
135       $res = pg_exec("SELECT objectid, objectname FROM object WHERE objectid=" . $_POST['compare_to']);
136       $cmp_obj = pg_fetch_object($res, 0);
137       echo " compared to " . $cmp_obj->objectname . "</h1><hr>";
138
139       $res = pg_exec("SELECT objectid, paramid, name FROM parameter "
140                      . "WHERE objectid=" . $obj->objectid . " OR objectid=" . $cmp_obj->objectid
141                      . " AND class='" . $_POST['class'] . "' ORDER BY name, objectid");
142       
143      ?>
144      <?php
145       echo "<table>\n";
146       echo "<tr><th colspan='2'>" . $obj->objectname . "</th>";
147       echo "<th colspan='2'>" . $cmp_obj->objectname . "</th></tr>\n";
148       echo "<tr><th>Name</th><th>Properties</th><th>Name</th><th>Propterties</th></tr>\n";
149       $row = 0;
150       while ($row < pg_numrows($res))
151       {
152          $par = pg_fetch_object($res, $row);
153          $nextpar = 'false';
154          if ($row + 1 < pg_numrows($res))
155          {
156             $nextpar = pg_fetch_object($res, $row + 1);
157          }
158          echo "<tr>";
159
160          if ($nextpar && $par->name == $nextpar->name)
161          {
162             /*  Both objects have this parameter */
163             $row++;
164
165             $pr = param_properties($conn, $par->paramid);
166             $prnext = param_properties($conn, $nextpar->paramid);
167             if (property_compare($pr, $prnext))
168             {
169                display_parameter($par->name, $pr);
170                display_parameter($nextpar->name, $prnext);
171             }
172             else
173             {
174                // We want the parameters of $obj on the left, so we need
175                // to swap the left and right sides if appropriate.
176
177                if ($par->objectid == $obj->objectid)
178                {
179                   display_parameter($par->name, $pr, "both");
180                   display_parameter($nextpar->name, $prnext, "both");
181                }
182                else
183                {
184                   display_parameter($nextpar->name, $prnext, "both");
185                   display_parameter($par->name, $pr, "both");
186                }
187             }
188          }
189          else
190          {
191             /*  Only one of the objects has this parameter */
192
193             $pr = param_properties($conn, $par->paramid);
194
195             if ($par->objectid == $obj->objectid)
196             {
197                /*  Parameter belongs to the object on the left */
198
199                display_parameter($par->name, $pr, "left");
200                echo "<td>&nbsp;</td><td>&nbsp;";
201             }
202             else
203             {
204                /*  Parameter belongs to the object on the right */
205
206                echo "<td>";
207                echo "&nbsp;</td><td>&nbsp;</td>";
208                display_parameter($par->name, $pr, "right");
209             }
210          }
211          echo "</tr>\n";
212          $row++;
213       }
214       echo "</table>\n";
215    }
216 }
217 ?>
218
219 </body>
220 </html>