Skip to content
Snippets Groups Projects
unl.module 40.9 KiB
Newer Older
function unl_send_site_created_email($site = NULL) {
  $shared_prefix = unl_get_shared_db_prefix();

  // If no site was specified, get the current site.
  if (!$site) {
    if (!is_array($GLOBALS['databases']['default']['default']['prefix'])) {
      return;
    }
    $db_prefix = $GLOBALS['databases']['default']['default']['prefix']['default'];
    $db_prefix = substr($db_prefix, 0, 0 - strlen($shared_prefix) - 1);
    $data = db_query(
      "SELECT * "
      . "FROM {$shared_prefix}unl_sites "
      . "WHERE db_prefix = :prefix ",
      array(':prefix' => $db_prefix)
    )->fetchAll();
    if (count($data) == 0) {
      return;
    }
    $site = $data[0];
  }
  $unl_site_created_email_address = unl_shared_variable_get('unl_site_created_email_address');
  $unl_site_created_alert_admins = unl_shared_variable_get('unl_site_created_alert_admins');
  $recipients = array();
  if ($unl_site_created_email_address) {
    $recipients[] = $unl_site_created_email_address;
  }
  if ($unl_site_created_alert_admins) {
    $role = user_role_load_by_name('Site Admin');
    $select = db_select('users_roles', 'r');
    $select->fields('r', array('uid'));
    $select->condition('r.rid', $role->rid);
    $uids = $select->execute()->fetchCol();
    $users = user_load_multiple($uids);
    foreach ($users as $user) {
      if (!$user->mail) {
        continue;
      }
      $recipients[] = $user->mail;
    }
  }

  foreach ($recipients as $recipient) {
    drupal_mail('unl', 'site_created', $recipient, language_default(), array('site' => $site), unl_shared_variable_get('site_mail'));
  }
}

/**
 * Custom function that outputs a simple "I'm still alive" page to check to see that drupal is working.
 */
function unl_still_alive() {
  header('Content-type: text/plain');
  echo '200 Still Alive';
}

/**
 * Custom function to return the current admin theme for use with hook_menu_alter().
 */
function _unl_get_admin_theme(){
  return variable_get('admin_theme', '0');
}

/**
 * Custom function for imce access on content administration pages since imce_user_page_access()
 * can't be used because only one file can be included in a hook_menu item ($items['admin/content/filebrowser'])
 */
function _unl_imce_file_browser_access() {
  global $user;
  $profile = imce_user_profile($user);
  return $profile['usertab'];
}

/**
 * Implements hook_block_view_alter()
 * @param array $data
 * @param stdClass $block
 */
function unl_block_view_alter(&$data, $block) {
  if ($block->module == 'system' && $block->delta == 'main-menu') {
    return unl_block_view_system_main_menu_alter($data, $block);
  }
}

/**
 * Tries to implement hook_block_view_MODULE_DELTA_alter, but since the delta contains a -,
 * this is actually called from unl_block_view_alter() for now.
 * Used to determine if a "sub-menu" should be used instead of the normal menu.
 * @param array $data
 * @param stdClass $block
 */
function unl_block_view_system_main_menu_alter(&$data, $block) {
  $current_menu_link = _unl_get_current_menu_link();
  if (!$current_menu_link) {
    return;
  }
  $submenu = _unl_get_current_submenu($data['content'], $current_menu_link->mlid);
  if ($submenu && $submenu['#original_link']['depth'] > 1) {
    $data['content'] = $submenu['#below'];
  }
  $data['content'] = _unl_limit_menu_depth($data['content'], 2);
}

/**
 * Return the mlid of the currently selected menu item.
 * If the current page has no menu item, use return the mlid of its parent instead.
 */
function _unl_get_current_menu_link() {
  $result = db_select('menu_links')
    ->fields('menu_links')
    ->condition('menu_name', 'main-menu')
    ->condition('link_path', current_path())
    ->execute()
    ->fetch();
    
  if (!$result) {
    return FALSE;
  }
  
  while (($result->hidden || $result->depth % 2 !== 0 || !$result->has_children) && $result->depth > 1) {
    $result = db_select('menu_links')
      ->fields('menu_links')
      ->condition('menu_name', 'main-menu')
      ->condition('mlid', $result->plid)
      ->execute()
      ->fetch();
  }
  
  return $result;
}

/**
 * Find the the submenu we are currently "drilled-down" to.
 * @param array $menu_links
 * @param int $current_mlid
 */
function _unl_get_current_submenu($menu_links, $current_mlid) {
  foreach (element_children($menu_links) as $index) {
    $menu_item = $menu_links[$index];
    if ($menu_item['#original_link']['mlid'] == $current_mlid) {
      return $menu_item;
    }
    $sub_menu = _unl_get_current_submenu($menu_item['#below'], $current_mlid);
    if ($sub_menu) {
      return $sub_menu;
    }
  }
  return FALSE;
}

/**
 * Remove any menu items that are more than $depth levels below the current root.
 * @param array $menu_links
 * @param int $depth
 */
function _unl_limit_menu_depth($menu_links, $depth) {
  if ($depth == 0) {
    return array();
  }
  
  foreach (element_children($menu_links) as $index) {
    $menu_links[$index]['#below'] = _unl_limit_menu_depth($menu_links[$index]['#below'], $depth - 1);
  }
  
  return $menu_links;
}





/**
 * Implements hook_block_info()
 */
function unl_block_info() {
  $blocks = array();
  
  $blocks['my_sites'] = array(
    'info' => 'My Sites',
    'cache' => DRUPAL_CACHE_PER_USER,
  );
  
  return $blocks;
}

/**
 * Implements hook_block_view()
 */
function unl_block_view($delta = '') {
  switch ($delta) {
    case 'my_sites':
      return unl_block_view_my_sites();
      break;
    default:
      return array();
  }
}

/**
 * Implements hook_block_view('my_sites').
 * Displays the list of sites/roles for the current user.
 */
function unl_block_view_my_sites()
{
  if (user_is_anonymous()) {
    return array();
  }
  
  require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'unl_site_creation.php';

  $block = array();
  $block['content'] = _unl_get_user_audit_content($GLOBALS['user']->name);
  
  return $block;
}