<commit_before><?php

namespace Atotrukis\MainBundle\Service;

use Atotrukis\MainBundle\Entity\EventKeywords;
use Doctrine\ORM\EntityManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Atotrukis\MainBundle\Entity\Event;

class AdminService
{

    protected $entityManager;
    protected $container;

    /**
     * @param EntityManager $entityManager
     * @param ContainerInterface $container
     */
    public function __construct(EntityManager $entityManager, ContainerInterface $container)
    {
        $this->entityManager = $entityManager;
        $this->container = $container;
    }

    /**
     * @param $request \Symfony\Component\HttpFoundation\Request
     * @return array of \Atotrukis\MainBundle\Entity\User
     */
    public function readUsers($request)
    {

        $user = $this->entityManager
            ->getRepository('AtotrukisMainBundle:User')
            ->findAll();
        if (!$user) {
            $this->addFlash($request, 'Vartotojų nėra!', 'danger');
        }
        return $user;
    }

    /**
     * blocks/unblocks user
     *
     * @param $request \Symfony\Component\HttpFoundation\Request
     * @param $userId int
     */
    public function blockUser($request, $userId)
    {

        $user = $this->entityManager
            ->getRepository('AtotrukisMainBundle:User')
            ->findOneById($userId);
        if (!$user) {
            $this->addFlash($request, 'Vartotojo su tokiu ID nėra!', 'danger');
        }
        if ($user->isLocked()) {
            $user->setLocked(0);
        } else {
            $user->setLocked(1);
        }
        $entityManager = $this->entityManager;
        $entityManager->persist($user);
        $entityManager->flush();
    }

    /**
     * @param $request \Symfony\Component\HttpFoundation\Request
     * @param $message String
     * @param $status String
     */
    public function addFlash($request, $message, $status)
    {
        $request->getSession()->getFlashBag()->add($status, $message);
    }

    /**
     * gets information from rss feed
     *
     * @param $x
     * @param $regexDate
     * @param $regexStartTime
     */
    public function updateEvents($x, $regexDate, $regexStartTime)
    {
        foreach($x->channel->item as $entry) {

            $name = preg_split($regexDate, $entry->title)[0];

            $startDate = $this->getStartDate($regexDate, $entry, $regexStartTime);
            $endDate = $startDate;

            $description = $this->getDescription($entry);
            $this->getDescription($entry);

            $keywords = explode(" ", $entry->title);

            if ($name) {
                $this->addToDatabase($name, $startDate, $endDate, $description, $keywords);
            }
        }
    }

    /**
     * changes date format from dd.mm.yyyy to yyyy.mm.dd
     *
     * @param $regexDate
     * @param $entry
     * @return string of date in yyyy.mm.dd format
     */
    private function getStartDateYmd($regexDate, $entry)
    {
        preg_match($regexDate, $entry->title, $regDateMatch);
        $startYmd = date('Y-m-d', strtotime($regDateMatch[1]));
        return $startYmd;
    }

    /**
     * joins date, hour and minutes to one datetime object
     *
     * @param $regexDate
     * @param $entry
     * @param $regexStartTime
     * @return \DateTime of start date
     */
    private function getStartDate($regexDate, $entry, $regexStartTime)
    {
        $startYmd = $this->getStartDateYmd($regexDate, $entry);
        if (preg_match($regexStartTime, $entry->title, $regTimeMatch)) {
            $dateTime = $startYmd . " " . $regTimeMatch[1];
        } else {
            $dateTime = $startYmd;
        }
        return new \DateTime($dateTime);
    }

    private function getDescription($entry)
    {
        $exploded = explode("Durys atidaromos", $entry->description);
        if (preg_match('#<span style=\"(.*)\">(.*?)</span>#', $exploded[0], $match)) {
            $withoutColored = preg_replace('#<span style=\"(.*)\">(.*?)</span>#', '', $exploded[0]);
            return $withoutColored;
        } else {
            return $exploded[0];
        }
    }

    /**
     * adds new events and their keywords to database
     *
     * @param $name
     * @param $startDate
     * @param $endDate
     * @param $description
     * @param $keywords
     * @internal param $title
     */
    private function addToDatabase($name, $startDate, $endDate, $description, $keywords)<commit_msg>Getting end date from rss<commit_after><?php

namespace Atotrukis\MainBundle\Service;

use Atotrukis\MainBundle\Entity\EventKeywords;
use Doctrine\ORM\EntityManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Atotrukis\MainBundle\Entity\Event;

class AdminService
{

    protected $entityManager;
    protected $container;

    /**
     * @param EntityManager $entityManager
     * @param ContainerInterface $container
     */
    public function __construct(EntityManager $entityManager, ContainerInterface $container)
    {
        $this->entityManager = $entityManager;
        $this->container = $container;
    }

    /**
     * @param $request \Symfony\Component\HttpFoundation\Request
     * @return array of \Atotrukis\MainBundle\Entity\User
     */
    public function readUsers($request)
    {

        $user = $this->entityManager
            ->getRepository('AtotrukisMainBundle:User')
            ->findAll();
        if (!$user) {
            $this->addFlash($request, 'Vartotojų nėra!', 'danger');
        }
        return $user;
    }

    /**
     * blocks/unblocks user
     *
     * @param $request \Symfony\Component\HttpFoundation\Request
     * @param $userId int
     */
    public function blockUser($request, $userId)
    {

        $user = $this->entityManager
            ->getRepository('AtotrukisMainBundle:User')
            ->findOneById($userId);
        if (!$user) {
            $this->addFlash($request, 'Vartotojo su tokiu ID nėra!', 'danger');
        }
        if ($user->isLocked()) {
            $user->setLocked(0);
        } else {
            $user->setLocked(1);
        }
        $entityManager = $this->entityManager;
        $entityManager->persist($user);
        $entityManager->flush();
    }

    /**
     * @param $request \Symfony\Component\HttpFoundation\Request
     * @param $message String
     * @param $status String
     */
    public function addFlash($request, $message, $status)
    {
        $request->getSession()->getFlashBag()->add($status, $message);
    }

    /**
     * gets information from rss feed
     *
     * @param $x
     * @param $regexDate
     * @param $regexStartTime
     */
    public function updateEvents($x, $regexDate, $regexStartTime)
    {
        foreach($x->channel->item as $entry) {

            $name = preg_split($regexDate, $entry->title)[0];

            $startDate = $this->getStartDate($regexDate, $entry, $regexStartTime);

            $endDate = $this->getEndDate($regexDate, $entry, $regexStartTime);

            $description = $this->getDescription($entry);
            $this->getDescription($entry);

            $keywords = explode(" ", $entry->title);

            if ($name) {
                $this->addToDatabase($name, $startDate, $endDate, $description, $keywords);
            }
        }
    }

    /**
     * changes date format from dd.mm.yyyy to yyyy.mm.dd
     *
     * @param $regexDate
     * @param $entry
     * @return string of date in yyyy.mm.dd format
     */
    private function getStartDateYmd($regexDate, $entry)
    {
        preg_match($regexDate, $entry->title, $regDateMatch);
        $startYmd = date('Y-m-d', strtotime($regDateMatch[1]));
        return $startYmd;
    }

    /**
     * joins date, hour and minutes to one datetime object
     *
     * @param $regexDate
     * @param $entry
     * @param $regexStartTime
     * @return \DateTime of start date
     */
    private function getStartDate($regexDate, $entry, $regexStartTime)
    {
        $startYmd = $this->getStartDateYmd($regexDate, $entry);
        if (preg_match($regexStartTime, $entry->title, $regTimeMatch)) {
            $dateTime = $startYmd . " " . $regTimeMatch[1];
        } else {
            $dateTime = $startYmd;
        }
        return new \DateTime($dateTime);
    }

    private function getEndDate($regexDate, $entry, $regexStartTime)
    {
        preg_match($regexDate, $entry->title, $regStartOriginal);
        $exploded = explode($regStartOriginal[0], $entry->title);
        $endDate = $regStartOriginal[0];
        $dateWithDashes = $exploded[1];
        if (preg_match($regexDate, $dateWithDashes, $matchEndDate)) {
            $endDate = date('Y-m-d', strtotime($matchEndDate[0]));
        } else {
            $r = explode("Renginio trukmė:", $entry->description);
            if (isset($r[1])) {
                $r = explode("Pertraukos", $r[1]);
                if (isset($r[1])) {
                    $duration = str_replace('~', '', $r[0]);
                    if (preg_match('/\d{1}:\d{2}/', $duration, $mathDuration)) {
                        if (preg_match($regexStartTime, $entry->title, $regTimeMatch)) {
                            $start = strtotime($regTimeMatch[0]);
                            $dur = strtotime($mathDuration[0]);
                            $today = strtotime("TODAY");
                            $m_time1 = $start - $today;
                            $m_time2 = $dur - $today;
                            $endTime = $m_time1 + $m_time2 + $today;
                            $endTimeDate = date('H:i', $endTime);
                            $endDate = $regStartOriginal[0] . " " . $endTimeDate;
                        }
                    }
                }
            }
        }
        return new \DateTime($endDate);
    }

    private function getDescription($entry)
    {
        $exploded = explode("Durys atidaromos", $entry->description);
        if (preg_match('#<span style=\"(.*)\">(.*?)</span>#', $exploded[0], $match)) {
            $withoutColored = preg_replace('#<span style=\"(.*)\">(.*?)</span>#', '', $exploded[0]);
            return $withoutColored;
        } else {
            return $exploded[0];
        }
    }

    /**
     * adds new events and their keywords to database
     *
     * @param $name
     * @param $startDate
     * @param $endDate
     * @param $description
     * @param $keywords
     * @internal param $title
     */
    private function addToDatabase($name, $startDate, $endDate, $description, $keywords)
{'fraction_non_alphanumeric': 0.14426821160604045, 'fraction_numerical': 0.0031342007788014055, 'mean_word_length': 2.142345568487019, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 78, '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': '2006541', 'n_tokens_mistral': 3177, 'n_tokens_neox': 3116, 'n_words': 607}