public function getEntityTypeSubtypeWhereSql($table, $types, $subtypes, $pairs)
{
// subtype depends upon type.
if ($subtypes && !$types) {
$this->logger->warn("Cannot set subtypes without type.");
return false;
}
// short circuit if nothing is requested
if (!$types && !$subtypes && !$pairs) {
return '';
}
// these are the only valid types for entities in elgg
$valid_types = $this->config->get('entity_types');
// pairs override
$wheres = array();
if (!is_array($pairs)) {
if (!is_array($types)) {
$types = array($types);
}
if ($subtypes && !is_array($subtypes)) {
$subtypes = array($subtypes);
}
// decrementer for valid types. Return false if no valid types
$valid_types_count = count($types);
$valid_subtypes_count = 0;
// remove invalid types to get an accurate count of
// valid types for the invalid subtype detection to use
// below.
// also grab the count of ALL subtypes on valid types to decrement later on
// and check against.
//
// yes this is duplicating a foreach on $types.
foreach ($types as $type) {
if (!in_array($type, $valid_types)) {
$valid_types_count--;
unset($types[array_search($type, $types)]);
} else {
// do the checking (and decrementing) in the subtype section.
$valid_subtypes_count += count($subtypes);
}
}
// return false if nothing is valid.
if (!$valid_types_count) {
return false;
}
// subtypes are based upon types, so we need to look at each
// type individually to get the right subtype id.
foreach ($types as $type) {
$subtype_ids = array();
if ($subtypes) {
foreach ($subtypes as $subtype) {
// check that the subtype is valid
if (!$subtype && ELGG_ENTITIES_NO_VALUE === $subtype) {
// subtype value is 0
$subtype_ids[] = ELGG_ENTITIES_NO_VALUE;
} elseif (!$subtype) {
// subtype is ignored.
// this handles ELGG_ENTITIES_ANY_VALUE, '', and anything falsy that isn't 0
continue;
} else {
$subtype_id = get_subtype_id($type, $subtype);
if ($subtype_id) {
$subtype_ids[] = $subtype_id;
} else {
$valid_subtypes_count--;
$this->logger->notice("Type-subtype '{$type}:{$subtype}' does not exist!");
continue;
}
}
}
// return false if we're all invalid subtypes in the only valid type
if ($valid_subtypes_count <= 0) {
return false;
}
}
if (is_array($subtype_ids) && count($subtype_ids)) {
$subtype_ids_str = implode(',', $subtype_ids);
$wheres[] = "({$table}.type = '{$type}' AND {$table}.subtype IN ({$subtype_ids_str}))";
} else {
$wheres[] = "({$table}.type = '{$type}')";
}
}
} else {
// using type/subtype pairs
$valid_pairs_count = count($pairs);
$valid_pairs_subtypes_count = 0;
// same deal as above--we need to know how many valid types
// and subtypes we have before hitting the subtype section.
// also normalize the subtypes into arrays here.
foreach ($pairs as $paired_type => $paired_subtypes) {
if (!in_array($paired_type, $valid_types)) {
$valid_pairs_count--;
unset($pairs[array_search($paired_type, $pairs)]);
} else {
if ($paired_subtypes && !is_array($paired_subtypes)) {
$pairs[$paired_type] = array($paired_subtypes);
}
$valid_pairs_subtypes_count += count($paired_subtypes);
}
}
if ($valid_pairs_count <= 0) {
return false;
}
foreach ($pairs as $paired_type => $paired_subtypes) {
// this will always be an array because of line 2027, right?
// no...some overly clever person can say pair => array('object' => null)
if (is_array($paired_subtypes)) {
$paired_subtype_ids = array();
foreach ($paired_subtypes as $paired_subtype) {
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--;
$this->logger->notice("Type-subtype '{$paired_type}:{$paired_subtype}' does not exist!");
// return false if we're all invalid subtypes in the only valid type
continue;
}
}
// return false if there are no valid subtypes.
if ($valid_pairs_subtypes_count <= 0) {
return false;
}
if ($paired_subtype_ids_str = implode(',', $paired_subtype_ids)) {
$wheres[] = "({$table}.type = '{$paired_type}'" . " AND {$table}.subtype IN ({$paired_subtype_ids_str}))";
}
} else {
$wheres[] = "({$table}.type = '{$paired_type}')";
}
}
}
// pairs override the above. return false if they don't exist.
if (is_array($wheres) && count($wheres)) {
$where = implode(' OR ', $wheres);
return "({$where})";
}
return '';
}