Posted by josepvalls on December 17, 2009 at 11:58am
1 follower
Jump to:
| Project: | User Referral |
| Version: | 6.x-1.x-dev |
| Component: | Miscellaneous |
| Category: | feature request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | active |
Issue Summary
I found myself using the ?destination feature very often in my site and I hacked a little the module to use a parameter in the URL of any node instead of the referral URL.
First, I created a block with the link to the current page and a URL parameter containing the referral code (_referral_uid2ref($user->uid);) so I can include it in any page. Then I added hook_nodeapi to check for the referral code in each page and set the cookie.
Is there any cleaner way of doing this? Is there something I should be aware of that I might had ignored? I hope this code may be helpful to somebody or somehow included in the module.
These are some of my modifications:
<?php
function referral_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL){
global $user;
if ($op=="view" && $a4==true){
if (isset($_GET[REFERRAL_QUERY_PARAMETER])){
if (!$user->uid) {
// User has not logged in, or registered yet
$uid = _referral_ref2uid($_GET[REFERRAL_QUERY_PARAMETER]);
if (($uid) && is_numeric($uid)) {
// Set the referral cookie
referral_set_cookie($uid);
}
} else drupal_set_message(t('You followed a referral link but you are already registered, pelase, update your bookmarks.'),"info");
}
}
}
?>
Comments
#1
By the way, I saw that the module relies on cookies enabled. I was wondering if it may be worth adding the referral parameter to the url of ALL the links of the current page (and future visited pages and so on), so the parameter is NOT lost in case cookies are not enabled and then a check on the user registration hook to record the referral (double checked). Anyone else had this concern? Any ideas how I could approach this?
UPDATE: I got impatient and tried an approach myself. This is what my user_save function looks like:
<?php
function _referral_user_save($uid) {
if (isset($_COOKIE[REFERRAL_COOKIE])) {
// Retrieve referral info from the cookie
$cookie = unserialize($_COOKIE[REFERRAL_COOKIE]);
} else if (isset($_GET[REFERRAL_QUERY_PARAMETER])){
// No cookie, but there is a parameter
$referral_uid = _referral_ref2uid($_GET[REFERRAL_QUERY_PARAMETER]);
if (($referral_uid) && is_numeric($referral_uid)) {
$cookie = array(
'uid' => $referral_uid,
'timestamp' => time(),
'ip' => ip_address(),
'referer' => $_SERVER['HTTP_REFERER']
);
}
}
if (empty($cookie)) {
// Nothing to do ...
return;
}
//Rest of the function as it was
?>
Then I made a block. The goal is to convert it into some sort of send to a friend-like thing, but this works for now. I'd be glad to submit a patch (never done it before) if someone could tell me what they think first.
<?phpfunction referral_block($op = 'list', $delta = 0) {
$title[0] = t('Top referring users');
switch ($op) {
case 'list':
$block[0]['info'] = $title[0];
$block[1]['info'] = t('Referral generic link block');
$block[1]['cache'] = BLOCK_NO_CACHE;
return $block;
case 'view':
switch ($delta) {
case 0:
$block['subject'] = $title[0];
$block['content'] = referral_block_content();
break;
case 1:
$block['subject'] = t('Refer a friend');
GLOBAL $user;
$path = (arg(0) == 'node' && is_numeric(arg(1))) ? 'node/' . arg(1) : 'register';
$link = url($path, array('absolute'=>true,'query'=>array(REFERRAL_QUERY_PARAMETER=>_referral_uid2ref($user->uid))));
$block['content'] = "<div>" . t('Send a link to this content to a friend:') . '<br/>' ;
$block['content'] .= referral_selectable_textbox($link);
$block['content'] .= '<br/>' . t('If they register, you will be accounted as their referral.');
$block['content'] .= "</div>";
}
return $block;
}
}
function referral_selectable_textbox($text,$class='selectable'){
return '<input type="text" class="' . $class . '" onclick="this.focus(); this.select();" value="' . $text . '" style="width: 100%;"/>';
}
?>
btw, forgot to mention that I also added:
define('REFERRAL_QUERY_PARAMETER', 'referral');