Skip to content
Snippets Groups Projects
unl.module 43.8 KiB
Newer Older
    return;
  }
  
  if (drupal_lookup_path('source', dirname($path))) {
    drupal_goto(dirname($path));
    return;
  }
  
  if (dirname($path) == '.') {
    drupal_goto('<front>');
    return;
  }
/**
 * Implementation of hook_mail().
 */
function unl_mail($key, &$message, $params) {
  if ($key == 'site_created') {
    $site = $params['site'];
    $uri = $site->uri;
    $site_admin = $site->site_admin;
    $department = $site->department;
    $message['subject'] = 'New UNLcms site for ' . $department;
    $message['body'][] = <<<EOF
To $site_admin,

Thank you for registering your site at UNLcms. You may now log in using your myUNL information by clicking the link below or copying and pasting it to your browser:
$uri
You must log in in order manage your web site and edit the content. The "Login" link is at the top of the page and you log in using your myUNL information.
For information on how to manage your site & content, please view the online how-to videos found in the menu at the top of the following page, within the category of "Get Help":
http://unlcms.unl.edu

When this new site was created, an attempt was made to copy the content from your current site to this new site. You will want to review all of the content to verify that it is complete and that it is presented correctly.  This import process is dependent on the current's site using and adherence to UNL Templates.

Once your content has been finalized and your are ready to go public with your new site, please contact us to activate the proper public URL (cleaner and shorter). This current URL you've been given will not be indexed or found in search engines until the proper public URL has been established.

Please let us know if you have suggestions or questions.

Thank you,
EOF;

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);
  }
/**
 * 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;
}