You are here

function apps_request_manifest in Apps 7

Request manifest and media assets from the app server.

Parameters

string $server: A server array

Return value

array An array representation of the json manifest, with images replaced by file objects.

2 calls to apps_request_manifest()
apps_cron in ./apps.module
Implements hook_cron().
apps_manifest in ./apps.manifest.inc
Produce the Process Manifest.

File

./apps.manifest.inc, line 210
Handles pulling and processing of app data from the server

Code

function apps_request_manifest($server) {
  if ($cache = cache_get("apps_manifest_{$server['name']}")) {
    return $cache->data;
  }

  // Gather all local apps.
  $local_apps = apps_server_local($server);
  $manifest = $server;
  $manifest['apps'] = $local_apps;

  // If there is no remote server, or we're in offline mode, stop here.
  if (!$server['manifest'] || variable_get('apps_offline_mode', FALSE)) {
    apps_request_manifest_image_process($manifest);
    return $manifest;
  }

  // Now process remote apps.
  $info = drupal_parse_info_file(dirname(__FILE__) . '/apps.info');
  $rest_data = array(
    'client_id' => apps_client_id(),
    'apps_version' => isset($info['version']) ? $info['version'] : NULL,
    'format' => 'json',
  ) + $server;
  $url = url($server['manifest'], array(
    'query' => $rest_data,
  ));
  $request = drupal_http_request($url);
  if (isset($request->error)) {
    $msg = t("Manifest error from @server: @error", array(
      '@server' => $server['title'],
      '@error' => $request->error,
    ));
    watchdog("apps", $msg);
    drupal_set_message($msg, 'warning');
  }
  elseif ($remote_manifest = json_decode($request->data, TRUE)) {

    // Translate index to machine name.
    foreach ($remote_manifest['apps'] as $index => $app) {

      // App server has a bug where it puts the server base url for blank logos.
      if (strpos($server['manifest'], $app['logo']) === 0) {
        unset($app['logo']);
      }

      // If it is already a local app, note the remote version.
      if (array_key_exists($app['machine_name'], $manifest['apps'])) {
        $manifest['apps'][$app['machine_name']]['remote manifest'] = $app;

        // Override app info with remote information.
        $manifest['apps'][$app['machine_name']] = $app + $manifest['apps'][$app['machine_name']];
      }
      else {
        $remote_manifest['apps'][$app['machine_name']] = $app;
        $remote_manifest['apps'][$app['machine_name']]['remote'] = TRUE;
      }
      unset($remote_manifest['apps'][$index]);
    }
    $manifest['apps'] = array_merge($remote_manifest['apps'], $manifest['apps']);
  }
  else {
    $msg = t("Manifest JSON from @server not parsable", array(
      '@server' => $server['title'],
    ));
    watchdog("apps", $msg);
    drupal_set_message($msg, 'warning');
  }
  apps_request_manifest_image_process($manifest);
  cache_set("apps_manifest_{$server['name']}", $manifest, 'cache', REQUEST_TIME + 60 + 30);
  return $manifest;
}