<?php
// namespace administrator\components\com_jchat\framework\html;
/**  
 * @package JCHAT::administrator::components::com_jchat
 * @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' );

/**
 * Articles multiselect element class
 *
 * @package JCHAT::administrator::components::com_jchat
 * @subpackage framework
 * @subpackage html
 *        
 */
class JChatHtmlArticles extends JObject {
	/**
	 * Build the multiple select list for Menu Links/Pages
	 * 
	 * @access public
	 * @return array
	 */
	public static function getArticles() {
		$db = JFactory::getDBO ();
		
		$articleOptions = array();
		$articleOptions[] = JHtml::_('select.option', '0', JText::_('COM_JCHAT_NOARTICLES'), 'value', 'text');
		$categories = array();
		$categories = JHtml::_('category.options', 'com_content');
		
				
		if(!empty($categories)) {
			foreach ($categories as $category) {
				if(!$category->value) {
					continue;
				}
				// Get category indent from cat to replicate on articles
				preg_match('/^([-\s])+/', $category->text, $matches);
				$indent = null;
				if(isset($matches[0])) {
					$indent = $matches[0];
				}
				
				// Get a list of articles in this category
				$query = "SELECT c.id AS value, c.title AS text" .
						 "\n FROM " . $db->quoteName('#__content') . " AS " . $db->quoteName('c') .
						 "\n WHERE c.state = 1" .
						 "\n AND c.catid = " . (int)$category->value .
						 "\n ORDER BY c.ordering";
				$db->setQuery ( $query );
				$articles = $db->loadObjectList ();
				
				// Group articles by OPTGROUP category
				$articleOptions[] = JHtml::_ ( 'select.option', '<OPTGROUP>', $category->text );
				
				if(!empty($articles)) {
					foreach ($articles as $article) {
						$articleOptions[] = JHtml::_('select.option', $article->value, $indent . $article->text, 'value', 'text');
					}
				}
				
				// Close the OPTGROUP
				$articleOptions[] = JHtml::_ ( 'select.option', '</OPTGROUP>');
			}
		}
		
		return $articleOptions;
	}
}