You are here

minisite.module in Mini site 7

Same filename and directory in other branches
  1. 8 minisite.module

Module file.

File

minisite.module
View source
<?php

/**
 * @file
 * Module file.
 */
define('MINISITE_UPLOADPATH', 'minisite/upload');
define('MINISITE_ASSETPATH', 'minisite/static');
define('MINISITE_EXTENSIONS_WHITELIST', 'html htm js css png jpg gif svg pdf doc docx ppt pptx xls xlsx tif xml txt woff woff2 ttf eot');
define('MINISITE_EXTENSIONS_BLACKLIST', 'exe scr bmp');
define('MINISITE_EXTENSIONS_NOREWRITE', 'pdf doc docx ppt pptx xls xlsx tif xml txt');
define('MINISITE_MAXFILESIZE', format_size(file_upload_max_size()));

// Load all Field module hooks for Minisite.
require_once dirname(__FILE__) . '/includes/minisite.field.inc';

// Load all theme hooks for Minisite.
require_once dirname(__FILE__) . '/includes/minisite.theme.inc';

/**
 * Implements hook_help().
 */
function minisite_help($path, $arg) {
  switch ($path) {
    case 'admin/help#minisite':
      $output = '<p>' . t("Provides the ability to upload arbitrary 'minisites' to a Drupal website.") . '</p>';
      return $output;
  }
}

/**
 * Minisite site information lookup.
 */
function minisite_site_lookup() {
  $minisite_info =& drupal_static(__FUNCTION__);
  if (!isset($minisite_info)) {

    // Load module inc file.
    module_load_include('inc', 'minisite', 'includes/minisite.site');
    return minisite_site_lookup_path();
  }
}

/**
 * Load minisite.
 *
 * Possible side effect of issuing a redirect if the minisite is not aliased.
 *
 * @param boolean $redirect
 *   If this is TRUE and the minisite is not aliased this function will perform
 *   the redirect.
 *
 * @return mixed
 *  - FALSE is the minisite is not found or invalid
 *  - An array of info if the minisite is found and aliased, or $redirect is FALSE
 *  - NULL if the minisite is found, unaliased and $redirect is TRUE
 */
function minisite_site_load($redirect = TRUE) {
  $minisite_info = minisite_site_lookup();
  if (empty($minisite_info) || empty($minisite_info['mid']) || empty($minisite_info['source'])) {
    return FALSE;
  }
  if (!empty($minisite_info['alias_status']) && $minisite_info['alias_status'] == 1) {
    return $minisite_info;
  }
  if ($redirect) {
    $minisite_site_url = _minisite_site_file_public_path_base_url() . $minisite_info['source'] . '/index.html';
    drupal_goto($minisite_site_url);
  }
  else {
    return $minisite_info;
  }
}

/**
 * Save and extract minisite.
 */
function minisite_site_save($entity_type, $entity, $field, $instance, $langcode, &$items) {
  foreach ($items as &$item) {
    if (isset($item['fid'])) {
      $file = file_load($item['fid']);
      if (!empty($file)) {

        // Load module inc file.
        module_load_include('inc', 'minisite', 'includes/minisite.site');
        list($minisite_extract_path, $minisite_base_path, $minisite_listing) = minisite_site_asset_info($item['fid'], FALSE);

        // Extract files.
        minisite_site_extract($file, $minisite_extract_path);

        // Save specific settings in data.
        $item['site_path'] = $minisite_extract_path . '/' . $minisite_base_path;
        $minisite_field_data = array(
          'minisite_alias_status' => isset($item['alias_status']) ? $item['alias_status'] : 0,
          'minisite_extract_path' => $minisite_extract_path,
          'minisite_base_path' => $minisite_base_path,
        );
        $item['data'] = serialize($minisite_field_data);
        $minisite_info = array(
          'minisite_extract_path' => $minisite_extract_path,
          'minisite_base_path' => $minisite_base_path,
          'minisite_listing' => $minisite_listing,
        );

        // Save assets information.
        drupal_register_shutdown_function('minisite_site_save_asset', $entity_type, $entity, $field['field_name'], $langcode, $item, $minisite_info);
      }
    }
  }
}

/**
 * Save minisite asset into database.
 */
function minisite_site_save_asset($entity_type, $entity, $field_name, $langcode, $item, $minisite_info) {
  $entity_uri = entity_uri($entity_type, $entity);
  $entity_path = $entity_uri['path'];
  $entity_path_alias = drupal_get_path_alias($entity_path, $langcode);
  list($entity_id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);

  // Prepare keys.
  $keys = array(
    'entity_type' => $entity_type,
    'bundle' => $bundle,
    'entity_id' => $entity_id,
    'revision_id' => $vid,
    'minisite_field_name' => $field_name,
    'minisite_fid' => $item['fid'],
    'language' => $langcode,
    'source' => $minisite_info['minisite_extract_path'] . '/' . $minisite_info['minisite_base_path'],
  );

  // Prepare fields.
  $fields = array(
    'alias' => $entity_path_alias,
    'alias_status' => isset($item['alias_status']) ? $item['alias_status'] : 0,
  );

  // Save minisite landing page information.
  db_merge('minisite_asset')
    ->key($keys)
    ->fields($fields)
    ->execute();

  // Save minisite assets information.
  if (isset($item['alias_status']) && $item['alias_status'] == 1) {
    foreach ($minisite_info['minisite_listing'] as $minisite_file) {
      if (strpos(strtolower($minisite_file), '.html') !== FALSE || strpos(strtolower($minisite_file), '.htm') !== FALSE) {
        $keys['source'] = $minisite_info['minisite_extract_path'] . '/' . $minisite_file;
        $search = $minisite_info['minisite_base_path'];
        $replace = $entity_path_alias;
        $fields['alias'] = str_replace($search, $replace, $minisite_file);
        db_merge('minisite_asset')
          ->key($keys)
          ->fields($fields)
          ->execute();
      }
    }
  }
}

/**
 * Delete minisite.
 */
function minisite_site_delete($item, $field, $entity_type, $id) {
  $file = (object) $item;
  $file_usage = file_usage_list($file);
  if ($file->status == 0 || !empty($file_usage['minisite'])) {
    if (!empty($file)) {

      // Delete database records.
      db_delete('minisite_asset')
        ->condition('minisite_fid', $file->fid)
        ->execute();

      // Load module inc file.
      module_load_include('inc', 'minisite', 'includes/minisite.site');
      $path = 'public://' . MINISITE_ASSETPATH . '/' . minisite_site_hash($file);
      file_unmanaged_delete_recursive($path);
    }
  }
}

/**
 * Build minisite.
 */
function minisite_site_build($minisite_info) {
  $minisite_html =& drupal_static(__FUNCTION__);
  if (!isset($minisite_html)) {
    $minisite_html = array();
  }
  if (!isset($minisite_html[$minisite_info['mid']])) {

    // Load module inc file.
    module_load_include('inc', 'minisite', 'includes/minisite.site');
    $html = minisite_site_parse($minisite_info);
    if (empty($html)) {
      $minisite_html[$minisite_info['mid']] = '';
      _minisite_fast_404();
    }
    else {
      $minisite_html[$minisite_info['mid']] = $html;
    }
  }
  return $minisite_html[$minisite_info['mid']];
}

Functions

Namesort descending Description
minisite_help Implements hook_help().
minisite_site_build Build minisite.
minisite_site_delete Delete minisite.
minisite_site_load Load minisite.
minisite_site_lookup Minisite site information lookup.
minisite_site_save Save and extract minisite.
minisite_site_save_asset Save minisite asset into database.

Constants