Elgg  Version master
entities.php
Go to the documentation of this file.
1 <?php
8 
19 function elgg_get_entity_class(string $type, string $subtype): string {
20  return _elgg_services()->entityTable->getEntityClass($type, $subtype);
21 }
22 
42 function elgg_set_entity_class(string $type, string $subtype, string $class = ''): void {
43  _elgg_services()->entityTable->setEntityClass($type, $subtype, $class);
44 }
45 
57 function elgg_get_entity_as_row(int $guid): ?\stdClass {
58  return _elgg_services()->entityTable->getRow($guid);
59 }
60 
68 function get_entity(int $guid): ?\ElggEntity {
69  if ($guid === 1) {
70  return _elgg_services()->config->site;
71  }
72 
73  return _elgg_services()->entityTable->get($guid);
74 }
75 
89 function elgg_entity_exists(int $guid): bool {
90  return _elgg_services()->entityTable->exists($guid);
91 }
92 
100  return _elgg_services()->config->site;
101 }
102 
505 function elgg_get_entities(array $options = []) {
506  return \Elgg\Database\Entities::find($options);
507 }
508 
516 function elgg_count_entities(array $options = []): int {
517  $options['count'] = true;
518 
519  return (int) elgg_get_entities($options);
520 }
521 
549 function elgg_list_entities(array $options = [], callable $getter = null, callable $viewer = null): string {
550  $getter = $getter ?? 'elgg_get_entities'; // callables only support default NULL value
551  $viewer = $viewer ?? 'elgg_view_entity_list'; // callables only support default NULL value
552  $offset_key = $options['offset_key'] ?? 'offset';
553 
554  $defaults = [
555  'offset' => (int) max(get_input($offset_key, 0), 0),
556  'limit' => (int) max(get_input('limit', _elgg_services()->config->default_limit), 0),
557  'sort_by' => get_input('sort_by', []),
558  'full_view' => false,
559  'pagination' => true,
560  'no_results' => '',
561  'preload_owners' => true,
562  'preload_containers' => true,
563  ];
564 
565  $options = array_merge($defaults, $options);
566 
567  $options['register_rss_link'] = elgg_extract('register_rss_link', $options, elgg_extract('pagination', $options));
568  if ($options['register_rss_link']) {
570  }
571 
572  $options['count'] = false;
573  $entities = call_user_func($getter, $options);
574  $options['count'] = is_array($entities) ? count($entities) : 0;
575 
576  if (!is_array($entities) && $viewer === 'elgg_view_entity_list') {
577  elgg_log(elgg_echo('list:error:getter:admin', [$getter, gettype($entities), $viewer]), 'ERROR');
578 
579  return elgg_echo('list:error:getter:user');
580  }
581 
582  if (!empty($entities) || !empty($options['offset'])) {
583  $count_needed = true;
584  if (!$options['pagination']) {
585  $count_needed = false;
586  } elseif (!$options['offset'] && !$options['limit']) {
587  $count_needed = false;
588  } elseif (($options['count'] < (int) $options['limit']) && !$options['offset']) {
589  $count_needed = false;
590  }
591 
592  if ($count_needed) {
593  $options['count'] = true;
594 
595  $options['count'] = (int) call_user_func($getter, $options);
596  }
597  }
598 
599  return call_user_func($viewer, $entities, $options);
600 }
601 
614 function elgg_get_entity_dates(array $options = []): array {
615  return \Elgg\Database\Entities::with($options)->getDates();
616 }
617 
642 function elgg_search(array $options = []) {
643  try {
644  return _elgg_services()->search->search($options);
645  } catch (\Elgg\Exceptions\DomainException $e) {
646  return false;
647  }
648 }
649 
658 function elgg_get_entity_statistics(int $owner_guid = 0): array {
659  $select = Select::fromTable(EntityTable::TABLE_NAME);
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:19
elgg_get_entity_statistics(int $owner_guid=0)
Return an array reporting the number of various entities in the system.
Definition: entities.php:658
$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:614
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:57
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:89
$owner_guid
get_entity(int $guid)
Loads and returns an entity object from a guid.
Definition: entities.php:68
elgg_get_entities(array $options=[])
Fetches/counts entities or performs a calculation on their properties.
Definition: entities.php:505
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:516
$getter
$offset_key
Definition: pagination.php:30
elgg_get_site_entity()
Get the current site entity.
Definition: entities.php:99
elgg_list_entities(array $options=[], callable $getter=null, callable $viewer=null)
Returns a string of rendered entities.
Definition: entities.php:549
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:42
$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:642
$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