<?php
/*
   The Index software may be freely distributed.
   See license.txt for details.
*/
define('ROOT', './');
include(ROOT . 'text.inc');
include(ROOT . 'counts/count.inc');
include(ROOT . 'layout.inc');
include(ROOT . 'list/list.inc');
include(ROOT . 'notes/notes.inc');

function MakeTitle($sname, $sauth, $scat, $surl, $sdesc, $sany, $snotes, $sort)
{
    global $categories;
    global $PHP_SELF;
    ?>
        <h2>Results</h2>
        <p><a href="<?php print $PHP_SELF?>">Back to the full list</a></p>
        <p>
    <?php

    if (!($sname == "" and $sauth == "" and $scat == ""
        and $surl == "" and $sdesc == "" and $sany == "" and $snotes == ""))
    {
        ?>
        Searched for
        <?php
        $and = false;
        if ($sname != "") 
        {
            print "name <b>$sname</b>";
            $and = true;
        }
        if ($surl != "") 
        {
            if ($and) 
                print " and ";
            print "URL <b>$surl</b>";
            $and = true;
        }
        if ($sauth != "") 
        {
            if ($and) 
                print " and ";
            print "author <b>$sauth</b>";
            $and = true;
        }
        if ($scat != "") 
        {
            if ($and) 
                print " and ";
            print "category <b>" . $categories[$scat]['name'] . "</b>";
            $and = true;
        }
        if ($sdesc != "") 
        {
            if ($and) 
                print " and ";
            print "description <b>$sdesc</b>";
            $and = true;
        }
        if ($sany != "") 
        {
            if ($and) 
                print " and ";
            print "<b>$sany</b> anywhere in item";
            $and = true;
        }
        if ($snotes != "") 
        {
            if ($and) 
                print " and ";
            print "notes containing <b>$snotes</b>";
            $and = true;
        }
        print ".";
    }
    if ($sort != "") 
    {
        ?>
        Sorted by <b>
        <?php
        switch ($sort)
        {
            case "name":
                print "name";
                break;
            case "author":
                print "author";
                break;
            case "date":
                print "date changed";
                break;
            case "url":
                print "URL";
                break;
            case "count":
                print "popularity";
                break;
        }
        print "</b>.";
    }
    ?>
    </p>
    <?php
}                


ReadList();
ReadNotes();

start_page();
if ($sname == "" and $sauth == "" and $scat == ""
        and $surl == "" and $sdesc == "" and $sany == "" and $snotes == ""
        and $sort == "")
{
    // No filters, no sort: Print intro
    print $introduction;
}

if ($sort == "")
    SortList($combilist, 'category');
else
    SortList($combilist, $sort);

$sname = submit2text($sname);
$sauth = submit2text($sauth);
$surl = submit2text($surl);
$sdesc = submit2text($sdesc);
$sany = submit2text($sany);
$snotes = submit2text($snotes);

if ($sname)
    FilterList('name', $sname);
if ($sauth)
    FilterList('author', $sauth);
if ($surl)
    FilterList('url', $surl);
if ($scat)
    FilterList('category', $scat);
if ($sdesc)
    FilterList('desc', $sdesc);
if ($sany)
    FilterList('', $sany);
if ($snotes)
    FilterList('notes', $snotes);
$category = "";
$inlist = false;
reset($combilist);    
while (list($key, $value) = each($combilist))
{
    if (!Excluded($key))
    {
        if ($sname == "" and $sauth == "" and $scat == ""
                and $surl == "" and $sdesc == "" and $sany == ""
                and $snotes == ""
                and $sort == "")
        {
            // No filters, no sort
            ereg("([^,]*)", $value['category'], $reg);
            $maincat = $reg[1];
            if ($maincat != $category)
            {
                // New category
                $category = $maincat;
                if ($inlist)
                {
                    end_itemlist();
                    $inlist = false;
                }
                category($maincat);
            }
        }
        else
        {
            //Filters or sort
            if (!$inlist)
            {
                MakeTitle($sname, $sauth, $scat, $surl, $sdesc, $sany, $snotes, $sort);
            }
        }
        if (!$inlist)
        {
            start_itemlist();
            $inlist = true;
        }
        start_item();
        item($key, $value, 0, 1);
        end_item();
    }
}
if ($inlist)
    end_itemlist();
else // None found
{
    MakeTitle($sname, $sauth, $scat, $surl, $sdesc, $sany, $snotes, $sort);
    ?><blockquote><b>None found.</b></blockquote><?php
}
end_page();
?>