<?php
/**
 * @package     Joomla.Administrator
 * @subpackage  com_leaflet
 *
 * @copyright   Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

// No direct access to this file
defined('_JEXEC') or die('Restricted access');

/**
 * leaflet Model
 *
 * @since  0.0.1
 */
class leafletModelleaflet extends JModelItem
{
	/**
	 * @var string message
	 */
	protected $message;

	//------------------- ID CATEGORIES
    public function getCategory()
	{
		try 
		{
			$db    = JFactory::getDbo();
			$query = $db
				->getQuery(true)
				->select('id')
				->from($db->quoteName('q8x14_categories'))
				->where($db->quoteName('alias') . " = " . $db->quote('experiences'), 'OR')
				->where($db->quoteName('alias') . " = " . $db->quote('appel-a-projet-dt'), 'OR')
				->where($db->quoteName('alias') . " = " . $db->quote('savoir-faire-siae'));

			$db->setQuery($query);
			$results = $db->loadColumn();
			
		}
		catch (Exception $e)
		{
			$msg = $e->getMessage();
			JFactory::getApplication()->enqueueMessage($msg, 'error'); 
			$results = null;
		}

		return $results; 
	}
	//--------------------- EOF ID CATEGORIES

	//--------------------- VALEURS 
	public function getTerritoires()
	{
		try 
		{
			$db    = JFactory::getDbo();
			$query = $db->getQuery(true);

			$query->select($db->quoteName(array('id', 'title', 'name', 'fieldparams')));
			$query->from($db->quoteName('q8x14_fields'));
			$query->where($db->quoteName('name') . " = " . $db->quote('departement'));
			$db->setQuery($query);
			$results = $db->loadAssoc();
		}
		catch (Exception $e)
		{
			$msg = $e->getMessage();
			JFactory::getApplication()->enqueueMessage($msg, 'error'); 
			$results = null;
		}

		return $results; 
	}

	public function getCategoriesExperience()
	{
		try 
		{
			$db    = JFactory::getDbo();
			$query = $db->getQuery(true);

			$query->select($db->quoteName(array('id', 'title', 'name', 'fieldparams')));
			$query->from($db->quoteName('q8x14_fields'));
			/*NOM SUJET A EVOLUTION THOMAS*/
			$query->where($db->quoteName('name') . " = " . $db->quote('categorie'));
			$db->setQuery($query);
			$results = $db->loadAssoc();

		}
		catch (Exception $e)
		{
			$msg = $e->getMessage();
			JFactory::getApplication()->enqueueMessage($msg, 'error'); 
			$results = null;
		}

		return $results; 
	}

	public function getActivite()
	{
		try 
		{
			$db    = JFactory::getDbo();
			$query = $db->getQuery(true);

			$query->select($db->quoteName(array('id', 'title', 'name', 'fieldparams')));
			$query->from($db->quoteName('q8x14_fields'));
			$query->where($db->quoteName('name') . " = " . $db->quote('activite'));
			$db->setQuery($query);
			$results = $db->loadAssoc();

		}
		catch (Exception $e)
		{
			$msg = $e->getMessage();
			JFactory::getApplication()->enqueueMessage($msg, 'error'); 
			$results = null;
		}

		return $results; 
	}
	//------------------------- EOF VALEURS

	public function getMarkers($catId)
	{
		try 
		{
			$db    = JFactory::getDbo();

			$query = $db->getQuery(true);
			$groupId = 4;

			//On récupère les noms et id des custom fields
			$query->select($db->quoteName(array('f.id', 'f.name')));
			$query->from($db->quoteName('q8x14_fields_groups', 'g'));
			$query->join('INNER', $db->quoteName('q8x14_fields', 'f') . ' ON ' . $db->quoteName('g.id') . ' = ' . $db->quoteName('f.group_id'));
			$query->where($db->quoteName('g.id') . ' = ' . $groupId);
			//test offset pour voir si la premiere entrée est problématique
			//$query->setLimit(27, 1);
			$db->setQuery($query);
			$fields = $db->loadObjectList();

			$fieldsSelect = array('c.id',
			'c.title',
			'c.alias',
			'c.state',
			'c.introtext');
			foreach($fields as $field) {
				array_push($fieldsSelect, 'GROUP_CONCAT(DISTINCT IF(field_id = ' . $field->id . ', value, NULL) SEPARATOR ",") AS ' . $field->name);
			}
			$stateValue = 1;
			$query->clear();
			$query->select($fieldsSelect);
			$query->from($db->quoteName('q8x14_content', 'c'));
			$query->join('INNER', $db->quoteName('q8x14_fields_values', 'f') . ' ON ' . $db->quoteName('c.id') . ' = ' . $db->quoteName('f.item_id'));
			$query->where($db->quoteName('c.catid') . ' = ' . $catId); // projects
			$query->where($db->quoteName('c.state') . " = " . $stateValue);
			$query->group($db->quoteName('c.id'));

			$db->setQuery($query);
			$results = $db->loadObjectList(); 

			// NEED TO REVIEW GROUP CONCAT TO SEE IF WE CAN GET ARRAY DIRECTLY
			foreach($results as $content) {
				$content->departement = explode(',', $content->departement);
				$content->categorie = explode(',', $content->categorie);
				$content->activite = explode(',', $content->activite);
				
				
			}
		}
		catch (Exception $e)
		{
			$msg = $e->getMessage();
			JFactory::getApplication()->enqueueMessage($msg, 'error'); 
			$results = null;
		}

		return $results; 
	}
}
