Elgg  Version 5.1
entities.php
Go to the documentation of this file.
1 <?php
7 
18 function elgg_get_entity_class(string $type, string $subtype): string {
19  return _elgg_services()->entityTable->getEntityClass($type, $subtype);
20 }
21 
41 function elgg_set_entity_class(string $type, string $subtype, string $class = ''): void {
42  _elgg_services()->entityTable->setEntityClass($type, $subtype, $class);
43 }
44 
56 function elgg_get_entity_as_row(int $guid): ?\stdClass {
57  return _elgg_services()->entityTable->getRow($guid);
58 }
59 
67 function get_entity(int $guid): ?\ElggEntity {
68  if ($guid === 1) {
69  return _elgg_services()->config->site;
70  }
71 
72  return _elgg_services()->entityTable->get($guid);
73 }
74 
88 function elgg_entity_exists(int $guid): bool {
89  return _elgg_services()->entityTable->exists($guid);
90 }
91 
99  return _elgg_services()->config->site;
100 }
101 
504 function elgg_get_entities(array $options = []) {
505  return \Elgg\Database\Entities::find($options);
506 }
507 
515 function elgg_count_entities(array $options = []): int {
516  $options['count'] = true;
517 
518  return (int) elgg_get_entities($options);
519 }
520 
548 function elgg_list_entities(array $options = [], callable $getter = null, callable $viewer = null): string {
549  $getter = $getter ?? 'elgg_get_entities'; // callables only support default NULL value
550  $viewer = $viewer ?? 'elgg_view_entity_list'; // callables only support default NULL value
551  $offset_key = $options['offset_key'] ?? 'offset';
552 
553  $defaults = [
554  'offset' => (int) max(get_input($offset_key, 0), 0),
555  'limit' => (int) max(get_input('limit', _elgg_services()->config->default_limit), 0),
556  'sort_by' => get_input('sort_by', []),
557  'full_view' => false,
558  'pagination' => true,
559  'no_results' => '',
560  'preload_owners' => true,
561  'preload_containers' => true,
562  ];
563 
564  $options = array_merge($defaults, $options);
565 
566  $options['register_rss_link'] = elgg_extract('register_rss_link', $options, elgg_extract('pagination', $options));
567  if ($options['register_rss_link']) {
569  }
570 
571  $options['count'] = false;
572  $entities = call_user_func($getter, $options);
573  $options['count'] = is_array($entities) ? count($entities) : 0;
574 
575  if (!is_array($entities) && $viewer === 'elgg_view_entity_list') {
576  elgg_log(elgg_echo('list:error:getter:admin', [$getter, gettype($entities), $viewer]), 'ERROR');
577 
578  return elgg_echo('list:error:getter:user');
579  }
580 
581  if (!empty($entities) || !empty($options['offset'])) {
582  $count_needed = true;
583  if (!$options['pagination']) {
584  $count_needed = false;
585  } elseif (!$options['offset'] && !$options['limit']) {
586  $count_needed = false;
587  } elseif (($options['count'] < (int) $options['limit']) && !$options['offset']) {
588  $count_needed = false;
589  }
590 
591  if ($count_needed) {
592  $options['count'] = true;
593 
594  $options['count'] = (int) call_user_func($getter, $options);
595  }
596  }
597 
598  return call_user_func($viewer, $entities, $options);
599 }
600 
613 function elgg_get_entity_dates(array $options = []): array {
614  return \Elgg\Database\Entities::with($options)->getDates();
615 }
616 
641 function elgg_search(array $options = []) {
642  try {
643  return _elgg_services()->search->search($options);
644  } catch (\Elgg\Exceptions\DomainException $e) {
645  return false;
646  }
647 }
648 
657 function elgg_get_entity_statistics(int $owner_guid = 0): array {
658 
659  $select = Select::fromTable('entities');
660  $select->select('type')
661  ->addSelect('subtype')
662  ->addSelect('count(*) AS total')
663  ->where($select->compare('enabled', '=', 'yes', ELGG_VALUE_STRING))
664  ->groupBy('type')
665  ->addGroupBy('subtype')
666  ->orderBy('total', 'desc');
667 
668  if (!empty($owner_guid)) {
669  $select->andWhere($select->compare('owner_guid', '=', $owner_guid, ELGG_VALUE_GUID));
670  }
671 
672  $entity_stats = [];
673 
674  $rows = _elgg_services()->db->getData($select);
675  foreach ($rows as $row) {
676  $type = $row->type;
677  if (!isset($entity_stats[$type]) || !is_array($entity_stats[$type])) {
678  $entity_stats[$type] = [];
679  }
680 
681  $entity_stats[$type][$row->subtype] = $row->total;
682  }
683 
684  return $entity_stats;
685 }
686 
698 function elgg_entity_has_capability(string $type, string $subtype, string $capability, bool $default = false): bool {
699  return _elgg_services()->entity_capabilities->hasCapability($type, $subtype, $capability, $default);
700 }
701 
712 function elgg_entity_enable_capability(string $type, string $subtype, string $capability): void {
713  _elgg_services()->entity_capabilities->setCapability($type, $subtype, $capability, true);
714 }
715 
726 function elgg_entity_disable_capability(string $type, string $subtype, string $capability): void {
727  _elgg_services()->entity_capabilities->setCapability($type, $subtype, $capability, false);
728 }
729 
738 function elgg_entity_types_with_capability(string $capability): array {
739  return _elgg_services()->entity_capabilities->getTypesWithCapability($capability);
740 }
$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:726
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:18
elgg_get_entity_statistics(int $owner_guid=0)
Return an array reporting the number of various entities in the system.
Definition: entities.php:657
$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:613
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
const ELGG_VALUE_GUID
Definition: constants.php:113
$type
Definition: delete.php:22
elgg_get_entity_as_row(int $guid)
Returns a database row from the entities table.
Definition: entities.php:56
elgg_entity_enable_capability(string $type, string $subtype, string $capability)
Enables the capability for a specified type/subtype.
Definition: entities.php:712
if($item instanceof\ElggEntity) elseif($item instanceof\ElggRiverItem) elseif($item instanceof\ElggRelationship) elseif(is_callable([$item, 'getType']))
Definition: item.php:48
$options
Elgg admin footer.
Definition: footer.php:6
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:254
elgg_entity_exists(int $guid)
Does an entity exist?
Definition: entities.php:88
$owner_guid
get_entity(int $guid)
Loads and returns an entity object from a guid.
Definition: entities.php:67
elgg_get_entities(array $options=[])
Fetches/counts entities or performs a calculation on their properties.
Definition: entities.php:504
elgg_log($message, $level=\Psr\Log\LogLevel::NOTICE)
Log a message.
Definition: elgglib.php:86
elgg_count_entities(array $options=[])
Returns a count of entities.
Definition: entities.php:515
$getter
$offset_key
Definition: pagination.php:30
elgg_get_site_entity()
Get the current site entity.
Definition: entities.php:98
elgg_list_entities(array $options=[], callable $getter=null, callable $viewer=null)
Returns a string of rendered entities.
Definition: entities.php:548
const ELGG_VALUE_STRING
Definition: constants.php:112
elgg_set_entity_class(string $type, string $subtype, string $class= '')
Sets class constructor name for entities with given type and subtype.
Definition: entities.php:41
$subtype
Definition: delete.php:23
elgg_entity_types_with_capability(string $capability)
Returns an array of type/subtypes with the requested capability enabled.
Definition: entities.php:738
_elgg_services()
Get the global service provider.
Definition: elgglib.php:346
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:698
$entity_stats
Definition: numentities.php:3
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:641
$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:1293