View source
<?php
function quote_help($section) {
switch ($section) {
case 'admin/settings/quote':
return t('<p>The quote filter allows users to quote other posts in their
comments. Besides the following settings, the quote filter will need to be
enabled for each <a href="!input-format">input format</a> (as required). Please
make sure that the quote filter is arranged <em>after</em> any HTML filters and
<em>before</em> the line break filter. For more information, please visit the
<a href="!project-page">project page</a>.</p>', array(
'!input-format' => url('admin/settings/filters'),
'!project-page' => url('http://drupal.org/project/quote', NULL, NULL, TRUE),
));
}
}
function quote_menu($may_cache) {
$items = array();
if ($may_cache) {
$items[] = array(
'path' => 'admin/settings/quote',
'title' => t('Quote'),
'description' => t('Configure the behaviour of the quote module.'),
'callback' => 'drupal_get_form',
'callback arguments' => 'quote_settings_form',
'access' => user_access('administer filters'),
);
}
else {
drupal_add_css(drupal_get_path('module', 'quote') . '/quote.css');
}
return $items;
}
function quote_link($type, $post = NULL, $teaser = FALSE) {
$links = array();
if (user_access('post comments')) {
$link = array(
'title' => t('Quote'),
'attributes' => array(
'title' => t('Quote this post in your reply.'),
),
'query' => 'quote=1',
'fragment' => 'comment-form',
);
if ($type == 'comment') {
$node = node_load($post->nid);
if (in_array($node->type, _quote_variable_get('node_types')) && $node->comment == COMMENT_NODE_READ_WRITE) {
$link['href'] = "comment/reply/{$post->nid}/{$post->cid}";
$link['title'] = t('quote');
$links['quote'] = $link;
}
}
elseif ($type == 'node' && in_array($post->type, _quote_variable_get('node_types')) && $post->comment == COMMENT_NODE_READ_WRITE && _quote_variable_get('node_link_display')) {
$link['href'] = "comment/reply/{$post->nid}";
$links['quote'] = $link;
}
}
return $links;
}
function quote_form_alter($form_id, &$form) {
if ($form_id == 'comment_form' && isset($_GET['quote']) && $_GET['quote']) {
$nid = arg(2);
$cid = arg(3);
if ($cid) {
$comment = db_fetch_object(db_query('SELECT c.*, u.uid, u.name AS registered_name FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.cid = %d AND c.status = 0', $cid));
if ($comment->uid) {
$author = $comment->registered_name;
}
else {
$author = !empty($comment->name) ? $comment->name : variable_get('anonymous', 'Anonymous');
}
$quote = $comment->comment;
$subject = $comment->subject;
}
elseif ($nid && _quote_variable_get('node_link_display')) {
$node = node_load(array(
'nid' => $nid,
));
if (in_array($node->type, _quote_variable_get('node_types'))) {
$quote = $node->body;
$author = !empty($node->name) ? $node->name : variable_get('anonymous', 'Anonymous');
}
else {
return;
}
}
else {
return;
}
$form['comment_filter']['comment']['#default_value'] = '[quote=' . $author . ']' . trim($quote) . "[/quote]\n" . $form['comment_filter']['comment']['#default_value'];
if (_quote_variable_get('subject_required')) {
$form['subject']['#required'] = TRUE;
}
}
}
function quote_filter($op, $delta = 0, $format = -1, $text = '') {
switch ($op) {
case 'list':
return array(
0 => t('Quote filter'),
);
case 'description':
return t('Converts [quote] tags into <div> tags. Must usually apply after HTML filters unless an exception is made for <div> tags.');
case 'process':
return _quote_filter_process($text);
default:
return $text;
}
}
function quote_filter_tips($delta, $format, $long = FALSE) {
if ($long) {
$simple_quote = '[quote]This is a simple quote.[/quote]';
$attributed_quote = '[quote=Mr. Drupal]This is a quote with an attribution line.[/quote]';
$nested_quote = '[quote]I think she says it best...
[quote=Ms. Quotation]This is a quote nested within another quote.[/quote]
but you can\'t argue with
[quote=Ms. Reply]The more quotes, the merrier.
Just don\'t get too carried away.[/quote]
And I have nothing more to say.[/quote]';
return t('<p>Quoted content can be placed between [quote] tags in order to
be displayed as an indented quote. Every [quote] tag <em>must</em> have a
corresponding [/quote] tag. For example:
<pre>!simple-quote</pre> is displayed as:</p>
!simple-quote-processed
<p>Additionally, there is an optional attribute which allows quotes to
specify the original author.
<pre>!attributed-quote</pre> is displayed as:</p>
!attributed-quote-processed
<p>Finally, multiple [quote] tags can be nested within one another. Just
remember that every [quote] tag <strong>must</strong> have a corresponding
[/quote] tag.
<pre>!nested-quote</pre> is displayed as:</p>
!nested-quote-processed', array(
'!simple-quote' => $simple_quote,
'!simple-quote-processed' => _quote_filter_process($simple_quote),
'!attributed-quote' => $attributed_quote,
'!attributed-quote-processed' => _quote_filter_process($attributed_quote),
'!nested-quote' => $nested_quote,
'!nested-quote-processed' => _quote_filter_process($nested_quote),
));
}
else {
return t('You may quote other posts using [quote] tags.');
}
}
function quote_settings_form() {
$form['quote'] = array(
'#type' => 'fieldset',
'#title' => t('Quote module settings'),
'#tree' => TRUE,
);
$form['quote']['node_types'] = array(
'#type' => 'checkboxes',
'#title' => t('Node associations'),
'#description' => t('Select the node types to associate with the quote filter.'),
'#options' => _quote_get_node_types(),
'#default_value' => _quote_variable_get('node_types'),
);
$form['quote']['node_link_display'] = array(
'#type' => 'checkbox',
'#title' => t('Display the quote link for nodes'),
'#description' => t('Leave this option disabled if you use a PHP or similar filter in your nodes. The quote link is always displayed for comments.'),
'#default_value' => _quote_variable_get('node_link_display'),
);
$form['quote']['subject_required'] = array(
'#type' => 'checkbox',
'#title' => t('Make the comment subject field a required field'),
'#description' => t('Making the comment subject field a required field will ensure that unsightly [quote] tags are not displayed.'),
'#default_value' => _quote_variable_get('subject_required'),
);
return system_settings_form($form);
}
function quote_settings_form_validate($form_id, $form_values, $form) {
form_set_value($form['quote']['node_types'], array_filter($form_values['quote']['node_types']));
}
function _quote_variable_get($name = NULL) {
static $variables = array();
if (empty($variables)) {
$defaults = array(
'node_types' => array(
'blog',
'story',
),
'node_link_display' => 1,
'subject_required' => 1,
);
$variables = variable_get('quote', array());
$variables = array_merge($defaults, $variables);
}
return $name ? $variables[$name] : $variables;
}
function _quote_get_node_types($keys = FALSE) {
$node_types = node_get_types();
if (!$keys) {
foreach ($node_types as $type => $object) {
$node_types[$type] = $object->name;
}
return $node_types;
}
return array_keys($node_types);
}
function _quote_filter_process($text) {
if (stristr($text, '[quote')) {
$text = preg_replace_callback('#\\[(quote.*?)]((?>\\[(?!/?quote[^[]*?])|[^[]|(?R))*)\\[/quote]#is', '_quote_filter_process_callback', $text);
}
return $text;
}
function _quote_filter_process_callback($matches) {
$quote_author = trim(substr($matches[1], 6));
$quote_content = _quote_filter_process($matches[2]);
$quote_output = theme('quote', $quote_content, $quote_author);
return $quote_output;
}
function theme_quote($quote_content, $quote_author) {
$quote_output = '<div class="quote-msg">';
if ($quote_author != '') {
$quote_output .= '<div class="quote-author">' . t('%name wrote:', array(
'%name' => $quote_author,
)) . '</div>';
}
else {
$quote_output .= '<div class="quote-author">' . t('Quote:') . '</div>';
}
$quote_output .= $quote_content;
$quote_output .= '</div>';
return $quote_output;
}