<?php
namespace  Offy\Bundle\DashboardBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use FOS\RestBundle\Controller\Annotations\View;
use Offy\Bundle\DashboardBundle\Document\OpenPosition;
use Offy\Bundle\DashboardBundle\Document\Job;
class PositionController extends Controller
{
    /**
     * 
     * @return array
     * @View()
     */
    public function getPositionAction()
    {
        $dm = $this->get('doctrine_mongodb')->getManager();
        //reset job open positions
        $openList = $dm->getRepository('OffyDashboardBundle:Job')->findAllJobOpen();
        foreach ($openList as $open)
        {
            $open->setNumberOpen(0);
            $dm->flush();
        }
        $repository = $this->get('doctrine_mongodb')
                ->getManager()
                ->getRepository('OffyDashboardBundle:OpenPosition');
        $openPositions = $repository->findAll();
        
        foreach ($openPositions as $position)
        {
            $a = new OpenPosition();
            $a =  $position;
            $id = $a->getJobId();
           
            $openJob = $dm->getRepository('OffyDashboardBundle:Job')->findOneBy(array("job_id"=>$id));
            if (!$openJob) {
                throw $this->createNotFoundException('No job found for id '.$id);
            }
            $numberOpen = $openJob->getNumberOpen();
            $numberOpen = $numberOpen + 1;
            $openJob->setNumberOpen($numberOpen);
            $dm->flush();
        }
        $openList = $dm->getRepository('OffyDashboardBundle:Job')->findAllJobOpen();
        $results = array();
        foreach ($openList as $open)
        {
            $results[] = $open;
        }
        
        return array("results" => true, "error" => "ok", "data" => $results);
    }
}