Elgg  Version 6.1
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 }
$default
Definition: checkbox.php:30
elgg_entity_disable_capability(string $type, string $subtype, string $capability)
Disables the capability for a specified type/subtype.
Definition: entities.php:745
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_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
$rows
Definition: redis.php:25
$defaults
Generic entity header upload helper.
Definition: header.php:6
elgg_get_entity_dates(array $options=[])
Returns a list of months in which entities were updated or created.
Definition: entities.php:616
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
elgg_echo(string $message_key, array $args=[], string $language= '')
Elgg language module Functions to manage language and translations.
Definition: languages.php:17
Database abstraction query builder.
$type
Definition: delete.php:21
elgg_get_entity_as_row(int $guid)
Returns a database row from the entities table.
Definition: entities.php:59
elgg_entity_enable_capability(string $type, string $subtype, string $capability)
Enables the capability for a specified type/subtype.
Definition: entities.php:731
if($item instanceof\ElggEntity) elseif($item instanceof\ElggRiverItem) elseif($item instanceof\ElggRelationship) elseif(is_callable([$item, 'getType']))
Definition: item.php:48
elgg_get_entity_statistics(array $options=[])
Return an array reporting the number of various entities in the system.
Definition: entities.php:661
get_input(string $variable, $default=null, bool $filter_result=true)
Parameter input functions.
Definition: input.php:20
$class
Definition: summary.php:44
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
if($who_can_change_language=== 'nobody') elseif($who_can_change_language=== 'admin_only'&&!elgg_is_admin_logged_in()) $options
Definition: language.php:20
elgg_entity_exists(int $guid)
Does an entity exist?
Definition: entities.php:91
const ELGG_IGNORE_ACCESS
elgg_call() flags
Definition: constants.php:121
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
elgg_count_entities(array $options=[])
Returns a count of entities.
Definition: entities.php:518
$getter
$offset_key
Definition: pagination.php:30
elgg_get_site_entity()
Get the current site entity.
Definition: entities.php:101
Extends QueryBuilder with ORDER BY clauses.
elgg_list_entities(array $options=[], callable $getter=null, callable $viewer=null)
Returns a string of rendered entities.
Definition: entities.php:551
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
$subtype
Definition: delete.php:22
elgg_entity_types_with_capability(string $capability)
Returns an array of type/subtypes with the requested capability enabled.
Definition: entities.php:757
$required
Definition: label.php:12
_elgg_services()
Get the global service provider.
Definition: elgglib.php:353
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
$entity_stats
Definition: numentities.php:5
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
$qb
Definition: queue.php:12
$guid
Reset an ElggUpgrade.
Definition: reset.php:6
elgg_register_rss_link()
Include the RSS icon link and link element in the head.
Definition: views.php:1272