View source
<?php
define('VUD_COMMENT_DISPLAY_HIDDEN', 0);
define('VUD_COMMENT_DISPLAY_NORMAL', 1);
function vud_comment_menu() {
$items = array();
$items['admin/config/search/voteupdown/comment'] = array(
'title' => 'Comments',
'description' => 'Vote Up/Down Comment settings',
'page arguments' => array(
'vud_comment_admin_settings',
),
'access arguments' => array(
'administer vote up/down on comments',
),
'type' => MENU_LOCAL_TASK,
);
return $items;
}
function vud_comment_permission() {
return array(
'administer vote up/down on comments' => array(
'title' => t('Administer Vote Up/Down on Comments'),
),
'use vote up/down on comments' => array(
'title' => t('Vote on Comments'),
),
'view vote up/down count on comments' => array(
'title' => t('View Vote Count on comments'),
),
);
}
function vud_comment_admin_settings() {
$form['vud_comment_node_types'] = array(
'#type' => 'checkboxes',
'#title' => t('Node types'),
'#description' => t('Select the node types for which voting on comments will be activated.'),
'#default_value' => variable_get('vud_comment_node_types', array()),
'#options' => node_type_get_names(),
);
$form['vud_comment_widget'] = array(
'#type' => 'radios',
'#title' => t('Widget selection'),
'#description' => t('Select the voting widget theme that will be displayed.'),
'#default_value' => variable_get('vud_comment_widget', 'plain'),
'#options' => vud_widget_get_names(),
);
$form['vud_comment_widget_display'] = array(
'#type' => 'radios',
'#title' => t('Widget display'),
'#description' => t('Select how the voting widget will be displayed.'),
'#default_value' => variable_get('vud_comment_widget_display', VUD_COMMENT_DISPLAY_NORMAL),
'#options' => array(
VUD_COMMENT_DISPLAY_NORMAL => t('Append to the comment content.'),
VUD_COMMENT_DISPLAY_HIDDEN => t('Do not append. I will manually insert <code>$comment->vud_comment_widget</code> variable on my theme.'),
),
);
$form['vud_comment_votes'] = array(
'#type' => 'radios',
'#title' => t('Votes display'),
'#description' => t('Choose if the total number of votes will be displayed in the links part.'),
'#default_value' => variable_get('vud_comment_votes', 1),
'#options' => array(
0 => 'No',
1 => 'Yes',
),
);
$form['vud_comment_reset'] = array(
'#type' => 'radios',
'#title' => t('Votes reset'),
'#description' => t('Choose if users are allowed to reset their vote on a comment.'),
'#default_value' => variable_get('vud_comment_reset', 0),
'#options' => array(
0 => 'No',
1 => 'Yes',
),
);
ctools_include('dependent');
$form['vud_comment_dim_active'] = array(
'#type' => 'radios',
'#title' => t('Dim comments'),
'#description' => t('Choose if comments should be dimmed.'),
'#default_value' => variable_get('vud_comment_dim_active', 0),
'#options' => array(
0 => 'No',
1 => 'Yes',
),
);
$form['vud_comment_dim_threshold'] = array(
'#type' => 'textfield',
'#title' => t('Dim threshold'),
'#default_value' => variable_get('vud_comment_dim_threshold', 0),
'#description' => t('Comments with total votes less or equal to this values will be dimmed.'),
'#process' => array(
'ctools_dependent_process',
),
'#dependency' => array(
'radio:vud_comment_dim_active' => array(
1,
),
),
);
return system_settings_form($form);
}
function vud_comment_admin_settings_validate($form, &$form_state) {
if ($form_state['values']['vud_comment_dim_active'] == 1 && !is_numeric($form_state['values']['vud_comment_dim_threshold'])) {
form_set_error('vud_comment_dim_threshold', t('You must use a numeric value for the dim threshold.'));
}
}
function vud_comment_comment_view($comment, $view_mode, $langcode) {
if (!isset($comment->cid)) {
return;
}
$type = _vud_comment_get_node_type($comment->nid);
$comment_allow = in_array($type, variable_get('vud_comment_node_types', array()), TRUE);
$can_edit = user_access('use vote up/down on comments');
if ($comment_allow && ($can_edit || user_access('view vote up/down count on comments'))) {
$tag = variable_get('vud_tag', 'vote');
$widget_theme = variable_get('vud_comment_widget', 'plain');
$variables = array(
'entity_id' => $comment->cid,
'type' => 'comment',
'tag' => $tag,
'widget_theme' => $widget_theme,
'readonly' => !$can_edit,
);
$comment->vud_comment_widget = theme('vud_widget', $variables);
if (variable_get('vud_comment_widget_display', VUD_COMMENT_DISPLAY_NORMAL) == VUD_COMMENT_DISPLAY_NORMAL) {
$comment->content['vud_comment_widget'] = array(
'#markup' => $comment->vud_comment_widget,
);
}
}
}
function vud_comment_preprocess_comment(&$variables) {
if (variable_get('vud_comment_dim_active', 0) != '1') {
return;
}
$entity_id = $variables['comment']->cid;
$tag = variable_get('vud_tag', 'vote');
$criteria = array(
'entity_type' => 'comment',
'entity_id' => $entity_id,
'value_type' => 'points',
'tag' => $tag,
'function' => 'sum',
);
$vud_sum = (int) votingapi_select_single_result_value($criteria);
$vud_dim_threshold = (int) variable_get('vud_comment_dim_threshold', 0);
if ($vud_sum <= $vud_dim_threshold) {
$variables['status'] .= ' vud-comment-dimmed';
}
}
function _vud_comment_get_node_type($nid) {
return db_query("SELECT type from {node} where nid = :nid", array(
':nid' => $nid,
))
->fetchField();
}
function vud_comment_link($type, $object, $teaser = FALSE) {
$links = array();
switch ($type) {
case 'comment':
$comment =& $object;
$votes_display_mode = variable_get('vud_comment_votes', 1);
$widget_theme = variable_get('vud_comment_widget', 'plain');
$tag = variable_get('vud_tag', 'vote');
$node = node_load($comment->nid);
$votable_node_type_comment = in_array($node->type, variable_get('vud_comment_node_types', array()), TRUE);
if ($votable_node_type_comment && $votes_display_mode) {
$links['vud_comment_votes_count'] = array(
'title' => theme('vud_votes', $comment->cid, $type, $tag, $widget_theme),
'html' => TRUE,
);
}
if ($votable_node_type_comment && variable_get('vud_comment_reset', 0) && user_access('reset vote up/down votes')) {
$criteria = array(
'entity_type' => $type,
'entity_id' => $comment->cid,
'tag' => $tag,
);
$criteria += votingapi_current_user_identifier();
$user_vote = votingapi_select_single_vote_value($criteria);
if (!is_null($user_vote)) {
$reset_token = drupal_get_token("votereset/comment/{$comment->cid}/{$tag}/{$widget_theme}");
$links['vud_comment_votes_reset_link'] = array(
'title' => t('Reset your vote'),
'href' => "votereset/comment/{$comment->cid}/{$tag}/{$widget_theme}/{$reset_token}",
'attributes' => array(
'rel' => 'nofollow',
),
'html' => TRUE,
);
}
}
break;
}
return $links;
}
function vud_comment_template_suggestions($template_type, $plugin, $entity_id) {
$comment = comment_load($entity_id);
$node = node_load($comment->nid);
return array(
$template_type,
$template_type . '_comment',
$template_type . '_comment_' . $plugin['name'],
$template_type . '_comment_' . $plugin['name'] . '__' . $node->type,
);
}
function vud_comment_views_api() {
return array(
'api' => 2,
'path' => drupal_get_path('module', 'vud_comment') . '/views',
);
}