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

/Nodes/Controller/Component/NodesComponent.php

https://github.com/kareypowell/croogo
PHP | 115 lines | 64 code | 11 blank | 40 comment | 5 complexity | bd771b2b132ab6b69b2e2bf66c1a34de MD5 | raw file
  1. <?php
  2. App::uses('Component', 'Controller');
  3. /**
  4. * Nodes Component
  5. *
  6. * @category Component
  7. * @package Croogo.Nodes.Controller.Component
  8. * @version 1.0
  9. * @author Fahad Ibnay Heylaal <contact@fahad19.com>
  10. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  11. * @link http://www.croogo.org
  12. */
  13. class NodesComponent extends Component {
  14. /**
  15. * Nodes for layout
  16. *
  17. * @var string
  18. * @access public
  19. */
  20. public $nodesForLayout = array();
  21. /**
  22. * initialize
  23. *
  24. * @param Controller $controller instance of controller
  25. */
  26. public function initialize(Controller $controller) {
  27. $this->controller = $controller;
  28. if (isset($controller->Node)) {
  29. $this->Node = $controller->Node;
  30. } else {
  31. $this->Node = ClassRegistry::init('Nodes.Node');
  32. }
  33. if (Configure::read('Access Control.multiRole')) {
  34. Configure::write('Acl.classname', 'Acl.HabtmDbAcl');
  35. App::uses('HabtmDbAcl', 'Acl.Controller/Component/Acl');
  36. $controller->Acl->adapter('HabtmDbAcl');
  37. $this->Node->User->bindModel(array(
  38. 'hasAndBelongsToMany' => array(
  39. 'Role' => array(
  40. 'className' => 'Users.Role',
  41. 'with' => 'Users.RolesUser',
  42. ),
  43. ),
  44. ), false);
  45. }
  46. }
  47. /**
  48. * Startup
  49. *
  50. * @param Controller $controller instance of controller
  51. * @return void
  52. */
  53. public function startup(Controller $controller) {
  54. if (!isset($controller->request->params['admin']) && !isset($controller->request->params['requested'])) {
  55. $this->nodes();
  56. }
  57. }
  58. /**
  59. * Nodes
  60. *
  61. * Nodes will be available in this variable in views: $nodes_for_layout
  62. *
  63. * @return void
  64. */
  65. public function nodes() {
  66. $roleId = $this->controller->Croogo->roleId();
  67. $nodes = $this->controller->Blocks->blocksData['nodes'];
  68. $_nodeOptions = array(
  69. 'find' => 'all',
  70. 'conditions' => array(
  71. 'Node.status' => 1,
  72. 'OR' => array(
  73. 'Node.visibility_roles' => '',
  74. 'Node.visibility_roles LIKE' => '%"' . $roleId . '"%',
  75. ),
  76. ),
  77. 'order' => 'Node.created DESC',
  78. 'limit' => 5,
  79. );
  80. foreach ($nodes as $alias => $options) {
  81. $options = Hash::merge($_nodeOptions, $options);
  82. $options['limit'] = str_replace('"', '', $options['limit']);
  83. $node = $this->Node->find($options['find'], array(
  84. 'conditions' => $options['conditions'],
  85. 'order' => $options['order'],
  86. 'limit' => $options['limit'],
  87. 'cache' => array(
  88. 'prefix' => 'nodes_' . $alias,
  89. 'config' => 'croogo_nodes',
  90. ),
  91. ));
  92. $this->nodesForLayout[$alias] = $node;
  93. }
  94. }
  95. /**
  96. * beforeRender
  97. *
  98. * @param object $controller instance of controller
  99. * @return void
  100. */
  101. public function beforeRender(Controller $controller) {
  102. $controller->set('nodes_for_layout', $this->nodesForLayout);
  103. }
  104. }