Elgg  Version 3.0
ElggWidget.php
Go to the documentation of this file.
1 <?php
2 
16 class ElggWidget extends \ElggObject {
17 
23  protected function initializeAttributes() {
24  parent::initializeAttributes();
25 
26  $this->attributes['subtype'] = "widget";
27  }
28 
35  public function __get($name) {
36  // See if its in our base attribute
37  if (array_key_exists($name, $this->attributes)) {
38  return $this->attributes[$name];
39  }
40 
41  // object title and description are stored as metadata
42  if (in_array($name, ['title', 'description'])) {
43  return parent::__get($name);
44  }
45 
46  return $this->getPrivateSetting($name);
47  }
48 
56  public function __set($name, $value) {
57  if (array_key_exists($name, $this->attributes)) {
58  // Check that we're not trying to change the guid!
59  if ((array_key_exists('guid', $this->attributes)) && ($name == 'guid')) {
60  return;
61  }
62 
63  $this->attributes[$name] = $value;
64  return;
65  }
66 
67  // object title and description are stored as metadata
68  if (in_array($name, ['title', 'description'])) {
69  parent::__set($name, $value);
70  return;
71  }
72 
74  }
75 
88  public function __isset($name) {
89  if (array_key_exists($name, $this->attributes)) {
90  return parent::__isset($name);
91  }
92 
93  // object title and description are stored as metadata
94  if (in_array($name, ['title', 'description'])) {
95  return parent::__isset($name);
96  }
97 
98  $private_setting = $this->getPrivateSetting($name);
99  return !is_null($private_setting);
100  }
101 
109  public function setContext($context) {
110  $this->context = $context;
111  return true;
112  }
113 
120  public function getContext() {
121  return (string) $this->context;
122  }
123 
131  public function getTitle() {
132  _elgg_services()->deprecation->sendNotice(__METHOD__ . ' is deprecated. Use \ElggWidget::getDisplayName() instead', '3.0');
133  return $this->getDisplayName();
134  }
135 
139  public function getDisplayName() {
140  $result = parent::getDisplayName();
141  if (!$result) {
142  $container = $this->getContainerEntity() ? : null;
143  $result = _elgg_services()->widgets->getNameById($this->handler, $this->getContext(), $container);
144  }
145  return $result;
146  }
147 
156  public function move($column, $rank) {
157  $options = [
158  'type' => 'object',
159  'subtype' => 'widget',
160  'container_guid' => $this->container_guid,
161  'limit' => false,
162  'private_setting_name_value_pairs' => [
163  ['name' => 'context', 'value' => $this->getContext()],
164  ['name' => 'column', 'value' => $column]
165  ]
166  ];
167  /* @var $widgets \ElggWidget[] */
169  if (empty($widgets)) {
170  $this->column = (int) $column;
171  $this->order = 0;
172  return;
173  }
174 
175  usort($widgets, function($a, $b) {
176  return (int) $a->order > (int) $b->order;
177  });
178 
179  // remove widgets from inactive plugins
181  'context' => $this->context,
182  'container' => $this->getContainerEntity(),
183  ]);
184  $inactive_widgets = [];
185  foreach ($widgets as $index => $widget) {
186  if (!array_key_exists($widget->handler, $widget_types)) {
187  $inactive_widgets[] = $widget;
188  unset($widgets[$index]);
189  }
190  }
191 
192  $bottom_rank = count($widgets);
193  if ($column == $this->column) {
194  $bottom_rank--;
195  }
196 
197  if ($rank == 0) {
198  // top of the column
199  $this->order = reset($widgets)->order - 10;
200  } elseif ($rank == $bottom_rank) {
201  // bottom of the column of active widgets
202  $this->order = end($widgets)->order + 10;
203  } else {
204  // reorder widgets
205 
206  // remove the widget that's being moved from the array
207  foreach ($widgets as $index => $widget) {
208  if ($widget->guid == $this->guid) {
209  unset($widgets[$index]);
210  }
211  }
212 
213  // split the array in two and recombine with the moved widget in middle
214  $before = array_slice($widgets, 0, $rank);
215  array_push($before, $this);
216  $after = array_slice($widgets, $rank);
217  $widgets = array_merge($before, $after);
218  ksort($widgets);
219  $order = 0;
220  foreach ($widgets as $widget) {
221  $widget->order = $order;
222  $order += 10;
223  }
224  }
225 
226  // put inactive widgets at the bottom
227  if ($inactive_widgets) {
228  $bottom = 0;
229  foreach ($widgets as $widget) {
230  if ($widget->order > $bottom) {
231  $bottom = $widget->order;
232  }
233  }
234  $bottom += 10;
235  foreach ($inactive_widgets as $widget) {
236  $widget->order = $bottom;
237  $bottom += 10;
238  }
239  }
240 
241  $this->column = $column;
242  }
243 
259  public function saveSettings($params) {
260  if (!$this->canEdit()) {
261  return false;
262  }
263 
264  // plugin hook handlers should return true to indicate the settings have
265  // been saved so that default code does not run
266  $hook_params = [
267  'widget' => $this,
268  'params' => $params
269  ];
270  if (_elgg_services()->hooks->trigger('widget_settings', $this->handler, $hook_params, false) === true) {
271  return true;
272  }
273 
274  if (is_array($params) && count($params) > 0) {
275  foreach ($params as $name => $value) {
276  if (is_array($value)) {
277  // private settings cannot handle arrays
278  return false;
279  } else {
280  $this->$name = $value;
281  }
282  }
283  $this->save();
284  }
285 
286  return true;
287  }
288 }
$context
Definition: add.php:8
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
setContext($context)
Set the widget context.
Definition: ElggWidget.php:109
$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:156
getContext()
Get the widget context.
Definition: ElggWidget.php:120
saveSettings($params)
Saves the widget&#39;s settings.
Definition: ElggWidget.php:259
setPrivateSetting($name, $value)
Adds a private setting to this entity.
Definition: ElggEntity.php:589
getPrivateSetting($name)
Returns a private setting value.
Definition: ElggEntity.php:625
save()
Save an entity.
$column
Definition: add.php:10
__get($name)
Get a value from attributes, metadata or private settings.
Definition: ElggWidget.php:35
elgg_get_widget_types($context="")
Get the widget types for a context.
Definition: widgets.php:130
canEdit($user_guid=0)
Can a user edit this entity?
$options
Elgg admin footer.
Definition: footer.php:6
$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:56
__isset($name)
Test if property is set either as an attribute, metadata or private setting.
Definition: ElggWidget.php:88
if($item instanceof\ElggEntity) elseif($item instanceof\ElggRiverItem) elseif(is_callable([$item, 'getType']))
Definition: item.php:39
if($guid===false) $widget
Definition: add.php:30
$widgets
Definition: widgets.php:35
initializeAttributes()
Set subtype to widget.
Definition: ElggWidget.php:23
$value
Definition: debugging.php:7
_elgg_services()
Get the global service provider.
Definition: elgglib.php:1292
$index
Definition: gallery.php:47
$container_guid
getContainerEntity()
Get the container entity for this object.
getTitle()
Get the title of the widget.
Definition: ElggWidget.php:131