<?php
/**
* @package		Komento
* @copyright	Copyright (C) 2010 - 2018 Stack Ideas Sdn Bhd. All rights reserved.
* @license		GNU/GPL, see LICENSE.php
* Komento is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*/
defined('_JEXEC') or die('Unauthorized Access');

require_once(__DIR__ . '/abstract.php');

class KomentoComsample extends KomentoExtension
{
	public $_item;

	// map the keys here
	public $_map = array(
		// not needed with custom getContentId()
		'id'			=> 'id_field',

		// not needed with custom getContentTitle()
		'title'			=> 'title_field',

		// not needed with custom getContentHits()
		'hits'			=> 'hits_field',

		// not needed with custom getAuthorId()
		'created_by'	=> 'created_by_field',

		// not needed with custom getCategoryId()
		'catid'			=> 'catid_field',

		// not needed with custom getContentPermalink()
		'permalink'		=> 'permalink_field'
		);

	// constructor. add all required files here
	public function __construct( $component )
	{
		// Load all required files by component
		// $this->addFile( your component's files );

		parent::__construct( $component );
	}

	// load all main properties here based on article id
	public function load( $cid )
	{
		static $instances = array();

		if( !isset( $instances[$cid] ) )
		{
			// populate $this->_item with:
			// id_field
			// title_field
			// hits_field
			// created_by_field
			// catid_field
			// permalink_field

			$db		= KT::db();
			$query	= 'SELECT `id_field`, `title_field`, `hits_field`, `created_by_field`, `catid_field` FROM `ARTICLE_TABLE` WHERE `ARTICLE_ID` = ' . $db->quote( $cid );
			$db->setQuery( $query );

			// return false or call the onLoadArticleError event if there are no objects to load
			if( !$this->_item = $db->loadObject() )
			{
				return $this->onLoadArticleError( $cid );
			}

			// generate link for this article
			$this->_item->permalink_field = 'index.php?options=com_sample&view=article&id=' . $this->_item->id_field;

			// call the prepareLink function and leave the rest to us
			// unless you have custom SEF methods, then use "getContentPermalink" function to overwrite
			$this->_item->permalink_field = $this->prepareLink( $this->_item->permalink_field );

			$instances[$cid] = $this->_item;
		}

		$this->_item = $instances[$cid];

		return $this;
	}

	public function getContentIds( $categories = '' )
	{
		$db		= KT::db();
		$query = '';

		if( empty( $categories ) )
		{
			$query = 'SELECT `id_field` FROM `ARTICLE_TABLE` ORDER BY `id_field`';
		}
		else
		{
			if( is_array( $categories ) )
			{
				$categories = implode( ',', $categories );
			}

			$query = 'SELECT `id_field` FROM `ARTICLE_TABLE` WHERE `catid_field` IN (' . $categories . ') ORDER BY `id_field`';
		}

		$db->setQuery( $query );
		return $db->loadResultArray();
	}

	public function getCategories()
	{
		$db		= KT::db();
		$query	= 'SELECT `id`, `title`, `level`, `parent_id` FROM `CATEGORY_TABLE`';
		$db->setQuery( $query );
		$categories = $db->loadObjectList();

		// optional. populate tree listing
		foreach( $categories as &$row )
		{
			$repeat = ( $row->level - 1 >= 0 ) ? $row->level - 1 : 0;
			$row->treename = str_repeat( '.&#160;&#160;&#160;', $repeat ) . ( $row->level - 1 > 0 ? '|_&#160;' : '' ) . $row->title;
		}

		return $categories;
	}

	// to determine if is listing view
	public function isListingView()
	{
		$views = array('featured', 'category', 'categories', 'archive', 'frontpage' );

		return in_array($this->input->get('view'), $views);
	}

	// to determine if is entry view
	public function isEntryView()
	{
		return $this->input->get('view') == 'article';
	}

	public function onExecute( &$article, $html, $view, $options = array() )
	{
		// $html is the html content generated by komento (includes listing and form)

		$article->text .= $html;
		return $html;
	}
}
