comment.module

You are here

  1. 7 modules/comment/comment.module
  2. 4.6 modules/comment.module
  3. 4.7 modules/comment.module
  4. 5 modules/comment/comment.module
  5. 6 modules/comment/comment.module
  6. 8 core/modules/comment/comment.module

Enables users to comment on published content.

When enabled, the Drupal comment module creates a discussion board for each Drupal node. Users can post comments to discuss a forum topic, weblog post, story, collaborative book page, etc.

Functions

Namesort descending Description
comment_access This is *not* a hook_access() implementation. This function is called to determine whether the current user has access to a particular comment.
comment_action_info Implementation of hook_action_info().
comment_block Implementation of hook_block().
comment_controls Build a comment control form.
comment_controls_submit Process comment_controls form submissions.
comment_form Generate the basic commenting form, for appending to a node or display on a separate page.
comment_form_add_preview Form builder; Generate and validate a comment preview form.
comment_form_alter Implementation of hook_form_alter().
comment_form_box Theme the comment form box.
comment_form_submit Process comment form submissions; prepare the comment, store it, and set a redirection target.
comment_form_validate Validate comment form submissions.
comment_get_recent Find a number of recent comments. This is done in two steps. 1. Find the n (specified by $number) nodes that have the most recent comments. This is done by querying node_comment_statistics which has an index on last_comment_timestamp, and is thus a…
comment_help Implementation of hook_help().
comment_hook_info Implementation of hook_hook_info().
comment_invoke_comment Invoke a hook_comment() operation in all modules.
comment_link Implementation of hook_link().
comment_links Build command links for a comment (e.g.\ edit, reply, delete) with respect to the current user's access permissions.
comment_menu Implementation of hook_menu().
comment_new_page_count Calculate page number for first new comment.
comment_nodeapi Implementation of hook_nodeapi().
comment_node_type Implementation of hook_node_type().
comment_node_url A simple helper function.
comment_num_all Get comment count for a node.
comment_num_new Get number of new comments for current user and specified node.
comment_num_replies Get replies count for a comment.
comment_operations Comment operations. We offer different update operations depending on which comment administration page we're on.
comment_perm Implementation of hook_perm().
comment_publish_action Action to publish a comment.
comment_render Renders comment(s).
comment_save Accepts a submission of new or changed comment content.
comment_theme Implementation of hook_theme().
comment_unpublish_action Action to unpublish a comment.
comment_unpublish_by_keyword_action Action to unpublish a comment if it contains a certain string.
comment_unpublish_by_keyword_action_form Form builder; Prepare a form for blacklisted keywords.
comment_unpublish_by_keyword_action_submit Process comment_unpublish_by_keyword_action_form form submissions.
comment_user Implementation of hook_user().
comment_validate Validate comment data.
int2vancode Generate vancode.
template_preprocess_comment Process variables for comment.tpl.php.
template_preprocess_comment_folded Process variables for comment-folded.tpl.php.
template_preprocess_comment_wrapper Process variables for comment-wrapper.tpl.php.
theme_comment_block Returns a formatted list of recent comments to be displayed in the comment block.
theme_comment_controls Theme comment controls box where the user can change the default display mode and display order of comments.
theme_comment_flat_collapsed Theme comment flat collapsed view.
theme_comment_flat_expanded Theme comment flat expanded view.
theme_comment_post_forbidden Theme a "you can't post comments" notice.
theme_comment_submitted Theme a "Submitted by ..." notice.
theme_comment_thread_collapsed Theme comment thread collapsed view.
theme_comment_thread_expanded Theme comment thread expanded view.
theme_comment_view Themes a single comment and related items.
vancode2int Decode vancode back to an integer.
_comment_form_submit Prepare a comment for submission.
_comment_get_display_setting Return a current comment display setting
_comment_get_modes Return an array of viewing modes for comment listings.
_comment_get_orders Return an array of viewing orders for comment listings.
_comment_load Load the entire comment by cid.
_comment_per_page Return an array of "comments per page" settings from which the user can choose.
_comment_update_node_statistics Updates the comment statistics for a given node. This should be called any time a comment is added, deleted, or updated.

Constants

Namesort descending Description
COMMENT_ANONYMOUS_MAYNOT_CONTACT Anonymous posters may not enter their contact information.
COMMENT_ANONYMOUS_MAY_CONTACT Anonymous posters may leave their contact information.
COMMENT_ANONYMOUS_MUST_CONTACT Anonymous posters must leave their contact information.
COMMENT_CONTROLS_ABOVE Comment controls should be shown above the comment list.
COMMENT_CONTROLS_ABOVE_BELOW Comment controls should be shown both above and below the comment list.
COMMENT_CONTROLS_BELOW Comment controls should be shown below the comment list.
COMMENT_CONTROLS_HIDDEN Comment controls are hidden.
COMMENT_FORM_BELOW Comment form should be shown below post or list of comments.
COMMENT_FORM_SEPARATE_PAGE Comment form should be displayed on a separate page.
COMMENT_MODE_FLAT_COLLAPSED Comments are displayed in a flat list - collapsed.
COMMENT_MODE_FLAT_EXPANDED Comments are displayed in a flat list - expanded.
COMMENT_MODE_THREADED_COLLAPSED Comments are displayed as a threaded list - collapsed.
COMMENT_MODE_THREADED_EXPANDED Comments are displayed as a threaded list - expanded.
COMMENT_NODE_DISABLED Comments for this node are disabled.
COMMENT_NODE_READ_ONLY Comments for this node are locked.
COMMENT_NODE_READ_WRITE Comments are enabled on this node.
COMMENT_NOT_PUBLISHED Comment is awaiting approval.
COMMENT_ORDER_NEWEST_FIRST Comments are ordered by date - newest first.
COMMENT_ORDER_OLDEST_FIRST Comments are ordered by date - oldest first.
COMMENT_PREVIEW_OPTIONAL Comment preview is optional.
COMMENT_PREVIEW_REQUIRED Comment preview is required.
COMMENT_PUBLISHED Comment is published.

File

modules/comment/comment.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * Enables users to comment on published content.
  5. *
  6. * When enabled, the Drupal comment module creates a discussion
  7. * board for each Drupal node. Users can post comments to discuss
  8. * a forum topic, weblog post, story, collaborative book page, etc.
  9. */
  10. /**
  11. * Comment is published.
  12. */
  13. define('COMMENT_PUBLISHED', 0);
  14. /**
  15. * Comment is awaiting approval.
  16. */
  17. define('COMMENT_NOT_PUBLISHED', 1);
  18. /**
  19. * Comments are displayed in a flat list - collapsed.
  20. */
  21. define('COMMENT_MODE_FLAT_COLLAPSED', 1);
  22. /**
  23. * Comments are displayed in a flat list - expanded.
  24. */
  25. define('COMMENT_MODE_FLAT_EXPANDED', 2);
  26. /**
  27. * Comments are displayed as a threaded list - collapsed.
  28. */
  29. define('COMMENT_MODE_THREADED_COLLAPSED', 3);
  30. /**
  31. * Comments are displayed as a threaded list - expanded.
  32. */
  33. define('COMMENT_MODE_THREADED_EXPANDED', 4);
  34. /**
  35. * Comments are ordered by date - newest first.
  36. */
  37. define('COMMENT_ORDER_NEWEST_FIRST', 1);
  38. /**
  39. * Comments are ordered by date - oldest first.
  40. */
  41. define('COMMENT_ORDER_OLDEST_FIRST', 2);
  42. /**
  43. * Comment controls should be shown above the comment list.
  44. */
  45. define('COMMENT_CONTROLS_ABOVE', 0);
  46. /**
  47. * Comment controls should be shown below the comment list.
  48. */
  49. define('COMMENT_CONTROLS_BELOW', 1);
  50. /**
  51. * Comment controls should be shown both above and below the comment list.
  52. */
  53. define('COMMENT_CONTROLS_ABOVE_BELOW', 2);
  54. /**
  55. * Comment controls are hidden.
  56. */
  57. define('COMMENT_CONTROLS_HIDDEN', 3);
  58. /**
  59. * Anonymous posters may not enter their contact information.
  60. */
  61. define('COMMENT_ANONYMOUS_MAYNOT_CONTACT', 0);
  62. /**
  63. * Anonymous posters may leave their contact information.
  64. */
  65. define('COMMENT_ANONYMOUS_MAY_CONTACT', 1);
  66. /**
  67. * Anonymous posters must leave their contact information.
  68. */
  69. define('COMMENT_ANONYMOUS_MUST_CONTACT', 2);
  70. /**
  71. * Comment form should be displayed on a separate page.
  72. */
  73. define('COMMENT_FORM_SEPARATE_PAGE', 0);
  74. /**
  75. * Comment form should be shown below post or list of comments.
  76. */
  77. define('COMMENT_FORM_BELOW', 1);
  78. /**
  79. * Comments for this node are disabled.
  80. */
  81. define('COMMENT_NODE_DISABLED', 0);
  82. /**
  83. * Comments for this node are locked.
  84. */
  85. define('COMMENT_NODE_READ_ONLY', 1);
  86. /**
  87. * Comments are enabled on this node.
  88. */
  89. define('COMMENT_NODE_READ_WRITE', 2);
  90. /**
  91. * Comment preview is optional.
  92. */
  93. define('COMMENT_PREVIEW_OPTIONAL', 0);
  94. /**
  95. * Comment preview is required.
  96. */
  97. define('COMMENT_PREVIEW_REQUIRED', 1);
  98. /**
  99. * Implementation of hook_help().
  100. */
  101. function comment_help($path, $arg) {
  102. switch ($path) {
  103. case 'admin/help#comment':
  104. $output = '<p>'. t('The comment module allows visitors to comment on your posts, creating ad hoc discussion boards. Any <a href="@content-type">content type</a> may have its <em>Default comment setting</em> set to <em>Read/Write</em> to allow comments, or <em>Disabled</em>, to prevent comments. Comment display settings and other controls may also be customized for each content type (some display settings are customizable by individual users).', array('@content-type' => url('admin/content/types'))) .'</p>';
  105. $output .= '<p>'. t('Comment permissions are assigned to user roles, and are used to determine whether anonymous users (or other roles) are allowed to comment on posts. If anonymous users are allowed to comment, their individual contact information may be retained in cookies stored on their local computer for use in later comment submissions. When a comment has no replies, it may be (optionally) edited by its author. The comment module uses the same input formats and HTML tags available when creating other forms of content.') .'</p>';
  106. $output .= '<p>'. t('For more information, see the online handbook entry for <a href="@comment">Comment module</a>.', array('@comment' => 'http://drupal.org/handbook/modules/comment/')) .'</p>';
  107. return $output;
  108. case 'admin/content/comment':
  109. return '<p>'. t("Below is a list of the latest comments posted to your site. Click on a subject to see the comment, the author's name to edit the author's user information, 'edit' to modify the text, and 'delete' to remove their submission.") .'</p>';
  110. case 'admin/content/comment/approval':
  111. return '<p>'. t("Below is a list of the comments posted to your site that need approval. To approve a comment, click on 'edit' and then change its 'moderation status' to Approved. Click on a subject to see the comment, the author's name to edit the author's user information, 'edit' to modify the text, and 'delete' to remove their submission.") .'</p>';
  112. }
  113. }
  114. /**
  115. * Implementation of hook_theme().
  116. */
  117. function comment_theme() {
  118. return array(
  119. 'comment_block' => array(
  120. 'arguments' => array(),
  121. ),
  122. 'comment_admin_overview' => array(
  123. 'arguments' => array('form' => NULL),
  124. ),
  125. 'comment_preview' => array(
  126. 'arguments' => array('comment' => NULL, 'node' => NULL, 'links' => array(), 'visible' => 1),
  127. ),
  128. 'comment_view' => array(
  129. 'arguments' => array('comment' => NULL, 'node' => NULL, 'links' => array(), 'visible' => 1),
  130. ),
  131. 'comment_controls' => array(
  132. 'arguments' => array('form' => NULL),
  133. ),
  134. 'comment' => array(
  135. 'template' => 'comment',
  136. 'arguments' => array('comment' => NULL, 'node' => NULL, 'links' => array()),
  137. ),
  138. 'comment_folded' => array(
  139. 'template' => 'comment-folded',
  140. 'arguments' => array('comment' => NULL),
  141. ),
  142. 'comment_flat_collapsed' => array(
  143. 'arguments' => array('comment' => NULL, 'node' => NULL),
  144. ),
  145. 'comment_flat_expanded' => array(
  146. 'arguments' => array('comment' => NULL, 'node' => NULL),
  147. ),
  148. 'comment_thread_collapsed' => array(
  149. 'arguments' => array('comment' => NULL, 'node' => NULL),
  150. ),
  151. 'comment_thread_expanded' => array(
  152. 'arguments' => array('comment' => NULL, 'node' => NULL),
  153. ),
  154. 'comment_post_forbidden' => array(
  155. 'arguments' => array('nid' => NULL),
  156. ),
  157. 'comment_wrapper' => array(
  158. 'template' => 'comment-wrapper',
  159. 'arguments' => array('content' => NULL, 'node' => NULL),
  160. ),
  161. 'comment_submitted' => array(
  162. 'arguments' => array('comment' => NULL),
  163. ),
  164. );
  165. }
  166. /**
  167. * Implementation of hook_menu().
  168. */
  169. function comment_menu() {
  170. $items['admin/content/comment'] = array(
  171. 'title' => 'Comments',
  172. 'description' => 'List and edit site comments and the comment moderation queue.',
  173. 'page callback' => 'comment_admin',
  174. 'access arguments' => array('administer comments'),
  175. 'file' => 'comment.admin.inc',
  176. );
  177. // Tabs:
  178. $items['admin/content/comment/new'] = array(
  179. 'title' => 'Published comments',
  180. 'type' => MENU_DEFAULT_LOCAL_TASK,
  181. 'weight' => -10,
  182. );
  183. $items['admin/content/comment/approval'] = array(
  184. 'title' => 'Approval queue',
  185. 'page arguments' => array('approval'),
  186. 'access arguments' => array('administer comments'),
  187. 'type' => MENU_LOCAL_TASK,
  188. 'file' => 'comment.admin.inc',
  189. );
  190. $items['comment/delete'] = array(
  191. 'title' => 'Delete comment',
  192. 'page callback' => 'comment_delete',
  193. 'access arguments' => array('administer comments'),
  194. 'type' => MENU_CALLBACK,
  195. 'file' => 'comment.admin.inc',
  196. );
  197. $items['comment/edit'] = array(
  198. 'title' => 'Edit comment',
  199. 'page callback' => 'comment_edit',
  200. 'access arguments' => array('post comments'),
  201. 'type' => MENU_CALLBACK,
  202. 'file' => 'comment.pages.inc',
  203. );
  204. $items['comment/reply/%node'] = array(
  205. 'title' => 'Reply to comment',
  206. 'page callback' => 'comment_reply',
  207. 'page arguments' => array(2),
  208. 'access callback' => 'node_access',
  209. 'access arguments' => array('view', 2),
  210. 'type' => MENU_CALLBACK,
  211. 'file' => 'comment.pages.inc',
  212. );
  213. return $items;
  214. }
  215. /**
  216. * Implementation of hook_node_type().
  217. */
  218. function comment_node_type($op, $info) {
  219. $settings = array(
  220. 'comment',
  221. 'comment_default_mode',
  222. 'comment_default_order',
  223. 'comment_default_per_page',
  224. 'comment_controls',
  225. 'comment_anonymous',
  226. 'comment_subject_field',
  227. 'comment_preview',
  228. 'comment_form_location',
  229. );
  230. switch ($op) {
  231. case 'delete':
  232. foreach ($settings as $setting) {
  233. variable_del($setting .'_'. $info->type);
  234. }
  235. break;
  236. }
  237. }
  238. /**
  239. * Implementation of hook_perm().
  240. */
  241. function comment_perm() {
  242. return array('access comments', 'post comments', 'administer comments', 'post comments without approval');
  243. }
  244. /**
  245. * Implementation of hook_block().
  246. *
  247. * Generates a block with the most recent comments.
  248. */
  249. function comment_block($op = 'list', $delta = 0) {
  250. if ($op == 'list') {
  251. $blocks[0]['info'] = t('Recent comments');
  252. return $blocks;
  253. }
  254. else if ($op == 'view' && user_access('access comments')) {
  255. $block['subject'] = t('Recent comments');
  256. $block['content'] = theme('comment_block');
  257. return $block;
  258. }
  259. }
  260. /**
  261. * Find a number of recent comments. This is done in two steps.
  262. * 1. Find the n (specified by $number) nodes that have the most recent
  263. * comments. This is done by querying node_comment_statistics which has
  264. * an index on last_comment_timestamp, and is thus a fast query.
  265. * 2. Loading the information from the comments table based on the nids found
  266. * in step 1.
  267. *
  268. * @param $number
  269. * (optional) The maximum number of comments to find.
  270. * @return
  271. * An array of comment objects each containing a nid,
  272. * subject, cid, and timestamp, or an empty array if there are no recent
  273. * comments visible to the current user.
  274. */
  275. function comment_get_recent($number = 10) {
  276. // Select the $number nodes (visible to the current user) with the most
  277. // recent comments. This is efficient due to the index on
  278. // last_comment_timestamp.
  279. $result = db_query_range(db_rewrite_sql("SELECT nc.nid FROM {node_comment_statistics} nc WHERE nc.comment_count > 0 ORDER BY nc.last_comment_timestamp DESC", 'nc'), 0, $number);
  280. $nids = array();
  281. while ($row = db_fetch_object($result)) {
  282. $nids[] = $row->nid;
  283. }
  284. $comments = array();
  285. if (!empty($nids)) {
  286. // From among the comments on the nodes selected in the first query,
  287. // find the $number most recent comments.
  288. $result = db_query_range('SELECT c.nid, c.subject, c.cid, c.timestamp FROM {comments} c INNER JOIN {node} n ON n.nid = c.nid WHERE c.nid IN ('. implode(',', $nids) .') AND n.status = 1 AND c.status = %d ORDER BY c.cid DESC', COMMENT_PUBLISHED, 0, $number);
  289. while ($comment = db_fetch_object($result)) {
  290. $comments[] = $comment;
  291. }
  292. }
  293. return $comments;
  294. }
  295. /**
  296. * Calculate page number for first new comment.
  297. *
  298. * @param $num_comments
  299. * Number of comments.
  300. * @param $new_replies
  301. * Number of new replies.
  302. * @param $node
  303. * The first new comment node.
  304. * @return
  305. * "page=X" if the page number is greater than zero; empty string otherwise.
  306. */
  307. function comment_new_page_count($num_comments, $new_replies, $node) {
  308. $comments_per_page = _comment_get_display_setting('comments_per_page', $node);
  309. $mode = _comment_get_display_setting('mode', $node);
  310. $order = _comment_get_display_setting('sort', $node);
  311. $pagenum = NULL;
  312. $flat = in_array($mode, array(COMMENT_MODE_FLAT_COLLAPSED, COMMENT_MODE_FLAT_EXPANDED));
  313. if ($num_comments <= $comments_per_page || ($flat && $order == COMMENT_ORDER_NEWEST_FIRST)) {
  314. // Only one page of comments or flat forum and newest first.
  315. // First new comment will always be on first page.
  316. $pageno = 0;
  317. }
  318. else {
  319. if ($flat) {
  320. // Flat comments and oldest first.
  321. $count = $num_comments - $new_replies;
  322. }
  323. else {
  324. // Threaded comments. See the documentation for comment_render().
  325. if ($order == COMMENT_ORDER_NEWEST_FIRST) {
  326. // Newest first: find the last thread with new comment
  327. $result = db_query('SELECT thread FROM (SELECT thread FROM {comments} WHERE nid = %d AND status = 0 ORDER BY timestamp DESC LIMIT %d) AS thread ORDER BY thread DESC LIMIT 1', $node->nid, $new_replies);
  328. $thread = db_result($result);
  329. $result_count = db_query("SELECT COUNT(*) FROM {comments} WHERE nid = %d AND status = 0 AND thread > '". $thread ."'", $node->nid);
  330. }
  331. else {
  332. // Oldest first: find the first thread with new comment
  333. $result = db_query('SELECT thread FROM (SELECT thread FROM {comments} WHERE nid = %d AND status = 0 ORDER BY timestamp DESC LIMIT %d) AS thread ORDER BY SUBSTRING(thread, 1, (LENGTH(thread) - 1)) LIMIT 1', $node->nid, $new_replies);
  334. $thread = substr(db_result($result), 0, -1);
  335. $result_count = db_query("SELECT COUNT(*) FROM {comments} WHERE nid = %d AND status = 0 AND SUBSTRING(thread, 1, (LENGTH(thread) - 1)) < '". $thread ."'", $node->nid);
  336. }
  337. $count = db_result($result_count);
  338. }
  339. $pageno = $count / $comments_per_page;
  340. }
  341. if ($pageno >= 1) {
  342. $pagenum = "page=". intval($pageno);
  343. }
  344. return $pagenum;
  345. }
  346. /**
  347. * Returns a formatted list of recent comments to be displayed in the comment block.
  348. *
  349. * @return
  350. * The comment list HTML.
  351. * @ingroup themeable
  352. */
  353. function theme_comment_block() {
  354. $items = array();
  355. foreach (comment_get_recent() as $comment) {
  356. $items[] = l($comment->subject, 'node/'. $comment->nid, array('fragment' => 'comment-'. $comment->cid)) .'<br />'. t('@time ago', array('@time' => format_interval(time() - $comment->timestamp)));
  357. }
  358. if ($items) {
  359. return theme('item_list', $items);
  360. }
  361. }
  362. /**
  363. * Implementation of hook_link().
  364. */
  365. function comment_link($type, $node = NULL, $teaser = FALSE) {
  366. $links = array();
  367. if ($type == 'node' && $node->comment) {
  368. if ($teaser) {
  369. // Main page: display the number of comments that have been posted.
  370. if (user_access('access comments')) {
  371. $all = comment_num_all($node->nid);
  372. if ($all) {
  373. $links['comment_comments'] = array(
  374. 'title' => format_plural($all, '1 comment', '@count comments'),
  375. 'href' => "node/$node->nid",
  376. 'attributes' => array('title' => t('Jump to the first comment of this posting.')),
  377. 'fragment' => 'comments'
  378. );
  379. $new = comment_num_new($node->nid);
  380. if ($new) {
  381. $links['comment_new_comments'] = array(
  382. 'title' => format_plural($new, '1 new comment', '@count new comments'),
  383. 'href' => "node/$node->nid",
  384. 'query' => comment_new_page_count($all, $new, $node),
  385. 'attributes' => array('title' => t('Jump to the first new comment of this posting.')),
  386. 'fragment' => 'new'
  387. );
  388. }
  389. }
  390. else {
  391. if ($node->comment == COMMENT_NODE_READ_WRITE) {
  392. if (user_access('post comments')) {
  393. $links['comment_add'] = array(
  394. 'title' => t('Add new comment'),
  395. 'href' => "comment/reply/$node->nid",
  396. 'attributes' => array('title' => t('Add a new comment to this page.')),
  397. 'fragment' => 'comment-form'
  398. );
  399. }
  400. else {
  401. $links['comment_forbidden']['title'] = theme('comment_post_forbidden', $node);
  402. }
  403. }
  404. }
  405. }
  406. }
  407. else {
  408. // Node page: add a "post comment" link if the user is allowed to
  409. // post comments, if this node is not read-only, and if the comment form isn't already shown
  410. if ($node->comment == COMMENT_NODE_READ_WRITE) {
  411. if (user_access('post comments')) {
  412. if (variable_get('comment_form_location_'. $node->type, COMMENT_FORM_SEPARATE_PAGE) == COMMENT_FORM_SEPARATE_PAGE) {
  413. $links['comment_add'] = array(
  414. 'title' => t('Add new comment'),
  415. 'href' => "comment/reply/$node->nid",
  416. 'attributes' => array('title' => t('Share your thoughts and opinions related to this posting.')),
  417. 'fragment' => 'comment-form'
  418. );
  419. }
  420. }
  421. else {
  422. $links['comment_forbidden']['title'] = theme('comment_post_forbidden', $node);
  423. }
  424. }
  425. }
  426. }
  427. if ($type == 'comment') {
  428. $links = comment_links($node, $teaser);
  429. }
  430. if (isset($links['comment_forbidden'])) {
  431. $links['comment_forbidden']['html'] = TRUE;
  432. }
  433. return $links;
  434. }
  435. /**
  436. * Implementation of hook_form_alter().
  437. */
  438. function comment_form_alter(&$form, $form_state, $form_id) {
  439. if ($form_id == 'node_type_form' && isset($form['identity']['type'])) {
  440. $form['comment'] = array(
  441. '#type' => 'fieldset',
  442. '#title' => t('Comment settings'),
  443. '#collapsible' => TRUE,
  444. '#collapsed' => TRUE,
  445. );
  446. $form['comment']['comment'] = array(
  447. '#type' => 'radios',
  448. '#title' => t('Default comment setting'),
  449. '#default_value' => variable_get('comment_'. $form['#node_type']->type, COMMENT_NODE_READ_WRITE),
  450. '#options' => array(t('Disabled'), t('Read only'), t('Read/Write')),
  451. '#description' => t('Users with the <em>administer comments</em> permission will be able to override this setting.'),
  452. );
  453. $form['comment']['comment_default_mode'] = array(
  454. '#type' => 'radios',
  455. '#title' => t('Default display mode'),
  456. '#default_value' => variable_get('comment_default_mode_'. $form['#node_type']->type, COMMENT_MODE_THREADED_EXPANDED),
  457. '#options' => _comment_get_modes(),
  458. '#description' => t('The default view for comments. Expanded views display the body of the comment. Threaded views keep replies together.'),
  459. );
  460. $form['comment']['comment_default_order'] = array(
  461. '#type' => 'radios',
  462. '#title' => t('Default display order'),
  463. '#default_value' => variable_get('comment_default_order_'. $form['#node_type']->type, COMMENT_ORDER_NEWEST_FIRST),
  464. '#options' => _comment_get_orders(),
  465. '#description' => t('The default sorting for new users and anonymous users while viewing comments. These users may change their view using the comment control panel. For registered users, this change is remembered as a persistent user preference.'),
  466. );
  467. $form['comment']['comment_default_per_page'] = array(
  468. '#type' => 'select',
  469. '#title' => t('Default comments per page'),
  470. '#default_value' => variable_get('comment_default_per_page_'. $form['#node_type']->type, 50),
  471. '#options' => _comment_per_page(),
  472. '#description' => t('Default number of comments for each page: more comments are distributed in several pages.'),
  473. );
  474. $form['comment']['comment_controls'] = array(
  475. '#type' => 'radios',
  476. '#title' => t('Comment controls'),
  477. '#default_value' => variable_get('comment_controls_'. $form['#node_type']->type, COMMENT_CONTROLS_HIDDEN),
  478. '#options' => array(
  479. t('Display above the comments'),
  480. t('Display below the comments'),
  481. t('Display above and below the comments'),
  482. t('Do not display')),
  483. '#description' => t('Position of the comment controls box. The comment controls let the user change the default display mode and display order of comments.'),
  484. );
  485. $form['comment']['comment_anonymous'] = array(
  486. '#type' => 'radios',
  487. '#title' => t('Anonymous commenting'),
  488. '#default_value' => variable_get('comment_anonymous_'. $form['#node_type']->type, COMMENT_ANONYMOUS_MAYNOT_CONTACT),
  489. '#options' => array(
  490. COMMENT_ANONYMOUS_MAYNOT_CONTACT => t('Anonymous posters may not enter their contact information'),
  491. COMMENT_ANONYMOUS_MAY_CONTACT => t('Anonymous posters may leave their contact information'),
  492. COMMENT_ANONYMOUS_MUST_CONTACT => t('Anonymous posters must leave their contact information')),
  493. '#description' => t('This option is enabled when anonymous users have permission to post comments on the <a href="@url">permissions page</a>.', array('@url' => url('admin/user/permissions', array('fragment' => 'module-comment')))),
  494. );
  495. if (!user_access('post comments', drupal_anonymous_user())) {
  496. $form['comment']['comment_anonymous']['#disabled'] = TRUE;
  497. }
  498. $form['comment']['comment_subject_field'] = array(
  499. '#type' => 'radios',
  500. '#title' => t('Comment subject field'),
  501. '#default_value' => variable_get('comment_subject_field_'. $form['#node_type']->type, 1),
  502. '#options' => array(t('Disabled'), t('Enabled')),
  503. '#description' => t('Can users provide a unique subject for their comments?'),
  504. );
  505. $form['comment']['comment_preview'] = array(
  506. '#type' => 'radios',
  507. '#title' => t('Preview comment'),
  508. '#default_value' => variable_get('comment_preview_'. $form['#node_type']->type, COMMENT_PREVIEW_REQUIRED),
  509. '#options' => array(t('Optional'), t('Required')),
  510. '#description' => t("Forces a user to look at their comment by clicking on a 'Preview' button before they can actually add the comment"),
  511. );
  512. $form['comment']['comment_form_location'] = array(
  513. '#type' => 'radios',
  514. '#title' => t('Location of comment submission form'),
  515. '#default_value' => variable_get('comment_form_location_'. $form['#node_type']->type, COMMENT_FORM_SEPARATE_PAGE),
  516. '#options' => array(t('Display on separate page'), t('Display below post or comments')),
  517. );
  518. }
  519. elseif (isset($form['type']) && isset($form['#node'])) {
  520. if ($form['type']['#value'] .'_node_form' == $form_id) {
  521. $node = $form['#node'];
  522. $form['comment_settings'] = array(
  523. '#type' => 'fieldset',
  524. '#access' => user_access('administer comments'),
  525. '#title' => t('Comment settings'),
  526. '#collapsible' => TRUE,
  527. '#collapsed' => TRUE,
  528. '#weight' => 30,
  529. );
  530. $form['comment_settings']['comment'] = array(
  531. '#type' => 'radios',
  532. '#parents' => array('comment'),
  533. '#default_value' => $node->comment,
  534. '#options' => array(t('Disabled'), t('Read only'), t('Read/Write')),
  535. );
  536. }
  537. }
  538. }
  539. /**
  540. * Implementation of hook_nodeapi().
  541. */
  542. function comment_nodeapi(&$node, $op, $arg = 0) {
  543. switch ($op) {
  544. case 'load':
  545. return db_fetch_array(db_query("SELECT last_comment_timestamp, last_comment_name, comment_count FROM {node_comment_statistics} WHERE nid = %d", $node->nid));
  546. break;
  547. case 'prepare':
  548. if (!isset($node->comment)) {
  549. $node->comment = variable_get("comment_$node->type", COMMENT_NODE_READ_WRITE);
  550. }
  551. break;
  552. case 'insert':
  553. db_query('INSERT INTO {node_comment_statistics} (nid, last_comment_timestamp, last_comment_name, last_comment_uid, comment_count) VALUES (%d, %d, NULL, %d, 0)', $node->nid, $node->changed, $node->uid);
  554. break;
  555. case 'delete':
  556. db_query('DELETE FROM {comments} WHERE nid = %d', $node->nid);
  557. db_query('DELETE FROM {node_comment_statistics} WHERE nid = %d', $node->nid);
  558. break;
  559. case 'update index':
  560. $text = '';
  561. $comments = db_query('SELECT subject, comment, format FROM {comments} WHERE nid = %d AND status = %d', $node->nid, COMMENT_PUBLISHED);
  562. while ($comment = db_fetch_object($comments)) {
  563. $text .= '<h2>'. check_plain($comment->subject) .'</h2>'. check_markup($comment->comment, $comment->format, FALSE);
  564. }
  565. return $text;
  566. case 'search result':
  567. $comments = db_result(db_query('SELECT comment_count FROM {node_comment_statistics} WHERE nid = %d', $node->nid));
  568. return format_plural($comments, '1 comment', '@count comments');
  569. case 'rss item':
  570. if ($node->comment != COMMENT_NODE_DISABLED) {
  571. return array(array('key' => 'comments', 'value' => url('node/'. $node->nid, array('fragment' => 'comments', 'absolute' => TRUE))));
  572. }
  573. else {
  574. return array();
  575. }
  576. }
  577. }
  578. /**
  579. * Implementation of hook_user().
  580. */
  581. function comment_user($type, $edit, &$user, $category = NULL) {
  582. if ($type == 'delete') {
  583. db_query('UPDATE {comments} SET uid = 0 WHERE uid = %d', $user->uid);
  584. db_query('UPDATE {node_comment_statistics} SET last_comment_uid = 0 WHERE last_comment_uid = %d', $user->uid);
  585. }
  586. }
  587. /**
  588. * This is *not* a hook_access() implementation. This function is called
  589. * to determine whether the current user has access to a particular comment.
  590. *
  591. * Authenticated users can edit their comments as long they have not been
  592. * replied to. This prevents people from changing or revising their
  593. * statements based on the replies to their posts.
  594. *
  595. * @param $op
  596. * The operation that is to be performed on the comment. Only 'edit' is recognized now.
  597. * @param $comment
  598. * The comment object.
  599. * @return
  600. * TRUE if the current user has acces to the comment, FALSE otherwise.
  601. */
  602. function comment_access($op, $comment) {
  603. global $user;
  604. if ($op == 'edit') {
  605. return ($user->uid && $user->uid == $comment->uid && comment_num_replies($comment->cid) == 0 && $comment->status == COMMENT_PUBLISHED) || user_access('administer comments');
  606. }
  607. }
  608. /**
  609. * A simple helper function.
  610. *
  611. * @return
  612. * The 0th and the 1st path components joined by a slash.
  613. */
  614. function comment_node_url() {
  615. return arg(0) .'/'. arg(1);
  616. }
  617. /**
  618. * Accepts a submission of new or changed comment content.
  619. *
  620. * @param $edit
  621. * A comment array.
  622. *
  623. * @return
  624. * If the comment is successfully saved the comment ID is returned. If the comment
  625. * is not saved, FALSE is returned.
  626. */
  627. function comment_save($edit) {
  628. global $user;
  629. if (user_access('post comments') && (user_access('administer comments') || node_comment_mode($edit['nid']) == COMMENT_NODE_READ_WRITE)) {
  630. if (!form_get_errors()) {
  631. $edit += array(
  632. 'mail' => '',
  633. 'homepage' => '',
  634. 'name' => '',
  635. 'status' => user_access('post comments without approval') ? COMMENT_PUBLISHED : COMMENT_NOT_PUBLISHED,
  636. );
  637. if ($edit['cid']) {
  638. // Update the comment in the database.
  639. db_query("UPDATE {comments} SET status = %d, timestamp = %d, subject = '%s', comment = '%s', format = %d, uid = %d, name = '%s', mail = '%s', homepage = '%s' WHERE cid = %d", $edit['status'], $edit['timestamp'], $edit['subject'], $edit['comment'], $edit['format'], $edit['uid'], $edit['name'], $edit['mail'], $edit['homepage'], $edit['cid']);
  640. // Allow modules to respond to the updating of a comment.
  641. comment_invoke_comment($edit, 'update');
  642. // Add an entry to the watchdog log.
  643. watchdog('content', 'Comment: updated %subject.', array('%subject' => $edit['subject']), WATCHDOG_NOTICE, l(t('view'), 'node/'. $edit['nid'], array('fragment' => 'comment-'. $edit['cid'])));
  644. }
  645. else {
  646. // Add the comment to database.
  647. // Here we are building the thread field. See the documentation for
  648. // comment_render().
  649. if ($edit['pid'] == 0) {
  650. // This is a comment with no parent comment (depth 0): we start
  651. // by retrieving the maximum thread level.
  652. $max = db_result(db_query('SELECT MAX(thread) FROM {comments} WHERE nid = %d', $edit['nid']));
  653. // Strip the "/" from the end of the thread.
  654. $max = rtrim($max, '/');
  655. // Finally, build the thread field for this new comment.
  656. $thread = int2vancode(vancode2int($max) + 1) .'/';
  657. }
  658. else {
  659. // This is comment with a parent comment: we increase
  660. // the part of the thread value at the proper depth.
  661. // Get the parent comment:
  662. $parent = _comment_load($edit['pid']);
  663. // Strip the "/" from the end of the parent thread.
  664. $parent->thread = (string) rtrim((string) $parent->thread, '/');
  665. // Get the max value in _this_ thread.
  666. $max = db_result(db_query("SELECT MAX(thread) FROM {comments} WHERE thread LIKE '%s.%%' AND nid = %d", $parent->thread, $edit['nid']));
  667. if ($max == '') {
  668. // First child of this parent.
  669. $thread = $parent->thread .'.'. int2vancode(0) .'/';
  670. }
  671. else {
  672. // Strip the "/" at the end of the thread.
  673. $max = rtrim($max, '/');
  674. // We need to get the value at the correct depth.
  675. $parts = explode('.', $max);
  676. $parent_depth = count(explode('.', $parent->thread));
  677. $last = $parts[$parent_depth];
  678. // Finally, build the thread field for this new comment.
  679. $thread = $parent->thread .'.'. int2vancode(vancode2int($last) + 1) .'/';
  680. }
  681. }
  682. if (empty($edit['timestamp'])) {
  683. $edit['timestamp'] = time();
  684. }
  685. if ($edit['uid'] === $user->uid && isset($user->name)) { // '===' Need to modify anonymous users as well.
  686. $edit['name'] = $user->name;
  687. }
  688. db_query("INSERT INTO {comments} (nid, pid, uid, subject, comment, format, hostname, timestamp, status, thread, name, mail, homepage) VALUES (%d, %d, %d, '%s', '%s', %d, '%s', %d, %d, '%s', '%s', '%s', '%s')", $edit['nid'], $edit['pid'], $edit['uid'], $edit['subject'], $edit['comment'], $edit['format'], ip_address(), $edit['timestamp'], $edit['status'], $thread, $edit['name'], $edit['mail'], $edit['homepage']);
  689. $edit['cid'] = db_last_insert_id('comments', 'cid');
  690. // Tell the other modules a new comment has been submitted.
  691. comment_invoke_comment($edit, 'insert');
  692. // Add an entry to the watchdog log.
  693. watchdog('content', 'Comment: added %subject.', array('%subject' => $edit['subject']), WATCHDOG_NOTICE, l(t('view'), 'node/'. $edit['nid'], array('fragment' => 'comment-'. $edit['cid'])));
  694. }
  695. _comment_update_node_statistics($edit['nid']);
  696. // Clear the cache so an anonymous user can see his comment being added.
  697. cache_clear_all();
  698. // Explain the approval queue if necessary, and then
  699. // redirect the user to the node he's commenting on.
  700. if ($edit['status'] == COMMENT_NOT_PUBLISHED) {
  701. drupal_set_message(t('Your comment has been queued for moderation by site administrators and will be published after approval.'));
  702. }
  703. else {
  704. comment_invoke_comment($edit, 'publish');
  705. }
  706. return $edit['cid'];
  707. }
  708. else {
  709. return FALSE;
  710. }
  711. }
  712. else {
  713. watchdog('content', 'Comment: unauthorized comment submitted or comment submitted to a closed post %subject.', array('%subject' => $edit['subject']), WATCHDOG_WARNING);
  714. drupal_set_message(t('Comment: unauthorized comment submitted or comment submitted to a closed post %subject.', array('%subject' => $edit['subject'])), 'error');
  715. return FALSE;
  716. }
  717. }
  718. /**
  719. * Build command links for a comment (e.g.\ edit, reply, delete) with respect to the current user's access permissions.
  720. *
  721. * @param $comment
  722. * The comment to which the links will be related.
  723. * @param $return
  724. * Not used.
  725. * @return
  726. * An associative array containing the links.
  727. */
  728. function comment_links($comment, $return = 1) {
  729. global $user;
  730. $links = array();
  731. // If we are viewing just this comment, we link back to the node.
  732. if ($return) {
  733. $links['comment_parent'] = array(
  734. 'title' => t('parent'),
  735. 'href' => comment_node_url(),
  736. 'fragment' => "comment-$comment->cid"
  737. );
  738. }
  739. if (node_comment_mode($comment->nid) == COMMENT_NODE_READ_WRITE) {
  740. if (user_access('administer comments') && user_access('post comments')) {
  741. $links['comment_delete'] = array(
  742. 'title' => t('delete'),
  743. 'href' => "comment/delete/$comment->cid"
  744. );
  745. $links['comment_edit'] = array(
  746. 'title' => t('edit'),
  747. 'href' => "comment/edit/$comment->cid"
  748. );
  749. $links['comment_reply'] = array(
  750. 'title' => t('reply'),
  751. 'href' => "comment/reply/$comment->nid/$comment->cid"
  752. );
  753. }
  754. else if (user_access('post comments')) {
  755. if (comment_access('edit', $comment)) {
  756. $links['comment_edit'] = array(
  757. 'title' => t('edit'),
  758. 'href' => "comment/edit/$comment->cid"
  759. );
  760. }
  761. $links['comment_reply'] = array(
  762. 'title' => t('reply'),
  763. 'href' => "comment/reply/$comment->nid/$comment->cid"
  764. );
  765. }
  766. else {
  767. $node = node_load($comment->nid);
  768. $links['comment_forbidden']['title'] = theme('comment_post_forbidden', $node);
  769. }
  770. }
  771. return $links;
  772. }
  773. /**
  774. * Renders comment(s).
  775. *
  776. * @param $node
  777. * The node which comment(s) needs rendering.
  778. * @param $cid
  779. * Optional, if given, only one comment is rendered.
  780. *
  781. * To display threaded comments in the correct order we keep a 'thread' field
  782. * and order by that value. This field keeps this data in
  783. * a way which is easy to update and convenient to use.
  784. *
  785. * A "thread" value starts at "1". If we add a child (A) to this comment,
  786. * we assign it a "thread" = "1.1". A child of (A) will have "1.1.1". Next
  787. * brother of (A) will get "1.2". Next brother of the parent of (A) will get
  788. * "2" and so on.
  789. *
  790. * First of all note that the thread field stores the depth of the comment:
  791. * depth 0 will be "X", depth 1 "X.X", depth 2 "X.X.X", etc.
  792. *
  793. * Now to get the ordering right, consider this example:
  794. *
  795. * 1
  796. * 1.1
  797. * 1.1.1
  798. * 1.2
  799. * 2
  800. *
  801. * If we "ORDER BY thread ASC" we get the above result, and this is the
  802. * natural order sorted by time. However, if we "ORDER BY thread DESC"
  803. * we get:
  804. *
  805. * 2
  806. * 1.2
  807. * 1.1.1
  808. * 1.1
  809. * 1
  810. *
  811. * Clearly, this is not a natural way to see a thread, and users will get
  812. * confused. The natural order to show a thread by time desc would be:
  813. *
  814. * 2
  815. * 1
  816. * 1.2
  817. * 1.1
  818. * 1.1.1
  819. *
  820. * which is what we already did before the standard pager patch. To achieve
  821. * this we simply add a "/" at the end of each "thread" value. This way out
  822. * thread fields will look like depicted below:
  823. *
  824. * 1/
  825. * 1.1/
  826. * 1.1.1/
  827. * 1.2/
  828. * 2/
  829. *
  830. * we add "/" since this char is, in ASCII, higher than every number, so if
  831. * now we "ORDER BY thread DESC" we get the correct order. However this would
  832. * spoil the reverse ordering, "ORDER BY thread ASC" -- here, we do not need
  833. * to consider the trailing "/" so we use a substring only.
  834. */
  835. function comment_render($node, $cid = 0) {
  836. global $user;
  837. $output = '';
  838. if (user_access('access comments')) {
  839. // Pre-process variables.
  840. $nid = $node->nid;
  841. if (empty($nid)) {
  842. $nid = 0;
  843. }
  844. $mode = _comment_get_display_setting('mode', $node);
  845. $order = _comment_get_display_setting('sort', $node);
  846. $comments_per_page = _comment_get_display_setting('comments_per_page', $node);
  847. if ($cid && is_numeric($cid)) {
  848. // Single comment view.
  849. $query = 'SELECT c.cid, c.pid, c.nid, c.subject, c.comment, c.format, c.timestamp, c.name, c.mail, c.homepage, u.uid, u.name AS registered_name, u.signature, u.signature_format, u.picture, u.data, c.status FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.cid = %d';
  850. $query_args = array($cid);
  851. if (!user_access('administer comments')) {
  852. $query .= ' AND c.status = %d';
  853. $query_args[] = COMMENT_PUBLISHED;
  854. }
  855. $query = db_rewrite_sql($query, 'c', 'cid');
  856. $result = db_query($query, $query_args);
  857. if ($comment = db_fetch_object($result)) {
  858. $comment->name = $comment->uid ? $comment->registered_name : $comment->name;
  859. $links = module_invoke_all('link', 'comment', $comment, 1);
  860. drupal_alter('link', $links, $node, $comment);
  861. $output .= theme('comment_view', $comment, $node, $links);
  862. }
  863. }
  864. else {
  865. // Multiple comment view
  866. $query_count = 'SELECT COUNT(*) FROM {comments} c WHERE c.nid = %d';
  867. $query = 'SELECT c.cid as cid, c.pid, c.nid, c.subject, c.comment, c.format, c.timestamp, c.name, c.mail, c.homepage, u.uid, u.name AS registered_name, u.signature, u.signature_format, u.picture, u.data, c.thread, c.status FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.nid = %d';
  868. $query_args = array($nid);
  869. if (!user_access('administer comments')) {
  870. $query .= ' AND c.status = %d';
  871. $query_count .= ' AND c.status = %d';
  872. $query_args[] = COMMENT_PUBLISHED;
  873. }
  874. if ($order == COMMENT_ORDER_NEWEST_FIRST) {
  875. if ($mode == COMMENT_MODE_FLAT_COLLAPSED || $mode == COMMENT_MODE_FLAT_EXPANDED) {
  876. $query .= ' ORDER BY c.cid DESC';
  877. }
  878. else {
  879. $query .= ' ORDER BY c.thread DESC';
  880. }
  881. }
  882. else if ($order == COMMENT_ORDER_OLDEST_FIRST) {
  883. if ($mode == COMMENT_MODE_FLAT_COLLAPSED || $mode == COMMENT_MODE_FLAT_EXPANDED) {
  884. $query .= ' ORDER BY c.cid';
  885. }
  886. else {
  887. // See comment above. Analysis reveals that this doesn't cost too
  888. // much. It scales much much better than having the whole comment
  889. // structure.
  890. $query .= ' ORDER BY SUBSTRING(c.thread, 1, (LENGTH(c.thread) - 1))';
  891. }
  892. }
  893. $query = db_rewrite_sql($query, 'c', 'cid');
  894. $query_count = db_rewrite_sql($query_count, 'c', 'cid');
  895. // Start a form, for use with comment control.
  896. $result = pager_query($query, $comments_per_page, 0, $query_count, $query_args);
  897. $divs = 0;
  898. $num_rows = FALSE;
  899. $comments = '';
  900. drupal_add_css(drupal_get_path('module', 'comment') .'/comment.css');
  901. while ($comment = db_fetch_object($result)) {
  902. $comment = drupal_unpack($comment);
  903. $comment->name = $comment->uid ? $comment->registered_name : $comment->name;
  904. $comment->depth = count(explode('.', $comment->thread)) - 1;
  905. if ($mode == COMMENT_MODE_THREADED_COLLAPSED || $mode == COMMENT_MODE_THREADED_EXPANDED) {
  906. if ($comment->depth > $divs) {
  907. $divs++;
  908. $comments .= '<div class="indented">';
  909. }
  910. else {
  911. while ($comment->depth < $divs) {
  912. $divs--;
  913. $comments .= '</div>';
  914. }
  915. }
  916. }
  917. if ($mode == COMMENT_MODE_FLAT_COLLAPSED) {
  918. $comments .= theme('comment_flat_collapsed', $comment, $node);
  919. }
  920. else if ($mode == COMMENT_MODE_FLAT_EXPANDED) {
  921. $comments .= theme('comment_flat_expanded', $comment, $node);
  922. }
  923. else if ($mode == COMMENT_MODE_THREADED_COLLAPSED) {
  924. $comments .= theme('comment_thread_collapsed', $comment, $node);
  925. }
  926. else if ($mode == COMMENT_MODE_THREADED_EXPANDED) {
  927. $comments .= theme('comment_thread_expanded', $comment, $node);
  928. }
  929. $num_rows = TRUE;
  930. }
  931. while ($divs-- > 0) {
  932. $comments .= '</div>';
  933. }
  934. $comment_controls = variable_get('comment_controls_'. $node->type, COMMENT_CONTROLS_HIDDEN);
  935. if ($num_rows && ($comment_controls == COMMENT_CONTROLS_ABOVE || $comment_controls == COMMENT_CONTROLS_ABOVE_BELOW)) {
  936. $output .= drupal_get_form('comment_controls', $mode, $order, $comments_per_page);
  937. }
  938. $output .= $comments;
  939. $output .= theme('pager', NULL, $comments_per_page, 0);
  940. if ($num_rows && ($comment_controls == COMMENT_CONTROLS_BELOW || $comment_controls == COMMENT_CONTROLS_ABOVE_BELOW)) {
  941. $output .= drupal_get_form('comment_controls', $mode, $order, $comments_per_page);
  942. }
  943. }
  944. // If enabled, show new comment form if it's not already being displayed.
  945. $reply = arg(0) == 'comment' && arg(1) == 'reply';
  946. if (user_access('post comments') && node_comment_mode($nid) == COMMENT_NODE_READ_WRITE && (variable_get('comment_form_location_'. $node->type, COMMENT_FORM_SEPARATE_PAGE) == COMMENT_FORM_BELOW) && !$reply) {
  947. $output .= comment_form_box(array('nid' => $nid), t('Post new comment'));
  948. }
  949. if ($output) {
  950. $output = theme('comment_wrapper', $output, $node);
  951. }
  952. }
  953. return $output;
  954. }
  955. /**
  956. * Comment operations. We offer different update operations depending on
  957. * which comment administration page we're on.
  958. *
  959. * @param $action
  960. * The comment administration page.
  961. * @return
  962. * An associative array containing the offered operations.
  963. */
  964. function comment_operations($action = NULL) {
  965. if ($action == 'publish') {
  966. $operations = array(
  967. 'publish' => array(t('Publish the selected comments'), 'UPDATE {comments} SET status = '. COMMENT_PUBLISHED .' WHERE cid = %d'),
  968. 'delete' => array(t('Delete the selected comments'), ''),
  969. );
  970. }
  971. else if ($action == 'unpublish') {
  972. $operations = array(
  973. 'unpublish' => array(t('Unpublish the selected comments'), 'UPDATE {comments} SET status = '. COMMENT_NOT_PUBLISHED .' WHERE cid = %d'),
  974. 'delete' => array(t('Delete the selected comments'), ''),
  975. );
  976. }
  977. else {
  978. $operations = array(
  979. 'publish' => array(t('Publish the selected comments'), 'UPDATE {comments} SET status = '. COMMENT_PUBLISHED .' WHERE cid = %d'),
  980. 'unpublish' => array(t('Unpublish the selected comments'), 'UPDATE {comments} SET status = '. COMMENT_NOT_PUBLISHED .' WHERE cid = %d'),
  981. 'delete' => array(t('Delete the selected comments'), ''),
  982. );
  983. }
  984. return $operations;
  985. }
  986. /**
  987. * Misc functions: helpers, privates, history
  988. */
  989. /**
  990. * Load the entire comment by cid.
  991. *
  992. * @param $cid
  993. * The identifying comment id.
  994. * @return
  995. * The comment object.
  996. */
  997. function _comment_load($cid) {
  998. return db_fetch_object(db_query('SELECT * FROM {comments} WHERE cid = %d', $cid));
  999. }
  1000. /**
  1001. * Get comment count for a node.
  1002. *
  1003. * @param $nid
  1004. * The node id.
  1005. * @return
  1006. * The comment count.
  1007. */
  1008. function comment_num_all($nid) {
  1009. static $cache;
  1010. if (!isset($cache[$nid])) {
  1011. $cache[$nid] = db_result(db_query('SELECT comment_count FROM {node_comment_statistics} WHERE nid = %d', $nid));
  1012. }
  1013. return $cache[$nid];
  1014. }
  1015. /**
  1016. * Get replies count for a comment.
  1017. *
  1018. * @param $pid
  1019. * The comment id.
  1020. * @return
  1021. * The replies count.
  1022. */
  1023. function comment_num_replies($pid) {
  1024. static $cache;
  1025. if (!isset($cache[$pid])) {
  1026. $cache[$pid] = db_result(db_query('SELECT COUNT(cid) FROM {comments} WHERE pid = %d AND status = %d', $pid, COMMENT_PUBLISHED));
  1027. }
  1028. return $cache[$pid];
  1029. }
  1030. /**
  1031. * Get number of new comments for current user and specified node.
  1032. *
  1033. * @param $nid
  1034. * node-id to count comments for
  1035. * @param $timestamp
  1036. * time to count from (defaults to time of last user access
  1037. * to node)
  1038. */
  1039. function comment_num_new($nid, $timestamp = 0) {
  1040. global $user;
  1041. if ($user->uid) {
  1042. // Retrieve the timestamp at which the current user last viewed the
  1043. // specified node.
  1044. if (!$timestamp) {
  1045. $timestamp = node_last_viewed($nid);
  1046. }
  1047. $timestamp = ($timestamp > NODE_NEW_LIMIT ? $timestamp : NODE_NEW_LIMIT);
  1048. // Use the timestamp to retrieve the number of new comments.
  1049. $result = db_result(db_query('SELECT COUNT(c.cid) FROM {node} n INNER JOIN {comments} c ON n.nid = c.nid WHERE n.nid = %d AND timestamp > %d AND c.status = %d', $nid, $timestamp, COMMENT_PUBLISHED));
  1050. return $result;
  1051. }
  1052. else {
  1053. return 0;
  1054. }
  1055. }
  1056. /**
  1057. * Validate comment data.
  1058. *
  1059. * @param $edit
  1060. * An associative array containig the comment data.
  1061. * @return
  1062. * The original $edit.
  1063. */
  1064. function comment_validate($edit) {
  1065. global $user;
  1066. // Invoke other validation handlers
  1067. comment_invoke_comment($edit, 'validate');
  1068. if (isset($edit['date'])) {
  1069. // As of PHP 5.1.0, strtotime returns FALSE upon failure instead of -1.
  1070. if (strtotime($edit['date']) <= 0) {
  1071. form_set_error('date', t('You have to specify a valid date.'));
  1072. }
  1073. }
  1074. if (isset($edit['author']) && !$account = user_load(array('name' => $edit['author']))) {
  1075. form_set_error('author', t('You have to specify a valid author.'));
  1076. }
  1077. // Check validity of name, mail and homepage (if given)
  1078. if (!$user->uid || isset($edit['is_anonymous'])) {
  1079. $node = node_load($edit['nid']);
  1080. if (variable_get('comment_anonymous_'. $node->type, COMMENT_ANONYMOUS_MAYNOT_CONTACT) > COMMENT_ANONYMOUS_MAYNOT_CONTACT) {
  1081. if ($edit['name']) {
  1082. $taken = db_result(db_query("SELECT COUNT(uid) FROM {users} WHERE LOWER(name) = '%s'", $edit['name']));
  1083. if ($taken != 0) {
  1084. form_set_error('name', t('The name you used belongs to a registered user.'));
  1085. }
  1086. }
  1087. else if (variable_get('comment_anonymous_'. $node->type, COMMENT_ANONYMOUS_MAYNOT_CONTACT) == COMMENT_ANONYMOUS_MUST_CONTACT) {
  1088. form_set_error('name', t('You have to leave your name.'));
  1089. }
  1090. if ($edit['mail']) {
  1091. if (!valid_email_address($edit['mail'])) {
  1092. form_set_error('mail', t('The e-mail address you specified is not valid.'));
  1093. }
  1094. }
  1095. else if (variable_get('comment_anonymous_'. $node->type, COMMENT_ANONYMOUS_MAYNOT_CONTACT) == COMMENT_ANONYMOUS_MUST_CONTACT) {
  1096. form_set_error('mail', t('You have to leave an e-mail address.'));
  1097. }
  1098. if ($edit['homepage']) {
  1099. if (!valid_url($edit['homepage'], TRUE)) {
  1100. form_set_error('homepage', t('The URL of your homepage is not valid. Remember that it must be fully qualified, i.e. of the form <code>http://example.com/directory</code>.'));
  1101. }
  1102. }
  1103. }
  1104. }
  1105. return $edit;
  1106. }
  1107. /**
  1108. * Generate the basic commenting form, for appending to a node or display on a separate page.
  1109. *
  1110. * @param $title
  1111. * Not used.
  1112. * @ingroup forms
  1113. * @see comment_form_validate()
  1114. * @see comment_form_submit()
  1115. */
  1116. function comment_form(&$form_state, $edit, $title = NULL) {
  1117. global $user;
  1118. $op = isset($_POST['op']) ? $_POST['op'] : '';
  1119. $node = node_load($edit['nid']);
  1120. if (!$user->uid && variable_get('comment_anonymous_'. $node->type, COMMENT_ANONYMOUS_MAYNOT_CONTACT) != COMMENT_ANONYMOUS_MAYNOT_CONTACT) {
  1121. drupal_add_js(drupal_get_path('module', 'comment') .'/comment.js');
  1122. }
  1123. $edit += array('name' => '', 'mail' => '', 'homepage' => '');
  1124. if ($user->uid) {
  1125. if (!empty($edit['cid']) && user_access('administer comments')) {
  1126. if (!empty($edit['author'])) {
  1127. $author = $edit['author'];
  1128. }
  1129. elseif (!empty($edit['name'])) {
  1130. $author = $edit['name'];
  1131. }
  1132. else {
  1133. $author = $edit['registered_name'];
  1134. }
  1135. if (!empty($edit['status'])) {
  1136. $status = $edit['status'];
  1137. }
  1138. else {
  1139. $status = 0;
  1140. }
  1141. if (!empty($edit['date'])) {
  1142. $date = $edit['date'];
  1143. }
  1144. else {
  1145. $date = format_date($edit['timestamp'], 'custom', 'Y-m-d H:i O');
  1146. }
  1147. $form['admin'] = array(
  1148. '#type' => 'fieldset',
  1149. '#title' => t('Administration'),
  1150. '#collapsible' => TRUE,
  1151. '#collapsed' => TRUE,
  1152. '#weight' => -2,
  1153. );
  1154. if ($edit['registered_name'] != '') {
  1155. // The comment is by a registered user
  1156. $form['admin']['author'] = array(
  1157. '#type' => 'textfield',
  1158. '#title' => t('Authored by'),
  1159. '#size' => 30,
  1160. '#maxlength' => 60,
  1161. '#autocomplete_path' => 'user/autocomplete',
  1162. '#default_value' => $author,
  1163. '#weight' => -1,
  1164. );
  1165. }
  1166. else {
  1167. // The comment is by an anonymous user
  1168. $form['is_anonymous'] = array(
  1169. '#type' => 'value',
  1170. '#value' => TRUE,
  1171. );
  1172. $form['admin']['name'] = array(
  1173. '#type' => 'textfield',
  1174. '#title' => t('Authored by'),
  1175. '#size' => 30,
  1176. '#maxlength' => 60,
  1177. '#default_value' => $author,
  1178. '#weight' => -1,
  1179. );
  1180. $form['admin']['mail'] = array(
  1181. '#type' => 'textfield',
  1182. '#title' => t('E-mail'),
  1183. '#maxlength' => 64,
  1184. '#size' => 30,
  1185. '#default_value' => $edit['mail'],
  1186. '#description' => t('The content of this field is kept private and will not be shown publicly.'),
  1187. );
  1188. $form['admin']['homepage'] = array(
  1189. '#type' => 'textfield',
  1190. '#title' => t('Homepage'),
  1191. '#maxlength' => 255,
  1192. '#size' => 30,
  1193. '#default_value' => $edit['homepage'],
  1194. );
  1195. }
  1196. $form['admin']['date'] = array('#type' => 'textfield', '#parents' => array('date'), '#title' => t('Authored on'), '#size' => 20, '#maxlength' => 25, '#default_value' => $date, '#weight' => -1);
  1197. $form['admin']['status'] = array('#type' => 'radios', '#parents' => array('status'), '#title' => t('Status'), '#default_value' => $status, '#options' => array(t('Published'), t('Not published')), '#weight' => -1);
  1198. }
  1199. else {
  1200. $form['_author'] = array('#type' => 'item', '#title' => t('Your name'), '#value' => theme('username', $user)
  1201. );
  1202. $form['author'] = array('#type' => 'value', '#value' => $user->name);
  1203. }
  1204. }
  1205. else if (variable_get('comment_anonymous_'. $node->type, COMMENT_ANONYMOUS_MAYNOT_CONTACT) == COMMENT_ANONYMOUS_MAY_CONTACT) {
  1206. $form['name'] = array('#type' => 'textfield', '#title' => t('Your name'), '#maxlength' => 60, '#size' => 30, '#default_value' => $edit['name'] ? $edit['name'] : variable_get('anonymous', t('Anonymous'))
  1207. );
  1208. $form['mail'] = array('#type' => 'textfield', '#title' => t('E-mail'), '#maxlength' => 64, '#size' => 30, '#default_value' => $edit['mail'], '#description' => t('The content of this field is kept private and will not be shown publicly.')
  1209. );
  1210. $form['homepage'] = array('#type' => 'textfield', '#title' => t('Homepage'), '#maxlength' => 255, '#size' => 30, '#default_value' => $edit['homepage']);
  1211. }
  1212. else if (variable_get('comment_anonymous_'. $node->type, COMMENT_ANONYMOUS_MAYNOT_CONTACT) == COMMENT_ANONYMOUS_MUST_CONTACT) {
  1213. $form['name'] = array('#type' => 'textfield', '#title' => t('Your name'), '#maxlength' => 60, '#size' => 30, '#default_value' => $edit['name'] ? $edit['name'] : variable_get('anonymous', t('Anonymous')), '#required' => TRUE);
  1214. $form['mail'] = array('#type' => 'textfield', '#title' => t('E-mail'), '#maxlength' => 64, '#size' => 30, '#default_value' => $edit['mail'], '#description' => t('The content of this field is kept private and will not be shown publicly.'), '#required' => TRUE);
  1215. $form['homepage'] = array('#type' => 'textfield', '#title' => t('Homepage'), '#maxlength' => 255, '#size' => 30, '#default_value' => $edit['homepage']);
  1216. }
  1217. if (variable_get('comment_subject_field_'. $node->type, 1) == 1) {
  1218. $form['subject'] = array('#type' => 'textfield', '#title' => t('Subject'), '#maxlength' => 64, '#default_value' => !empty($edit['subject']) ? $edit['subject'] : '');
  1219. }
  1220. if (!empty($edit['comment'])) {
  1221. $default = $edit['comment'];
  1222. }
  1223. else {
  1224. $default = '';
  1225. }
  1226. $form['comment_filter']['comment'] = array(
  1227. '#type' => 'textarea',
  1228. '#title' => t('Comment'),
  1229. '#rows' => 15,
  1230. '#default_value' => $default,
  1231. '#required' => TRUE,
  1232. );
  1233. if (!isset($edit['format'])) {
  1234. $edit['format'] = FILTER_FORMAT_DEFAULT;
  1235. }
  1236. $form['comment_filter']['format'] = filter_form($edit['format']);
  1237. $form['cid'] = array('#type' => 'value', '#value' => !empty($edit['cid']) ? $edit['cid'] : NULL);
  1238. $form['pid'] = array('#type' => 'value', '#value' => !empty($edit['pid']) ? $edit['pid'] : NULL);
  1239. $form['nid'] = array('#type' => 'value', '#value' => $edit['nid']);
  1240. $form['uid'] = array('#type' => 'value', '#value' => !empty($edit['uid']) ? $edit['uid'] : 0);
  1241. // Only show save button if preview is optional or if we are in preview mode.
  1242. // We show the save button in preview mode even if there are form errors so that
  1243. // optional form elements (e.g., captcha) can be updated in preview mode.
  1244. if (!form_get_errors() && ((variable_get('comment_preview_'. $node->type, COMMENT_PREVIEW_REQUIRED) == COMMENT_PREVIEW_OPTIONAL) || ($op == t('Preview')) || ($op == t('Save')))) {
  1245. $form['submit'] = array('#type' => 'submit', '#value' => t('Save'), '#weight' => 19);
  1246. }
  1247. $form['preview'] = array('#type' => 'button', '#value' => t('Preview'), '#weight' => 20);
  1248. $form['#token'] = 'comment'. $edit['nid'] . (isset($edit['pid']) ? $edit['pid'] : '');
  1249. if ($op == t('Preview')) {
  1250. $form['#after_build'] = array('comment_form_add_preview');
  1251. }
  1252. if (empty($edit['cid']) && empty($edit['pid'])) {
  1253. $form['#action'] = url('comment/reply/'. $edit['nid']);
  1254. }
  1255. return $form;
  1256. }
  1257. /**
  1258. * Theme the comment form box.
  1259. *
  1260. * @param $edit
  1261. * The form structure.
  1262. * @param $title
  1263. * The form title.
  1264. */
  1265. function comment_form_box($edit, $title = NULL) {
  1266. return theme('box', $title, drupal_get_form('comment_form', $edit, $title));
  1267. }
  1268. /**
  1269. * Form builder; Generate and validate a comment preview form.
  1270. *
  1271. * @ingroup forms
  1272. */
  1273. function comment_form_add_preview($form, &$form_state) {
  1274. global $user;
  1275. $edit = $form_state['values'];
  1276. drupal_set_title(t('Preview comment'));
  1277. $output = '';
  1278. $node = node_load($edit['nid']);
  1279. // Invoke full validation for the form, to protect against cross site
  1280. // request forgeries (CSRF) and setting arbitrary values for fields such as
  1281. // the input format. Preview the comment only when form validation does not
  1282. // set any errors.
  1283. drupal_validate_form($form['form_id']['#value'], $form, $form_state);
  1284. if (!form_get_errors()) {
  1285. _comment_form_submit($edit);
  1286. $comment = (object)$edit;
  1287. // Attach the user and time information.
  1288. if (!empty($edit['author'])) {
  1289. $account = user_load(array('name' => $edit['author']));
  1290. }
  1291. elseif ($user->uid && !isset($edit['is_anonymous'])) {
  1292. $account = $user;
  1293. }
  1294. if (!empty($account)) {
  1295. $comment->uid = $account->uid;
  1296. $comment->name = check_plain($account->name);
  1297. }
  1298. elseif (empty($comment->name)) {
  1299. $comment->name = variable_get('anonymous', t('Anonymous'));
  1300. }
  1301. $comment->timestamp = !empty($edit['timestamp']) ? $edit['timestamp'] : time();
  1302. $output .= theme('comment_view', $comment, $node);
  1303. }
  1304. $form['comment_preview'] = array(
  1305. '#value' => $output,
  1306. '#weight' => -100,
  1307. '#prefix' => '<div class="preview">',
  1308. '#suffix' => '</div>',
  1309. );
  1310. $output = '';
  1311. if ($edit['pid']) {
  1312. $comment = db_fetch_object(db_query('SELECT c.*, u.uid, u.name AS registered_name, u.signature, u.signature_format, u.picture, u.data FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.cid = %d AND c.status = %d', $edit['pid'], COMMENT_PUBLISHED));
  1313. $comment = drupal_unpack($comment);
  1314. $comment->name = $comment->uid ? $comment->registered_name : $comment->name;
  1315. $output .= theme('comment_view', $comment, $node);
  1316. }
  1317. else {
  1318. $suffix = empty($form['#suffix']) ? '' : $form['#suffix'];
  1319. $form['#suffix'] = $suffix . node_view($node);
  1320. $edit['pid'] = 0;
  1321. }
  1322. $form['comment_preview_below'] = array('#value' => $output, '#weight' => 100);
  1323. return $form;
  1324. }
  1325. /**
  1326. * Validate comment form submissions.
  1327. */
  1328. function comment_form_validate($form, &$form_state) {
  1329. global $user;
  1330. if ($user->uid === 0) {
  1331. foreach (array('name', 'homepage', 'mail') as $field) {
  1332. // Set cookie for 365 days.
  1333. if (isset($form_state['values'][$field])) {
  1334. setcookie('comment_info_'. $field, $form_state['values'][$field], time() + 31536000, '/');
  1335. }
  1336. }
  1337. }
  1338. comment_validate($form_state['values']);
  1339. }
  1340. /**
  1341. * Prepare a comment for submission.
  1342. *
  1343. * @param $comment_values
  1344. * An associative array containing the comment data.
  1345. */
  1346. function _comment_form_submit(&$comment_values) {
  1347. $comment_values += array('subject' => '');
  1348. if (!isset($comment_values['date'])) {
  1349. $comment_values['date'] = 'now';
  1350. }
  1351. $comment_values['timestamp'] = strtotime($comment_values['date']);
  1352. if (isset($comment_values['author'])) {
  1353. $account = user_load(array('name' => $comment_values['author']));
  1354. $comment_values['uid'] = $account->uid;
  1355. $comment_values['name'] = $comment_values['author'];
  1356. }
  1357. // Validate the comment's subject. If not specified, extract
  1358. // one from the comment's body.
  1359. if (trim($comment_values['subject']) == '') {
  1360. // The body may be in any format, so we:
  1361. // 1) Filter it into HTML
  1362. // 2) Strip out all HTML tags
  1363. // 3) Convert entities back to plain-text.
  1364. // Note: format is checked by check_markup().
  1365. $comment_values['subject'] = truncate_utf8(trim(decode_entities(strip_tags(check_markup($comment_values['comment'], $comment_values['format'])))), 29, TRUE);
  1366. // Edge cases where the comment body is populated only by HTML tags will
  1367. // require a default subject.
  1368. if ($comment_values['subject'] == '') {
  1369. $comment_values['subject'] = t('(No subject)');
  1370. }
  1371. }
  1372. }
  1373. /**
  1374. * Process comment form submissions; prepare the comment, store it, and set a redirection target.
  1375. */
  1376. function comment_form_submit($form, &$form_state) {
  1377. _comment_form_submit($form_state['values']);
  1378. if ($cid = comment_save($form_state['values'])) {
  1379. $node = node_load($form_state['values']['nid']);
  1380. // Add 1 to existing $node->comment count to include new comment being added.
  1381. $comment_count = $node->comment_count + 1;
  1382. $page = comment_new_page_count($comment_count, 1, $node);
  1383. $form_state['redirect'] = array('node/'. $node->nid, $page, "comment-$cid");
  1384. return;
  1385. }
  1386. }
  1387. /**
  1388. * Themes a single comment and related items.
  1389. *
  1390. * @param $comment
  1391. * The comment object.
  1392. * @param $node
  1393. * The comment node.
  1394. * @param $links
  1395. * An associative array containing control links suitable for passing into
  1396. * theme_links(). These are generated by modules implementing hook_link() with
  1397. * $type='comment'. Typical examples are links for editing and deleting
  1398. * comments.
  1399. * @param $visible
  1400. * Switches between folded/unfolded view. If TRUE the comments are visible, if
  1401. * FALSE the comments are folded.
  1402. * @ingroup themeable
  1403. */
  1404. function theme_comment_view($comment, $node, $links = array(), $visible = TRUE) {
  1405. static $first_new = TRUE;
  1406. $output = '';
  1407. $comment->new = node_mark($comment->nid, $comment->timestamp);
  1408. if ($first_new && $comment->new != MARK_READ) {
  1409. // Assign the anchor only for the first new comment. This avoids duplicate
  1410. // id attributes on a page.
  1411. $first_new = FALSE;
  1412. $output .= "<a id=\"new\"></a>\n";
  1413. }
  1414. $output .= "<a id=\"comment-$comment->cid\"></a>\n";
  1415. // Switch to folded/unfolded view of the comment
  1416. if ($visible) {
  1417. $comment->comment = check_markup($comment->comment, $comment->format, FALSE);
  1418. // Comment API hook
  1419. comment_invoke_comment($comment, 'view');
  1420. $output .= theme('comment', $comment, $node, $links);
  1421. }
  1422. else {
  1423. $output .= theme('comment_folded', $comment);
  1424. }
  1425. return $output;
  1426. }
  1427. /**
  1428. * Build a comment control form.
  1429. *
  1430. * @param $mode
  1431. * Comment display mode.
  1432. * @param $order
  1433. * Comment order mode.
  1434. * @param $comments_per_page
  1435. * Comments per page.
  1436. * @ingroup forms
  1437. */
  1438. function comment_controls(&$form_state, $mode = COMMENT_MODE_THREADED_EXPANDED, $order = COMMENT_ORDER_NEWEST_FIRST, $comments_per_page = 50) {
  1439. $form['mode'] = array('#type' => 'select',
  1440. '#default_value' => $mode,
  1441. '#options' => _comment_get_modes(),
  1442. '#weight' => 1,
  1443. );
  1444. $form['order'] = array(
  1445. '#type' => 'select',
  1446. '#default_value' => $order,
  1447. '#options' => _comment_get_orders(),
  1448. '#weight' => 2,
  1449. );
  1450. foreach (_comment_per_page() as $i) {
  1451. $options[$i] = t('!a comments per page', array('!a' => $i));
  1452. }
  1453. $form['comments_per_page'] = array('#type' => 'select',
  1454. '#default_value' => $comments_per_page,
  1455. '#options' => $options,
  1456. '#weight' => 3,
  1457. );
  1458. $form['submit'] = array('#type' => 'submit',
  1459. '#value' => t('Save settings'),
  1460. '#weight' => 20,
  1461. );
  1462. return $form;
  1463. }
  1464. /**
  1465. * Theme comment controls box where the user can change the default display mode and display order of comments.
  1466. *
  1467. * @param $form
  1468. * The form structure.
  1469. * @ingroup themeable
  1470. */
  1471. function theme_comment_controls($form) {
  1472. $output = '<div class="container-inline">';
  1473. $output .= drupal_render($form);
  1474. $output .= '</div>';
  1475. $output .= '<div class="description">'. t('Select your preferred way to display the comments and click "Save settings" to activate your changes.') .'</div>';
  1476. return theme('box', t('Comment viewing options'), $output);
  1477. }
  1478. /**
  1479. * Process comment_controls form submissions.
  1480. */
  1481. function comment_controls_submit($form, &$form_state) {
  1482. global $user;
  1483. $mode = $form_state['values']['mode'];
  1484. $order = $form_state['values']['order'];
  1485. $comments_per_page = $form_state['values']['comments_per_page'];
  1486. if ($user->uid) {
  1487. $account = user_save($user, array('mode' => $mode, 'sort' => $order, 'comments_per_page' => $comments_per_page));
  1488. // Terminate if an error occured during user_save().
  1489. if (!$account) {
  1490. drupal_set_message(t("Error saving user account."), 'error');
  1491. return;
  1492. }
  1493. $user = $account;
  1494. }
  1495. else {
  1496. $_SESSION['comment_mode'] = $mode;
  1497. $_SESSION['comment_sort'] = $order;
  1498. $_SESSION['comment_comments_per_page'] = $comments_per_page;
  1499. }
  1500. }
  1501. /**
  1502. * Process variables for comment.tpl.php.
  1503. *
  1504. * @see comment.tpl.php
  1505. * @see theme_comment()
  1506. */
  1507. function template_preprocess_comment(&$variables) {
  1508. $comment = $variables['comment'];
  1509. $node = $variables['node'];
  1510. $variables['author'] = theme('username', $comment);
  1511. $variables['content'] = $comment->comment;
  1512. $variables['date'] = format_date($comment->timestamp);
  1513. $variables['links'] = isset($variables['links']) ? theme('links', $variables['links']) : '';
  1514. $variables['new'] = $comment->new ? t('new') : '';
  1515. $variables['picture'] = theme_get_setting('toggle_comment_user_picture') ? theme('user_picture', $comment) : '';
  1516. $variables['signature'] = $comment->signature;
  1517. $variables['submitted'] = theme('comment_submitted', $comment);
  1518. $variables['title'] = l($comment->subject, $_GET['q'], array('fragment' => "comment-$comment->cid"));
  1519. $variables['template_files'][] = 'comment-'. $node->type;
  1520. // set status to a string representation of comment->status.
  1521. if (isset($comment->preview)) {
  1522. $variables['status'] = 'comment-preview';
  1523. }
  1524. else {
  1525. $variables['status'] = ($comment->status == COMMENT_NOT_PUBLISHED) ? 'comment-unpublished' : 'comment-published';
  1526. }
  1527. }
  1528. /**
  1529. * Process variables for comment-folded.tpl.php.
  1530. *
  1531. * @see comment-folded.tpl.php
  1532. * @see theme_comment_folded()
  1533. */
  1534. function template_preprocess_comment_folded(&$variables) {
  1535. $comment = $variables['comment'];
  1536. $variables['author'] = theme('username', $comment);
  1537. $variables['date'] = format_date($comment->timestamp);
  1538. $variables['new'] = $comment->new ? t('new') : '';
  1539. $variables['title'] = l($comment->subject, comment_node_url() .'/'. $comment->cid, array('fragment' => "comment-$comment->cid"));
  1540. }
  1541. /**
  1542. * Theme comment flat collapsed view.
  1543. *
  1544. * @param $comment
  1545. * The comment to be themed.
  1546. * @param $node
  1547. * The comment node.
  1548. * @ingroup themeable
  1549. */
  1550. function theme_comment_flat_collapsed($comment, $node) {
  1551. return theme('comment_view', $comment, $node, '', 0);
  1552. }
  1553. /**
  1554. * Theme comment flat expanded view.
  1555. *
  1556. * @param $comment
  1557. * The comment to be themed.
  1558. * @param $node
  1559. * The comment node.
  1560. * @ingroup themeable
  1561. */
  1562. function theme_comment_flat_expanded($comment, $node) {
  1563. $links = module_invoke_all('link', 'comment', $comment, 0);
  1564. drupal_alter('link', $links, $node, $comment);
  1565. return theme('comment_view', $comment, $node, $links);
  1566. }
  1567. /**
  1568. * Theme comment thread collapsed view.
  1569. *
  1570. * @param $comment
  1571. * The comment to be themed.
  1572. * @param $node
  1573. * The comment node.
  1574. * @ingroup themeable
  1575. */
  1576. function theme_comment_thread_collapsed($comment, $node) {
  1577. return theme('comment_view', $comment, $node, '', 0);
  1578. }
  1579. /**
  1580. * Theme comment thread expanded view.
  1581. *
  1582. * @param $comment
  1583. * The comment to be themed.
  1584. * @param $node
  1585. * The comment node.
  1586. * @ingroup themeable
  1587. */
  1588. function theme_comment_thread_expanded($comment, $node) {
  1589. $links = module_invoke_all('link', 'comment', $comment, 0);
  1590. drupal_alter('link', $links, $node, $comment);
  1591. return theme('comment_view', $comment, $node, $links);
  1592. }
  1593. /**
  1594. * Theme a "you can't post comments" notice.
  1595. *
  1596. * @param $node
  1597. * The comment node.
  1598. * @ingroup themeable
  1599. */
  1600. function theme_comment_post_forbidden($node) {
  1601. global $user;
  1602. static $authenticated_post_comments;
  1603. if (!$user->uid) {
  1604. if (!isset($authenticated_post_comments)) {
  1605. // We only output any link if we are certain, that users get permission
  1606. // to post comments by logging in. We also locally cache this information.
  1607. $authenticated_post_comments = array_key_exists(DRUPAL_AUTHENTICATED_RID, user_roles(TRUE, 'post comments') + user_roles(TRUE, 'post comments without approval'));
  1608. }
  1609. if ($authenticated_post_comments) {
  1610. // We cannot use drupal_get_destination() because these links
  1611. // sometimes appear on /node and taxonomy listing pages.
  1612. if (variable_get('comment_form_location_'. $node->type, COMMENT_FORM_SEPARATE_PAGE) == COMMENT_FORM_SEPARATE_PAGE) {
  1613. $destination = 'destination='. rawurlencode("comment/reply/$node->nid#comment-form");
  1614. }
  1615. else {
  1616. $destination = 'destination='. rawurlencode("node/$node->nid#comment-form");
  1617. }
  1618. if (variable_get('user_register', 1)) {
  1619. // Users can register themselves.
  1620. return t('<a href="@login">Login</a> or <a href="@register">register</a> to post comments', array('@login' => url('user/login', array('query' => $destination)), '@register' => url('user/register', array('query' => $destination))));
  1621. }
  1622. else {
  1623. // Only admins can add new users, no public registration.
  1624. return t('<a href="@login">Login</a> to post comments', array('@login' => url('user/login', array('query' => $destination))));
  1625. }
  1626. }
  1627. }
  1628. }
  1629. /**
  1630. * Process variables for comment-wrapper.tpl.php.
  1631. *
  1632. * @see comment-wrapper.tpl.php
  1633. */
  1634. function template_preprocess_comment_wrapper(&$variables) {
  1635. // Provide contextual information.
  1636. $variables['display_mode'] = _comment_get_display_setting('mode', $variables['node']);
  1637. $variables['display_order'] = _comment_get_display_setting('sort', $variables['node']);
  1638. $variables['comment_controls_state'] = variable_get('comment_controls_'. $variables['node']->type, COMMENT_CONTROLS_HIDDEN);
  1639. $variables['template_files'][] = 'comment-wrapper-'. $variables['node']->type;
  1640. }
  1641. /**
  1642. * Theme a "Submitted by ..." notice.
  1643. *
  1644. * @param $comment
  1645. * The comment.
  1646. * @ingroup themeable
  1647. */
  1648. function theme_comment_submitted($comment) {
  1649. return t('Submitted by !username on @datetime.',
  1650. array(
  1651. '!username' => theme('username', $comment),
  1652. '@datetime' => format_date($comment->timestamp)
  1653. ));
  1654. }
  1655. /**
  1656. * Return an array of viewing modes for comment listings.
  1657. *
  1658. * We can't use a global variable array because the locale system
  1659. * is not initialized yet when the comment module is loaded.
  1660. */
  1661. function _comment_get_modes() {
  1662. return array(
  1663. COMMENT_MODE_FLAT_COLLAPSED => t('Flat list - collapsed'),
  1664. COMMENT_MODE_FLAT_EXPANDED => t('Flat list - expanded'),
  1665. COMMENT_MODE_THREADED_COLLAPSED => t('Threaded list - collapsed'),
  1666. COMMENT_MODE_THREADED_EXPANDED => t('Threaded list - expanded')
  1667. );
  1668. }
  1669. /**
  1670. * Return an array of viewing orders for comment listings.
  1671. *
  1672. * We can't use a global variable array because the locale system
  1673. * is not initialized yet when the comment module is loaded.
  1674. */
  1675. function _comment_get_orders() {
  1676. return array(
  1677. COMMENT_ORDER_NEWEST_FIRST => t('Date - newest first'),
  1678. COMMENT_ORDER_OLDEST_FIRST => t('Date - oldest first')
  1679. );
  1680. }
  1681. /**
  1682. * Return an array of "comments per page" settings from which the user
  1683. * can choose.
  1684. */
  1685. function _comment_per_page() {
  1686. return drupal_map_assoc(array(10, 30, 50, 70, 90, 150, 200, 250, 300));
  1687. }
  1688. /**
  1689. * Return a current comment display setting
  1690. *
  1691. * @param $setting
  1692. * can be one of these: 'mode', 'sort', 'comments_per_page'
  1693. * @param $node
  1694. * The comment node in question.
  1695. */
  1696. function _comment_get_display_setting($setting, $node) {
  1697. global $user;
  1698. if (isset($_GET[$setting])) {
  1699. $value = $_GET[$setting];
  1700. }
  1701. else {
  1702. // get the setting's site default
  1703. switch ($setting) {
  1704. case 'mode':
  1705. $default = variable_get('comment_default_mode_'. $node->type, COMMENT_MODE_THREADED_EXPANDED);
  1706. break;
  1707. case 'sort':
  1708. $default = variable_get('comment_default_order_'. $node->type, COMMENT_ORDER_NEWEST_FIRST);
  1709. break;
  1710. case 'comments_per_page':
  1711. $default = variable_get('comment_default_per_page_'. $node->type, 50);
  1712. }
  1713. if (variable_get('comment_controls_'. $node->type, COMMENT_CONTROLS_HIDDEN) == COMMENT_CONTROLS_HIDDEN) {
  1714. // if comment controls are disabled use site default
  1715. $value = $default;
  1716. }
  1717. else {
  1718. // otherwise use the user's setting if set
  1719. if (isset($user->$setting) && $user->$setting) {
  1720. $value = $user->$setting;
  1721. }
  1722. else if (isset($_SESSION['comment_'. $setting]) && $_SESSION['comment_'. $setting]) {
  1723. $value = $_SESSION['comment_'. $setting];
  1724. }
  1725. else {
  1726. $value = $default;
  1727. }
  1728. }
  1729. }
  1730. return $value;
  1731. }
  1732. /**
  1733. * Updates the comment statistics for a given node. This should be called any
  1734. * time a comment is added, deleted, or updated.
  1735. *
  1736. * The following fields are contained in the node_comment_statistics table.
  1737. * - last_comment_timestamp: the timestamp of the last comment for this node or the node create stamp if no comments exist for the node.
  1738. * - last_comment_name: the name of the anonymous poster for the last comment
  1739. * - last_comment_uid: the uid of the poster for the last comment for this node or the node authors uid if no comments exists for the node.
  1740. * - comment_count: the total number of approved/published comments on this node.
  1741. */
  1742. function _comment_update_node_statistics($nid) {
  1743. $count = db_result(db_query('SELECT COUNT(cid) FROM {comments} WHERE nid = %d AND status = %d', $nid, COMMENT_PUBLISHED));
  1744. // comments exist
  1745. if ($count > 0) {
  1746. $last_reply = db_fetch_object(db_query_range('SELECT cid, name, timestamp, uid FROM {comments} WHERE nid = %d AND status = %d ORDER BY cid DESC', $nid, COMMENT_PUBLISHED, 0, 1));
  1747. db_query("UPDATE {node_comment_statistics} SET comment_count = %d, last_comment_timestamp = %d, last_comment_name = '%s', last_comment_uid = %d WHERE nid = %d", $count, $last_reply->timestamp, $last_reply->uid ? '' : $last_reply->name, $last_reply->uid, $nid);
  1748. }
  1749. // no comments
  1750. else {
  1751. $node = db_fetch_object(db_query("SELECT uid, created FROM {node} WHERE nid = %d", $nid));
  1752. db_query("UPDATE {node_comment_statistics} SET comment_count = 0, last_comment_timestamp = %d, last_comment_name = '', last_comment_uid = %d WHERE nid = %d", $node->created, $node->uid, $nid);
  1753. }
  1754. }
  1755. /**
  1756. * Invoke a hook_comment() operation in all modules.
  1757. *
  1758. * @param &$comment
  1759. * A comment object.
  1760. * @param $op
  1761. * A string containing the name of the comment operation.
  1762. * @return
  1763. * The returned value of the invoked hooks.
  1764. */
  1765. function comment_invoke_comment(&$comment, $op) {
  1766. $return = array();
  1767. foreach (module_implements('comment') as $name) {
  1768. $function = $name .'_comment';
  1769. $result = $function($comment, $op);
  1770. if (isset($result) && is_array($result)) {
  1771. $return = array_merge($return, $result);
  1772. }
  1773. else if (isset($result)) {
  1774. $return[] = $result;
  1775. }
  1776. }
  1777. return $return;
  1778. }
  1779. /**
  1780. * Generate vancode.
  1781. *
  1782. * Consists of a leading character indicating length, followed by N digits
  1783. * with a numerical value in base 36. Vancodes can be sorted as strings
  1784. * without messing up numerical order.
  1785. *
  1786. * It goes:
  1787. * 00, 01, 02, ..., 0y, 0z,
  1788. * 110, 111, ... , 1zy, 1zz,
  1789. * 2100, 2101, ..., 2zzy, 2zzz,
  1790. * 31000, 31001, ...
  1791. */
  1792. function int2vancode($i = 0) {
  1793. $num = base_convert((int)$i, 10, 36);
  1794. $length = strlen($num);
  1795. return chr($length + ord('0') - 1) . $num;
  1796. }
  1797. /**
  1798. * Decode vancode back to an integer.
  1799. */
  1800. function vancode2int($c = '00') {
  1801. return base_convert(substr($c, 1), 36, 10);
  1802. }
  1803. /**
  1804. * Implementation of hook_hook_info().
  1805. */
  1806. function comment_hook_info() {
  1807. return array(
  1808. 'comment' => array(
  1809. 'comment' => array(
  1810. 'insert' => array(
  1811. 'runs when' => t('After saving a new comment'),
  1812. ),
  1813. 'update' => array(
  1814. 'runs when' => t('After saving an updated comment'),
  1815. ),
  1816. 'delete' => array(
  1817. 'runs when' => t('After deleting a comment')
  1818. ),
  1819. 'view' => array(
  1820. 'runs when' => t('When a comment is being viewed by an authenticated user')
  1821. ),
  1822. ),
  1823. ),
  1824. );
  1825. }
  1826. /**
  1827. * Implementation of hook_action_info().
  1828. */
  1829. function comment_action_info() {
  1830. return array(
  1831. 'comment_publish_action' => array(
  1832. 'description' => t('Publish comment'),
  1833. 'type' => 'comment',
  1834. 'configurable' => FALSE,
  1835. 'hooks' => array(
  1836. 'comment' => array('insert', 'update'),
  1837. ),
  1838. ),
  1839. 'comment_unpublish_action' => array(
  1840. 'description' => t('Unpublish comment'),
  1841. 'type' => 'comment',
  1842. 'configurable' => FALSE,
  1843. 'hooks' => array(
  1844. 'comment' => array('insert', 'update'),
  1845. )
  1846. ),
  1847. 'comment_unpublish_by_keyword_action' => array(
  1848. 'description' => t('Unpublish comment containing keyword(s)'),
  1849. 'type' => 'comment',
  1850. 'configurable' => TRUE,
  1851. 'hooks' => array(
  1852. 'comment' => array('insert', 'update'),
  1853. )
  1854. )
  1855. );
  1856. }
  1857. /**
  1858. * Action to publish a comment.
  1859. *
  1860. * @param $comment
  1861. * An optional comment object.
  1862. * @param $context
  1863. * Keyed array. Must contain the id of the comment if $comment is not passed.
  1864. *
  1865. * @ingroup actions
  1866. */
  1867. function comment_publish_action($comment, $context = array()) {
  1868. if (isset($comment->cid)) {
  1869. $cid = $comment->cid;
  1870. $subject = $comment->subject;
  1871. }
  1872. else {
  1873. $cid = $context['cid'];
  1874. $subject = db_result(db_query("SELECT subject FROM {comments} WHERE cid = %d", $cid));
  1875. }
  1876. db_query('UPDATE {comments} SET status = %d WHERE cid = %d', COMMENT_PUBLISHED, $cid);
  1877. watchdog('action', 'Published comment %subject.', array('%subject' => $subject));
  1878. }
  1879. /**
  1880. * Action to unpublish a comment.
  1881. *
  1882. * @param $comment
  1883. * An optional comment object.
  1884. * @param $context
  1885. * Keyed array. Must contain the id of the comment if $comment is not passed.
  1886. *
  1887. * @ingroup actions
  1888. */
  1889. function comment_unpublish_action($comment, $context = array()) {
  1890. if (isset($comment->cid)) {
  1891. $cid = $comment->cid;
  1892. $subject = $comment->subject;
  1893. }
  1894. else {
  1895. $cid = $context['cid'];
  1896. $subject = db_result(db_query("SELECT subject FROM {comments} WHERE cid = %d", $cid));
  1897. }
  1898. db_query('UPDATE {comments} SET status = %d WHERE cid = %d', COMMENT_NOT_PUBLISHED, $cid);
  1899. watchdog('action', 'Unpublished comment %subject.', array('%subject' => $subject));
  1900. }
  1901. /**
  1902. * Action to unpublish a comment if it contains a certain string.
  1903. *
  1904. * @param $comment
  1905. * A comment object.
  1906. * @param $context
  1907. * An array providing more information about the context of the call to this action.
  1908. * Unused here, since this action currently only supports the insert and update ops of
  1909. * the comment hook, both of which provide a complete $comment object.
  1910. *
  1911. * @ingroup actions
  1912. * @see comment_unpublish_by_keyword_action_form()
  1913. * @see comment_unpublish_by_keyword_action_submit()
  1914. */
  1915. function comment_unpublish_by_keyword_action($comment, $context) {
  1916. foreach ($context['keywords'] as $keyword) {
  1917. if (strpos($comment->comment, $keyword) !== FALSE || strpos($comment->subject, $keyword) !== FALSE) {
  1918. db_query('UPDATE {comments} SET status = %d WHERE cid = %d', COMMENT_NOT_PUBLISHED, $comment->cid);
  1919. watchdog('action', 'Unpublished comment %subject.', array('%subject' => $comment->subject));
  1920. break;
  1921. }
  1922. }
  1923. }
  1924. /**
  1925. * Form builder; Prepare a form for blacklisted keywords.
  1926. *
  1927. * @ingroup forms
  1928. * @see comment_unpublish_by_keyword_action()
  1929. * @see comment_unpublish_by_keyword_action_submit()
  1930. */
  1931. function comment_unpublish_by_keyword_action_form($context) {
  1932. $form['keywords'] = array(
  1933. '#title' => t('Keywords'),
  1934. '#type' => 'textarea',
  1935. '#description' => t('The comment will be unpublished if it contains any of the character sequences above. Use a comma-separated list of character sequences. Example: funny, bungee jumping, "Company, Inc.". Character sequences are case-sensitive.'),
  1936. '#default_value' => isset($context['keywords']) ? drupal_implode_tags($context['keywords']) : '',
  1937. );
  1938. return $form;
  1939. }
  1940. /**
  1941. * Process comment_unpublish_by_keyword_action_form form submissions.
  1942. *
  1943. * @see comment_unpublish_by_keyword_action()
  1944. */
  1945. function comment_unpublish_by_keyword_action_submit($form, $form_state) {
  1946. return array('keywords' => drupal_explode_tags($form_state['values']['keywords']));
  1947. }