Elgg  Version 3.0
entities.php
Go to the documentation of this file.
1 <?php
17  return _elgg_services()->entityTable->getEntityClass($type, $subtype);
18 }
19 
40  _elgg_services()->entityTable->setEntityClass($type, $subtype, $class);
41 }
42 
43 
59  return _elgg_services()->entityTable->getRow($guid);
60 }
61 
76 function entity_row_to_elggstar($row) {
77  return _elgg_services()->entityTable->rowToElggStar($row);
78 }
79 
87 function get_entity($guid) {
88  if ($guid == 1) {
89  return _elgg_config()->site;
90  }
91  return _elgg_services()->entityTable->get($guid);
92 }
93 
108  return _elgg_services()->entityTable->exists($guid);
109 }
110 
120 function elgg_enable_entity($guid, $recursive = true) {
121  return _elgg_services()->entityTable->enable($guid, $recursive);
122 }
123 
131  return _elgg_config()->site;
132 }
133 
545 function elgg_get_entities(array $options = []) {
546  return \Elgg\Database\Entities::find($options);
547 }
548 
577 function elgg_list_entities(array $options = [], $getter = 'elgg_get_entities', $viewer = 'elgg_view_entity_list') {
578 
579  $offset_key = isset($options['offset_key']) ? $options['offset_key'] : 'offset';
580 
581  $defaults = [
582  'offset' => (int) max(get_input($offset_key, 0), 0),
583  'limit' => (int) max(get_input('limit', _elgg_config()->default_limit), 0),
584  'full_view' => false,
585  'list_type_toggle' => false,
586  'pagination' => true,
587  'no_results' => '',
588  'preload_owners' => true,
589  'preload_containers' => true,
590  ];
591 
592  $options = array_merge($defaults, $options);
593 
594  $options['register_rss_link'] = elgg_extract('register_rss_link', $options, elgg_extract('pagination', $options));
595  if ($options['register_rss_link']) {
597  }
598 
599  if ($options['no_results'] === true) {
600  $options['no_results'] = elgg_echo('notfound');
601  }
602 
603  $options['count'] = false;
604  $entities = call_user_func($getter, $options);
605  $options['count'] = is_array($entities) ? count($entities) : 0;
606 
607  if (!empty($entities)) {
608  $count_needed = true;
609  if (!$options['pagination']) {
610  $count_needed = false;
611  } elseif (!$options['offset'] && !$options['limit']) {
612  $count_needed = false;
613  } elseif (($options['count'] < (int) $options['limit']) && !$options['offset']) {
614  $count_needed = false;
615  }
616 
617  if ($count_needed) {
618  $options['count'] = true;
619 
620  $options['count'] = (int) call_user_func($getter, $options);
621  }
622  }
623 
624  return call_user_func($viewer, $entities, $options);
625 }
626 
639 function elgg_get_entity_dates(array $options = []) {
640  return \Elgg\Database\Entities::with($options)->getDates();
641 }
642 
658  $type = strtolower($type);
659  if (!in_array($type, \Elgg\Config::getEntityTypes())) {
660  return false;
661  }
662 
663  $entities = _elgg_config()->registered_entities;
664  if (empty($entities)) {
665  $entities = [];
666  }
667 
668  if (!isset($entities[$type])) {
669  $entities[$type] = [];
670  }
671 
672  if ($subtype) {
673  if (in_array($subtype, $entities[$type])) {
674  // subtype already registered
675  return true;
676  }
677 
678  $entities[$type][] = $subtype;
679  }
680 
681  _elgg_config()->registered_entities = $entities;
682 
683  return true;
684 }
685 
699  $type = strtolower($type);
700  if (!in_array($type, \Elgg\Config::getEntityTypes())) {
701  return false;
702  }
703 
704  $entities = _elgg_config()->registered_entities;
705  if (empty($entities)) {
706  return false;
707  }
708 
709  if (!isset($entities[$type])) {
710  return false;
711  }
712 
713  if ($subtype) {
714  if (in_array($subtype, $entities[$type])) {
715  $key = array_search($subtype, $entities[$type]);
716  unset($entities[$type][$key]);
717  } else {
718  return false;
719  }
720  } else {
721  unset($entities[$type]);
722  }
723 
724  _elgg_config()->registered_entities = $entities;
725  return true;
726 }
727 
737  $registered_entities = _elgg_config()->registered_entities;
738  if (empty($registered_entities)) {
739  return false;
740  }
741 
742  if ($type) {
743  $type = strtolower($type);
744  }
745 
746  if (!empty($type) && !isset($registered_entities[$type])) {
747  return false;
748  }
749 
750  if (empty($type)) {
751  return $registered_entities;
752  }
753 
754  return $registered_entities[$type];
755 }
756 
766  $registered_entities = _elgg_config()->registered_entities;
767  if (empty($registered_entities)) {
768  return false;
769  }
770 
771  $type = strtolower($type);
772 
773  // @todo registering a subtype implicitly registers the type.
774  // see #2684
775  if (!isset($registered_entities[$type])) {
776  return false;
777  }
778 
779  if ($subtype && !in_array($subtype, $registered_entities[$type])) {
780  return false;
781  }
782  return true;
783 }
784 
798 function elgg_instanceof($entity, $type = null, $subtype = null) {
799  $return = ($entity instanceof \ElggEntity);
800 
801  if ($type) {
802  /* @var \ElggEntity $entity */
803  $return = $return && ($entity->getType() == $type);
804  }
805 
806  if ($subtype) {
807  $return = $return && ($entity->getSubtype() == $subtype);
808  }
809 
810  return $return;
811 }
812 
821  $site_guid = elgg_extract('site_guid', $options, elgg_extract('site_guids', $options));
822  if ($site_guid === null) {
823  return;
824  }
825 
826  $backtrace = debug_backtrace();
827  // never show this call.
828  array_shift($backtrace);
829 
830  if (!empty($backtrace[0]['class'])) {
831  $warning = "Passing site_guid or site_guids to the method {$backtrace[0]['class']}::{$backtrace[0]['file']} is not supported.";
832  $warning .= "Please update your usage of the method.";
833  } else {
834  $warning = "Passing site_guid or site_guids to the function {$backtrace[0]['function']} in {$backtrace[0]['file']} is not supported.";
835  $warning .= "Please update your usage of the function.";
836  }
837 
838  _elgg_services()->logger->warning($warning);
839 }
840 
852 function _elgg_entities_test($hook, $type, $value) {
860  return $value;
861 }
862 
871  elgg_register_plugin_hook_handler('unit_test', 'system', '_elgg_entities_test');
872 }
873 
877 return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
878  $events->registerHandler('init', 'system', '_elgg_entities_init');
879 };
$CONFIG default_limit
The default "limit" used in site queries.
Definition: config.php:121
elgg_enable_entity($guid, $recursive=true)
Enable an entity.
Definition: entities.php:120
$defaults
elgg_get_entity_dates(array $options=[])
Returns a list of months in which entities were updated or created.
Definition: entities.php:639
elgg_unregister_entity_type($type, $subtype=null)
Unregisters an entity type and subtype as a public-facing type.
Definition: entities.php:698
Events service.
$subtype
Definition: delete.php:22
elgg_entity_exists($guid)
Does an entity exist?
Definition: entities.php:107
$guid
Removes an admin notice.
$type
Definition: delete.php:21
elgg_register_plugin_hook_handler($hook, $type, $callback, $priority=500)
Definition: elgglib.php:634
elgg_echo($message_key, array $args=[], $language="")
Given a message key, returns an appropriately translated full-text string.
Definition: languages.php:21
elgg_instanceof($entity, $type=null, $subtype=null)
Checks if $entity is an and optionally for type and subtype.
Definition: entities.php:798
elgg_list_entities(array $options=[], $getter= 'elgg_get_entities', $viewer= 'elgg_view_entity_list')
Returns a string of rendered entities.
Definition: entities.php:577
$options
Elgg admin footer.
Definition: footer.php:6
entity_row_to_elggstar($row)
Create an Elgg* object from a given entity row.
Definition: entities.php:76
is_registered_entity_type($type, $subtype=null)
Returns if the entity type and subtype have been registered with elgg_register_entity_type().
Definition: entities.php:765
get_registered_entity_types($type=null)
Returns registered entity types and subtypes.
Definition: entities.php:736
Configuration exception.
Base class for events and hooks.
$class
Definition: field.php:29
$entity
Definition: reset.php:8
elgg_get_entities(array $options=[])
Fetches/counts entities or performs a calculation on their properties.
Definition: entities.php:545
get_input($variable, $default=null, $filter_result=true)
Get some input from variables passed submitted through GET or POST.
Definition: input.php:27
elgg_get_entity_class($type, $subtype)
Procedural code for creating, loading, and modifying objects.
Definition: entities.php:16
_elgg_entities_test($hook, $type, $value)
Runs unit tests for the entity objects.
Definition: entities.php:852
elgg_get_site_entity()
Get the current site entity.
Definition: entities.php:130
if($item instanceof\ElggEntity) elseif($item instanceof\ElggRiverItem) elseif(is_callable([$item, 'getType']))
Definition: item.php:39
elgg_extract($key, $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:1131
get_entity_as_row($guid)
Returns a database row from the entities table.
Definition: entities.php:58
if($container instanceof ElggGroup &&$container->guid!=elgg_get_page_owner_guid()) $key
Definition: summary.php:55
elgg_set_entity_class($type, $subtype, $class="")
Sets class constructor name for entities with given type and subtype.
Definition: entities.php:39
_elgg_config()
Get the Elgg config service.
$value
Definition: debugging.php:7
_elgg_check_unsupported_site_guid(array $options=[])
Checks options for the existing of site_guid or site_guids contents and reports a warning if found...
Definition: entities.php:820
if(!$limit=(int) elgg_extract('limit', $vars, elgg_get_config('default_limit'))) $offset_key
Definition: pagination.php:32
class
Definition: placeholder.php:21
_elgg_services()
Get the global service provider.
Definition: elgglib.php:1292
elgg_register_entity_type($type, $subtype=null)
Registers an entity type and subtype as a public-facing entity that should be shown in search and by ...
Definition: entities.php:657
elgg ElggEntity
Definition: ElggEntity.js:15
_elgg_entities_init()
Entities init function; establishes the default entity page handler.
Definition: entities.php:870
get_entity($guid)
Loads and returns an entity object from a guid.
Definition: entities.php:87
elgg_register_rss_link()
Include the RSS icon link and link element in the head.
Definition: views.php:1575