Elgg  Version 4.x
ElggWidget.php
Go to the documentation of this file.
1 <?php
12 class ElggWidget extends \ElggObject {
13 
19  protected function initializeAttributes() {
20  parent::initializeAttributes();
21 
22  $this->attributes['subtype'] = 'widget';
23  }
24 
32  public function __get($name) {
33  // See if its in our base attribute
34  if (array_key_exists($name, $this->attributes)) {
35  return $this->attributes[$name];
36  }
37 
38  // object title and description are stored as metadata
39  if (in_array($name, ['title', 'description'])) {
40  return parent::__get($name);
41  }
42 
43  return $this->getPrivateSetting($name);
44  }
45 
54  public function __set($name, $value) {
55  if (array_key_exists($name, $this->attributes)) {
56  // Check that we're not trying to change the guid!
57  if ((array_key_exists('guid', $this->attributes)) && ($name == 'guid')) {
58  return;
59  }
60 
61  $this->attributes[$name] = $value;
62  return;
63  }
64 
65  // object title and description are stored as metadata
66  if (in_array($name, ['title', 'description'])) {
67  parent::__set($name, $value);
68  return;
69  }
70 
72  }
73 
86  public function __isset($name) {
87  if (array_key_exists($name, $this->attributes)) {
88  return parent::__isset($name);
89  }
90 
91  // object title and description are stored as metadata
92  if (in_array($name, ['title', 'description'])) {
93  return parent::__isset($name);
94  }
95 
96  return !is_null($this->getPrivateSetting($name));
97  }
98 
102  public function getDisplayName() {
103  $result = parent::getDisplayName();
104  if ($result) {
105  return $result;
106  }
107 
108  $container = $this->getContainerEntity() ? : null;
109  return _elgg_services()->widgets->getNameById($this->handler, (string) $this->context, $container) ?: (string) $this->handler;
110  }
111 
121  public function move($column, $rank) {
122  /* @var $widgets \ElggWidget[] */
124  'type' => 'object',
125  'subtype' => 'widget',
126  'container_guid' => $this->container_guid,
127  'limit' => false,
128  'private_setting_name_value_pairs' => [
129  ['name' => 'context', 'value' => (string) $this->context],
130  ['name' => 'column', 'value' => $column],
131  ],
132  ]);
133 
134  if (empty($widgets)) {
135  $this->column = (int) $column;
136  $this->order = 0;
137  return;
138  }
139 
140  usort($widgets, function($a, $b) {
141  return ((int) $a->order > (int) $b->order) ? 1 : -1;
142  });
143 
144  // remove widgets from inactive plugins
146  'context' => $this->context,
147  'container' => $this->getContainerEntity(),
148  ]);
149 
150  $inactive_widgets = [];
151  foreach ($widgets as $index => $widget) {
152  if (!array_key_exists($widget->handler, $widget_types)) {
153  $inactive_widgets[] = $widget;
154  unset($widgets[$index]);
155  }
156  }
157 
158  $bottom_rank = count($widgets);
159  if ($column == $this->column) {
160  $bottom_rank--;
161  }
162 
163  if ($rank == 0) {
164  // top of the column
165  $this->order = reset($widgets)->order - 10;
166  } elseif ($rank == $bottom_rank) {
167  // bottom of the column of active widgets
168  $this->order = end($widgets)->order + 10;
169  } else {
170  // reorder widgets
171 
172  // remove the widget that's being moved from the array
173  foreach ($widgets as $index => $widget) {
174  if ($widget->guid == $this->guid) {
175  unset($widgets[$index]);
176  }
177  }
178 
179  // split the array in two and recombine with the moved widget in middle
180  $before = array_slice($widgets, 0, $rank);
181  array_push($before, $this);
182  $after = array_slice($widgets, $rank);
183  $widgets = array_merge($before, $after);
184  ksort($widgets);
185  $order = 0;
186  foreach ($widgets as $widget) {
187  $widget->order = $order;
188  $order += 10;
189  }
190  }
191 
192  // put inactive widgets at the bottom
193  if ($inactive_widgets) {
194  $bottom = 0;
195  foreach ($widgets as $widget) {
196  if ($widget->order > $bottom) {
197  $bottom = $widget->order;
198  }
199  }
200  $bottom += 10;
201  foreach ($inactive_widgets as $widget) {
202  $widget->order = $bottom;
203  $bottom += 10;
204  }
205  }
206 
207  $this->column = $column;
208  }
209 
225  public function saveSettings($params) {
226  if (!$this->canEdit()) {
227  return false;
228  }
229 
230  // plugin hook handlers should return true to indicate the settings have
231  // been saved so that default code does not run
232  $hook_params = [
233  'widget' => $this,
234  'params' => $params
235  ];
236  if (_elgg_services()->hooks->trigger('widget_settings', $this->handler, $hook_params, false) === true) {
237  return true;
238  }
239 
240  if (is_array($params) && count($params) > 0) {
241  foreach ($params as $name => $value) {
242  if (is_array($value)) {
243  // private settings cannot handle arrays
244  return false;
245  } else {
246  $this->$name = $value;
247  }
248  }
249 
250  $this->save();
251  }
252 
253  return true;
254  }
255 }
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
$params
Saves global plugin settings.
Definition: save.php:13
$widget_types
Definition: add_panel.php:28
move($column, $rank)
Move the widget.
Definition: ElggWidget.php:121
saveSettings($params)
Saves the widget&#39;s settings.
Definition: ElggWidget.php:225
setPrivateSetting($name, $value)
Adds a private setting to this entity.
Definition: ElggEntity.php:608
getPrivateSetting($name)
Returns a private setting value.
Definition: ElggEntity.php:646
$column
Definition: add.php:10
__get($name)
Get a value from attributes, metadata or private settings.
Definition: ElggWidget.php:32
elgg_get_widget_types($context="")
Get the widget types for a context.
Definition: widgets.php:127
canEdit($user_guid=0)
Can a user edit this entity?
$value
Definition: generic.php:51
$container
Definition: delete.php:23
elgg_get_entities(array $options=[])
Fetches/counts entities or performs a calculation on their properties.
Definition: entities.php:545
__set($name, $value)
Set an attribute, metadata or private setting value.
Definition: ElggWidget.php:54
__isset($name)
Test if property is set either as an attribute, metadata or private setting.
Definition: ElggWidget.php:86
if($guid===false) $widget
Definition: add.php:30
$widgets
Definition: widgets.php:37
initializeAttributes()
Set subtype to widget.
Definition: ElggWidget.php:19
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:777
$index
Definition: gallery.php:40
getContainerEntity()
Get the container entity for this object.
if($entity instanceof\ElggComment) $comment container_guid
Definition: save.php:54