Boilerplate to use a Directus Instance to Build a Custom Website, Content will be Manage by Directus
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
2.5 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. <?php
  2. namespace App\Repositories;
  3. use App\Repositories\RepositoryAbstract;
  4. /**
  5. * request menu items from a menu
  6. *
  7. * @author Björn Hase, Tentakelfabrik
  8. * @license http://opensource.org/licenses/MIT The MIT License
  9. * @link https://gitea.tentakelfabrik.de/Tentakelfabrik/super-gear-directus
  10. *
  11. */
  12. class MenuRepository extends RepositoryAbstract
  13. {
  14. /** endpoint */
  15. protected $endpoint = 'menu_items';
  16. /**
  17. * find menu_items by name of menu
  18. * menu must be published
  19. *
  20. * @param string $slug
  21. * @return array
  22. */
  23. public function findByName($name)
  24. {
  25. $results = $this->queryBuilder
  26. ->fields([
  27. 'title',
  28. 'target',
  29. 'url',
  30. 'page.title',
  31. 'page.status',
  32. 'page.slug',
  33. 'menus.sort',
  34. 'menus.menus_id.name'
  35. ])
  36. ->aliases('page[title]', 'page_title')
  37. ->aliases('page[status]', 'page_status')
  38. ->aliases('page[slug]', 'page_slug')
  39. ->filter([
  40. '_or' => [
  41. [
  42. '_and' => [
  43. [ 'page' => [
  44. 'id' => [
  45. '_null' => 'true'
  46. ]
  47. ]],
  48. [ 'menus' => [
  49. 'menus_id' => [
  50. 'name' => $name,
  51. 'status' => 'published'
  52. ]
  53. ]]
  54. ]
  55. ],
  56. [
  57. '_and' => [
  58. [ 'page' => [
  59. 'status' => 'published'
  60. ]],
  61. [ 'menus' => [
  62. 'menus_id' => [
  63. 'name' => $name,
  64. 'status' => 'published'
  65. ]
  66. ]]
  67. ]
  68. ],
  69. ]
  70. ])
  71. ->find();
  72. // @TODO Workaround sort functions seems have problems with relationals fields
  73. if (count($results['data'])) {
  74. usort($results['data'], function($a, $b) {
  75. return ($a['menus'][0]['sort'] > $b['menus'][0]['sort']);
  76. });
  77. }
  78. return $results;
  79. }
  80. }