Elgg  Version 4.3
EntityNavigation.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Menus;
4 
6 
14 
22  public static function registerPreviousNext(\Elgg\Hook $hook) {
23  $entity = $hook->getEntityParam();
24  if (!$entity instanceof \ElggEntity) {
25  return;
26  }
27 
28  /* @var $return MenuItems */
29  $return = $hook->getValue();
30 
31  $options = [
32  'type' => $entity->getType(),
33  'subtype' => $entity->getSubtype(),
34  'container_guid' => $entity->container_guid,
35  'limit' => 1,
36  ];
37 
38  $previous = self::getPreviousMenuItem($entity, $options);
39  if ($previous instanceof \ElggMenuItem) {
40  $return[] = $previous;
41  }
42 
43  $next = self::getNextMenuItem($entity, $options);
44  if ($next instanceof \ElggMenuItem) {
45  $return[] = $next;
46  }
47 
48  return $return;
49  }
50 
59  protected static function getPreviousMenuItem(\ElggEntity $entity, array $base_options) {
60  $base_options['wheres'] = [
61  function (\Elgg\Database\QueryBuilder $qb, $main_alias) use ($entity) {
62  return $qb->merge([
63  $qb->compare("{$main_alias}.time_created", '<', $entity->time_created, ELGG_VALUE_INTEGER),
64  $qb->merge([
65  $qb->compare("{$main_alias}.time_created", '=', $entity->time_created, ELGG_VALUE_INTEGER),
66  $qb->compare("{$main_alias}.guid", '<', $entity->guid, ELGG_VALUE_GUID),
67  ], 'AND'),
68  ], 'OR');
69  },
70  ];
71  $base_options['order_by'] = [
72  new \Elgg\Database\Clauses\OrderByClause('time_created', 'DESC'),
73  new \Elgg\Database\Clauses\OrderByClause('guid', 'DESC'),
74  ];
75 
76  $previous = elgg_get_entities($base_options);
77  if (empty($previous)) {
78  return;
79  }
80 
81  $previous = $previous[0];
82  return \ElggMenuItem::factory([
83  'name' => 'previous',
84  'text' => elgg_echo('previous'),
85  'title' => $previous->getDisplayName(),
86  'href' => $previous->getUrl(),
87  'entity' => $previous,
88  'icon' => 'angle-double-left',
89  'link_class' => 'elgg-button elgg-button-outline',
90  'priority' => 100,
91  ]);
92  }
93 
102  protected static function getNextMenuItem(\ElggEntity $entity, array $base_options) {
103  $base_options['wheres'] = [
104  function (\Elgg\Database\QueryBuilder $qb, $main_alias) use ($entity) {
105  return $qb->merge([
106  $qb->compare("{$main_alias}.time_created", '>', $entity->time_created, ELGG_VALUE_INTEGER),
107  $qb->merge([
108  $qb->compare("{$main_alias}.time_created", '=', $entity->time_created, ELGG_VALUE_INTEGER),
109  $qb->compare("{$main_alias}.guid", '>', $entity->guid, ELGG_VALUE_GUID),
110  ], 'AND'),
111  ], 'OR');
112  },
113  ];
114  $base_options['order_by'] = [
115  new \Elgg\Database\Clauses\OrderByClause('time_created', 'ASC'),
116  new \Elgg\Database\Clauses\OrderByClause('guid', 'ASC'),
117  ];
118 
119  $next = elgg_get_entities($base_options);
120  if (empty($next)) {
121  return;
122  }
123 
124  $next = $next[0];
125  return \ElggMenuItem::factory([
126  'name' => 'next',
127  'text' => elgg_echo('next'),
128  'title' => $next->getDisplayName(),
129  'href' => $next->getUrl(),
130  'entity' => $next,
131  'icon_alt' => 'angle-double-right',
132  'link_class' => 'elgg-button elgg-button-outline',
133  'priority' => 800,
134  ]);
135  }
136 }
static registerPreviousNext(\Elgg\Hook $hook)
Register the previous/next menu items.
static getPreviousMenuItem(\ElggEntity $entity, array $base_options)
Get a menu item to an entity before the current entity.
const ELGG_VALUE_INTEGER
Value types.
Definition: constants.php:126
static getNextMenuItem(\ElggEntity $entity, array $base_options)
Get a menu item to an entity after the current entity.
const ELGG_VALUE_GUID
Definition: constants.php:128
elgg_echo($message_key, array $args=[], $language="")
Elgg language module Functions to manage language and translations.
Definition: languages.php:18
$options
Elgg admin footer.
Definition: footer.php:6
Models an event passed to hook handlers.
Definition: Hook.php:11
$entity
Definition: reset.php:8
elgg_get_entities(array $options=[])
Fetches/counts entities or performs a calculation on their properties.
Definition: entities.php:545
Register menu items to the entity_navigation menu.
$qb
Definition: queue.php:11