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

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

class KipViewUsers extends JView
{
	function display()
	{
		global $mainframe, $option;

		// Initialize variables
		$db			= &JFactory::getDBO();
		$nullDate	= $db->getNullDate();

		$document	= & JFactory::getDocument();
		$document->setTitle(JText::_('User Selection'));

		JHTML::_('behavior.modal');

		$template = $mainframe->getTemplate();
		$document->addStyleSheet("templates/$template/css/general.css");

		$limitstart = JRequest::getVar('limitstart', '0', '', 'int');

		$lists = $this->_getLists();

		//Ordering allowed ?
		$ordering = ($lists['order'] == 'section_name' && $lists['order_Dir'] == 'ASC');

		$rows = &$this->get('List');
		$page = &$this->get('Pagination');
		JHTML::_('behavior.tooltip');
		?>
		<form action="index.php?option=<?php echo $option ?>&amp;view=users&amp;tmpl=component" method="post" name="adminForm">

			<table>
				<tr>
					<td width="100%">
						<?php echo JText::_( 'Filter' ); ?>:
						<input type="text" name="search" id="search" value="<?php echo $lists['search'];?>" class="text_area" onchange="document.adminForm.submit();" />
						<button onclick="this.form.submit();"><?php echo JText::_( 'Go' ); ?></button>
						<button onclick="document.getElementById('search').value='';this.form.submit();"><?php echo JText::_( 'Reset' ); ?></button>
					</td>
					<td nowrap="nowrap">
						<?php
						echo $lists['type'];
						echo $lists['logged'];
						?>
					</td>
				</tr>
			</table>

			<table class="adminlist" cellpadding="1">
				<thead>
					<tr>
						<th width="2%" class="title">
							<?php echo JText::_( 'NUM' ); ?>
						</th>
						<th class="title">
							<?php echo JHTML::_('grid.sort',   'Name', 'c.name', @$this->lists['order_Dir'], @$this->lists['order'] ); ?>
						</th>
						<th width="15%" class="title" >
							<?php echo JHTML::_('grid.sort',   'Username', 'c.username', @$this->lists['order_Dir'], @$this->lists['order'] ); ?>
						</th>
						<th width="5%" class="title" nowrap="nowrap">
							<?php echo JText::_( 'Logged In' ); ?>
						</th>
						<th width="5%" class="title" nowrap="nowrap">
							<?php echo JHTML::_('grid.sort',   'Enabled', 'c.block', @$this->lists['order_Dir'], @$this->lists['order'] ); ?>
						</th>
						<th width="15%" class="title">
							<?php echo JHTML::_('grid.sort',   'Group', 'groupname', @$this->lists['order_Dir'], @$this->lists['order'] ); ?>
						</th>
						<th width="15%" class="title">
							<?php echo JHTML::_('grid.sort',   'E-Mail', 'c.email', @$this->lists['order_Dir'], @$this->lists['order'] ); ?>
						</th>
						<th width="10%" class="title">
							<?php echo JHTML::_('grid.sort',   'Last Visit', 'c.lastvisitDate', @$this->lists['order_Dir'], @$this->lists['order'] ); ?>
						</th>
						<th width="1%" class="title" nowrap="nowrap">
							<?php echo JHTML::_('grid.sort',   'ID', 'c.id', @$this->lists['order_Dir'], @$this->lists['order'] ); ?>
						</th>
					</tr>
				</thead>
				<tfoot>
					<tr>
						<td colspan="10">
							<?php echo $page->getListFooter(); ?>
						</td>
					</tr>
				</tfoot>
				<tbody>
				<?php
					$k = 0;
					for ($i=0, $n=count( $rows ); $i < $n; $i++)
					{
						$row 	=& $rows[$i];

						$img 	= $row->block ? 'publish_x.png' : 'tick.png';
						$task 	= $row->block ? 'unblock' : 'block';
						$alt 	= $row->block ? JText::_( 'Enabled' ) : JText::_( 'Blocked' );

						if ($row->lastvisitDate == "0000-00-00 00:00:00") {
							$lvisit = JText::_( 'Never' );
						} else {
							$lvisit	= JHTML::_('date', $row->lastvisitDate, '%Y-%m-%d %H:%M:%S');
						}
					?>
					<tr class="<?php echo "row$k"; ?>">
						<td>
							<?php echo $i+1+$this->pagination->limitstart;?>
						</td>
						<td>
						<a href="#" style="cursor: pointer;" onclick="window.parent.jSelectUser('<?php echo $row->id; ?>', '<?php echo str_replace(array("'", "\""), array("\\'", ""),$row->name).'('.str_replace(array("'", "\""), array("\\'", ""),$row->username).')'; ?>');">
								<?php echo $row->name; ?></a>
						</td>
						<td>
							<?php echo $row->username; ?>
						</td>
						<td align="center">
							<?php echo $row->loggedin ? '<img src="images/tick.png" width="16" height="16" border="0" alt="" />': ''; ?>
						</td>
						<td align="center">
								<img src="images/<?php echo $img;?>" width="16" height="16" border="0" alt="<?php echo $alt; ?>" />
						</td>
						<td>
							<?php echo JText::_( $row->groupname ); ?>
						</td>
						<td>
							<a href="mailto:<?php echo $row->email; ?>">
								<?php echo $row->email; ?></a>
						</td>
						<td nowrap="nowrap">
							<?php echo $lvisit; ?>
						</td>
						<td>
							<?php echo $row->id; ?>
						</td>
					</tr>
					<?php
						$k = 1 - $k;
						}
					?>
				</tbody>
			</table>

		<input type="hidden" name="boxchecked" value="0" />
		<input type="hidden" name="filter_order" value="<?php echo $lists['order']; ?>" />
		<input type="hidden" name="filter_order_Dir" value="<?php echo $lists['order_Dir']; ?>" />
		</form>
		<?php
	}

	function _getLists()
	{
		global $mainframe;

		// Initialize variables
		$db		= &JFactory::getDBO();

		// Get some variables from the request
		//$sectionid			= JRequest::getVar( 'sectionid', -1, '', 'int' );
		//$redirect			= $sectionid;
		$option				= JRequest::getCmd( 'option' );
		$filter_order		= $mainframe->getUserStateFromRequest('kipelement.filter_order',		'filter_order',		'',	'cmd');
		$filter_order_Dir	= $mainframe->getUserStateFromRequest('kipelement.filter_order_Dir',	'filter_order_Dir',	'',	'word');
		$filter_state		= $mainframe->getUserStateFromRequest('kipelement.filter_state',		'filter_state',		'',	'word');
		$filter_type		= $mainframe->getUserStateFromRequest('kipelement.filter_type',				'filter_type',			0,	'string');
		$filter_logged		= $mainframe->getUserStateFromRequest('kipelement.filter_logged',		'filter_logged',	0,	'int');
		//$filter_sectionid	= $mainframe->getUserStateFromRequest('articleelement.filter_sectionid',	'filter_sectionid',	-1,	'int');
		$limit				= $mainframe->getUserStateFromRequest('global.list.limit',					'limit', $mainframe->getCfg('list_limit'), 'int');
		$limitstart			= $mainframe->getUserStateFromRequest('kipelement.limitstart',			'limitstart',		0,	'int');
		$search				= $mainframe->getUserStateFromRequest('kipelement.search',				'search',			'',	'string');
		$search				= JString::strtolower($search);

		// get list of Groups for dropdown filter
		$query = 'SELECT name AS value, name AS text'
			. ' FROM #__core_acl_aro_groups'
			. ' WHERE name != "ROOT"'
			. ' AND name != "USERS"'
		;
		$db->setQuery( $query );
		$types[] 		= JHTML::_('select.option',  '0', '- '. JText::_( 'Select Group' ) .' -' );
		foreach( $db->loadObjectList() as $obj )
		{
			$types[] = JHTML::_('select.option',  $obj->value, JText::_( $obj->text ) );
		}
		$lists['type'] 	= JHTML::_('select.genericlist',   $types, 'filter_type', 'class="inputbox" size="1" onchange="document.adminForm.submit( );"', 'value', 'text', $filter_type );

		// get list of Log Status for dropdown filter
		$logged[] = JHTML::_('select.option',  0, '- '. JText::_( 'Select Log Status' ) .' -');
		$logged[] = JHTML::_('select.option',  1, JText::_( 'Logged In' ) );
		$lists['logged'] = JHTML::_('select.genericlist',   $logged, 'filter_logged', 'class="inputbox" size="1" onchange="document.adminForm.submit( );"', 'value', 'text', $filter_logged );

		// table ordering
		$lists['order_Dir']	= $filter_order_Dir;
		$lists['order']		= $filter_order;

		// search filter
		$lists['search'] = $search;

		return $lists;
	}
}
