<commit_before><?php

namespace Armd\NewsBundle\Controller;

use Symfony\Component\DependencyInjection\ContainerInterface;
use Armd\NewsBundle\Entity\NewsManager;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Armd\ListBundle\Controller\ListController;
use Armd\MkCommentBundle\Entity\Thread;
use DateTime;
use Armd\UserBundle\Entity\Favorites;
use Armd\UserBundle\Entity\User;

class NewsController extends Controller {
    private $palette_color = 'palette-color-1';
    private $palette_favoritesIcon = 'palette-favoritesIcon-1';
    private $palette_background = 'palette-background-1';

    /**
     * @Route("/{category}", requirements={"category" = "[a-z]+"}, defaults={"category" = null}, name="armd_news_list_index_by_category", options={"expose"=true})
     * @Template("ArmdNewsBundle:NewsNew:index.html.twig")
     */
    function newsIndexAction($category = null) { //TODO: should be renamed to '$categorySlug'
        $request = $this->getRequest();
        $searchQuery = $request->get('search_query');

        $categoryRepository = $this->getDoctrine()->getRepository('ArmdNewsBundle:Category');
        $categories = $categoryRepository->findAll();



        //$category = $categorySlug ? $categoryRepository->findOneBySlug($categorySlug) : null;
        //$category = $category ? array($category) : array('news', 'interviews', 'reportages', 'articles'); //TODO: weird!

        //$newsByDate = array();
/*
        if ($request->query->has('to_date') || $request->query->has('from_date')) {
            $criteria = array(
                NewsManager::CRITERIA_LIMIT => 100
            );

            if ($request->query->has('from_date')) {
                $criteria[NewsManager::CRITERIA_NEWS_DATE_SINCE] = new \DateTime($request->get('from_date'));
            }
            if ($request->query->has('to_date')) {
                $criteria[NewsManager::CRITERIA_NEWS_DATE_TILL] = new \DateTime($request->get('to_date'));
            }

            $news = $newsManager->findObjects($criteria);
            $newsByDate = $this->getDateGroupedNews($category, $news);
        }
        */
        //else { //the default way
            //$firstLoadedDate = new \DateTime($request->get('first_loaded_date')); //at first get minimal date

            //if ($request->query->has('first_loaded_date')) {
            //    $firstLoadedDate->sub(new \DateInterval('P1D'))->setTime(0, 0);
            //}

            $criteria = array(
                NewsManager::CRITERIA_CATEGORY_SLUGS_OR => $category,
                //NewsManager::CRITERIA_NEWS_DATE_TILL => $firstLoadedDate,
                NewsManager::CRITERIA_LIMIT => 10,
                NewsManager::CRITERIA_ORDER_BY => array('newsDate' => 'DESC')
            );

            $news = $this->getNewsManager()->findObjects($criteria);
//print count($news);
            /*
            if (count($news) > 0) {
                // this is low date
                $criteria[NewsManager::CRITERIA_NEWS_DATE_SINCE] = $news[count($news) - 1]->getNewsDate();

                // now get news
                unset($criteria[NewsManager::CRITERIA_LIMIT]);
                $news = $newsManager->findObjects($criteria);
                $newsByDate = $this->getDateGroupedNews($category, $news);
            }
            */
        //}

        return array(
            'news' => $news,
            'categories' => $categories,
            'currentCategory' => $category,
            'palette_color' => $this->palette_color,
            'searchQuery' => $searchQuery
        );
    }

    /**
     * @Route("/{id}", requirements={"id" = "\d+"}, name="armd_news_item_by_category", options={"expose"=true})
     * @Template("ArmdNewsBundle:NewsNew:item.html.twig")
     */
    function newsItemAction($id) {
        $categoryRepository = $this->getDoctrine()->getRepository('ArmdNewsBundle:Category');
        $categories = $categoryRepository->findAll();

        $entityManager = $this->getDoctrine()->getManager();
        $entity = $entityManager->getRepository('ArmdNewsBundle:News')->findOneBy(array('id' => $id, 'published' => true));
        if ($entity) {
            throw $this->createNotFoundException(sprintf('Unable to find record %d', $id));
        }
        $this->getTagManager()->loadTagging($entity);

        $user = $this->container->get('security.context')->getToken()->getUser();
        if ($user instanceof User) { //if you are logged in
            $favorite = $entityManager->getRepository('ArmdUserBundle:Favorites')->findBy(array(
                'user' => $user->getId(),
                'resourceType' => Favorites::TYPE_MEDIA,
                'resourceId' => $entity->getId()
            ));
        }
        else {
            $favorite = false;
        }

        return array(
            'entity' => $entity,
            'isFavored' => $favorite ? true : false,
            'categories' => $categories,
            'palette_color' => $this->palette_color,
            'palette_favoritesIcon' => $this->palette_favoritesIcon,
            'palette_background' => $this->palette_background,
            'isCommentable' => $this->isCommentable($entity)<commit_msg>- Small News bug fix.<commit_after><?php

namespace Armd\NewsBundle\Controller;

use Symfony\Component\DependencyInjection\ContainerInterface;
use Armd\NewsBundle\Entity\NewsManager;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Armd\ListBundle\Controller\ListController;
use Armd\MkCommentBundle\Entity\Thread;
use DateTime;
use Armd\UserBundle\Entity\Favorites;
use Armd\UserBundle\Entity\User;

class NewsController extends Controller {
    private $palette_color = 'palette-color-1';
    private $palette_favoritesIcon = 'palette-favoritesIcon-1';
    private $palette_background = 'palette-background-1';

    /**
     * @Route("/{category}", requirements={"category" = "[a-z]+"}, defaults={"category" = null}, name="armd_news_list_index_by_category", options={"expose"=true})
     * @Template("ArmdNewsBundle:NewsNew:index.html.twig")
     */
    function newsIndexAction($category = null) { //TODO: should be renamed to '$categorySlug'
        $request = $this->getRequest();
        $searchQuery = $request->get('search_query');

        $categoryRepository = $this->getDoctrine()->getRepository('ArmdNewsBundle:Category');
        $categories = $categoryRepository->findAll();



        //$category = $categorySlug ? $categoryRepository->findOneBySlug($categorySlug) : null;
        //$category = $category ? array($category) : array('news', 'interviews', 'reportages', 'articles'); //TODO: weird!

        //$newsByDate = array();
/*
        if ($request->query->has('to_date') || $request->query->has('from_date')) {
            $criteria = array(
                NewsManager::CRITERIA_LIMIT => 100
            );

            if ($request->query->has('from_date')) {
                $criteria[NewsManager::CRITERIA_NEWS_DATE_SINCE] = new \DateTime($request->get('from_date'));
            }
            if ($request->query->has('to_date')) {
                $criteria[NewsManager::CRITERIA_NEWS_DATE_TILL] = new \DateTime($request->get('to_date'));
            }

            $news = $newsManager->findObjects($criteria);
            $newsByDate = $this->getDateGroupedNews($category, $news);
        }
        */
        //else { //the default way
            //$firstLoadedDate = new \DateTime($request->get('first_loaded_date')); //at first get minimal date

            //if ($request->query->has('first_loaded_date')) {
            //    $firstLoadedDate->sub(new \DateInterval('P1D'))->setTime(0, 0);
            //}

            $criteria = array(
                NewsManager::CRITERIA_CATEGORY_SLUGS_OR => $category,
                //NewsManager::CRITERIA_NEWS_DATE_TILL => $firstLoadedDate,
                NewsManager::CRITERIA_LIMIT => 10,
                NewsManager::CRITERIA_ORDER_BY => array('newsDate' => 'DESC')
            );

            $news = $this->getNewsManager()->findObjects($criteria);
//print count($news);
            /*
            if (count($news) > 0) {
                // this is low date
                $criteria[NewsManager::CRITERIA_NEWS_DATE_SINCE] = $news[count($news) - 1]->getNewsDate();

                // now get news
                unset($criteria[NewsManager::CRITERIA_LIMIT]);
                $news = $newsManager->findObjects($criteria);
                $newsByDate = $this->getDateGroupedNews($category, $news);
            }
            */
        //}

        return array(
            'news' => $news,
            'categories' => $categories,
            'currentCategory' => $category,
            'palette_color' => $this->palette_color,
            'searchQuery' => $searchQuery
        );
    }

    /**
     * @Route("/{id}", requirements={"id" = "\d+"}, name="armd_news_item_by_category", options={"expose"=true})
     * @Template("ArmdNewsBundle:NewsNew:item.html.twig")
     */
    function newsItemAction($id) {
        $categoryRepository = $this->getDoctrine()->getRepository('ArmdNewsBundle:Category');
        $categories = $categoryRepository->findAll();

        $entityManager = $this->getDoctrine()->getManager();
        $entity = $entityManager->getRepository('ArmdNewsBundle:News')->findOneBy(array('id' => $id, 'published' => true));
        if (!$entity) {
            throw $this->createNotFoundException(sprintf('Unable to find record %d', $id));
        }
        $this->getTagManager()->loadTagging($entity);

        $user = $this->container->get('security.context')->getToken()->getUser();
        if ($user instanceof User) { //if you are logged in
            $favorite = $entityManager->getRepository('ArmdUserBundle:Favorites')->findBy(array(
                'user' => $user->getId(),
                'resourceType' => Favorites::TYPE_MEDIA,
                'resourceId' => $entity->getId()
            ));
        }
        else {
            $favorite = false;
        }

        return array(
            'entity' => $entity,
            'isFavored' => $favorite ? true : false,
            'categories' => $categories,
            'palette_color' => $this->palette_color,
            'palette_favoritesIcon' => $this->palette_favoritesIcon,
            'palette_background' => $this->palette_background,
            'isCommentable' => $this->isCommentable($entity)
{'fraction_non_alphanumeric': 0.1546486935647678, 'fraction_numerical': 0.0024005170344381867, 'mean_word_length': 2.678098471986418, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 153, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2645022', 'n_tokens_mistral': 3149, 'n_tokens_neox': 3079, 'n_words': 471}