comments_block.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /**
  3. * Display the latest related comments
  4. *
  5. * Generally used in a sidebar.
  6. *
  7. * @uses $vars['subtypes'] Object subtype string or array of subtypes
  8. * @uses $vars['owner_guid'] The owner of the content being commented on
  9. * @uses $vars['container_guid'] The container of the content being commented on
  10. * @uses $vars['limit'] The number of comments to display
  11. */
  12. $options = array(
  13. 'type' => 'object',
  14. 'subtype' => 'comment',
  15. 'limit' => elgg_extract('limit', $vars, 4),
  16. 'wheres' => array(),
  17. 'preload_owners' => true,
  18. 'distinct' => false,
  19. );
  20. $owner_guid = elgg_extract('owner_guid', $vars);
  21. $container_guid = elgg_extract('container_guid', $vars);
  22. $subtypes = elgg_extract('subtypes', $vars);
  23. if ($owner_guid || $container_guid || $subtypes) {
  24. $db_prefix = elgg_get_config('dbprefix');
  25. // Join on the entities table to check container subtype and/or owner
  26. $options['joins'] = array("JOIN {$db_prefix}entities ce ON e.container_guid = ce.guid");
  27. }
  28. // If owner is defined, view only the comments that have
  29. // been posted on objects owned by that user
  30. if ($owner_guid) {
  31. $options['wheres'][] = "ce.owner_guid = $owner_guid";
  32. }
  33. // If container is defined, view only the comments that have
  34. // been posted on objects placed inside that container
  35. if ($container_guid) {
  36. $options['wheres'][] = "ce.container_guid = $container_guid";
  37. }
  38. // If subtypes are defined, view only the comments that have been
  39. // posted on objects that belong to any of those subtypes
  40. if ($subtypes) {
  41. if (is_array($subtypes)) {
  42. $subtype_ids = array();
  43. foreach ($subtypes as $subtype) {
  44. $id = (int)get_subtype_id('object', $subtype);
  45. if ($id) {
  46. $subtype_ids[] = $id;
  47. }
  48. }
  49. if ($subtype_ids) {
  50. $subtype_string = implode(',', $subtype_ids);
  51. $options['wheres'][] = "ce.subtype IN ($subtype_string)";
  52. } else {
  53. // subtype ids do not exist so cannot display comments
  54. $options['wheres'][] = "1 = -1";
  55. }
  56. } else {
  57. $subtype_id = (int)get_subtype_id('object', $subtypes);
  58. $options['wheres'][] = "ce.subtype = $subtype_id";
  59. }
  60. }
  61. $title = elgg_echo('generic_comments:latest');
  62. $comments = elgg_get_entities($options);
  63. if ($comments) {
  64. $body = elgg_view('page/components/list', array(
  65. 'items' => $comments,
  66. 'pagination' => false,
  67. 'list_class' => 'elgg-latest-comments',
  68. 'full_view' => false,
  69. ));
  70. } else {
  71. $body = '<p>' . elgg_echo('generic_comment:none') . '</p>';
  72. }
  73. echo elgg_view_module('aside', $title, $body);