Elgg  Version 2.2
 All Classes Namespaces Files Functions Variables Pages
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() {
167  if (!$title) {
168  $title = _elgg_services()->widgets->getNameById($this->handler, $this->getContext(), $this->getContainerEntity());
169  }
170  return $title;
171  }
172 
181  public function move($column, $rank) {
182  $options = array(
183  'type' => 'object',
184  'subtype' => 'widget',
185  'container_guid' => $this->container_guid,
186  'limit' => false,
187  'private_setting_name_value_pairs' => array(
188  array('name' => 'context', 'value' => $this->getContext()),
189  array('name' => 'column', 'value' => $column)
190  )
191  );
193  if (!$widgets) {
194  $this->column = (int)$column;
195  $this->order = 0;
196  return;
197  }
198 
199  usort($widgets, create_function('$a,$b','return (int)$a->order > (int)$b->order;'));
200 
201  // remove widgets from inactive plugins
203  'context' => $this->context,
204  'container' => $this->getContainerEntity(),
205  ]);
206  $inactive_widgets = array();
207  foreach ($widgets as $index => $widget) {
208  if (!array_key_exists($widget->handler, $widget_types)) {
209  $inactive_widgets[] = $widget;
210  unset($widgets[$index]);
211  }
212  }
213 
214  $bottom_rank = count($widgets);
215  if ($column == $this->column) {
216  $bottom_rank--;
217  }
218 
219  if ($rank == 0) {
220  // top of the column
221  $this->order = reset($widgets)->order - 10;
222  } elseif ($rank == $bottom_rank) {
223  // bottom of the column of active widgets
224  $this->order = end($widgets)->order + 10;
225  } else {
226  // reorder widgets
227 
228  // remove the widget that's being moved from the array
229  foreach ($widgets as $index => $widget) {
230  if ($widget->guid == $this->guid) {
231  unset($widgets[$index]);
232  }
233  }
234 
235  // split the array in two and recombine with the moved widget in middle
236  $before = array_slice($widgets, 0, $rank);
237  array_push($before, $this);
238  $after = array_slice($widgets, $rank);
239  $widgets = array_merge($before, $after);
240  ksort($widgets);
241  $order = 0;
242  foreach ($widgets as $widget) {
243  $widget->order = $order;
244  $order += 10;
245  }
246  }
247 
248  // put inactive widgets at the bottom
249  if ($inactive_widgets) {
250  $bottom = 0;
251  foreach ($widgets as $widget) {
252  if ($widget->order > $bottom) {
253  $bottom = $widget->order;
254  }
255  }
256  $bottom += 10;
257  foreach ($inactive_widgets as $widget) {
258  $widget->order = $bottom;
259  $bottom += 10;
260  }
261  }
262 
263  $this->column = $column;
264  }
265 
281  public function saveSettings($params) {
282  if (!$this->canEdit()) {
283  return false;
284  }
285 
286  // plugin hook handlers should return true to indicate the settings have
287  // been saved so that default code does not run
288  $hook_params = array(
289  'widget' => $this,
290  'params' => $params
291  );
292  if (_elgg_services()->hooks->trigger('widget_settings', $this->handler, $hook_params, false) == true) {
293  return true;
294  }
295 
296  if (is_array($params) && count($params) > 0) {
297  foreach ($params as $name => $value) {
298  if (is_array($value)) {
299  // private settings cannot handle arrays
300  return false;
301  } else {
302  $this->$name = $value;
303  }
304  }
305  $this->save();
306  }
307 
308  return true;
309  }
310 }
$widgets
Definition: dashboard.php:10
$context
Definition: add.php:11
setContext($context)
Set the widget context.
Definition: ElggWidget.php:145
move($column, $rank)
Move the widget.
Definition: ElggWidget.php:181
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:690
saveSettings($params)
Saves the widget's settings.
Definition: ElggWidget.php:281
setPrivateSetting($name, $value)
Adds a private setting to this entity.
Definition: ElggEntity.php:656
getPrivateSetting($name)
Returns a private setting value.
Definition: ElggEntity.php:672
elgg_get_entities_from_private_settings(array $options=array())
Returns entities based upon private settings.
save()
Save an entity.
$value
Definition: longtext.php:26
$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
__set($name, $value)
Set an attribute or private setting value.
Definition: ElggWidget.php:71
set($name, $value)
Override entity get and sets in order to save data to private data store.
Definition: ElggWidget.php:92
__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:1070
$widget_types
Definition: widget.php:18
_elgg_services(\Elgg\Di\ServiceProvider $services=null)
Get the global service provider.
Definition: autoloader.php:17
$comment container_guid
Definition: save.php:59
initializeAttributes()
Set subtype to widget.
Definition: ElggWidget.php:23
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
getContainerEntity()
Get the container entity for this object.
getTitle()
Get the title of the widget.
Definition: ElggWidget.php:165