Elgg  Version 2.3
entities.php
Go to the documentation of this file.
1 <?php
7 
28  return _elgg_services()->subtypeTable->getId($type, $subtype);
29 }
30 
39 function get_subtype_from_id($subtype_id) {
40  return _elgg_services()->subtypeTable->getSubtype($subtype_id);
41 }
42 
58  return _elgg_services()->subtypeTable->getClass($type, $subtype);
59 }
60 
71 function get_subtype_class_from_id($subtype_id) {
72  return _elgg_services()->subtypeTable->getClassFromId($subtype_id);
73 }
74 
95 function add_subtype($type, $subtype, $class = "") {
96  return _elgg_services()->subtypeTable->add($type, $subtype, $class);
97 }
98 
114  return _elgg_services()->subtypeTable->remove($type, $subtype);
115 }
116 
127  return _elgg_services()->subtypeTable->update($type, $subtype, $class);
128 }
129 
147 function can_write_to_container($user_guid = 0, $container_guid = 0, $type = 'all', $subtype = 'all') {
148  elgg_deprecated_notice(__FUNCTION__ . ' is deprecated. Use ElggEntity::canWriteToContainer()', '2.2');
149  if (!$container_guid) {
151  }
153  if (!$container) {
154  return false;
155  }
156 
157  return $container->canWriteToContainer($user_guid, $type, $subtype);
158 }
159 
175  return _elgg_services()->entityTable->getRow($guid);
176 }
177 
194  return _elgg_services()->entityTable->rowToElggStar($row);
195 }
196 
204 function get_entity($guid) {
205  return _elgg_services()->entityTable->get($guid);
206 }
207 
222  return _elgg_services()->entityTable->exists($guid);
223 }
224 
234 function elgg_enable_entity($guid, $recursive = true) {
235  return _elgg_services()->entityTable->enable($guid, $recursive);
236 }
237 
326 function elgg_get_entities(array $options = array()) {
327  return _elgg_services()->entityTable->getEntities($options);
328 }
329 
342  return _elgg_services()->entityTable->getGuidBasedWhereSql($column, $guids);
343 }
344 
359 function _elgg_get_entity_time_where_sql($table, $time_created_upper = null,
360  $time_created_lower = null, $time_updated_upper = null, $time_updated_lower = null) {
361  return _elgg_services()->entityTable->getEntityTimeWhereSql($table,
362  $time_created_upper, $time_created_lower, $time_updated_upper, $time_updated_lower);
363 }
364 
393 function elgg_list_entities(array $options = array(), $getter = 'elgg_get_entities',
394  $viewer = 'elgg_view_entity_list') {
395 
397 
398  $offset_key = isset($options['offset_key']) ? $options['offset_key'] : 'offset';
399 
400  $defaults = array(
401  'offset' => (int) max(get_input($offset_key, 0), 0),
402  'limit' => (int) max(get_input('limit', elgg_get_config('default_limit')), 0),
403  'full_view' => false,
404  'list_type_toggle' => false,
405  'pagination' => true,
406  'no_results' => '',
407  );
408 
409  $options = array_merge($defaults, $options);
410 
411  // backward compatibility
412  if (isset($options['view_type_toggle'])) {
413  elgg_deprecated_notice("Option 'view_type_toggle' deprecated by 'list_type_toggle' in elgg_list* functions", 1.9);
414  $options['list_type_toggle'] = $options['view_type_toggle'];
415  }
416 
417  $options['count'] = false;
418  $entities = call_user_func($getter, $options);
419  $options['count'] = is_array($entities) ? count($entities) : 0;
420 
421  if (!empty($entities)) {
422  $count_needed = true;
423  if (!$options['pagination']) {
424  $count_needed = false;
425  } elseif (!$options['offset'] && !$options['limit']) {
426  $count_needed = false;
427  } elseif (($options['count'] < (int) $options['limit']) && !$options['offset']) {
428  $count_needed = false;
429  }
430 
431  if ($count_needed) {
432  $options['count'] = true;
433 
434  $options['count'] = (int) call_user_func($getter, $options);
435  }
436  }
437 
438  return call_user_func($viewer, $entities, $options);
439 }
440 
473 function elgg_get_entities_from_attributes(array $options = array()) {
474  return _elgg_services()->entityTable->getEntitiesFromAttributes($options);
475 }
476 
486  return _elgg_services()->entityTable->getEntityAttributeWhereSql($options);
487 }
488 
506 function get_entity_dates($type = '', $subtype = '', $container_guid = 0, $site_guid = 0,
507  $order_by = 'time_created') {
508  return _elgg_services()->entityTable->getDates(
509  $type, $subtype, $container_guid, $site_guid, $order_by);
510 }
511 
527  global $CONFIG;
528 
529  $type = strtolower($type);
530  if (!in_array($type, $CONFIG->entity_types)) {
531  return false;
532  }
533 
534  if (!isset($CONFIG->registered_entities)) {
535  $CONFIG->registered_entities = array();
536  }
537 
538  if (!isset($CONFIG->registered_entities[$type])) {
539  $CONFIG->registered_entities[$type] = array();
540  }
541 
542  if ($subtype) {
543  $CONFIG->registered_entities[$type][] = $subtype;
544  }
545 
546  return true;
547 }
548 
562  global $CONFIG;
563 
564  $type = strtolower($type);
565  if (!in_array($type, $CONFIG->entity_types)) {
566  return false;
567  }
568 
569  if (!isset($CONFIG->registered_entities)) {
570  return false;
571  }
572 
573  if (!isset($CONFIG->registered_entities[$type])) {
574  return false;
575  }
576 
577  if ($subtype) {
578  if (in_array($subtype, $CONFIG->registered_entities[$type])) {
579  $key = array_search($subtype, $CONFIG->registered_entities[$type]);
580  unset($CONFIG->registered_entities[$type][$key]);
581  } else {
582  return false;
583  }
584  } else {
585  unset($CONFIG->registered_entities[$type]);
586  }
587 
588  return true;
589 }
590 
600  global $CONFIG;
601 
602  if (!isset($CONFIG->registered_entities)) {
603  return false;
604  }
605  if ($type) {
606  $type = strtolower($type);
607  }
608  if (!empty($type) && empty($CONFIG->registered_entities[$type])) {
609  return false;
610  }
611 
612  if (empty($type)) {
613  return $CONFIG->registered_entities;
614  }
615 
616  return $CONFIG->registered_entities[$type];
617 }
618 
628  global $CONFIG;
629 
630  if (!isset($CONFIG->registered_entities)) {
631  return false;
632  }
633 
634  $type = strtolower($type);
635 
636  // @todo registering a subtype implicitly registers the type.
637  // see #2684
638  if (!isset($CONFIG->registered_entities[$type])) {
639  return false;
640  }
641 
642  if ($subtype && !in_array($subtype, $CONFIG->registered_entities[$type])) {
643  return false;
644  }
645  return true;
646 }
647 
666 function elgg_list_registered_entities(array $options = array()) {
668 
669  $defaults = array(
670  'full_view' => false,
671  'allowed_types' => true,
672  'list_type_toggle' => false,
673  'pagination' => true,
674  'offset' => 0,
675  'types' => array(),
676  'type_subtype_pairs' => array(),
677  );
678 
679  $options = array_merge($defaults, $options);
680 
681  // backward compatibility
682  if (isset($options['view_type_toggle'])) {
683  elgg_deprecated_notice("Option 'view_type_toggle' deprecated by 'list_type_toggle' in elgg_list* functions", 1.9);
684  $options['list_type_toggle'] = $options['view_type_toggle'];
685  }
686 
687  $types = get_registered_entity_types();
688 
689  foreach ($types as $type => $subtype_array) {
690  if (in_array($type, $options['allowed_types']) || $options['allowed_types'] === true) {
691  // you must explicitly register types to show up in here and in search for objects
692  if ($type == 'object') {
693  if (is_array($subtype_array) && count($subtype_array)) {
694  $options['type_subtype_pairs'][$type] = $subtype_array;
695  }
696  } else {
697  if (is_array($subtype_array) && count($subtype_array)) {
698  $options['type_subtype_pairs'][$type] = $subtype_array;
699  } else {
700  $options['type_subtype_pairs'][$type] = ELGG_ENTITIES_ANY_VALUE;
701  }
702  }
703  }
704  }
705 
706  if (!empty($options['type_subtype_pairs'])) {
707  $count = elgg_get_entities(array_merge(array('count' => true), $options));
708  if ($count > 0) {
709  $entities = elgg_get_entities($options);
710  } else {
711  $entities = array();
712  }
713  } else {
714  $count = 0;
715  $entities = array();
716  }
717 
718  $options['count'] = $count;
719  return elgg_view_entity_list($entities, $options);
720 }
721 
736 function elgg_instanceof($entity, $type = null, $subtype = null, $class = null) {
738 
739  if ($type) {
740  /* @var \ElggEntity $entity */
741  $return = $return && ($entity->getType() == $type);
742  }
743 
744  if ($subtype) {
745  $return = $return && ($entity->getSubtype() == $subtype);
746  }
747 
748  if ($class) {
749  $return = $return && ($entity instanceof $class);
750  }
751 
752  return $return;
753 }
754 
769  elgg_deprecated_notice(__FUNCTION__ . ' has been deprecated. Refrain from updating last action timestamp manually', '2.3');
770 
771  $result = false;
772  $ia = elgg_set_ignore_access(true);
774  if ($entity) {
775  $result = $entity->updateLastAction($posted);
776  }
778  return $result;
779 }
780 
791 function _elgg_entities_test($hook, $type, $value) {
792  global $CONFIG;
793  $value[] = $CONFIG->path . 'engine/tests/ElggEntityTest.php';
794  $value[] = $CONFIG->path . 'engine/tests/ElggCoreAttributeLoaderTest.php';
795  $value[] = $CONFIG->path . 'engine/tests/ElggCoreGetEntitiesTest.php';
796  $value[] = $CONFIG->path . 'engine/tests/ElggCoreGetEntitiesFromAnnotationsTest.php';
797  $value[] = $CONFIG->path . 'engine/tests/ElggCoreGetEntitiesFromMetadataTest.php';
798  $value[] = $CONFIG->path . 'engine/tests/ElggCoreGetEntitiesFromPrivateSettingsTest.php';
799  $value[] = $CONFIG->path . 'engine/tests/ElggCoreGetEntitiesFromRelationshipTest.php';
800  $value[] = $CONFIG->path . 'engine/tests/ElggCoreGetEntitiesFromAttributesTest.php';
801  $value[] = $CONFIG->path . 'engine/tests/ElggEntityPreloaderIntegrationTest.php';
802  return $value;
803 }
804 
813  elgg_register_plugin_hook_handler('unit_test', 'system', '_elgg_entities_test');
814 }
815 
816 return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
817  $events->registerHandler('init', 'system', '_elgg_entities_init');
818 };
elgg_get_config($name, $site_guid=0)
Get an Elgg configuration value.
elgg_get_entities_from_attributes(array $options=array())
Gets entities based upon attributes in secondary tables.
Definition: entities.php:473
$table
Definition: cron.php:34
get_subtype_id($type, $subtype)
Return the id for a given subtype.
Definition: entities.php:27
elgg_enable_entity($guid, $recursive=true)
Enable an entity.
Definition: entities.php:234
$defaults
_elgg_get_guid_based_where_sql($column, $guids)
Returns SQL where clause for owner and containers.
Definition: entities.php:341
update_entity_last_action($guid, $posted=null)
Update the last_action column in the entities table for $guid.
Definition: entities.php:768
_elgg_get_entity_time_where_sql($table, $time_created_upper=null, $time_created_lower=null, $time_updated_upper=null, $time_updated_lower=null)
Returns SQL where clause for entity time limits.
Definition: entities.php:359
get_subtype_from_id($subtype_id)
Gets the denormalized string for a given subtype ID.
Definition: entities.php:39
$value
Definition: longtext.php:42
get_entity_dates($type= '', $subtype= '', $container_guid=0, $site_guid=0, $order_by= 'time_created')
Returns a list of months in which entities were updated or created.
Definition: entities.php:506
$column
Definition: add.php:13
$class
Definition: field.php:20
elgg_unregister_entity_type($type, $subtype=null)
Unregisters an entity type and subtype as a public-facing type.
Definition: entities.php:561
$subtype
Definition: delete.php:28
$return
Definition: opendd.php:15
elgg_entity_exists($guid)
Does an entity exist?
Definition: entities.php:221
$guid
Removes an admin notice.
elgg_register_plugin_hook_handler($hook, $type, $callback, $priority=500)
Definition: elgglib.php:740
$options
Elgg admin footer.
Definition: footer.php:6
get_subtype_class_from_id($subtype_id)
Returns the class name for a subtype id.
Definition: entities.php:71
entity_row_to_elggstar($row)
Create an Elgg* object from a given entity row.
Definition: entities.php:193
get_subtype_class($type, $subtype)
Return the class name for a registered type and subtype.
Definition: entities.php:57
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:627
get_registered_entity_types($type=null)
Returns registered entity types and subtypes.
Definition: entities.php:599
elgg_instanceof($entity, $type=null, $subtype=null, $class=null)
Checks if $entity is an and optionally for type and subtype.
Definition: entities.php:736
add_subtype($type, $subtype, $class="")
Register with a certain type and subtype to be loaded as a specific class.
Definition: entities.php:95
$key
Definition: summary.php:34
$container
Definition: delete.php:29
elgg_set_ignore_access($ignore=true)
Set if Elgg&#39;s access system should be ignored.
Definition: access.php:43
get_input($variable, $default=null, $filter_result=true)
Get some input from variables passed submitted through GET or POST.
Definition: input.php:27
global $CONFIG
can_write_to_container($user_guid=0, $container_guid=0, $type= 'all', $subtype= 'all')
Determine if a given user can write to an entity container.
Definition: entities.php:147
const ELGG_ENTITIES_ANY_VALUE
Definition: elgglib.php:2095
elgg_get_entities(array $options=array())
Returns an array of entities with optional filtering.
Definition: entities.php:326
_elgg_entities_test($hook, $type, $value)
Runs unit tests for the entity objects.
Definition: entities.php:791
elgg_deprecated_notice($msg, $dep_version, $backtrace_level=1)
Log a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:1098
elgg global
Pointer to the global context.
Definition: elgglib.js:12
_elgg_services(\Elgg\Di\ServiceProvider $services=null)
Get the global service provider.
Definition: autoloader.php:17
get_entity_as_row($guid)
Returns a database row from the entities table.
Definition: entities.php:174
update_subtype($type, $subtype, $class= '')
Update a registered type, subtype, and class name.
Definition: entities.php:126
$posted
Definition: comment.php:83
$guids
elgg_view_entity_list($entities, array $vars=array())
Returns a rendered list of entities with pagination.
Definition: views.php:1048
elgg_list_entities(array $options=array(), $getter= 'elgg_get_entities', $viewer= 'elgg_view_entity_list')
Returns a string of rendered entities.
Definition: entities.php:393
elgg_list_registered_entities(array $options=array())
Returns a viewable list of entities based on the registered types.
Definition: entities.php:666
if(!$limit=(int) elgg_extract('limit', $vars, elgg_get_config('default_limit'))) $offset_key
Definition: pagination.php:32
remove_subtype($type, $subtype)
Removes a registered type, subtype, and classname.
Definition: entities.php:113
$entity
Definition: delete.php:7
if(elgg_in_context('widget')) $count
Definition: pagination.php:21
$row
_elgg_get_entity_attribute_where_sql(array $options=array())
Get the join and where clauses for working with entity attributes.
Definition: entities.php:485
$container_guid
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:526
$user_guid
Avatar remove action.
Definition: remove.php:6
elgg ElggEntity
Definition: ElggEntity.js:16
http free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:5
elgg_get_page_owner_guid($guid=0)
Gets the guid of the entity that owns the current page.
Definition: pageowner.php:22
_elgg_entities_init()
Entities init function; establishes the default entity page handler.
Definition: entities.php:812
get_entity($guid)
Loads and returns an entity object from a guid.
Definition: entities.php:204
elgg_register_rss_link()
Include the RSS icon link and link element in the head.
Definition: views.php:1618
if(!$display_name) $type
Definition: delete.php:27