Exemple #1
0
 /**
  * @Route("/record/anonymous", name="createRecordAnonymously")
  * @Method("POST")
  */
 public function createAnonymouslyAction(Request $request)
 {
     // user anonymous
     $repository = $this->getDoctrine()->getRepository('AppBundle:User');
     $count = $repository->count();
     $username = '******' . dechex($count);
     $password = chr(rand(65, 90)) . chr(rand(65, 90)) . chr(rand(65, 90)) . chr(rand(65, 90)) . chr(rand(65, 90));
     $user = new User();
     $user->setUsername($username);
     $user->setPassword(md5($password));
     $em = $this->getDoctrine()->getManager();
     $em->persist($user);
     $em->flush();
     // record
     $content = $request->getContent();
     $response = json_decode($content, true);
     if ($user != null) {
         $record = new Record();
         $record->setScore($response['score']);
         $record->setUserId($user->getId());
         $em = $this->getDoctrine()->getManager();
         $em->persist($record);
         $em->flush();
         return new JsonResponse(['code' => 1, 'record' => ['id' => $record->getId(), 'score' => $record->getScore(), 'userId' => $record->getUserId()], 'user' => ['username' => $user->getUsername(), 'password' => $password]]);
     } else {
         return new JsonResponse(['code' => 0, 'message' => 'record not save']);
     }
 }
 /**
  * @Route(
  *    path = "/search/searchByText/{text}",
  *    name = "searchByText",
  *    defaults={"text" = "012345678901"}
  * )
  */
 public function searchByTextAction(Request $request)
 {
     /* TODO: DISPLAYING A MESSAGE WHEN NOTHING HAS BEEN FOUND */
     /* DISPLAYING THE INPUT FORM */
     $textQuery = array('text' => '');
     $form = $this->createFormBuilder($textQuery)->setAction($this->generateUrl('searchByText'))->setMethod('GET')->add('text', TextType::class, array('label' => 'Keywords', 'attr' => array('class' => 'form-control')))->add('submit', SubmitType::class, array('label' => 'Look for possible equivalent items', 'attr' => array('class' => 'btn btn-default')))->getForm();
     $form->handleRequest($request);
     if ($form->isSubmitted()) {
         // Form has been submitted or some text has been passed in the URL
         $data = $form->getData();
         if ($data['text'] != '') {
             $text = $data['text'];
         }
         // ... perform some action, such as saving the task to the database
         //$resString = print_r($data);
         //return(new Response($resString));
         /*
         searching by text,
         if found displaying what has been found
         */
         // 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');
         $query = new Search();
         $query->setKeywords($text);
         $query->setCategory('Music');
         $query->setResponseGroup(array('ItemAttributes', 'Images', 'Tracks'));
         // More detailed information
         $apaiIo = new ApaiIO($conf);
         $response = $apaiIo->runOperation($query);
         $logger = $this->get('logger');
         $records = array();
         if (property_exists($response, 'Items')) {
             // handlig the case when one item only is returned,
             // in that case $response->Items->Item is not an array!!!
             $results = array();
             if (is_array($response->Items->Item)) {
                 $results = $response->Items->Item;
             } else {
                 $results[] = $response->Items->Item;
             }
             foreach ($results as $item) {
                 $logger->info('----------------------------');
                 $record = new Record();
                 // ASIN
                 $logger->info("ASIN: {$item->ASIN}");
                 $record->setAsin($item->ASIN);
                 // TODO:
                 // now getting UPC just for testing purposes,
                 // this should be stopped...
                 if (property_exists($item->ItemAttributes, 'UPC')) {
                     $logger->info('UPC: ' . $item->ItemAttributes->UPC);
                 } else {
                     $logger->info('NO UPC!');
                 }
                 // Artist
                 // Structure changes if several artists are listed
                 if (property_exists($item->ItemAttributes, 'Artist')) {
                     if (!is_array($item->ItemAttributes->Artist)) {
                         $record->setArtist($item->ItemAttributes->Artist);
                     } else {
                         $logger->info('Here it is the array: ' . print_r($item->ItemAttributes->Artist, true) . ' - Seen as: ' . gettype($item->ItemAttributes->Artist) . ' - size: ' . sizeof($item->ItemAttributes->Artist));
                         $artist = 'Various artists (';
                         for ($i = 1; $i < sizeof($item->ItemAttributes->Artist); $i++) {
                             $artist .= $item->ItemAttributes->Artist[$i];
                             if ($i < sizeof($item->ItemAttributes->Artist) - 1) {
                                 $artist .= ', ';
                             }
                         }
                         $artist .= ')';
                         $record->setArtist($artist);
                         $logger->info('Values: ' . join(' - ', $item->ItemAttributes->Artist));
                     }
                     $logger->info('Artist: ' . $record->getArtist());
                 }
                 // title
                 $record->setTitle($item->ItemAttributes->Title);
                 $logger->info('Title: ' . $record->getTitle());
                 // label
                 if (property_exists($item->ItemAttributes, 'Brand')) {
                     $record->setRecordLabel($item->ItemAttributes->Brand);
                 } else {
                     if (property_exists($item->ItemAttributes, 'Label')) {
                         $record->setRecordLabel($item->ItemAttributes->Label);
                     } else {
                         //
                     }
                 }
                 $logger->info('Label: ' . $record->getRecordLabel());
                 // Year
                 if (property_exists($item->ItemAttributes, 'ReleaseDate')) {
                     $record->setYear(substr($item->ItemAttributes->ReleaseDate, 0, 4));
                     $logger->info('Label: ' . $record->getYear());
                 } else {
                     $logger->info('\'ReleaseDate\' not available, so dunno how to get the year...');
                 }
                 // Media count
                 if (property_exists($item->ItemAttributes, 'NumberOfItems')) {
                     $record->setMediaCount($item->ItemAttributes->NumberOfItems);
                     $logger->info('Media count: ' . $record->getMediaCount());
                 } else {
                     if (property_exists($item->ItemAttributes, 'NumberOfDiscs')) {
                         $record->setMediaCount($item->ItemAttributes->NumberOfDiscs);
                         $logger->info('Media count: ' . $record->getMediaCount());
                     } else {
                         //$logger->info(print_r($item->ItemAttributes, true));
                         $logger->info('Number of media not available. Now what?! Tentatively I guess the value \'1\'.');
                         $record->setMediaCount('1');
                         $item->ItemAttributes->NumberOfItems = '1';
                     }
                 }
                 // Media type
                 $record->setMediaType($item->ItemAttributes->Binding);
                 $logger->info('Media type: ' . $record->getMediaType());
                 // Cover image (it may be unavailable)
                 if (property_exists($item, 'LargeImage')) {
                     $record->setCoverImageUrl($item->LargeImage->URL);
                     $logger->info('Cover image URL: ' . $record->getCoverImageUrl());
                 }
                 // each media item has a track list
                 $trackLists = array();
                 if (property_exists($item, 'Tracks')) {
                     // Tracks info is not for granted...
                     // 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 ' . $record->getMediaCount() . ' media, but all tracks are in just one list.');
                         $oneListOnly = 1;
                     }
                     if ($record->getMediaCount() < 2 || $oneListOnly) {
                         // one item only, one list
                         if (property_exists($item->Tracks, 'Disc')) {
                             $totalTracks = sizeof($item->Tracks->Disc->Track);
                             for ($i = 0; $i < $totalTracks; $i++) {
                                 $tracksLists[0][] = $item->Tracks->Disc->Track[$i]->_;
                             }
                         } else {
                             $logger->info('Couldn\'t find "Disc"...   ' . print_r($item->Tracks, true));
                         }
                     } else {
                         for ($j = 0; $j < $record->getMediaCount(); $j++) {
                             $totalTracks = sizeof($item->Tracks->Disc[$j]->Track);
                             for ($i = 0; $i < $totalTracks; $i++) {
                                 $tracksLists[$j][] = $item->Tracks->Disc[$j]->Track[$i]->_;
                             }
                         }
                     }
                     $record->setTracksLists($tracksLists);
                 } else {
                     // Tracks info is not available (no property "Tracks" in the object)
                     $logger->info('Tracks info is not available');
                 }
                 // Appending the record to the list of possible records
                 $records[] = $record;
             }
         }
         // TODO:
         // saving the array of results in session
         $session = new Session();
         //$session->start();
         $session->set('results', $records);
         $logger->info('Session ID: ' . $session->getId());
         // Returning the list of the results
         // TODO:
         // handling in the template the id of each record, so that in can be passed
         // afterwards for insertion and recovered from session without re-fetching
         // its data
         return $this->render('AppBundle:Record:results.html.twig', array('records' => $records));
     }
     // The form has not been submitted, displaying the form
     return $this->render('AppBundle:Record:searchByText.html.twig', array('form' => $form->createView()));
 }