Index: shorturl.admin.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/shorturl/Attic/shorturl.admin.inc,v retrieving revision 1.1.2.1 diff -u -r1.1.2.1 shorturl.admin.inc --- shorturl.admin.inc 8 Aug 2009 06:52:50 -0000 1.1.2.1 +++ shorturl.admin.inc 26 Sep 2009 18:59:21 -0000 @@ -12,23 +12,56 @@ */ function shorturl_admin_settings_form(&$form_state) { - $form['shorturl_domain'] = array( + $form['shorturl_engine'] = array( + '#type' => 'fieldset', + '#title' => t('Engine Options'), + '#collapsible' => TRUE, + '#collapsed' => FALSE, + ); + $form['shorturl_engine']['shorturl_domain'] = array( '#type' => 'textfield', '#title' => t('Short Domain:'), '#default_value' => variable_get('shorturl_domain', $_SERVER['HTTP_HOST']), '#required' => TRUE, '#description' => t('The domain to be used in short URLs'), ); - - $form['shorturl_start_from'] = array( + $form['shorturl_engine']['shorturl_start_from'] = array( '#type' => 'textfield', '#title' => t('Start From ID#:'), '#default_value' => variable_get('shorturl_start_from', 3844), '#required' => TRUE, '#description' => t('3844 is approximately the first 3-characters long short URI. Indicate a number that is less or more, depending on your needs.'), ); - - + $form['shorturl_engine']['shorturl_allow_duplicates'] = array( + '#type' => 'checkbox', + '#title' => t('Allow duplicate submissions'), + '#description' => t('Allows user to create more than one link to the same URL. This is important for tracking links by users and for targeting different links to different roups for the purpose of comparing metrics.'), + '#default_value' => variable_get('shorturl_allow_duplicates', 0), + '#return_value' => 1, + ); + $form['shorturl_statistics'] = array( + '#type' => 'fieldset', + '#title' => t('Statistics Options'), + '#collapsible' => TRUE, + '#collapsed' => FALSE, + ); + $form['shorturl_statistics']['shorturl_date_format'] = array( + '#type' => 'select', + '#title' => t('Date format'), + '#options' => array( + 'small' => format_date($time, 'small'), + 'medium' => format_date($time, 'medium'), + 'large' => format_date($time, 'large'), + 'custom' => t('Custom'), + ), + '#default_value' => variable_get('shorturl_date_format', 'small'), + ); + $form['shorturl_statistics']['shorturl_custom_date_format'] = array( + '#type' => 'textfield', + '#title' => t('Custom date format'), + '#description' => t('If "Custom", see the PHP docs for date formats.'), + '#default_value' => variable_get('shorturl_custom_date_format', ''), + ); return system_settings_form($form); } Index: shorturl.install =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/shorturl/shorturl.install,v retrieving revision 1.1.2.1 diff -u -r1.1.2.1 shorturl.install --- shorturl.install 8 Aug 2009 06:52:50 -0000 1.1.2.1 +++ shorturl.install 25 Oct 2009 21:08:08 -0000 @@ -1,8 +1,6 @@ 'ShortURL Links Table.', 'fields' => array( 'lid' => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE, 'length' => 22), + 'uid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, 'length' => 22), 'orig_url' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''), + 'title' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE, 'default' => ''), + 'description' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE, 'default' => ''), + 'keywords' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE, 'default' => ''), 'created' => array('type' => 'int', 'not null' => TRUE, 'default' => 0), - 'remote_ip' => array('type' => 'varchar', 'length' => 20, 'not null' => FALSE, 'default' => ''), + 'remote_ip' => array('type' => 'varchar', 'length' => 20, 'not null' => FALSE, 'default' => ''), + 'clicks' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, 'length' => 22), ), 'indexes' => array( 'shorturl_orig_url' => array('orig_url'), + 'shorturl_uid' => array('uid'), + 'shorturl_keywords' => array('keywords'), ), 'primary key' => array('lid'), ); @@ -26,34 +31,74 @@ $schema['shorturl_access'] = array( 'fields' => array( 'aid' => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE, 'length' => 22), - 'url_id' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, 'length' => 22), + 'url_id' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, 'length' => 22), 'url_key' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''), 'remote_ip' => array('type' => 'varchar', 'length' => 20, 'not null' => TRUE, 'default' => ''), - 'referer' => array('type' => 'text', 'not null' => TRUE, 'size' => 'normal'), - 'browser' => array('type' => 'text', 'not null' => TRUE, 'size' => 'normal'), - 'access_time' => array('type' => 'int', 'not null' => TRUE, 'default' => 0), + 'referrer' => array('type' => 'text', 'not null' => TRUE, 'size' => 'normal'), + 'browser' => array('type' => 'text', 'not null' => TRUE, 'size' => 'normal'), + 'access_time' => array('type' => 'int', 'not null' => TRUE, 'default' => 0), ), 'primary key' => array('aid'), ); - + return $schema; } - - /** + +/** * Implementation of hook_install() */ function shorturl_install() { drupal_install_schema('shorturl'); - + // Set this module at the top! db_query("UPDATE {system} SET weight = -100 WHERE name = 'shorturl'"); - + } - /** +/** * Implementation of hook_uninstall(). */ function shorturl_uninstall() { drupal_uninstall_schema('shorturl'); } +/** + * Add user ID to shorturl links table + */ +function shorturl_update_1() { + $ret = array(); + db_add_field($ret, 'shorturl_link', 'uid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, 'length' => 22), array('indexes' => array('shorturl_uid' => array('uid')))); + return $ret; +} + +/** + * Add title, description, and keywords to shorturl links table + */ +function shorturl_update_2() { + $ret = array(); + db_add_field($ret, 'shorturl_link', 'title', array('type' => 'varchar', 'length' => 255, 'not null' => FALSE, 'default' => '')); + db_add_field($ret, 'shorturl_link', 'description', array('type' => 'varchar', 'length' => 255, 'not null' => FALSE, 'default' => '')); + db_add_field($ret, 'shorturl_link', 'keywords', array('type' => 'varchar', 'length' => 255, 'not null' => FALSE, 'default' => ''), array('indexes' => array('shorturl_keywords' => array('keywords')))); + return $ret; +} + +/** + * Correct typo in field name (referer -> referrer) + */ +function shorturl_update_3() { + $ret = array(); + db_change_field($ret, 'shorturl_access', 'referer', 'referrer', array('type' => 'text', 'not null' => TRUE, 'size' => 'normal')); + return $ret; +} + +/** + * Add clickthrough count to shorturl links table + */ +function shorturl_update_4() { + $ret = array(); + db_add_field($ret, 'shorturl_link', 'clicks', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, 'length' => 22)); + + $sql = 'UPDATE {shorturl_link} sl SET sl.clicks = (SELECT COUNT(*) FROM {shorturl_access} WHERE url_id = sl.lid)'; + $result = db_query($sql); + return $ret; +} \ No newline at end of file Index: shorturl.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/shorturl/shorturl.module,v retrieving revision 1.1.2.1 diff -u -r1.1.2.1 shorturl.module --- shorturl.module 8 Aug 2009 06:52:50 -0000 1.1.2.1 +++ shorturl.module 25 Oct 2009 21:34:16 -0000 @@ -1,9 +1,4 @@ TRUE)); //Let drupal make relative URLs absolute } + $remote_ip = (empty($_SERVER['REMOTE_ADDR'])) ? '' : $_SERVER['REMOTE_ADDR']; + $referrer = (empty($_SERVER['HTTP_REFERER'])) ? '' : $_SERVER['HTTP_REFERER']; + $browser = (empty($_SERVER['HTTP_USER_AGENT'])) ? '' : $_SERVER['HTTP_USER_AGENT']; + db_query("INSERT INTO {shorturl_access} (`url_id`, `url_key`, `remote_ip`, `referrer`, `browser`, `access_time`) VALUES (%d, '%s', '%s', '%s', '%s', %d) ", $indx, $token, $remote_ip, $referrer, $browser, (int)time()); + db_query("UPDATE {shorturl_link} SET clicks = clicks + 1 WHERE lid = %d", $indx); + + //echo $orig_url; drupal_goto($orig_url); exit(); } } - } -/** -* hook_perm implementation -*/ +/** + * Implementation of hook_perm(). + */ function shorturl_perm() { - return array('administer shorturl'); + return array('view own statistics', 'view all statistics', 'view link details', 'export all statistics', 'export own statistics', 'administer shorturl'); } /** -* hook_menu() implementation -*/ + * Implementation of hook_menu(). + */ function shorturl_menu() { - + + $items = array(); + $items['shorturl'] = array( + 'title' => 'Short URL Statistics', + 'page callback' => 'shorturl_statistics', + 'access arguments' => array('view all statistics'), + 'type' => MENU_NORMAL_ITEM + ); + $items['shorturl/all'] = array( + 'title' => 'All users', + 'page callback' => 'shorturl_statistics', + 'page arguments' => array('all'), + 'access arguments' => array('view all statistics'), + 'type' => MENU_LOCAL_TASK + ); + $items['shorturl/all/export'] = array( + 'title' => 'Export all to Excel', + 'description' => t('Export link details as a CSV file'), + 'page callback' => 'shorturl_statistics_export', + 'access arguments' => array('export all statistics'), + 'type' => MENU_CALLBACK + ); + $items['shorturl/user'] = array( + 'title' => 'Your URLs', + 'page callback' => 'shorturl_statistics', + 'page arguments' => array('all'), + 'access arguments' => array('view own statistics'), + 'type' => MENU_DEFAULT_LOCAL_TASK + ); + $items['shorturl/user/%user'] = array( + 'title' => 'Short URL Statistics', + 'page callback' => 'shorturl_statistics', + 'page arguments' => array(2), + 'access arguments' => array('view all statistics'), + 'type' => MENU_CALLBACK + ); + $items['shorturl/user/%user/export'] = array( + 'title' => 'Export to Excel', + 'description' => t('Export link details as a CSV file'), + 'page callback' => 'shorturl_statistics_export', + 'page arguments' => array(2), + 'access arguments' => array('export own statistics'), + 'type' => MENU_CALLBACK + ); + $items['shorturl/list/%shorturl'] = array( + 'title' => 'Short URL Statistics', + 'page callback' => 'shorturl_statistics', + 'page arguments' => array('all', 2), + 'access arguments' => array('view own statistics'), + 'type' => MENU_CALLBACK + ); + $items['shorturl/link/%shorturl'] = array( + 'title' => 'Short URL Statistics', + 'page callback' => 'shorturl_statistics_link', + 'page arguments' => array(2), + 'access arguments' => array('view link details'), + 'type' => MENU_CALLBACK + ); $items['admin/settings/shorturl'] = array( 'title' => 'ShortURL', 'description' => 'Configure the settings for ShortURL.', @@ -52,12 +110,242 @@ 'page arguments' => array('shorturl_admin_settings_form'), 'access arguments' => array('administer shorturl'), 'file' => 'shorturl.admin.inc', - 'type' => MENU_NORMAL_ITEM, ); - + $items['admin/reports/shorturl'] = array( + 'title' => 'Short URL Statistics', + 'page callback' => 'shorturl_statistics', + 'page arguments' => array('all'), + 'access arguments' => array('administer shorturl'), + ); return $items; } +function shorturl_load($lid) { + return db_fetch_object(db_query("SELECT * FROM {shorturl_link} WHERE lid = '%d'", $lid)); +} + +/** + * Implementation of hook_block(). + */ +function shorturl_block($op = 'list', $delta = 0, $edit = array()) { + switch ($op) { + case 'list': + $block['shorturl_totals']['info'] = t('Short URL Totals'); + return $block; + case 'view': + if ($delta == 'shorturl_totals') { + $block['subject'] = t('Short URL Totals'); + + $all_links = db_result(db_query('SELECT COUNT(lid) FROM {shorturl_link}')); + $all_clicks = db_result(db_query('SELECT COUNT(aid) FROM {shorturl_access}')); + + $block['content'] = '
'. t('!site has shortened !links URLs that have been clicked !clicks times.', array('!site' => variable_get('site_name', 'Drupal'), '!links' => ''. $all_links .'', '!clicks' => ''. $all_clicks .'')) .'
'; + return $block; + } + } +} + +function shorturl_statistics($user = NULL, $link = NULL) { + if (empty($user)) { + global $user; + drupal_set_title(($user->uid != 0) ? t('Your recent links') : t('Recently Shortened Links')); + $access = 'own'; + } + else { + drupal_set_title(is_object($user) ? t("!username's Recent Links", array('!username' => $user->name)) : t('Recently Shortened Links')); + $access = 'all'; + } + drupal_add_css(drupal_get_path('module', 'shorturl') .'/shorturl.css', 'module', 'all'); + $output = ''; + + if (is_object($user) && ($user->uid != 0)) { + /*$sql = "SELECT lid, orig_url, title, description, keywords, count(aid) as clicks, created, uid FROM {shorturl_link} sl LEFT JOIN {shorturl_access} sa ON lid=url_id WHERE sl.uid='$user->uid' GROUP BY lid ORDER BY created DESC";*/ + $sql = "SELECT lid, orig_url, title, description, keywords, clicks, created, uid FROM {shorturl_link} WHERE uid='$user->uid' GROUP BY lid ORDER BY created DESC"; + $sql_count = "SELECT COUNT(lid) FROM {shorturl_link} WHERE uid='$user->uid'"; + $result = pager_query($sql, 25, 0, $sql_count); + } + else if (is_object($link)) { + /*$sql = "SELECT lid, orig_url, title, description, keywords, count(aid) as clicks, created, uid FROM {shorturl_link} sl LEFT JOIN {shorturl_access} sa ON lid=url_id WHERE sl.orig_url='$link->orig_url' GROUP BY lid ORDER BY clicks DESC";*/ + $sql = "SELECT lid, orig_url, title, description, keywords, clicks, created, uid FROM {shorturl_link} WHERE orig_url='$link->orig_url' GROUP BY lid ORDER BY clicks DESC"; + $sql_count = "SELECT COUNT(lid) FROM {shorturl_link} WHERE orig_url='$link->orig_url'"; + $result = pager_query($sql, 25, 0, $sql_count); + } + else { + /*$sql = 'SELECT lid, orig_url, title, description, keywords, count(aid) as clicks, count(orig_url) as links, created, uid FROM {shorturl_link} sl LEFT JOIN {shorturl_access} sa ON lid=url_id GROUP BY orig_url ORDER BY clicks DESC LIMIT 25';*/ + $sql = 'SELECT lid, orig_url, title, description, keywords, clicks, count(orig_url) as links, created, uid FROM {shorturl_link} GROUP BY orig_url ORDER BY clicks DESC LIMIT 25'; + $result = db_query($sql); + } + + $rows = array(); + while ($link = db_fetch_object($result)) { + $key = shorturl_encode_url($link->lid); + $target = (is_object($user) && ($user->uid != 0)) ? $link->orig_url : $key; + + if (!empty($link->title)) { + $title = html_entity_decode($link->title, ENT_QUOTES, 'UTF-8'); + $url = (strlen($link->orig_url) > 65) ? substr($link->orig_url, 0, 65).'...' : $link->orig_url; + $title = (strlen($title) > 50) ? substr($title, 0, 50).'...' : $title; + $details = ''. l($link_short, $link->orig_url).'
Short URL: '.l(url($key, array('absolute' => true)), url($key, array('absolute' => true))).'
'. $clicks .' '. t('click throughs') .'. '. t('Posted by !user on !date', array('!user' => l($user->name, 'shorturl/user/'. $user->uid), '!date' => format_date($link->created, 'medium'))) .'
'; + $output .= ''. check_plain($link->description).'
'; + $output .= '