PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/Comments/Controller/Component/CommentsComponent.php

https://github.com/kareypowell/croogo
PHP | 62 lines | 37 code | 6 blank | 19 comment | 2 complexity | 1b91b7a672af0b9dd386c22ee1010352 MD5 | raw file
  1. <?php
  2. App::uses('Component', 'Controller');
  3. /**
  4. * Comments Component
  5. *
  6. * @category Component
  7. * @package Croogo.Comments.Controller.Component
  8. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  9. * @link http://www.croogo.org
  10. */
  11. class CommentsComponent extends Component {
  12. /**
  13. * Initialize
  14. */
  15. public function initialize(Controller $controller) {
  16. $this->_setupEvents($controller);
  17. }
  18. /**
  19. * Setup Event handlers
  20. *
  21. * @return void
  22. */
  23. protected function _setupEvents(Controller $controller) {
  24. $callback = array($this, 'getCommentData');
  25. $eventManager = $controller->getEventManager();
  26. $eventManager->attach($callback, 'Controller.Nodes.view');
  27. }
  28. /**
  29. * Get comment data
  30. */
  31. public function getCommentData($event) {
  32. $controller = $event->subject;
  33. $alias = $controller->modelClass;
  34. $data = $event->data['data'];
  35. if ($data[$alias]['comment_count'] > 0) {
  36. $primaryKey = $controller->{$alias}->primaryKey;
  37. $comments = $controller->{$alias}->Comment->find('threaded', array(
  38. 'conditions' => array(
  39. 'Comment.model' => $alias,
  40. 'Comment.foreign_key' => $data[$alias][$primaryKey],
  41. 'Comment.status' => 1,
  42. ),
  43. 'contain' => array(
  44. 'User',
  45. ),
  46. 'cache' => array(
  47. 'name' => 'comment_node_' . $data[$alias][$primaryKey],
  48. 'config' => 'nodes_view',
  49. ),
  50. ));
  51. } else {
  52. $comments = array();
  53. }
  54. $controller->set(compact('comments'));
  55. }
  56. }