/** * @Route( * path = "/insert/insertByUPC/{upc}", * name = "insertByUPC", * defaults={"upc" = "012345678901"} * ) */ public function insertByUPCAction(Request $request, $upc) { /* TODO: DISPLAYING A MESSAGE WHEN UPC VALUE HAS NOT BEEN FOUND */ /* DISPLAYING THE INPUT FORM */ $upcQuery = array('upc' => ''); $form = $this->createFormBuilder($upcQuery)->setAction($this->generateUrl('insertByUPC'))->setMethod('GET')->add('upc', TextType::class)->add('save', SubmitType::class, array('label' => 'UPC search and possible insertion'))->getForm(); $form->handleRequest($request); if ($form->isSubmitted()) { // Form has been submitted or upc has been passed in the URL $data = $form->getData(); if ($data['upc'] != '') { $upc = $data['upc']; } // ... perform some action, such as saving the task to the database //$resString = print_r($data); //return(new Response($resString)); /* searching by UPC, if found creating an object and setting its properties accordingly */ // TODO: // moving configuration data outside in a configuration file $conf = new GenericConfiguration(); $conf->setCountry('com')->setAccessKey('AKIAJD57F37W2KGLXEVQ')->setSecretKey('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')->setAssociateTag('quercusroburn-20')->setRequest('\\ApaiIO\\Request\\Soap\\Request'); $lookup = new Lookup(); $lookup->setIdType('UPC'); //$lookup->setItemId('724384062026'); //$lookup->setItemId('724382912521'); $lookup->setItemId($upc); $lookup->setResponseGroup(array('ItemAttributes', 'Images', 'Tracks')); // More detailed information $apaiIo = new ApaiIO($conf); $response = $apaiIo->runOperation($lookup); $logger = $this->get('logger'); // Cozy variable to shorten object invocation $item = $response->Items->Item; // TODO: // refining the way to decide if a result has been found if (property_exists($item->ItemAttributes, 'UPC')) { /* Item with that UPC has been found */ $newRecord = new Record(); $newRecord->setUpc($item->ItemAttributes->UPC); $newRecord->setAsin($item->ASIN); $newRecord->setCoverImageUrl($item->LargeImage->URL); if (property_exists($item->ItemAttributes, 'Brand')) { $newRecord->setRecordLabel($item->ItemAttributes->Brand); } else { $newRecord->setRecordLabel($item->ItemAttributes->Label); } $newRecord->setYear(substr($item->ItemAttributes->ReleaseDate, 0, 4)); $logger->info('Vediamo: ' . json_encode($item)); if (property_exists($item->ItemAttributes, 'Artist')) { if (is_array($item->ItemAttributes->Artist)) { // MEMO // "implode" and "join" are the same, they are alias $newRecord->setArtist(implode(', ', $item->ItemAttributes->Artist)); } else { $newRecord->setArtist($item->ItemAttributes->Artist); } } else { if (property_exists($item->ItemAttributes, 'Creator')) { // UPC: 828767523224 // ASIN: B000C6NONM // Fabrizio De André, "In direzione ostinata e contraria" $newRecord->setArtist($item->ItemAttributes->Creator->_); // "Creator":{"_":"Fabrizio De Andr\u00e9","Role":"Performer"}, // TODO // Porting this mechanism in searchByText } else { // Couldn't extract artist info } } $newRecord->setTitle($item->ItemAttributes->Title); $newRecord->setMediaType($item->ItemAttributes->Binding); $newRecord->setMediaCount($item->ItemAttributes->NumberOfItems); // each media item has a track list $trackLists = array(); if (property_exists($item, 'Tracks')) { // Having the number of items does not mean tracks info is available... // When number of media/items is greater than 1, but // the tracks list is just one // (it happened at least once with B000B66OVW, see EX03 below) // then ->Tracks->Disc is not an array at it should be // when more than one media are actually present $oneListOnly = 0; if (!is_array($item->Tracks->Disc)) { $logger->info('The web service reported ' . $newRecord->getMediaCount() . ' media, but all tracks are in just one list.'); $oneListOnly = 1; } if ($item->ItemAttributes->NumberOfItems < 2 || $oneListOnly) { //$newRecord->setMediaCount(1); // One item only, one list, see EX00 at the bottom $totalTracks = sizeof($item->Tracks->Disc->Track); for ($i = 0; $i < $totalTracks; $i++) { $tracksLists[0][] = $item->Tracks->Disc->Track[$i]->_; } } else { // More than one media (EX01 ath the bottom) for ($j = 0; $j < $item->ItemAttributes->NumberOfItems; $j++) { $totalTracks = sizeof($item->Tracks->Disc[$j]->Track); for ($i = 0; $i < $totalTracks; $i++) { $tracksLists[$j][] = $item->Tracks->Disc[$j]->Track[$i]->_; } } } } else { // Tracks info is not available (no property "Tracks" in the object) $logger->info('Tracks info is not available'); } $newRecord->setTracksLists($tracksLists); $em = $this->getDoctrine()->getManager(); $em->persist($newRecord); $em->flush(); return $this->redirectToRoute('record_show', array('id' => $newRecord->getId())); } else { // TODO: DISPLAYING message telling the upc has not been found return new Response('upc not found'); } } else { // Form has not been submitted and UPC has not been passed via URL return $this->render('AppBundle:Record:insertByUPC.html.twig', array('form' => $form->createView())); } }