[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

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

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


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7