public function requestAction() { $this->stationRequired(); if (!$this->station->requests_enabled) { throw new \DF\Exception\DisplayOnly('This station does not support requests at this time.'); } if ($this->station->requests_external_url) { return $this->redirect($this->station->requests_external_url); } $is_supported = CentovaCast::isStationSupported($this->station); if (!$is_supported) { throw new \DF\Exception\DisplayOnly('This station is not functioning properly and cannot accept requests at this time.'); } // Search redirection. if ($_GET) { $this->redirectFromHere($_GET); } // Process a request. if ($this->getParam('track')) { try { $track_id = (int) $this->getParam('track'); CentovaCast::request($this->station, $track_id); $track = StationMedia::find($track_id); $this->alert('<b>Your song, "' . $track->title . '" by ' . $track->artist . ', has been requested.</b><br>Stay tuned to the station to hear it!', 'green'); } catch (\DF\Exception $e) { $this->alert('<b>Your song could not be requested. An error occurred:</b><br>' . $e->getMessage(), 'red'); } $this->redirectFromHere(array('track' => NULL)); return; } // Most requested songs. $top_songs = $this->em->createQuery('SELECT sm FROM Entity\\StationMedia sm WHERE sm.station_id = :station_id AND sm.requests > 0 ORDER BY sm.requests DESC')->setParameter('station_id', $this->station_id)->setMaxResults(10)->getArrayResult(); $this->view->top_songs = $top_songs; // Artist names. $artist_names_raw = $this->em->createQuery('SELECT DISTINCT sm.artist FROM Entity\\StationMedia sm WHERE sm.station_id = :station_id ORDER BY sm.artist ASC')->setParameter('station_id', $this->station_id)->getArrayResult(); $artist_names = array(); foreach ($artist_names_raw as $name) { $artist_names[] = $name['artist']; } $this->view->artist_names = $artist_names; // Paginated results. if ($this->hasParam('q')) { $query = $this->getParam('q'); $media = StationMedia::search($this->station, $query); $this->view->page_title = 'Search Results for "' . htmlspecialchars($query) . '"'; $this->view->reset_button = true; } else { if ($this->hasParam('artist')) { $artist = $this->getParam('artist'); $media = StationMedia::getByArtist($this->station, $artist); $this->view->page_title = 'All Songs by ' . htmlspecialchars($artist); $this->view->reset_button = true; } else { $media = StationMedia::getRequestable($this->station); $this->view->page_title = 'All Available Songs'; $this->view->reset_button = false; } } $pager = new \DF\Paginator($media, $this->getParam('page'), 50); $this->view->pager = $pager; }
public static function sync() { if (DF_APPLICATION_ENV !== 'production') { return false; } $db = self::getDatabase(); $em = self::getEntityManager(); $settings = self::getSettings(); // Force correct account settings (enable global unified request system). $account_values = array('allowrequests' => '1', 'autoqueuerequests' => '1', 'requestprobability' => '50', 'requestdelay' => '0', 'emailunknownrequests' => '0'); $db->update('accounts', $account_values, array('expectedstate' => 'up')); // Clear out old logs. $threshold = strtotime('-1 month'); $threshold_date = date('Y-m-d', $threshold) . ' 00:00:00'; $db->executeQuery('DELETE FROM playbackstats_tracks WHERE endtime <= ?', array($threshold_date)); $db->executeQuery('DELETE FROM visitorstats_sessions WHERE endtime <= ?', array($threshold_date)); // Delete old requests still listed as pending. $requesttime = new \DateTime('NOW'); $requesttime->modify('-3 hours'); $requesttime->setTimezone(new \DateTimeZone($settings['timezone'])); $threshold_requests = $requesttime->format('Y-m-d h:i:s'); $db->executeQuery('DELETE FROM playlist_tracks_requests WHERE requesttime <= ?', array($threshold_requests)); // Force playlist enabling for existing pending requests. $request_playlists_raw = $db->fetchAll('SELECT DISTINCT ptr.playlistid AS pid FROM playlist_tracks_requests AS ptr'); foreach ($request_playlists_raw as $pl) { $pl_id = $pl['pid']; $db->update('playlists', array('status' => 'enabled'), array('id' => $pl_id)); } // Preload all station media locally. $stations = $em->createQuery('SELECT s FROM Entity\\Station s WHERE s.requests_enabled = 1')->execute(); foreach ($stations as $station) { $account_id = self::getStationID($station); if (!$account_id) { continue; } // Clear existing items. $existing_ids_raw = $em->createQuery('SELECT sm FROM Entity\\StationMedia sm WHERE sm.station_id = :station_id')->setParameter('station_id', $station->id)->execute(); $existing_records = array(); foreach ($existing_ids_raw as $row) { $existing_records[$row['id']] = $row; } // Find all tracks in active playlists. $new_records_raw = self::fetchTracks($station); $records_by_hash = array(); foreach ($new_records_raw as $track_info) { if ($track_info['length'] < 60) { continue; } if ($track_info['playlist_status'] !== 'enabled') { continue; } $row = array('id' => $track_info['id'], 'title' => $track_info['title'], 'artist' => $track_info['artist'], 'album' => $track_info['album'], 'length' => $track_info['length']); $song_hash = Song::getSongHash(array('text' => $row['artist'] . ' - ' . $row['title'], 'artist' => $row['artist'], 'title' => $row['title'])); $records_by_hash[$song_hash] = $row; } $new_records = array(); foreach ($records_by_hash as $row) { $new_records[$row['id']] = $row; } // Reconcile differences. $existing_guids = array_keys($existing_records); $new_guids = array_keys($new_records); $guids_to_delete = array_diff($existing_guids, $new_guids); if ($guids_to_delete) { foreach ($guids_to_delete as $guid) { $record = $existing_records[$guid]; $em->remove($record); } } $guids_to_add = array_diff($new_guids, $existing_guids); if ($guids_to_add) { foreach ($guids_to_add as $guid) { $record = new StationMedia(); $record->station = $station; $record->fromArray($new_records[$guid]); $em->persist($record); } } $em->flush(); } return true; }