/** * New items * * @param \AnimeDb\Bundle\CatalogBundle\Entity\Item $item * @param \Symfony\Component\HttpFoundation\Request $request * * @return \Symfony\Component\HttpFoundation\Response */ public function indexAction(Item $item, Request $request) { /* @var $response \Symfony\Component\HttpFoundation\Response */ $response = $this->get('cache_time_keeper')->getResponse($item->getDateUpdate(), self::CACHE_LIFETIME); /* @var $widget \AnimeDb\Bundle\ShikimoriWidgetBundle\Service\Widget */ $widget = $this->get('anime_db.shikimori.widget'); // get shikimori item id if (!($item_id = $widget->getItemId($item))) { return $response; } $list = $this->get('anime_db.shikimori.browser')->get(str_replace('#ID#', $item_id, self::PATH_RELATED_ITEMS)); $list = $this->filter($list); $response->setEtag($this->hash($list)); // response was not modified for this request if ($response->isNotModified($request) || !$list) { return $response; } // build list item entities foreach ($list as $key => $item) { $list[$key] = $widget->getWidgetItem($widget->getItem($item['anime']['id'])); // add relation if (substr($this->container->getParameter('locale'), 0, 2) == 'ru') { $list[$key]->setName($list[$key]->getName() . ' (' . $item['relation_russian'] . ')'); } else { $list[$key]->setName($list[$key]->getName() . ' (' . $item['relation'] . ')'); } } return $this->render('AnimeDbShikimoriRelatedItemsWidgetBundle:Widget:index.html.twig', ['items' => $list], $response); }
/** * @param ItemEntity $item * * @return ItemEntity[] */ public function findDuplicate(ItemEntity $item) { // get all names $names = [$item->getName()]; foreach ($item->getNames() as $name) { $names[] = $name->getName(); } // find from item main name $duplicate = $this->getEntityManager()->createQuery(' SELECT i FROM AnimeDbCatalogBundle:Item i WHERE i.name IN (:names) ')->setParameter(':names', $names)->getResult(); // find frim item other names $item_names = $this->getEntityManager()->createQuery(' SELECT n FROM AnimeDbCatalogBundle:Name n WHERE n.name IN (:names) GROUP BY n.item ')->setParameter(':names', $names)->getResult(); foreach ($item_names as $item_name) { // element has been added foreach ($duplicate as $item) { if ($item === $item_name->getItem()) { continue 2; } } $duplicate[] = $item_name->getItem(); } return $duplicate; }
/** * New items * * @param \AnimeDb\Bundle\CatalogBundle\Entity\Item $item * @param \Symfony\Component\HttpFoundation\Request $request * * @return \Symfony\Component\HttpFoundation\Response */ public function indexAction(Item $item, Request $request) { /* @var $response \Symfony\Component\HttpFoundation\Response */ $response = $this->get('cache_time_keeper')->getResponse($item->getDateUpdate(), self::CACHE_LIFETIME); /* @var $widget \AnimeDb\Bundle\ShikimoriWidgetBundle\Service\Widget */ $widget = $this->get('anime_db.shikimori.widget'); // get shikimori item id if (!($item_id = $widget->getItemId($item))) { return $response; } $list = $this->get('anime_db.shikimori.browser')->get(str_replace('#ID#', $item_id, self::PATH_SIMILAR_ITEMS)); // add Etag by list items $response->setEtag($widget->hash($list)); // response was not modified for this request if ($response->isNotModified($request) || !$list) { return $response; } // build list item entities foreach ($list as $key => $item) { $list[$key] = $widget->getWidgetItem($widget->getItem($item['id'])); } return $this->render('AnimeDbShikimoriSimilarItemsWidgetBundle:Widget:index.html.twig', ['items' => $list], $response); }
/** * @param Item $item * * @return Name */ public function setItem(Item $item = null) { if ($this->item !== $item) { // romove link on this item for old item if ($this->item instanceof Item) { $tmp = $this->item; $this->item = null; $tmp->removeName($this); } $this->item = $item; // add link on this item if ($item instanceof Item) { $this->item->addName($this); } } return $this; }
/** * @param Item $item * * @return string */ public function getSourceForFill(Item $item) { /* @var $source Source */ foreach ($item->getSources() as $source) { if (strpos($source->getUrl(), $this->browser->getHost()) === 0) { return $source->getUrl(); } } return ''; }
/** * @param Item $item * @param array $body * * @return Item */ public function setImages(Item $item, $body) { $images = $this->browser->get(str_replace('#ID#', $body['id'], self::FILL_IMAGES_URL)); foreach ($images as $image) { if ($path = parse_url($image['original'], PHP_URL_PATH)) { $image = new Image(); $image->setSource(self::NAME . '/' . $body['id'] . '/' . pathinfo($path, PATHINFO_BASENAME)); if ($this->uploadImage($this->browser->getHost() . $image['original'], $image)) { $item->addImage($image); } } } return $item; }
/** * Get info filename * * @param \AnimeDb\Bundle\CatalogBundle\Entity\Item $item * * @return string */ protected function getInfo(ItemEntity $item) { $filename = self::INFO_FILENAME . '.html'; if (is_file($item->getPath())) { return dirname($item->getPath()) . pathinfo($item->getPath(), PATHINFO_FILENAME) . '-' . $filename; } else { return $item->getPath() . '/' . $filename; } }
/** * Get the field refill attributes. * * @param string $field * @param ItemEntity|null $item * * @return array */ protected function getRefillAttr($field, ItemEntity $item = null) { // item exists and can be refilled if ($item instanceof ItemEntity && $item->getName() && ($plugins = $this->chain->getPluginsThatCanFillItem($item, $field))) { /* @var $plugin RefillerInterface */ foreach ($plugins as $key => $plugin) { $plugins[$key] = ['name' => $plugin->getName(), 'title' => $plugin->getTitle(), 'can_refill' => $plugin->isCanRefill($item, $field)]; } return ['data-type' => 'refill', 'data-plugins' => $this->templating->render('AnimeDbCatalogBundle:Form:refillers.html.twig', ['item' => $item, 'field' => $field, 'plugins' => $plugins])]; } return []; }
/** * Try to find the MyAnimeList id for the item * * @param ItemCatalog $item * * @return int|null */ protected function findIdForItem(ItemCatalog $item) { // find name for search $query = ''; if (preg_match('/[a-z]+/i', $item->getName())) { $query = $item->getName(); } else { /* @var $name Name */ foreach ($item->getNames() as $name) { if (preg_match('/[a-z]+/i', $name->getName())) { $query = $name->getName(); break; } } } // try search return $query ? $this->client->search($query) : null; }
/** * @param CatalogItem $item * * @return Item|null */ public function findByCatalogItem(CatalogItem $item) { return $this->createQueryBuilder('i')->where('i.item_id = :id')->setParameter(':id', $item->getId())->getQuery()->getOneOrNullResult(); }
/** * Get source for fill * * @param \AnimeDb\Bundle\CatalogBundle\Entity\Item $item * * @return string */ public function getSourceForFill(Item $item) { /* @var $source \AnimeDb\Bundle\CatalogBundle\Entity\Source */ foreach ($item->getSources() as $source) { if (strpos($source->getUrl(), $this->browser->getHost()) === 0) { return $source->getUrl(); } } return ''; }
/** * Get form for field. * * @param string $field * @param Item $item_origin * @param Item $item_fill * * @return Form */ protected function getForm($field, Item $item_origin, Item $item_fill) { switch ($field) { case RefillerInterface::FIELD_DATE_END: $form = new DateEndForm(); $data = ['date_end' => $item_fill->getDateEnd()]; break; case RefillerInterface::FIELD_DATE_PREMIERE: $form = new DatePremiereForm(); $data = ['date_premiere' => $item_fill->getDatePremiere()]; break; case RefillerInterface::FIELD_DURATION: $form = new DurationForm(); $data = ['duration' => $item_fill->getDuration()]; break; case RefillerInterface::FIELD_EPISODES: $form = new EpisodesForm(); $data = ['episodes' => $item_fill->getEpisodes()]; break; case RefillerInterface::FIELD_EPISODES_NUMBER: $form = new EpisodesNumberForm(); $data = ['episodes_number' => $item_fill->getEpisodesNumber()]; break; case RefillerInterface::FIELD_FILE_INFO: $form = new FileInfoForm(); $data = ['file_info' => $item_fill->getFileInfo()]; break; case RefillerInterface::FIELD_GENRES: $form = $this->get('anime_db.form.type.refill.gengres'); $data = ['genres' => $item_fill->getGenres()]; break; case RefillerInterface::FIELD_IMAGES: $form = new ImagesForm(); $data = ['images' => $item_fill->getImages()]; break; case RefillerInterface::FIELD_COUNTRY: $form = $this->get('anime_db.form.type.refill.country'); $data = ['country' => $item_fill->getCountry()]; break; case RefillerInterface::FIELD_NAMES: $form = new NamesForm(); $data = ['names' => $item_fill->getNames()]; break; case RefillerInterface::FIELD_SOURCES: $form = new SourcesForm(); $data = ['sources' => $item_fill->getSources()]; break; case RefillerInterface::FIELD_SUMMARY: $form = new SummaryForm(); $data = ['summary' => $item_fill->getSummary()]; break; case RefillerInterface::FIELD_TRANSLATE: $form = new TranslateForm(); $data = ['translate' => $item_fill->getTranslate()]; break; case RefillerInterface::FIELD_STUDIO: $form = $this->get('anime_db.form.type.refill.studio'); $data = ['studio' => $item_fill->getStudio()]; break; default: throw $this->createNotFoundException('Field \'' . $field . '\' is not supported'); } // search new source link /* @var $sources_origin Source[] */ $sources_origin = array_reverse($item_origin->getSources()->toArray()); /* @var $sources_fill Source[] */ $sources_fill = array_reverse($item_fill->getSources()->toArray()); foreach ($sources_fill as $source_fill) { // sources is already added foreach ($sources_origin as $source_origin) { if ($source_fill->getUrl() == $source_origin->getUrl()) { continue 2; } } $data['source'] = $source_fill->getUrl(); break; } return $this->createForm($form, $data); }
/** * @dataProvider getUrlNames * * @param string $name * @param string $expected */ public function testGetUrlName($name, $expected) { $this->item->setName($name); $this->assertEquals($expected, $this->item->getUrlName()); }
/** * Fill names for Cinema type * * @param \AnimeDb\Bundle\CatalogBundle\Entity\Item $item * @param \DOMXPath $xpath * @param \DOMElement $head * * @return \AnimeDb\Bundle\CatalogBundle\Entity\Item */ protected function fillCinemaNames(Item $item, \DOMXPath $xpath, \DOMElement $head) { // get list names $names = []; foreach ($xpath->query('table[1]/tr/td/table/tr/td', $head) as $name) { $names[] = $name->nodeValue; } // clear $name = preg_replace('/\\[\\d{4}\\]/', '', array_shift($names)); // example: [2011] $name = preg_replace('/\\[?(ТВ|OVA|ONA)(\\-\\d)?\\]?/', '', $name); // example: [TV-1] $name = preg_replace('/\\(фильм \\w+\\)/u', '', $name); // example: (фильм седьмой) $item->setName(trim($name)); // add other names foreach ($names as $name) { $name = trim(preg_replace('/(\\(\\d+\\))/', '', $name)); $item->addName((new Name())->setName($name)); } return $item; }
/** * @param Item $item * @param Request $request * * @return Response */ public function showAction(Item $item, Request $request) { $date = [$item->getDateUpdate()]; // use storage update date if ($item->getStorage() instanceof Storage) { $date[] = $item->getStorage()->getDateUpdate(); } $response = $this->getCacheTimeKeeper()->getResponse($date); // response was not modified for this request if ($response->isNotModified($request)) { return $response; } return $this->render('AnimeDbCatalogBundle:Item:show.html.twig', ['item' => $item, 'widget_bottom' => self::WIDGET_PALCE_BOTTOM, 'widget_in_content' => self::WIDGET_PALCE_IN_CONTENT, 'widget_right' => self::WIDGET_PALCE_RIGHT], $response); }
/** * @param Item $item * @param Crawler $body * * @return Item */ public function setGenres(Item $item, Crawler $body) { $repository = $this->doctrine->getRepository('AnimeDbCatalogBundle:Genre'); $categories = $body->filter('categories > category > name'); foreach ($categories as $category) { if (isset($this->category_to_genre[$category->nodeValue])) { $genre = $repository->findOneBy(['name' => $this->category_to_genre[$category->nodeValue]]); } else { $genre = $repository->findOneBy(['name' => $category->nodeValue]); } if ($genre instanceof Genre) { $item->addGenre($genre); } } return $item; }
/** * Get Shikimori item id from sources * * @param \AnimeDb\Bundle\CatalogBundle\Entity\Item $item * * @return integer */ public function getItemId(Item $item) { /* @var $source \AnimeDb\Bundle\CatalogBundle\Entity\Source */ foreach ($item->getSources() as $source) { if (strpos($source->getUrl(), $this->browser->getHost()) === 0 && preg_match(self::REG_ITEM_ID, $source->getUrl(), $match)) { return (int) $match['id']; } } return 0; }