<?php
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.application.component.view');

class KipViewRules extends JView
{
	function display( $tpl = null )
	{
		switch($this->_layout){
			case 'form':
				$this->displayForm($tpl);
			break;
			default:
				$this->displayDefault($tpl);
		}
		
	}
	
	function displayDefault($tpl){
		global $mainframe, $option;

		$items		= &$this->get('Items');
		$pagination	= &$this->get('Pagination');
		$lists		= &$this->_getViewLists();
		$user		= &JFactory::getUser();


		$this->assignRef('user',		JFactory::getUser());
		$this->assignRef('lists',		$lists);
		$this->assignRef('items',		$items);
		$this->assignRef('pagination',	$pagination);
		$this->assignRef('option', $option);
		$this->assignRef('view', $this->getName());
		parent::display($tpl);
	}


	function &_getViewLists()
	{
		$filters		= &$this->get('Filters');

		$lists['state']	= JHTML::_('grid.state',  $filters['state']['value'], 'enabled', 'disabled' );
		$lists['order']	= $filters['order']['value'];
		$lists['order_Dir']	= $filters['order_Dir']['value'];
		$lists['search'] = $filters['search']['value'];
		$lists['client_id'] = $this->getClientIdList($filters['client_id']['value']);
		return $lists;
	}


	function displayForm($tpl){
		global $mainframe, $option;

		$user 	=& JFactory::getUser();

		$cid 	= JRequest::getVar( 'cid', array(0), '', 'array' );
		$uid 	= (int) @$cid[0];
		$edit=JRequest::getVar( 'edit', true );

		$item		= &$this->get('Item');
		
		$db = & JFactory::getDBO();
		$db->setQuery('
			SELECT CONCAT(name,\'(\',username,\')\')
			FROM #__users
			WHERE id='.$item->userid.'
		');
		$item->username = $db->loadResult();
		
		
		if ($item->isCheckedOut( $user->get('id') )) {
			$msg = JText::sprintf( 'DESCBEINGEDITTED', JText::_( 'The item' ), $item->title );
			$this->setRedirect( 'index.php?option='. $option.'&view='.$this->getName, $msg );
		}

		if ($item->id == 0)
			$item->published = 1;

		$lists['published'] 		= JHTML::_('select.booleanlist',  'published', 'class="inputbox"', $item->published , 'enabled', 'disabled');
		$lists['client_id'] = $this->getClientIdList($item->client_id, false);
		
		$this->assignRef('item',	$item);
		$this->assignRef('lists', $lists);
		$this->assignRef('option', $option);
		$this->assignRef('view', $this->getName());
		parent::display($tpl);
	}

	function & getClientIdList($client_id = 0, $isFilter = true){
		$db = & JFactory::getDBO();
		if($isFilter)
			$clientid[] = JHTML::_('select.option', '-1', '- '.JText::_('Select Type').' -');
		$clientid[] = JHTML::_('select.option', '0', JText::_('Frontend'));
		$clientid[] = JHTML::_('select.option', '1', JText::_('Backend'));
		$clientid[] = JHTML::_('select.option', '2', JText::_('User'));
		
		return JHTML::_('select.genericlist',  $clientid, ($isFilter ? 'filter_' : '').'client_id', 'class="inputbox" size="1"'.($isFilter ? ' onchange="document.adminForm.submit( );"' : 'onchange="if(this.value==2) {$(\'user_outer\').setStyle(\'display\',\'\')} else $(\'user_outer\').setStyle(\'display\',\'none\')"'), 'value', 'text', $client_id);
	}
}
?>