You are here

function mediafront_playlist_node in MediaFront 7

Same name and namespace in other branches
  1. 6.2 mediafront.module \mediafront_playlist_node()
  2. 6 mediafront.module \mediafront_playlist_node()

Implements hook_playlist_node()

File

./mediafront.module, line 829

Code

function mediafront_playlist_node($node) {
  $media_added = false;
  $additions = array();
  $preview = '';
  $field_additions = array();

  // If they provide the field_mediafront_media field.
  if ($field = _mediafront_get_field($node, 'field_mediafront_media', 0)) {
    $media_added = true;
    $additions["mediafiles"]["media"]["media"] = $field['value'];
  }

  // If they provide the field_mediafront_image field.
  if ($field = _mediafront_get_field($node, 'field_mediafront_image', 0)) {
    $media_added = true;
    $additions["mediafiles"]["images"]["image"] = $field['value'];
  }

  // Use the media module.
  $media_module = module_exists('media');

  // Now look for any CCK fields.
  foreach ($node as $fieldname => $field) {

    // This is a CCK filefield.
    if ($field = _mediafront_get_field($node, $fieldname)) {

      // This is a text field.
      if (isset($field[0]['value']) && is_string($field[0]['value'])) {
        $field_additions[$fieldname] = $field[0]['value'];
      }

      // This is a file field.
      if (isset($field[0]['fid']) || isset($field[0]['filename'])) {

        // Get the media type for this field.
        $media_field = mediafront_filefield_get('node', $node->type, $fieldname);

        // See if we wish to include this field.
        if (!$media_field || !empty($media_field->media_type) && $media_field->media_type != 'none' || !($media_field->node_preset == 'none' && $media_field->thumb_preset == 'none')) {

          // Iterate through the file field instances.
          foreach ($field as $index => $instance) {

            // Check to see if we need to load the media.
            if ($instance['fid'] && !isset($instance['filename']) && !isset($instance['data'])) {
              $media = file_load($instance['fid']);
              $instance['data'] = (array) $media;
            }

            // Check to see if there is a preview.
            if ($media_module && isset($instance['fid'])) {
              $media = isset($instance['data']) ? $instance['data'] : $instance;
              $preview = mediafront_get_media_preview($media);
            }

            // If this instance has a data struct...
            if (isset($instance['data']) && $instance['data']) {

              // Merge the file data with the file info.
              $fileData = $instance['data'];
              unset($instance['data']);
              $file = array_merge($instance, $fileData);
            }
            else {
              $file = $instance;
            }

            // Set the media type for this file.
            $file['mediaType'] = $media_field ? $media_field->media_type : '';

            // Add this media to our struct.
            $media_added |= mediafront_add_media($additions, $file);
          }

          // If the media field is defined.
          if ($media_field) {

            // Get the filename.
            $filename = $field[0]['filename'];

            // If this is a preview image.
            if ($media_field->node_preset && $media_field->node_preset != 'mediafront_original') {
              $file['path'] = image_style_url($media_field->node_preset, $file['uri']);
              $file['class'] = 'images';
              $file['mediaType'] = 'preview';
              $media_added |= mediafront_add_media($additions, $file);
            }

            // This is a thumbnail image.
            if ($media_field->thumb_preset && $media_field->thumb_preset != 'mediafront_original') {
              $file['path'] = image_style_url($media_field->thumb_preset, $file['uri']);
              $file['class'] = 'images';
              $file['mediaType'] = 'thumbnail';
              $media_added |= mediafront_add_media($additions, $file);
            }
          }
        }
      }
    }
  }
  $additions['nid'] = $node->nid;
  $additions['title'] = $node->title;

  // If no images exist, then add the preview...
  if ($preview && (!isset($additions["mediafiles"]) || !isset($additions["mediafiles"]["images"]))) {
    $additions["mediafiles"]["images"]["thumbnail"] = $preview;
    $additions["mediafiles"]["images"]["preview"] = str_replace('square_thumbnail', 'large', $preview);
  }

  // Now add the CCK fields.
  foreach ($field_additions as $field => $value) {
    $additions[$field] = $value;
  }

  // Return our additions...
  return $additions;
}