'Comment Easy Reply settings',
'description' => 'Configure Comment Easy Reply module.',
'page callback' => 'drupal_get_form',
'page arguments' => array('comment_easy_reply_settings_form'),
'access arguments' => array('administer site configuration'),
'file' => 'comment_easy_reply.admin.inc',
);
$items['admin/config/content/comment-easy-reply/settings'] = array(
'title' => 'Settings',
'description' => 'Configure Comment Easy Reply module.',
'page callback' => 'drupal_get_form',
'page arguments' => array('comment_easy_reply_settings_form'),
'access arguments' => array('administer site configuration'),
'type' => MENU_DEFAULT_LOCAL_TASK,
'file' => 'comment_easy_reply.admin.inc',
'weight' => 0,
);
$items['admin/config/content/comment-easy-reply/status'] = array(
'title' => 'Status',
'description' => 'Comment Easy Reply status report.',
'page callback' => 'comment_easy_reply_status_report_page',
'access arguments' => array('administer site configuration'),
'type' => MENU_LOCAL_TASK,
'file' => 'comment_easy_reply.admin.inc',
'weight' => 99,
);
$items['ajax/comment-easy-reply/quote/%/%'] = array(
'title' => '',
'description' => '',
'page callback' => '_comment_easy_reply_quote_get_quote_bbcode',
'page arguments' => array(3, 4),
'access callback' => '_comment_easy_reply_quote_ajax_access',
'access arguments' => array(3, 4),
'type' => MENU_CALLBACK,
);
$items['ajax/comment-easy-reply/reply/%/%'] = array(
'title' => '',
'description' => '',
'page callback' => '_comment_easy_reply_ajax_reply_get_data',
'page arguments' => array(3, 4),
'access callback' => '_comment_easy_reply_ajax_reply_access',
'access arguments' => array(3, 4),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implements hook_comment_view().
*/
function comment_easy_reply_comment_view($comment, $view_mode, $langcode) {
if (!_comment_easy_reply_comment_node_type_is_allowed($comment)) {
return;
}
$comment = _comment_easy_reply_comment_replace($comment);
if (!isset($comment->in_preview) || !$comment->in_preview) {
$comment_number = _comment_easy_reply_get_comment_number($comment);
if (isset($comment->content['links']['comment']['#links']['comment-reply'])) {
$comment->content['links']['comment']['#links']['comment-reply']['attributes'] = array('class' => array('comment-easy-reply-number-' . $comment_number));
$comment->content['links']['comment']['#links']['comment-reply']['fragment'] = 'edit-comment-body';
}
}
}
/**
* Implements hook_comment_view_alter().
*/
function comment_easy_reply_comment_view_alter(&$build) {
if (!_comment_easy_reply_comment_node_type_is_allowed($build['#comment'])) {
return;
}
$node_type = _comment_easy_reply_get_comment_node_type($build['#comment']);
if (_comment_easy_reply_get_settings_value('comment_easy_reply_fragment_activated', $node_type)) {
$fragment = _comment_easy_reply_get_fragment($build['#comment']);
if (!isset($build['#prefix'])) {
$build['#prefix'] = '';
}
$build['#prefix'] = '' . "\n" . $build['#prefix'];
}
}
/**
* Implements hook_comment_presave().
*/
function comment_easy_reply_comment_presave($comment) {
if (!_comment_easy_reply_comment_node_type_is_allowed($comment)) {
return;
}
if (_comment_easy_reply_is_comment_node_type_threaded($comment)) {
$comment_body = $comment->comment_body[$comment->language][0]['value'];
$parents = _comment_easy_reply_comment_find_references($comment_body, $comment->nid);
if (!empty($parents)) {
$parent_cid = array_shift($parents);
$comment->pid = $parent_cid;
}
}
else {
$comment->pid = 0;
}
}
/**
* Implements hook_preprocess_comment().
*/
function comment_easy_reply_preprocess_comment(&$variables) {
if (!_comment_easy_reply_comment_node_type_is_allowed($variables['comment'])) {
return;
}
if (isset($variables['comment']->in_preview) && $variables['comment']->in_preview) {
$variables['permalink'] = '';
}
else {
$comment_num = _comment_easy_reply_get_comment_number($variables['comment']);
$variables['comment_easy_reply_number'] = $comment_num;
$variables['permalink'] = _comment_easy_reply_get_tooltip('comment_number_link', $variables);
$node_type = _comment_easy_reply_get_comment_node_type($variables['comment']);
if (_comment_easy_reply_get_settings_value('comment_easy_reply_fragment_activated', $node_type)) {
$fragment = _comment_easy_reply_get_fragment($variables['comment']);
$variables['title'] = str_replace('#comment-' . $variables['comment']->cid, '#' . $fragment, $variables['title']);
}
}
}
/**
* Implements hook_theme().
*/
function comment_easy_reply_theme($existing, $type, $theme, $path) {
return array(
'comment_easy_reply_comment_number_link' => array(
'variables' => array('comment' => NULL),
'template' => 'templates/comment-easy-reply-comment-number-link',
),
'comment_easy_reply_comment_referrer_link' => array(
'variables' => array(
'comment' => NULL,
'comment_easy_reply_comment_num' => NULL,
'class' => NULL,
),
'template' => 'templates/comment-easy-reply-comment-referrer-link',
),
'comment_easy_reply_comment_number_tips' => array(
'variables' => array('comment' => NULL),
'template' => 'templates/comment-easy-reply-comment-number-tips',
),
'comment_easy_reply_comment_referrer_tips' => array(
'variables' => array(
'comment' => NULL,
'comment_easy_reply_comment_num' => NULL,
'class' => NULL,
),
'template' => 'templates/comment-easy-reply-comment-referrer-tips',
),
'comment_easy_reply_status_report_page' => array(
'variables' => array('comment_easy_reply_node_types' => NULL),
'file' => 'comment_easy_reply.theme.inc',
),
'comment_easy_reply_node_type_settings_report' => array(
'variables' => array('node_type' => NULL),
'file' => 'comment_easy_reply.theme.inc',
),
'comment_easy_reply_quote' => array(
'variables' => array(
'quote_content' => NULL,
'quote_author' => NULL,
'nest' => 0,
'attributes' => NULL,
),
'file' => 'comment_easy_reply.theme.inc',
),
);
}
/**
* Implements hook_theme_registry_alter().
*/
function comment_easy_reply_theme_registry_alter(&$theme_registry) {
if (isset($theme_registry['comment_block'])) {
$theme_registry['comment_block']['function'] = 'theme_comment_easy_reply_comment_block';
$theme_registry['comment_block']['file'] = 'comment_easy_reply.theme.inc';
}
}
/**
* Implements hook_views_api().
*/
function comment_easy_reply_views_api() {
return array(
'api' => 3,
'path' => drupal_get_path('module', 'comment_easy_reply') . '/views',
);
}
/**
* Implements hook_filter_info_alter().
*/
function comment_easy_reply_filter_info_alter(&$info) {
if (isset($info['quote'])) {
$info['quote']['process callback'] = '_comment_easy_reply_quote_filter_process';
}
}
/**
* Implements hook_tokens().
*/
function comment_easy_reply_tokens($type, $tokens, array $data = array(), array $options = array()) {
$replacements = array();
if ($type == 'comment_easy_reply' && !empty($data['comment'])) {
$comment = $data['comment'];
foreach ($tokens as $name => $original) {
switch ($name) {
case 'comment_number':
$replacements[$original] = _comment_easy_reply_get_comment_number($comment);
break;
}
}
}
return $replacements;
}
/**
* Implements hook_token_info().
*/
function comment_easy_reply_token_info() {
$type = array(
'name' => t('Comment Easy Reply'),
'description' => t('Tokens related to Comment Easy Reply module.'),
'needs-data' => 'comment',
);
$tokens['comment_number'] = array(
'name' => t("Comment number"),
'description' => t("The comment number."),
);
return array(
'types' => array('comment_easy_reply' => $type),
'tokens' => array('comment_easy_reply' => $tokens),
);
}
/**
* Implements hook_hook_info().
*/
function comment_easy_reply_hook_info() {
$hooks['comment_easy_reply_tags'] = array(
'group' => 'comment_easy_reply',
);
$hooks['comment_easy_reply_tooltip'] = array(
'group' => 'comment_easy_reply',
);
$hooks['comment_easy_reply_tooltip_number_link'] = array(
'group' => 'comment_easy_reply',
);
$hooks['comment_easy_reply_tooltip_number_tips'] = array(
'group' => 'comment_easy_reply',
);
$hooks['comment_easy_reply_tooltip_referrer_link'] = array(
'group' => 'comment_easy_reply',
);
$hooks['comment_easy_reply_tooltip_referrer_tips'] = array(
'group' => 'comment_easy_reply',
);
$hooks['comment_easy_reply_tooltip_quote'] = array(
'group' => 'comment_easy_reply',
);
$hooks['comment_easy_reply_settings'] = array(
'group' => 'comment_easy_reply',
);
$hooks['comment_easy_reply_text_replace_alter'] = array(
'group' => 'comment_easy_reply',
);
$hooks['comment_easy_reply_text_matches_alter'] = array(
'group' => 'comment_easy_reply',
);
return $hooks;
}
/**
* Implements hook_entity_property_info().
*/
function comment_easy_reply_entity_property_info() {
$info = array();
$properties = &$info['comment']['properties'];
$properties['comment_easy_reply_parents'] = array(
'label' => t("Comment Easy Reply parents"),
'type' => 'list',
'description' => t("The parents of a comment using Comment Easy Reply tag system."),
'getter callback' => '_comment_easy_reply_entity_property_get',
);
return $info;
}
/**
* Implements hook_form_alter().
*/
function comment_easy_reply_form_alter(&$form, &$form_state, $form_id) {
$comment_form_id = 'comment-form';
if (substr($form['#id'], 0, strlen($comment_form_id)) == $comment_form_id && $form['#node'] && _comment_easy_reply_is_node_type_active($form['#node']->type)) {
$nid = $form['#node']->nid;
$cid = NULL;
if (isset($form['pid']['#value'])) {
$cid = $form['pid']['#value'];
}
if ($cid && $nid) {
$comment = comment_load($cid);
if ($comment) {
$number = _comment_easy_reply_get_comment_number($comment);
$language = $form['comment_body']['#language'];
$data = _comment_easy_reply_comment_reply_get_data($nid, $cid);
if (array_key_exists('#default_value', $form['comment_body'][$language][0])) {
$form['comment_body'][$language][0]['#default_value'] = $data['body'] . ' ' . $form['comment_body'][$language][0]['#default_value'];
}
else {
$form['comment_body'][$language][0]['value']['#default_value'] = $data['body'] . ' ' . $form['comment_body'][$language][0]['value']['#default_value'];
}
if (isset($form['subject'])) {
$form['subject']['#default_value'] = $data['subject'] . ' ' . $form['subject']['#default_value'];
}
if (strpos($form_id, 'comment_form_node_') === 0 && module_exists('ideal_comments')) {
$settings = array();
$settings['comment_easy_reply_js_override'] = array(
'scrollform_enabled' => FALSE,
'ajax_reply_enabled' => FALSE,
'ideal_comments_enabled' => TRUE);
comment_easy_reply_add_js($settings);
}
}
}
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function comment_easy_reply_form_node_type_form_alter(&$form, &$form_state, $form_id) {
if (!isset($form['#node_type'])) {
return;
}
$node_type = $form['#node_type']->type;
$active = _comment_easy_reply_is_node_type_active($node_type);
$form['comment']['comment_easy_reply'] = array(
'#type' => 'fieldset',
'#title' => t('Comment Easy Reply settings'),
'#collapsible' => TRUE,
'#collapsed' => !$active,
);
$form['comment']['comment_easy_reply']['comment_easy_reply_active_' . $node_type] = array(
'#type' => 'checkbox',
'#title' => t('Activate Comment Easy Reply mode for this node type.'),
'#default_value' => $active,
);
$form['comment']['comment_easy_reply']['override'] = array(
'#type' => 'fieldset',
'#title' => t('Override general settings'),
'#collapsed' => !$active,
'#collapsible' => TRUE,
);
$override = _comment_easy_reply_is_node_type_override_active($node_type);
$form['comment']['comment_easy_reply']['override']['comment_easy_reply_override_' . $node_type] = array(
'#type' => 'checkbox',
'#title' => t('Override !settings for this node type.',
array(
'!settings' => l(t('Comment Easy Reply general settings'), 'admin/config/content/comment-easy-reply'),
)),
'#default_value' => $override,
);
$form['comment']['comment_easy_reply']['override']['active'] = array(
'#type' => 'fieldset',
'#title' => '',
'#collapsed' => !$override,
'#collapsible' => FALSE,
);
if (!$override) {
$form['comment']['comment_easy_reply']['override']['active']['#attributes'] = array('class' => array('collapsed'));
}
module_load_include('inc', 'comment_easy_reply', 'comment_easy_reply.admin');
$form['comment']['comment_easy_reply']['override']['active'] += _comment_easy_reply_settings_form($form_state, $node_type);
$form['#submit'][] = 'node_type_form_comments_easy_reply_submit';
$form['#validate'][] = 'node_type_form_comments_easy_reply_validate';
$js_path = drupal_get_path('module', 'comment_easy_reply') . '/js';
if (!isset($form['comment']['#attached']['js'])) {
$form['comment']['#attached']['js'] = array();
}
$form['comment']['#attached']['js'][] = $js_path . '/comment_easy_reply-admin.js';
$settings = array();
$settings['comment_easy_reply'] = array('admin' => array('node_type' => $node_type));
drupal_add_js($settings, 'setting');
}
/**
* Implements hook_preprocess_comment_easy_reply_comment_referrer_link().
*
* Provide a template variable containing a link to a comment referenced in
* current comment body.
* The link will be shown as #COMMENT_NUMBER or @COMMENT_AUTHOR, dependes of
* settings configuration.
* The link will also show the content of referenced comment.
*
* @param array $variables
* An associative array with the following structure:
* 'comment' - The comment.
* 'class' - An array containing the link's CSS classes (optional
* Use it to replace standard link classes (optional).
* 'more_class' - An array containing additional link's CSS classes.
* Use it to extend standard link classes (optional).
*/
function comment_easy_reply_preprocess_comment_easy_reply_comment_referrer_link(&$variables) {
$comment = $variables['comment'];
comment_easy_reply_add_js($variables);
comment_easy_reply_add_css($variables);
if (isset($variables['comment_easy_reply_comment_num'])) {
$comment_num = $variables['comment_easy_reply_comment_num'];
}
else {
$comment_num = _comment_easy_reply_get_comment_number($comment);
}
$comment->comment_easy_reply_comment_number = $comment_num;
$variables['comment'] = $comment;
$variables['classes_array'] = _comment_easy_reply_get_referrer_link_classes($variables);
$text = _comment_easy_reply_get_tag($comment);
$page = _comment_easy_reply_get_comment_page($comment);
$query = drupal_get_query_parameters();
if (!is_null($page)) {
if ($page > 0 || ($page == 0 && isset($_GET['page']) && $page == $_GET['page'])) {
$query['page'] = $page;
}
elseif ($page == 0 && isset($_GET['page']) && $page != $_GET['page'] && isset($query['page'])) {
unset($query['page']);
}
}
$fragment = 'comment-' . $comment->cid;
$node_type = _comment_easy_reply_get_comment_node_type($comment);
if (_comment_easy_reply_get_settings_value('comment_easy_reply_fragment_activated', $node_type)) {
$fragment = _comment_easy_reply_get_fragment($comment);
}
$options = array(
'fragment' => $fragment,
'external' => TRUE,
'attributes' => array(
'class' => $variables['classes_array'],
),
'query' => $query,
'html' => TRUE,
);
if (isset($variables['comment_easy_reply']) && is_array($variables['comment_easy_reply']['link_attributes'])) {
// Let hooks add attributes to link.
$options['attributes'] = drupal_array_merge_deep($options['attributes'], $variables['comment_easy_reply']['link_attributes']);
}
if ($text) {
$variables['link'] = l($text, '', $options);
}
}
/**
* Implements hook_preprocess_comment_easy_reply_comment_number_link().
*
* Provide a template variable to show a number beside a comment.
* The number will be show as link, to let user reply to comment by clicking
* on it.
*
* @param array $variables
* An associative array with the following structure:
* 'comment' - The comment.
* 'comments_per_page' - The number of comments per page (optional).
* 'custom_source' - An array of comments to use to find the comment's
* page number (optional).
*/
function comment_easy_reply_preprocess_comment_easy_reply_comment_number_link(&$variables) {
$comment = $variables['comment'];
comment_easy_reply_add_js($variables);
comment_easy_reply_add_css($variables);
$comments_per_page = NULL;
if (isset($variables['comments_per_page'])) {
$comments_per_page = $variables['comments_per_page'];
}
$custom_source = array();
$use_static = TRUE;
if (isset($variables['custom_source'])) {
$custom_source = $variables['custom_source'];
$use_static = FALSE;
}
$comment_num = _comment_easy_reply_get_comment_number($comment, $use_static);
$page = _comment_easy_reply_get_comment_page($comment, $comments_per_page, $custom_source);
$query = drupal_get_query_parameters();
if (!is_null($page)) {
if ($page > 0 || ($page == 0 && isset($_GET['page']) && $page == $_GET['page'])) {
$query['page'] = $page;
}
elseif ($page == 0 && isset($_GET['page']) && $page != $_GET['page'] && isset($query['page'])) {
unset($query['page']);
}
}
$fragment = 'comment-' . $comment->cid;
$node_type = _comment_easy_reply_get_comment_node_type($comment);
if (_comment_easy_reply_get_settings_value('comment_easy_reply_fragment_activated', $node_type)) {
$fragment = _comment_easy_reply_get_settings_value('comment_easy_reply_fragment_text', $node_type);
$fragment = token_replace($fragment, array('comment' => $variables['comment']));
}
$variables['comment_num'] = $comment_num;
$options = array(
'fragment' => $fragment,
'external' => TRUE,
'attributes' => array(
'class' => _comment_easy_reply_get_number_link_classes($variables),
),
'query' => $query,
'html' => TRUE,
);
$variables['tips'] = '';
if (isset($variables['comment_easy_reply']['link_attributes']) && is_array($variables['comment_easy_reply']['link_attributes'])) {
// Let hooks add attributes to link.
$options['attributes'] = drupal_array_merge_deep($options['attributes'], $variables['comment_easy_reply']['link_attributes']);
}
if (_comment_easy_reply_get_settings_value('comment_easy_reply_replytip_activated', $node_type) && user_access('post comments')) {
_comment_easy_reply_get_tooltip('comment_number_tips', $variables);
}
if (!isset($variables['link'])) {
$variables['link'] = l('#' . $comment_num, '', $options);
}
}
/**
* Implements hook_preprocess_comment_easy_reply_comment_number_tips().
*
* Provide the text to be show next to comment_number_link.
*
* @param array $variables
* An associative array with the following structure:
* 'comment' - The comment.
*/
function comment_easy_reply_preprocess_comment_easy_reply_comment_number_tips(&$variables) {
$node_type = _comment_easy_reply_get_comment_node_type($variables['comment']);
$text = _comment_easy_reply_get_settings_value('comment_easy_reply_replytip_text', $node_type);
$text = token_replace($text, $variables);
$variables['classes_array'] = _comment_easy_reply_get_number_tips_classes($variables);
$variables['tips'] = l($text,
'comment/reply/' . $variables['comment']->nid . '/' . $variables['comment']->cid,
array('fragment' => 'edit-comment-body')
);
}
/**
* Implements hook_preprocess_comment_easy_reply_comment_referrer_tips().
*
* Provide the referred comment text to be show next to comment_referrer_link.
*
* @param array $variables
* An associative array with the following structure:
* 'comment' - The comment.
* 'comment_easy_reply_comment_num' - The comment number (optional).
* 'override_text' - The text to be written inside the tooltip.
* This will override the standard module tooltip
* content (optional).
* 'class' - An array containing the link's CSS classes.
* Use it to replace standard link classes (optional).
* 'more_class' - An array containing additional link's CSS classes.
* Use it to extend standard link classes (optional).
*/
function comment_easy_reply_preprocess_comment_easy_reply_comment_referrer_tips(&$variables) {
if (!isset($variables['comment_easy_reply_comment_num']) || empty($variables['comment_easy_reply_comment_num'])) {
$variables['comment_easy_reply_comment_num'] = _comment_easy_reply_get_comment_number($variables['comment']);
}
$classes = _comment_easy_reply_get_referrer_tips_classes($variables);
$added_class = FALSE;
$lenght = strlen('comment-easy-reply-added-');
foreach ($classes as $single_class) {
if (substr($single_class, 0, $lenght) == 'comment-easy-reply-added-') {
$added_class = TRUE;
break;
}
}
if (!$added_class) {
$classes[] = 'comment-easy-reply-added-noclass';
}
$variables['classes_array'] = $classes;
$variables['tips'] = _comment_easy_reply_get_referrer_tips($variables);
}
/**
* Validate callback for the node type settings form.
*/
function node_type_form_comments_easy_reply_validate($form, &$form_state) {
$values = $form_state['values'];
$node_type = $values['type'];
$active = $values['comment_easy_reply_active_' . $node_type];
if ($active) {
$override = $values['comment_easy_reply_override_' . $node_type];
if ($override) {
$fragment_active = $values['comment_easy_reply_fragment_activated_' . $node_type];
if ($fragment_active) {
$text = $values['comment_easy_reply_fragment_text_' . $node_type];
if (empty($text)) {
form_set_error('comment_easy_reply_fragment_text_' . $node_type, t('Fragment text cannot be empty if activated.'));
}
else {
$error = FALSE;
$messages = _comment_easy_reply_validate_fragment_text($text, $error);
if ($error) {
foreach ($messages as $msg) {
form_set_error('comment_easy_reply_fragment_text_' . $node_type, $msg);
}
}
}
}
}
}
}
/**
* Submit callback for the node type settings form.
*/
function node_type_form_comments_easy_reply_submit($form, &$form_state) {
$values = $form_state['values'];
$node_type = $values['type'];
$active_types = _comment_easy_reply_get_active_node_types();
$active = $values['comment_easy_reply_active_' . $node_type];
if (empty($active_types)) {
if ($active) {
$active_types[] = $node_type;
_comment_easy_reply_set_active_node_types($active_types);
}
return;
}
$last_value = in_array($node_type, $active_types);
if (!$active && $last_value) {
foreach ($active_types as $pos => $type) {
if ($type == $node_type) {
unset($active_types[$pos]);
}
}
}
elseif ($active) {
if (!$last_value) {
$active_types[] = $node_type;
}
$override = $values['comment_easy_reply_override_' . $node_type];
if (!$override) {
variable_set('comment_easy_reply_override_' . $node_type, FALSE);
}
else {
variable_set('comment_easy_reply_override_' . $node_type, TRUE);
variable_set('comment_easy_reply_reply_from_numlink_' . $node_type, $values['comment_easy_reply_reply_from_numlink_' . $node_type]);
variable_set('comment_easy_reply_link_mode_' . $node_type, $values['comment_easy_reply_link_mode_' . $node_type]);
variable_set('comment_easy_reply_replytip_activated_' . $node_type, $values['comment_easy_reply_replytip_activated_' . $node_type]);
variable_set('comment_easy_reply_replytip_text_' . $node_type, $values['comment_easy_reply_replytip_text_' . $node_type]);
variable_set('comment_easy_reply_referrertip_text_' . $node_type, $values['comment_easy_reply_referrertip_text_' . $node_type]);
variable_set('comment_easy_reply_fragment_activated_' . $node_type, $values['comment_easy_reply_fragment_activated_' . $node_type]);
variable_set('comment_easy_reply_fragment_text_' . $node_type, $values['comment_easy_reply_fragment_text_' . $node_type]);
variable_set('comment_easy_reply_filter_' . $node_type, $values['comment_easy_reply_filter_' . $node_type]);
if (isset($values['comment_easy_reply_quote_tooltip_' . $node_type])) {
variable_set('comment_easy_reply_quote_tooltip_' . $node_type, $values['comment_easy_reply_quote_tooltip_' . $node_type]);
}
variable_set('comment_easy_reply_comment_title_' . $node_type, $values['comment_easy_reply_comment_title_' . $node_type]);
variable_set('comment_easy_reply_tooltip_name_' . $node_type, $values['comment_easy_reply_tooltip_name_' . $node_type]);
}
}
_comment_easy_reply_set_active_node_types($active_types);
}
/**
* Generates output for tooltip and links.
*
* @param string $type
* The type of requested output.
* Allowed types:
* - comment_number_link: the comment permalink tooltip link.
* - comment_number_tips: the comment permalink tooltip.
* - comment_referrer_link: the comment referrer tooltip link.
* - comment_number_tips: the comment referrer tooltip.
* - comment_easy_reply_quote: the comment referrer tooltip containing a
* blockquote.
* @param array &$variables
* An array of options to be passed to theme functions.
*
* @return string
* The output of the tooltip or the link.
*/
function _comment_easy_reply_get_tooltip($type, &$variables) {
switch ($type) {
case 'comment_number_link':
$tooltip_name = _comment_easy_reply_get_tooltip_name($variables['comment']);
$variables['number_link'] = _comment_easy_reply_tooltip_module_callback($tooltip_name, 'number_link', $variables);
return $variables['number_link'];
case 'comment_number_tips':
$node_type = _comment_easy_reply_get_comment_node_type($variables['comment']);
if (_comment_easy_reply_get_settings_value('comment_easy_reply_replytip_activated', $node_type)) {
$tooltip_name = _comment_easy_reply_get_tooltip_name($variables['comment']);
$variables['tips'] = _comment_easy_reply_tooltip_module_callback($tooltip_name, 'number_tips', $variables);
return $variables['tips'];
}
case 'comment_referrer_link':
$tooltip_name = _comment_easy_reply_get_tooltip_name($variables['comment']);
$variables['referrer_link'] = _comment_easy_reply_tooltip_module_callback($tooltip_name, 'referrer_link', $variables);
return $variables['referrer_link'];
case 'comment_referrer_tips':
$tooltip_name = _comment_easy_reply_get_tooltip_name($variables['comment']);
$variables['referrer_tips'] = _comment_easy_reply_tooltip_module_callback($tooltip_name, 'referrer_tips', $variables);
return $variables['referrer_tips'];
case 'comment_easy_reply_quote':
$tooltip_name = '';
if (isset($variables['comment']) && $variables['comment']) {
$tooltip_name = _comment_easy_reply_get_tooltip_name($variables['comment']);
}
return _comment_easy_reply_tooltip_module_callback($tooltip_name, 'quote', $variables);
}
}
/**
* Returns the currently selected tooltip type's name.
*
* @param object $comment
* The comment.
*
* @return string
* The name of the currently select tooltip type name.
*/
function _comment_easy_reply_get_tooltip_name($comment) {
$node_type = _comment_easy_reply_get_comment_node_type($comment);
return _comment_easy_reply_get_settings_value('comment_easy_reply_tooltip_name', $node_type);
}
/**
* Callback for tooltip generation.
*
* Select the right function to be called for a tooltip or a link.
*
* @param string $tooltip_name
* The name of tooltip module to be used for output generation.
* @param string $hook_type
* The type of tooltip or link.
* @param array &$variables
* The array of variables to be passed to the creation function.
*
* @return string
* The html output.
*/
function _comment_easy_reply_tooltip_module_callback($tooltip_name, $hook_type, &$variables) {
if (empty($tooltip_name)) {
watchdog('Comment Easy Reply', 'Tooltip "%name" seems to not exist anymore (type=@type). Please configure your tooltip configuration on @link.',
array(
'%name' => $tooltip_name,
'@link' => l(t('Comment Easy Reply settings page'), 'admin/config/content/comment-easy-reply'),
'@type' => $hook_type,
), WATCHDOG_WARNING);
return '';
}
comment_easy_reply_add_js($variables);
$tooltips = _comment_easy_reply_get_tooltips();
$hook = 'comment_easy_reply_tooltip_' . $tooltip_name . '_' . $hook_type;
if (isset($tooltips[$tooltip_name]) && isset($tooltips[$tooltip_name]['module'])) {
if (module_hook($tooltips[$tooltip_name]['module'], $hook)) {
return call_user_func_array($tooltips[$tooltip_name]['module'] . '_' . $hook, array(&$variables));
}
$hook = 'comment_easy_reply_tooltip_' . $hook_type;
if (module_hook($tooltips[$tooltip_name]['module'], $hook)) {
return call_user_func_array($tooltips[$tooltip_name]['module'] . '_' . $hook, array(&$variables));
}
}
return call_user_func_array('comment_easy_reply_' . $hook, array(&$variables));
}
/**
* Defines all tooltips info defined by modules.
*
* @return array
* An array containing defined tooltips info.
*/
function _comment_easy_reply_get_tooltips() {
static $tooltips;
if (!$tooltips) {
foreach (module_implements('comment_easy_reply_tooltip') as $module) {
$info = module_invoke($module, 'comment_easy_reply_tooltip');
if (!empty($info) && is_array($info)) {
foreach ($info as $tooltip_name => $tooltip_info) {
if (!isset($tooltips[$tooltip_name])) {
$tooltip_info['module'] = $module;
$tooltips[$tooltip_name] = $tooltip_info;
}
}
}
}
drupal_alter('comment_easy_reply_tooltip', $tooltips);
}
return $tooltips;
}
/**
* Gets default css classes for a tooltip on comment's permalink.
*
* @param array $variables
* An associative array with the following structure:
* 'comment' - The comment.
* 'comment_easy_reply_comment_num' - The comment number (optional).
* @return array
* An array of classes.
*/
function _comment_easy_reply_get_number_tips_classes($variables) {
if (isset($variables['comment_easy_reply_comment_num'])) {
$comment_num = $variables['comment_easy_reply_comment_num'];
}
else {
$comment_num = _comment_easy_reply_get_comment_number($variables['comment']);
}
$comment_num = str_replace('.', '-', $comment_num);
$default_classes = array(
'comment-easy-reply-number-link-tips',
'comment-easy-reply-number-link-tips-' . $comment_num,
);
return _comment_easy_reply_merge_classes($default_classes, $variables);
}
/**
* Gets default css classes for a permalink tooltip's link.
*
* @param array $variables
* An associative array with the following structure:
* 'comment' - The comment.
* 'comment_easy_reply_comment_num' - The comment number (optional).
* @return array
* An array of classes.
*/
function _comment_easy_reply_get_number_link_classes($variables) {
if (isset($variables['comment_easy_reply_comment_num'])) {
$comment_num = $variables['comment_easy_reply_comment_num'];
}
else {
$comment_num = _comment_easy_reply_get_comment_number($variables['comment']);
}
$comment_num = str_replace('.', '-', $comment_num);
$default_classes = array(
'comment-easy-reply-number-link',
'comment-easy-reply-comment-number-link',
'comment-easy-reply-linknum-' . $comment_num,
'comment-easy-reply-node-' . $variables['comment']->nid,
'comment-easy-reply-cid-' . $variables['comment']->cid,
);
return _comment_easy_reply_merge_classes($default_classes, $variables);
}
/**
* Gets default css classes for a referrer link.
*
* @param array $variables
* An associative array with the following structure:
* 'comment' - The comment.
* 'comment_easy_reply_comment_num' - The comment number (optional).
* @return array
* An array of classes.
*/
function _comment_easy_reply_get_referrer_link_classes($variables) {
if (isset($variables['comment_easy_reply_comment_num'])) {
$comment_num = $variables['comment_easy_reply_comment_num'];
}
else {
$comment_num = _comment_easy_reply_get_comment_number($variables['comment']);
}
$comment_num = str_replace('.', '-', $comment_num);
$default_classes = array(
'comment-easy-reply-number-link',
'comment-easy-reply-referrer-link',
'comment-easy-reply-linknum-' . $comment_num,
);
return _comment_easy_reply_merge_classes($default_classes, $variables);
}
/**
* Gets default css classes for a referrer link's tooltip.
*
* @param array $variables
* An associative array with the following structure:
* 'comment' - The comment.
* 'comment_easy_reply_comment_num' - The comment number (optional).
* @return array
* An array of classes.
*/
function _comment_easy_reply_get_referrer_tips_classes($variables) {
if (isset($variables['comment_easy_reply_comment_num'])) {
$comment_num = $variables['comment_easy_reply_comment_num'];
}
else {
$comment_num = _comment_easy_reply_get_comment_number($variables['comment']);
}
$comment_num = str_replace('.', '-', $comment_num);
$default_classes = array('comment-easy-reply-referrer-link-hover', 'comment-easy-reply-referrer-tips-' . $comment_num);
return _comment_easy_reply_merge_classes($default_classes, $variables);
}
/**
* Merge default css classes with additional classes.
*
* @param array $default_classes
* An array of default css classes.
* @param array $variables
* An associative array with the following structure:
* 'class' - An array of additional classes (optional).
* @return array
* An array of classes.
*/
function _comment_easy_reply_merge_classes($default_classes, $variables) {
if (isset($variables['class']) && !empty($variables['class'])) {
if (!is_array($variables['class'])) {
$default_classes[] = $variables['class'];
}
else {
$default_classes = drupal_array_merge_deep($default_classes, $variables['class']);
}
}
return $default_classes;
}
/**
* Gets tooltip for a comment referrer link.
*
* @param array $variables
* An associative array with the following structure:
* 'comment' - The comment.
* 'comment_easy_reply_comment_num' - The comment number (optional).
* 'override_text' - The text to be written inside the tooltip.
* This will override the standard module tooltip
* content (optional).
* @return string
* The tooltip output.
*/
function _comment_easy_reply_get_referrer_tips($variables) {
$node_type = _comment_easy_reply_get_comment_node_type($variables['comment']);
if (isset($variables['override_text']) && !empty($variables['override_text'])) {
$text = $variables['override_text'];
}
else {
$text = _comment_easy_reply_get_settings_value('comment_easy_reply_referrertip_text', $node_type);
$text = token_replace($text, array('comment' => $variables['comment']));
$text = _comment_easy_reply_comment_body_replace($text, $variables['comment']->nid, FALSE, $variables);
}
$tips = '';
if (!empty($text)) {
$filter_name = _comment_easy_reply_get_settings_value('comment_easy_reply_filter', $node_type);
if ($filter_name) {
$text = check_markup($text, $filter_name);
$tips = $text;
}
else {
$tips = $text;
}
}
return $tips;
}
/**
* Calculate a comment number.
*
* The number is based on comment's thread value.
*
* @param object $comment
* The comment to get the number for.
* @param bool $use_static
* If FALSE, forces the function to calculate comment nuber on every
* occurrence.
*
* @return int|string
* The comment's thread value.
*/
function _comment_easy_reply_get_comment_number($comment, $use_static = TRUE) {
if (empty($comment)) {
return 0;
}
if ($use_static) {
$cid2num = &drupal_static('comment_easy_reply_cid2num');
$num2cid = &drupal_static('comment_easy_reply_num2cid');
if (empty($cid2num[$comment->nid])) {
$cid2num[$comment->nid] = array();
}
else {
if (isset($cid2num[$comment->nid][$comment->cid])) {
return $cid2num[$comment->nid][$comment->cid];
}
}
}
$thread_string = '';
if (isset($comment->thread)) {
$thread_string = _comment_easy_reply_get_comment_thread_string($comment);
if ($thread_string) {
$cid2num[$comment->nid][$comment->cid] = $thread_string;
$num2cid[$comment->nid][$thread_string] = $comment->cid;
}
}
return $thread_string;
}
/**
* Add module's javascript file and settings.
*
* If 'comment_easy_reply_js_path' variable has been set during preprocess
* functions, it will be used as path to js file.
* Otherwise module's default path will be used.
*
* @param array $variables
* An associative array with the following structure:
* 'comment' - The comment.
* 'comment_easy_reply_js_path' - The js file path (optional).
* 'comment_easy_reply_replytip_activated' - TRUE if the replytip mode is
* active for comment's node type (optional).
* 'comment_easy_reply_reply_from_numlink' - TRUE if the reply from permalink
* mode is active for comment's node type (optional).
*/
function comment_easy_reply_add_js($variables) {
static $js;
static $settings;
$javascript = '';
if (!$js) {
if (isset($variables['comment_easy_reply_js_path'])) {
$js_path = $variables['comment_easy_reply_js_path'];
}
else {
$js_path = drupal_get_path('module', 'comment_easy_reply') . '/js';
}
drupal_add_js($js_path . '/comment_easy_reply.js', 'file');
if (empty($settings)) {
$settings = array('comment_easy_reply' => array());
$settings['comment_easy_reply']['clean_url'] = variable_get('clean_url', FALSE);
$settings['comment_easy_reply']['reply_page'] = FALSE;
$settings['comment_easy_reply']['user_can_reply'] = FALSE;
$settings['comment_easy_reply']['in_preview'] = FALSE;
$settings['comment_easy_reply']['scrollform_enabled'] = TRUE;
$settings['comment_easy_reply']['ajax_reply_enabled'] = TRUE;
$settings['comment_easy_reply']['tooltip_native_enabled'] = TRUE;
$settings['comment_easy_reply']['quote_enabled'] = module_exists('quote');
$settings['comment_easy_reply']['ideal_comments_enabled'] = module_exists('ideal_comments');
$settings['comment_easy_reply']['b2_nice_comments_enabled'] = module_exists('b2_nice_comments');
if (isset($variables['comment'])) {
$node_type = _comment_easy_reply_get_comment_node_type($variables['comment']);
$settings['comment_easy_reply']['replytip_activated'] = _comment_easy_reply_get_settings_value('comment_easy_reply_replytip_activated', $node_type);
$settings['comment_easy_reply']['reply_from_numlink'] = _comment_easy_reply_get_settings_value('comment_easy_reply_reply_from_numlink', $node_type);
}
if (_comment_easy_reply_is_reply_page()) {
$settings['comment_easy_reply']['reply_page'] = TRUE;
}
if (user_access('post comments')) {
$settings['comment_easy_reply']['user_can_reply'] = TRUE;
}
}
}
elseif (isset($variables['comment_easy_reply_js_override']) && is_array($variables['comment_easy_reply_js_override'])) {
$javascript = &drupal_static('drupal_add_js', array());
foreach ($javascript['settings']['data'] as $key => &$data) {
if (isset($data['comment_easy_reply'])) {
foreach ($variables['comment_easy_reply_js_override'] as $override_key => $override_value) {
$data['comment_easy_reply'][$override_key] = $override_value;
}
}
}
}
if (isset($variables['comment_easy_reply_replytip_activated'])) {
$settings['comment_easy_reply']['replytip_activated'] = $variables['comment_easy_reply_replytip_activated'];
}
if (isset($variables['comment_easy_reply_reply_from_numlink'])) {
$settings['comment_easy_reply']['reply_from_numlink'] = $variables['comment_easy_reply_reply_from_numlink'];
}
if (isset($variables['comment']->in_preview) && $variables['comment']->in_preview) {
$settings['comment_easy_reply']['in_preview'] = TRUE;
}
if ($js) {
return $javascript;
}
$js = TRUE;
return drupal_add_js($settings, 'setting');
}
/**
* Add module's css file.
*
* If 'comment_easy_reply_css_path' variable has been set during preprocess
* functions, it will be used as path to css file.
* Otherwise module's default path will be used.
*
* @param array $variables
* An associative array with the following structure:
* 'comment_easy_reply_css_path' - The css file path (optional).
*/
function comment_easy_reply_add_css($variables) {
static $css;
if (!$css) {
if (isset($variables['comment_easy_reply_css_path'])) {
$css_path = $variables['comment_easy_reply_css_path'];
}
else {
$css_path = drupal_get_path('module', 'comment_easy_reply') . '/css';
}
drupal_add_css($css_path . '/comment_easy_reply.css', 'file');
$css = TRUE;
}
}
/**
* Calculate a comment number.
*
* If comment is threated, the number will be equal to thread.
*
* @param object $comment
* The comment to get the number for.
* @param int $comments_per_page
* The number of comments to be dysplayed on a single page.
* This parameter is used when comments per page value is different from the
* node settings (i.e. in Views)
* @param array $custom_source
* A custom array of comments, with different order from the standard node
* comments.
* This parameter is used when comments order is different from the node
* settings (i.e. in Views)
*
* @return int|string
* The number if comment is not threaded, the thread otherwise.
*/
function _comment_easy_reply_get_comment_page($comment, $comments_per_page = NULL, $custom_source = array()) {
if (is_null($comments_per_page)) {
$node_type = _comment_easy_reply_get_comment_node_type($comment);
$comments_per_page = variable_get('comment_default_per_page_' . $node_type, 50);
}
if (!empty($custom_source)) {
$current_page = 0;
$current_pos = 0;
foreach ($custom_source as $cid => $thread) {
if ($current_pos > 0) {
if (($current_pos % $comments_per_page) == 0) {
$current_page++;
}
}
$current_pos++;
if ($comment->cid == $cid) {
return $current_page;
}
}
}
_comment_easy_reply_get_comment_number($comment);
$cid2num = &drupal_static('comment_easy_reply_cid2num');
$current_page = 0;
$current_pos = 0;
if (isset($cid2num[$comment->nid]) && !empty($cid2num[$comment->nid])) {
$numbers = $cid2num[$comment->nid];
asort($numbers);
foreach ($numbers as $cid => $thread) {
if ($comment->cid == $cid) {
return $current_page;
}
if ($current_pos > 0 && ($current_pos % $comments_per_page) == 0) {
$current_page++;
}
$current_pos++;
}
}
return NULL;
}
/**
* Get a comment cid from number.
*
* If comment is threated, the number will be equal to thread.
*
* @param int|string $number
* The comment number or thread.
* @param int $nid
* The comment's node id.
*
* @return int
* The comment's id.
*/
function _comment_easy_reply_get_comment_cid_from_num($number, $nid) {
$cid2num = &drupal_static('comment_easy_reply_cid2num');
$num2cid = &drupal_static('comment_easy_reply_num2cid');
if (isset($num2cid[$nid][$number])) {
return $num2cid[$nid][$number];
}
if (!empty($num2cid[$nid]) && isset($num2cid[$nid][$number])) {
return $num2cid[$nid][$number];
}
$node = node_load($nid);
$mode = variable_get('comment_default_mode_' . $node->type, COMMENT_MODE_THREADED);
$comments_per_page = variable_get('comment_default_per_page_' . $node->type, 50);
$flat = FALSE;
if ($mode == COMMENT_MODE_FLAT) {
$flat = TRUE;
}
$cids = comment_easy_reply_comment_get_thread($node, $mode);
sort($cids);
if (!empty($cids)) {
$comments = comment_load_multiple($cids);
foreach ($comments as $comment_obj) {
$thread_string = _comment_easy_reply_get_comment_thread_string($comment_obj);
if ($thread_string) {
$num2cid[$nid][$thread_string] = $comment_obj->cid;
$cid2num[$nid][$comment_obj->cid] = $thread_string;
}
}
if (!empty($num2cid[$nid])) {
if (isset($num2cid[$nid][$number])) {
return $num2cid[$nid][$number];
}
}
}
return 0;
}
/**
* Calculate a comment number based on its thread.
*
* @param object $comment
* The comment.
*
* @return string
* The comment's number.
*/
function _comment_easy_reply_get_comment_thread_string($comment) {
$thread_string = '';
if (!$comment || !isset($comment->thread)) {
return $thread_string;
}
$thread = explode('.', substr($comment->thread, 0, -1));
$thread_parts = array();
while ($th = array_shift($thread)) {
$num = vancode2int($th);
if (!empty($thread_parts)) {
$num++;
}
$thread_parts[] = $num;
}
$thread_string = implode('.', $thread_parts);
return $thread_string;
}
/**
* Gets node comments.
*
* This function is derived from core's comment_get_thread() function.
*
* @param object $node
* The node to extract comments from.
* @param int $mode
* The node comments mode (flat or threaded).
*
* @return array
* The comments ids.
*
* @see comment_get_thread()
*/
function comment_easy_reply_comment_get_thread($node, $mode) {
$query = db_select('comment', 'c');
$query->addField('c', 'cid');
$query->condition('c.nid', $node->nid);
if (!user_access('administer comments')) {
$query->condition('c.status', COMMENT_PUBLISHED);
}
if ($mode === COMMENT_MODE_FLAT) {
$query->orderBy('c.cid', 'ASC');
}
else {
// See comment above. Analysis reveals that this doesn't cost too
// much. It scales much much better than having the whole comment
// structure.
$query->addExpression('SUBSTRING(c.thread, 1, (LENGTH(c.thread) - 1))', 'torder');
$query->orderBy('torder', 'ASC');
}
$cids = $query->execute()->fetchCol();
return $cids;
}
/**
* Replace references in a comment.
*
* @param object $comment
* The comment with a comment reference in its body.
* @param bool $include_parent
* If TRUE, the parent comment body will be displayed inside the tooltip.
*
* @return object
* The comment with references replaced.
*/
function _comment_easy_reply_comment_replace($comment, $include_parent = TRUE) {
$comment_body = '';
if (isset($comment->content['comment_body'][0]['#markup'])) {
$comment_body = &$comment->content['comment_body'][0]['#markup'];
}
else {
$items = field_get_items('comment', $comment, 'comment_body');
if (!empty($items)) {
$value = array_shift($items);
$comment_body = &$value['safe_value'];
}
}
if (!empty($comment_body)) {
$comment_nid = $comment->nid;
$matches = _comment_easy_reply_get_matches($comment_body . $comment->subject);
if (!empty($matches)) {
foreach ($matches as $num) {
$parent = NULL;
if ($include_parent) {
$parent_cid = _comment_easy_reply_get_comment_cid_from_num($num, $comment_nid);
$parent = comment_load($parent_cid);
if (!empty($parent)) {
$comment = _comment_easy_reply_replace_comment_parent_occurrences($num, $comment_body, $parent, $comment);
}
}
if (!$include_parent || empty($parent)) {
$comm_cid = _comment_easy_reply_get_comment_cid_from_num($num, $comment_nid);
$comm = comment_load($comm_cid);
if ($comm) {
$tag = _comment_easy_reply_get_tag($comm, FALSE);
if ($tag) {
$options = array(
'tag' => '#' . $tag . '',
'comment' => $comm,
);
$comment_body = _comment_easy_reply_comment_body_replace($comment_body, $comment_nid, FALSE, $options);
}
$tag = _comment_easy_reply_get_tag($comm, FALSE);
if ($tag) {
$options['tag'] = '#' . $tag . '';
$comment->subject = _comment_easy_reply_comment_body_replace($comment->subject, $comment_nid, FALSE, $options);
}
}
}
}
}
$comment->content['comment_body'][0]['#markup'] = $comment_body;
}
return $comment;
}
/**
* Replace references in a comment body.
*
* @param string $comment_body
* The comment body containing a comment reference.
* @param int $comment_nid
* The nid of content owning the comment.
* @param bool $include_parent
* If TRUE, the parent comment body will be displayed inside the tooltip.
* @param array $options
* An associative array of options:
* - tag: the tag used to replace a #@NUM tag occurrence.
* - tag_link: if FALSE, the tag used to replace a #@NUM tag occurrence
* will not be added as a link but as a simple text.
*
* @return string
* The comment body with references replaced.
*/
function _comment_easy_reply_comment_body_replace($comment_body, $comment_nid, $include_parent = TRUE, $options = array()) {
$original_body = $comment_body;
$matches = _comment_easy_reply_get_matches($comment_body);
if (!empty($matches)) {
foreach ($matches as $num) {
if (empty($num)) {
continue;
}
$parent = NULL;
if ($include_parent) {
$parent_cid = _comment_easy_reply_get_comment_cid_from_num($num, $comment_nid);
$parent = comment_load($parent_cid);
if (!empty($parent)) {
_comment_easy_reply_replace_comment_parent_occurrences($num, $comment_body, $parent);
}
}
if (!$include_parent || empty($parent)) {
if (isset($options['tag'])) {
$tag = '';
if (is_array($options['tag'])) {
if (isset($options['tag'][$num])) {
$tag = $options['tag'][$num];
}
}
else {
$tag = $options['tag'];
}
if ($tag) {
if (isset($options['tag_link']) && $options['tag_link'] === FALSE) {
$comment_body = str_replace('@#' . $num, $tag, $comment_body);
}
else {
$comment_body = str_replace('@#' . $num, '#' . $tag . '', $comment_body);
}
}
}
else {
if (isset($options['tag_link']) && $options['tag_link'] === FALSE) {
$comment_body = str_replace('@#' . $num, $num, $comment_body);
}
else {
$comment_body = str_replace('@#' . $num, '#' . $num . '', $comment_body);
}
}
}
}
}
foreach (module_implements('comment_easy_reply_text_replace_alter') as $module) {
$function = $module . '_comment_easy_reply_text_replace_alter';
$function($comment_body, $comment_nid, $include_parent, $options, $original_body);
}
return $comment_body;
}
/**
* Find @#NUM occurences inside a given string.
*
* @param string $text
* The string to parse.
*
* @return array
* A array of occurrences. If no occurrence is found, the array will be
* empty.
*/
function _comment_easy_reply_get_matches($text) {
$result = array();
$matches = array();
preg_match_all(COMMENT_EASY_REPLY_REGEXP_MATCHES, $text, $matches);
if (isset($matches[1]) && !empty($matches[1])) {
rsort($matches[1]);
foreach ($matches[1] as $num) {
if (substr($num, -1) == '.') {
$num = substr($num, 0, -1);
}
$parts = explode('.', $num);
$part = array_shift($parts);
// The first number must be greater than 0.
if (!is_numeric($part) || $part <= 0) {
continue;
}
if (count($parts)) {
// Checking other numbers following the first.
foreach ($parts as $part) {
if (!is_numeric($part) || $part < 0) {
continue;
}
}
}
$result[$num] = $num;
}
}
foreach (module_implements('comment_easy_reply_text_matches_alter') as $module) {
$function = $module . '_comment_easy_reply_text_matches_alter';
$function($text, $result);
}
return $result;
}
/**
* Replace @#NUM occurences inside a given comment with a tooltip.
*
* All the occurences of a given @#NUM will be replaced with a link and a
* tooltip of one of the comment's parents.
* Also the comment subject can be replaced at the same time.
*
* @param int $num
* The comment parent number.
* @param string &$comment_body
* The comment body.
* @param object $parent
* The comment's parent that will be used to replace @#NUM occurences.
* To compose @#NUM tag will be used the given $num variable.
* @param object $comment
* If the original comment is given, also the tags into its subject will be
* replaced. If $comment is NULL, no subject will be parsed.
*
* @return object|NULL
* The comment with subject replaced if a comment is given, NULL otherwise.
*/
function _comment_easy_reply_replace_comment_parent_occurrences($num, &$comment_body, $parent, $comment = NULL) {
$parent = _comment_easy_reply_comment_replace($parent, FALSE);
$variables = array('comment' => $parent, 'comment_easy_reply_comment_num' => $num);
$variables['comment_body'] = $comment_body;
$variables['num'] = $num;
$variables['parent'] = $parent;
_comment_easy_reply_get_tooltip('comment_referrer_tips', $variables);
_comment_easy_reply_get_tooltip('comment_referrer_link', $variables);
$comment_body = str_replace('@#' . $variables['num'], $variables['referrer_link'], $comment_body);
$comment_body .= $variables['referrer_tips'];
if (!is_null($comment)) {
$title_tag = _comment_easy_reply_get_tag($parent, TRUE);
$comment->subject = str_replace('@#' . $num, $title_tag, $comment->subject);
}
return $comment;
}
/**
* Find references in a comment body.
*
* @param string $comment_body
* The comment body with a comment reference in it.
* @param int $comment_nid
* The comment nid.
*
* @return array
* The references' cids.
*/
function _comment_easy_reply_comment_find_references($comment_body, $comment_nid) {
$parents = array();
$matches = _comment_easy_reply_get_matches($comment_body);
if (!empty($matches)) {
foreach ($matches as $num) {
$parent_cid = _comment_easy_reply_get_comment_cid_from_num($num, $comment_nid);
if ($parent_cid) {
$parents[$parent_cid] = $parent_cid;
}
}
}
return $parents;
}
/**
* Check if Comment Easy Reply is enabled for a comment's node type.
*
* @param object $comment
* The comment to check node type for.
*
* @return bool
* TRUE if module is enabled for comment's node type, FALSE otherwise.
*/
function _comment_easy_reply_comment_node_type_is_allowed($comment) {
$node_type = _comment_easy_reply_get_comment_node_type($comment);
return _comment_easy_reply_is_node_type_active($node_type);
}
/**
* Get a comment's node type.
*
* @param object $comment
* The comment to get node type from.
*
* @return string
* The comment's node type.
*/
function _comment_easy_reply_get_comment_node_type($comment) {
static $node_types;
if (empty($node_types)) {
$node_types = array();
}
elseif (isset($node_types[$comment->cid])) {
return $node_types[$comment->cid];
}
if (!isset($comment->node_type)) {
$comment = comment_load($comment->cid);
}
$node_type = $comment->node_type;
$node_type = substr($node_type, 13);
$node_types[$comment->cid] = $node_type;
return $node_type;
}
/**
* Return a list of node types having the module activated on their comments.
*
* @return array
* An array containing the list of node types.
*/
function _comment_easy_reply_get_active_node_types() {
return variable_get('comment_easy_reply_node_types', array());
}
/**
* Set a list of node types having the module activated on their comments.
*
* @param array $node_types
* An array containing the list of node types.
*/
function _comment_easy_reply_set_active_node_types($node_types) {
variable_set('comment_easy_reply_node_types', $node_types);
cache_clear_all('field:comment:', 'cache_field', TRUE);
}
/**
* Return the module's status for a specific node_type.
*
* @param string $node_type
* The node type.
*
* @return bool
* TRUE if module has been activated for the node type, FALSE otherwise.
*/
function _comment_easy_reply_is_node_type_active($node_type) {
$active = FALSE;
$actives = _comment_easy_reply_get_active_node_types();
if (!empty($actives) && in_array($node_type, $actives)) {
$active = TRUE;
}
return $active;
}
/**
* Return the module's override option status for a specific node_type.
*
* @param string $node_type
* The node type.
*
* @return bool
* TRUE if the node type has overrided settings, FALSE otherwise.
*/
function _comment_easy_reply_is_node_type_override_active($node_type) {
return _comment_easy_reply_get_settings_value('comment_easy_reply_override', $node_type);
}
/**
* Return a specific setting value for a specific node_type.
*
* Every node type can have specific settings that override the general
* module's settings. Using this function, the returned
*
* @param string $name
* The option name.
* @param string $node_type
* The node type.
*
* @return bool|string|int
* The setting's value.
*/
function _comment_easy_reply_get_settings_value($name, $node_type) {
if (!_comment_easy_reply_is_node_type_active($node_type)) {
return FALSE;
}
$settings = _comment_easy_reply_get_settings_names();
if (isset($settings[$name]['callback'])) {
return call_user_func_array($settings[$name]['callback'], array($name, $node_type));
}
return FALSE;
}
/**
* Get a settings value.
*
* @param string $name
* the setting name.
* @param string $node_type
* the content node type machine name.
*
* @return mixed
* the setting value. If variable does not exists, returns FALSE.
*/
function _comment_easy_reply_settings_value_callback($name, $node_type) {
switch ($name) {
case 'comment_easy_reply_override':
$value = variable_get('comment_easy_reply_override_' . $node_type, FALSE);
if ($value) {
return TRUE;
}
return FALSE;
case 'comment_easy_reply_reply_from_numlink':
if (_comment_easy_reply_is_node_type_override_active($node_type)) {
return variable_get('comment_easy_reply_reply_from_numlink_' . $node_type, COMMENT_EASY_REPLY_DEFAULT_REPLY_FROM_NUMLINK);
}
return variable_get('comment_easy_reply_reply_from_numlink', COMMENT_EASY_REPLY_DEFAULT_REPLY_FROM_NUMLINK);
case 'comment_easy_reply_replytip_activated':
if (_comment_easy_reply_is_node_type_override_active($node_type)) {
return variable_get('comment_easy_reply_replytip_activated_' . $node_type, COMMENT_EASY_REPLY_DEFAULT_REPLYTIP_ACTIVATED);
}
return variable_get('comment_easy_reply_replytip_activated', COMMENT_EASY_REPLY_DEFAULT_REPLYTIP_ACTIVATED);
case 'comment_easy_reply_fragment_activated':
if (_comment_easy_reply_is_node_type_override_active($node_type)) {
return variable_get('comment_easy_reply_fragment_activated_' . $node_type, COMMENT_EASY_REPLY_DEFAULT_FRAGMENT_ACTIVATED);
}
return variable_get('comment_easy_reply_fragment_activated', COMMENT_EASY_REPLY_DEFAULT_FRAGMENT_ACTIVATED);
case 'comment_easy_reply_link_mode':
if (_comment_easy_reply_is_node_type_override_active($node_type)) {
return variable_get('comment_easy_reply_link_mode_' . $node_type, COMMENT_EASY_REPLY_DEFAULT_LINK_MODE);
}
return variable_get('comment_easy_reply_link_mode', COMMENT_EASY_REPLY_DEFAULT_LINK_MODE);
case 'comment_easy_reply_replytip_text':
if (_comment_easy_reply_is_node_type_override_active($node_type)) {
$value = variable_get('comment_easy_reply_replytip_text_' . $node_type, COMMENT_EASY_REPLY_DEFAULT_REPLYTIP_TEXT);
}
else {
$value = variable_get('comment_easy_reply_replytip_text', COMMENT_EASY_REPLY_DEFAULT_REPLYTIP_TEXT);
}
return check_plain($value);
case 'comment_easy_reply_referrertip_text':
if (_comment_easy_reply_is_node_type_override_active($node_type)) {
$value = variable_get('comment_easy_reply_referrertip_text_' . $node_type, COMMENT_EASY_REPLY_DEFAULT_REFERRERTIP_TEXT);
}
else {
$value = variable_get('comment_easy_reply_referrertip_text', COMMENT_EASY_REPLY_DEFAULT_REFERRERTIP_TEXT);
}
return check_plain($value);
case 'comment_easy_reply_filter':
if (_comment_easy_reply_is_node_type_override_active($node_type)) {
return variable_get('comment_easy_reply_filter_' . $node_type, '');
}
return variable_get('comment_easy_reply_filter', '');
case 'comment_easy_reply_fragment_text':
if (_comment_easy_reply_is_node_type_override_active($node_type)) {
$value = variable_get('comment_easy_reply_fragment_text_' . $node_type, COMMENT_EASY_REPLY_DEFAULT_FRAGMENT_TEXT);
}
else {
$value = variable_get('comment_easy_reply_fragment_text', COMMENT_EASY_REPLY_DEFAULT_FRAGMENT_TEXT);
}
return check_plain($value);
case 'comment_easy_reply_quote_tooltip':
if (_comment_easy_reply_is_node_type_override_active($node_type)) {
return variable_get('comment_easy_reply_quote_tooltip_' . $node_type, COMMENT_EASY_REPLY_DEFAULT_QUOTE_TOOLTIP);
}
return variable_get('comment_easy_reply_quote_tooltip', COMMENT_EASY_REPLY_DEFAULT_QUOTE_TOOLTIP);
case 'comment_easy_reply_comment_title':
if (_comment_easy_reply_is_node_type_override_active($node_type)) {
return variable_get('comment_easy_reply_comment_title_' . $node_type, COMMENT_EASY_REPLY_DEFAULT_REPLY_TITLE);
}
return variable_get('comment_easy_reply_comment_title', COMMENT_EASY_REPLY_DEFAULT_REPLY_TITLE);
case 'comment_easy_reply_tooltip_name':
if (_comment_easy_reply_is_node_type_override_active($node_type)) {
$name = variable_get('comment_easy_reply_tooltip_name_' . $node_type, COMMENT_EASY_REPLY_DEFAULT_TOOLTIP_MODULE);
if (empty($name)) {
return COMMENT_EASY_REPLY_DEFAULT_TOOLTIP_MODULE;
}
return $name;
}
$name = variable_get('comment_easy_reply_tooltip_name', COMMENT_EASY_REPLY_DEFAULT_TOOLTIP_MODULE);
if (empty($name)) {
return COMMENT_EASY_REPLY_DEFAULT_TOOLTIP_MODULE;
}
return $name;
}
return FALSE;
}
/**
* Invokes hook_comment_easy_reply_settings() to get all settings.
*
* @return array
* The array of settings callbacks and info, grouped by module names.
*/
function _comment_easy_reply_get_settings_names() {
$return = &drupal_static('comment_easy_reply_settings');
if (!empty($return)) {
return $return;
}
$return = array();
$return = module_invoke_all('comment_easy_reply_settings');
drupal_alter('comment_easy_reply_settings', $return);
return $return;
}
/**
* Return a comment's url fragment.
*
* Process the fragment using settings and tokens.
*
* @param object $comment
* The comment.
*
* @return string
* The custom url fragment.
*/
function _comment_easy_reply_get_fragment($comment) {
$fragment = 'comment-' . $comment->cid;
$node_type = _comment_easy_reply_get_comment_node_type($comment);
if (_comment_easy_reply_get_settings_value('comment_easy_reply_fragment_activated', $node_type)) {
$fragment = _comment_easy_reply_get_settings_value('comment_easy_reply_fragment_text', $node_type);
$fragment = token_replace($fragment, array('comment' => $comment));
}
return $fragment;
}
/**
* Validate a comment's url fragment.
*
* @param string $text
* The fragment.
* @param bool &$error
* Initiallly FALSE, can be turned to TRUE if fragment is not valid.
*
* @return array
* An array containing not allowed characters found in fragment.
*/
function _comment_easy_reply_validate_fragment_text($text, &$error = FALSE) {
$match = array();
$messages = array();
if (empty($text)) {
return array();
}
$text_tokens = token_scan($text);
if (!empty($text_tokens)) {
foreach ($text_tokens as $type => $tokens) {
$text = str_replace(array_values($tokens), '', $text);
}
}
else {
$error = TRUE;
$messages[] = t('At least one token (i.e. !tokens) must be included in Fragment text in order to let the fragment be unique in the page it is showed.',
array('!tokens' => '[comment_easy_reply:comment_number], [comment:cid], [comment:created]'));
}
if (!empty($text)) {
preg_match_all('/[^a-zA-Z0-9\-]/', $text, $match);
if (!empty($match[0]) && is_array($match[0])) {
$error = TRUE;
$messages[] = t('Only %allowed are allowed in fragment.', array('%allowed' => 'numbers, letters, dashes and tokens'));
}
}
return $messages;
}
/**
* Check if current page is a comment reply page.
*
* @return bool
* TRUE if the page is a comment reply page, FALSE otherwise.
*/
function _comment_easy_reply_is_reply_page() {
if (arg(0) == 'comment' && arg(1) == 'reply') {
return TRUE;
}
return FALSE;
}
/**
* Returns the current threading setting status of a comment's node.
*
* @param object $comment
* The comment.
*
* @return bool
* TRUE if the node's comments are threaded, FALSE otherwise.
*/
function _comment_easy_reply_is_comment_node_type_threaded($comment) {
$node_type = _comment_easy_reply_get_comment_node_type($comment);
$mode = variable_get('comment_default_mode_' . $node_type, COMMENT_MODE_THREADED);
$threaded = TRUE;
if ($mode == COMMENT_MODE_FLAT) {
$threaded = FALSE;
}
return $threaded;
}
/**
* Invokes hook_comment_easy_reply_tags() to get all tags callbacks.
*
* @return array
* The array of tags callbacks and info, grouped by module names.
*/
function _comment_easy_reply_get_tags() {
$return = &drupal_static('comment_easy_reply_tags');
if (!empty($return)) {
return $return;
}
$return = array();
foreach (module_implements('comment_easy_reply_tags') as $module) {
$function = $module . '_comment_easy_reply_tags';
if (function_exists($function)) {
$result = call_user_func_array($function, array());
if (isset($result) && is_array($result)) {
$return[$module] = $result;
}
}
}
drupal_alter('comment_easy_reply_tags', $return);
return $return;
}
/**
* Get the setted tag for a comment's node type.
*
* @param object $comment
* The comment.
* @param bool $title
* TRUE if the title callback has to be invoked, FALSE otherwise.
*
* @return string
* The tag to use into the comment.
*/
function _comment_easy_reply_get_tag($comment, $title = FALSE) {
static $tags;
if (empty($tags)) {
$tags = array();
}
if ($title) {
if (isset($tags[$comment->cid]) && isset($tags[$comment->cid]['title'])) {
return $tags[$comment->cid]['title'];
}
}
else {
if (isset($tags[$comment->cid]) && isset($tags[$comment->cid]['body'])) {
return $tags[$comment->cid]['body'];
}
}
$tag = '';
$node_type = _comment_easy_reply_get_comment_node_type($comment);
$mode = _comment_easy_reply_get_settings_value('comment_easy_reply_link_mode', $node_type);
$values = explode('|', $mode);
if (!empty($values)) {
if (count($values) > 1) {
$module_name = array_shift($values);
$mode_name = array_shift($values);
$tags = _comment_easy_reply_get_tags();
if ($title) {
if (isset($tags[$module_name][$mode_name]['title callback'])) {
$tag = call_user_func_array($tags[$module_name][$mode_name]['title callback'], array($comment));
}
elseif (isset($tags[$module_name][$mode_name]['callback'])) {
$tag = call_user_func_array($tags[$module_name][$mode_name]['callback'], array($comment));
}
else {
$tag = _comment_easy_reply_tag_number_title($comment);
}
$tags[$comment->cid]['title'] = $tag;
}
else {
if (isset($tags[$module_name][$mode_name]['callback'])) {
$tag = call_user_func_array($tags[$module_name][$mode_name]['callback'], array($comment));
}
else {
$tag = _comment_easy_reply_tag_number($comment);
}
$tags[$comment->cid]['body'] = $tag;
}
}
else {
$mode_name = array_shift($values);
if ($mode == 'number') {
$tag = _comment_easy_reply_tag_number($comment);
}
else {
$tag = _comment_easy_reply_tag_username($comment);
}
$tags[$comment->cid]['body'] = $tag;
}
}
return $tag;
}
/**
* Callback for hook_comment_easy_reply_tags().
*
* @param object $comment
* The comment.
*
* @return string
* The string to replace the tag into the title with.
*
* @see comment_easy_reply_comment_easy_reply_tags()
*/
function _comment_easy_reply_tag_number_title($comment) {
return _comment_easy_reply_tag_number($comment);
}
/**
* Callback for hook_comment_easy_reply_tags().
*
* @param object $comment
* The comment.
*
* @return string
* The string to replace the tag into the title with.
*
* @see comment_easy_reply_comment_easy_reply_tags()
*/
function _comment_easy_reply_tag_username_title($comment) {
return _comment_easy_reply_tag_username($comment);
}
/**
* Callback for hook_comment_easy_reply_tags().
*
* @param object $comment
* The comment.
*
* @return string
* The string to replace the tag into the comment body with.
*
* @see comment_easy_reply_comment_easy_reply_tags()
*/
function _comment_easy_reply_tag_number($comment) {
static $tags;
if (empty($tags)) {
$tags = array();
}
if (isset($tags[$comment->cid])) {
return $tags[$comment->cid];
}
if (isset($comment->comment_easy_reply_comment_number)) {
$comment_num = $comment->comment_easy_reply_comment_number;
}
else {
$comment_num = _comment_easy_reply_get_comment_number($comment);
}
if ($comment_num) {
$tags[$comment->cid] = '#' . $comment_num;
return $tags[$comment->cid];
}
return '';
}
/**
* Callback for hook_comment_easy_reply_tags().
*
* @param object $comment
* The comment.
*
* @return string
* The string to replace the tag into the comment body with.
*
* @see comment_easy_reply_comment_easy_reply_tags()
*/
function _comment_easy_reply_tag_username($comment) {
return '@' . $comment->name;
}
/**
* Callback for quote bbcode ajax inserting.
*
* This function is the same of _quote_filter_process function in
* Quote module, but it calls a different callback function for the [quote]
* replacing.
*
* @param int $nid
* The current node nid.
* @param int $cid
* The quoted comment cid.
*
* @return string
* The drupal_json_output function output of the quote bbcode string.
*
* @see drupal_json_output()
*/
function _comment_easy_reply_quote_get_quote_bbcode($nid, $cid) {
$quote = NULL;
if (module_exists('quote')) {
extract(_quote_get_quoted_data($nid, $cid));
$quote = "[quote=$author reply=$nid/$cid]" . trim($content) . "[/quote]\n";
$context = array(
'nid' => $nid,
'cid' => $cid,
);
$data = _comment_easy_reply_comment_reply_get_data($nid, $cid);
drupal_alter('comment_easy_reply_quote_input', $quote, $context);
}
return drupal_json_output(array('comment' => array('subject' => $data['subject'], 'body' => $quote)));
}
/**
* Callback for reply ajax inserting.
*
* Render the values to be inserted on reply comment form during ajax reply.
*
* @param int $nid
* The current node nid.
* @param int $cid
* The replied comment cid.
*
* @return string
* The drupal_json_output function output of the values.
*
* @see drupal_json_output()
*/
function _comment_easy_reply_ajax_reply_get_data($nid, $cid) {
$data = _comment_easy_reply_comment_reply_get_data($nid, $cid);
return drupal_json_output(array('comment' => $data));
}
/**
* Fetch values to be inserted on comment reply form.
*
* @param int $nid
* The current node nid.
* @param int $cid
* The replied comment cid.
*
* @return array
* An associative array with the following structure:
* 'subject' - The comment subject configured on module's settings.
* The value should populate the subject field on comment form.
* 'body' - A tag referring to the replied comment.
* The value should populate the body field on comment form.
*/
function _comment_easy_reply_comment_reply_get_data($nid, $cid) {
$data = array();
$comment = comment_load($cid);
$node = node_load($nid);
$data['subject'] = $comment->subject;
$title_settings = _comment_easy_reply_get_settings_value('comment_easy_reply_comment_title', $node->type);
if ($title_settings) {
$data['subject'] = token_replace($title_settings, array('comment' => $comment));
}
$comment_number = _comment_easy_reply_get_comment_number($comment);
$data['body'] = '@#' . $comment_number;
return $data;
}
/**
* Callback for quote filter.
*
* This function is the same of _quote_filter_process function in
* Quote module, but it calls a different callback function for the [quote]
* replacing.
*
* @param string $text
* The string eventually containing the [quote] string to replace.
*
* @return string
* The resulting html output.
*
* @see comment_easy_reply_filter_info_alter()
* @see _quote_filter_process()
*/
function _comment_easy_reply_quote_filter_process($text) {
if (stristr($text, '[quote')) {
// Single regexp with callback allowing for theme calls and quote
// nesting/recursion with regexp code from
// http://www.php.net/manual/en/function.preg-replace-callback.php#85836
$text = preg_replace_callback('#\[(quote.*?)]((?>\[(?!/?quote[^[]*?])|[^[]|(?R))*)\[/quote]#is', '_comment_easy_reply_quote_filter_process_callback', $text);
}
return $text;
}
/**
* Callback for preg_replace_callback in quote filter.
*
* This function is the same of quote_filter_process_callback function in
* Quote module, but it adds a new theming layer that can handle
* arguments in quote bbcode.
*
* @param array $matches
* An array of matched elements in the text string.
*
* @return string
* The resulting html output.
*
* @see quote_filter_process_callback()
* @see preg_replace_callback()
*/
function _comment_easy_reply_quote_filter_process_callback($matches) {
static $index = 0;
$nest = ++$index;
if (!stristr($matches[2], '[quote')) {
$index = 0;
}
$comment = NULL;
$attributes = array();
$parts = explode(' ', $matches[1]);
foreach ($parts as $part) {
$keyvalue = explode('=', $part);
if (count($keyvalue) > 1) {
$attributes[$keyvalue[0]] = $keyvalue[1];
if ($keyvalue[0] == 'reply') {
list($nid, $cid) = explode('/', $keyvalue[1]);
if ($nid) {
$node = node_load($nid);
if ($node && _comment_easy_reply_is_node_type_active($node->type)
&& _comment_easy_reply_get_settings_value('comment_easy_reply_quote_tooltip', $node->type)) {
$comment = comment_load($cid);
$number = _comment_easy_reply_get_comment_number($comment);
if (!empty($comment)) {
$page = _comment_easy_reply_get_comment_page($comment);
$query = drupal_get_query_parameters();
if (!is_null($page)) {
if ($page > 0 || ($page == 0 && isset($_GET['page']) && $page == $_GET['page'])) {
$query['page'] = $page;
}
elseif ($page == 0 && isset($_GET['page']) && $page != $_GET['page'] && isset($query['page'])) {
unset($query['page']);
}
}
$fragment = 'comment-' . $comment->cid;
$node_type = _comment_easy_reply_get_comment_node_type($comment);
if (_comment_easy_reply_get_settings_value('comment_easy_reply_fragment_activated', $node_type)) {
$fragment = _comment_easy_reply_get_settings_value('comment_easy_reply_fragment_text', $node_type);
$fragment = token_replace($fragment, array('comment' => $comment));
}
$attributes['cite'] = url(NULL, array('query' => $query, 'fragment' => $fragment));
}
}
}
}
}
else {
$attributes[$keyvalue[0]] = '';
}
}
if (isset($attributes['quote'])) {
$quote_author = trim($attributes['quote']);
}
else {
$quote_author = trim(drupal_substr($matches[1], 6));
}
$quote_content = _comment_easy_reply_quote_filter_process($matches[2]);
$options = array(
'quote_content' => $quote_content,
'quote_author' => $quote_author,
'nest' => $nest,
'attributes' => $attributes,
'comment' => $comment,
);
$quote_output = _comment_easy_reply_get_tooltip('comment_easy_reply_quote', $options);
return $quote_output;
}
/**
* Callback to check if user can quote on a comment through the ajax call.
*
* @param int $nid
* The current node nid.
* @param int $cid
* The quoted comment cid.
*
* @return bool
* TRUE if user is allowed to quote, FALSE otherwise.
*/
function _comment_easy_reply_quote_ajax_access($nid, $cid) {
if (user_access('post comments')) {
$node = node_load($nid);
if ($node && in_array($node->type, _quote_variable_get('node_types'))
&& $node->comment == COMMENT_NODE_OPEN) {
return TRUE;
}
}
return FALSE;
}
/**
* Callback to check if user can reply to a comment through the ajax call.
*
* @param int $nid
* The current node nid.
* @param int $cid
* The quoted comment cid.
*
* @return bool
* TRUE if user is allowed to reply, FALSE otherwise.
*/
function _comment_easy_reply_ajax_reply_access($nid, $cid) {
if (user_access('post comments')) {
$node = node_load($nid);
if ($node && $node->comment == COMMENT_NODE_OPEN) {
return TRUE;
}
}
return FALSE;
}
/**
* Callback for hook_entity_property_info().
*
* Find all parents of a comment using #NUM tags occurrences in comment body.
*
* @param object $data
* The comment.
* @param array $options
* The callback's options.
* @param string $name
* The caller entity property name.
* @param string $type
* The entity type name.
* @param array $info
* The full entity property tree.
*
* @return array
* The list of comment's parents.
*
* @see hook_entity_property_info()
*/
function _comment_easy_reply_entity_property_get($data, array $options, $name, $type, $info) {
$parents = _comment_easy_reply_comment_get_parents($data, TRUE);
return $parents;
}
/**
* Get all comment's parents.
*
* With Comment Easy Reply module, a comment can have multiple parents using
* tags into its body.
*
* @param object $comment
* The current comment.
* @param bool $use_pid
* If TRUE, the standard parent id, appearing on $comment->pid, will be
* included on comment parents list.
*
* @return array
* The array of parents.
*/
function _comment_easy_reply_comment_get_parents($comment, $use_pid = TRUE) {
static $parents;
$comment_body = '';
if (empty($parents)) {
$parents = array();
$parents[$comment->cid] = array();
}
if (isset($parents[$comment->cid])) {
return $parents[$comment->cid];
}
if (isset($comment->content['comment_body'][0]['#markup'])) {
$comment_body = &$comment->content['comment_body'][0]['#markup'];
}
elseif (isset($comment->comment_body)) {
$comment_body = &$comment->comment_body[$comment->language][0]['value'];
}
if (!empty($comment_body)) {
$comment_nid = $comment->nid;
$matches = _comment_easy_reply_get_matches($comment_body);
if (!empty($matches)) {
foreach ($matches as $num) {
$parent_cid = _comment_easy_reply_get_comment_cid_from_num($num, $comment_nid);
$parent = comment_load($parent_cid);
if (!empty($parent)) {
$parents[$comment->cid][$parent->cid] = $parent;
}
}
}
}
if ($use_pid) {
$parent = comment_load($comment->pid);
if (!empty($parent)) {
$parents[$comment->cid][$parent->cid] = $parent;
}
}
return $parents[$comment->cid];
}
/**
* Callback function for quote theme function on comments.
*
* @param array &$variables
* An associative array with the following structure:
* 'comment' - The comment.
* 'attributes' - The quote bbcode attributes.
*
* @return string
* The quote text.
*
* @see theme_comment_easy_reply_quote()
*/
function _comment_easy_reply_get_quote_text(&$variables) {
$quote_output = theme('quote', $variables);
if (isset($variables['attributes']) && isset($variables['attributes']['reply'])) {
list($nid, $cid) = explode('/', $variables['attributes']['reply']);
if ($nid) {
$node = node_load($nid);
if ($node && _comment_easy_reply_is_node_type_active($node->type)
&& _comment_easy_reply_get_settings_value('comment_easy_reply_quote_tooltip', $node->type)) {
$comment = comment_load($cid);
$number = _comment_easy_reply_get_comment_number($comment);
if (!empty($comment)) {
$variables['comment'] = $comment;
$variables['comment_easy_reply_comment_num'] = $number;
$quote_output = _comment_easy_reply_comment_body_replace($quote_output, $nid, FALSE, $variables);
$variables['comment_easy_reply_quote_processed'] = TRUE;
}
}
}
}
return $quote_output;
}
/**
* Disable module's native js tooltips.
*
* @return sring
* The current array of JavaScript files, settings, and in-line code.
*/
function _comment_easy_reply_disable_native_js() {
$variables = array('comment_easy_reply_js_override' => array());
$variables['comment_easy_reply_js_override']['tooltip_native_enabled'] = FALSE;
return comment_easy_reply_add_js($variables);
}
/**
* Add module's native js tooltips.
*/
function comment_easy_reply_add_native_tooltip_js() {
static $added;
if (!$added) {
drupal_add_js(drupal_get_path('module', 'comment_easy_reply') . '/js/comment_easy_reply-tooltip.js');
}
}
/**
* The hook_comment_easy_reply_settings() report status value callback.
*
* @param string $name
* The setting name.
* @param string $value
* The setting value to be parsed.
*
* @return mixed
* The return parsed value.
*/
function _comment_easy_reply_status_report_callback($name, $value = '') {
switch ($name) {
case 'comment_easy_reply_reply_from_numlink':
case 'comment_easy_reply_replytip_activated':
case 'comment_easy_reply_fragment_activated':
case 'comment_easy_reply_quote_tooltip':
if ($value) {
$value = t('enabled');
}
else {
$value = t('disabled');
}
break;
case 'comment_easy_reply_filter':
$filter_info = filter_formats();
$return = t('None');
foreach ($filter_info as $name => $filter) {
if ($name == $value) {
$return = $filter->name;
break;
}
}
$value = $return;
break;
case 'comment_easy_reply_tooltip_name':
$tooltips = _comment_easy_reply_get_tooltips();
foreach ($tooltips as $type => $tooltip) {
if ($type == $value) {
$value = $tooltip['name'];
break;
}
}
break;
}
return $value;
}