$plural = $singular . 's';
// normalize the singular to plural
- if (isset($options[$singular]) && $options[$singular] !== NULL && $options[$singular] !== FALSE) {
+ // isset() returns FALSE for array values of NULL, so they are ignored.
+ // everything else falsy is included.
+ //if (isset($options[$singular]) && $options[$singular] !== NULL && $options[$singular] !== FALSE) {
+ if (isset($options[$singular])) {
if (isset($options[$plural])) {
if (is_array($options[$plural])) {
$options[$plural][] = $options[$singlar];
$options[$plural] = array($options[$singular]);
}
}
- $options[$singular] = NULL;
+ unset($options[$singular]);
}
return $options;
/**
* Some useful constant definitions
*/
-define('ACCESS_DEFAULT',-1);
-define('ACCESS_PRIVATE',0);
-define('ACCESS_LOGGED_IN',1);
-define('ACCESS_PUBLIC',2);
-define('ACCESS_FRIENDS',-2);
+define('ACCESS_DEFAULT', -1);
+define('ACCESS_PRIVATE', 0);
+define('ACCESS_LOGGED_IN', 1);
+define('ACCESS_PUBLIC', 2);
+define('ACCESS_FRIENDS', -2);
-register_elgg_event_handler('init','system','elgg_init');
-register_elgg_event_handler('boot','system','elgg_boot',1000);
+define('ELGG_ENTITIES_ANY_VALUE', NULL);
+define('ELGG_ENTITIES_NO_VALUE', 0);
+
+register_elgg_event_handler('init', 'system', 'elgg_init');
+register_elgg_event_handler('boot', 'system', 'elgg_boot', 1000);
register_plugin_hook('unit_test', 'system', 'elgg_api_test');
\ No newline at end of file
/**
- * Get all entities.
+ * Get all entities. NB: Plural arguments can be written as
+ * singular if only specifying a single element. (e.g., 'type' => 'object'
+ * vs 'types' => array('object')).
*
* @param array $options Array in format:
*
function elgg_get_entities(array $options = array()) {
global $CONFIG;
- //@todo allow use of singular types that rewrite to plural ones.
$defaults = array(
- 'type' => NULL,
- 'types' => NULL,
- 'subtypes' => NULL,
- 'subtype' => NULL,
- 'type_subtype_pairs' => NULL,
- 'owner_guids' => NULL,
- 'owner_guid' => NULL,
- 'container_guids' => NULL,
- 'container_guid' => NULL,
- 'site_guids' => $CONFIG->site_guid,
- 'site_guid' => NULL,
-
- 'modified_time_lower' => NULL,
- 'modified_time_upper' => NULL,
- 'created_time_lower' => NULL,
- 'created_time_upper' => NULL,
-
- 'order_by' => 'e.time_created desc',
- 'group_by' => NULL,
- 'limit' => 10,
- 'offset' => 0,
- 'count' => FALSE,
- 'selects' => array(),
- 'wheres' => array(),
- 'joins' => array()
+ 'types' => ELGG_ENTITIES_ANY_VALUE,
+ 'subtypes' => ELGG_ENTITIES_ANY_VALUE,
+ 'type_subtype_pairs' => ELGG_ENTITIES_ANY_VALUE,
+
+ 'owner_guids' => ELGG_ENTITIES_ANY_VALUE,
+ 'container_guids' => ELGG_ENTITIES_ANY_VALUE,
+ 'site_guids' => $CONFIG->site_guid,
+
+ 'modified_time_lower' => ELGG_ENTITIES_ANY_VALUE,
+ 'modified_time_upper' => ELGG_ENTITIES_ANY_VALUE,
+ 'created_time_lower' => ELGG_ENTITIES_ANY_VALUE,
+ 'created_time_upper' => ELGG_ENTITIES_ANY_VALUE,
+
+ 'order_by' => 'e.time_created desc',
+ 'group_by' => ELGG_ENTITIES_ANY_VALUE,
+ 'limit' => 10,
+ 'offset' => 0,
+ 'count' => FALSE,
+ 'selects' => array(),
+ 'wheres' => array(),
+ 'joins' => array()
);
+
$options = array_merge($defaults, $options);
$singulars = array('type', 'subtype', 'owner_guid', 'container_guid', 'site_guid');
foreach ($types as $type) {
$subtype_ids = array();
if ($subtypes) {
- // subtypes can be NULL or '' or 0, which means "no subtype"
foreach ($subtypes as $subtype) {
- // if a subtype is sent that doesn't exist
- if (0 === $subtype || $subtype_id = get_subtype_id($type, $subtype)) {
- $subtype_ids[] = (0 === $subtype) ? 0 : $subtype_id;
+ // check that the subtype is valid (with ELGG_ENTITIES_NO_VALUE being a valid subtype)
+ if (ELGG_ENTITIES_NO_VALUE === $subtype || $subtype_id = get_subtype_id($type, $subtype)) {
+ $subtype_ids[] = (ELGG_ENTITIES_NO_VALUE === $subtype) ? ELGG_ENTITIES_NO_VALUE : $subtype_id;
} else {
$valid_subtypes_count--;
elgg_log("Type-subtype $type:$subtype' does not exist!", 'WARNING');
- // return false if we're all invalid subtypes in the only valid type
continue;
}
}
+ // return false if we're all invalid subtypes in the only valid type
if ($valid_subtypes_count <= 0) {
return FALSE;
}
if (is_array($paired_subtypes)) {
$paired_subtype_ids = array();
foreach ($paired_subtypes as $paired_subtype) {
- if ($paired_subtype && ($paired_subtype_id = get_subtype_id($paired_type, $paired_subtype))) {
- $paired_subtype_ids[] = $paired_subtype_id;
+ if (ELGG_ENTITIES_NO_VALUE === $paired_subtype || ($paired_subtype_id = get_subtype_id($paired_type, $paired_subtype))) {
+ $paired_subtype_ids[] = (ELGG_ENTITIES_NO_VALUE === $paired_subtype) ? ELGG_ENTITIES_NO_VALUE : $paired_subtype_id;
} else {
$valid_pairs_subtypes_count--;
elgg_log("Type-subtype $paired_type:$paired_subtype' does not exist!", 'WARNING');
/**
- * Returns entities based upon metadata.
+ * Returns entities based upon metadata. Also accepts all
+ * options available to elgg_get_entities(). Supports
+ * the singular option shortcut.
*
+ * @see elgg_get_entities
* @param array $options Array in format:
*
* metadata_names => NULL|ARR metadata names
*/
function elgg_get_entities_from_metadata(array $options = array()) {
$defaults = array(
- 'metadata_names' => NULL,
- 'metadata_name' => NULL,
- 'metadata_values' => NULL,
- 'metadata_value' => NULL,
- 'metadata_name_value_pair' => NULL,
- 'metadata_name_value_pairs' => NULL,
+ 'metadata_names' => ELGG_ENTITIES_ANY_VALUE,
+ 'metadata_values' => ELGG_ENTITIES_ANY_VALUE,
+ 'metadata_name_value_pairs' => ELGG_ENTITIES_ANY_VALUE,
+
'metadata_name_value_pairs_operator' => 'AND',
'metadata_case_sensitive' => TRUE
);
$es = elgg_get_entities($options);
$this->assertFalse($es);
}
+
+
+
+
+
+
+ public function testElggApiGettersEntityNoSubtype() {
+ // create an entity we can later delete.
+ // order by time created and limit by 1 should == this entity.
+
+ $e = new ElggObject();
+ $e->save();
+
+ $options = array(
+ 'type' => 'object',
+ 'limit' => 1,
+ 'order_by' => 'e.time_created desc'
+ );
+
+ // grab ourself again to fill out attributes.
+ $e = get_entity($e->getGUID());
+
+ $entities = elgg_get_entities($options);
+
+ $this->assertEqual(count($entities), 1);
+
+ foreach ($entities as $entity) {
+ $this->assertIdentical($e->getGUID(), $entity->getGUID());
+ }
+
+ $e->delete();
+ }
+
+ public function testElggApiGettersEntityNoValueSubtypeNotSet() {
+ // create an entity we can later delete.
+ // order by time created and limit by 1 should == this entity.
+
+ $e = new ElggObject();
+ $e->save();
+
+ $options = array(
+ 'type' => 'object',
+ 'subtype' => ELGG_ENTITY_NO_VALUE,
+ 'limit' => 1,
+ 'order_by' => 'e.time_created desc'
+ );
+
+ // grab ourself again to fill out attributes.
+ $e = get_entity($e->getGUID());
+
+ $entities = elgg_get_entities($options);
+
+ $this->assertEqual(count($entities), 1);
+
+ foreach ($entities as $entity) {
+ $this->assertIdentical($e->getGUID(), $entity->getGUID());
+ }
+
+ $e->delete();
+ }
+
+ public function testElggApiGettersEntityNoValueSubtypeSet() {
+ global $CONFIG;
+ // create an entity we can later delete.
+ // order by time created and limit by 1 should == this entity.
+
+ $subtype = 'subtype_' . rand();
+
+ $e = new ElggObject();
+ $e->subtype = $subtype;
+ $e->save();
+
+ $options = array(
+ 'type' => 'object',
+ 'subtype' => ELGG_ENTITIES_NO_VALUE,
+ 'limit' => 1,
+ 'order_by' => 'e.time_created desc'
+ );
+
+ // grab ourself again to fill out attributes.
+ $e = get_entity($e->getGUID());
+
+ $entities = elgg_get_entities($options);
+
+ $this->assertEqual(count($entities), 1);
+
+ // this entity should NOT be the entity we just created
+ // and should have no subtype
+ foreach ($entities as $entity) {
+ $this->assertNotEqual($e->getGUID(), $entity->getGUID());
+ $this->assertEqual($entity->subtype_id, 0);
+ }
+
+ $e->delete();
+
+ $q = "DELETE FROM {$CONFIG->dbprefix}entity_subtypes WHERE subtype = '$subtype'";
+ delete_data($q);
+ }
}