<?php
 
lt_include( PLOG_CLASS_PATH."class/plugin/pluginbase.class.php" );
    
    /*
     * Plugin that offers features to return links within a
     *  given link category with extra html in between each link
     */
class PluginSortedLinks extends PluginBase
{
    function PluginSortedLinks($source = ""){
        $this->PluginBase($source);

	    $this->id = "sortedlinks";
        $this->version = "20070903";
        $this->author = "Jon Daley";
        $this->desc = "This plugin adds the ability to retrieve 
                 links from within a category, sorted, and then displayed";
    }

    function printHtml($categoryName, $sortSeparator, $html){
        lt_include( PLOG_CLASS_PATH."class/dao/mylinkscategories.class.php" );
        lt_include( PLOG_CLASS_PATH."class/dao/mylinks.class.php" );
        $lc = new MyLinksCategories();

            // get the category id for the given category name
        $categories = $lc->getMyLinksCategories($this->blogInfo->getId(),
                                                 MYLINKS_CATEGORIES_ALPHABETICAL_ORDER,
                                                 $categoryName, 1, 1);
        unset($lc);
        if(!count($categories)){
            return "Error - no category found";
        }

        $myLinks = new MyLinks();
        $links = $myLinks->getLinks($this->blogInfo->getId(), $categories[0]->getId());

        $sortedLinkNames = Array();
        $sortedLink = Array();
        foreach($links as $link){
            list($key, $value) = explode($sortSeparator, $link->getName(), 2);
            $sortedLinkNames[$key] = $value;
            $sortedLinks[$key] = $link->getUrl();
        }
        ksort($sortedLinkNames);

        $printed = 0;
        foreach($sortedLinkNames as $key => $name){
            if($printed == 0)
                $printed = 1;
            else
                print $html;

            print "<a href='".$sortedLinks[$key]."'>$name</a>";
        }
    }
}
?>
