You are here

function scald_audio_scald_add_form_fill in Scald: Media Management made easy 7

Implements hook_scald_add_form_fill().

File

modules/providers/scald_audio/scald_audio.module, line 48
Scald Audio is a Scald Atom Provider for audio files.

Code

function scald_audio_scald_add_form_fill(&$atoms, $form, $form_state) {
  $dir_audio_thumb = ScaldAtomController::getThumbnailPath('audio');
  foreach ($atoms as $delta => $atom) {
    if (is_array($form_state['values']['file']) && module_exists('plupload')) {
      module_load_include('inc', 'scald', 'includes/scald.plupload');
      $file = scald_plupload_save_file($form_state['values']['file'][$delta]['tmppath'], $form['file']['#upload_location'] . $form_state['values']['file'][$delta]['name']);
    }
    else {
      $file = file_load($form_state['values']['file']);
    }
    $atom->base_id = $file->fid;
    $atom->file_source = $file->uri;
    $atom->data['audio_file'] = $file->uri;
    $atom->data['audio_id'] = $file->fid;
    $atom->title = $file->filename;
    if (file_prepare_directory($dir_audio_thumb, FILE_CREATE_DIRECTORY)) {
      if (module_exists('waudio_getid3') && ($getid3 = _waudio_getid3_load())) {
        $filepath = drupal_realpath($file->uri);

        // Get all id3 infos.
        $ret = waudio_getid3_ret_infos($filepath, $getid3);
      }
      elseif (module_exists('getid3') && ($id3 = getid3_instance())) {
        $filepath = drupal_realpath($file->uri);
        $ret = array();

        // Get all id3 infos.
        $info = $id3
          ->analyze($filepath);
        foreach ($info['tags']['id3v2'] as $key => $value) {
          $ret['tags'][$key] = $value[0];
        }
        if (!empty($info['comments']['picture'][0]['data']) && !empty($info['comments']['picture'][0]['image_mime'])) {
          $ret['images'][0]['data'] = $info['comments']['picture'][0]['data'];
          $ret['images'][0]['image_mime'] = $info['comments']['picture'][0]['image_mime'];
        }
        elseif (!empty($info['id3v2']['APIC'][0]['data']) && !empty($info['id3v2']['APIC'][0]['image_mime'])) {
          $ret['images'][0]['data'] = $info['id3v2']['APIC'][0]['data'];
          $ret['images'][0]['image_mime'] = $info['id3v2']['APIC'][0]['image_mime'];
        }
      }
      if (!empty($ret)) {
        $atom->title = !empty($ret['tags']['title']) ? $ret['tags']['title'] : $file->filename;

        // Prefill the author.
        $langcode = field_language('scald_atom', $atom, 'scald_authors');
        $atom->scald_authors[$langcode][0] = array(
          'tid' => 0,
          'taxonomy_term' => (object) array(
            'name' => isset($ret['tags']['artist']) ? $ret['tags']['artist'] : t('Unknown'),
          ),
        );

        // Prefill tags.
        $langcode = field_language('scald_atom', $atom, 'scald_tags');
        $atom->scald_tags[$langcode][0] = array(
          'tid' => 0,
          // Beware, this is not a real tid, it's just an index.
          'taxonomy_term' => (object) array(
            'name' => isset($ret['tags']['genre']) ? $ret['tags']['genre'] : t('Unknown'),
          ),
        );
        $atom->data['artist'] = isset($ret['tags']['artist']) ? $ret['tags']['artist'] : '';
        $atom->data['title'] = isset($ret['tags']['title']) ? $ret['tags']['title'] : '';
        $atom->data['album'] = isset($ret['tags']['album']) ? $ret['tags']['album'] : '';
        $atom->data['track'] = isset($ret['tags']['track']) ? $ret['tags']['track'] : isset($ret['tags']['track_number']) ? $ret['tags']['track_number'] : '';
        $atom->data['year'] = isset($ret['tags']['year']) ? $ret['tags']['year'] : '';
        $atom->data['genre'] = isset($ret['tags']['genre']) ? $ret['tags']['genre'] : '';

        // If the MP3 includes a cover art, use it as the default thumbnail.
        if (isset($ret['images'][0]['data']) && $ret['images'][0]['data'] != '') {
          $extension = '.jpg';
          if ($ret['images'][0]['image_mime'] == 'image/png') {
            $extension = '.png';
          }
          elseif ($ret['images'][0]['image_mime'] == 'image/gif') {
            $extension = '.gif';
          }
          $dest = $dir_audio_thumb . '/' . $file->filename . $extension;
          $file = file_save_data($ret['images'][0]['data'], $dest);
          if ($file) {

            // Set the file status to temporary (image thumb).
            db_update('file_managed')
              ->condition('fid', $file->fid)
              ->fields(array(
              'status' => 0,
            ))
              ->execute();
            $langcode = field_language('scald_atom', $atom, 'scald_thumbnail');
            $atom->scald_thumbnail[$langcode][0] = (array) $file;
          }
        }
      }
    }
  }
}