Elgg  Version 2.2
 All Classes Namespaces Files Functions Variables Pages
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 
316 function elgg_get_entities(array $options = array()) {
317  return _elgg_services()->entityTable->getEntities($options);
318 }
319 
332  return _elgg_services()->entityTable->getGuidBasedWhereSql($column, $guids);
333 }
334 
349 function _elgg_get_entity_time_where_sql($table, $time_created_upper = null,
350  $time_created_lower = null, $time_updated_upper = null, $time_updated_lower = null) {
351  return _elgg_services()->entityTable->getEntityTimeWhereSql($table,
352  $time_created_upper, $time_created_lower, $time_updated_upper, $time_updated_lower);
353 }
354 
382 function elgg_list_entities(array $options = array(), $getter = 'elgg_get_entities',
383  $viewer = 'elgg_view_entity_list') {
384 
386 
387  $offset_key = isset($options['offset_key']) ? $options['offset_key'] : 'offset';
388 
389  $defaults = array(
390  'offset' => (int) max(get_input($offset_key, 0), 0),
391  'limit' => (int) max(get_input('limit', elgg_get_config('default_limit')), 0),
392  'full_view' => false,
393  'list_type_toggle' => false,
394  'pagination' => true,
395  'no_results' => '',
396  );
397 
398  $options = array_merge($defaults, $options);
399 
400  // backward compatibility
401  if (isset($options['view_type_toggle'])) {
402  elgg_deprecated_notice("Option 'view_type_toggle' deprecated by 'list_type_toggle' in elgg_list* functions", 1.9);
403  $options['list_type_toggle'] = $options['view_type_toggle'];
404  }
405 
406  $options['count'] = true;
407  $count = call_user_func($getter, $options);
408 
409  if ($count > 0) {
410  $options['count'] = false;
411  $entities = call_user_func($getter, $options);
412  } else {
413  $entities = array();
414  }
415 
416  $options['count'] = $count;
417 
418  return call_user_func($viewer, $entities, $options);
419 }
420 
453 function elgg_get_entities_from_attributes(array $options = array()) {
454  return _elgg_services()->entityTable->getEntitiesFromAttributes($options);
455 }
456 
466  return _elgg_services()->entityTable->getEntityAttributeWhereSql($options);
467 }
468 
486 function get_entity_dates($type = '', $subtype = '', $container_guid = 0, $site_guid = 0,
487  $order_by = 'time_created') {
488  return _elgg_services()->entityTable->getDates(
489  $type, $subtype, $container_guid, $site_guid, $order_by);
490 }
491 
507  global $CONFIG;
508 
509  $type = strtolower($type);
510  if (!in_array($type, $CONFIG->entity_types)) {
511  return false;
512  }
513 
514  if (!isset($CONFIG->registered_entities)) {
515  $CONFIG->registered_entities = array();
516  }
517 
518  if (!isset($CONFIG->registered_entities[$type])) {
519  $CONFIG->registered_entities[$type] = array();
520  }
521 
522  if ($subtype) {
523  $CONFIG->registered_entities[$type][] = $subtype;
524  }
525 
526  return true;
527 }
528 
542  global $CONFIG;
543 
544  $type = strtolower($type);
545  if (!in_array($type, $CONFIG->entity_types)) {
546  return false;
547  }
548 
549  if (!isset($CONFIG->registered_entities)) {
550  return false;
551  }
552 
553  if (!isset($CONFIG->registered_entities[$type])) {
554  return false;
555  }
556 
557  if ($subtype) {
558  if (in_array($subtype, $CONFIG->registered_entities[$type])) {
559  $key = array_search($subtype, $CONFIG->registered_entities[$type]);
560  unset($CONFIG->registered_entities[$type][$key]);
561  } else {
562  return false;
563  }
564  } else {
565  unset($CONFIG->registered_entities[$type]);
566  }
567 
568  return true;
569 }
570 
580  global $CONFIG;
581 
582  if (!isset($CONFIG->registered_entities)) {
583  return false;
584  }
585  if ($type) {
586  $type = strtolower($type);
587  }
588  if (!empty($type) && empty($CONFIG->registered_entities[$type])) {
589  return false;
590  }
591 
592  if (empty($type)) {
593  return $CONFIG->registered_entities;
594  }
595 
596  return $CONFIG->registered_entities[$type];
597 }
598 
608  global $CONFIG;
609 
610  if (!isset($CONFIG->registered_entities)) {
611  return false;
612  }
613 
614  $type = strtolower($type);
615 
616  // @todo registering a subtype implicitly registers the type.
617  // see #2684
618  if (!isset($CONFIG->registered_entities[$type])) {
619  return false;
620  }
621 
622  if ($subtype && !in_array($subtype, $CONFIG->registered_entities[$type])) {
623  return false;
624  }
625  return true;
626 }
627 
646 function elgg_list_registered_entities(array $options = array()) {
648 
649  $defaults = array(
650  'full_view' => false,
651  'allowed_types' => true,
652  'list_type_toggle' => false,
653  'pagination' => true,
654  'offset' => 0,
655  'types' => array(),
656  'type_subtype_pairs' => array(),
657  );
658 
659  $options = array_merge($defaults, $options);
660 
661  // backward compatibility
662  if (isset($options['view_type_toggle'])) {
663  elgg_deprecated_notice("Option 'view_type_toggle' deprecated by 'list_type_toggle' in elgg_list* functions", 1.9);
664  $options['list_type_toggle'] = $options['view_type_toggle'];
665  }
666 
667  $types = get_registered_entity_types();
668 
669  foreach ($types as $type => $subtype_array) {
670  if (in_array($type, $options['allowed_types']) || $options['allowed_types'] === true) {
671  // you must explicitly register types to show up in here and in search for objects
672  if ($type == 'object') {
673  if (is_array($subtype_array) && count($subtype_array)) {
674  $options['type_subtype_pairs'][$type] = $subtype_array;
675  }
676  } else {
677  if (is_array($subtype_array) && count($subtype_array)) {
678  $options['type_subtype_pairs'][$type] = $subtype_array;
679  } else {
680  $options['type_subtype_pairs'][$type] = ELGG_ENTITIES_ANY_VALUE;
681  }
682  }
683  }
684  }
685 
686  if (!empty($options['type_subtype_pairs'])) {
687  $count = elgg_get_entities(array_merge(array('count' => true), $options));
688  if ($count > 0) {
689  $entities = elgg_get_entities($options);
690  } else {
691  $entities = array();
692  }
693  } else {
694  $count = 0;
695  $entities = array();
696  }
697 
698  $options['count'] = $count;
699  return elgg_view_entity_list($entities, $options);
700 }
701 
716 function elgg_instanceof($entity, $type = null, $subtype = null, $class = null) {
717  $return = ($entity instanceof \ElggEntity);
718 
719  if ($type) {
720  /* @var \ElggEntity $entity */
721  $return = $return && ($entity->getType() == $type);
722  }
723 
724  if ($subtype) {
725  $return = $return && ($entity->getSubtype() == $subtype);
726  }
727 
728  if ($class) {
729  $return = $return && ($entity instanceof $class);
730  }
731 
732  return $return;
733 }
734 
748  return _elgg_services()->entityTable->updateLastAction($guid, $posted);
749 }
750 
761 function _elgg_entities_test($hook, $type, $value) {
762  global $CONFIG;
763  $value[] = $CONFIG->path . 'engine/tests/ElggEntityTest.php';
764  $value[] = $CONFIG->path . 'engine/tests/ElggCoreAttributeLoaderTest.php';
765  $value[] = $CONFIG->path . 'engine/tests/ElggCoreGetEntitiesTest.php';
766  $value[] = $CONFIG->path . 'engine/tests/ElggCoreGetEntitiesFromAnnotationsTest.php';
767  $value[] = $CONFIG->path . 'engine/tests/ElggCoreGetEntitiesFromMetadataTest.php';
768  $value[] = $CONFIG->path . 'engine/tests/ElggCoreGetEntitiesFromPrivateSettingsTest.php';
769  $value[] = $CONFIG->path . 'engine/tests/ElggCoreGetEntitiesFromRelationshipTest.php';
770  $value[] = $CONFIG->path . 'engine/tests/ElggCoreGetEntitiesFromAttributesTest.php';
771  $value[] = $CONFIG->path . 'engine/tests/ElggEntityPreloaderIntegrationTest.php';
772  return $value;
773 }
774 
783  elgg_register_plugin_hook_handler('unit_test', 'system', '_elgg_entities_test');
784 }
785 
786 return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
787  $events->registerHandler('init', 'system', '_elgg_entities_init');
788 };
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:453
$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:331
update_entity_last_action($guid, $posted=null)
Update the last_action column in the entities table for $guid.
Definition: entities.php:747
_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:349
get_subtype_from_id($subtype_id)
Gets the denormalized string for a given subtype ID.
Definition: entities.php:39
$value
Definition: longtext.php:26
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:486
$column
Definition: add.php:13
elgg_unregister_entity_type($type, $subtype=null)
Unregisters an entity type and subtype as a public-facing type.
Definition: entities.php:541
$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:715
if(isset($vars['id'])) $class
Definition: ajax_loader.php:19
$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:607
get_registered_entity_types($type=null)
Returns registered entity types and subtypes.
Definition: entities.php:579
elgg_instanceof($entity, $type=null, $subtype=null, $class=null)
Checks if $entity is an and optionally for type and subtype.
Definition: entities.php:716
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
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
Exception indicating a user could not be looked up for a permissions check.
const ELGG_ENTITIES_ANY_VALUE
Definition: elgglib.php:2001
elgg_get_entities(array $options=array())
Returns an array of entities with optional filtering.
Definition: entities.php:316
_elgg_entities_test($hook, $type, $value)
Runs unit tests for the entity objects.
Definition: entities.php:761
elgg_deprecated_notice($msg, $dep_version, $backtrace_level=1)
Log a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:1070
_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:1007
elgg_list_entities(array $options=array(), $getter= 'elgg_get_entities', $viewer= 'elgg_view_entity_list')
Returns a string of rendered entities.
Definition: entities.php:382
elgg_list_registered_entities(array $options=array())
Returns a viewable list of entities based on the registered types.
Definition: entities.php:646
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:465
$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:506
$user_guid
Avatar remove action.
Definition: remove.php:6
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:782
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:1483
if(!$display_name) $type
Definition: delete.php:27