/** * @Route("/market/book/add" , name="market_book_add" ) */ public function addBookAction() { $MarketManager = $this->isAuth(); if (!$this->isAuth()) { return $this->redirect($this->generateUrl("market_login")); } if ($MarketManager) { $categories = $this->getDoctrine()->getRepository('EvrikaMainBundle:MarketCategory')->findByMarketUser($this->auth); $msg = null; $request = $this->getRequest(); if ($request->isMethod('POST')) { $em = $this->getDoctrine()->getManager(); $book = new Market(); $book->setCurrency('RUB'); $book->setName($request->get('name')); $book->setAuthor($request->get('author')); $book->setPublicHouse($request->get('publicHouse')); $book->setLink($request->get('link')); $book->setOfferId($request->get('offerId')); $book->setCategory($this->getDoctrine()->getRepository('EvrikaMainBundle:MarketCategory')->findOneById($request->get('category'))); $book->setCena($request->get('cena')); $book->setIsbn($request->get('isbn')); $book->setCountList($request->get('countList')); $book->setYear($request->get('year')); $book->setMarketUser($MarketManager); $book->setEnabled($request->get('enabled')); $book->setDescription($request->get('description')); $book->setType('book'); if ($request->files->get('image')) { $img = $request->files->get('image'); $origName = $img->getClientOriginalName(); $format = substr($origName, strrpos($origName, '.')); if ($format == '.png' || $format == '.jpg') { if (!is_dir('upload/products')) { mkdir('upload/products', 0775); } $name = substr($origName, 0, strlen($origName) - 4); $name .= '_' . time() . $format; $img->move('upload/products/', $name); $book->setImageURL('http://evrika.ru/upload/products/' . $name); } else { $msg = 'Ошибка файла. Неподдерживаемый формат файла'; return $this->redirect($this->generateUrl('market_book_add'), array('book' => $book, 'categories' => $categories, 'auth' => $MarketManager, 'msg' => $msg)); } } $em->persist($book); $em->flush(); return $this->redirect($this->generateUrl("market_list")); } return $this->render('EvrikaMainBundle:Market:product_book_.html.twig', array('categories' => $categories, 'auth' => $MarketManager, 'msg' => $msg)); } }
protected function execute(InputInterface $input, OutputInterface $output) { if ($input->getArgument('type') == 'book') { $html = file_get_contents("http://www.medknigaservis.ru/ervr/medknigaservis.xml"); $html = mb_convert_encoding($html, 'HTML-ENTITIES', "UTF-8"); $html = iconv("windows-1251", "utf-8", $html); $saw = new \nokogiri($html); $date = new \DateTime(); $xml = $saw->get('offer')->toArray(); /** * Делаем поле enabled для всех книжек от этого издательства = 0 */ // $mu = $this // ->getContainer() // ->get('doctrine') // ->getEntityManager() // ->getRepository('EvrikaMainBundle:marketUser') // ->findOneById(1); # $mu это MarketUser $mu = 1; $result = $this->getContainer()->get('doctrine')->getEntityManager()->createQuery('UPDATE EvrikaMainBundle:Market m SET m.enabled = false WHERE m.marketUser = :mu')->setParameter('mu', $mu)->execute(); if ($result !== true) { $output->writeln('ERROR : UPDATE enabled = false is Error'); } for ($i = 0; $i < 3500; $i++) { if (isset($xml[$i])) { if (isset($xml[$i]['categoryid']['0']['#text']['0'])) { $category = $this->getContainer()->get('doctrine')->getEntityManager()->getRepository('EvrikaMainBundle:MarketCategory')->findOneByOfferId($xml[$i]['categoryid']['0']['#text']['0']); } else { $category = NULL; } $name = isset($xml[$i]['name']['0']['#text']['0']) ? $xml[$i]['name']['0']['#text']['0'] : ''; $offerid = isset($xml[$i]['id']) ? $xml[$i]['id'] : ''; $author = isset($xml[$i]['author']['0']['#text']['0']) ? $xml[$i]['author']['0']['#text']['0'] : ''; $cena = isset($xml[$i]['price']['0']['#text']['0']) ? $xml[$i]['price']['0']['#text']['0'] : ''; $publisher = isset($xml[$i]['publisher']['0']['#text']['0']) ? $xml[$i]['publisher']['0']['#text']['0'] : ''; $year = isset($xml[$i]['year']['0']['#text']['0']) ? $xml[$i]['year']['0']['#text']['0'] : ''; $isbn = isset($xml[$i]['isbn']['0']['#text']['0']) ? $xml[$i]['isbn']['0']['#text']['0'] : ''; $page_extent = isset($xml[$i]['page_extent']['0']['#text']['0']) ? $xml[$i]['page_extent']['0']['#text']['0'] : ''; $description = isset($xml[$i]['description']) ? $this->arrToStr($xml[$i]['description']) : ''; $url = isset($xml[$i]['url']['0']['#text']['0']) ? $xml[$i]['url']['0']['#text']['0'] : ''; $currency = isset($xml[$i]['currencyid']['0']['#text']['0']) ? $xml[$i]['currencyid']['0']['#text']['0'] : ''; $picture = isset($xml[$i]['picture']['0']['#text']['0']) ? $xml[$i]['picture']['0']['#text']['0'] : ''; $marketUser = $this->getContainer()->get('doctrine')->getEntityManager()->getRepository('EvrikaMainBundle:MarketUser')->findOneByid(1); $book = $this->getContainer()->get('doctrine')->getEntityManager()->getRepository('EvrikaMainBundle:Market')->findOneByOfferId($offerid); $action = 'UPDATE'; $em = $this->getContainer()->get('doctrine')->getEntityManager(); if ($book == NULL) { $action = 'INSERT'; $book = new Market(); $book->setOfferId($offerid); $book->setType('book'); $book->setName($name); $book->setCategory($category); $book->setAuthor($author); $book->setPublicHouse($publisher); $book->setYear($year); $book->setIsbn($isbn); $book->setCountList($page_extent); $book->setDescription($description); $book->setLink($url); $book->setImageURL($picture); $book->setCurrency($currency); $book->setCountList($page_extent); $book->setCena($cena); $book->setEnabled('true'); $book->setUpdated($date); $book->setMarketUser($marketUser); $em->persist($book); } else { $book->setCena($cena); $book->setEnabled('true'); $book->setUpdated($date); } $book->setEnabled(1); $em->flush(); $output->writeln($offerid . ' : ' . $action . ' ----------- OK'); } else { $output->writeln('ERROR : NOT FOUND'); } } $result = $this->getContainer()->get('doctrine')->getEntityManager()->createQuery('DELETE FROM EvrikaMainBundle:Market m WHERE m.enabled = false AND m.marketUser = :mu')->setParameter('mu', $mu)->execute(); if ($result !== true) { $output->writeln('ERROR : DELETE old items'); } } else { $output->writeln('ERROR : INVALID ARGUMENT'); } }