function hosting_site_available_options

7.x-3.x hosting_site.form.inc hosting_site_available_options($node, $platform = NULL)

Pass in a site node and return an array of valid options for it's fields.

Modules can define the hook_hosting_site_options_alter function to modify which fields are available for selection.

4 calls to hosting_site_available_options()

File

site/hosting_site.form.inc, line 78
Site node form.

Code

function hosting_site_available_options($node, $platform = NULL) {
  // Cast to object if it's an array.
  $node = (is_array($node)) ? (object) $node : clone $node;

  $return = array();

  $return['profile'] = array();
  $return['platform'] = array();
  $return['site_language'] = array();

  if (!hosting_feature('client')) {
    // Setting the return value of a text field to null,
    // will signal to the front end that the field needs to
    // be displayed, but is not editable.
    $return['client'] = NULL;
  }

  // Load up the user we'll use to check platform and profile access.
  $user = user_load($GLOBALS['user']->uid);

  // Install profiles.
  $profiles = hosting_get_profiles();
  $platform_profiles = array();
  foreach ($profiles as $id => $name) {

    // Trim down the list of profiles to those that are available and the user has access to
    // XXX This hack (next 22 lines) hides profiles that can't be accessed
    // Eventually we should lighten up the content of this callback
    $result = db_query("SELECT l.nid
                        FROM {hosting_package_instance} i
                        JOIN {hosting_package} p
                        ON p.nid = i.package_id
                        JOIN {hosting_platform} l
                        ON l.nid = i.rid
                        WHERE i.package_id = :package_id
                        AND p.package_type = :package_type
                        AND l.status = :status;", array(
      ':package_id' => $id,
      ':package_type' => 'profile',
      ':status' => HOSTING_PLATFORM_ENABLED,
    ));
    if (is_null($platform)) {
      $allowed_plats = _hosting_get_allowed_platforms($user->uid);
    }
    else {
      $allowed_plats = array($platform => $platform);
    }
    $access_check = FALSE;

    foreach ($result as $row) {
      if (!is_null($platform) && array_key_exists($row->nid, $allowed_plats)) {
        $platform_profiles[$id] = $name;
      }
      elseif (array_key_exists($row->nid, $allowed_plats)) {
        $access_check = TRUE;
      }
      // Open access if no platform access has been set.
      // @todo move this into _hosting_get_allowed_platforms
      // @todo confirm this logic
      elseif (!$unrestricted = db_query("SELECT cid
                                         FROM {hosting_platform_client_access}
                                         WHERE pid = :pid LIMIT 1", array(':pid' => $row->nid))->fetchField()) {
        $access_check = TRUE;
      }
    }
    if (!$access_check) {
      unset($profiles[$id]);
    }
  }
  if (!is_null($platform)) {
    $profiles = $platform_profiles;
  }
  reset($profiles);
  $return['profile'] = array_keys($profiles);

  if (!isset($node->profile)) {
    $node->profile = hosting_get_default_profile($return['profile'][0]);
  }

  // Filter the available platforms based on which clients the user has access to.
  if (!is_null($platform)) {
    $node->profile = $return['profile'][0];
    $return['platform'] = array($platform);
  }
  else {
    $options = array();
    $platforms = hosting_get_profile_platforms($node->profile, isset($node->check_profile_migrations) ? $node->check_profile_migrations : FALSE);
    if (count($platforms)) {
      foreach ($platforms as $nid => $title) {
        $platform = node_load($nid);

        if ($platform->platform_status != HOSTING_PLATFORM_LOCKED || user_access('create sites on locked platforms')) {
          if (!isset($platform->clients) || count(array_intersect(array_keys($user->client_id), $platform->clients)) || $user->uid == 1) {
            $options[] = $nid;
          }
        }
      }

      $return['platform'] = $options;
    }
  }

  if (!isset($node->platform) || !in_array($node->platform, $return['platform'])) {
    $node->platform = $return['platform'][0];
  }

  $return['site_language'] = array_keys((array) hosting_get_profile_languages($node->profile, $node->platform));

  drupal_alter('hosting_site_options', $return, $node);

  return $return;
}