m_rsc (zotonic_core v1.0.0-rc.14)

Model for resource data. Interfaces between zotonic, templates and the database.

Link to this section Summary

Functions

Common properties, these are used by exporter and backup routines.
Delete a resource
Duplicate a resource.
Ensure that a resource has a name, caller must have update rights.
Read a whole resource. Return 'undefined' if the resource was not found, crash on database errors. The properties are filtered by the ACL.
Get the ACL fields for the resource with the id. Will always return a valid record, even if the resource does not exist.
Get all properties of a resource for export. This adds the page urls for all languages and the language neutral uri to the resource properties. Note that normally the page_url is a single property without translation, as it is generated using the current language context. As this export is used for UIs, all language variants are added.
Get the resource from the database, do not fetch the pivot fields and do not use the cached result. The properties are NOT filtered by the ACL.
Same as get_raw/2 but also lock the resource for update. The properties are NOT filtered by the ACL.
Insert a new resource
Return the categories and the inherited categories of the resource. Returns a list with category atoms
Check if the resource is in a category.
Return the categories and the inherited categories of the resource. Returns a list with category ids
Check if the resource is exactly the category
Fetch the value for the key from a model source
Make a resource authoritative. This removes the uri from the resource and sets the resource as 'authoritative'. The uri is added to the m_rsc_gone delete log.
Merge a resource with another, delete the loser.
Merge a resource with another, delete the loser.
Return the id of the resource with a certain unique name.
Return the id of the resource with the name
Used for dereferencing object edges inside template expressions
Return the list of objects with a certain predicate
Fetch a property from a resource. When the rsc does not exist, the property does not exist or the user does not have access rights to the property then return 'undefined'.
Fetch a property from a resource, no ACL check is done.
Given a page path, return {ok, Id} with the id of the found resource. When the resource does not have the page path, but did so once, this function will return {redirect, Id} to indicate that the page path was found but is no longer the current page path for the resource.
Fetch a resource id from any input
"Touch" the rsc, incrementing the version nr and the modification date/ modifier_id. This should be called as part of another update or transaction and does not resync the caches, and does not check the ACL. After "touching" the resource will be re-pivoted.
Update a resource
Determine the non informational uri of a resource.
Return the id of the resource with a certain uri.

Link to this section Types

-type digits() :: 48..57.
Link to this type

duplicate_option/0

-type duplicate_option() :: edges | {edges, boolean()} | medium | {medium, boolean()}.
Link to this type

duplicate_options/0

-type duplicate_options() :: [duplicate_option()].
-type props() :: map().
-type props_all() :: props() | props_legacy().
Link to this type

props_legacy/0

-type props_legacy() :: proplists:proplist().
-type resource() ::
    resource_id() | [digits()] | resource_name() | resource_uri() | resource_uri_map() | undefined.
Link to this type

resource_id/0

-type resource_id() :: integer().
Link to this type

resource_name/0

-type resource_name() :: string() | binary() | atom().
Link to this type

resource_uri/0

-type resource_uri() :: binary().
Link to this type

resource_uri_map/0

-type resource_uri_map() :: #{binary() => term()}.
Link to this type

update_function/0

-type update_function() ::
    fun((resource() | insert_rsc, props(), z:context()) ->
            {ok, UpdateProps :: props()} | {error, term()}).

Link to this section Functions

Link to this function

common_properties(Context)

Common properties, these are used by exporter and backup routines.
Link to this function

delete(Id, Context)

-spec delete(resource(), z:context()) -> ok | {error, term()}.
Delete a resource
Link to this function

delete(Id, FollowUp, Context)

-spec delete(resource(), resource(), z:context()) -> ok | {error, term()}.
Link to this function

duplicate(Id, Props, Context)

-spec duplicate(resource(), props_all(), z:context()) ->
             {ok, NewId :: resource_id()} | {error, Reason :: term()}.
Duplicate a resource.
Link to this function

duplicate(Id, Props, Options, Context)

-spec duplicate(resource(), props_all(), duplicate_options(), z:context()) ->
             {ok, NewId :: resource_id()} | {error, Reason :: term()}.
Link to this function

ensure_name(Id, Context)

-spec ensure_name(integer(), z:context()) -> ok.
Ensure that a resource has a name, caller must have update rights.
Link to this function

exists(Id, Context)

-spec exists(resource(), z:context()) -> boolean().
Link to this function

get(Id, Context)

-spec get(resource(), z:context()) -> map() | undefined.
Read a whole resource. Return 'undefined' if the resource was not found, crash on database errors. The properties are filtered by the ACL.
Link to this function

get_acl_props(Id, Context)

-spec get_acl_props(Id :: resource(), z:context()) -> #acl_props{}.
Get the ACL fields for the resource with the id. Will always return a valid record, even if the resource does not exist.
Link to this function

get_export(Id, Context)

-spec get_export(Id, Context) -> {ok, Rsc} | {error, Reason}
              when
                  Id :: resource(),
                  Context :: z:context(),
                  Rsc :: props(),
                  Reason :: eacces | enoent.
Get all properties of a resource for export. This adds the page urls for all languages and the language neutral uri to the resource properties. Note that normally the page_url is a single property without translation, as it is generated using the current language context. As this export is used for UIs, all language variants are added.
Link to this function

get_raw(Id, Context)

-spec get_raw(resource(), z:context()) -> {ok, map()} | {error, term()}.
Get the resource from the database, do not fetch the pivot fields and do not use the cached result. The properties are NOT filtered by the ACL.
Link to this function

get_raw_lock(Id, Context)

-spec get_raw_lock(resource(), z:context()) -> {ok, map()} | {error, term()}.
Same as get_raw/2 but also lock the resource for update. The properties are NOT filtered by the ACL.
Link to this function

insert(Props, Context)

-spec insert(props_all(), z:context()) -> {ok, resource_id()} | {error, term()}.
Insert a new resource
Link to this function

insert(Props, Options, Context)

-spec insert(props_all(), list(), z:context()) -> {ok, resource_id()} | {error, term()}.
Link to this function

is_a(Id, Context)

-spec is_a(resource(), z:context()) -> [atom()].
Return the categories and the inherited categories of the resource. Returns a list with category atoms
Link to this function

is_a(Id, Cat, Context)

-spec is_a(resource(), m_category:category(), z:context()) -> boolean().
Check if the resource is in a category.
Link to this function

is_a_id(Id, Context)

-spec is_a_id(resource(), z:context()) -> [pos_integer()].
Return the categories and the inherited categories of the resource. Returns a list with category ids
Link to this function

is_cat(Id, Cat, Context)

-spec is_cat(resource(), atom(), z:context()) -> boolean().
Check if the resource is exactly the category
Link to this function

is_deletable(Id, Context)

-spec is_deletable(resource(), z:context()) -> boolean().
Link to this function

is_editable(Id, Context)

-spec is_editable(resource(), z:context()) -> boolean().
Link to this function

is_linkable(Id, Context)

-spec is_linkable(resource(), z:context()) -> boolean().
Link to this function

is_me(Id, Context)

-spec is_me(resource(), z:context()) -> boolean().
Link to this function

is_visible(Id, Context)

-spec is_visible(resource(), z:context()) -> boolean().
Link to this function

m_delete(_, Msg, Context)

Link to this function

m_get(Rest, Msg, Context)

-spec m_get(list(), zotonic_model:opt_msg(), z:context()) -> zotonic_model:return().
Fetch the value for the key from a model source
Link to this function

make_authoritative(RscId, Context)

-spec make_authoritative(m_rsc:resource(), z:context()) -> {ok, resource_id()} | {error, term()}.
Make a resource authoritative. This removes the uri from the resource and sets the resource as 'authoritative'. The uri is added to the m_rsc_gone delete log.
Link to this function

media(Id, Context)

-spec media(resource(), z:context()) -> list().
Link to this function

merge_delete(WinnerId, LoserId, Context)

-spec merge_delete(resource(), resource(), z:context()) -> ok | {error, term()}.
Merge a resource with another, delete the loser.
Link to this function

merge_delete(WinnerId, LoserId, Options, Context)

-spec merge_delete(resource(), resource(), list(), z:context()) -> ok | {error, term()}.
Merge a resource with another, delete the loser.
Link to this function

name_lookup(Name, Context)

-spec name_lookup(resource_name(), z:context()) -> resource_id() | undefined.
Return the id of the resource with a certain unique name.
Link to this function

name_to_id(Name, Context)

-spec name_to_id(resource_name(), z:context()) ->
              {ok, resource_id()} | {error, {unknown_rsc, resource_name()}}.
Return the id of the resource with the name
Link to this function

name_to_id_cat(Name, Cat, Context)

-spec name_to_id_cat(resource(), resource_name(), z:context()) ->
                  {ok, resource_id()} | {error, {unknown_rsc_cat, resource(), resource_name()}}.
-spec o(resource(), z:context()) -> fun().
Used for dereferencing object edges inside template expressions
Link to this function

o(Id, Predicate, Context)

-spec o(resource(), atom(), z:context()) -> list().
Return the list of objects with a certain predicate
Link to this function

o(Id, Predicate, N, Context)

-spec o(resource(), atom(), pos_integer(), z:context()) -> resource_id() | undefined.
Link to this function

op(Id, Context)

-spec op(resource(), z:context()) -> list().
Link to this function

p(Id, Property, Context)

-spec p(resource(), atom() | binary() | string(), z:context()) -> term() | undefined.
Fetch a property from a resource. When the rsc does not exist, the property does not exist or the user does not have access rights to the property then return 'undefined'.
Link to this function

p(Id, Property, DefaultValue, Context)

Link to this function

p_no_acl(Id, Predicate, Context)

Fetch a property from a resource, no ACL check is done.
Link to this function

page_path_to_id(Path, Context)

-spec page_path_to_id(binary() | string(), z:context()) ->
                   {ok, resource_id()} |
                   {redirect, resource_id()} |
                   {error, {unknown_page_path, binary()}} |
                   {error, {illegal_page_path, binary(), length | unicode}}.
Given a page path, return {ok, Id} with the id of the found resource. When the resource does not have the page path, but did so once, this function will return {redirect, Id} to indicate that the page path was found but is no longer the current page path for the resource.
Link to this function

page_url(Id, Context)

-spec page_url(resource(), z:context()) -> iodata() | undefined.
Link to this function

page_url_abs(Id, Context)

-spec page_url_abs(resource(), z:context()) -> iodata() | undefined.
Link to this function

rid(Id, Context)

-spec rid(resource() | #trans{}, z:context()) -> resource_id() | undefined.
Fetch a resource id from any input
-spec s(resource(), z:context()) -> fun().
Link to this function

s(Id, Predicate, Context)

-spec s(resource(), atom(), z:context()) -> list().
Link to this function

s(Id, Predicate, N, Context)

-spec s(resource(), atom(), pos_integer(), z:context()) -> resource_id() | undefined.
Link to this function

sp(Id, Context)

-spec sp(resource(), z:context()) -> list().
Link to this function

touch(Id, Context)

-spec touch(resource(), z:context()) -> {ok, resource_id()} | {error, enoent}.
"Touch" the rsc, incrementing the version nr and the modification date/ modifier_id. This should be called as part of another update or transaction and does not resync the caches, and does not check the ACL. After "touching" the resource will be re-pivoted.
Link to this function

update(Id, Props, Context)

-spec update(resource(), props_all() | update_function(), z:context()) ->
          {ok, resource()} | {error, term()}.
Update a resource
Link to this function

update(Id, Props, Options, Context)

-spec update(resource(), props_all() | update_function(), list(), z:context()) ->
          {ok, resource()} | {error, term()}.
Link to this function

uri(Id, Context)

-spec uri(resource() | undefined, z:context()) -> binary() | undefined.
Determine the non informational uri of a resource.
Link to this function

uri_lookup(Uri, Context)

-spec uri_lookup(resource_uri() | string(), z:context()) -> resource_id() | undefined.
Return the id of the resource with a certain uri.