Elgg  Version 1.9
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 
106  public function setContext($context) {
107  return $this->setPrivateSetting('context', $context);
108  }
109 
116  public function getContext() {
117  return $this->getPrivateSetting('context');
118  }
119 
126  public function getTitle() {
128  if (!$title) {
129  $title = _elgg_services()->widgets->getNameByType($this->handler);
130  }
131  return $title;
132  }
133 
142  public function move($column, $rank) {
143  $options = array(
144  'type' => 'object',
145  'subtype' => 'widget',
146  'container_guid' => $this->container_guid,
147  'limit' => false,
148  'private_setting_name_value_pairs' => array(
149  array('name' => 'context', 'value' => $this->getContext()),
150  array('name' => 'column', 'value' => $column)
151  )
152  );
154  if (!$widgets) {
155  $this->column = (int)$column;
156  $this->order = 0;
157  return;
158  }
159 
160  usort($widgets, create_function('$a,$b','return (int)$a->order > (int)$b->order;'));
161 
162  // remove widgets from inactive plugins
163  $widget_types = elgg_get_widget_types($this->context);
164  $inactive_widgets = array();
165  foreach ($widgets as $index => $widget) {
166  if (!array_key_exists($widget->handler, $widget_types)) {
167  $inactive_widgets[] = $widget;
168  unset($widgets[$index]);
169  }
170  }
171 
172  $bottom_rank = count($widgets);
173  if ($column == $this->column) {
174  $bottom_rank--;
175  }
176 
177  if ($rank == 0) {
178  // top of the column
179  $this->order = reset($widgets)->order - 10;
180  } elseif ($rank == $bottom_rank) {
181  // bottom of the column of active widgets
182  $this->order = end($widgets)->order + 10;
183  } else {
184  // reorder widgets
185 
186  // remove the widget that's being moved from the array
187  foreach ($widgets as $index => $widget) {
188  if ($widget->guid == $this->guid) {
189  unset($widgets[$index]);
190  }
191  }
192 
193  // split the array in two and recombine with the moved widget in middle
194  $before = array_slice($widgets, 0, $rank);
195  array_push($before, $this);
196  $after = array_slice($widgets, $rank);
197  $widgets = array_merge($before, $after);
198  ksort($widgets);
199  $order = 0;
200  foreach ($widgets as $widget) {
201  $widget->order = $order;
202  $order += 10;
203  }
204  }
205 
206  // put inactive widgets at the bottom
207  if ($inactive_widgets) {
208  $bottom = 0;
209  foreach ($widgets as $widget) {
210  if ($widget->order > $bottom) {
211  $bottom = $widget->order;
212  }
213  }
214  $bottom += 10;
215  foreach ($inactive_widgets as $widget) {
216  $widget->order = $bottom;
217  $bottom += 10;
218  }
219  }
220 
221  $this->column = $column;
222  }
223 
239  public function saveSettings($params) {
240  if (!$this->canEdit()) {
241  return false;
242  }
243 
244  // plugin hook handlers should return true to indicate the settings have
245  // been saved so that default code does not run
246  $hook_params = array(
247  'widget' => $this,
248  'params' => $params
249  );
250  if (elgg_trigger_plugin_hook('widget_settings', $this->handler, $hook_params, false) == true) {
251  return true;
252  }
253 
254  if (is_array($params) && count($params) > 0) {
255  foreach ($params as $name => $value) {
256  if (is_array($value)) {
257  // private settings cannot handle arrays
258  return false;
259  } else {
260  $this->$name = $value;
261  }
262  }
263  $this->save();
264  }
265 
266  return true;
267  }
268 }
$context
Definition: add.php:11
setContext($context)
Set the widget context.
Definition: ElggWidget.php:106
move($column, $rank)
Move the widget.
Definition: ElggWidget.php:142
getContext()
Get the widget context.
Definition: ElggWidget.php:116
if($guid==elgg_get_logged_in_user_guid()) $name
Definition: delete.php:21
saveSettings($params)
Saves the widget&#39;s settings.
Definition: ElggWidget.php:239
setPrivateSetting($name, $value)
Adds a private setting to this entity.
Definition: ElggEntity.php:669
getPrivateSetting($name)
Returns a private setting value.
Definition: ElggEntity.php:685
elgg_get_entities_from_private_settings(array $options=array())
Returns entities based upon private settings.
save()
Save an entity.
$value
Definition: longtext.php:29
$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:24
$params
Definition: login.php:72
$options
Definition: index.php:14
__set($name, $value)
Set an attribute or private setting value.
Definition: ElggWidget.php:71
_elgg_services()
Definition: autoloader.php:14
elgg_trigger_plugin_hook($hook, $type, $params=null, $returnvalue=null)
Trigger a Plugin Hook and run all handler callbacks registered to that hook:type. ...
Definition: elgglib.php:925
elgg_deprecated_notice($msg, $dep_version, $backtrace_level=1)
Sends a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:1171
$widget_types
Definition: widget.php:18
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:119
$widgets
Definition: dashboard.php:10
getTitle()
Get the title of the widget.
Definition: ElggWidget.php:126
Elgg Object.
Definition: ElggObject.php:22
$comment container_guid
Definition: save.php:51