Elgg  Version 2.3
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  // @todo clean up now that private settings return null
42  // No, so see if its in the private data store.
43  $meta = $this->getPrivateSetting($name);
44  if ($meta) {
45  return $meta;
46  }
47 
48  // Can't find it, so return null
49  return null;
50  }
51 
59  public function get($name) {
60  elgg_deprecated_notice("Use -> instead of get()", 1.9);
61  return $this->__get($name);
62  }
63 
71  public function __set($name, $value) {
72  if (array_key_exists($name, $this->attributes)) {
73  // Check that we're not trying to change the guid!
74  if ((array_key_exists('guid', $this->attributes)) && ($name == 'guid')) {
75  return;
76  }
77 
78  $this->attributes[$name] = $value;
79  } else {
81  }
82  }
83 
92  public function set($name, $value) {
93  elgg_deprecated_notice("Use -> instead of set()", 1.9);
94  $this->__set($name, $value);
95 
96  return true;
97  }
98 
109  public function __unset($name) {
110  if (array_key_exists($name, $this->attributes)) {
111  parent::__unset($name);
112  } else {
113  $this->removePrivateSetting($name);
114  }
115  }
116 
129  public function __isset($name) {
130  if (array_key_exists($name, $this->attributes)) {
131  return parent::__isset($name);
132  } else {
133  $private_setting = $this->getPrivateSetting($name);
134  return !is_null($private_setting);
135  }
136  }
137 
145  public function setContext($context) {
146  return $this->setPrivateSetting('context', $context);
147  }
148 
155  public function getContext() {
156  return $this->getPrivateSetting('context');
157  }
158 
165  public function getTitle() {
166  $title = $this->getDisplayName();
167  if (!$title) {
168  $container = $this->getContainerEntity() ? : null;
169  $title = _elgg_services()->widgets->getNameById($this->handler, $this->getContext(), $container);
170  }
171  return $title;
172  }
173 
182  public function move($column, $rank) {
183  $options = array(
184  'type' => 'object',
185  'subtype' => 'widget',
186  'container_guid' => $this->container_guid,
187  'limit' => false,
188  'private_setting_name_value_pairs' => array(
189  array('name' => 'context', 'value' => $this->getContext()),
190  array('name' => 'column', 'value' => $column)
191  )
192  );
194  if (!$widgets) {
195  $this->column = (int)$column;
196  $this->order = 0;
197  return;
198  }
199 
200  usort($widgets, function($a, $b) {return (int) $a->order > (int) $b->order;});
201 
202  // remove widgets from inactive plugins
204  'context' => $this->context,
205  'container' => $this->getContainerEntity(),
206  ]);
207  $inactive_widgets = array();
208  foreach ($widgets as $index => $widget) {
209  if (!array_key_exists($widget->handler, $widget_types)) {
210  $inactive_widgets[] = $widget;
211  unset($widgets[$index]);
212  }
213  }
214 
215  $bottom_rank = count($widgets);
216  if ($column == $this->column) {
217  $bottom_rank--;
218  }
219 
220  if ($rank == 0) {
221  // top of the column
222  $this->order = reset($widgets)->order - 10;
223  } elseif ($rank == $bottom_rank) {
224  // bottom of the column of active widgets
225  $this->order = end($widgets)->order + 10;
226  } else {
227  // reorder widgets
228 
229  // remove the widget that's being moved from the array
230  foreach ($widgets as $index => $widget) {
231  if ($widget->guid == $this->guid) {
232  unset($widgets[$index]);
233  }
234  }
235 
236  // split the array in two and recombine with the moved widget in middle
237  $before = array_slice($widgets, 0, $rank);
238  array_push($before, $this);
239  $after = array_slice($widgets, $rank);
240  $widgets = array_merge($before, $after);
241  ksort($widgets);
242  $order = 0;
243  foreach ($widgets as $widget) {
244  $widget->order = $order;
245  $order += 10;
246  }
247  }
248 
249  // put inactive widgets at the bottom
250  if ($inactive_widgets) {
251  $bottom = 0;
252  foreach ($widgets as $widget) {
253  if ($widget->order > $bottom) {
254  $bottom = $widget->order;
255  }
256  }
257  $bottom += 10;
258  foreach ($inactive_widgets as $widget) {
259  $widget->order = $bottom;
260  $bottom += 10;
261  }
262  }
263 
264  $this->column = $column;
265  }
266 
282  public function saveSettings($params) {
283  if (!$this->canEdit()) {
284  return false;
285  }
286 
287  // plugin hook handlers should return true to indicate the settings have
288  // been saved so that default code does not run
289  $hook_params = array(
290  'widget' => $this,
291  'params' => $params
292  );
293  if (_elgg_services()->hooks->trigger('widget_settings', $this->handler, $hook_params, false) == true) {
294  return true;
295  }
296 
297  if (is_array($params) && count($params) > 0) {
298  foreach ($params as $name => $value) {
299  if (is_array($value)) {
300  // private settings cannot handle arrays
301  return false;
302  } else {
303  $this->$name = $value;
304  }
305  }
306  $this->save();
307  }
308 
309  return true;
310  }
311 }
$widgets
Definition: dashboard.php:10
$context
Definition: add.php:11
setContext($context)
Set the widget context.
Definition: ElggWidget.php:145
$widget_types
Definition: add_panel.php:16
move($column, $rank)
Move the widget.
Definition: ElggWidget.php:182
getContext()
Get the widget context.
Definition: ElggWidget.php:155
if($guid==elgg_get_logged_in_user_guid()) $name
Definition: delete.php:21
removePrivateSetting($name)
Removes private setting.
Definition: ElggEntity.php:709
saveSettings($params)
Saves the widget&#39;s settings.
Definition: ElggWidget.php:282
setPrivateSetting($name, $value)
Adds a private setting to this entity.
Definition: ElggEntity.php:675
getPrivateSetting($name)
Returns a private setting value.
Definition: ElggEntity.php:691
elgg_get_entities_from_private_settings(array $options=array())
Returns entities based upon private settings.
save()
Save an entity.
$value
Definition: longtext.php:42
$column
Definition: add.php:13
$widget
Definition: delete.php:9
__get($name)
Get a value from attributes or private settings.
Definition: ElggWidget.php:35
canEdit($user_guid=0)
Can a user edit this entity?
$title
Definition: save.php:22
$options
Elgg admin footer.
Definition: footer.php:6
$params
Definition: login.php:72
$container
Definition: delete.php:29
__set($name, $value)
Set an attribute or private setting value.
Definition: ElggWidget.php:71
__isset($name)
Test if property is set either as an attribute or private setting.
Definition: ElggWidget.php:129
elgg_deprecated_notice($msg, $dep_version, $backtrace_level=1)
Log a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:1098
_elgg_services(\Elgg\Di\ServiceProvider $services=null)
Get the global service provider.
Definition: autoloader.php:17
initializeAttributes()
Set subtype to widget.
Definition: ElggWidget.php:23
getDisplayName()
{}
Definition: ElggObject.php:179
elgg_get_widget_types($context="", $exact=false)
Get the widget types for a context.
Definition: widgets.php:140
__unset($name)
Unset a property from private settings or attribute.
Definition: ElggWidget.php:109
$index
Definition: gallery.php:49
getContainerEntity()
Get the container entity for this object.
getTitle()
Get the title of the widget.
Definition: ElggWidget.php:165
$comment container_guid
Definition: save.php:59