<?php
/*
    SEF Advance extension guide
    http://www.sakic.net/support/sef_advance_extensions/
*/

class sef_alfcontact {

    function create ($string) {
        global $database;
        if (empty($database)) {
            // Joomla! 1.5 native
            $database =& JFactory::getDBO();
        }
        // $string == "index.php?option=com_alfcontact&Itemid=$Itemid&catid=$catid&id=$id"
        $sefstring = '';
        if (eregi('&amp;catid=',$string)) {
            $temp = explode('&amp;catid=', $string);
            $temp = explode('&', $temp[1]);
            $catid = $temp[0];
            $query = "SELECT name FROM #__alfcontact_categories "
                ."WHERE id='$catid'";
            $database->setQuery($query);
            $category = $database->loadResult();
            $sefstring .= sefencode($category).'/';
        }
        if (eregi('&amp;id=',$string)) {
            $temp = explode('&amp;id=', $string);
            $temp = explode('&', $temp[1]);
            $id = $temp[0];
            $query = "SELECT name FROM #__alfcontact_entries WHERE id='$id'";
            $database->setQuery($query);
            $entry = $database->loadResult();
            $sefstring .= sefencode($entry).'/';
        }
        // $sefstring == "$category/$entry/"
        return $sefstring;
    }

    function revert ($url_array, $pos) {
        global $database;
        if (empty($database)) {
            // Joomla! 1.5 native
            $database =& JFactory::getDBO();
        }
        $QUERY_STRING = '';
        if (isset($url_array[$pos+2]) && $url_array[$pos+2]!='') {
            // .../alfcontact/$category/
            $category = sefdecode($url_array[$pos+2]);
            $query = "SELECT id FROM #__alfcontact_categories "
                ."WHERE name='$category'";
            $database->setQuery($query);
            $catid = $database->loadResult();
            $_GET['catid'] = $_REQUEST['catid'] = $catid;
            $QUERY_STRING .= "&catid=$catid";
        }
        if (isset($url_array[$pos+3]) && $url_array[$pos+3]!='') {
            // .../alfcontact/$category/$entry/
            $entry = sefdecode($url_array[$pos+3]);
            $query = "SELECT id FROM #__alfcontact_entries "
                ."WHERE name='$entry' AND catid='$catid'";
            $database->setQuery($query);
            $id = $database->loadResult();
            $_GET['id'] = $_REQUEST['id'] = $id;
            $QUERY_STRING .= "&id=$id";
        }
        // $QUERY_STRING == "catid=$catid&id=$id";
        return $QUERY_STRING;
    }

}
?> 