Commit 6b16d972 authored by salvamomo's avatar salvamomo

Initial commit. First working version, with 2 simple blocks (save and reset buttons)

parents
name = user_homepage
description = Provides users with the ability to choose their own homepage.
core = 7.x
\ No newline at end of file
<?php
/**
* @file
* user_homepage .install
* Install and Uninstall hooks for user_homepage module.
*/
/**
* Implements hook_schema().
*/
function user_homepage_schema() {
$schema['user_homepage'] = array(
'description' => "Storage of user's custom homepages",
'fields' => array(
'uid' => array(
'description' => 'The User ID for which a custom homepage is stored.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
),
'path' => array(
'description' => 'The internal stored path.',
'type' => 'varchar',
'length' => 255,
'not null' => FALSE,
),
),
'primary key' => array('uid'),
);
return $schema;
}
<?php
/**
* @file
* user_homepage module
* Provides users with the ability to choose their own homepage.
*/
/*
* Implements hook_user_login().
*/
function user_homepage_user_login() {
// We don't redirect if there's already a destination.
if (isset($_GET['destination'])) {
drupal_goto($_GET['destination']);
}
// Perform the custom redirect.
else if ($homepage = user_homepage_get_homepage()) {
$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.
*/
function user_homepage_button($form) {
$form = array();
$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.
*/
function user_homepage_reset_button($form) {
$form = array();
$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'))
->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);
// @TODO: Store the encoded url, stripping the first '/', as it will be added
//when calling drupal_goto(). Check if there's a cleaner way to achieve this.
$encoded_url = rawurlencode(substr($homepage_url, 1));
return $encoded_url;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment