[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/filefield/filefield_meta/ -> filefield_meta.module (source)

   1  <?php
   2  // $Id: filefield_meta.module,v 1.18 2010/06/29 19:14:17 quicksketch Exp $
   3  
   4  /**
   5   * @file
   6   * FileField Meta: Add Video Support to File Field.
   7   */
   8  
   9  /**
  10   * Implementation of hook_init().
  11   */
  12  function filefield_meta_init() {
  13    // Conditional module support.
  14    if (module_exists('token')) {
  15      module_load_include('inc', 'filefield_meta', 'filefield_meta.token');
  16    }
  17  }
  18  
  19  /**
  20   * Implementation of hook_theme().
  21   */
  22  function filefield_meta_theme() {
  23    return array(
  24      'filefield_meta_duration' => array(
  25        'arguments' => array('duration' => NULL),
  26      ),
  27      'filefield_meta_samplerate' => array(
  28        'arguments' => array('samplerate' => NULL),
  29      ),
  30      'filefield_meta_bitrate' => array(
  31        'arguments' => array('bitrate' => NULL),
  32      ),
  33    );
  34  }
  35  
  36  /**
  37   * Implementation of hook_cron(). 
  38   */
  39  function filefield_meta_cron() {
  40    $result = db_query('SELECT fm.fid FROM {filefield_meta} fm LEFT JOIN {files} f ON fm.fid=f.fid WHERE f.fid IS NULL');
  41    while ($file = db_fetch_object($result)) {
  42      db_query('DELETE FROM {filefield_meta} WHERE fid = %d', $file->fid);
  43    }
  44  }
  45  
  46  /**
  47   * Implementation of hook_views_api().
  48   */
  49  function filefield_meta_views_api() {
  50    return array(
  51      'api' => 2.0,
  52      'path' => drupal_get_path('module', 'filefield_meta') . '/includes',
  53    );
  54  }
  55  
  56  /**
  57   * Implementation of FileField's hook_file_load().
  58   */
  59  function filefield_meta_file_load(&$file) {
  60    $result = db_query("SELECT * FROM {filefield_meta} WHERE fid = %d", $file->fid);
  61    $data = db_fetch_array($result);
  62  
  63    // Essentially this is a lazy-loader. If no record exists, read in the file.
  64    if ($data) {
  65      $data['tags'] = isset($data['tags']) ? unserialize($data['tags']) : array();
  66      $file->data = isset($file->data) ? array_merge($file->data, $data) : $data;
  67    }
  68    else {
  69      filefield_meta_file_insert($file);
  70    }
  71  }
  72  
  73  /**
  74   * Implementation of FileField's hook_file_insert().
  75   */
  76  function filefield_meta_file_insert(&$file) {
  77    if (!empty($file->fid)) {
  78      filefield_meta($file);
  79      $record = array_merge($file->data, array('fid' => $file->fid));
  80      drupal_write_record('filefield_meta', $record);
  81    }
  82  }
  83  
  84  /**
  85   * Implementation of FileField's hook_file_update().
  86   */
  87  function filefield_meta_file_update(&$file) {
  88    if (!empty($file->fid)) {
  89      filefield_meta_file_delete($file);
  90      filefield_meta_file_insert($file);
  91    }
  92  }
  93  
  94  /**
  95   * Implementation of FileField's hook_file_delete().
  96   */
  97  function filefield_meta_file_delete($file) {
  98    db_query('DELETE FROM {filefield_meta} WHERE fid = %d', $file->fid);
  99  }
 100  
 101  /**
 102   * Adds the width, height and duration to the file's data property.
 103   */
 104  function filefield_meta(&$file) {
 105    $info = getid3_analyze($file->filepath);
 106  
 107    $file->data = isset($file->data) ? $file->data : array();
 108    $file->data['width'] = $file->data['height'] = $file->data['duration'] = 0;
 109    if (isset($info['video']['resolution_x'])) {
 110      $file->data['width'] = $info['video']['resolution_x'];
 111      $file->data['height'] = $info['video']['resolution_y'];
 112    }
 113    elseif (isset($info['video']['streams'])) {
 114      foreach ($info['video']['streams'] as $stream) {
 115        $file->data['width'] = max($file->data['width'], $stream['resolution_x']);
 116        $file->data['height'] = max($file->data['height'], $stream['resolution_y']);
 117      }
 118    }
 119  
 120    if (isset($info['playtime_seconds']))  {
 121      $file->data['duration'] = $info['playtime_seconds'];
 122    }
 123  
 124    // Initialize fields. 
 125    $file->data['audio_format'] = $file->data['audio_channel_mode'] = $file->data['audio_bitrate_mode'] = '';
 126    $file->data['audio_sample_rate'] = $file->data['audio_bitrate'] = 0;
 127   
 128    if (isset($info['audio']))  { 
 129      $file->data['audio_format'] = $info['audio']['dataformat']; //e.g. mp3
 130      $file->data['audio_sample_rate'] = $info['audio']['sample_rate']; //e.g. 44100
 131      $file->data['audio_channel_mode'] = $info['audio']['channelmode']; // e.g. mono
 132      $file->data['audio_bitrate'] = isset($info['audio']['bitrate']) ? $info['audio']['bitrate'] : NULL; //e.g. 64000
 133      $file->data['audio_bitrate_mode'] = isset($info['audio']['bitrate_mode']) ? $info['audio']['bitrate_mode'] : NULL; //e.g. cbr
 134    }
 135  
 136    // Add in arbitrary ID3 tags.
 137    if (isset($info['tags_html'])) {
 138      // We use tags_html instead of tags because it is the most reliable data
 139      // source for pulling in non-UTF-8 characters according to getID3 docs.
 140      foreach ($info['tags_html'] as $type => $values) {
 141        // Typically $type may be IDv2 (for MP3s) or quicktime (for AAC).
 142        foreach ($values as $key => $value) {
 143          $value = isset($value[0]) ? (string) $value[0] : (string) $value;
 144          if (!empty($value) && $key != 'coverart') {
 145            $file->data['tags'][$key] = html_entity_decode($value, ENT_QUOTES, 'UTF-8');
 146          }
 147        }
 148      }
 149    }
 150  }
 151  
 152  /**
 153   * Utility function that simply returns the current list of all known ID3 tags.
 154   *
 155   * If new or different ID3 tags are desired, these may be overridden by adding
 156   * the following to your site's settings.php file.
 157   *
 158   * @code
 159   * $conf['filefield_meta_tags'] = array(
 160   *   'title' => t('Title'),
 161   *   'artist' => t('Artist'),
 162   *   'composer' => t('Composer'),
 163   *   // etc...
 164   * );
 165   * @endcode
 166   */
 167  function filefield_meta_tags() {
 168    $defaults = array(
 169      'title' => t('Title'),
 170      'artist' => t('Artist'),
 171      'album' => t('Album'),
 172      'year' => t('Year'),
 173      'genre' => t('Genre'),
 174    );
 175    return variable_get('filefield_meta_tags', $defaults);
 176  }
 177  
 178  /**
 179   * Convert the float duration into a pretty string.
 180   *
 181   * @param $duration
 182   */
 183  function theme_filefield_meta_duration($duration) {
 184    $seconds = round((($duration / 60) - floor($duration / 60)) * 60);
 185    $minutes = floor($duration / 60);
 186    if ($seconds >= 60) {
 187      $seconds -= 60;
 188      $minutes++;
 189    }
 190    return intval($minutes) . ':' . str_pad($seconds, 2, 0, STR_PAD_LEFT);
 191  }
 192  
 193  
 194  /**
 195   * Formats audio sample rate.
 196   */
 197  function theme_filefield_meta_samplerate($samplerate) {
 198    return t('@sampleratekHz', array('@samplerate' => (int) ($samplerate/1000)));
 199  }
 200   
 201  /**
 202   * Formats audio bit rate.
 203   */
 204  function theme_filefield_meta_bitrate($bitrate) {
 205    return t('@bitrateKbps', array('@bitrate' => (int) ($bitrate/1000)));
 206  }


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7