<?php

namespace Rapsys\BlogBundle\Repository;

/**
 * ArticleRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class ArticleRepository extends \Doctrine\ORM\EntityRepository {
	/**
	 * Find article
	 *
	 * @param string $_locale
	 * @param string $_article
	 */
	public function findArticle($_locale, $_article) {
#		//TODO: voir comment faire ça sans gros hack sql ?
#		//Declare all field
#		$rsm = new \Doctrine\ORM\Query\ResultSetMapping();
#		$rsm->addScalarResult('id', 'id');
#		$rsm->addScalarResult('created', 'created');
#		$rsm->addScalarResult('updated', 'updated');
#		$rsm->addScalarResult('slug', 'slug');
#		$rsm->addScalarResult('title', 'title');
#		$rsm->addScalarResult('description', 'description');
#		$rsm->addScalarResult('body', 'body');
#		$rsm->addScalarResult('keywords', 'keywords');
#		$rsm->addScalarResult('slugs', 'slugs');
#
#		//Fetch article
#		$ret = $this->getEntityManager()
#			#->createQuery('SELECT b.*, JSON(bl.iso6391, bat.slug) AS slugs FROM (SELECT a.id, a.created, a.updated, at.slug, at.title, at.description, at.body, JSON(kt.slug, kt.title) AS keywords FROM RapsysBlogBundle:Article a JOIN RapsysBlogBundle:ArticleTranslation at WITH (at.article_id = a.id AND at.slug = :_article) JOIN RapsysBlogBundle:Language AS l WITH (l.id = at.language_id AND l.iso6391 = :_locale) LEFT JOIN RapsysBlogBundle:ArticleKeyword ak WITH (ak.article_id = a.id) LEFT JOIN RapsysBlogBundle:KeywordTranslation kt WITH (kt.keyword_id = ak.keyword_id AND kt.language_id = at.language_id) GROUP BY a.id) b LEFT JOIN RapsysBlogBundle:Language AS bl WITH (bl.iso6391 <> :_locale) LEFT JOIN RapsysBlogBundle:ArticleTranslation bat WITH (bat.language_id = bl.id AND bat.article_id = b.id) GROUP BY b.id')
#			->createNativeQuery('SELECT b.*, CONCAT(\'{\', GROUP_CONCAT(CONCAT(\'"\', REPLACE(bl.iso6391, \'"\', \'\\\\"\'), \'": "\', REPLACE(bat.slug, \'"\', \'\\\\"\'), \'"\')), \'}\') AS slugs FROM (SELECT a.id, a.created, a.updated, at.slug, at.title, at.description, at.body, CONCAT(\'{\', GROUP_CONCAT(CONCAT(\'"\', REPLACE(kt.slug, \'"\', \'\\\\"\'), \'": "\', REPLACE(kt.title, \'"\', \'\\\\"\'), \'"\')), \'}\') AS keywords FROM articles a JOIN articles_translations at ON (at.article_id = a.id AND at.slug = ?) JOIN languages AS l ON (l.id = at.language_id AND l.iso6391 = ?) LEFT JOIN articles_keywords ak ON (ak.article_id = a.id) LEFT JOIN keywords_translations kt ON (kt.keyword_id = ak.keyword_id AND kt.language_id = at.language_id) GROUP BY a.id) b LEFT JOIN languages AS bl ON (bl.iso6391 <> ?) LEFT JOIN articles_translations bat ON (bat.language_id = bl.id AND bat.article_id = b.id) GROUP BY b.id', $rsm)
#			->setParameter(0, $_article)
#			->setParameter(1, $_locale)
#			->setParameter(2, $_locale)
#			->getSingleResult();
#		//Decode json
#		if (!empty($ret['keywords'])) {
#			$ret['keywords'] = json_decode($ret['keywords'], true);
#		}
#		if (!empty($ret['slugs'])) {
#			$ret['slugs'] = json_decode($ret['slugs'], true);
#		}
#		//Send result
#		return $ret;
#
		//Fetch article
		$ret = $this->getEntityManager()
			#->createQuery('SELECT b.*, JSON(bl.iso6391, bat.slug) AS slugs FROM (SELECT a.id, a.created, a.updated, at.slug, at.title, at.description, at.body, JSON(kt.slug, kt.title) AS keywords FROM RapsysBlogBundle:Article a JOIN RapsysBlogBundle:ArticleTranslation at WITH (at.article_id = a.id AND at.slug = :_article) JOIN RapsysBlogBundle:Language AS l WITH (l.id = at.language_id AND l.iso6391 = :_locale) LEFT JOIN RapsysBlogBundle:ArticleKeyword ak WITH (ak.article_id = a.id) LEFT JOIN RapsysBlogBundle:KeywordTranslation kt WITH (kt.keyword_id = ak.keyword_id AND kt.language_id = at.language_id) GROUP BY a.id) b LEFT JOIN RapsysBlogBundle:Language AS bl WITH (bl.iso6391 <> :_locale) LEFT JOIN RapsysBlogBundle:ArticleTranslation bat WITH (bat.language_id = bl.id AND bat.article_id = b.id) GROUP BY b.id')
			->createQuery('SELECT a.id, a.created, a.updated, at.slug, at.title, at.description, at.body, JSON(kt.slug, kt.title) AS keywords FROM RapsysBlogBundle:Article a JOIN RapsysBlogBundle:ArticleTranslation at WITH (at.article_id = a.id AND at.slug = :_article) JOIN RapsysBlogBundle:Language AS l WITH (l.id = at.language_id AND l.iso6391 = :_locale) LEFT JOIN RapsysBlogBundle:ArticleKeyword ak WITH (ak.article_id = a.id) LEFT JOIN RapsysBlogBundle:KeywordTranslation kt WITH (kt.keyword_id = ak.keyword_id AND kt.language_id = at.language_id) GROUP BY a.id')
			->setParameter('_locale', $_locale)
			->setParameter('_article', $_article)
			->getSingleResult();

		//Decode json
		if (!empty($ret['keywords'])) {
			$ret['keywords'] = json_decode($ret['keywords'], true);
		}

		//Fetch article's slugs in other locale
		$slugs = $this->getEntityManager()
			->createQuery('SELECT JSON(bl.iso6391, bat.slug) AS slugs FROM RapsysBlogBundle:Language bl LEFT JOIN RapsysBlogBundle:ArticleTranslation bat WITH (bat.article_id = :_article AND bat.language_id = bl.id) WHERE (bl.iso6391 <> :_locale) GROUP BY bat.article_id')
			->setParameter('_locale', $_locale)
			->setParameter('_article', $ret['id'])
			->getSingleResult();

		//Decode json
		if (!empty($slugs['slugs'])) {
			$ret['slugs'] = json_decode($slugs['slugs'], true);
		}

		//Send result
		return $ret;
	}

	/**
	 * Find articles
	 *
	 * @param string $_locale
	 */
	public function findArticles($_locale) {
		//Fetch articles
		$ret = $this->getEntityManager()
                        ->createQuery('SELECT a.id, a.created, a.updated, at.slug, at.title, at.description, JSON(kt.slug, kt.title) AS keywords FROM RapsysBlogBundle:Article a JOIN RapsysBlogBundle:ArticleTranslation at WITH (at.article_id = a.id) JOIN RapsysBlogBundle:Language AS l WITH (l.id = at.language_id AND l.iso6391 = :_locale) LEFT JOIN RapsysBlogBundle:ArticleKeyword ak WITH (ak.article_id = a.id) LEFT JOIN RapsysBlogBundle:KeywordTranslation kt WITH (kt.keyword_id = ak.keyword_id AND kt.language_id = at.language_id) GROUP BY a.id')
                        ->setParameter('_locale', $_locale)
			->execute();
		//Decode json
		if (!empty($ret)) {
			foreach ($ret as $id => $article) {
				if (!empty($article['keywords'])) {
					$ret[$id]['keywords'] = json_decode($article['keywords'], true);
				}
			}
		}
		//Send result
		return $ret;
	}
}