diff -urNp user_titles-5.x-1.x-dev/user_titles/README.txt user_titles-6.x-1.x-dev/user_titles/README.txt --- user_titles-5.x-1.x-dev/user_titles/README.txt 2007-11-19 11:51:51.000000000 +1100 +++ user_titles-6.x-1.x-dev/user_titles/README.txt 2008-10-13 02:02:10.000000000 +1100 @@ -8,8 +8,7 @@ your template.php or in your node.tpl.ph is recommended that you place this in your template.php. In template.php, in the function _phptemplate_variables, place this -piece of code under case 'node': and a similar one under case 'comment' (see -below): +piece of code under case 'node': and case 'comment': if (module_exists('user_titles')) { $vars['user_title'] = user_titles_get_user_title($vars['node']->uid); @@ -24,12 +23,9 @@ file, you may create one: function _phptemplate_variables($hook, $vars = array()) { switch ($hook) { case 'node': - if (module_exists('user_titles')) { - $vars['user_title'] = user_titles_get_user_title($vars['node']->uid); - } case 'comment': if (module_exists('user_titles')) { - $vars['user_title'] = user_titles_get_user_title($vars['comment']->uid); + $vars['user_title'] = user_titles_get_user_title($vars['node']->uid); } } return $vars; @@ -49,4 +45,4 @@ these. NOTE: The settings UI for this currently requires javascript and doesn't degrade. -Patches welcome. +Patches welcome. \ No newline at end of file diff -urNp user_titles-5.x-1.x-dev/user_titles/user_titles.info user_titles-6.x-1.x-dev/user_titles/user_titles.info --- user_titles-5.x-1.x-dev/user_titles/user_titles.info 2007-11-19 23:08:58.000000000 +1100 +++ user_titles-6.x-1.x-dev/user_titles/user_titles.info 2008-10-20 03:04:00.000000000 +1100 @@ -1,9 +1,7 @@ -; $Id: user_titles.info,v 1.2 2007/07/20 18:18:45 merlinofchaos Exp $ +; $Id$ name = "User titles" description = "Allow users to have titles based upon how many posts they have." -; Information added by drupal.org packaging script on 2007-11-19 -version = "5.x-1.x-dev" -project = "user_titles" -datestamp = "1195474138" +version = "6.x-0.1" +core = "6.x" diff -urNp user_titles-5.x-1.x-dev/user_titles/user_titles.install user_titles-6.x-1.x-dev/user_titles/user_titles.install --- user_titles-5.x-1.x-dev/user_titles/user_titles.install 2007-07-27 10:19:56.000000000 +1000 +++ user_titles-6.x-1.x-dev/user_titles/user_titles.install 2008-10-20 04:48:11.000000000 +1100 @@ -1,37 +1,29 @@ array( + 'tid' => array('type' => 'int', 'not null' => TRUE, 'disp-width' => '10'), + 'title' => array('type' => 'varchar', 'length' => '255', 'not null' => TRUE), + 'value' => array('type' => 'int', 'not null' => TRUE, 'disp-width' => '10')), + 'primary key' => array('tid'), + ); + + $schema['user_titles_posts'] = array( + 'fields' => array( + 'uid' => array('type' => 'int', 'not null' => TRUE, 'disp-width' => '10'), + 'posts' => array('type' => 'int', 'not null' => FALSE, 'disp-width' => '11')), + 'primary key' => array('uid'), + ); + + return $schema; } -function user_titles_update_1() { - $ret = array(); - $ret[] = update_sql("RENAME TABLE {user_titles} TO {user_titles_posts}"); - $ret[] = update_sql(" - CREATE TABLE {user_titles} ( - tid int(10) NOT NULL PRIMARY KEY, - title varchar(255) NOT NULL, - value int(10) NOT NULL - ) /*!40100 DEFAULT CHARACTER SET utf8 */ - "); - - return $ret; +function user_titles_install() { + drupal_install_schema('user_titles'); } function user_titles_uninstall() { - db_query("DROP TABLE {user_titles}"); - db_query("DROP TABLE {user_titles_posts}"); + drupal_uninstall_schema('user_titles'); } diff -urNp user_titles-5.x-1.x-dev/user_titles/user_titles.module user_titles-6.x-1.x-dev/user_titles/user_titles.module --- user_titles-5.x-1.x-dev/user_titles/user_titles.module 2007-11-19 11:46:36.000000000 +1100 +++ user_titles-6.x-1.x-dev/user_titles/user_titles.module 2008-10-20 09:12:41.000000000 +1100 @@ -1,5 +1,5 @@ t('User titles'), - 'description' => t('Configure user titles and number of posts required'), - 'path' => 'admin/user/user-titles', - 'callback' => 'drupal_get_form', - 'callback arguments' => array('user_titles_settings_form'), - 'access' => user_access('administer user titles'), - 'type' => MENU_NORMAL_ITEM, - ); - return $items; - } +function user_titles_menu() { + $items['admin/user/user-titles'] = array( + 'title' => 'User titles', + 'description' => 'Configure user titles and number of posts required', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('user_titles_settings_form'), + 'access arguments' => array('administer user titles'), + ); + return $items; } /** @@ -116,6 +112,14 @@ function user_titles_settings_form() { return $form; } +function user_titles_theme() { + return array( + 'user_titles_settings_form' => array( + 'arguments' => array('form' => NULL), + ), + ); +} + /** * Theme the user title settings form. * @@ -137,7 +141,7 @@ function theme_user_titles_settings_form $output = '
'; - foreach (element_children($form['titles']) as $key) { + foreach (element_children($form['titles']) as $key) { // set a reference so that the drupal_render gets remembered. unset($elem); $elem = &$form['titles'][$key]; @@ -194,7 +198,7 @@ function user_titles_settings_form_valid */ function user_titles_settings_form_submit($form_id, $form_values) { $titles = array(); - foreach ($form_values['titles'] as $id => $title) { + foreach ($form_values['values']['titles'] as $id => $title) { if (empty($title['value']) && empty($title['title'])) { continue; } @@ -202,13 +206,14 @@ function user_titles_settings_form_submi } user_titles_set_titles($titles); - // If they change the allowed types, wipe the existing counts so each one will be fresh. + // If they change the allowed types, rebuild the existing counts so each one will be fresh. $types = user_titles_get_allowed_types(); - if ($types != $form_values['types']) { - db_query("TRUNCATE {user_titles_posts}"); + + if ($types != array_keys(array_filter($form_values['values']['types']))) { + user_titles_rebuild_counts(); + user_titles_set_allowed_types(array_keys(array_filter($form_values['values']['types']))); } - user_titles_set_allowed_types(array_keys(array_filter($form_values['types']))); drupal_set_message(t('The configuration has been updated.')); } @@ -216,12 +221,12 @@ function user_titles_settings_form_submi * Implementation of hook_nodeapi */ function user_titles_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) { - if ($op == 'insert' || $op == 'delete') { - $allowed_types = user_titles_get_allowed_types(); - if (in_array($node->type, $allowed_types)) { - $inc = ($op == 'insert') ? 1 : -1; - db_query("REPLACE INTO {user_titles_posts} (uid, posts) VALUES (%d, %d)", $node->uid, user_titles_get_posts($node->uid) + $inc); - } + switch ($op) { + case 'insert': + $allowed_types = user_titles_get_allowed_types(); + if (in_array($node->type, $allowed_types)) { + db_query("REPLACE INTO {user_titles_posts} (uid, posts) VALUES (%d, %d)", $node->uid, user_titles_get_posts($node->uid) + 1); + } } } @@ -229,11 +234,8 @@ function user_titles_nodeapi(&$node, $op * Implementation of hook_comment. */ function user_titles_comment($a1, $op) { - if ($op == 'insert' || $op == 'delete') { - $a1 = (object)$a1; // sometimes an array is passed in - $uid = $a1->uid; - $inc = ($op == 'insert') ? 1 : -1; - db_query("REPLACE INTO {user_titles_posts} (uid, posts) VALUES (%d, %d)", $uid, user_titles_get_posts($uid) + $inc); + if ($op == 'insert') { + db_query("REPLACE INTO {user_titles_posts} (uid, posts) VALUES (%d, %d)", $a1->uid, user_titles_get_posts($a1->uid) + 1); } } @@ -319,7 +321,7 @@ function user_titles_update_post_count($ $allowed_types = user_titles_get_allowed_types(); if ($allowed_types) { $types = implode("','", $allowed_types); - $count = db_result(db_query("SELECT COUNT(*) FROM {node} WHERE uid = %d AND type IN ('$types')", $uid)); + $count = db_result(db_query("SELECT COUNT(*) FROM {node} WHERE uid = %d AND type IN ('%s')", $uid, $types)); if (module_exists('comment')) { $count += db_result(db_query("SELECT COUNT(*) FROM {comments} WHERE uid = %d", $uid)); } @@ -352,14 +354,12 @@ function user_titles_set_allowed_types($ */ function user_titles_get_titles() { $query = db_query("SELECT title, value, tid FROM {user_titles} ORDER BY value DESC"); - if(db_num_rows($query)) { - while($r = db_fetch_array($query)) { - $result[] = $r; - } - } - else { - $result = array(); + + $result = array(); + while ($r = db_fetch_array($query)) { + $result[] = $r; } + return $result; } @@ -368,9 +368,9 @@ function user_titles_get_titles() { */ function user_titles_set_titles($titles) { db_query("TRUNCATE {user_titles}"); - foreach($titles as $title) { - if($title['tid'] == 'new') { - $title['tid'] = db_next_id("{user_titles}_tid"); + foreach ($titles as $title) { + if ($title['tid'] == 'new') { + $title['tid'] = db_last_insert_id("user_titles", "tid"); } db_query("INSERT INTO {user_titles} (title, value, tid) VALUES ('%s', %d, %d)", $title['title'], $title['value'], $title['tid']); } @@ -400,11 +400,11 @@ function user_titles_user($op, $edit, &$ } $form['user_titles']['current_title'] = array( - '#value' => '
' . t('Current user title:') . ' ' . filter_xss_admin($title) . '
', + '#value' => '
'. t('Current user title:') .' '. filter_xss_admin($title) .'
', ); if (isset($title_info['title']) && empty($title_info['tid'])) { - $default_title_info = user_titles_get_title(user_titles_get_posts($user->uid)); + $default_title_info = user_titles_get_title(user_titles_get_posts($uid)); if (!isset($default_title_info['title'])) { $default_title = t('No title set'); } @@ -412,7 +412,7 @@ function user_titles_user($op, $edit, &$ $default_title = $default_title_info['title']; } $form['user_titles']['default_title'] = array( - '#value' => '
' . t('Default user title:') . ' ' . filter_xss_admin($default_title) . '
', + '#value' => '
'. t('Default user title:') .' '. filter_xss_admin($default_title) .'
', ); } @@ -435,3 +435,32 @@ function user_titles_user($op, $edit, &$ } } } + +function user_titles_rebuild_counts() { + db_query('TRUNCATE TABLE {user_titles_posts}'); + + $types = user_titles_get_allowed_types(); + foreach ($types as $id => $type) { + $types[$id] = "'". $type ."'"; + } + $types_clause = implode(',', $types); + $user_node_count_query = "SELECT n.uid, COUNT(n.uid) AS user_node_count FROM {node} n WHERE n.type IN ($types_clause) GROUP BY n.uid ORDER BY n.uid"; + + // Create the actual structure that we will return. + $user_node_count_result = db_query($user_node_count_query); + + while ($user_node_count_row = db_fetch_array($user_node_count_result)) { + $user_node_count_insert_query = db_query("INSERT INTO {user_titles_posts}(uid,posts) VALUES(%d,%d)", + $user_node_count_row['uid'], $user_node_count_row['user_node_count']); + } + + // We may not need to do all this, depending. + /* + $user_result = db_query("SELECT uid FROM {users} ORDER BY uid"); + while ($uid_row = db_fetch_array($user_result)) { + $result[$uid_row['uid']] = 0; + } + */ + + drupal_set_message(t("Users' post counts have been rebuilt.")); +}