Elgg  Version 3.0
WidgetsService.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg;
4 
6 
19 
23  private $widgets = [];
24 
29  private $widgetCache = [];
30 
45  public function getWidgets($owner_guid, $context) {
46  $widget_cache_key = "$context-$owner_guid";
47 
48  if (isset($this->widgetCache[$widget_cache_key])) {
49  return $this->widgetCache[$widget_cache_key];
50  }
51 
53  'type' => 'object',
54  'subtype' => 'widget',
55  'owner_guid' => $owner_guid,
56  'private_setting_name' => 'context',
57  'private_setting_value' => $context,
58  'limit' => 0,
59  'preload_private_settings' => true,
60  ]);
61 
62  if (!$widgets) {
63  $this->widgetCache[$widget_cache_key] = [];
64  return [];
65  }
66 
67  $sorted_widgets = [];
68  foreach ($widgets as $widget) {
69  $widget_column = (int) $widget->column;
70 
71  if (!isset($sorted_widgets[$widget_column])) {
72  $sorted_widgets[$widget_column] = [];
73  }
74 
75  if (!isset($sorted_widgets[$widget_column][$widget->order])) {
76  $sorted_widgets[$widget_column][$widget->order] = [];
77  }
78 
79  $sorted_widgets[$widget_column][$widget->order][] = $widget;
80  }
81 
82  foreach ($sorted_widgets as $col => $orders) {
83  ksort($orders);
84  $sorted_col = [];
85 
86  foreach ($orders as $widgets) {
87  foreach ($widgets as $widget) {
88  $sorted_col[] = $widget;
89  }
90  }
91 
92  $sorted_widgets[$col] = $sorted_col;
93  }
94 
95  $this->widgetCache[$widget_cache_key] = $sorted_widgets;
96 
97  return $sorted_widgets;
98  }
99 
112  public function createWidget($owner_guid, $handler, $context, $access_id = null) {
113  if (empty($owner_guid) || empty($handler)) {
114  return false;
115  }
116 
118  if (!$owner) {
119  return false;
120  }
121  if (!$this->validateType($handler, $context, $owner)) {
122  return false;
123  }
124 
125  $widget = new \ElggWidget;
126  $widget->owner_guid = $owner_guid;
127  $widget->container_guid = $owner_guid;
128  $widget->access_id = isset($access_id) ? $access_id : get_default_access();
129 
130  if (!$widget->save()) {
131  return false;
132  }
133 
134  // private settings cannot be set until \ElggWidget saved
135  $widget->handler = $handler;
136  $widget->context = $context;
137 
138  return $widget->getGUID();
139  }
140 
151  public function canEditLayout($context, $user_guid = 0) {
152  try {
153  $user = _elgg_services()->entityTable->getUserForPermissionsCheck($user_guid);
154  } catch (UserFetchFailureException $e) {
155  return false;
156  }
157 
159  $default = false;
160 
161  if ($page_owner) {
162  $default = $page_owner->canEdit($user_guid);
163  } elseif ($user) {
164  $default = $user->isAdmin();
165  }
166 
167  $params = [
168  'user' => $user,
169  'context' => $context,
170  'page_owner' => $page_owner,
171  ];
172  return _elgg_services()->hooks->trigger('permissions_check', 'widget_layout', $params, $default);
173  }
174 
184  public function registerType(WidgetDefinition $definition) {
185  $id = $definition->id;
186  if (empty($id)) {
187  return false;
188  }
189 
190  $this->widgets[$id] = $definition;
191 
192  return true;
193  }
194 
204  public function unregisterType($id) {
205  if (!isset($this->widgets[$id])) {
206  return false;
207  }
208 
209  unset($this->widgets[$id]);
210  return true;
211  }
212 
224  public function validateType($id, $context = null, \ElggEntity $container = null) {
225  $types = $this->getTypes([
226  'context' => $context,
227  'container' => $container,
228  ]);
229  $found = array_key_exists($id, $types);
230 
231  if (!$found && ($context === null)) {
232  // Pre Elgg 2.2 this function returned true if a widget was registered regardless of context
233  $found = array_key_exists($id, $this->widgets);
234  }
235 
236  return $found;
237  }
238 
244  public function getAllTypes() {
245  return $this->widgets;
246  }
247 
259  public function getNameById($id, $context = '', \ElggEntity $container = null) {
260  $types = $this->getTypes([
261  'context' => $context,
262  'container' => $container,
263  ]);
264  if (isset($types[$id])) {
265  return $types[$id]->name;
266  }
267  return false;
268  }
269 
280  public function getTypes(array $params = []) {
281  $context = elgg_extract('context', $params, '');
282  if (!$context) {
284  $params['context'] = $context;
285  }
286 
287  $available_widgets = _elgg_services()->hooks->trigger('handlers', 'widgets', $params, $this->widgets);
288  if (!is_array($available_widgets)) {
289  return [];
290  }
291 
292  $widgets = [];
293  /* @var $widget_definition \Elgg\WidgetDefinition */
294  foreach ($available_widgets as $widget_definition) {
295  if (!($widget_definition instanceof WidgetDefinition)) {
296  continue;
297  }
298 
299  if (!in_array($context, $widget_definition->context)) {
300  continue;
301  }
302 
303  if (!$widget_definition->isValid()) {
304  continue;
305  }
306 
307  $widgets[$widget_definition->id] = $widget_definition;
308  }
309 
310  return $widgets;
311  }
312 }
$context
Definition: add.php:8
$params
Saves global plugin settings.
Definition: save.php:13
getAllTypes()
Get all widgets.
$access_id
Definition: access.php:11
if(!$enabled) if(PHP_SAPI!== 'cli')
Interates through each element of an array and calls callback a function.
validateType($id, $context=null,\ElggEntity $container=null)
Checks if a widget type exists for a given id.
getNameById($id, $context= '',\ElggEntity $container=null)
Returns widget name based on id.
WARNING: API IN FLUX.
$user_guid
Validate a user.
Definition: validate.php:6
unregisterType($id)
Remove a widget type.
$owner_guid
Configuration exception.
getTypes(array $params=[])
createWidget($owner_guid, $handler, $context, $access_id=null)
Create a new widget instance.
$id
River item delete action.
Definition: delete.php:6
$owner
Definition: crop.php:7
$container
Definition: delete.php:23
elgg_get_entities(array $options=[])
Fetches/counts entities or performs a calculation on their properties.
Definition: entities.php:545
canEditLayout($context, $user_guid=0)
Can the user edit the widget layout.
Exception indicating a user could not be looked up for a permissions check.
$user
Definition: ban.php:7
elgg_get_context()
Get the current context.
Definition: pageowner.php:201
elgg_get_page_owner_entity()
Gets the owner entity for the current page.
Definition: pageowner.php:65
getWidgets($owner_guid, $context)
Get widgets for a particular context.
$page_owner
Definition: add.php:15
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
$default
Definition: checkbox.php:35
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:1131
$widgets
Definition: widgets.php:35
registerType(WidgetDefinition $definition)
Register a widget type.
_elgg_services()
Get the global service provider.
Definition: elgglib.php:1292
$handler
Definition: add.php:7
WidgetDefinition.
get_default_access(ElggUser $user=null, array $input_params=[])
Gets the default access permission.
Definition: access.php:127
get_entity($guid)
Loads and returns an entity object from a guid.
Definition: entities.php:87