public function deleteAction()
 {
     $record = Record::find($this->getParam('id'));
     if ($record) {
         $record->delete();
     }
     $this->alert('Record deleted.', 'green');
     $this->redirectFromHere(array('action' => 'index', 'id' => NULL, 'csrf' => NULL));
 }
 public function viewAction()
 {
     $id = (int) $this->getParam('id');
     $record = Artist::find($id);
     if (!$record instanceof Artist) {
         throw new \DF\Exception\DisplayOnly('Artist Not Found');
     }
     $this->view->artist = $record;
     // Generate statistics.
     $cache_key = 'artist_' . $record->id . '_stats';
     $stats = \DF\Cache::get($cache_key);
     if (empty($stats)) {
         $stats = array('plays_per_day' => array(), 'song_lists' => array('most_played' => array('label' => 'Most Played Songs', 'songs' => array()), 'most_liked' => array('label' => 'Most Liked Songs', 'songs' => array()), 'most_recent' => array('label' => 'Most Recently Played', 'songs' => array())));
         $active_streams = \Entity\StationStream::getMainRadioStreams();
         $songs = $this->em->createQuery('SELECT s, sh
             FROM Entity\\Song s
             LEFT JOIN s.history sh
             WHERE s.artist LIKE :artist_q
             AND sh.stream_id IN (:streams)
             ORDER BY s.title, sh.timestamp DESC')->setParameter('artist_q', '%' . $record->name . '%')->setParameter('streams', $active_streams)->getArrayResult();
         $plays_per_day = array();
         foreach ($songs as &$song) {
             foreach ((array) $song['history'] as $i => $history) {
                 // Get day of song play, incremenet counter.
                 $day = strtotime(date('Y-m-d', $history['timestamp']) . ' 00:00:00') * 1000;
                 $plays_per_day[$day] += 1;
                 // Increment votes.
                 $song['score_likes'] += $history['score_likes'];
                 $song['score_dislikes'] += $history['score_dislikes'];
             }
             unset($song['history']);
             // Increment vote totals.
             $song['score_total'] = $song['score_likes'] - $song['score_dislikes'];
             $song['votes'] = $song['score_likes'] + $song['score_dislikes'];
         }
         // Remove current day, as it will always be lower.
         $current_day = strtotime(date('Y-m-d') . ' 00:00:00') * 1000;
         unset($plays_per_day[$current_day]);
         ksort($plays_per_day);
         foreach ($plays_per_day as $plays_day => $plays_total) {
             $stats['plays_per_day'][] = array($plays_day, $plays_total);
         }
         $stats['song_lists']['most_played']['songs'] = array_slice(Utilities::irsort($songs, 'play_count'), 0, 10);
         $stats['song_lists']['most_liked']['songs'] = array_slice(Utilities::irsort($songs, 'score_total'), 0, 10);
         $stats['song_lists']['most_recent']['songs'] = array_slice(Utilities::irsort($songs, 'last_played'), 0, 10);
         \DF\Cache::save($stats, $cache_key, array(), 300);
     }
     $this->view->stats = $stats;
 }