You are here

function scald_prerender in Scald: Media Management made easy 6

Same name and namespace in other branches
  1. 7 scald.module \scald_prerender()

Prepare a Scald Atom for rendering

1 call to scald_prerender()
scald_render in ./scald.module
Render a Scald Atom

File

./scald.module, line 2089

Code

function scald_prerender(&$atom, $context, $options = NULL) {
  $scald_config = variable_get('scald_config', 0);
  $context_config = $scald_config->contexts[$context];

  // Build the portion of the Rendered member which Providers are expected to
  //  manipulate.
  $atom->rendered = new stdClass();
  $atom->rendered->sid = $atom->sid;
  $atom->rendered->title = $atom->title;
  $atom->rendered->file_source_url = empty($atom->file_source) ? NULL : url($atom->file_source);
  $atom->rendered->file_transcoded_url = NULL;
  $atom->rendered->thumbnail_source_url = file_create_url($atom->thumbnail_source);
  $atom->rendered->description = $atom->description;
  $atom->rendered->nocache = FALSE;

  // Type & Atom prerenders
  scald_invoke($scald_config->types[$atom->type]['provider'], 'scald_prerender', $atom, $context, $options, 'type');
  scald_invoke($atom->provider, 'scald_prerender', $atom, $context, $options, 'atom');

  // Transcoder Provider prerender -- only if there is a Transcoder specified
  //  for this Context.
  //  POPULATES: $atom->rendered->file_transcoded_url
  if (!empty($context_config['type_format']) && !empty($context_config['type_format'][$atom->type])) {
    scald_invoke($scald_config->transcoders[$context_config['type_format'][$atom->type]['transcoder']]['provider'], 'scald_prerender', $atom, $context, $options, 'transcoder');
  }

  // Context prerender
  scald_invoke($context_config['provider'], 'scald_prerender', $atom, $context, $options, 'context');

  // Populate default rendered members & validate other rendered members.
  $atom->rendered->title = check_plain($atom->rendered->title);

  // @@@TODO: Determine if it makes sense to *always* do this or make it dependant on Context render language.
  $atom->rendered->description = check_markup($atom->rendered->description, FILTER_FORMAT_DEFAULT, FALSE);

  // @@@TODO: Determine if this is reasonable or if it should be left to Providers
  $atom->rendered->type = check_plain($scald_config->types[$atom->type]['title']);

  // Actions
  $atom->rendered->actions = array();
  $current_actions = scald_actions($atom) & ~SCALD_ACTIONS_ADMIN_BIT;

  // Remove the Admin bit as it is irrelevant for rendering purposes
  foreach ($scald_config->actions as $slug => $details) {
    if ($current_actions & $details['mask']) {
      $atom->rendered->actions[] = array(
        'title' => $details['title'],
        'path' => 'scald/actions/' . $slug . '/' . $atom->sid,
        'link' => l($details['title'], 'scald/actions/' . $slug . '/' . $atom->sid),
      );
    }
  }

  // Authors
  $atom->rendered->authors = array();
  if (!empty($atom->authors)) {
    $authors_result = db_query("\n      SELECT\n        aid,\n        name,\n        uid,\n        url\n      FROM\n        {scald_authors}\n      WHERE\n        aid IN (" . implode(', ', $atom->authors) . ")\n    ");
    $authors_details = array();
    while ($authors_raw = db_fetch_array($authors_result)) {
      $authors_details[$authors_raw['aid']] = array(
        'name' => $authors_raw['name'],
        'uid' => $authors_raw['uid'],
        'url' => $authors_raw['url'],
      );
    }
    foreach ($atom->authors as $weight => $aid) {
      $author = $authors_details[$aid];
      if ($author['url']) {
        $author['link'] = l($author['name'], $author['url']);
      }
      else {
        $author['link'] = check_plain($author['name']);
      }
      $atom->rendered->authors[$weight] = $author;
    }
  }

  // Relationships
  $atom->rendered->relationships = array();
  foreach ($atom->relationships as $direction => $relationships) {
    $title_accessor = $direction == 'forward' ? 'title' : 'title_reverse';
    foreach ($relationships as $slug => $sids) {
      foreach ($sids as $sid) {
        $rel_atom = scald_is_registered($sid);
        $atom->rendered->relationships[$scald_config->relationships[$slug][$title_accessor]][] = l($rel_atom->title, 'scald/actions/view/' . $sid);
      }
    }
  }

  // User
  // NOTE: This query is much faster than a user_load() (no JOIN)
  $name = db_result(db_query("SELECT name FROM {users} WHERE uid = %d", $atom->publisher));
  $atom->rendered->publisher = array(
    'uid' => $atom->publisher,
    'name' => $name,
    'path' => 'user/' . $atom->publisher,
    'link' => l($name, 'user/' . $atom->publisher),
  );
}