<?php
namespace JExtstore\Component\JRealtimeAnalytics\Administrator\Framework\Html;
/**
 *
 * @package JREALTIMEANALYTICS::FRAMEWORK::administrator::components::com_jrealtimeanalytics
 * @subpackage framework
 * @subpackage html
 * @author Joomla! Extensions Store
 * @copyright (C) 2015 - Joomla! Extensions Store
 * @license GNU/GPLv2 http://www.gnu.org/licenses/gpl-2.0.html
 */
defined ( '_JEXEC' ) or die ( 'Restricted access' );
use Joomla\CMS\Language\Text;
use Joomla\CMS\Factory;
use Joomla\CMS\HTML\HTMLHelper;

/**
 * Override for html utility functions
 * 
 * @package JREALTIMEANALYTICS::FRAMEWORK::administrator::components::com_jrealtimeanalytics
 * @subpackage framework
 * @subpackage html
 * @since 1.0
 */
class Categories {
	/**
	 * A flexible category list that respects access controls
	 *
	 * @var string
	 * @since 1.6
	 */
	public $type = 'CategoryEdit';
	
	/**
	 * Returns an array of categories for the given extension.
	 *
	 * @param string $extension
	 *        	The extension option e.g. com_something.
	 * @param array $config
	 *        	An array of configuration options. By default, only
	 *        	published and unpublished categories are returned.
	 *        	
	 * @return array
	 *
	 * @since 1.5
	 */
	public static function getOptions($isFilter = false, $exclude = null) {
		$db = Factory::getContainer()->get('DatabaseDriver');
		
		$query = method_exists ( $db, 'createQuery' ) ? $db->createQuery () : $db->getQuery ( true );
		$query->select ( 'a.id, a.title, a.level' )
			  ->from ( '#__realtimeanalytics_categories AS a' )
			  ->where ( 'a.parent_id > 0' );
		$query->where ( 'a.published = 1');
		$query->order ( 'a.lft' );
		
		$db->setQuery ( $query );
		$items = $db->loadObjectList ();
		
		$options = array();
		if(!$isFilter) {
			$options[] = HTMLHelper::_ ( 'select.option', 1, Text::_('COM_JREALTIME_ROOTCAT'));
		} else {
			$options[] = HTMLHelper::_ ( 'select.option', null, Text::_('COM_JREALTIME_FILTER_BYCAT'));
		}
		
		foreach ( $items as &$item ) {
			$repeat = ($item->level - 1 >= 0) ? $item->level : 0;
			$item->title = str_repeat ( '- ', $repeat - (int)$isFilter) . $item->title;
			$disabled = $item->id == $exclude ? true : false;
			$options[] = HTMLHelper::_ ( 'select.option', $item->id, $item->title, 'value', 'text', $disabled);
		}
		
		return $options;
	}
}
