Elgg  Version 6.2
entities.php
Go to the documentation of this file.
1 <?php
10 
21 function elgg_get_entity_class(string $type, string $subtype): string {
22  return _elgg_services()->entityTable->getEntityClass($type, $subtype);
23 }
24 
44 function elgg_set_entity_class(string $type, string $subtype, string $class = ''): void {
45  _elgg_services()->entityTable->setEntityClass($type, $subtype, $class);
46 }
47 
59 function elgg_get_entity_as_row(int $guid): ?\stdClass {
60  return _elgg_services()->entityTable->getRow($guid);
61 }
62 
70 function get_entity(int $guid): ?\ElggEntity {
71  if ($guid === 1) {
72  return _elgg_services()->config->site;
73  }
74 
75  return _elgg_services()->entityTable->get($guid);
76 }
77 
91 function elgg_entity_exists(int $guid): bool {
92  return _elgg_services()->entityTable->exists($guid);
93 }
94 
102  return _elgg_services()->config->site;
103 }
104 
507 function elgg_get_entities(array $options = []) {
508  return \Elgg\Database\Entities::find($options);
509 }
510 
518 function elgg_count_entities(array $options = []): int {
519  $options['count'] = true;
520 
521  return (int) elgg_get_entities($options);
522 }
523 
551 function elgg_list_entities(array $options = [], ?callable $getter = null, ?callable $viewer = null): string {
552  $getter = $getter ?? 'elgg_get_entities'; // callables only support default NULL value
553  $viewer = $viewer ?? 'elgg_view_entity_list'; // callables only support default NULL value
554  $offset_key = $options['offset_key'] ?? 'offset';
555 
556  $defaults = [
557  'offset' => (int) max(get_input($offset_key, 0), 0),
558  'limit' => (int) max(get_input('limit', _elgg_services()->config->default_limit), 0),
559  'sort_by' => get_input('sort_by', []),
560  'full_view' => false,
561  'pagination' => true,
562  'no_results' => '',
563  'preload_owners' => true,
564  'preload_containers' => true,
565  ];
566 
567  $options = array_merge($defaults, $options);
568 
569  $options['register_rss_link'] = elgg_extract('register_rss_link', $options, elgg_extract('pagination', $options));
570  if ($options['register_rss_link']) {
572  }
573 
574  $options['count'] = false;
575  $entities = call_user_func($getter, $options);
576  $options['count'] = is_array($entities) ? count($entities) : 0;
577 
578  if (!is_array($entities) && $viewer === 'elgg_view_entity_list') {
579  _elgg_services()->logger->error(elgg_echo('list:error:getter:admin', [$getter, gettype($entities), $viewer]));
580 
581  return elgg_echo('list:error:getter:user');
582  }
583 
584  if (!empty($entities) || !empty($options['offset'])) {
585  $count_needed = true;
586  if (!$options['pagination']) {
587  $count_needed = false;
588  } elseif (!$options['offset'] && !$options['limit']) {
589  $count_needed = false;
590  } elseif (($options['count'] < (int) $options['limit']) && !$options['offset']) {
591  $count_needed = false;
592  }
593 
594  if ($count_needed) {
595  $options['count'] = true;
596 
597  $options['count'] = (int) call_user_func($getter, $options);
598  }
599  }
600 
601  return call_user_func($viewer, $entities, $options);
602 }
603 
616 function elgg_get_entity_dates(array $options = []): array {
617  return \Elgg\Database\Entities::with($options)->getDates();
618 }
619 
644 function elgg_search(array $options = []) {
645  try {
646  return _elgg_services()->search->search($options);
647  } catch (\Elgg\Exceptions\DomainException $e) {
648  return false;
649  }
650 }
651 
661 function elgg_get_entity_statistics(array $options = []): array {
662  $required = [
663  'selects' => [
664  'count(*) AS total',
665  ],
666  'group_by' => [
667  function(QueryBuilder $qb, $main_alias) {
668  return "{$main_alias}.type";
669  },
670  function(QueryBuilder $qb, $main_alias) {
671  return "{$main_alias}.subtype";
672  },
673  ],
674  'order_by' => [
675  new OrderByClause('total', 'desc'),
676  ],
677  'callback' => function($row) {
678  return (object) [
679  'type' => $row->type,
680  'subtype' => $row->subtype,
681  'total' => $row->total,
682  ];
683  },
684  'limit' => false,
685  ];
686 
687  $options = array_merge($options, $required);
688 
689  $rows = elgg_call(ELGG_IGNORE_ACCESS, function() use ($options) {
690  return elgg_get_entities($options);
691  });
692 
693  $entity_stats = [];
694  foreach ($rows as $row) {
695  $type = $row->type;
696  if (!isset($entity_stats[$type]) || !is_array($entity_stats[$type])) {
697  $entity_stats[$type] = [];
698  }
699 
700  $entity_stats[$type][$row->subtype] = $row->total;
701  }
702 
703  return $entity_stats;
704 }
705 
717 function elgg_entity_has_capability(string $type, string $subtype, string $capability, bool $default = false): bool {
718  return _elgg_services()->entity_capabilities->hasCapability($type, $subtype, $capability, $default);
719 }
720 
731 function elgg_entity_enable_capability(string $type, string $subtype, string $capability): void {
732  _elgg_services()->entity_capabilities->setCapability($type, $subtype, $capability, true);
733 }
734 
745 function elgg_entity_disable_capability(string $type, string $subtype, string $capability): void {
746  _elgg_services()->entity_capabilities->setCapability($type, $subtype, $capability, false);
747 }
748 
757 function elgg_entity_types_with_capability(string $capability): array {
758  return _elgg_services()->entity_capabilities->getTypesWithCapability($capability);
759 }
$guid
Reset an ElggUpgrade.
Definition: reset.php:6
$subtype
Definition: delete.php:22
$type
Definition: delete.php:21
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
$entity_stats
Definition: numentities.php:5
$class
Definition: summary.php:44
$getter
Extends QueryBuilder with ORDER BY clauses.
Entity table database service.
Definition: EntityTable.php:24
Database abstraction query builder.
Query builder for fetching data from the database.
Definition: Select.php:8
const ELGG_IGNORE_ACCESS
elgg_call() flags
Definition: constants.php:121
if($who_can_change_language==='nobody') elseif($who_can_change_language==='admin_only' &&!elgg_is_admin_logged_in()) $options
Definition: language.php:20
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_call(int $flags, Closure $closure)
Calls a callable autowiring the arguments using public DI services and applying logic based on flags.
Definition: elgglib.php:306
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
get_input(string $variable, $default=null, bool $filter_result=true)
Parameter input functions.
Definition: input.php:20
elgg_list_entities(array $options=[], ?callable $getter=null, ?callable $viewer=null)
Returns a string of rendered entities.
Definition: entities.php:551
get_entity(int $guid)
Loads and returns an entity object from a guid.
Definition: entities.php:70
elgg_entity_disable_capability(string $type, string $subtype, string $capability)
Disables the capability for a specified type/subtype.
Definition: entities.php:745
elgg_get_entity_as_row(int $guid)
Returns a database row from the entities table.
Definition: entities.php:59
elgg_search(array $options=[])
Returns search results as an array of entities, as a batch, or a count, depending on parameters given...
Definition: entities.php:644
elgg_count_entities(array $options=[])
Returns a count of entities.
Definition: entities.php:518
elgg_get_entities(array $options=[])
Fetches/counts entities or performs a calculation on their properties.
Definition: entities.php:507
elgg_entity_types_with_capability(string $capability)
Returns an array of type/subtypes with the requested capability enabled.
Definition: entities.php:757
elgg_set_entity_class(string $type, string $subtype, string $class='')
Sets class constructor name for entities with given type and subtype.
Definition: entities.php:44
elgg_get_entity_class(string $type, string $subtype)
Return the class name registered as a constructor for an entity of a given type and subtype.
Definition: entities.php:21
elgg_get_entity_statistics(array $options=[])
Return an array reporting the number of various entities in the system.
Definition: entities.php:661
elgg_get_entity_dates(array $options=[])
Returns a list of months in which entities were updated or created.
Definition: entities.php:616
elgg_get_site_entity()
Get the current site entity.
Definition: entities.php:101
elgg_entity_enable_capability(string $type, string $subtype, string $capability)
Enables the capability for a specified type/subtype.
Definition: entities.php:731
elgg_entity_has_capability(string $type, string $subtype, string $capability, bool $default=false)
Checks if a capability is enabled for a specified type/subtype.
Definition: entities.php:717
elgg_entity_exists(int $guid)
Does an entity exist?
Definition: entities.php:91
$defaults
Generic entity header upload helper.
Definition: header.php:6
$default
Definition: checkbox.php:30
$required
Definition: label.php:12
elgg_echo(string $message_key, array $args=[], string $language='')
Elgg language module Functions to manage language and translations.
Definition: languages.php:17
elgg_register_rss_link()
Include the RSS icon link and link element in the head.
Definition: views.php:1272
$offset_key
Definition: pagination.php:30
$qb
Definition: queue.php:12
$rows
Definition: redis.php:25
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