user_homepage.module 5.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
<?php

/**
 * @file
 * user_homepage module
 * Provides users with the ability to choose their own homepage.
 */

/*
 * Implements hook_user_login().
 */
12
function user_homepage_user_login(&$edit, $account) {
13 14 15 16 17
  // We don't redirect if there's already a destination.
  if (isset($_GET['destination'])) {
    drupal_goto($_GET['destination']);
  }
  // Perform the custom redirect.
18
  else if ($homepage = user_homepage_get_homepage($account->uid)) {
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    $parsed_homepage = parse_url($homepage);
    $options = array();
    if (isset($parsed_homepage['query'])) {
      $options = array(
        'query' => drupal_get_query_array($parsed_homepage['query']),
      );
    }
    drupal_goto($parsed_homepage['path'], $options);
  }
}

/**
 * Implements hook_permission().
 */
function user_homepage_permission() {
  return array(
    'configure own homepage' =>  array(
      'title' => t('Configure own homepage'),
      'description' => t('Allows users to choose the page to be redirected to after login.'),
    ),
  );
}

/**
 * Implements hook_block_info().
 */
function user_homepage_block_info() {
  // "Save as homepage" button.
  $blocks['user_homepage_button'] = array(
    'info' => t('[User Homepage] - Save as homepage button.'),
    'cache' => DRUPAL_CACHE_GLOBAL,
  );

  // "Reset" button
  $blocks['user_homepage_reset_button'] = array(
    'info' => t('[User Homepage] - Reset homepage button.'),
    'cache' => DRUPAL_CACHE_GLOBAL,
  );
  return $blocks;
}

/**
 * Implements hook_block_view().
 */
function user_homepage_block_view($delta = '') {
  $block = array();
  switch ($delta) {
    case 'user_homepage_button':
      // Show only if the user has permissions to use this feature.
      if (user_access('configure own homepage')) {
        $block['subject'] = t('Make this your homepage.');
        $block['content'] = drupal_get_form('user_homepage_button');
      }
      break;
    case 'user_homepage_reset_button':
      // Show only if the user has permissions to use this feature, and a
      // homepage configured.
      if (user_access('configure own homepage') && user_homepage_get_homepage()) {
        $block['subject'] = t('Reset your homepage.');
        $block['content'] = drupal_get_form('user_homepage_reset_button');
      }
      break;
  }
  return $block;
}

/**
 * Simple "Save as homepage" button form.
 *
 * @return array The form array.
 */
90
function user_homepage_button($form, &$form_state) {
91 92 93 94 95 96 97 98 99 100 101 102 103
  $form['user_homepage_button'] = array(
    '#type' =>  'submit',
    '#value' => t('Save as homepage'),
  );

  return $form;
}

/**
 * Simple "Save as homepage" button form.
 *
 * @return array The form array.
 */
104
function user_homepage_reset_button($form, &$form_state) {
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
  $form['user_homepage_reset_button'] = array(
    '#type' =>  'submit',
    '#value' => t('Unset configured homepage'),
  );

  return $form;
}

/**
 * Submit handler for user_homepage_button form.
 */
function user_homepage_button_submit($form, &$form_state) {
  // Get the encoded url that will be stored on the database.
  $encoded_url = user_homepage_build_homepage_path_from_request();

  // Create or Update entry for the user on the user_homepage table.
  user_homepage_set_homepage($encoded_url);
}

/**
 * Submit handler for user_homepage_reset_button form.
 */
function user_homepage_reset_button_submit($form, &$form_state) {
  // Set the custom path for the user to none on the user_homepage table.
  user_homepage_set_homepage();
}

/**
 * Sets the path of the homepage for a given user (current user if no user_id is
 * provided).
 *
 * @param string $path Path of the homepage to be set.
 * @param string $user_id ID of the user whose homepage is going to be stored.
 */
function user_homepage_set_homepage($path = '', $user_id = '') {
  if (empty($user_id)) {
    global $user;
    $user_id = $user->uid;
  }

  // db_merge() tackles the Insert or Update action.
  $merge_result = db_merge('user_homepage')
    ->key(array('uid' => $user_id))
    ->fields(array(
      'uid' => $user_id,
      'path' => $path,
    ))
    ->execute();

  if ($merge_result == MergeQuery::STATUS_INSERT) {
    drupal_set_message(t('Page saved successfully as homepage.'));
  }
  else if ($merge_result == MergeQuery::STATUS_UPDATE) {
    drupal_set_message(t('Homepage successfully updated.'));
  }
  else {
    drupal_set_message(t('Page couldn\'t be saved as homepage. Try again please.', 'error'));
  }
}

/**
 * Gets the path of the homepage for a given user (current user if no user_id is
 * provided).
 *
 * @param string $user_id ID of the user whose homepage is needed.
 * @return string The homepage path for the specified user.
 */
function user_homepage_get_homepage($user_id = '') {
  if (empty($user_id)) {
    global $user;
    $user_id = $user->uid;
  }

  $query = db_select('user_homepage', 'uh')
    ->fields('uh', array('path'))
180
    ->condition('uh.uid', $user_id)
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
    ->execute();
  $homepage_path = $query->fetchField();
  $decoded_path = rawurldecode($homepage_path);

  return $decoded_path;
}

/**
 * Gets the URL of the page being viewed, including any query parameters, and
 * returns it encoded.
 *
 * @return string Encoded url to be stored on the user_homepage table.
 */
function user_homepage_build_homepage_path_from_request() {
  // Retrieve the current page, including query parameters. Alias is set to TRUE
  // to prevent url() from translating any internal path to its alias.
  $options = array(
    'query' => drupal_get_query_parameters(),
    'alias' => TRUE,
  );
  $homepage_url = url(current_path(), $options);

203 204 205
  // Store the encoded url, stripping the first '/', as it will be added when
  // calling drupal_goto().
  // TODO: Check if there's a cleaner way to achieve this without stripping '/'.
206 207 208 209
  $encoded_url = rawurlencode(substr($homepage_url, 1));

  return $encoded_url;
}