<?php 
/**
*
* @package SimpleMembership
* @copyright 2012 Andrey Kvasnevskiy-OrdaSoft(akbet@mail.ru);Rob de Cleen(rob@decleen.com)
* Homepage: http://www.ordasoft.com
* @version: 1.2
*
**/

defined('_JEXEC') or die;


function simplemembershipBuildRoute(&$query){ //what writting
    global $array_MediaLibraryBuildRoute;
    $array_MediaLibraryBuildRoute = $query;
    $segments = array();
    $db = JFactory::getDBO();

    

    if (isset($query['option']) && $query['option'] == 'com_simplemembership'){ //check component
        $segments[0] = (isset($query['Itemid'])) && ($query['Itemid']!=0) ? $query['Itemid'] : '0';

        if (isset($query['task'])){
           $segments[1] = $query['task'];
         }
   
   
        unset($query['task']);
        unset($query['view']);
    

        if (isset($query['Itemid'])){
            $query['Itemid'] = "";
            unset($query['Itemid']);
        }

		if (isset($query['tab'])){
            $segments[] = $query['tab'];
            unset($query['tab']);
        }
		if (isset($query['is_show_data'])){
            $segments[] = $query['is_show_data'];
            unset($query['is_show_data']);
        }
		
		if (isset($query['name'])){
            $segments[] = $query['name'];
            unset($query['name']);
        }
		
		if (isset($query['user'])){
            $segments[] = $query['user'];
            unset($query['user']);
        }

		if (isset($query['userId'])){
            $segments[] = $query['userId'];
            unset($query['userId']);
        }
		if (isset($query['viewtype'])){
            $segments[] = $query['viewtype'];
            unset($query['viewtype']);
        }	
		
    }
    unset($query);
    return $segments;
}


/**
 * Parse the segments of a URL.
**/
function simplemembershipParseRoute($segments){ //where send
    $jSite = new JSite();
    $db = JFactory::getDBO();
    $menu = $jSite -> getMenu();
    $vars = array();
    $count = count($segments);
    $vars['option'] = 'com_simplemembership';
/*	//Debug information
*	for($i=0;$i<$count;$i++)
*        echo "\"".$i."\"  \"".@$segments[$i]."\" ";
*/
   // print_r($segments);
    if ($count > 1){

        if (isset($segments[0]) && strlen($segments[0])>4) {
            $vars['task'] = $segments[0];
            
            if (isset($segments[1])) $vars['tab'] = $segments[1];
            if (isset($segments[2])) $vars['is_show_data'] = $segments[2];
            if (isset($segments[3])) $vars['name'] = $segments[3];
            if (isset($segments[4])) $vars['userId'] = $segments[4];
            
        } elseif (strlen($segments[0])<=4) {
            $vars['itemId'] = $segments[0];
            $vars['task'] = $segments[1];
            $vars['userId'] = $segments[2];
        }
        
       
	}
        else
        {
            $vars['task'] =$segments[0]; 
        }
    return $vars;
}
