You are here

function hosting_site_available_options in Hosting 7.4

Same name and namespace in other branches
  1. 6.2 site/hosting_site.form.inc \hosting_site_available_options()
  2. 7.3 site/hosting_site.form.inc \hosting_site_available_options()

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()
hosting_site_validate in site/hosting_site.form.inc
Implements hook_validate().
hosting_ssl_nodeapi_site_validate in web_server/ssl/hosting_ssl.nodeapi.inc
Implements hook_nodeapi_TYPE_OP().
hosting_task_migrate_form in migrate/hosting_migrate.module
Implements hook_form().
_hosting_site_form_check in site/hosting_site.module

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.
  if (!module_exists('hosting_package')) {
    return $return;
  }
  $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\n                        FROM {hosting_package_instance} i\n                        JOIN {hosting_package} p\n                        ON p.nid = i.package_id\n                        JOIN {hosting_platform} l\n                        ON l.nid = i.rid\n                        WHERE i.package_id = :package_id\n                        AND p.package_type = :package_type\n                        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;
      }
      elseif (!($unrestricted = db_query("SELECT cid\n                                         FROM {hosting_platform_client_access}\n                                         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;
}