<?php
/**
 * ------------------------------------------------------------------------
 * Plugin Ajax JA Content Type
 * ------------------------------------------------------------------------
 * Copyright (C) 2004-2018 J.O.O.M Solutions Co., Ltd. All Rights Reserved.
 * @license - GNU/GPL, http://www.gnu.org/licenses/gpl.html
 * Author: J.O.O.M Solutions Co., Ltd
 * Websites: http://www.joomlart.com - http://www.joomlancers.com
 * ------------------------------------------------------------------------
 */

defined('_JEXEC') or die;

use Joomla\CMS\Factory;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Language\Text;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Session\Session;
use Joomla\CMS\Language\Multilanguage;

if(version_compare(JVERSION, '4', 'lt')) {
	require_once JPATH_ROOT . '/components/com_contact/helpers/route.php';
}

HTMLHelper::addIncludePath(JPATH_COMPONENT.'/helpers/html');

HTMLHelper::_('bootstrap.tooltip');
if(version_compare(JVERSION, '4', 'lt')) HTMLHelper::_('behavior.framework', true);

$items   = $displayData['items'];
$pagination    = $displayData['pagination'];
$state    = $displayData['state'];

$input     = Factory::getApplication()->input;
$field_id = $input->get('field', '');
$function  = $input->getCmd('function', 'jSelectContact');
$listOrder = $this->escape($state->get('list.ordering'));
$listDirn  = $this->escape($state->get('list.direction'));
?>
<form action="<?php echo Route::_('index.php?option=com_ajax&plugin=jacontenttype&view=contacts&tmpl=component&format=html&function='.$function.'&'.Session::getFormToken().'=1');?>" method="post" name="adminForm" id="adminForm" class="form-inline">
	<fieldset class="filter clearfix">
		<div class="btn-toolbar">
			<div class="btn-group pull-left">
				<label for="filter_search">
					<?php echo Text::_('JSEARCH_FILTER_LABEL'); ?>
				</label>
				<input type="text" name="filter[search]" id="filter_search" value="<?php echo $this->escape($state->get('filter.search')); ?>" size="30" title="<?php echo Text::_('COM_CONTACT_FILTER_SEARCH_DESC'); ?>" />
			</div>
			<div class="btn-group pull-left">
				<button type="submit" class="btn hasTooltip" title="<?php echo HTMLHelper::tooltipText('JSEARCH_FILTER_SUBMIT'); ?>" data-placement="bottom">
					<i class="icon-search"></i></button>
				<button type="button" class="btn hasTooltip" title="<?php echo HTMLHelper::tooltipText('JSEARCH_FILTER_CLEAR'); ?>" data-placement="bottom" onclick="document.getElementById('filter_search').value='';this.form.submit();">
					<i class="icon-remove"></i></button>
			</div>
			<div class="clearfix"></div>
		</div>
		<hr class="hr-condensed" />

		<div class="filters pull-left">
			<select name="filter[access]" class="input-medium" onchange="this.form.submit()">
				<option value=""><?php echo Text::_('JOPTION_SELECT_ACCESS');?></option>
				<?php echo HTMLHelper::_('select.options', HTMLHelper::_('access.assetgroups'), 'value', 'text', $state->get('filter.access'));?>
			</select>

			<select name="filter[published]" class="input-medium" onchange="this.form.submit()">
				<option value=""><?php echo Text::_('JOPTION_SELECT_PUBLISHED');?></option>
				<?php echo HTMLHelper::_('select.options', HTMLHelper::_('jgrid.publishedOptions'), 'value', 'text', $state->get('filter.published'), true);?>
			</select>

			<?php if ($state->get('filter.forcedLanguage')) : ?>
				<select name="filter[category_id]" class="input-medium" onchange="this.form.submit()">
					<option value=""><?php echo Text::_('JOPTION_SELECT_CATEGORY');?></option>
					<?php echo HTMLHelper::_('select.options', HTMLHelper::_('category.options', 'com_contact', array('filter.language' => array('*', $state->get('filter.forcedLanguage')))), 'value', 'text', $state->get('filter.category_id'));?>
				</select>
				<input type="hidden" name="forcedLanguage" value="<?php echo $this->escape($state->get('filter.forcedLanguage')); ?>" />
				<input type="hidden" name="filter[language]" value="<?php echo $this->escape($state->get('filter.language')); ?>" />
			<?php else : ?>
				<select name="filter[category_id]" class="input-medium" onchange="this.form.submit()">
					<option value=""><?php echo Text::_('JOPTION_SELECT_CATEGORY');?></option>
					<?php echo HTMLHelper::_('select.options', HTMLHelper::_('category.options', 'com_contact'), 'value', 'text', $state->get('filter.category_id'));?>
				</select>
				<select name="filter[language]" class="input-medium" onchange="this.form.submit()">
					<option value=""><?php echo Text::_('JOPTION_SELECT_LANGUAGE');?></option>
					<?php echo HTMLHelper::_('select.options', HTMLHelper::_('contentlanguage.existing', true, true), 'value', 'text', $state->get('filter.language'));?>
				</select>
			<?php endif; ?>
		</div>
	</fieldset>

	<table class="table table-striped table-condensed">
		<thead>
		<tr>
			<th class="title">
				<?php echo HTMLHelper::_('grid.sort', 'JGLOBAL_TITLE', 'a.name', $listDirn, $listOrder); ?>
			</th>
			<th class="center nowrap">
				<?php echo HTMLHelper::_('grid.sort', 'COM_CONTACT_FIELD_LINKED_USER_LABEL', 'ul.name', $listDirn, $listOrder); ?>
			</th>
			<th width="15%" class="center nowrap">
				<?php echo HTMLHelper::_('grid.sort', 'JGRID_HEADING_ACCESS', 'access_level', $listDirn, $listOrder); ?>
			</th>
			<th width="15%" class="center nowrap">
				<?php echo HTMLHelper::_('grid.sort', 'JCATEGORY', 'a.catid', $listDirn, $listOrder); ?>
			</th>
			<th width="5%" class="center nowrap">
				<?php echo HTMLHelper::_('grid.sort', 'JGRID_HEADING_LANGUAGE', 'language', $listDirn, $listOrder); ?>
			</th>
			<th width="1%" class="center nowrap">
				<?php echo HTMLHelper::_('grid.sort', 'JGRID_HEADING_ID', 'a.id', $listDirn, $listOrder); ?>
			</th>
		</tr>
		</thead>
		<tfoot>
		<tr>
			<td colspan="6">
				<?php echo $pagination->getListFooter(); ?>
			</td>
		</tr>
		</tfoot>
		<tbody>
		<?php foreach ($items as $i => $item) : ?>
			<?php if ($item->language && Multilanguage::isEnabled())
			{
				$tag = strlen($item->language);
				if ($tag == 5)
				{
					$lang = substr($item->language, 0, 2);
				}
				elseif ($tag == 6)
				{
					$lang = substr($item->language, 0, 3);
				}
				else {
					$lang = "";
				}
			}
			elseif (!Multilanguage::isEnabled())
			{
				$lang = "";
			}
			?>
			<tr class="row<?php echo $i % 2; ?>">
				<td>
					<?php if (version_compare(JVERSION, '4.0', 'ge')): ?>
						<a class="pointer button-select" 
							href="#" 
							data-custom-value="<?php echo $item->id; ?>" 
							data-custom-name="<?php echo $this->escape($item->name); ?>" 
							data-user-value="<?php echo $item->id; ?>" 
							data-user-name="<?php echo $this->escape($item->name); ?>" 
							data-user-field="<?php echo $field_id; ?>">
							<?php echo $this->escape($item->name); ?>
						</a>
					<?php else: ?>
						<a href="javascript:void(0)" onclick="if (window.parent) window.parent.<?php echo $this->escape($function);?>('<?php echo $item->id; ?>', '<?php echo $this->escape(addslashes($item->name)); ?>', '<?php echo $this->escape($item->catid); ?>', null, '<?php echo $this->escape(ContactHelperRoute::getContactRoute($item->id, $item->catid, $item->language)); ?>', '<?php echo $this->escape($lang); ?>', null);">
						<?php echo $this->escape($item->name); ?></a>
					<?php endif; ?>
				</td>
				<td align="center">
					<?php if (!empty($item->linked_user)) : ?>
						<?php echo $item->linked_user;?>
					<?php endif; ?>
				</td>
				<td class="center">
					<?php echo $this->escape($item->access_level); ?>
				</td>
				<td class="center">
					<?php echo $this->escape($item->category_title); ?>
				</td>
				<td class="center">
					<?php if ($item->language == '*'):?>
						<?php echo Text::alt('JALL', 'language'); ?>
					<?php else:?>
						<?php echo $item->language_title ? $this->escape($item->language_title) : Text::_('JUNDEFINED'); ?>
					<?php endif;?>
				</td>
				<td align="center">
					<?php echo (int) $item->id; ?>
				</td>
			</tr>
		<?php endforeach; ?>
		</tbody>
	</table>

	<input type="hidden" name="task" value="" />
	<input type="hidden" name="filter_order" value="<?php echo $listOrder; ?>" />
	<input type="hidden" name="filter_order_Dir" value="<?php echo $listDirn; ?>" />
	<?php echo HTMLHelper::_('form.token'); ?>
</form>
