Elgg  Version master
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 }
$widget_types
Definition: add_panel.php:33
if(empty($page_owner)||$owner->guid!==$page_owner->guid) $widgets
Definition: widgets.php:40
c Accompany it with the information you received as to the offer to distribute corresponding source complete source code means all the source code for all modules it plus any associated interface definition plus the scripts used to control compilation and installation of the executable as a special the source code distributed need not include anything that is normally and so on of the operating system on which the executable unless that component itself accompanies the executable If distribution of executable or object code is made by offering access to copy from a designated then offering equivalent access to copy the source code from the same place counts as distribution of the source even though third parties are not compelled to copy the source along with the object code You may not or distribute the Program except as expressly provided under this License Any attempt otherwise to sublicense or distribute the Program is void
Definition: LICENSE.txt:215
$column
Definition: add.php:10
if($item instanceof\ElggEntity) elseif($item instanceof\ElggRiverItem) elseif($item instanceof\ElggRelationship) elseif(is_callable([$item, 'getType']))
Definition: item.php:48
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
$container
Definition: delete.php:23
if($guid===false) $widget
Definition: add.php:31
if($entity instanceof\ElggComment) $comment container_guid
Definition: save.php:54
initializeAttributes()
Set subtype to widget.
Definition: ElggWidget.php:17
_elgg_services()
Get the global service provider.
Definition: elgglib.php:351
move(int $column, int $rank)
Move the widget.
Definition: ElggWidget.php:45
$index
Definition: gallery.php:40
getContainerEntity()
Get the container entity for this object.
getDisplayName()
Definition: ElggWidget.php:26