<?php
/*------------------------------------------------------------------------
# com_guru
# ------------------------------------------------------------------------
# author    iJoomla
# copyright Copyright (C) 2013 ijoomla.com. All Rights Reserved.
# @license - http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
# Websites: http://www.ijoomla.com
# Technical Support:  Forum - http://www.ijoomla.com.com/forum/index/
-------------------------------------------------------------------------*/

defined('_JEXEC') or die('Restricted access');
jimport('joomla.html.html');
jimport('joomla.form.formfield');

class JFormFieldCategories extends JFormFieldList
{
	protected $type = 'categories';
	public $return_array = array();

	function getAllRows($parent, $level)
	{
		$db = JFactory::getDbo();

		$sql = "SELECT id, description, name, child_id as cid, parent_id as pid, ordering, published FROM #__guru_category, #__guru_categoryrel WHERE #__guru_category.id = #__guru_categoryrel.child_id and #__guru_categoryrel.parent_id=" . intval($parent) . " ORDER BY `ordering` ASC";
		$db->setQuery($sql);
		$db->execute();
		$result = $db->loadAssocList();

		if (isset($result) && is_array($result) && count($result) > 0) {
			$level++;
			foreach ($result as $key => $value) {
				$value["level"] = $level;
				$this->return_array[] = $value;
				$this->getAllRows($value["id"], $level);
			}
		}

		return $this->return_array;
	}

	protected function getOptions()
	{
		$options = array(
			array(
				'text' => '-- All Categories --',
				'value' => 0,
			)
		);
		$params = $this->form->getValue('params');
		$category = "0";

		if (isset($params->category)) {
			$category = $params->category;
		}

		$categories = $this->getAllRows(0, 0);

		foreach ($categories as $cat) {
			$options[] = array(
				'text' => str_repeat(' - ', $cat['level']) . $cat['name'],
				'value' => $cat['id'],
			);
		}

		return $options;
	}
}
