Exemplo n.º 1
0
 /**
  * @return A string containing the icon's image tag.
  */
 public function icon($params)
 {
     if (!is_array($params)) {
         $params = array('image' => $params);
     }
     // Default values for icons.
     $defaults = array('type' => 'png', 'alt' => '(Icon)', 'class' => 'icon');
     $params = array_merge($defaults, $params);
     if (substr($params['image'], 0, 5) == "icon-") {
         $params['class'] .= ' ' . $params['image'];
         unset($params['type'], $params['image']);
         return $this->iconComposeTag('i', $params);
     } else {
         if ($params['type'] == "png") {
             $params['class'] .= ' ui-silk ui-silk-' . str_replace('_', '-', $params['image']);
             unset($params['type'], $params['image']);
             return $this->iconComposeTag('span', $params);
         } else {
             $icon_name = str_replace('.png', '', $params['image']) . '.png';
             $params['src'] = \DF\Url::content('common/icons/' . $params['type'] . '/' . $icon_name);
             unset($params['size'], $params['image'], $params['type']);
             return $this->iconComposeTag('img', $params, '');
         }
     }
 }
Exemplo n.º 2
0
 public function indexAction()
 {
     $id = $this->getParam('id');
     if (empty($id)) {
         $this->redirectHome();
     }
     $record = Song::find($id);
     if (!$record instanceof Song) {
         throw new \DF\Exception\DisplayOnly('Song not found!');
     }
     $song_info = array();
     $song_info['record'] = $record;
     // Get external provider information.
     $song_info['external'] = $record->getExternal();
     // Get album art and lyrics from all providers.
     $adapters = Song::getExternalAdapters();
     $external_fields = array('lyrics', 'purchase_url', 'description');
     foreach ($external_fields as $field_name) {
         $song_info[$field_name] = NULL;
         foreach ($adapters as $adapter_name => $adapter_class) {
             if (!empty($song_info['external'][$adapter_name][$field_name])) {
                 $song_info[$field_name] = $song_info['external'][$adapter_name][$field_name];
                 break;
             }
         }
     }
     $song_info['image_url'] = $record->image_url;
     if (!$song_info['image_url']) {
         $song_info['image_url'] = \DF\Url::content('images/song_generic.png');
     }
     $song_info['description'] = $this->_cleanUpText($song_info['description']);
     $song_info['lyrics'] = $this->_cleanUpText($song_info['lyrics']);
     // Get most recent playback information.
     $history_raw = $this->em->createQuery('
         SELECT sh, st
         FROM Entity\\SongHistory sh JOIN sh.station st
         WHERE sh.song_id = :song_id AND st.category IN (:categories) AND sh.timestamp >= :threshold
         ORDER BY sh.timestamp DESC')->setParameter('song_id', $record->id)->setParameter('categories', array('audio', 'video'))->setParameter('threshold', strtotime('-1 week'))->getArrayResult();
     $history = array();
     $last_row = NULL;
     foreach ($history_raw as $i => $row) {
         if ($last_row && $row['station_id'] == $last_row['station_id']) {
             $timestamp_diff = abs($row['timestamp'] - $last_row['timestamp']);
             if ($timestamp_diff < 60) {
                 continue;
             }
         }
         $history[] = $row;
         $last_row = $row;
     }
     $song_info['recent_history'] = $history;
     // Get requestable locations.
     $song_info['request_on'] = $this->em->createQuery('
         SELECT sm, st
         FROM Entity\\StationMedia sm JOIN sm.station st
         WHERE sm.song_id = :song_id
         GROUP BY sm.station_id')->setParameter('song_id', $record->id)->getArrayResult();
     $this->view->song = $song_info;
 }
Exemplo n.º 3
0
 /**
  * Return the remote URL of the file (or local fallback).
  *
  * @param $remote_file_path
  * @return string
  */
 public static function url($remote_file_path)
 {
     if (self::isEnabled()) {
         return DF_UPLOAD_URL . '/' . $remote_file_path;
     } else {
         return \DF\Url::content($remote_file_path);
     }
 }
Exemplo n.º 4
0
 public static function getFileUrl($file_name)
 {
     if (defined('DF_UPLOAD_URL')) {
         return DF_UPLOAD_URL . '/' . $file_name;
     } else {
         return \DF\Url::content($file_name);
     }
 }
Exemplo n.º 5
0
 public function process()
 {
     // Now Playing defaults.
     $np = array('on_air' => array('text' => 'Stream Offline', 'thumbnail' => \DF\Url::content('images/video_thumbnail.png')), 'meta' => array('status' => 'offline', 'listeners' => 0));
     if (!$this->stream->is_active) {
         return $np;
     }
     // Merge station-specific info into defaults.
     $this->_process($np);
     // Update status code for offline stations, clean up song info for online ones.
     if ($np['on_air']['text'] == 'Stream Offline') {
         $np['meta']['status'] = 'offline';
     }
     return $np;
 }
Exemplo n.º 6
0
 /**
  * Generate random image from a folder.
  *
  * @param $static_dir
  * @return string
  */
 public static function randomImage($static_dir)
 {
     $img = null;
     $folder = DF_INCLUDE_STATIC . DIRECTORY_SEPARATOR . $static_dir;
     $extList = array('gif' => 'image/gif', 'jpg' => 'image/jpeg', 'jpeg' => 'image/jpeg', 'png' => 'image/png');
     $handle = opendir($folder);
     while ($file = readdir($handle)) {
         $file_info = pathinfo($file);
         $file_ext = strtolower($file_info['extension']);
         if (isset($extList[$file_ext])) {
             $fileList[] = $file;
         }
     }
     closedir($handle);
     if (count($fileList) > 0) {
         $imageNumber = time() % count($fileList);
         $img = $fileList[$imageNumber];
     }
     return \DF\Url::content($static_dir . '/' . $img);
 }
Exemplo n.º 7
0
 public static function getEpisodeRotatorUrl($episode, $podcast = NULL, $source = NULL)
 {
     if ($episode instanceof self) {
         if ($podcast === null) {
             $podcast = $episode->podcast;
         }
         if ($source === null) {
             $source = $episode->source;
         }
     }
     if ($episode['banner_url'] && !$podcast['is_adult'] && !$podcast['always_use_banner_url']) {
         $image_path_base = 'podcast_episodes/' . $episode['guid'] . '.jpg';
         $image_path = AmazonS3::path($image_path_base);
         // Crop remote banner URL if the local version doesn't already exist.
         if (file_exists($image_path)) {
             return AmazonS3::url($image_path_base);
         } else {
             $temp_path_ext = \DF\File::getFileExtension($episode['banner_url']);
             $temp_path = DF_INCLUDE_TEMP . DIRECTORY_SEPARATOR . '/podcast_episodes/podcast_episode_' . $episode['id'] . '_temp.' . $temp_path_ext;
             @mkdir(dirname($temp_path));
             @copy($episode['banner_url'], $temp_path);
             if (file_exists($temp_path)) {
                 try {
                     Image::resizeImage($temp_path, $temp_path, 600, 300, TRUE);
                     AmazonS3::upload($temp_path, $image_path_base);
                     return AmazonS3::url($image_path_base);
                 } catch (\Exception $e) {
                 }
             }
         }
     }
     if ($podcast !== null && !empty($podcast['banner_url'])) {
         // Reference the podcast's existing banner URL.
         return AmazonS3::url($podcast['banner_url']);
     }
     if ($source !== null) {
         return Url::content('images/podcast_' . $source['type'] . '_banner.png');
     }
     return Url::content('images/podcast_default.png');
 }
Exemplo n.º 8
0
 public static function run($force_run = false)
 {
     $di = \Phalcon\Di::getDefault();
     $em = $di->get('em');
     $config = $di->get('config');
     // Set up Google Client.
     $gclient_api_key = $config->apis->google_apis_key;
     $gclient_app_name = $config->application->name;
     if (empty($gclient_api_key)) {
         return null;
     }
     $gclient = new \Google_Client();
     $gclient->setApplicationName($gclient_app_name);
     $gclient->setDeveloperKey($gclient_api_key);
     $gcal = new \Google_Service_Calendar($gclient);
     // Prevent running repeatedly in too short of a time (avoid API limits).
     $last_run = Settings::getSetting('schedule_manager_last_run', 0);
     if ($last_run > time() - 60 && !$force_run) {
         return null;
     }
     $schedule_items = array();
     $schedule_records = array();
     $stations = $em->createQuery('SELECT s FROM Entity\\Station s WHERE (s.gcal_url IS NOT NULL AND s.gcal_url != \'\') AND s.is_active = 1')->getArrayResult();
     $active_stations = Utilities::ipull($stations, 'id');
     // Clear all invalid station records.
     $em->createQuery('DELETE FROM Entity\\Schedule s WHERE (s.station_id IS NOT NULL) AND (s.station_id NOT IN (:station_ids))')->setParameter('station_ids', $active_stations)->execute();
     foreach ($stations as $station) {
         if ($station['gcal_url']) {
             $schedule_items[] = array('name' => $station['name'], 'url' => $station['gcal_url'], 'type' => 'station', 'station_id' => $station['id'], 'image_url' => \DF\Url::content($station['image_url']));
         }
     }
     Debug::startTimer('Get Calendar Records');
     // Time boundaries for calendar entries.
     $threshold_start = date(\DateTime::RFC3339, strtotime('-1 week'));
     $threshold_end = date(\DateTime::RFC3339, strtotime('+1 year'));
     foreach ($schedule_items as $item) {
         // Get the "calendar_id" from the URL provided by the user.
         $orig_url_parts = parse_url($item['url']);
         $url_path_parts = explode('/', $orig_url_parts['path']);
         $calendar_id = urldecode($url_path_parts[3]);
         if (empty($calendar_id)) {
             continue;
         }
         // Call the external Google Calendar client.
         try {
             $all_events = $gcal->events->listEvents($calendar_id, array('timeMin' => $threshold_start, 'timeMax' => $threshold_end, 'singleEvents' => 'true', 'orderBy' => 'startTime', 'maxResults' => '300'));
         } catch (\Exception $e) {
             continue;
         }
         // Process each individual event.
         foreach ($all_events as $event_orig) {
             $title = $event_orig->summary;
             $body = $event_orig->description;
             $location = $event_orig->location;
             $web_url = $event_orig->htmlLink;
             $banner_url = null;
             $is_all_day = false;
             $start_time_obj = $event_orig->start;
             if ($start_time_obj->date) {
                 $is_all_day = true;
                 $start_time = strtotime($start_time_obj->date . ' 00:00:00');
             } else {
                 $start_time = strtotime($start_time_obj->dateTime);
             }
             $end_time_obj = $event_orig->end;
             if ($end_time_obj->date) {
                 $is_all_day = true;
                 $end_time = strtotime($end_time_obj->date . ' 00:00:00');
             } elseif ($end_time_obj) {
                 $end_time = strtotime($end_time_obj->dateTime);
             } else {
                 $end_time = $start_time;
             }
             // Detect URLs for link.
             if ($body && !$web_url) {
                 preg_match('@((https?://)?([-\\w]+\\.[-\\w\\.]+)+\\w(:\\d+)?(/([-\\w/_\\.]*(\\?\\S+)?)?)*)@', $body, $urls);
                 if (count($urls) > 0) {
                     $web_url = $urls[0];
                 }
             }
             // Detect URLs for photo.
             if ($location) {
                 preg_match('@((https?://)?([-\\w]+\\.[-\\w\\.]+)+\\w(:\\d+)?(/([-\\w/_\\.]*(\\?\\S+)?)?)*)@', $location, $urls);
                 if (count($urls) > 0) {
                     $banner_url = $urls[0];
                 }
             }
             $guid = md5(implode('|', array($event_orig->id, $start_time, $end_time, $title, $location)));
             $schedule_record = array('guid' => $guid, 'type' => $item['type'], 'start_time' => $start_time, 'end_time' => $end_time, 'is_all_day' => $is_all_day, 'title' => $title, 'location' => $location, 'body' => \DF\Utilities::truncateText(strip_tags($body), 300), 'banner_url' => $banner_url, 'web_url' => $web_url);
             \PVL\Debug::print_r($schedule_record);
             $schedule_records[$item['station_id']][$guid] = $schedule_record;
         }
     }
     Debug::endTimer('Get Calendar Records');
     if (count($schedule_records) == 0) {
         Debug::log('Error: No calendar records loaded');
         return;
     }
     // Add/Remove all differential records.
     Debug::startTimer('Sync DB Records');
     foreach ($schedule_records as $station_id => $station_records) {
         $station = Station::find($station_id);
         if ($station_id == 0) {
             $existing_guids_raw = $em->createQuery('SELECT s.guid FROM Entity\\Schedule s WHERE s.station_id IS NULL')->getArrayResult();
         } else {
             $existing_guids_raw = $em->createQuery('SELECT s.guid FROM Entity\\Schedule s WHERE s.station_id = :sid')->setParameter('sid', $station_id)->getArrayResult();
         }
         $existing_guids = array();
         foreach ($existing_guids_raw as $i) {
             $existing_guids[] = $i['guid'];
         }
         $new_guids = array_keys($station_records);
         $guids_to_delete = array_diff($existing_guids, $new_guids);
         if ($guids_to_delete) {
             $em->createQuery('DELETE FROM Entity\\Schedule s WHERE s.guid IN (:guids)')->setParameter('guids', $guids_to_delete)->execute();
         }
         $guids_to_add = array_diff($new_guids, $existing_guids);
         if ($guids_to_add) {
             foreach ($guids_to_add as $guid) {
                 $schedule_record = $station_records[$guid];
                 $record = new Schedule();
                 $record->station = $station;
                 $record->fromArray($schedule_record);
                 $em->persist($record);
             }
         }
         $em->flush();
         $em->clear();
     }
     Debug::endTimer('Sync DB Records');
     Settings::setSetting('schedule_manager_last_run', time());
 }