72 $this->
table = $this->db->prefix .
"metadata";
94 function delete(
$id) {
124 $allow_multiple = (boolean)$allow_multiple;
131 $owner_guid = $this->session->getLoggedInUserGuid();
134 $access_id = (int) $access_id;
136 $query =
"SELECT * FROM {$this->table} 137 WHERE entity_guid = :entity_guid and name_id = :name_id LIMIT 1";
141 ':name_id' => $this->metastringsTable->getId(
$name)
144 $existing = $this->db->getDataRow($query, null,
$params);
145 if ($existing && !$allow_multiple) {
146 $id = (int)$existing->id;
159 $value_id = $this->metastringsTable->getId(
$value);
164 $name_id = $this->metastringsTable->getId(
$name);
170 $query =
"INSERT INTO {$this->table} 171 (entity_guid, name_id, value_id, value_type, owner_guid, time_created, access_id) 172 VALUES (:entity_guid, :name_id, :value_id, :value_type, :owner_guid, :time_created, :access_id)";
176 ':name_id' => $name_id,
177 ':value_id' => $value_id,
178 ':value_type' => $value_type,
180 ':time_created' => $time,
181 ':access_id' => $access_id,
187 $obj = $this->
get(
$id);
188 if ($this->events->trigger(
'create',
'metadata', $obj)) {
217 if (!$md = $this->
get(
$id)) {
220 if (!$md->canEdit()) {
228 $owner_guid = $this->session->getLoggedInUserGuid();
231 $access_id = (int)$access_id;
238 $value_id = $this->metastringsTable->getId(
$value);
243 $name_id = $this->metastringsTable->getId(
$name);
249 $query =
"UPDATE {$this->table} 250 SET name_id = :name_id, 251 value_id = :value_id, 252 value_type = :value_type, 253 access_id = :access_id, 254 owner_guid = :owner_guid 258 ':name_id' => $name_id,
259 ':value_id' => $value_id,
260 ':value_type' => $value_type,
261 ':access_id' => $access_id,
270 $this->cache->clear($md->entity_guid);
275 $obj = $this->
get(
$id);
276 $this->events->trigger(
'update',
'metadata', $obj);
301 foreach ($name_and_values as $k => $v) {
303 $access_id, $allow_multiple);
345 $options[
'metadata_calculation'] =
'count';
349 $options[
'metastring_type'] =
'metadata';
367 $options[
'metastring_type'] =
'metadata';
372 $this->cache->invalidateByOptions($options);
390 $this->cache->invalidateByOptions($options);
396 $options[
'metastring_type'] =
'metadata';
412 if (!$options || !is_array($options)) {
416 $this->cache->invalidateByOptions($options);
418 $options[
'metastring_type'] =
'metadata';
487 'metadata_name_value_pairs_operator' =>
'AND',
488 'metadata_case_sensitive' =>
true,
489 'order_by_metadata' => array(),
496 $singulars = array(
'metadata_name',
'metadata_value',
497 'metadata_name_value_pair',
'metadata_owner_guid');
505 return $this->entityTable->getEntities(
$options);
531 $pairs = null, $pair_operator =
'AND', $case_sensitive =
true, $order_by_metadata = null,
532 $owner_guids = null) {
537 if ((!$names && $names !== 0)
538 && (!$values && $values !== 0)
539 && (!$pairs && $pairs !== 0)
540 && (!$owner_guids && $owner_guids !== 0)
541 && !$order_by_metadata) {
551 $binary = ($case_sensitive) ?
' BINARY ' :
'';
554 'table_alias' =>
'n_table',
555 'guid_column' =>
'entity_guid',
565 $return[
'joins'][] =
"JOIN {$this->db->prefix}{$n_table} n_table on 566 {$e_table}.guid = n_table.entity_guid";
572 if ($names !== null) {
573 if (!is_array($names)) {
574 $names = array($names);
577 $sanitised_names = array();
578 foreach ($names as
$name) {
583 $sanitised_names[] =
'\'' . $this->db->sanitizeString($name) .
'\'';
586 if ($names_str = implode(
',', $sanitised_names)) {
587 $return[
'joins'][] =
"JOIN {$this->metastringsTable->getTableName()} msn on n_table.name_id = msn.id";
588 $names_where =
"(msn.string IN ($names_str))";
594 if ($values !== null) {
595 if (!is_array($values)) {
596 $values = array($values);
599 $sanitised_values = array();
600 foreach ($values as
$value) {
605 $sanitised_values[] =
'\'' . $this->db->sanitizeString($value) .
'\'';
608 if ($values_str = implode(
',', $sanitised_values)) {
609 $return[
'joins'][] =
"JOIN {$this->metastringsTable->getTableName()} msv on n_table.value_id = msv.id";
610 $values_where =
"({$binary}msv.string IN ($values_str))";
614 if ($names_where && $values_where) {
615 $wheres[] =
"($names_where AND $values_where AND $access)";
616 } elseif ($names_where) {
617 $wheres[] =
"($names_where AND $access)";
618 } elseif ($values_where) {
619 $wheres[] =
"($values_where AND $access)";
624 if (is_array($pairs)) {
626 if (isset($pairs[
'name']) || isset($pairs[
'value'])) {
627 $pairs = array($pairs);
630 $pair_wheres = array();
635 foreach ($pairs as
$index => $pair) {
638 if (!is_array($pair)) {
646 if (!isset($pair[
'name']) || !isset($pair[
'value'])) {
653 if (isset($pair[
'case_sensitive'])) {
654 $pair_binary = ($pair[
'case_sensitive']) ?
' BINARY ' :
'';
656 $pair_binary = $binary;
659 if (isset($pair[
'operand'])) {
660 $operand = $this->db->sanitizeString($pair[
'operand']);
666 $trimmed_operand = trim(strtolower($operand));
669 'table_alias' =>
"n_table{$i}",
670 'guid_column' =>
'entity_guid',
677 $num_safe_operands = array(
'>',
'<',
'>=',
'<=');
678 $num_test_operand = trim(strtoupper($operand));
680 if (is_numeric($pair[
'value']) && in_array($num_test_operand, $num_safe_operands)) {
681 $value = $this->db->sanitizeString($pair[
'value']);
682 }
else if (is_bool($pair[
'value'])) {
683 $value = (int)$pair[
'value'];
684 }
else if (is_array($pair[
'value'])) {
685 $values_array = array();
687 foreach ($pair[
'value'] as $pair_value) {
688 if (is_numeric($pair_value) && !in_array($num_test_operand, $num_safe_operands)) {
689 $values_array[] = $this->db->sanitizeString($pair_value);
691 $values_array[] =
"'" . $this->db->sanitizeString($pair_value) .
"'";
696 $value =
'(' . implode(
', ', $values_array) .
')';
702 }
else if ($trimmed_operand ==
'in') {
703 $value =
"({$pair['value']})";
705 $value =
"'" . $this->db->sanitizeString($pair[
'value']) .
"'";
708 $name = $this->db->sanitizeString($pair[
'name']);
711 $return[
'joins'][] =
"JOIN {$this->db->prefix}{$n_table} n_table{$i} 712 on {$e_table}.guid = n_table{$i}.entity_guid";
713 $return[
'joins'][] =
"JOIN {$this->metastringsTable->getTableName()} msn{$i} 714 on n_table{$i}.name_id = msn{$i}.id";
715 $return[
'joins'][] =
"JOIN {$this->metastringsTable->getTableName()} msv{$i} 716 on n_table{$i}.value_id = msv{$i}.id";
718 $pair_wheres[] =
"(msn{$i}.string = '$name' AND {$pair_binary}msv{$i}.string 719 $operand $value AND $access)";
724 if ($where = implode(
" $pair_operator ", $pair_wheres)) {
725 $wheres[] =
"($where)";
731 if (is_array($owner_guids)) {
732 $sanitised = array_map(
'sanitise_int', $owner_guids);
733 $owner_str = implode(
',', $sanitised);
735 $owner_str = (int)$owner_guids;
738 $wheres[] =
"(n_table.owner_guid IN ($owner_str))";
741 if ($where = implode(
' AND ', $wheres)) {
742 $return[
'wheres'][] =
"($where)";
745 if (is_array($order_by_metadata)) {
746 if ((count($order_by_metadata) > 0) && !isset($order_by_metadata[0])) {
748 $order_by_metadata = array($order_by_metadata);
750 foreach ($order_by_metadata as $order_by) {
751 if (is_array($order_by) && isset($order_by[
'name'])) {
752 $name = $this->db->sanitizeString($order_by[
'name']);
753 if (isset($order_by[
'direction'])) {
754 $direction = $this->db->sanitizeString($order_by[
'direction']);
758 $return[
'joins'][] =
"JOIN {$this->db->prefix}{$n_table} n_table{$i} 759 on {$e_table}.guid = n_table{$i}.entity_guid";
760 $return[
'joins'][] =
"JOIN {$this->metastringsTable->getTableName()} msn{$i} 761 on n_table{$i}.name_id = msn{$i}.id";
762 $return[
'joins'][] =
"JOIN {$this->metastringsTable->getTableName()} msv{$i} 763 on n_table{$i}.value_id = msv{$i}.id";
766 'table_alias' =>
"n_table{$i}",
767 'guid_column' =>
'entity_guid',
770 $return[
'wheres'][] =
"(msn{$i}.string = '$name' AND $access)";
771 if (isset($order_by[
'as']) && $order_by[
'as'] ==
'integer') {
772 $return[
'orders'][] =
"CAST(msv{$i}.string AS SIGNED) $direction";
774 $return[
'orders'][] =
"msv{$i}.string $direction";
794 $extender = $this->
get(
$id);
796 return $extender ? $extender->getURL() :
false;
847 $access_id = (int)
$object->access_id;
849 $query =
"update {$this->table} set access_id = {$access_id} where entity_guid = {$guid}";
850 $this->db->updateData($query);
$object
These two snippets demonstrates triggering an event and how to register for that event.
$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
static detectValueType($value, $value_type="")
Detect the value_type for a value to be stored as metadata or an annotation.
getCurrentTime($modifier= '')
Get the (cloned) time.
$guid
Removes an admin notice.
const ELGG_ENTITIES_ANY_VALUE
access_get_show_hidden_status()
Return current status of showing disabled entities.
trait TimeUsing
Adds methods for setting the current time (for testing)
_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
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