New module 'Nodecomment'

This commit is contained in:
Manuel Cillero 2017-07-26 11:36:38 +02:00
parent 09b74574f1
commit 3c2bb788b4
26 changed files with 3513 additions and 0 deletions

View file

@ -0,0 +1,260 @@
<?php
/**
* @file
* Provide views data and handlers for nodecomment.module
*/
/**
* Implementation of hook_views_data()
*/
function nodecomment_views_data() {
// since comments are nodes we keep with the Node group
$data['node_comments']['table']['group'] = t('Node comment');
// ----------------------------------------------------------------
// node table -- basic table information.
$data['node_comments']['table']['join'] = array(
// node_comments links to node
'node' => array(
'left_field' => 'nid',
'field' => 'cid',
),
);
// ----------------------------------------------------------------
// node_comments table -- fields
$data['node_comments']['cid'] = array(
'title' => t('Nid'),
'help' => t('The node ID of the comment.'),
'argument' => array(
'handler' => 'views_handler_argument_node_nid',
'name field' => 'title',
'numeric' => TRUE,
'validate type' => 'nid',
),
'filter' => array(
'handler' => 'views_handler_filter_numeric',
),
'sort' => array(
'handler' => 'views_handler_sort',
),
);
$data['node_comments']['nid'] = array(
'title' => t('Original Parent Nid'),
'help' => t('The original node the comment is a reply to.'),
'relationship' => array(
'base' => 'node',
'field' => 'nid',
'handler' => 'views_handler_relationship',
'label' => t('Node'),
),
);
$data['node_comments']['nid_or_self'] = array(
'title' => t('Original Parent Nid or Self'),
'help' => t('The original node the comment is a reply to, or the original node. This allows searching across nodes that may or may not be comments.'),
'relationship' => array(
'base' => 'node',
'field' => 'nid',
'real field' => 'nid',
'handler' => 'nodecomment_handler_relationship_nid_or_self',
'label' => t('Node'),
),
);
$data['node_comments']['pid'] = array(
'title' => t('Parent Nid'),
'help' => t('The node of the parent comment. Could be another comment.'),
'field' => array(
'handler' => 'views_handler_field',
),
'relationship' => array(
'title' => t('Parent comment'),
'help' => t('The parent comment.'),
'base' => 'comments',
'field' => 'cid',
'handler' => 'views_handler_relationship',
'label' => t('Parent comment'),
),
);
$data['node_comments']['name'] = array(
'title' => t('Author'),
'help' => t('The name of the poster. This does not query the user table, it includes only data in the node_comment table.'),
'field' => array(
'handler' => 'views_handler_field_username_comment',
'click sortable' => TRUE,
),
'filter' => array(
'handler' => 'views_handler_filter_string',
),
'sort' => array(
'handler' => 'views_handler_sort',
),
'argument' => array(
'handler' => 'views_handler_argument_string',
),
);
$data['node_comments']['homepage'] = array(
'title' => t("Author's website"),
'help' => t("The website address of the comment's author. Can be a link. The homepage can also be linked with the Name field. Will be empty if posted by a registered user."),
'field' => array(
'handler' => 'views_handler_field_url',
'click sortable' => TRUE,
),
'filter' => array(
'handler' => 'views_handler_filter_string',
),
'sort' => array(
'handler' => 'views_handler_sort',
),
'argument' => array(
'handler' => 'views_handler_argument_string',
),
);
$data['node_comments']['thread'] = array(
'field' => array(
'title' => t('Depth'),
'help' => t('Display the depth of the comment if it is threaded.'),
'handler' => 'views_handler_field_comment_depth',
),
'sort' => array(
'title' => t('Thread'),
'help' => t('Sort by the threaded order. This will keep child comments together with their parents.'),
'handler' => 'views_handler_sort_comment_thread',
),
);
// link to reply to comment
$data['node_comments']['replyto_comment'] = array(
'field' => array(
'title' => t('Reply-to link'),
'help' => t('Provide a simple link to reply to the comment.'),
'handler' => 'views_handler_field_comment_link_reply',
),
);
return $data;
}
/**
* Implementation of hook_views_data()
*/
function nodecomment_views_data_alter(&$data) {
$data['node']['nodecomment_link'] = array(
'field' => array(
'title' => t('Node comment link'),
'help' => t('If the node is a comment, provide a link to the parent node at the right location. Otherwise provide a normal link to the node.'),
'handler' => 'nodecomment_handler_field_link',
),
);
$data['node']['nodecomment_author'] = array(
'field' => array(
'title' => t('Node comment author'),
'help' => t('Provide a link to the author of the node. This works properly with node comment where the author may not be an actual user.'),
'handler' => 'nodecomment_handler_field_author',
),
);
// Replace comment.module's "new comments" handler with ours.
$data['node']['new_comments']['field']['handler'] = 'nodecomment_handler_field_node_new_comments';
// Replace comment module "user created or commented" argument handler.
$data['node']['uid_touch']['argument']['handler'] = 'nodecomment_handler_argument_comment_user_uid';
}
/**
* Implementation of hook_views_plugins
*/
function nodecomment_views_plugins() {
return array(
'display' => array(
'nodecomment_comments' => array(
'title' => t('Nodecomments'),
'handler' => 'nodecomment_plugin_display_comments',
'path' => drupal_get_path('module', 'nodecomment') .'/views',
'theme' => 'views_view',
'theme path' => drupal_get_path('module', 'views') .'/theme',
'help' => t('Display a view in the "comments" location'),
'use ajax' => TRUE,
'use pager' => TRUE,
'accept attachments' => TRUE,
'admin' => t('Nodecomments'),
'help topic' => 'display-nodecomments',
),
),
'style' => array(
'nodecomment_threaded' => array(
'path' => drupal_get_path('module', 'nodecomment') .'/views',
'theme path' => drupal_get_path('module', 'nodecomment') .'/views',
'title' => t('Node comments threaded'),
'help' => t('Display the comment with a threaded comment view.'),
'handler' => 'nodecomment_plugin_style_threaded',
'theme' => 'nodecomment_threaded',
'uses row plugin' => TRUE,
'type' => 'normal',
),
),
'cache' => array(
'nodecomment_comments' => array(
'path' => drupal_get_path('module', 'nodecomment') .'/views',
'title' => t('Nodecomment thread'),
'help' => t('Cache a single thread of nodecomments based on a nid.'),
'handler' => 'nodecomment_plugin_cache_comments',
'uses options' => TRUE,
'help topic' => 'cache-nodecomment',
),
),
);
}
/**
* Register handlers
*/
function nodecomment_views_handlers() {
return array(
'info' => array(
'path' => drupal_get_path('module', 'nodecomment') .'/views',
),
'handlers' => array(
'views_handler_field_username_comment' => array(
'parent' => 'views_handler_field',
),
'views_handler_field_comment_depth' => array(
'parent' => 'views_handler_field'
),
'views_handler_field_username_comment' => array(
'parent' => 'views_handler_field'
),
'views_handler_field_comment_link' => array(
'parent' => 'views_handler_field'
),
'views_handler_field_comment_link_reply' => array(
'parent' => 'views_handler_field_comment_link'
),
'views_handler_sort_comment_thread' => array(
'parent' => 'views_handler_sort'
),
'nodecomment_handler_relationship_nid_or_self' => array(
'parent' => 'views_handler_relationship'
),
'nodecomment_handler_field_link' => array(
'parent' => 'views_handler_field_node_link'
),
'nodecomment_handler_field_author' => array(
'parent' => 'views_handler_field_user_name'
),
'nodecomment_handler_field_node_new_comments' => array(
'parent' => 'views_handler_field_node_new_comments'
),
'nodecomment_handler_argument_comment_user_uid' => array(
'parent' => 'views_handler_argument_comment_user_uid'
),
),
);
}

View file

@ -0,0 +1,144 @@
<?php
/**
* @file
* Contains default views on behalf of the nodecomment module.
*/
/**
* Implementation of hook_default_view_views().
*/
function nodecomment_views_default_views() {
$view = new view;
$view->name = 'nodecomments';
$view->description = 'Node comments flat';
$view->tag = '';
$view->view_php = '';
$view->base_table = 'node';
$view->is_cacheable = FALSE;
$view->api_version = 2;
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
$handler = $view->new_display('default', 'Defaults', 'default');
$handler->override_option('relationships', array(
'nid' => array(
'label' => 'Node',
'required' => 0,
'id' => 'nid',
'table' => 'node_comments',
'field' => 'nid',
'relationship' => 'none',
),
));
$handler->override_option('fields', array(
'title' => array(
'label' => 'Title',
'link_to_node' => 0,
'exclude' => 0,
'id' => 'title',
'table' => 'node',
'field' => 'title',
'relationship' => 'none',
),
'name' => array(
'label' => 'Author',
'link_to_user' => 1,
'exclude' => 0,
'id' => 'name',
'table' => 'node_comments',
'field' => 'name',
'relationship' => 'none',
),
'thread' => array(
'label' => 'Depth',
'exclude' => 0,
'id' => 'thread',
'table' => 'node_comments',
'field' => 'thread',
'relationship' => 'none',
),
));
$handler->override_option('arguments', array(
'nid' => array(
'default_action' => 'not found',
'style_plugin' => 'default_summary',
'style_options' => array(),
'wildcard' => 'all',
'wildcard_substitution' => 'All',
'title' => '',
'default_argument_type' => 'fixed',
'default_argument' => '',
'validate_type' => 'none',
'validate_fail' => 'not found',
'break_phrase' => 0,
'not' => 0,
'id' => 'nid',
'table' => 'node',
'field' => 'nid',
'relationship' => 'nid',
'default_options_div_prefix' => '',
'default_argument_user' => 0,
'default_argument_fixed' => '',
'default_argument_php' => '',
'validate_argument_node_type' => array(
'comment' => 0,
'page' => 0,
'story' => 0,
),
'validate_argument_node_access' => 0,
'validate_argument_nid_type' => 'nid',
'validate_argument_vocabulary' => array(),
'validate_argument_type' => 'tid',
'validate_argument_php' => '',
),
));
$handler->override_option('filters', array(
'status' => array(
'operator' => '=',
'value' => 1,
'group' => '0',
'exposed' => FALSE,
'expose' => array(
'operator' => FALSE,
'label' => '',
),
'id' => 'status',
'table' => 'node',
'field' => 'status',
'relationship' => 'none',
),
));
$handler->override_option('access', array(
'type' => 'none',
'role' => array(),
'perm' => '',
));
$handler->override_option('cache', array(
'type' => 'nodecomment_comments',
'argument' => 'nid',
));
$handler->override_option('use_pager', '1');
$handler->override_option('style_options', array(
'grouping' => '',
));
$handler->override_option('row_plugin', 'node');
$handler->override_option('row_options', array(
'teaser' => 0,
'links' => 1,
));
$handler = $view->new_display('nodecomment_comments', 'Nodecomments', 'nodecomment_comments_1');
$handler = $view->new_display('nodecomment_comments', 'Nodecomments Topic Review', 'nodecomment_comments_2');
$handler->override_option('sorts', array(
'nid' => array(
'order' => 'DESC',
'id' => 'nid',
'table' => 'node',
'field' => 'nid',
'override' => array(
'button' => 'Use default',
),
'relationship' => 'none',
),
));
$views[$view->name] = $view;
return $views;
}

View file

@ -0,0 +1,18 @@
<?php
/**
* Argument handler to accept a user id to check for nodes that
* user posted or commented on (using Nodecomment).
*
* Based on original handler from the Views module.
*/
class nodecomment_handler_argument_comment_user_uid extends views_handler_argument_comment_user_uid {
function query() {
$this->ensure_my_table();
// This copies comment handler query. It could be rewritten in various
// different ways, but all variants would suck in terms of performance.
$where = "$this->table_alias.uid = %d OR
((SELECT COUNT(*) FROM {comments} c WHERE c.uid = %d AND c.nid = $this->table_alias.nid) > 0) OR
((SELECT COUNT(*) FROM {node_comments} nc WHERE nc.uid = %d AND nc.nid = $this->table_alias.nid) > 0)";
$this->query->add_where(0, $where, $this->argument, $this->argument, $this->argument);
}
}

View file

@ -0,0 +1,48 @@
<?php
/**
* Field handler to allow linking to a user account or homepage
*/
class nodecomment_handler_field_author extends views_handler_field_user_name {
/**
* Override init function to add uid and homepage fields.
*/
function init(&$view, &$data) {
parent::init($view, $data);
$this->additional_fields['uid'] = array('table' => 'users', 'field' => 'uid');
$this->additional_fields['user_name'] = array('table' => 'users', 'field' => 'name');
$this->additional_fields['name'] = array('table' => 'node_comments', 'field' => 'name');
$this->additional_fields['homepage'] = array('table' => 'node_comments', 'field' => 'homepage');
}
function option_definition() {
$options = parent::option_definition();
return $options;
}
function options_form(&$form, &$form_state) {
parent::options_form($form, $form_state);
}
function query() {
$this->add_additional_fields();
$this->field_alias = $this->aliases['user_name'];
}
function render_link($data, $values) {
// The parent will handle anything that isn't a node comment just fine.
if (!empty($values->{$this->aliases['uid']}) || empty($values->{$this->aliases['name']})) {
return parent::render_link($data, $values);
}
if (!empty($this->options['link_to_user'])) {
$account->uid = 0;
$account->name = $values->{$this->aliases['name']};
$account->homepage = $values->{$this->aliases['homepage']};
return theme('username', $account);
}
else {
return $data;
}
}
}

View file

@ -0,0 +1,69 @@
<?php
/**
* Field handler to present a link to the node comment
*/
class nodecomment_handler_field_link extends views_handler_field_node_link {
function construct() {
parent::construct();
$this->additional_fields['parent_nid'] = array('table' => 'node_comments', 'field' => 'nid');
$this->additional_fields['thread'] = array('table' => 'node_comments', 'field' => 'thread');
}
function query() {
$this->ensure_my_table();
$this->add_additional_fields();
$node_comments = $this->query->ensure_table('node_comments', $this->relationship);
$def = array(
'table' => 'node',
'field' => 'nid',
'left_table' => $node_comments,
'left_field' => 'nid',
);
$join = new views_join();
$join->definition = $def;
$join->construct();
$join->adjusted = TRUE;
// Add more info to figure out what page the comment is on.
$this->parent_node = $this->query->add_table('node', $this->relationship, $join);
$this->aliases['parent_type'] = $this->query->add_field($this->parent_node, 'type');
}
function render($values) {
$text = !empty($this->options['text']) ? $this->options['text'] : t('view');
$nid = $values->{$this->aliases['nid']};
$this->options['alter']['make_link'] = TRUE;
if (!empty($values->{$this->aliases['parent_nid']})) {
// Fake up two nodes so we can get the target page:
$comment = new stdClass();
$comment->nid = $values->{$this->aliases['nid']};
$comment->thread = $values->{$this->aliases['thread']};
$parent = new stdClass();
$parent->nid = $values->{$this->aliases['parent_nid']};
$parent->type = $values->{$this->aliases['parent_type']};
if ($pageno = nodecomment_page_count($comment, $parent)) {
$this->options['alter']['query'] = 'page=' . $pageno;
}
$this->options['alter']['path'] = "node/$parent->nid";
$this->options['alter']['fragment'] = 'comment-' . $nid;
}
else {
$this->options['alter']['path'] = "node/$nid";
$this->options['alter']['fragment'] = '';
}
$this->options['alter']['alter_text'] = TRUE;
if (empty($this->options['alter']['text'])) {
$this->options['alter']['text'] = $text;
}
return $text;
}
}

View file

@ -0,0 +1,67 @@
<?php
/**
* Field handler to display the number of new comments
*/
class nodecomment_handler_field_node_new_comments extends views_handler_field_node_new_comments {
function pre_render(&$values) {
global $user;
if (!$user->uid || empty($values)) {
return;
}
$nids = array(); // for comments
$nc_nids = array(); // for node_comments
$ids = array();
foreach ($values as $id => $result) {
if (nodecomment_get_comment_type($result->{$this->aliases['type']})) {
$nc_nids[] = $result->{$this->aliases['nid']};
}
else {
$nids[] = $result->{$this->aliases['nid']};
}
$values[$id]->{$this->field_alias} = 0;
// Create a reference so we can find this record in the values again.
if (empty($ids[$result->{$this->aliases['nid']}])) {
$ids[$result->{$this->aliases['nid']}] = array();
}
$ids[$result->{$this->aliases['nid']}][] = $id;
}
if ($nids) {
$result = db_query("SELECT n.nid, COUNT(c.cid) as num_comments FROM {node} n INNER JOIN {comments} c ON n.nid = c.nid LEFT JOIN {history} h ON h.nid = n.nid AND h.uid = %d WHERE n.nid IN (" . implode(', ', $nids) . ") AND c.timestamp > GREATEST(COALESCE(h.timestamp, %d), %d) AND c.status = %d GROUP BY n.nid ", $user->uid, NODE_NEW_LIMIT, NODE_NEW_LIMIT, COMMENT_PUBLISHED);
while ($node = db_fetch_object($result)) {
foreach ($ids[$node->nid] as $id) {
$values[$id]->{$this->field_alias} = $node->num_comments;
}
}
}
if ($nc_nids) {
$result = db_query("SELECT n.nid, COUNT(c.cid) as num_comments FROM {node} n INNER JOIN {node_comments} c ON n.nid = c.nid INNER JOIN {node} nc ON c.cid = nc.nid LEFT JOIN {history} h ON h.nid = n.nid AND h.uid = %d WHERE n.nid IN (" . implode(', ', $nc_nids) . ") AND nc.created > GREATEST(COALESCE(h.timestamp, %d), %d) AND nc.status <> 0 GROUP BY n.nid ", $user->uid, NODE_NEW_LIMIT, NODE_NEW_LIMIT);
while ($node = db_fetch_object($result)) {
foreach ($ids[$node->nid] as $id) {
$values[$id]->{$this->field_alias} = $node->num_comments;
}
}
}
}
function render_link($data, $values) {
if (!empty($this->options['link_to_comment']) && $data !== NULL && $data !== '') {
$node = new stdClass();
$node->nid = $values->{$this->aliases['nid']};
$node->type = $values->{$this->aliases['type']};
$this->options['alter']['make_link'] = TRUE;
$this->options['alter']['path'] = 'node/' . $node->nid;
$this->options['alter']['query'] = nodecomment_new_page_count($values->node_comment_statistics_comment_count, $values->{$this->field_alias}, $node);
$this->options['alter']['fragment'] = 'new';
}
return $data;
}
}

View file

@ -0,0 +1,59 @@
<?php
/**
* Field handler to display the depth of a comment
*/
class nodecomment_handler_relationship_nid_or_self extends views_handler_relationship {
function option_definition() {
$options = parent::option_definition();
unset($options['required']);
return $options;
}
/**
* Default options form that provides the label widget that all fields
* should have.
*/
function options_form(&$form, &$form_state) {
parent::options_form($form, $form_state);
unset($form['required']);
}
/**
* Called to implement a relationship in a query.
*/
function query() {
// Figure out what base table this relationship brings to the party.
$table_data = views_fetch_data($this->definition['base']);
$base_field = empty($this->definition['base field']) ? $table_data['table']['base']['field'] : $this->definition['base field'];
$this->ensure_my_table();
// We need to figure out the alias of our base table based upon our relationship:
if ($this->relationship) {
$base_table = $this->relationship;
}
else {
$base_table = $this->query->base_table;
}
$def = $this->definition;
$def['table'] = $this->definition['base'];
$def['field'] = $base_field;
$def['left_table'] = '';
$def['left_field'] = "COALESCE($this->table.$this->real_field, $base_table.$this->real_field)";
$def['type'] = 'INNER';
$join = new views_join();
$join->definition = $def;
$join->construct();
$join->adjusted = TRUE;
// use a short alias for this:
$alias = $def['table'] . '_' . $this->table;
$this->alias = $this->query->add_relationship($alias, $join, $this->definition['base'], $this->relationship);
}
}

View file

@ -0,0 +1,94 @@
<?php
/**
* Cache comment threads in Views.
*/
class nodecomment_plugin_cache_comments extends views_plugin_cache {
function option_defaults(&$options) {
$options['argument'] = '';
}
function options_form(&$form, &$form_state) {
$options = array();
$arguments = $this->view->display_handler->get_handlers('argument');
foreach ($arguments as $id => $argument) {
$options[$id] = $argument->ui_name();
}
$form['argument'] = array(
'#type' => 'select',
'#title' => t('Argument to use'),
'#description' => t('The argument that contains the node ID. This content will be re-cached whenever that node is updated. If there is no node ID then the cache expiration will default to 1 hour.'),
'#options' => $options,
'#default_value' => $this->options['argument'],
);
}
function summary_title() {
return t('Node comment thread');
}
function cache_expire($type) {
// extract the node ID.
if (isset($this->view->argument[$this->options['argument']])) {
$nid = $this->view->argument[$this->options['argument']]->argument;;
$node = node_load($nid);
if ($node) {
return max($node->changed, $node->last_comment_timestamp);
}
}
// default to 1 hour.
return time() - 3600;
}
/**
* Post process cached output for new strings.
*
* The template preprocess will use placeholders for any 'new' output, so
* that the post process can replace it. This postprocess runs despite caching,
* so the freshness of comments can always be checked accurately for the
* logged in user. Without this, the "new" values are incorrect. This can
* be extended by modules that utlize other values that need to be
* freshened very easily with hook_views_post_render.
*/
function post_render(&$output) {
$tokens = array();
// First comment checking.
static $first_new = TRUE;
if (!isset($this->view->argument[$this->options['argument']])) {
return;
}
$nid = $this->view->argument[$this->options['argument']]->argument;;
$node = node_load($nid);
if (!$node) {
return;
}
// Set up tokens for each row.
foreach ($this->view->result as $id => $row) {
// we probably shouldn't use node_created directly here, but the display
// doesn't use any relationship so the chances of this alias failing is
// much slimmer than other weird things going wrong.
$new_output = $first = $new_class = '';
$new = node_mark($node->nid, $row->node_changed);
if ($new) {
$new_output = $new ? '<span class="new">' . t('new') . '</span>' : '';
$new_class = 'comment-new';
if ($first_new) {
$first = "<a id=\"new\"></a>";
$first_new = FALSE;
}
}
$tokens["<!--post:first-new-$row->nid-->"] = $first;
$tokens["<!--post:new-$row->nid-->"] = $new_output;
$tokens["<!--post:new-class-$row->nid-->"] = $new_class;
}
// Replace
$output = strtr($output, $tokens);
}
}

View file

@ -0,0 +1,96 @@
<?php
class nodecomment_plugin_display_comments extends views_plugin_display {
function options_summary(&$categories, &$options) {
parent::options_summary($categories, $options);
$options['items_per_page']['value'] = t('From node type settings');
}
function options_form(&$form, &$form_state) {
// specifically override these options
if ($form_state['section'] == 'items_per_page') {
$form['#title'] .= $this->use_pager() ? t('Items per page') : t('Items to display');
$form['items_per_page'] = array(
'#type' => 'value',
'#value' => 10,
);
$form['markup'] = array(
'#value' => t('The number of items to display will be taken from the node type settings.'),
);
$form['offset'] = array(
'#type' => 'textfield',
'#title' => t('Offset'),
'#description' => t('The number of items to skip. For example, if this field is 3, the first 3 items will be skipped and not displayed. Offset can not be used if items to display is 0; instead use a very large number there.'),
'#default_value' => intval($this->get_option('offset')),
);
return;
}
// otherwise do the default
parent::options_form($form, $form_state);
if ($form_state['section'] == 'items_per_page') {
$form['style_plugin']['#description'] .= ' ' . t('Important note: the style will be overridden to the nodecomment threaded style if threading is enabled for the node type.');
}
}
function query() {
// Get the node from the argument handler
if (empty($this->node)) {
return;
}
// Override our sorting options if necessary
$node_comments = $this->view->query->ensure_table('node_comments');
$mode = _comment_get_display_setting('mode', $this->node);
$order = _comment_get_display_setting('sort', $this->node);
if ($order == COMMENT_ORDER_NEWEST_FIRST) {
$sort = 'DESC';
if ($mode == COMMENT_MODE_FLAT_COLLAPSED || $mode == COMMENT_MODE_FLAT_EXPANDED) {
$table = 'node';
$field = 'nid';
}
else {
$table = $node_comments;
$field = 'thread';
}
}
else if ($order == COMMENT_ORDER_OLDEST_FIRST) {
$sort = 'ASC';
if ($mode == COMMENT_MODE_FLAT_COLLAPSED || $mode == COMMENT_MODE_FLAT_EXPANDED) {
$table = 'node';
$field = 'nid';
}
else {
// See comment above. Analysis reveals that this doesn't cost too
// much. It scales much much better than having the whole comment
// structure.
$table = NULL;
$field = "SUBSTRING($node_comments.thread, 1, (LENGTH($node_comments.thread) - 1))";
}
}
$this->view->query->add_orderby($table, $field, $sort, 'nodecomment_sort');
$this->view->query->add_field('node', 'changed');
parent::query();
}
/**
* Called by an implementation of hook_views_pre_build() to let us
* check the argument and make changes based upon global settings.
*/
function pre_build() {
if (!empty($this->view->args[0]) && $node = node_load($this->view->args[0])) {
$this->node = $node;
// Change items per page.
$this->view->set_items_per_page(_comment_get_display_setting('comments_per_page', $node));
$mode = _comment_get_display_setting('mode', $node);
if ($mode == COMMENT_MODE_THREADED_COLLAPSED || $mode == COMMENT_MODE_THREADED_EXPANDED) {
$this->set_option('style_plugin', 'nodecomment_threaded');
}
}
}
}

View file

@ -0,0 +1,36 @@
<?php
class nodecomment_plugin_style_threaded extends views_plugin_style {
function render() {
$divs = 0;
$last_depth = 0;
$output = '';
drupal_add_css(drupal_get_path('module', 'comment') .'/comment.css');
foreach ($this->view->result as $n) {
$node = node_load($n->nid);
$node->view = &$this->view;
$node->depth = count(explode('.', $node->thread)) - 1;
if ($node->depth > $last_depth) {
$divs++;
$output .= '<div class="indented">';
$last_depth++;
}
else {
while ($node->depth < $last_depth) {
$divs--;
$output .= '</div>';
$last_depth--;
}
}
$output .= node_view($node);
}
for ($i = 0; $i < $divs; $i++) {
$output .= '</div>';
}
return $output;
}
}

View file

@ -0,0 +1,17 @@
<?php
/**
* Field handler to display the depth of a comment
*/
class views_handler_field_comment_depth extends views_handler_field {
function construct() {
parent::construct();
$this->additional_fields['thread'] = 'thread';
}
/**
* Work out the depth of this comment
*/
function render($values) {
return count(explode('.', $values->{$this->aliases['thread']})) - 1;
}
}

View file

@ -0,0 +1,39 @@
<?php
/**
* Base field handler to present a link.
*/
class views_handler_field_comment_link extends views_handler_field {
function construct() {
parent::construct();
$this->additional_fields['cid'] = 'cid';
$this->additional_fields['nid'] = 'nid';
}
function option_definition() {
$options = parent::option_definition();
$options['text'] = array('default' => '', 'translatable' => TRUE);
return $options;
}
function options_form(&$form, &$form_state) {
parent::options_form($form, $form_state);
$form['text'] = array(
'#type' => 'textfield',
'#title' => t('Text to display'),
'#default_value' => $this->options['text'],
);
}
function query() {
$this->ensure_my_table();
$this->add_additional_fields();
}
function render($values) {
$text = !empty($this->options['text']) ? $this->options['text'] : t('view');
return l($text, "node/" . $values->{$this->aliases['nid']}, array('html' => TRUE, 'fragment' => "comment-" . $values->{$this->aliases['cid']}));
}
}
?>

View file

@ -0,0 +1,21 @@
<?php
/**
* Field handler to present a link for replying to a nodecomment.
*/
class views_handler_field_comment_link_reply extends views_handler_field_comment_link {
function construct() {
parent::construct();
$this->additional_fields['type'] = array('table' => 'node', 'field' => 'type');
}
function render($values) {
//check for permission to reply to comments
if (!user_access('post comments')) {
return;
}
$comment_type = str_replace('_', '-', $values->{$this->aliases['type']});
$text = !empty($this->options['text']) ? $this->options['text'] : t('reply');
return l($text, 'node/add/'. $comment_type .'/' . $values->{$this->aliases['nid']} . '/' . $values->{$this->aliases['cid']});
}
}

View file

@ -0,0 +1,50 @@
<?php
/**
* Field handler to allow linking to a user account or homepage
*/
class views_handler_field_username_comment extends views_handler_field {
/**
* Override init function to add uid and homepage fields.
*/
function init(&$view, &$data) {
parent::init($view, $data);
$this->additional_fields['uid'] = 'uid';
$this->additional_fields['homepage'] = 'homepage';
}
function option_definition() {
$options = parent::option_definition();
$options['link_to_user'] = array('default' => TRUE);
return $options;
}
function options_form(&$form, &$form_state) {
parent::options_form($form, $form_state);
$form['link_to_user'] = array(
'#title' => t("Link this field to its user or an author's homepage"),
'#type' => 'checkbox',
'#default_value' => $this->options['link_to_user'],
);
}
function render_link($data, $values) {
$account->uid = $values->{$this->aliases['uid']};
$account->name = $values->{$this->field_alias};
$account->homepage = $values->{$this->aliases['homepage']};
if (!empty($this->options['link_to_user'])) {
return theme('username', $account);
}
else {
return $data;
}
}
function render($values) {
return $this->render_link(check_plain($values->{$this->field_alias}), $values);
}
}
?>

View file

@ -0,0 +1,23 @@
<?php
/**
* Sort handler for ordering by thread
*/
class views_handler_sort_comment_thread extends views_handler_sort {
function query() {
$this->ensure_my_table();
//Read comment_render() in comment.module for an explanation of the
//thinking behind this sort.
if ($this->options['order'] == 'DESC') {
$this->query->add_orderby($this->table_alias, $this->real_field, $this->options['order']);
}
else {
$alias = $this->table_alias . '_' . $this->real_field . 'asc';
//@todo is this secure?
$this->query->add_orderby(NULL, "SUBSTRING({$this->table_alias}.{$this->real_field}, 1, (LENGTH({$this->table_alias}.{$this->real_field}) - 1))", $this->options['order'], $alias);
}
}
}
?>