<?php
/**
 * Content Templater List View Template
 *
 * @package     Content Templater
 * @version     1.5.4
 *
 * @author      Peter van Westen <peter@nonumber.nl>
 * @link        http://www.nonumber.nl
 * @copyright   Copyright (C) 2010 NoNumber! All Rights Reserved
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 */

// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

// Import html tooltips
JHTML::_( 'behavior.tooltip' );

// Ordering allowed ?
$ordering = ( $this->lists['order'] == 'ordering' );

if ( !file_exists( JPATH_SITE.DS.'plugins'.DS.'editors-xtd'.DS.'contenttemplater.php' ) ) {
	$msg = JText::_( 'The editor button plugin is missing' );
	$message_set = 0;
	$messageQueue = $mainframe->getMessageQueue();
	foreach ( $messageQueue as $queue_message ) {
		if ( $queue_message['type'] == 'notice' && $queue_message['message'] == $msg ) {
			$message_set = 1;
			break;
		}
	}
	if ( !$message_set ) {
		$mainframe->enqueueMessage( $msg, 'notice' );
	}
}

function groupLftArrayFromGidList( $gid_list = '' )
{
	$acl =& JFactory::getACL();

	$lft_array = array();
	$gid_array = explode ( ',', $gid_list );
	foreach ( $gid_array as $gid ) {
		$lft_array[] = userLft( $gid );
	}
	sort( $lft_array );
	return $lft_array ;
}
function userLft( $gid = 0 )
{
	$acl =& JFactory::getACL();

	$group_data = $acl->get_group_data( $gid );
	$lft = $group_data[4];

	return $lft ;
}

/**
* Submit the admin form
* small hack: let task decides where it comes
*/
$script = "
	function submitform( pressbutton )
	{
		var form = document.adminForm;
		if ( pressbutton ) {
			form.task.value=pressbutton;
		}

		if (
			( pressbutton=='add' )||( pressbutton=='copy' )||( pressbutton=='edit' )||( pressbutton=='remove' )
			||( pressbutton=='publish' )||( pressbutton=='unpublish' )
			||( pressbutton=='orderdown' )||( pressbutton=='orderup' )||( pressbutton=='saveorder' )
			||( pressbutton=='export' )
		) {
			form.controller.value='item';
		} else {
			form.controller.value='list';
		}
		try {
			form.onsubmit();
			}
		catch( e ) {}

		form.submit();
	}
";
$doc =& JFactory::getDocument();
$doc->addScriptDeclaration( $script );

echo JText::_( 'List information' );
?>
<p></p>
<form action="<?php echo $this->request_url; ?>" method="post" name="adminForm" >
	<table>
		<tr>
			<td align="left" width="100%">
				<?php echo JText::_( 'Filter' ); ?>:
				<input type="text" name="search" id="search" value="<?php echo $this->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.getElementById('filter_state').value='';this.form.submit();"><?php echo JText::_( 'Reset' ); ?></button>
			</td>
			<td nowrap="nowrap">
				<?php echo $this->lists['state']; ?>
			</td>
		</tr>
	</table>

	<div id="editcell">
		<table class="adminlist">
		<thead>
			<tr>
				<th width="5">
					<?php echo JText::_( 'NUM' ); ?>
				</th>
				<th width="20">
					<input type="checkbox" name="toggle" value="" onclick="checkAll(<?php echo count( $this->items ); ?>);" />
				</th>
				<th class="title">
					<?php echo JHTML::_( 'grid.sort', 'Name', 'name', $this->lists['order_Dir'], $this->lists['order'] ); ?>
				</th>
				<th class="title">
					<?php echo JHTML::_( 'grid.sort', 'Description', 'description', $this->lists['order_Dir'], $this->lists['order'] ); ?>
				</th>
				<th width="5%" nowrap="nowrap">
					<?php echo JText::_( 'Frontend' ); ?>
				</th>
				<th width="5%" nowrap="nowrap">
					<?php echo JText::_( 'Auto Load' ); ?>
				</th>
				<th width="5%" nowrap="nowrap">
					<?php echo JHTML::_( 'grid.sort', 'Published', 'published', $this->lists['order_Dir'], $this->lists['order'] ); ?>
				</th>
				<th width="80" nowrap="nowrap">
					<?php echo JHTML::_( 'grid.sort', 'Order', 'ordering', $this->lists['order_Dir'], $this->lists['order'] ); ?>
				</th>
				<th width="1%">
					<?php echo JHTML::_( 'grid.order', $this->items ); ?>
				</th>
				<th width="5%" nowrap="nowrap">
					<?php echo JHTML::_( 'grid.sort', 'ID', 'id', $this->lists['order_Dir'], $this->lists['order'] ); ?>
				</th>
			</tr>
		</thead>
		<tfoot>
			<tr>
				<td colspan="12">
					<?php echo $this->pagination->getListFooter(); ?>
				</td>
			</tr>
		</tfoot>
		<?php

		$user =& JFactory::getUser();

		$rownr = 0;
		$item_count = count( $this->items );
		for ( $i=0; $i < $item_count; $i++ )
		{
			$item =& $this->items[$i];

			$enabled = 1;
			// hide items that are only for higher backend group levels
			if ( $item->button_assignto_usergrouplevels ) {
				if ( $item->button_assignto_usergrouplevels_selection ) {
					$usergrouplevels  = groupLftArrayFromGidList( $item->button_assignto_usergrouplevels_selection );
					// if first usergrouplevel is a backend userlevel and higher than user
					if ( $usergrouplevels[0] > 13 && userLft( $user->get( 'gid' ) ) < $usergrouplevels[0] ) {
						$enabled = 0;
					}
				}
			}

			if ( $enabled ) {
				$link 	= JRoute::_( 'index.php?option=com_contenttemplater&controller=item&task=edit&cid[]='. $item->id );

				$checked 	= JHTML::_( 'grid.checkedout', $item, $i ,'id' );
				$published 	= JHTML::_( 'grid.published', $item, $i );

				if ( $item->button_enable_in_frontend ) {
					$enable_in_frontend 	= '<img src="components/com_contenttemplater/images/tick.png" border="0" alt="'.JText::_( 'Enable in Frontend' ).'" title="'.JText::_( 'Enable in Frontend' ).'" />';
				} else {
					$enable_in_frontend 	= '<img src="components/com_contenttemplater/images/publish_x.png" border="0" alt="'.JText::_( 'Not' ).' '.JText::_( 'Enable in Frontend' ).'" title="'.JText::_( 'Not' ).' '.JText::_( 'Enable in Frontend' ).'" />';
				}

				if ( $item->load_enabled ) {
					$is_default 	= '<img src="components/com_contenttemplater/images/tick.png" border="0" alt="'.JText::_( 'Default' ).'" title="'.JText::_( 'Default' ).'" />';
				} else {
					$is_default 	= '<img src="components/com_contenttemplater/images/publish_x.png" border="0" alt="'.JText::_( 'Not' ).' '.JText::_( 'Default' ).'" title="'.JText::_( 'Not' ).' '.JText::_( 'Default' ).'" />';
				}
				?>
				<tr class="<?php echo 'row'.$rownr; ?>">
					<td>
						<?php echo $this->pagination->getRowOffset( $i ); ?>
					</td>
					<td>
						<?php echo $checked; ?>
					</td>
					<td>
						<?php
						if ( JTable::isCheckedOut( $this->user->get('id' ), $item->checked_out ) ) {
							echo $item->name;
						} else {
						?>
							<a href="<?php echo $link; ?>" title="<?php echo JText::_( 'Edit' ); ?>">
								<?php echo $item->name; ?></a>
						<?php
						}
						?>
					</td>
					<td>
						<?php
							$description = explode( '---', str_replace( "\n", '<br />', $item->description ) );
							echo $description['0'];
						?>
					</td>
					<td align="center">
						<?php echo $enable_in_frontend;?>
					</td>
					<td align="center">
						<?php echo $is_default;?>
					</td>
					<td align="center">
						<?php echo $published;?>
					</td>
					<td class="order" colspan="2">
						<span><?php echo $this->pagination->orderUpIcon( $i, true, $this->lists['order_Dir']!='desc'?'orderup':'orderdown', 'Move Up', $ordering ); ?></span>
						<span><?php echo $this->pagination->orderDownIcon( $i, $item_count, true, $this->lists['order_Dir']!='desc'?'orderdown':'orderup', 'Move Down', $ordering ); ?></span>
						<?php $disabled = $ordering ? '' : 'disabled="disabled"'; ?>
						<input type="text" name="order[]" size="5" value="<?php echo $item->ordering; ?>" <?php echo $disabled; ?> class="text_area" style="text-align: center" />
					</td>
					<td align="center">
						<?php echo $item->id; ?>
					</td>
				</tr>
				<?php
				$rownr = 1 - $rownr;
			}
		}
		?>
		</table>
	</div>

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