Elgg  Version master
WidgetsService.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg;
4 
6 
14 
18  private $widgets = [];
19 
24  private $widgetCache = [];
25 
40  public function getWidgets(int $owner_guid, string $context): array {
41  $widget_cache_key = "{$context}-{$owner_guid}";
42 
43  if (isset($this->widgetCache[$widget_cache_key])) {
44  return $this->widgetCache[$widget_cache_key];
45  }
46 
48  'type' => 'object',
49  'subtype' => 'widget',
50  'owner_guid' => $owner_guid,
51  'metadata_name' => 'context',
52  'metadata_value' => $context,
53  'limit' => false,
54  ]);
55 
56  if (!$widgets) {
57  $this->widgetCache[$widget_cache_key] = [];
58  return [];
59  }
60 
61  $sorted_widgets = [];
62  foreach ($widgets as $widget) {
63  $widget_column = (int) $widget->column;
64 
65  if (!isset($sorted_widgets[$widget_column])) {
66  $sorted_widgets[$widget_column] = [];
67  }
68 
69  if (!isset($sorted_widgets[$widget_column][$widget->order])) {
70  $sorted_widgets[$widget_column][$widget->order] = [];
71  }
72 
73  $sorted_widgets[$widget_column][$widget->order][] = $widget;
74  }
75 
76  foreach ($sorted_widgets as $col => $orders) {
77  ksort($orders);
78  $sorted_col = [];
79 
80  foreach ($orders as $widgets) {
81  foreach ($widgets as $widget) {
82  $sorted_col[] = $widget;
83  }
84  }
85 
86  $sorted_widgets[$col] = $sorted_col;
87  }
88 
89  $this->widgetCache[$widget_cache_key] = $sorted_widgets;
90 
91  return $sorted_widgets;
92  }
93 
106  public function createWidget(int $owner_guid, string $handler, string $context, ?int $access_id = null) {
107  if (empty($owner_guid) || empty($handler)) {
108  return false;
109  }
110 
112  if (!$owner) {
113  return false;
114  }
115 
116  if (!$this->validateType($handler, $context, $owner)) {
117  return false;
118  }
119 
120  $widget = new \ElggWidget();
121  $widget->owner_guid = $owner_guid;
122  $widget->container_guid = $owner_guid;
123  $widget->access_id = $access_id ?? elgg_get_default_access();
124 
125  if (!$widget->save()) {
126  return false;
127  }
128 
129  $widget->handler = $handler;
130  $widget->context = $context;
131 
132  return $widget->guid;
133  }
134 
145  public function canEditLayout(string $context, int $user_guid = 0): bool {
146  try {
147  $user = _elgg_services()->entityTable->getUserForPermissionsCheck($user_guid);
148  } catch (UserFetchFailureException $e) {
149  return false;
150  }
151 
153  $default = false;
154 
155  if ($page_owner) {
156  $default = $page_owner->canEdit($user_guid);
157  } elseif ($user) {
158  $default = $user->isAdmin();
159  }
160 
161  $params = [
162  'user' => $user,
163  'context' => $context,
164  'page_owner' => $page_owner,
165  ];
166  return _elgg_services()->events->triggerResults('permissions_check', 'widget_layout', $params, $default);
167  }
168 
178  public function registerType(WidgetDefinition $definition): void {
179  $this->widgets[$definition->id] = $definition;
180  }
181 
191  public function unregisterType(string $id): void {
192  unset($this->widgets[$id]);
193  }
194 
206  public function validateType(string $id, ?string $context = null, ?\ElggEntity $container = null): bool {
207  $types = $this->getTypes([
208  'context' => $context,
209  'container' => $container,
210  ]);
211  $found = array_key_exists($id, $types);
212 
213  if (!$found && ($context === null)) {
214  // Pre Elgg 2.2 this function returned true if a widget was registered regardless of context
215  $found = array_key_exists($id, $this->widgets);
216  }
217 
218  return $found;
219  }
220 
226  public function getAllTypes(): array {
227  return $this->widgets;
228  }
229 
241  public function getNameById(string $id, string $context = '', ?\ElggEntity $container = null): ?string {
242  $types = $this->getTypes([
243  'context' => $context,
244  'container' => $container,
245  ]);
246  if (isset($types[$id])) {
247  return $types[$id]->name;
248  }
249 
250  return null;
251  }
252 
266  public function getTypes(array $params = []): array {
267  $context = elgg_extract('context', $params, '');
268  if (!$context) {
270  $params['context'] = $context;
271  }
272 
273  $available_widgets = _elgg_services()->events->triggerResults('handlers', 'widgets', $params, $this->widgets);
274  if (!is_array($available_widgets)) {
275  return [];
276  }
277 
278  $widgets = [];
279  /* @var $widget_definition \Elgg\WidgetDefinition */
280  foreach ($available_widgets as $widget_definition) {
281  if (!$widget_definition instanceof WidgetDefinition) {
282  continue;
283  }
284 
285  if (!in_array($context, $widget_definition->context)) {
286  continue;
287  }
288 
289  if (!$widget_definition->isValid()) {
290  continue;
291  }
292 
293  $widgets[$widget_definition->id] = $widget_definition;
294  }
295 
296  return $widgets;
297  }
298 }
$id
Generic annotation delete action.
Definition: delete.php:6
$owner
Definition: upload.php:7
$container
Definition: delete.php:23
$params
Saves global plugin settings.
Definition: save.php:13
if($guid===false) $widget
Definition: add.php:31
$handler
Definition: add.php:7
$context
Definition: add.php:8
$page_owner
Definition: add.php:16
return[ 'admin/delete_admin_notices'=>['access'=> 'admin'], 'admin/menu/save'=>['access'=> 'admin'], 'admin/plugins/activate'=>['access'=> 'admin'], 'admin/plugins/activate_all'=>['access'=> 'admin'], 'admin/plugins/deactivate'=>['access'=> 'admin'], 'admin/plugins/deactivate_all'=>['access'=> 'admin'], 'admin/plugins/set_priority'=>['access'=> 'admin'], 'admin/security/security_txt'=>['access'=> 'admin'], 'admin/security/settings'=>['access'=> 'admin'], 'admin/security/regenerate_site_secret'=>['access'=> 'admin'], 'admin/site/cache/invalidate'=>['access'=> 'admin'], 'admin/site/flush_cache'=>['access'=> 'admin'], 'admin/site/icons'=>['access'=> 'admin'], 'admin/site/set_maintenance_mode'=>['access'=> 'admin'], 'admin/site/set_robots'=>['access'=> 'admin'], 'admin/site/theme'=>['access'=> 'admin'], 'admin/site/unlock_upgrade'=>['access'=> 'admin'], 'admin/site/settings'=>['access'=> 'admin'], 'admin/upgrade'=>['access'=> 'admin'], 'admin/upgrade/reset'=>['access'=> 'admin'], 'admin/user/ban'=>['access'=> 'admin'], 'admin/user/bulk/ban'=>['access'=> 'admin'], 'admin/user/bulk/delete'=>['access'=> 'admin'], 'admin/user/bulk/unban'=>['access'=> 'admin'], 'admin/user/bulk/validate'=>['access'=> 'admin'], 'admin/user/change_email'=>['access'=> 'admin'], 'admin/user/delete'=>['access'=> 'admin'], 'admin/user/login_as'=>['access'=> 'admin'], 'admin/user/logout_as'=>[], 'admin/user/makeadmin'=>['access'=> 'admin'], 'admin/user/resetpassword'=>['access'=> 'admin'], 'admin/user/removeadmin'=>['access'=> 'admin'], 'admin/user/unban'=>['access'=> 'admin'], 'admin/user/validate'=>['access'=> 'admin'], 'annotation/delete'=>[], 'avatar/upload'=>[], 'comment/save'=>[], 'diagnostics/download'=>['access'=> 'admin'], 'entity/chooserestoredestination'=>[], 'entity/delete'=>[], 'entity/mute'=>[], 'entity/restore'=>[], 'entity/subscribe'=>[], 'entity/trash'=>[], 'entity/unmute'=>[], 'entity/unsubscribe'=>[], 'login'=>['access'=> 'logged_out'], 'logout'=>[], 'notifications/mute'=>['access'=> 'public'], 'plugins/settings/remove'=>['access'=> 'admin'], 'plugins/settings/save'=>['access'=> 'admin'], 'plugins/usersettings/save'=>[], 'register'=>['access'=> 'logged_out', 'middleware'=>[\Elgg\Router\Middleware\RegistrationAllowedGatekeeper::class,],], 'river/delete'=>[], 'settings/notifications'=>[], 'settings/notifications/subscriptions'=>[], 'user/changepassword'=>['access'=> 'public'], 'user/requestnewpassword'=>['access'=> 'public'], 'useradd'=>['access'=> 'admin'], 'usersettings/save'=>[], 'widgets/add'=>[], 'widgets/delete'=>[], 'widgets/move'=>[], 'widgets/save'=>[],]
Definition: actions.php:73
$user
Definition: ban.php:7
Exception indicating a user could not be looked up for a permissions check.
Widgets service.
validateType(string $id, ?string $context=null, ?\ElggEntity $container=null)
Checks if a widget type exists for a given id.
canEditLayout(string $context, int $user_guid=0)
Can the user edit the widget layout.
getAllTypes()
Get all widgets.
createWidget(int $owner_guid, string $handler, string $context, ?int $access_id=null)
Create a new widget instance.
getWidgets(int $owner_guid, string $context)
Get widgets for a particular context.
getNameById(string $id, string $context='', ?\ElggEntity $container=null)
Returns widget name based on id.
unregisterType(string $id)
Remove a widget type.
getTypes(array $params=[])
Returns the registered widget types.
registerType(WidgetDefinition $definition)
Register a widget type.
$owner_guid
elgg_get_context()
Get the current context.
Definition: context.php:41
if($item instanceof \ElggEntity) elseif($item instanceof \ElggRiverItem) elseif($item instanceof \ElggRelationship) elseif(is_callable([ $item, 'getType']))
Definition: item.php:48
_elgg_services()
Get the global service provider.
Definition: elgglib.php:353
elgg_extract($key, $array, $default=null, bool $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:256
elgg_get_default_access(?\ElggUser $user=null, array $input_params=[])
Gets the default access permission.
Definition: access.php:56
get_entity(int $guid)
Loads and returns an entity object from a guid.
Definition: entities.php:70
elgg_get_entities(array $options=[])
Fetches/counts entities or performs a calculation on their properties.
Definition: entities.php:507
$default
Definition: checkbox.php:30
$user_guid
Definition: login_as.php:10
try
Definition: login_as.php:33
elgg_get_page_owner_entity()
Gets the owner entity for the current page.
Definition: pageowner.php:23
if(parse_url(elgg_get_site_url(), PHP_URL_PATH) !=='/') if(file_exists(elgg_get_root_path() . 'robots.txt'))
Set robots.txt.
Definition: robots.php:10
$access_id
Definition: access.php:10
if(empty($page_owner)|| $owner->guid !==$page_owner->guid) $widgets
Definition: widgets.php:40