Elgg  Version 6.2
ElggWidget.php
Go to the documentation of this file.
1 <?php
10 class ElggWidget extends \ElggObject {
11 
17  protected function initializeAttributes() {
18  parent::initializeAttributes();
19 
20  $this->attributes['subtype'] = 'widget';
21  }
22 
26  public function getDisplayName(): string {
27  $result = parent::getDisplayName();
28  if ($result) {
29  return $result;
30  }
31 
32  $container = $this->getContainerEntity() ?: null;
33  return _elgg_services()->widgets->getNameById($this->handler, (string) $this->context, $container) ?: (string) $this->handler;
34  }
35 
45  public function move(int $column, int $rank): void {
46  /* @var $widgets \ElggWidget[] */
48  'type' => 'object',
49  'subtype' => 'widget',
50  'container_guid' => $this->container_guid,
51  'limit' => false,
52  'metadata_name_value_pairs' => [
53  ['name' => 'context', 'value' => (string) $this->context],
54  ['name' => 'column', 'value' => $column],
55  ],
56  ]);
57 
58  if (empty($widgets)) {
59  $this->column = $column;
60  $this->order = 0;
61  return;
62  }
63 
64  usort($widgets, function($a, $b) {
65  return ((int) $a->order > (int) $b->order) ? 1 : -1;
66  });
67 
68  // remove widgets from inactive plugins
70  'context' => $this->context,
71  'container' => $this->getContainerEntity(),
72  ]);
73 
74  $inactive_widgets = [];
75  foreach ($widgets as $index => $widget) {
76  if (!array_key_exists($widget->handler, $widget_types)) {
77  $inactive_widgets[] = $widget;
78  unset($widgets[$index]);
79  }
80  }
81 
82  $bottom_rank = count($widgets);
83  if ($column == $this->column) {
84  $bottom_rank--;
85  }
86 
87  if ($rank === -1) {
88  $rank = $bottom_rank;
89  }
90 
91  if ($rank === 0) {
92  // top of the column
93  $this->order = !empty($widgets) ? reset($widgets)->order - 10 : 0;
94  } elseif ($rank === $bottom_rank) {
95  // bottom of the column of active widgets
96  $this->order = !empty($widgets) ? end($widgets)->order + 10 : 10;
97  } else {
98  // reorder widgets
99 
100  // remove the widget that's being moved from the array
101  foreach ($widgets as $index => $widget) {
102  if ($widget->guid === $this->guid) {
103  unset($widgets[$index]);
104  }
105  }
106 
107  // split the array in two and recombine with the moved widget in middle
108  $before = array_slice($widgets, 0, $rank);
109  $before[] = $this;
110  $after = array_slice($widgets, $rank);
111  $widgets = array_merge($before, $after);
112  ksort($widgets);
113  $order = 0;
114  foreach ($widgets as $widget) {
115  $widget->order = $order;
116  $order += 10;
117  }
118  }
119 
120  // put inactive widgets at the bottom
121  if ($inactive_widgets) {
122  $bottom = 0;
123  foreach ($widgets as $widget) {
124  if ($widget->order > $bottom) {
125  $bottom = $widget->order;
126  }
127  }
128 
129  $bottom += 10;
130  foreach ($inactive_widgets as $widget) {
131  $widget->order = $bottom;
132  $bottom += 10;
133  }
134  }
135 
136  $this->column = $column;
137  }
138 }
if($entity instanceof \ElggComment) $comment container_guid
Definition: save.php:54
$container
Definition: delete.php:23
if($guid===false) $widget
Definition: add.php:31
$column
Definition: add.php:10
$widget_types
Definition: add_panel.php:33
getContainerEntity()
Get the container entity for this object.
Definition: ElggEntity.php:552
initializeAttributes()
Set subtype to widget.
Definition: ElggWidget.php:17
getDisplayName()
{Get the entity's display name.string The title or name of this entity.}
Definition: ElggWidget.php:26
move(int $column, int $rank)
Move the widget.
Definition: ElggWidget.php:45
$index
Definition: gallery.php:40
if($item instanceof \ElggEntity) elseif($item instanceof \ElggRiverItem) elseif($item instanceof \ElggRelationship) elseif(is_callable([ $item, 'getType']))
Definition: item.php:48
_elgg_services()
Get the global service provider.
Definition: elgglib.php:353
elgg_get_widget_types(string|array $context='')
Get the widget types for a context.
Definition: widgets.php:112
elgg_get_entities(array $options=[])
Fetches/counts entities or performs a calculation on their properties.
Definition: entities.php:507
if(empty($page_owner)|| $owner->guid !==$page_owner->guid) $widgets
Definition: widgets.php:40