All web interface pages use the page class.
[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.3 $
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.3  2003-02-13 09:01:29  arjen
31    All web interface pages use the page class.
32
33    Revision 1.2  2003/02/05 09:47:39  arjen
34    Display the difference of all package class parameters for two objects
35
36 ******************************/
37
38 // RCSID = "$Id: parameter_compare.php,v 1.3 2003-02-13 09:01:29 arjen Exp $";
39
40 ini_set('include_path', '.:./classes:../phpclasses');
41
42 require_once('page.class.php');
43
44
45 /*  Returns an associative array with all properties of a parameter (pid) */
46
47 function param_properties($db, $pid)
48 {
49    $properties = NULL;
50    $r = pg_exec($db, "SELECT name, value FROM property WHERE paramid='" . $pid . "'");
51    for ($p = 0; $p < pg_numrows($r); $p++)
52    {
53       $prop = pg_fetch_object($r, $p);
54       $properties[$prop->name] = $prop->value;
55    }
56    return $properties;
57 }
58
59 /*  Return true if both associative arrays are identical */
60
61 function property_compare($prop, $comp)
62 {
63    $equal = true;
64
65    if (empty($prop) || empty($comp))
66    {
67       $equal = empty($prop) && empty($comp);
68    }
69    else
70    {
71       reset($comp);
72       foreach ($prop as $name => $val)
73       {
74          if ($equal)
75          {
76             $to_compare = each($comp);
77             $equal = $to_compare[0] == $name && $to_compare[1] == $val;
78          }
79       }
80    }
81    return $equal;
82 }
83
84 /*   Display a parameter in two adjecent table cells  */
85
86 function display_parameter($name, $properties, $css_class = "")
87 {
88    echo "<td";
89    if ($css_class != "")
90    {
91       echo " class='$css_class'";
92    }
93    echo ">";
94    echo $name;
95    echo "</td><td";
96    if ($css_class != "")
97    {
98       echo " class='$css_class'";
99    }
100    echo ">";
101    foreach ($properties as $p_name => $p_value)
102    {
103       echo " $p_name=$p_value";
104    }
105    echo "</td>";
106 }
107
108 class param_diff extends page
109 {
110
111    function Body()
112    {
113    if (!empty($_POST['oid']))
114    {
115       $res = pg_exec($this->database, "SELECT objectid, objectname FROM object WHERE objectid=" . $_POST['oid']);
116       $obj = pg_fetch_object($res, 0);
117       echo "<h1>" . $_POST['class'] . " parameters for " . $obj->objectname;
118       $res = pg_exec($this->database, "SELECT objectid, objectname FROM object WHERE objectid=" . $_POST['compare_to']);
119       $cmp_obj = pg_fetch_object($res, 0);
120       echo " compared to " . $cmp_obj->objectname . "</h1><hr>";
121
122       $res = pg_exec($this->database, "SELECT objectid, paramid, name FROM parameter "
123                      . "WHERE objectid=" . $obj->objectid . " OR objectid=" . $cmp_obj->objectid
124                      . " AND class='" . $_POST['class'] . "' ORDER BY name, objectid");
125       
126       echo "<table>\n";
127       echo "<tr><th colspan='2'>" . $obj->objectname . "</th>";
128       echo "<th colspan='2'>" . $cmp_obj->objectname . "</th></tr>\n";
129       echo "<tr><th>Name</th><th>Properties</th><th>Name</th><th>Propterties</th></tr>\n";
130       $row = 0;
131       while ($row < pg_numrows($res))
132       {
133          $par = pg_fetch_object($res, $row);
134          $nextpar = 'false';
135          if ($row + 1 < pg_numrows($res))
136          {
137             $nextpar = pg_fetch_object($res, $row + 1);
138          }
139          echo "<tr>";
140
141          if ($nextpar && $par->name == $nextpar->name)
142          {
143             /*  Both objects have this parameter */
144             $row++;
145
146             $pr = param_properties($this->database, $par->paramid);
147             $prnext = param_properties($this->database, $nextpar->paramid);
148             if (property_compare($pr, $prnext))
149             {
150                display_parameter($par->name, $pr);
151                display_parameter($nextpar->name, $prnext);
152             }
153             else
154             {
155                // We want the parameters of $obj on the left, so we need
156                // to swap the left and right sides if appropriate.
157
158                if ($par->objectid == $obj->objectid)
159                {
160                   display_parameter($par->name, $pr, "both");
161                   display_parameter($nextpar->name, $prnext, "both");
162                }
163                else
164                {
165                   display_parameter($nextpar->name, $prnext, "both");
166                   display_parameter($par->name, $pr, "both");
167                }
168             }
169          }
170          else
171          {
172             /*  Only one of the objects has this parameter */
173
174             $pr = param_properties($this->database, $par->paramid);
175
176             if ($par->objectid == $obj->objectid)
177             {
178                /*  Parameter belongs to the object on the left */
179
180                display_parameter($par->name, $pr, "left");
181                echo "<td>&nbsp;</td><td>&nbsp;";
182             }
183             else
184             {
185                /*  Parameter belongs to the object on the right */
186
187                echo "<td>";
188                echo "&nbsp;</td><td>&nbsp;</td>";
189                display_parameter($par->name, $pr, "right");
190             }
191          }
192          echo "</tr>\n";
193          $row++;
194       }
195       echo "</table>\n";
196    }
197    }
198 }
199
200 $page = new param_diff("Gnucomo Parameter Comparison");
201
202 $page->Showpage();
203
204 ?>