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