<?php
/**
 * View file for responding to Ajax request for performing Search Here on the map
 * 
 */
namespace Digitanie\Component\Leaflet\Site\View\Leaflet;

 use Joomla\CMS\Factory;
 use Joomla\CMS\Response\JsonResponse;
 use Joomla\CMS\MVC\View\HtmlView as BaseHtmlView;
 use Joomla\CMS\Language\Text;
 
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
 
class JsonView extends BaseHtmlView
{
	/**
	 * This display function returns in json format the Helloworld greetings
	 *   found within the latitude and longitude boundaries of the map.
	 * These bounds are provided in the parameters
	 *   minlat, minlng, maxlat, maxlng
	 */

	function display($tpl = null)
	{
		$input = Factory::getApplication()->input;
		$type = $input->get('type', null, 'string');
		$catId = $input->get('catId', null, 'string');

		$model = $this->getModel();
		if ($type == "categories")
		{
			$records = $model->getCategory();
			if ($records) 
			{
				echo new JsonResponse($records);
			}
			else
			{
				echo new JsonResponse(null, Text::_('COM_HELLOWORLD_ERROR_NO_RECORDS'), true);
			}
		} else if ($type == "departement") {
			$records = $model->getTerritoires();
			if ($records) 
			{
				echo new JsonResponse($records);
			}
			else
			{
				echo new JsonResponse(null, Text::_('COM_HELLOWORLD_ERROR_NO_RECORDS'), true);
			}
        } else if ($type == "categorie") {
			$records = $model->getCategoriesExperience();
			if ($records) 
			{
				echo new JsonResponse($records);
			}
			else
			{
				echo new JsonResponse(null, Text::_('COM_HELLOWORLD_ERROR_NO_RECORDS'), true);
			}
		} else if ($type == "activite") {
			$records = $model->getActivite();
			if ($records) 
			{
				echo new JsonResponse($records);
			}
			else
			{
				echo new JsonResponse(null, Text::_('COM_HELLOWORLD_ERROR_NO_RECORDS'), true);
			}
		} else if ($type == "markers" && isset($catId)) {
			$records = $model->getMarkers($catId);
			if ($records) 
			{
				echo new JsonResponse($records);
			}
			else
			{
				echo new JsonResponse(null, Text::_('COM_HELLOWORLD_ERROR_NO_RECORDS_1'), true);
			}
		}
		else 
		{
			$records = array();
			echo new JsonResponse($type);
		}
	}
}