r4045 Interface to renderSearchResults function was simplified: no more need to pass...
authorAlexey Andriyanov <alan@al-an.info>
Fri, 10 Dec 2010 09:55:27 +0000 (09:55 +0000)
committerAlexey Andriyanov <alan@al-an.info>
Fri, 10 Dec 2010 09:55:27 +0000 (09:55 +0000)
Possibility of dupes in search results eliminated by indexing by entity id
bugfix: create IPv4 network button, if pressed to open in a new window, was redirecting the parent window, too
bugfix: when searching for IP not belonging to any known network, the ugly assertion failed page was shown.
update: links to ports added to object search results
update: custom rearch results provided by users' plugins now supported

inc/interface.php:
 renderSearchResults: parameters were simplified, unknown IP bug was fixed, custom search results supported, links to ports added
 renderPortOIFCompatEditor: bug fixed with nested <input> into <a> (parent page redirection)
 printIPNetInfoTDs: idem
 renderPortIFCompatEditor: idem

inc/database.php:
 getIPv4PrefixSearchResult: indexing results by id, for simplifying duplicates management in search results
 getIPv4AddressSearchResult: idem
 getIPv6AddressSearchResult: idem
 getIPv4RSPoolSearchResult: idem
 getIPv4VServiceSearchResult: idem
 getFileSearchResult: idem
 getVLANSearchResult: idem
 getAccountSearchResult: idem, mergind code updated to reflect indexing changes

inc/functions.php:
 searchEntitiesByText: removed nhits and lasthit calculation and returning

ChangeLog
inc/database.php
inc/functions.php
inc/interface.php

index 001295c..e39ff8b 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -7,6 +7,10 @@
        bugfix: the feature of remembering last opened tab of realm page is now working
        update: UI: ports/IPs/nets highlighting enhancements
        bugfix: UI: pager in ipv4net shows appropriate page when IP is highlighted
+       bugfix: create IPv4 network button, if pressed to open in a new window, was redirecting the parent window, too
+       bugfix: when searching for IP not belonging to any known network, the ugly assertion failed page was shown.
+       update: links to ports added to object search results
+       update: custom rearch results provided by users' plugins now supported
 0.18.7
        bugfix: adjust 802.1Q command generation
        bugfix: fixed telnet session hanging in NX-OS4 connector
index 62e5f1f..a6cea6d 100644 (file)
@@ -1515,7 +1515,7 @@ function getIPv4PrefixSearchResult ($terms)
        );
        $ret = array();
        foreach ($byname as $row)
-               $ret[] = spotEntity ('ipv4net', $row['id']);
+               $ret[$row['id']] = spotEntity ('ipv4net', $row['id']);
        return $ret;
 }
 
@@ -1531,7 +1531,7 @@ function getIPv6PrefixSearchResult ($terms)
        );
        $ret = array();
        foreach ($byname as $row)
-               $ret[] = spotEntity ('ipv6net', $row['id']);
+               $ret[$row['id']] = spotEntity ('ipv6net', $row['id']);
        return $ret;
 }
 
@@ -1549,7 +1549,7 @@ function getIPv4AddressSearchResult ($terms)
        $result = usePreparedSelectBlade ($query, $qparams);
        $ret = array();
        while ($row = $result->fetch (PDO::FETCH_ASSOC))
-               $ret[] = $row;
+               $ret[$row['ip']] = $row;
        return $ret;
 }
 
@@ -1567,7 +1567,7 @@ function getIPv6AddressSearchResult ($terms)
        $result = usePreparedSelectBlade ($query, $qparams);
        $ret = array();
        while ($row = $result->fetch (PDO::FETCH_ASSOC))
-               $ret[] = $row;
+               $ret[$row['ip']] = $row;
        return $ret;
 }
 
@@ -1583,7 +1583,7 @@ function getIPv4RSPoolSearchResult ($terms)
        );
        $ret = array();
        foreach ($byname as $row)
-               $ret[] = spotEntity ('ipv4rspool', $row['id']);
+               $ret[$row['id']] = spotEntity ('ipv4rspool', $row['id']);
        return $ret;
 }
 
@@ -1599,7 +1599,7 @@ function getIPv4VServiceSearchResult ($terms)
        );
        $ret = array();
        foreach ($byname as $row)
-               $ret[] = spotEntity ('ipv4vs', $row['id']);
+               $ret[$row['id']] = spotEntity ('ipv4vs', $row['id']);
        return $ret;
 }
 
@@ -1621,18 +1621,13 @@ function getAccountSearchResult ($terms)
                $terms,
                'user_name'
        );
-       // Filter out dupes.
-       foreach ($byUsername as $res1)
-               foreach (array_keys ($byRealname) as $key2)
-                       if ($res1['user_id'] == $byRealname[$key2]['user_id'])
-                       {
-                               unset ($byRealname[$key2]);
-                               continue 2;
-                       }
-       $ret = array_merge ($byUsername, $byRealname);
-       // Set realm, so it's renderable.
-       foreach (array_keys ($ret) as $key)
-               $ret[$key]['realm'] = 'user';
+       // Merge it together, if duplicates persist, byUsername wins
+       foreach (array ($byRealname, $byUsername) as $array)
+               foreach ($array as $user)
+               {
+                       $user['realm'] = 'user';
+                       $ret[$user['user_id']] = $user;
+               }
        return $ret;
 }
 
@@ -1664,7 +1659,7 @@ function getFileSearchResult ($terms)
                        }
        $ret = array();
        foreach (array_merge ($byName, $byComment) as $row)
-               $ret[] = spotEntity ('file', $row['id']);
+               $ret[$row['id']] = spotEntity ('file', $row['id']);
        return $ret;
 }
 
@@ -1696,7 +1691,7 @@ function getRackSearchResult ($terms)
                        }
        $ret = array();
        foreach (array_merge ($byName, $byComment) as $row)
-               $ret[] = spotEntity ('rack', $row['id']);
+               $ret[$row['id']] = spotEntity ('rack', $row['id']);
        return $ret;
 }
 
@@ -1716,7 +1711,10 @@ function getVLANSearchResult ($terms)
                        1
                );
                foreach ($byID as $row)
-                       $ret[] = $row['domain_id'] . '-' . $row['vlan_id'];
+               {
+                       $vlan_ck = $row['domain_id'] . '-' . $row['vlan_id'];
+                       $ret[$vlan_ck] = $vlan_ck;
+               }
        }
        else
        {
@@ -1730,8 +1728,7 @@ function getVLANSearchResult ($terms)
                foreach ($byDescr as $row)
                {
                        $vlan_ck = $row['domain_id'] . '-' . $row['vlan_id'];
-                       if (!in_array ($vlan_ck, $ret))
-                               $ret[] = $vlan_ck;
+                       $ret[$vlan_ck] = $vlan_ck;
                }
        }
        return $ret;
index 66b5f64..b2d6ef2 100644 (file)
@@ -3870,143 +3870,77 @@ function questionMarks ($count = 0)
        return implode (', ', array_fill (0, $count, '?'));
 }
 
-// returns search results as an array with 'nhits', 'lasthit', 'summary' keys
+// returns search results as an array keyed with realm name
 // groups found entities by realms in 'summary'
 function searchEntitiesByText ($terms)
 {
-       $result = array
-       (
-               'nhits' => 0,
-               'lasthit' => NULL,
-               'summary' => array(),
-       );
-       $nhits = &$result['nhits'];
-       $lasthit = &$result['lasthit'];
-       $summary = &$result['summary'];
-       
+       $summary = array();
        $ipv6 = new IPv6Address;
+
        if (preg_match (RE_IP4_ADDR, $terms))
        // Search for IPv4 address.
        {
-               if (NULL !== getIPv4AddressNetworkId ($terms))
-               {
-                       $nhits++;
-                       $lasthit = 'ipv4addressbydq';
-                       $summary['ipv4addressbydq'][] = $terms;
-               }
+               if ($net_id = getIPv4AddressNetworkId ($terms))
+                       $summary['ipv4addressbydq'][$terms] = array ('net_id' => $net_id, 'ip' => $terms);
+               
        }
        elseif ($ipv6->parse ($terms))
        // Search for IPv6 address
        {
-               if (NULL !== $net_id = getIPv6AddressNetworkId ($ipv6))
-               {
-                       $nhits++;
-                       $lasthit = 'ipv6addressbydq';
-                       $summary['ipv6addressbydq'][] = array ('net_id' => $net_id, 'ip' => $ipv6);
-               }
+               if ($net_id = getIPv6AddressNetworkId ($ipv6))
+                       $summary['ipv6addressbydq'][$net_id] = array ('net_id' => $net_id, 'ip' => $ipv6);
        }
        elseif (preg_match (RE_IP4_NET, $terms))
        // Search for IPv4 network
        {
                list ($base, $len) = explode ('/', $terms);
-               if (NULL !== ($tmp = getIPv4AddressNetworkId ($base, $len + 1)))
-               {
-                       $nhits++;
-                       $lasthit = 'ipv4network';
-                       $summary['ipv4network'][] = spotEntity ('ipv4net', $tmp);
-               }
+               if (NULL !== ($net_id = getIPv4AddressNetworkId ($base, $len + 1)))
+                       $summary['ipv4network'][$net_id] = spotEntity('ipv4net', $net_id);
        }
        elseif (preg_match ('@(.*)/(\d+)$@', $terms, $matches) && $ipv6->parse ($matches[1]))
        // Search for IPv6 network
        {
-               if (NULL !== ($tmp = getIPv6AddressNetworkId ($ipv6, $matches[2] + 1)))
-               {
-                       $nhits++;
-                       $lasthit = 'ipv6network';
-                       $summary['ipv6network'][] = spotEntity ('ipv6net', $tmp);
-               }
+               if (NULL !== ($net_id = getIPv6AddressNetworkId ($ipv6, $matches[2] + 1)))
+                       $summary['ipv6network'][$net_id] = spotEntity('ipv6net', $net_id);
        }
        else
        // Search for objects, addresses, networks, virtual services and RS pools by their description.
        {
                $tmp = getObjectSearchResults ($terms);
                if (count ($tmp))
-               {
-                       $nhits += count ($tmp);
-                       $lasthit = 'object';
                        $summary['object'] = $tmp;
-               }
                $tmp = getIPv4AddressSearchResult ($terms);
                if (count ($tmp))
-               {
-                       $nhits += count ($tmp);
-                       $lasthit = 'ipv4addressbydescr';
                        $summary['ipv4addressbydescr'] = $tmp;
-               }
                $tmp = getIPv6AddressSearchResult ($terms);
                if (count ($tmp))
-               {
-                       $nhits += count ($tmp);
-                       $lasthit = 'ipv6addressbydescr';
                        $summary['ipv6addressbydescr'] = $tmp;
-               }
                $tmp = getIPv4PrefixSearchResult ($terms);
                if (count ($tmp))
-               {
-                       $nhits += count ($tmp);
-                       $lasthit = 'ipv4network';
                        $summary['ipv4network'] = $tmp;
-               }
                $tmp = getIPv6PrefixSearchResult ($terms);
                if (count ($tmp))
-               {
-                       $nhits += count ($tmp);
-                       $lasthit = 'ipv6network';
                        $summary['ipv6network'] = $tmp;
-               }
                $tmp = getIPv4RSPoolSearchResult ($terms);
                if (count ($tmp))
-               {
-                       $nhits += count ($tmp);
-                       $lasthit = 'ipv4rspool';
                        $summary['ipv4rspool'] = $tmp;
-               }
                $tmp = getIPv4VServiceSearchResult ($terms);
                if (count ($tmp))
-               {
-                       $nhits += count ($tmp);
-                       $lasthit = 'ipv4vs';
                        $summary['ipv4vs'] = $tmp;
-               }
                $tmp = getAccountSearchResult ($terms);
                if (count ($tmp))
-               {
-                       $nhits += count ($tmp);
-                       $lasthit = 'user';
                        $summary['user'] = $tmp;
-               }
                $tmp = getFileSearchResult ($terms);
                if (count ($tmp))
-               {
-                       $nhits += count ($tmp);
-                       $lasthit = 'file';
                        $summary['file'] = $tmp;
-               }
                $tmp = getRackSearchResult ($terms);
                if (count ($tmp))
-               {
-                       $nhits += count ($tmp);
-                       $lasthit = 'rack';
                        $summary['rack'] = $tmp;
-               }
-               if (count ($tmp = getVLANSearchResult ($terms)))
-               {
-                       $nhits += count ($tmp);
-                       $lasthit = 'vlan';
+               $tmp = getVLANSearchResult ($terms);
+               if (count ($tmp))
                        $summary['vlan'] = $tmp;
-               }
        }
-       return $result;
+       return $summary;
 }
 
 ?>
index 748c2e3..7ce269d 100644 (file)
@@ -3631,36 +3631,34 @@ function searchHandler () {
        renderSearchResults ($terms, $results);
 }
 
-function renderSearchResults ($terms, $results)
+function renderSearchResults ($terms, $summary)
 {
-       $nhits = &$results['nhits'];
-       $lasthit = &$results['lasthit'];
-       $summary = &$results['summary'];
-       
+       // calculate the number of found objects
+       $nhits = 0;
+       foreach ($summary as $realm => $list)
+               $nhits += count ($list);
+
        if ($nhits == 0)
                echo "<center><h2>Nothing found for '${terms}'</h2></center>";
        elseif ($nhits == 1)
        {
-               $record = current ($summary[$lasthit]);
-               switch ($lasthit)
+               $realm = key ($summary);
+               $record = current (current ($summary));
+               switch ($realm)
                {
                        case 'ipv4addressbydq':
-                               $parentnet = getIPv4AddressNetworkId ($record);
-                               if ($parentnet !== NULL)
-                                       echo "<script language='Javascript'>document.location='index.php?page=ipv4net&tab=default&id=${parentnet}&hl_ipv4_addr=${record}';//</script>";
-                               else
-                                       echo "<script language='Javascript'>document.location='index.php?page=ipaddress&ip=${record}';//</script>";
+                               if ($record['net_id'] !== NULL)
+                                       echo "<script language='Javascript'>document.location='index.php?page=ipv4net&tab=default&id=${record['net_id']}&hl_ipv4_addr=${record['ip']}';//</script>";
                                break;
                        case 'ipv6addressbydq':
                                $v6_ip_dq = $record['ip']->format();
-                               echo "<script language='Javascript'>document.location='index.php?page=ipv6net&tab=default&id=${record['net_id']}&hl_ipv6_addr=${v6_ip_dq}';//</script>";
+                               if ($record['net_id'] !== NULL)
+                                       echo "<script language='Javascript'>document.location='index.php?page=ipv6net&tab=default&id=${record['net_id']}&hl_ipv6_addr=${v6_ip_dq}';//</script>";
                                break;
                        case 'ipv4addressbydescr':
                                $parentnet = getIPv4AddressNetworkId ($record['ip']);
                                if ($parentnet !== NULL)
                                        echo "<script language='Javascript'>document.location='index.php?page=ipv4net&tab=default&id=${parentnet}&hl_ipv4_addr=${record['ip']}';//</script>";
-                               else
-                                       echo "<script language='Javascript'>document.location='index.php?page=ipaddress&ip=${record['ip']}';//</script>";
                                break;
                        case 'ipv6addressbydescr':
                                $v6_ip = new IPv6Address ($record['ip']);
@@ -3668,8 +3666,6 @@ function renderSearchResults ($terms, $results)
                                $parentnet = getIPv6AddressNetworkId ($v6_ip);
                                if ($parentnet !== NULL)
                                        echo "<script language='Javascript'>document.location='index.php?page=ipv4net&tab=default&id=${parentnet}&hl_ipv6_addr=${v6_ip_dq}';//</script>";
-                               else
-                                       echo "<script language='Javascript'>document.location='index.php?page=ipaddress&ip=${v6_ip_dq}';//</script>";
                                break;
                        case 'ipv4network':
                                echo "<script language='Javascript'>document.location='index.php?page=ipv4net";
@@ -3756,7 +3752,13 @@ function renderSearchResults ($terms, $results)
                                                                foreach ($object['ports'] as $port)
                                                                        if ($port['id'] == $port_id)
                                                                        {
-                                                                               echo "<tr><td>port ${port['name']}:</td>";
+                                                                               $port_href = '<a href="' . makeHref (array
+                                                                               (
+                                                                                       'page' => 'object',
+                                                                                       'object_id' => $object['id'],
+                                                                                       'hl_port_id' => $port_id
+                                                                               )) . '">port ' . $port['name'] . '</a>';
+                                                                               echo "<tr><td>${port_href}:</td>";
                                                                                echo "<td class=tdleft>${text}</td></tr>";
                                                                                break; // next reason
                                                                        }
@@ -3916,6 +3918,10 @@ function renderSearchResults ($terms, $results)
                                        echo '</table>';
                                        finishPortlet();
                                        break;
+                               default: // you can use that in your plugins to add some non-standard search results
+                                       startPortlet($where);
+                                       echo $what;
+                                       finishPortlet();
                        }
        }
 }
@@ -4085,7 +4091,7 @@ function renderPortOIFCompatEditor()
                }
                echo "<tr class=row_${order}><td>";
                echo '<a href="' . makeHrefProcess (array ('op' => 'del', 'type1' => $pair['type1'], 'type2' => $pair['type2'])) . '">';
-               printImageHREF ('delete', 'remove pair', TRUE);
+               printImageHREF ('delete', 'remove pair');
                echo "</a></td><td class=tdleft>${pair['type1name']}</td><td class=tdleft>${pair['type2name']}</td></tr>";
        }
        if (getConfigVar ('ADDNEW_AT_TOP') != 'yes')
@@ -6663,7 +6669,7 @@ function printIPNetInfoTDs ($netinfo, $decor = array())
                                'tab' => 'newrange',
                                'set-prefix' => $formatted,
                        )) . '">';
-                       printImageHREF ('knight', 'create network here', TRUE);
+                       printImageHREF ('knight', 'create network here');
                        echo '</a>';
                }
        }
@@ -7376,7 +7382,7 @@ function renderPortIFCompatEditor()
                }
                echo "<tr class=row_${order}><td>";
                echo '<a href="' . makeHrefProcess (array ('op' => 'del', 'iif_id' => $record['iif_id'], 'oif_id' => $record['oif_id'])) . '">';
-               printImageHREF ('delete', 'remove pair', TRUE);
+               printImageHREF ('delete', 'remove pair');
                echo "</a></td><td class=tdleft>${record['iif_name']}</td><td class=tdleft>${record['oif_name']}</td></tr>";
        }
        if (getConfigVar ('ADDNEW_AT_TOP') != 'yes')