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