Navigation.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. <?php
  2. /**
  3. *
  4. * Cube Framework $Id$ g+9aR/HEfb0V/s3OWo8wHeJZf6sGDt6ooiPHFmHzoYk=
  5. *
  6. * @link http://codecu.be/framework
  7. * @copyright Copyright (c) 2015 CodeCube SRL
  8. * @license http://codecu.be/framework/license Commercial License
  9. *
  10. * @version 1.4
  11. */
  12. /**
  13. * navigation view helper
  14. */
  15. namespace Cube\View\Helper;
  16. use Cube\Navigation\AbstractContainer,
  17. Cube\Navigation\Page\AbstractPage,
  18. Cube\Permissions;
  19. class Navigation extends AbstractHelper
  20. {
  21. /**
  22. *
  23. * initial navigation object (should not modified)
  24. *
  25. * @var \Cube\Navigation\Page\AbstractPage
  26. */
  27. protected $_initialContainer;
  28. /**
  29. *
  30. * navigation object
  31. *
  32. * @var \Cube\Navigation\Page\AbstractPage
  33. */
  34. protected $_container;
  35. /**
  36. *
  37. * path where to search for navigation view partials
  38. *
  39. * @var string
  40. */
  41. protected $_path;
  42. /**
  43. *
  44. * the minimum depth from which the rendering will start
  45. * default = 0 - from first page
  46. *
  47. * @var integer
  48. */
  49. protected $_minDepth = 0;
  50. /**
  51. *
  52. * the maximum depth where the rendering will stop
  53. * default = 0 - until last page
  54. *
  55. * @var integer
  56. */
  57. protected $_maxDepth = 0;
  58. /**
  59. *
  60. * ACL object to use
  61. *
  62. * @var \Cube\Permissions\Acl
  63. */
  64. protected $_acl;
  65. /**
  66. *
  67. * ACL role to use
  68. *
  69. * @var string|\Cube\Permissions\RoleInterface
  70. */
  71. protected $_role;
  72. /**
  73. *
  74. * get the initial navigation object
  75. *
  76. * @return \Cube\Navigation\Page\AbstractPage
  77. * @throws \InvalidArgumentException
  78. */
  79. public function getInitialContainer()
  80. {
  81. if (!$this->_initialContainer instanceof AbstractContainer) {
  82. throw new \InvalidArgumentException(
  83. sprintf("'%s' must be of type \Cube\Navigation\AbstractContainer.", $this->_container));
  84. }
  85. return $this->_initialContainer;
  86. }
  87. /**
  88. *
  89. * set the initial navigation container
  90. *
  91. * @param \Cube\Navigation\Page\AbstractPage $container
  92. *
  93. * @throws \InvalidArgumentException
  94. * @return $this
  95. */
  96. public function setInitialContainer($container)
  97. {
  98. if ($container instanceof AbstractContainer) {
  99. $this->_initialContainer = $container;
  100. $this->setContainer($this->_initialContainer);
  101. }
  102. else {
  103. throw new \InvalidArgumentException(
  104. sprintf("'%s' must be of type \Cube\Navigation\AbstractContainer.", $this->_container));
  105. }
  106. return $this;
  107. }
  108. /**
  109. *
  110. * get the navigation object
  111. *
  112. * @return \Cube\Navigation\Page\AbstractPage
  113. * @throws \InvalidArgumentException
  114. */
  115. public function getContainer()
  116. {
  117. if (!$this->_container instanceof AbstractContainer) {
  118. throw new \InvalidArgumentException(
  119. sprintf("'%s' must be of type \Cube\Navigation\AbstractContainer.", $this->_container));
  120. }
  121. return $this->_container;
  122. }
  123. /**
  124. *
  125. * set the navigation container
  126. *
  127. * @param \Cube\Navigation\Page\AbstractPage $container
  128. *
  129. * @throws \InvalidArgumentException
  130. * @return $this
  131. */
  132. public function setContainer($container)
  133. {
  134. if ($container instanceof AbstractContainer) {
  135. $this->_container = $container;
  136. }
  137. else if ($container !== null) {
  138. throw new \InvalidArgumentException(
  139. sprintf("'%s' must be of type \Cube\Navigation\AbstractContainer.", $this->_container));
  140. }
  141. return $this;
  142. }
  143. /**
  144. *
  145. * reset container
  146. *
  147. * @param bool $initialContainer
  148. *
  149. * @return $this
  150. */
  151. public function resetContainer($initialContainer = true)
  152. {
  153. if ($initialContainer === true) {
  154. $this->_container = $this->getInitialContainer();
  155. }
  156. else {
  157. $this->_container = null;
  158. }
  159. return $this;
  160. }
  161. /**
  162. *
  163. * get navigation partials path
  164. *
  165. * @return string
  166. */
  167. public function getPath()
  168. {
  169. return $this->_path;
  170. }
  171. /**
  172. *
  173. * set navigation partials path
  174. *
  175. * @param string $path
  176. *
  177. * @return $this
  178. */
  179. public function setPath($path)
  180. {
  181. // if (!is_dir($path)) {
  182. // throw new \InvalidArgumentException(
  183. // sprintf("The navigation files path (%s) does not exist.", $path));
  184. // }
  185. if (is_dir($path)) {
  186. $this->_path = $path;
  187. }
  188. return $this;
  189. }
  190. /**
  191. *
  192. * get the minimum depth of the container
  193. *
  194. * @return integer
  195. */
  196. public function getMinDepth()
  197. {
  198. return $this->_minDepth;
  199. }
  200. /**
  201. *
  202. * set the minimum depth of the container
  203. *
  204. * @param int $minDepth
  205. *
  206. * @return $this
  207. */
  208. public function setMinDepth($minDepth)
  209. {
  210. $this->_minDepth = (int)$minDepth;
  211. return $this;
  212. }
  213. /**
  214. *
  215. * get the maximum depth of the container
  216. *
  217. * @return int
  218. */
  219. public function getMaxDepth()
  220. {
  221. return $this->_maxDepth;
  222. }
  223. /**
  224. *
  225. * set the maximum depth of the container
  226. *
  227. * @param int $maxDepth
  228. *
  229. * @return $this
  230. */
  231. public function setMaxDepth($maxDepth)
  232. {
  233. $this->_maxDepth = (int)$maxDepth;
  234. return $this;
  235. }
  236. /**
  237. *
  238. * get ACL
  239. *
  240. * @return \Cube\Permissions\Acl
  241. * @throws \InvalidArgumentException
  242. */
  243. public function getAcl()
  244. {
  245. if (!$this->_acl instanceof Permissions\Acl) {
  246. throw new \InvalidArgumentException(
  247. sprintf("'%s' must be of type \Cube\Permissions\Acl.", $this->_acl));
  248. }
  249. return $this->_acl;
  250. }
  251. /**
  252. *
  253. * set ACL
  254. *
  255. * @param \Cube\Permissions\Acl $acl
  256. *
  257. * @throws \InvalidArgumentException
  258. * @return $this
  259. */
  260. public function setAcl($acl)
  261. {
  262. if ($acl instanceof Permissions\Acl) {
  263. $this->_acl = $acl;
  264. }
  265. else {
  266. throw new \InvalidArgumentException(
  267. sprintf("'%s' must be of type \Cube\Permissions\Acl.", $this->_acl));
  268. }
  269. return $this;
  270. }
  271. /**
  272. *
  273. * get ACL role
  274. *
  275. * @return string|\Cube\Permissions\RoleInterface
  276. */
  277. public function getRole()
  278. {
  279. return $this->_role;
  280. }
  281. /**
  282. *
  283. * set ACL role
  284. *
  285. * @param string|\Cube\Permissions\RoleInterface $role
  286. *
  287. * @return $this
  288. * @throws \InvalidArgumentException
  289. */
  290. public function setRole($role)
  291. {
  292. if ($role === null || is_string($role) || $role instanceof Permissions\RoleInterface) {
  293. $this->_role = $role;
  294. }
  295. else {
  296. throw new \InvalidArgumentException(
  297. sprintf("'%s' must be null, a string, or an instance of type \Cube\Permissions\RoleInterface.",
  298. $this->_role));
  299. }
  300. return $this;
  301. }
  302. /**
  303. *
  304. * function called by the reflection class when creating the helper
  305. * we will always check if the navigation object and view file are set correctly when calling the navigation proxy class
  306. *
  307. * @param \Cube\Navigation\AbstractContainer $container the navigation object
  308. * @param string $partial the name of the view partial used for rendering the navigation object
  309. *
  310. * @return $this
  311. */
  312. public function navigation($container = null, $partial = null)
  313. {
  314. if ($container !== null) {
  315. $this->setContainer($container);
  316. $this->setPartial($partial);
  317. }
  318. return $this;
  319. }
  320. /**
  321. *
  322. * create a navigation menu from a navigation container and a view partial
  323. *
  324. * @return string the rendered menu
  325. */
  326. public function menu()
  327. {
  328. $view = $this->getView();
  329. $view->set('menu', $this->getContainer());
  330. return $view->process(
  331. $this->getPartial(), true);
  332. }
  333. /**
  334. *
  335. * find the active page in the container set in the helper
  336. *
  337. * @return \Cube\Navigation\Page\AbstractPage|null return the page object if found or null otherwise
  338. */
  339. public function findActive()
  340. {
  341. $container = $this->getContainer();
  342. if ($container->isActive()) {
  343. return $container;
  344. }
  345. $iterator = new \RecursiveIteratorIterator($container,
  346. \RecursiveIteratorIterator::CHILD_FIRST);
  347. /** @var \Cube\Navigation\Page\AbstractPage $page */
  348. foreach ($iterator as $page) {
  349. if ($page->isActive()) {
  350. return $page;
  351. }
  352. }
  353. return null;
  354. }
  355. /**
  356. *
  357. * checks if a page is accepted in the iteration
  358. * the method is to be called from the navigation view helper
  359. *
  360. * @param \Cube\Navigation\Page\AbstractPage $page
  361. * @param bool $recursive default true
  362. *
  363. * @return bool
  364. */
  365. public function accept(AbstractPage $page, $recursive = true)
  366. {
  367. $accept = true;
  368. if (!$this->_acceptAcl($page)) {
  369. $accept = false;
  370. }
  371. if ($accept && $recursive) {
  372. $parent = $page->getParent();
  373. if ($parent instanceof AbstractPage) {
  374. $accept = $this->accept($parent, true);
  375. }
  376. }
  377. return $accept;
  378. }
  379. /**
  380. *
  381. * check if a page is allowed by ACL
  382. *
  383. * rules:
  384. * - helper has no ACL, page is accepted
  385. * - page has a resource or privilege defined:
  386. * => the ACL allows access to it using the helper's role,
  387. * => [OBSOLETE] the ACL doesn't have the resource called in the page
  388. * => if the resource isn't in the ACL - page isn't accepted
  389. *
  390. * - if page has no resource or privilege, page is accepted
  391. *
  392. * @param \Cube\Navigation\Page\AbstractPage $page
  393. *
  394. * @return bool
  395. */
  396. protected function _acceptAcl(AbstractPage $page)
  397. {
  398. if (!$acl = $this->getAcl()) {
  399. return true;
  400. }
  401. $role = $this->getRole();
  402. $resource = $page->getResource();
  403. $privilege = $page->getPrivilege();
  404. if ($resource || $privilege) {
  405. if ($acl->hasResource($resource)) {
  406. return $acl->isAllowed($role, $resource, $privilege);
  407. }
  408. return false;
  409. }
  410. return true;
  411. }
  412. /**
  413. *
  414. * get active page breadcrumbs array
  415. *
  416. * @return array
  417. */
  418. public function getBreadcrumbs()
  419. {
  420. $breadcrumbs = array();
  421. $depth = 0;
  422. $page = $this->findActive();
  423. if ($page instanceof AbstractPage) {
  424. array_push($breadcrumbs, $page);
  425. while (($parent = $page->getParent()) instanceof AbstractPage) {
  426. array_push($breadcrumbs, $parent);
  427. $page = $parent;
  428. }
  429. $breadcrumbs = array_reverse($breadcrumbs);
  430. foreach ($breadcrumbs as $key => $page) {
  431. if ($depth < $this->_minDepth ||
  432. ($depth > $this->_maxDepth && $this->_maxDepth > 0)
  433. ) {
  434. unset($breadcrumbs[$key]);
  435. }
  436. $depth++;
  437. }
  438. }
  439. return $breadcrumbs;
  440. }
  441. /**
  442. *
  443. * create a breadcrumbs helper by getting the active page from the navigation container
  444. * and applying it to a breadcrumbs view partial
  445. * if no active page is found, return an empty display output
  446. *
  447. * @return string|null
  448. */
  449. public function breadcrumbs()
  450. {
  451. $breadcrumbs = $this->getBreadcrumbs();
  452. if (count($breadcrumbs) > 0) {
  453. $view = $this->getView();
  454. $view->set('breadcrumbs', $breadcrumbs);
  455. return $view->process(
  456. $this->getPartial(), true);
  457. }
  458. return null;
  459. }
  460. }