| 1 |
<?php
|
| 2 |
// $Id: quote.module,v 1.29.2.8 2007/08/11 19:51:20 karthik Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Implementation of hook_help().
|
| 6 |
*/
|
| 7 |
function quote_help($section) {
|
| 8 |
switch ($section) {
|
| 9 |
case 'admin/settings/quote':
|
| 10 |
return t('<p>The quote filter allows users to quote other posts in their
|
| 11 |
comments. Besides the following settings, the quote filter will need to be
|
| 12 |
enabled for each <a href="!input-format">input format</a> (as required). Please
|
| 13 |
make sure that the quote filter is arranged <em>after</em> any HTML filters and
|
| 14 |
<em>before</em> the line break filter. For more information, please visit the
|
| 15 |
<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)));
|
| 16 |
}
|
| 17 |
}
|
| 18 |
|
| 19 |
/**
|
| 20 |
* Implementation of hook_menu().
|
| 21 |
*/
|
| 22 |
function quote_menu($may_cache) {
|
| 23 |
$items = array();
|
| 24 |
|
| 25 |
if ($may_cache) {
|
| 26 |
$items[] = array(
|
| 27 |
'path' => 'admin/settings/quote',
|
| 28 |
'title' => t('Quote'),
|
| 29 |
'callback' => 'drupal_get_form',
|
| 30 |
'callback arguments' => 'quote_settings_form',
|
| 31 |
'access' => user_access('administer filters')
|
| 32 |
);
|
| 33 |
}
|
| 34 |
else {
|
| 35 |
// Reference quote.css, if it exists.
|
| 36 |
drupal_add_css(drupal_get_path('module', 'quote') .'/quote.css');
|
| 37 |
}
|
| 38 |
|
| 39 |
return $items;
|
| 40 |
}
|
| 41 |
|
| 42 |
/**
|
| 43 |
* Implementation of hook_link().
|
| 44 |
*/
|
| 45 |
function quote_link($type, $post = 0, $main = 0) {
|
| 46 |
$links = array();
|
| 47 |
|
| 48 |
if (user_access('post comments')) {
|
| 49 |
$link = array(
|
| 50 |
'title' => t('quote'),
|
| 51 |
'attributes' => array('title' => t('Quote this post in your reply.')),
|
| 52 |
'query' => 'quote=1',
|
| 53 |
'fragment' => 'comment-form'
|
| 54 |
);
|
| 55 |
// $post can be either a node or a comment.
|
| 56 |
if ($type == 'comment') {
|
| 57 |
// Display quote link for comments only if the parent node is accepting
|
| 58 |
// comments and has the quote filter enabled.
|
| 59 |
$node = node_load(arg(1));
|
| 60 |
if (in_array($node->type, _quote_variable_get('node_types')) && $node->comment == COMMENT_NODE_READ_WRITE) {
|
| 61 |
$link['href'] = "comment/reply/$post->nid/$post->cid";
|
| 62 |
$links['quote'] = $link;
|
| 63 |
}
|
| 64 |
}
|
| 65 |
elseif ($type == 'node' && in_array($post->type, _quote_variable_get('node_types')) && $post->comment == COMMENT_NODE_READ_WRITE && _quote_variable_get('node_link_display')) {
|
| 66 |
// Display quote link for nodes only if the node is accepting comments,
|
| 67 |
// has the quote filter enabled and has quote_link_display set.
|
| 68 |
$link['href'] = "comment/reply/$post->nid";
|
| 69 |
$links['quote'] = $link;
|
| 70 |
}
|
| 71 |
}
|
| 72 |
|
| 73 |
return $links;
|
| 74 |
}
|
| 75 |
|
| 76 |
/**
|
| 77 |
* Implementation of hook_form_alter().
|
| 78 |
*/
|
| 79 |
function quote_form_alter($form_id, &$form) {
|
| 80 |
if ($form_id == 'comment_form' && isset($_GET['quote']) && $_GET['quote']) {
|
| 81 |
$nid = arg(2);
|
| 82 |
$cid = arg(3);
|
| 83 |
|
| 84 |
if ($cid) {
|
| 85 |
$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));
|
| 86 |
|
| 87 |
if ($comment->uid) {
|
| 88 |
$author = $comment->registered_name;
|
| 89 |
}
|
| 90 |
else {
|
| 91 |
$author = (!empty($comment->name)) ? $comment->name : variable_get('anonymous', 'Anonymous');
|
| 92 |
}
|
| 93 |
$quote = $comment->comment;
|
| 94 |
$subject = $comment->subject;
|
| 95 |
}
|
| 96 |
elseif ($nid && _quote_variable_get('node_link_display')) {
|
| 97 |
$node = node_load(array('nid' => $nid));
|
| 98 |
if (in_array($node->type, _quote_variable_get('node_types'))) {
|
| 99 |
$quote = $node->body;
|
| 100 |
$author = !empty($node->name) ? $node->name : variable_get('anonymous', 'Anonymous');
|
| 101 |
}
|
| 102 |
else {
|
| 103 |
return;
|
| 104 |
}
|
| 105 |
}
|
| 106 |
else {
|
| 107 |
return;
|
| 108 |
}
|
| 109 |
// Add quoted text and preserve existing content (signature etc.).
|
| 110 |
$form['comment_filter']['comment']['#default_value'] = '[quote='. $author .']'. $quote ."[/quote]\n". $form['comment_filter']['comment']['#default_value'];
|
| 111 |
if (_quote_variable_get('subject_required')) {
|
| 112 |
$form['subject']['#required'] = TRUE;
|
| 113 |
}
|
| 114 |
}
|
| 115 |
}
|
| 116 |
|
| 117 |
/**
|
| 118 |
* Implementation of hook_filter().
|
| 119 |
*/
|
| 120 |
function quote_filter($op, $delta = 0, $format = -1, $text = '') {
|
| 121 |
switch ($op) {
|
| 122 |
case 'list':
|
| 123 |
return array(0 => t('Quote filter'));
|
| 124 |
case 'description':
|
| 125 |
return t('Converts [quote] tags into <div> tags. Must apply after HTML filters.');
|
| 126 |
case 'process':
|
| 127 |
return _quote_filter_process($text);
|
| 128 |
default:
|
| 129 |
return $text;
|
| 130 |
}
|
| 131 |
}
|
| 132 |
|
| 133 |
/**
|
| 134 |
* Implementation of hook_filter_tips().
|
| 135 |
*/
|
| 136 |
function quote_filter_tips($delta, $format, $long = FALSE) {
|
| 137 |
if ($long) {
|
| 138 |
return t('
|
| 139 |
<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:
|
| 140 |
<pre>[quote]This is a simple quote.[/quote]</pre> is displayed as:</p>
|
| 141 |
<div class="quote-msg"><div class="quote-author">Quote:</div>This is a simple quote.</div>
|
| 142 |
<p>Additionally, there is an optional attribute which allows quotes to specify the original author.
|
| 143 |
<pre>[quote=Mr. Drupal]This is a quote with an attribution line.[/quote]
|
| 144 |
</pre> is displayed as:</p>
|
| 145 |
<div class="quote-msg"><div class="quote-author">Mr. Drupal wrote:</div>This is a quote with an attribution line.</div>
|
| 146 |
<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.
|
| 147 |
<pre>
|
| 148 |
[quote]I think she says it best...
|
| 149 |
[quote=Ms. Quotation]This is a quote nested within another quote.[/quote]
|
| 150 |
but you can\'t argue with
|
| 151 |
[quote=Ms. Reply]The more quotes, the merrier.
|
| 152 |
Just don\'t get too carried away.[/quote]
|
| 153 |
And I have nothing more to say.[/quote]</pre> is displayed as:</p>
|
| 154 |
<div class="quote-msg"><div class="quote-author">Quote:</div>I think she says it best...
|
| 155 |
<div class="quote-msg"><div class="quote-author">Ms. Quotation wrote:</div>This is a quote nested within another quote.</div>
|
| 156 |
but you can\'t argue with
|
| 157 |
<div class="quote-msg"><div class="quote-author">Ms. Reply wrote:</div>The more quotes, the merrier. Just don\'t get too carried away.</div>
|
| 158 |
And I have nothing more to say.</div>
|
| 159 |
');
|
| 160 |
}
|
| 161 |
else {
|
| 162 |
return t('You may quote other posts using [quote] tags.');
|
| 163 |
}
|
| 164 |
}
|
| 165 |
|
| 166 |
/**
|
| 167 |
* Menu callback: quote module settings form.
|
| 168 |
*/
|
| 169 |
function quote_settings_form() {
|
| 170 |
$form['quote'] = array(
|
| 171 |
'#type' => 'fieldset',
|
| 172 |
'#title' => 'Quote module settings',
|
| 173 |
'#tree' => TRUE
|
| 174 |
);
|
| 175 |
$form['quote']['node_types'] = array(
|
| 176 |
'#type' => 'checkboxes',
|
| 177 |
'#title' => t('Node associations'),
|
| 178 |
'#description' => t('Select the node types to associate with the quote filter.'),
|
| 179 |
'#options' => _quote_get_node_types(),
|
| 180 |
'#default_value' => _quote_variable_get('node_types')
|
| 181 |
);
|
| 182 |
$form['quote']['node_link_display'] = array(
|
| 183 |
'#type' => 'checkbox',
|
| 184 |
'#title' => t('Display the quote link for nodes'),
|
| 185 |
'#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.'),
|
| 186 |
'#default_value' => _quote_variable_get('node_link_display')
|
| 187 |
);
|
| 188 |
$form['quote']['subject_required'] = array(
|
| 189 |
'#type' => 'checkbox',
|
| 190 |
'#title' => t('Make the comment subject field a required field'),
|
| 191 |
'#description' => t('Making the comment subject field a required field will ensure that unsightly [quote] tags are not displayed.'),
|
| 192 |
'#default_value' => _quote_variable_get('subject_required')
|
| 193 |
);
|
| 194 |
|
| 195 |
return system_settings_form($form);
|
| 196 |
}
|
| 197 |
|
| 198 |
/**
|
| 199 |
* Validate quote settings form submission.
|
| 200 |
*/
|
| 201 |
function quote_settings_form_validate($form_id, $form_values, $form) {
|
| 202 |
// Run the node type checkboxes through array_filter to strip unselected
|
| 203 |
// items.
|
| 204 |
form_set_value($form['quote']['node_types'], array_filter($form_values['quote']['node_types']));
|
| 205 |
}
|
| 206 |
|
| 207 |
/**
|
| 208 |
* Return a quote module variable.
|
| 209 |
*
|
| 210 |
* @param $name
|
| 211 |
* The name of the variable to retrieve.
|
| 212 |
* @return
|
| 213 |
* The value of the variable requested.
|
| 214 |
*/
|
| 215 |
function _quote_variable_get($name = NULL) {
|
| 216 |
static $variables = array();
|
| 217 |
|
| 218 |
if (empty($variables)) {
|
| 219 |
$defaults = array(
|
| 220 |
'node_types' => array('blog', 'story'),
|
| 221 |
'node_link_display' => 1,
|
| 222 |
'subject_required' => 1
|
| 223 |
);
|
| 224 |
$variables = variable_get('quote', array());
|
| 225 |
$variables = array_merge($defaults, $variables);
|
| 226 |
}
|
| 227 |
|
| 228 |
return $name ? $variables[$name] : $variables;
|
| 229 |
}
|
| 230 |
|
| 231 |
/**
|
| 232 |
* Replace [quote] tags with markup for display.
|
| 233 |
*
|
| 234 |
* @param $text
|
| 235 |
* The text with the [quote] tags that need to be replaced with HTML tags.
|
| 236 |
*
|
| 237 |
* @return $text
|
| 238 |
* Filtered text.
|
| 239 |
*
|
| 240 |
* @todo Fix this with one preg_replace.
|
| 241 |
*/
|
| 242 |
function _quote_filter_process($text) {
|
| 243 |
// Thanks: function based on code from punbb.org
|
| 244 |
if (strstr($text, '[quote')) {
|
| 245 |
$pre = '<div class="quote-msg"><div class="quote-author">';
|
| 246 |
$post = '</div>';
|
| 247 |
$markup = $pre . t('Quote:') . $post;
|
| 248 |
$text = str_replace(array('[quote]', '[quote=]', '[/quote]'), array($markup, $markup, $post), $text);
|
| 249 |
$text = preg_replace('#\[quote=(?:"|\')?(.*?)["\']?(?:"|\')?\]#s', $pre . t('%name wrote:', array('%name' => '\\1')) . $post, $text);
|
| 250 |
}
|
| 251 |
return $text;
|
| 252 |
}
|
| 253 |
|
| 254 |
/**
|
| 255 |
* Helper function that returns node types.
|
| 256 |
*/
|
| 257 |
function _quote_get_node_types($keys = FALSE) {
|
| 258 |
$node_types = node_get_types();
|
| 259 |
if (!$keys) {
|
| 260 |
foreach ($node_types as $type => $object) {
|
| 261 |
$node_types[$type] = $object->name;
|
| 262 |
}
|
| 263 |
|
| 264 |
return $node_types;
|
| 265 |
}
|
| 266 |
|
| 267 |
return array_keys($node_types);
|
| 268 |
}
|