Ejemplo n.º 1
0
 public function import(Destination $dest, $month, $year)
 {
     $url = 'http://www.machupicchu.gob.pe/rpt//DisponibilidadPorMes.cfm?idLugar=' . $dest->getId() . '&mes=' . $month . '&ano=' . $year;
     $fn = __DIR__ . '/d.pdf';
     $return = true;
     file_put_contents($fn, file_get_contents($url));
     $pdf = new Pdf($fn);
     $paragraphs = $pdf->html()->find('p.ft03');
     $ret = array();
     $curr = 0;
     foreach ($paragraphs as $i => $p) {
         if ($i == 0) {
             continue;
         }
         if ($i % 2 != 0) {
             $curr = $p->text();
         } else {
             $ret[$curr] = $p->text();
         }
     }
     if (empty($ret)) {
         $return = false;
         $date = Carbon::createFromDate($year, $month, 1);
         $lastDayofMonth = $date->format('t') + 1;
         $ret = array();
         for ($i = 1; $i < $lastDayofMonth; $i++) {
             $ret[$i] = 0;
         }
     }
     echo '<pre>';
     var_dump($ret);
     foreach ($ret as $day => $availability) {
         $date = Carbon::createFromDate($year, $month, $day);
         $e = $this->getContainer()->get('doctrine')->getRepository('AppBundle:TourDate')->findOneBy(array('date' => $date, 'destination' => $dest));
         if (empty($e)) {
             $e = new TourDate();
             $e->setDestination($dest);
             $e->setDate($date);
         }
         $e->setAvailability($availability);
         $this->getContainer()->get('doctrine')->getEntityManager()->persist($e);
         $this->getContainer()->get('doctrine')->getEntityManager()->flush();
     }
     return $return;
 }