function create($entity_guid, $name, $value, $value_type = '', $owner_guid = 0, $access_id = ACCESS_PRIVATE, $allow_multiple = false)
{
$entity_guid = (int) $entity_guid;
$value_type = detect_extender_valuetype($value, $this->db->sanitizeString(trim($value_type)));
$owner_guid = (int) $owner_guid;
$allow_multiple = (bool) $allow_multiple;
if (!isset($value)) {
return false;
}
if ($owner_guid == 0) {
$owner_guid = $this->session->getLoggedInUserGuid();
}
$access_id = (int) $access_id;
$query = "SELECT * FROM {$this->table}\n\t\t\tWHERE entity_guid = :entity_guid and name = :name LIMIT 1";
$existing = $this->db->getDataRow($query, null, [':entity_guid' => $entity_guid, ':name' => $name]);
if ($existing && !$allow_multiple) {
$id = (int) $existing->id;
$result = $this->update($id, $name, $value, $value_type, $owner_guid, $access_id);
if (!$result) {
return false;
}
} else {
// Support boolean types
if (is_bool($value)) {
$value = (int) $value;
}
// If ok then add it
$query = "INSERT INTO {$this->table}\n\t\t\t\t(entity_guid, name, value, value_type, owner_guid, time_created, access_id)\n\t\t\t\tVALUES (:entity_guid, :name, :value, :value_type, :owner_guid, :time_created, :access_id)";
$id = $this->db->insertData($query, [':entity_guid' => $entity_guid, ':name' => $name, ':value' => $value, ':value_type' => $value_type, ':owner_guid' => (int) $owner_guid, ':time_created' => $this->getCurrentTime()->getTimestamp(), ':access_id' => $access_id]);
if ($id !== false) {
$obj = $this->get($id);
if ($this->events->trigger('create', 'metadata', $obj)) {
$this->cache->clear($entity_guid);
return $id;
} else {
$this->delete($id);
}
}
}
return $id;
}