23 private $independents = array();
35 private $metastringsTable;
63 $this->cache = $cache;
65 $this->entityTable = $entityTable;
67 $this->metastringsTable = $metastringsTable;
68 $this->session = $session;
69 $this->
table = $this->db->getTablePrefix() .
"metadata";
91 function delete(
$id) {
121 $allow_multiple = (boolean)$allow_multiple;
128 $owner_guid = $this->session->getLoggedInUserGuid();
131 $access_id = (int)$access_id;
133 $query =
"SELECT * from {$this->table}" 134 .
" WHERE entity_guid = $entity_guid and name_id=" . $this->metastringsTable->getId(
$name) .
" limit 1";
136 $existing = $this->db->getDataRow($query);
137 if ($existing && !$allow_multiple) {
138 $id = (int)$existing->id;
151 $value_id = $this->metastringsTable->getId(
$value);
156 $name_id = $this->metastringsTable->getId(
$name);
162 $query =
"INSERT into {$this->table}" 163 .
" (entity_guid, name_id, value_id, value_type, owner_guid, time_created, access_id)" 164 .
" VALUES ($entity_guid, '$name_id','$value_id','$value_type', $owner_guid, $time, $access_id)";
166 $id = $this->db->insertData($query);
169 $obj = $this->
get(
$id);
170 if ($this->
events->trigger(
'create',
'metadata', $obj)) {
199 if (!$md = $this->
get(
$id)) {
202 if (!$md->canEdit()) {
207 static $metabyname_memcache;
209 $metabyname_memcache = new \ElggMemcache(
'metabyname_memcache');
212 if ($metabyname_memcache) {
214 $metabyname_memcache->delete(
"{$md->entity_guid}:{$md->name_id}");
220 if ($owner_guid == 0) {
221 $owner_guid = $this->session->getLoggedInUserGuid();
224 $access_id = (int)$access_id;
231 $value_id = $this->metastringsTable->getId(
$value);
236 $name_id = $this->metastringsTable->getId(
$name);
242 $query =
"UPDATE {$this->table}" 243 .
" set name_id='$name_id', value_id='$value_id', value_type='$value_type', access_id=$access_id," 244 .
" owner_guid=$owner_guid where id=$id";
246 $result = $this->db->updateData($query);
249 $this->cache->clear($md->entity_guid);
254 $obj = $this->
get(
$id);
255 $this->
events->trigger(
'update',
'metadata', $obj);
280 foreach ($name_and_values as $k => $v) {
282 $access_id, $allow_multiple);
324 $options[
'metadata_calculation'] =
'count';
328 $options[
'metastring_type'] =
'metadata';
346 $options[
'metastring_type'] =
'metadata';
351 $this->cache->invalidateByOptions($options);
369 $this->cache->invalidateByOptions($options);
375 $options[
'metastring_type'] =
'metadata';
391 if (!$options || !is_array($options)) {
395 $this->cache->invalidateByOptions($options);
397 $options[
'metastring_type'] =
'metadata';
466 'metadata_name_value_pairs_operator' =>
'AND',
467 'metadata_case_sensitive' =>
true,
468 'order_by_metadata' => array(),
475 $singulars = array(
'metadata_name',
'metadata_value',
476 'metadata_name_value_pair',
'metadata_owner_guid');
484 return $this->entityTable->getEntities(
$options);
510 $pairs = null, $pair_operator =
'AND', $case_sensitive =
true, $order_by_metadata = null,
511 $owner_guids = null) {
516 if ((!$names && $names !== 0)
517 && (!$values && $values !== 0)
518 && (!$pairs && $pairs !== 0)
519 && (!$owner_guids && $owner_guids !== 0)
520 && !$order_by_metadata) {
530 $binary = ($case_sensitive) ?
' BINARY ' :
'';
541 $return[
'joins'][] =
"JOIN {$this->db->getTablePrefix()}{$n_table} n_table on 542 {$e_table}.guid = n_table.entity_guid";
548 if ($names !== null) {
549 if (!is_array($names)) {
550 $names = array($names);
553 $sanitised_names = array();
554 foreach ($names as
$name) {
559 $sanitised_names[] =
'\'' . $this->db->sanitizeString($name) .
'\'';
562 if ($names_str = implode(
',', $sanitised_names)) {
563 $return[
'joins'][] =
"JOIN {$this->metastringsTable->getTableName()} msn on n_table.name_id = msn.id";
564 $names_where =
"(msn.string IN ($names_str))";
570 if ($values !== null) {
571 if (!is_array($values)) {
572 $values = array($values);
575 $sanitised_values = array();
576 foreach ($values as
$value) {
581 $sanitised_values[] =
'\'' . $this->db->sanitizeString($value) .
'\'';
584 if ($values_str = implode(
',', $sanitised_values)) {
585 $return[
'joins'][] =
"JOIN {$this->metastringsTable->getTableName()} msv on n_table.value_id = msv.id";
586 $values_where =
"({$binary}msv.string IN ($values_str))";
590 if ($names_where && $values_where) {
591 $wheres[] =
"($names_where AND $values_where AND $access)";
592 } elseif ($names_where) {
593 $wheres[] =
"($names_where AND $access)";
594 } elseif ($values_where) {
595 $wheres[] =
"($values_where AND $access)";
600 if (is_array($pairs)) {
602 if (isset($pairs[
'name']) || isset($pairs[
'value'])) {
603 $pairs = array($pairs);
606 $pair_wheres = array();
611 foreach ($pairs as $index => $pair) {
614 if (!is_array($pair)) {
622 if (!isset($pair[
'name']) || !isset($pair[
'value'])) {
629 if (isset($pair[
'case_sensitive'])) {
630 $pair_binary = ($pair[
'case_sensitive']) ?
' BINARY ' :
'';
632 $pair_binary = $binary;
635 if (isset($pair[
'operand'])) {
636 $operand = $this->db->sanitizeString($pair[
'operand']);
642 $trimmed_operand = trim(strtolower($operand));
650 $num_safe_operands = array(
'>',
'<',
'>=',
'<=');
651 $num_test_operand = trim(strtoupper($operand));
653 if (is_numeric($pair[
'value']) && in_array($num_test_operand, $num_safe_operands)) {
654 $value = $this->db->sanitizeString($pair[
'value']);
655 }
else if (is_bool($pair[
'value'])) {
656 $value = (int)$pair[
'value'];
657 }
else if (is_array($pair[
'value'])) {
658 $values_array = array();
660 foreach ($pair[
'value'] as $pair_value) {
661 if (is_numeric($pair_value) && !in_array($num_test_operand, $num_safe_operands)) {
662 $values_array[] = $this->db->sanitizeString($pair_value);
664 $values_array[] =
"'" . $this->db->sanitizeString($pair_value) .
"'";
669 $value =
'(' . implode(
', ', $values_array) .
')';
675 }
else if ($trimmed_operand ==
'in') {
676 $value =
"({$pair['value']})";
678 $value =
"'" . $this->db->sanitizeString($pair[
'value']) .
"'";
681 $name = $this->db->sanitizeString($pair[
'name']);
684 $return[
'joins'][] =
"JOIN {$this->db->getTablePrefix()}{$n_table} n_table{$i} 685 on {$e_table}.guid = n_table{$i}.entity_guid";
686 $return[
'joins'][] =
"JOIN {$this->metastringsTable->getTableName()} msn{$i} 687 on n_table{$i}.name_id = msn{$i}.id";
688 $return[
'joins'][] =
"JOIN {$this->metastringsTable->getTableName()} msv{$i} 689 on n_table{$i}.value_id = msv{$i}.id";
691 $pair_wheres[] =
"(msn{$i}.string = '$name' AND {$pair_binary}msv{$i}.string 692 $operand $value AND $access)";
697 if ($where = implode(
" $pair_operator ", $pair_wheres)) {
698 $wheres[] =
"($where)";
704 if (is_array($owner_guids)) {
705 $sanitised = array_map(
'sanitise_int', $owner_guids);
706 $owner_str = implode(
',', $sanitised);
708 $owner_str = (int)$owner_guids;
711 $wheres[] =
"(n_table.owner_guid IN ($owner_str))";
714 if ($where = implode(
' AND ', $wheres)) {
715 $return[
'wheres'][] =
"($where)";
718 if (is_array($order_by_metadata)) {
719 if ((count($order_by_metadata) > 0) && !isset($order_by_metadata[0])) {
721 $order_by_metadata = array($order_by_metadata);
723 foreach ($order_by_metadata as $order_by) {
724 if (is_array($order_by) && isset($order_by[
'name'])) {
725 $name = $this->db->sanitizeString($order_by[
'name']);
726 if (isset($order_by[
'direction'])) {
727 $direction = $this->db->sanitizeString($order_by[
'direction']);
731 $return[
'joins'][] =
"JOIN {$this->db->getTablePrefix()}{$n_table} n_table{$i} 732 on {$e_table}.guid = n_table{$i}.entity_guid";
733 $return[
'joins'][] =
"JOIN {$this->metastringsTable->getTableName()} msn{$i} 734 on n_table{$i}.name_id = msn{$i}.id";
735 $return[
'joins'][] =
"JOIN {$this->metastringsTable->getTableName()} msv{$i} 736 on n_table{$i}.value_id = msv{$i}.id";
740 $return[
'wheres'][] =
"(msn{$i}.string = '$name' AND $access)";
741 if (isset($order_by[
'as']) && $order_by[
'as'] ==
'integer') {
742 $return[
'orders'][] =
"CAST(msv{$i}.string AS SIGNED) $direction";
744 $return[
'orders'][] =
"msv{$i}.string $direction";
764 $extender = $this->
get(
$id);
766 return $extender ? $extender->getURL() :
false;
817 $access_id = (int)
$object->access_id;
819 $query =
"update {$this->table} set access_id = {$access_id} where entity_guid = {$guid}";
820 $this->db->updateData($query);
$CONFIG independents
A list of entity types and subtypes that have metadata whose access permission can be changed indepen...
_elgg_is_valid_options_for_batch_operation($options, $type)
Checks if there are some constraints on the options array for potentially dangerous operations...
if($guid==elgg_get_logged_in_user_guid()) $name
$guid
Removes an admin notice.
events($event="", $object_type="", $function="", $priority=500, $call=false, $object=null)
Deprecated events core function.
detect_extender_valuetype($value, $value_type="")
Detect the value_type for a given value.
const ELGG_ENTITIES_ANY_VALUE
access_get_show_hidden_status()
Return current status of showing disabled entities.
is_memcache_available()
Return true if memcache is available and configured.
_elgg_normalize_plural_options_array($options, $singulars)
Normalise the singular keys in an options array to plural keys.
_elgg_get_access_where_sql(array $options=array())
Returns the SQL where clause for enforcing read access to data.
if(!$collection_name) $id