You are here

function hosting_site_insert in Hosting 7.4

Same name and namespace in other branches
  1. 5 site/hosting_site.module \hosting_site_insert()
  2. 6.2 site/hosting_site.nodeapi.inc \hosting_site_insert()
  3. 7.3 site/hosting_site.nodeapi.inc \hosting_site_insert()

Implements hook_insert().

1 call to hosting_site_insert()
hosting_site_update in site/hosting_site.nodeapi.inc
Implements hook_update().

File

site/hosting_site.nodeapi.inc, line 269
Site nodeapi implementations.

Code

function hosting_site_insert(&$node) {
  $client = hosting_get_client($node->client);
  $node->client = $client->nid;
  $node->site_language = isset($node->site_language) ? $node->site_language : 'en';

  // If the cron_key is set use it, otherwise generate a new one.
  $node->cron_key = isset($node->cron_key) ? $node->cron_key : '';

  // Ensure that the last_cron value is set.
  $node->last_cron = isset($node->last_cron) ? $node->last_cron : 0;

  // Provide a dummy profile, e.g. for hosting-import.
  $node->profile = isset($node->profile) ? $node->profile : 0;

  // If platform NID is not defined, but platform data is available,
  // create the platform.
  if (empty($node->platform) && !empty($node->platform_node) && !empty($node->platform_node->publish_path)) {

    // If a platform exists for the given path, use that.
    $existing_platform_nid = db_select('hosting_platform', 'p')
      ->condition('publish_path', $node->platform_node->publish_path)
      ->condition('status', HOSTING_PLATFORM_ENABLED)
      ->fields('p', array(
      'nid',
    ))
      ->execute()
      ->fetchField();

    // Use the existing platform NID.
    if ($existing_platform_nid) {
      $node->platform = $existing_platform_nid;
    }
    else {

      // If platform_status hasn't been explicitly set,
      // assume platform status matches site status:
      // If developer creates a site node that's disabled,
      // the platform should be too.
      if (!isset($node->platform_node->platform_status) && isset($node->site_status)) {
        $node->platform_node->platform_status = $node->site_status;
      }

      // If web_server hasn't been explicity set, use hostmaster's web server.
      if (!isset($node->platform_node->web_server)) {
        $hostmaster = hosting_context_load('hostmaster');
        $hostmaster_platform = node_load($hostmaster->platform);
        $node->platform_node->web_server = $hostmaster_platform->web_server;
      }

      // If platform title has not been set, generate one from the site title.
      if (empty($node->platform_node->title)) {
        $node->platform_node->title = 'platform_' . preg_replace("/[!\\W]/", "", $node->title);
      }

      // If platform UID has not been set, use site UID.
      if (empty($node->platform_node->uid)) {
        $node->platform_node->uid = $node->uid;
      }
      $node->platform_node->type = 'platform';

      // Don't queue a verify task on save. Site verify now runs platform verify first.
      $node->platform_node->no_verify = TRUE;
      if ($node->platform_node = node_submit($node->platform_node)) {
        node_save($node->platform_node);
      }
      $node->platform = $node->platform_node->nid;
    }

    // If db_server hasn't been explicity set, use hostmaster's web server.
    if (!isset($node->db_server)) {
      $hostmaster = hosting_context_load('hostmaster');
      $node->db_server = $hostmaster->db_server;
    }
  }

  // If there is no platform NID and no publish path, throw an exception.
  if (empty($node->platform) && empty($node->platform_node->publish_path)) {
    throw new Exception('Site nodes require either platform or platform_node->publish_path property');
  }
  $id = db_insert('hosting_site')
    ->fields(array(
    'vid' => $node->vid,
    'nid' => $node->nid,
    'client' => $node->client,
    'db_server' => $node->db_server,
    'db_name' => isset($node->db_name) ? $node->db_name : '',
    'platform' => $node->platform,
    'profile' => $node->profile,
    'language' => $node->site_language,
    'last_cron' => $node->last_cron,
    'cron_key' => $node->cron_key,
    'status' => isset($node->site_status) ? $node->site_status : HOSTING_SITE_QUEUED,
    'verified' => isset($node->verified) ? $node->verified : 0,
    'file_public_path' => $node->file_public_path ?: '',
    'file_private_path' => $node->file_private_path ?: '',
    'file_temporary_path' => $node->file_temporary_path ?: '',
  ))
    ->execute();
}