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