Elgg\Database\Annotations::create PHP Method

create() public method

Create a new annotation.
public create ( integer $entity_guid, string $name, string $value, string $value_type = '', integer $owner_guid, integer $access_id = ACCESS_PRIVATE ) : integer | boolean
$entity_guid integer GUID of entity to be annotated
$name string Name of annotation
$value string Value of annotation
$value_type string Type of value (default is auto detection)
$owner_guid integer Owner of annotation (default is logged in user)
$access_id integer Access level of annotation
return integer | boolean id on success or false on failure
    function create($entity_guid, $name, $value, $value_type = '', $owner_guid = 0, $access_id = ACCESS_PRIVATE)
    {
        $result = false;
        $entity_guid = (int) $entity_guid;
        $value_type = detect_extender_valuetype($value, $value_type);
        $owner_guid = (int) $owner_guid;
        if ($owner_guid == 0) {
            $owner_guid = $this->session->getLoggedInUserGuid();
        }
        $access_id = (int) $access_id;
        // @todo we don't check that the entity is loaded which means the user may
        // not have access to the entity
        $entity = get_entity($entity_guid);
        if ($this->events->trigger('annotate', $entity->type, $entity)) {
            $sql = "INSERT INTO {$this->db->prefix}annotations\n\t\t\t\t(entity_guid, name, value, value_type, owner_guid, time_created, access_id)\n\t\t\t\tVALUES\n\t\t\t\t(:entity_guid, :name, :value, :value_type, :owner_guid, :time_created, :access_id)";
            $result = $this->db->insertData($sql, [':entity_guid' => $entity_guid, ':name' => $name, ':value' => $value, ':value_type' => $value_type, ':owner_guid' => $owner_guid, ':time_created' => $this->getCurrentTime()->getTimestamp(), ':access_id' => $access_id]);
            if ($result !== false) {
                $obj = elgg_get_annotation_from_id($result);
                if ($this->events->trigger('create', 'annotation', $obj)) {
                    return $result;
                } else {
                    // plugin returned false to reject annotation
                    elgg_delete_annotation_by_id($result);
                    return false;
                }
            }
        }
        return $result;
    }