<?php
/*
 *
 * @package		ARI Smart Content
 * @author		ARI Soft
 * @copyright	Copyright (c) 2011 www.ari-soft.com. All rights reserved
 * @license		GNU/GPL (http://www.gnu.org/copyleft/gpl.html)
 * 
 */

(defined('_JEXEC') && defined('ARI_FRAMEWORK_LOADED')) or die;

AriKernel::import('Joomla.Controllers.Controller');

class AriSmartContentController extends AriController
{
	var $_originalName;
	var $_subPath = null;

	function __construct($config = array()) 
	{
		if (array_key_exists('originalName', $config))
			$this->_originalName = $config['originalName'];
		else if (array_key_exists('name', $config))
			$this->_originalName = $this->getName();

		$basePath = array_key_exists('base_path', $config)
			? $config['base_path']
			: JPATH_COMPONENT;

		if (!array_key_exists('view_path', $config))
			$config['view_path'] = $this->getPathTo('views', $basePath);

		parent::__construct($config);
	}
	
	function getPathTo($type, $basePath = null)
	{
		if (is_null($basePath))
			$basePath = J1_5 ? $this->_basePath : $this->basePath;

		return $basePath . '/' . $type . '/' . $this->getSubPath();
	}

	function getSubPath()
	{
		if (is_null($this->_subPath))
		{
			$originalName = $this->getOriginalName();
			$pathParts = explode('_', $originalName);
			array_pop($pathParts);
			$this->_subPath = join('/', $pathParts);
		}
		
		return $this->_subPath;
	}
	
	function getOriginalName()
	{
		return $this->_originalName;
	}

	function getView($name = '', $type = 'html', $prefix = '', $config = array()) 
	{
		if (empty($prefix))
			$prefix = $this->getPrefix() . 'View';

		$nameParts = explode('_', $name);
		$viewName = array_pop($nameParts);

		foreach ($nameParts as $namePart)
			$prefix .= ucfirst($namePart);

		$config['name'] = $name;

		if (!array_key_exists('template_path', $config))
			$config['template_path'] = $this->getPathTo('views') . '/' . $viewName . '/tmpl';			

		return parent::getView($viewName, $type, $prefix, $config);
	}
}