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 
111  $owner = get_entity($owner_guid);
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 }
$default
Definition: checkbox.php:30
elgg_get_default_access(\ElggUser $user=null, array $input_params=[])
Gets the default access permission.
Definition: access.php:56
$user_guid
Definition: login_as.php:10
$context
Definition: add.php:8
canEditLayout(string $context, int $user_guid=0)
Can the user edit the widget layout.
$params
Saves global plugin settings.
Definition: save.php:13
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
$owner
Definition: upload.php:7
getAllTypes()
Get all widgets.
if(empty($page_owner)||$owner->guid!==$page_owner->guid) $widgets
Definition: widgets.php:40
c Accompany it with the information you received as to the offer to distribute corresponding source complete source code means all the source code for all modules it plus any associated interface definition plus the scripts used to control compilation and installation of the executable as a special the source code distributed need not include anything that is normally and so on of the operating system on which the executable unless that component itself accompanies the executable If distribution of executable or object code is made by offering access to copy from a designated then offering equivalent access to copy the source code from the same place counts as distribution of the source even though third parties are not compelled to copy the source along with the object code You may not or distribute the Program except as expressly provided under this License Any attempt otherwise to sublicense or distribute the Program is void
Definition: LICENSE.txt:215
unregisterType(string $id)
Remove a widget type.
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_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:254
Widgets service.
$owner_guid
getTypes(array $params=[])
Returns the registered widget types.
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
createWidget(int $owner_guid, string $handler, string $context, int $access_id=null)
Create a new widget instance.
Exception indicating a user could not be looked up for a permissions check.
$user
Definition: ban.php:7
$container
Definition: delete.php:23
elgg_get_page_owner_entity()
Gets the owner entity for the current page.
Definition: pageowner.php:23
$page_owner
Definition: add.php:16
if($guid===false) $widget
Definition: add.php:31
registerType(WidgetDefinition $definition)
Register a widget type.
validateType(string $id, string $context=null,\ElggEntity $container=null)
Checks if a widget type exists for a given id.
getNameById(string $id, string $context= '',\ElggEntity $container=null)
Returns widget name based on id.
getWidgets(int $owner_guid, string $context)
Get widgets for a particular context.
$access_id
Definition: access.php:10
_elgg_services()
Get the global service provider.
Definition: elgglib.php:351
$handler
Definition: add.php:7
WidgetDefinition.
$id
Generic annotation delete action.
Definition: delete.php:6