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