<commit_before><?php
namespace AppBundle\Controller;

use AppBundle\Entity\Plant;
use AppBundle\Form\PlantHarvestType;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Common\Persistence\ManagerRegistry;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

class PlantController extends Controller
{
    public function indexAction(EntityManagerInterface $em, $_route)
    {
        $qb = $em->createQueryBuilder('p');
        $plantQ = $qb->addSelect('SUM(p.areaCapacity) AS seedling_total')
            ->addSelect('ANY_VALUE(p.id) AS id')
            ->addSelect('COUNT(p.area) AS area_count')
            ->addSelect('ANY_VALUE(sc.name) AS seed_category')
            ->addSelect('s AS seed')
            ->from('AppBundle:Plant', 'p')
            ->innerJoin('AppBundle:Seed', 's', 'WITH', 'p.seed = s.id')
            ->innerJoin('AppBundle:Area', 'a', 'WITH', 'p.area = a.id')
            ->innerJoin('AppBundle:SeedCategory', 'sc', 'WITH', 's.seedCategory = sc.id')
            ->groupBy('p.seed')
            ->getQuery();
        $plants = $plantQ->getResult();

        return $this->render('plant/index.html.twig', array(
            'plants' => $plants,
            'classActive' => $_route
        ));
    }

    /**
        Show the form to add new plant and persist it to the database.
    */
    public function createAction(EntityManagerInterface $em, Request $request, $_route)
    {
        $plant = new Plant();
        
        $form = $this->createForm(PlantType::class, $plant);
        $form->handleRequest($request);

        if($form->isSubmitted() && $form->isValid()) {
            // validate the area capacity and seed inventory
            $seedsInfo = $em->getRepository('AppBundle:Plant')->findBy(array('seed' => $plant->getSeed()->getId()));
            $areasInfo = $em->getRepository('AppBundle:Plant')->findBy(array('area' => $plant->getArea()->getId()));
            
            $usedSeed = array_reduce($seedsInfo, function($carry, $item) {
                return $carry += $item->getSeedlingAmount();
            });

            $usedArea = array_reduce($areasInfo, function($carry, $item) {
                return $carry += $item->getAreaCapacity();
            });

            $totalSeed = $usedSeed + $plant->getSeedlingAmount();
            $totalArea = $usedArea + $plant->getAreaCapacity();
            
            if($totalSeed <= $plant->getSeed()->getQuantity() && $totalArea <= $plant->getArea()->getCapacity()) {
                // save to database here
                $plant = $form->getData();
                $plant->setCreatedAt(new \DateTime('now'));
                
                $em->persist($plant);
                $em->flush();

                return $this->redirectToRoute('plants');
            } else {
                // give error message
                $this->addFlash('notice', 'The capacity of the area or the quantity of the seed are insufficient.');
                return $this->redirectToRoute('plants_create');
            }
        }

        return $this->render('plant/create.html.twig', array(
            'form' => $form->createView(),
            'classActive' => $_route
        ));
    }

    /**
        Display the detail of the plant
    */
    public function showAction($id, EntityManagerInterface $em, Request $request, $_route)
    {
        $plants = $em->getRepository('AppBundle:Plant')->findBy(array('seed' => $id));
        
        $plantsWithMeasurementAndDaysAgo = array_map(function($plant) {
            // translate the measurement
            if($plant->getArea()->getMeasurementUnit() == 1) {
                $unit = 'Pots/Points';
                $plant->getArea()->setMeasurementUnit($unit);
            } else if($plant->getArea()->getMeasurementUnit() == 2) {
                $unit = 'Trays';
                $plant->getArea()->setMeasurementUnit($unit);
            }

            // translate the date time object to "days ago" string
            $seedlingDate = date_create($plant->getSeedlingDate()->format('Y-m-d'));
            $currentDate = date_create(date('Y-m-d'));
            $interval = date_diff($currentDate, $seedlingDate);
            $plant->setSeedlingDate($interval->format('%a'));

            return $plant;
        }, $plants);
        
        return $this->render('plant/show.html.twig', array(
            'plants' => $plantsWithMeasurementAndDaysAgo,
            'totalArea' => count($plantsWithMeasurementAndDaysAgo),
            'classActive' => $_route
        ));
    }

    public function harvestAction($id, $_route, EntityManagerInterface $em, Request $request)
    {
        $plant = $em->getRepository('AppBundle:Plant')->find($id);
        
        $form = $this->createForm(PlantHarvestType::class, $plant);
        $form->handleRequest($request);

        if($form->isSubmitted() && $form->isValid()) {
            // when the farmer choose to harvest or dispose his/her plant
            if($plant->getAction() != 'donothing') {
                if($plant->getAction() == 'harvest') {
                    // harvest
                    $plant->setHarvestingDate(new \DateTime('now'));
                    $plant->setDisposingDate(null);
                } elseif($plant->getAction() == 'dispose') {
                    // dispose
                    $plant->setDisposingDate(new \DateTime('now'));
                    $plant->setHarvestingDate(null);
                }

                $plant = $form->getData();
                $plant->setUpdatedAt(new \DateTime('now'));
            } else {
                // do nothing
                $plant->setHarvestingDate(null);
                $plant->setDisposingDate(null);
            }

            // save to database here
            $em->persist($plant);
            $em->flush();

            return $this->redirectToRoute('plants_show', array('id' => $plant->getSeed()->getId()));
        }

        return $this->render('plant/harvest.html.twig', array(
            'classActive' => $_route,
            'form' => $form->createView(),
            'plant' => $plant,
            'seedlingDate' => $plant->getSeedlingDate()->format('d-M-Y')
        ));
    }
}
<commit_msg>Fixing the create new plant form type. I forgot to give use statement.<commit_after><?php
namespace AppBundle\Controller;

use AppBundle\Entity\Plant;
use AppBundle\Form\PlantHarvestType;
use AppBundle\Form\PlantType;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Common\Persistence\ManagerRegistry;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

class PlantController extends Controller
{
    public function indexAction(EntityManagerInterface $em, $_route)
    {
        $qb = $em->createQueryBuilder('p');
        $plantQ = $qb->addSelect('SUM(p.areaCapacity) AS seedling_total')
            ->addSelect('ANY_VALUE(p.id) AS id')
            ->addSelect('COUNT(p.area) AS area_count')
            ->addSelect('ANY_VALUE(sc.name) AS seed_category')
            ->addSelect('s AS seed')
            ->from('AppBundle:Plant', 'p')
            ->innerJoin('AppBundle:Seed', 's', 'WITH', 'p.seed = s.id')
            ->innerJoin('AppBundle:Area', 'a', 'WITH', 'p.area = a.id')
            ->innerJoin('AppBundle:SeedCategory', 'sc', 'WITH', 's.seedCategory = sc.id')
            ->groupBy('p.seed')
            ->getQuery();
        $plants = $plantQ->getResult();

        return $this->render('plant/index.html.twig', array(
            'plants' => $plants,
            'classActive' => $_route
        ));
    }

    /**
        Show the form to add new plant and persist it to the database.
    */
    public function createAction(EntityManagerInterface $em, Request $request, $_route)
    {
        $plant = new Plant();
        
        $form = $this->createForm(PlantType::class, $plant);
        $form->handleRequest($request);

        if($form->isSubmitted() && $form->isValid()) {
            // validate the area capacity and seed inventory
            $seedsInfo = $em->getRepository('AppBundle:Plant')->findBy(array('seed' => $plant->getSeed()->getId()));
            $areasInfo = $em->getRepository('AppBundle:Plant')->findBy(array('area' => $plant->getArea()->getId()));
            
            $usedSeed = array_reduce($seedsInfo, function($carry, $item) {
                return $carry += $item->getSeedlingAmount();
            });

            $usedArea = array_reduce($areasInfo, function($carry, $item) {
                return $carry += $item->getAreaCapacity();
            });

            $totalSeed = $usedSeed + $plant->getSeedlingAmount();
            $totalArea = $usedArea + $plant->getAreaCapacity();
            
            if($totalSeed <= $plant->getSeed()->getQuantity() && $totalArea <= $plant->getArea()->getCapacity()) {
                // save to database here
                $plant = $form->getData();
                $plant->setCreatedAt(new \DateTime('now'));
                
                $em->persist($plant);
                $em->flush();

                return $this->redirectToRoute('plants');
            } else {
                // give error message
                $this->addFlash('notice', 'The capacity of the area or the quantity of the seed are insufficient.');
                return $this->redirectToRoute('plants_create');
            }
        }

        return $this->render('plant/create.html.twig', array(
            'form' => $form->createView(),
            'classActive' => $_route
        ));
    }

    /**
        Display the detail of the plant
    */
    public function showAction($id, EntityManagerInterface $em, Request $request, $_route)
    {
        $plants = $em->getRepository('AppBundle:Plant')->findBy(array('seed' => $id));
        
        $plantsWithMeasurementAndDaysAgo = array_map(function($plant) {
            // translate the measurement
            if($plant->getArea()->getMeasurementUnit() == 1) {
                $unit = 'Pots/Points';
                $plant->getArea()->setMeasurementUnit($unit);
            } else if($plant->getArea()->getMeasurementUnit() == 2) {
                $unit = 'Trays';
                $plant->getArea()->setMeasurementUnit($unit);
            }

            // translate the date time object to "days ago" string
            $seedlingDate = date_create($plant->getSeedlingDate()->format('Y-m-d'));
            $currentDate = date_create(date('Y-m-d'));
            $interval = date_diff($currentDate, $seedlingDate);
            $plant->setSeedlingDate($interval->format('%a'));

            return $plant;
        }, $plants);
        
        return $this->render('plant/show.html.twig', array(
            'plants' => $plantsWithMeasurementAndDaysAgo,
            'totalArea' => count($plantsWithMeasurementAndDaysAgo),
            'classActive' => $_route
        ));
    }

    public function harvestAction($id, $_route, EntityManagerInterface $em, Request $request)
    {
        $plant = $em->getRepository('AppBundle:Plant')->find($id);
        
        $form = $this->createForm(PlantHarvestType::class, $plant);
        $form->handleRequest($request);

        if($form->isSubmitted() && $form->isValid()) {
            // when the farmer choose to harvest or dispose his/her plant
            if($plant->getAction() != 'donothing') {
                if($plant->getAction() == 'harvest') {
                    // harvest
                    $plant->setHarvestingDate(new \DateTime('now'));
                    $plant->setDisposingDate(null);
                } elseif($plant->getAction() == 'dispose') {
                    // dispose
                    $plant->setDisposingDate(new \DateTime('now'));
                    $plant->setHarvestingDate(null);
                }

                $plant = $form->getData();
                $plant->setUpdatedAt(new \DateTime('now'));
            } else {
                // do nothing
                $plant->setHarvestingDate(null);
                $plant->setDisposingDate(null);
            }

            // save to database here
            $em->persist($plant);
            $em->flush();

            return $this->redirectToRoute('plants_show', array('id' => $plant->getSeed()->getId()));
        }

        return $this->render('plant/harvest.html.twig', array(
            'classActive' => $_route,
            'form' => $form->createView(),
            'plant' => $plant,
            'seedlingDate' => $plant->getSeedlingDate()->format('d-M-Y')
        ));
    }
}

{'fraction_non_alphanumeric': 0.15627200876437905, 'fraction_numerical': 0.0003130135378355114, 'mean_word_length': 2.092184853617227, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 201, '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': '7140608', 'n_tokens_mistral': 3596, 'n_tokens_neox': 3473, 'n_words': 636}