Elgg  Version 2.3
WidgetsService.php
Go to the documentation of this file.
1 <?php
2 namespace Elgg;
3 
5 
18 
22  private $widgets = [];
23 
28  private $widgetCache = array();
29 
35  public function getWidgets($owner_guid, $context) {
36  $widget_cache_key = "$context-$owner_guid";
37 
38  if (isset($this->widgetCache[$widget_cache_key])) {
39  return $this->widgetCache[$widget_cache_key];
40  }
41 
42  $options = array(
43  'type' => 'object',
44  'subtype' => 'widget',
45  'owner_guid' => $owner_guid,
46  'private_setting_name' => 'context',
47  'private_setting_value' => $context,
48  'limit' => 0,
49  );
51  if (!$widgets) {
52  return array();
53  }
54 
55  $sorted_widgets = array();
56  foreach ($widgets as $widget) {
57  if (!isset($sorted_widgets[(int)$widget->column])) {
58  $sorted_widgets[(int)$widget->column] = array();
59  }
60  $sorted_widgets[(int)$widget->column][$widget->order] = $widget;
61  }
62 
63  foreach ($sorted_widgets as $col => $widgets) {
64  ksort($sorted_widgets[$col]);
65  }
66 
67  $this->widgetCache[$widget_cache_key] = $sorted_widgets;
68 
69  return $sorted_widgets;
70  }
71 
77  public function createWidget($owner_guid, $handler, $context, $access_id = null) {
78  if (empty($owner_guid) || empty($handler)) {
79  return false;
80  }
81 
83  if (!$owner) {
84  return false;
85  }
86  if (!$this->validateType($handler, $context, $owner)) {
87  return false;
88  }
89 
90  $widget = new \ElggWidget;
91  $widget->owner_guid = $owner_guid;
92  $widget->container_guid = $owner_guid; // @todo - will this work for group widgets?
93  if (isset($access_id)) {
94  $widget->access_id = $access_id;
95  } else {
96  $widget->access_id = get_default_access();
97  }
98 
99  if (!$widget->save()) {
100  return false;
101  }
102 
103  // private settings cannot be set until \ElggWidget saved
104  $widget->handler = $handler;
105  $widget->context = $context;
106 
107  return $widget->getGUID();
108  }
109 
115  public function canEditLayout($context, $user_guid = 0) {
116  try {
117  $user = _elgg_services()->entityTable->getUserForPermissionsCheck($user_guid);
118  } catch (UserFetchFailureException $e) {
119  return false;
120  }
121 
122  if ($user) {
123  $return = ($user->isAdmin() || (elgg_get_page_owner_guid() == $user->guid));
124  } else {
125  $return = false;
126  }
127 
128  $params = array(
129  'user' => $user,
130  'context' => $context,
131  'page_owner' => elgg_get_page_owner_entity(),
132  );
133  return _elgg_services()->hooks->trigger('permissions_check', 'widget_layout', $params, $return);
134  }
135 
145  public function registerType(WidgetDefinition $definition) {
146  if (!($definition instanceof WidgetDefinition)) {
147  return false;
148  }
149 
150  $id = $definition->id;
151  if (!$id) {
152  return false;
153  }
154 
155  $this->widgets[$id] = $definition;
156 
157  return true;
158  }
159 
167  public function unregisterType($id) {
168  if (isset($this->widgets[$id])) {
169  unset($this->widgets[$id]);
170  return true;
171  }
172  return false;
173  }
174 
186  public function validateType($id, $context = null, \ElggEntity $container = null) {
187  $types = $this->getTypes([
188  'context' => $context,
189  'container' => $container,
190  ]);
191  $found = array_key_exists($id, $types);
192 
193  if (!$found && ($context === null)) {
194  // Pre Elgg 2.2 this function returned true if a widget was registered regardless of context
195  $found = array_key_exists($id, $this->widgets);
196  }
197 
198  return $found;
199  }
200 
205  public function getAllTypes() {
206  return $this->widgets;
207  }
208 
221  public function getNameById($id, $context = '', \ElggEntity $container = null) {
222  $types = $this->getTypes([
223  'context' => $context,
224  'container' => $container,
225  ]);
226  if (isset($types[$id])) {
227  return $types[$id]->name;
228  }
229  return false;
230  }
231 
246  public function getTypes(array $params = []) {
247  $exact = (bool) elgg_extract('exact', $params, false);
248  $context = elgg_extract('context', $params, '');
249  if (!$context) {
251  $params['context'] = $context;
252  }
253 
254  $available_widgets = _elgg_services()->hooks->trigger('handlers', 'widgets', $params, $this->widgets);
255  if (!is_array($available_widgets)) {
256  return [];
257  }
258 
259  $widgets = [];
260  foreach ($available_widgets as $widget_definition) {
261  if (!($widget_definition instanceof WidgetDefinition)) {
262  continue;
263  }
264 
265  if ($exact) {
266  if (in_array($context, $widget_definition->context)) {
267  $widgets[$widget_definition->id] = $widget_definition;
268  }
269  } else {
270  if (in_array('all', $widget_definition->context) || in_array($context, $widget_definition->context)) {
271  $widgets[$widget_definition->id] = $widget_definition;
272  }
273  }
274  }
275 
276  return $widgets;
277  }
278 }
279 
$widgets
Definition: dashboard.php:10
$context
Definition: add.php:11
$e
Definition: metadata.php:12
elgg_get_entities_from_private_settings(array $options=array())
Returns entities based upon private settings.
elgg layout widgets elgg widgets
Definition: admin.css.php:1211
$widget
Definition: delete.php:9
$return
Definition: opendd.php:15
$exact
Definition: add_panel.php:13
validateType($id, $context=null,\ElggEntity $container=null)
Checks if a widget type exists for a given id.
$options
Elgg admin footer.
Definition: footer.php:6
getNameById($id, $context= '',\ElggEntity $container=null)
Returns widget name based on id.
$params
Definition: login.php:72
$owner_guid
Save menu items.
getTypes(array $params=[])
createWidget($owner_guid, $handler, $context, $access_id=null)
get_default_access(ElggUser $user=null, array $input_params=array())
Gets the default access permission.
Definition: access.php:118
$owner
Definition: crop.php:8
$container
Definition: delete.php:29
canEditLayout($context, $user_guid=0)
Exception indicating a user could not be looked up for a permissions check.
$user
Definition: ban.php:13
elgg_get_context()
Get the current context.
Definition: pageowner.php:204
elgg_get_page_owner_entity()
Gets the owner entity for the current page.
Definition: pageowner.php:56
getWidgets($owner_guid, $context)
_elgg_services(\Elgg\Di\ServiceProvider $services=null)
Get the global service provider.
Definition: autoloader.php:17
elgg_extract($key, $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:1375
registerType(WidgetDefinition $definition)
$handler
Definition: add.php:10
WidgetDefinition.
$user_guid
Avatar remove action.
Definition: remove.php:6
if(!$collection_name) $id
Definition: add.php:17
http free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:5
elgg_get_page_owner_guid($guid=0)
Gets the guid of the entity that owns the current page.
Definition: pageowner.php:22
get_entity($guid)
Loads and returns an entity object from a guid.
Definition: entities.php:204