Index: shorturl.admin.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/shorturl/Attic/shorturl.admin.inc,v retrieving revision 1.1.2.3 diff -c -r1.1.2.3 shorturl.admin.inc *** shorturl.admin.inc 16 Jun 2010 05:13:21 -0000 1.1.2.3 --- shorturl.admin.inc 4 Oct 2010 12:55:59 -0000 *************** *** 27,34 **** '#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.'), ); ! ! return system_settings_form($form); } --- 27,65 ---- '#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_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.4 diff -c -r1.1.2.4 shorturl.install *** shorturl.install 17 Aug 2010 23:16:09 -0000 1.1.2.4 --- shorturl.install 4 Oct 2010 12:55:59 -0000 *************** *** 13,24 **** 'description' => 'ShortURL Links Table.', 'fields' => array( 'lid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'length' => 22), ! 'orig_url' => array('type' => 'varchar', 'length' => 2048, 'not null' => TRUE, 'default' => ''), 'created' => array('type' => 'int', 'not null' => TRUE, 'default' => 0), 'remote_ip' => array('type' => 'varchar', 'length' => 20, 'not null' => FALSE, 'default' => ''), ), 'indexes' => array( 'shorturl_orig_url' => array('orig_url'), ), 'primary key' => array('lid'), ); --- 13,31 ---- 'description' => 'ShortURL Links Table.', 'fields' => array( 'lid' => array('type' => 'int', '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' => 2048, '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' => ''), + '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'), ); *************** *** 59,64 **** --- 66,104 ---- } /** + * 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; + } + + + /** + * 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; + } + + /** * Fixes: http://drupal.org/node/627668 * I can only test on MySQL so this fix is MySQL-specific. * If you want a fix for other DBs, please contribute code. Index: shorturl.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/shorturl/shorturl.module,v retrieving revision 1.1.2.7 diff -c -r1.1.2.7 shorturl.module *** shorturl.module 17 Aug 2010 23:16:09 -0000 1.1.2.7 --- shorturl.module 4 Oct 2010 12:56:00 -0000 *************** *** 28,33 **** --- 28,39 ---- $orig_url = url($orig_url, array('absolute'=>TRUE)); //Let drupal make relative URLs absolute } + $remote_ip = (empty($_SERVER['REMOTE_ADDR'])) ? '' : $_SERVER['REMOTE_ADDR']; + $referer = (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`, `referer`, `browser`, `access_time`) VALUES (%d, '%s', '%s', '%s', '%s', %d) ", $indx, $token, $remote_ip, $referer, $browser, (int)time()); + db_query("UPDATE {shorturl_link} SET clicks = clicks + 1 WHERE lid = %d", $indx); + drupal_goto($orig_url, $query = NULL, $fragment = NULL, $http_response_code = 301); exit(); } *************** *** 39,52 **** * hook_perm implementation */ function shorturl_perm() { ! return array('administer shorturl'); } /** * hook_menu() implementation */ function shorturl_menu() { ! $items['admin/settings/shorturl'] = array( 'title' => 'ShortURL', 'description' => 'Configure the settings for ShortURL.', --- 45,114 ---- * hook_perm implementation */ function shorturl_perm() { ! return array('view own statistics', 'view all statistics', 'view link details', 'export all statistics', 'export own statistics', 'administer shorturl'); } /** * hook_menu() implementation */ 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.', *************** *** 56,65 **** 'file' => 'shorturl.admin.inc', 'type' => MENU_NORMAL_ITEM, ); ! return $items; } /** * --- 118,358 ---- '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 .= '