Skip to content

Module to easily create, read, update and delete zotonic resources.

License

Notifications You must be signed in to change notification settings

channelme/zotonic_mod_crud

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Zotonic CRUD Module

Zotonic's data model is very flexible and easy to use from templates. It is straightforward to display the author of a resource. Use titles, and even custom properties. For a web-designer however it is not easy to create and modify resources, because it requires knowledge of Erlang, the programming language Zotonic is written in.

This module makes it possible for HTML designers to safely create, update and delete resources by using standard web-forms and using zotonic's builtin template language.

Create

Creating resources from a template with a form can be done with the create postback.

create postback

Parameter Description
cat The category which the new resource
name A name of a valid form fields.
props Optional A pre-defined set of values the new resource must get
on_success Optional An action which will be performed after the update succeeds

Example::

{% wire id=#do_event
        type="submit"
        postback={create cat="event"
                         name="title"
                         name="dt:ymd:0:date_start"
                         name="o.is_going"
                         props = %{
                             is_published: true
                             is_dependent: true
                             date_is_all_day: true
                             content_group: "user_content"
                             "o.author": id
                             "o.is_interested": id
                         }
                         on_success={reload}
        }
        delegate="mod_crud"
%}
<form id="{{ #do_event }}">
    <input name="title" type="text"/>
    <input name="dt:ymd:0:date_start" type="date" value="{{ now | add_week | date:"Y-m-d" }}"/>
    <input name="o.is_going" type="checkbox" 
           value="{{ m.acl.user }}" />
</form>

The name parameters whitelist form elements send by the browser to the server. This is needed to be able to safely add resources to the zotonic database. When this is not done, anyone who has access to your web-pages can create arbitrary properties to your resources, which can be unsafe.

Note All normal text fields are autmatically escaped by Zotonic. When you display custom resource properties you have will to make sure those values are proper html. This can be done by using the escape filter.

Update

Existing resources can be changed using the update postback.

update postback

Parameter Description
id The id of the resource which must be updated.
name A name of a valid form field
on_success Optional An action which will be performed after the update succeeds
unlink_when_empty Optional Make it possible to unlink a resource when an entry is empty

Example::

{% wire id=#do_event
        type="submit"
        postback={update id=event_id
                         name="title"
                         name="dt:ymd:0:date_start"
                         name="o.is_going"
                         unlink_when_empty =  ["o.is_going", is_going_edge]
                         on_success={reload}
        }
        delegate="mod_crud"
%}
<form id="{{ #do_event }}">
    <input name="title" type="text"
           value="{{ id.title }}"/>
    <input name="dt:ymd:0:date_start" type="date" 
           value="{{ id.date_start | date:"Y-m-d" }}"/>
    <input name="o.is_going" type="checkbox" 
           value="{{ m.acl.user }}"
           {% if m.edge[id].is_going.[m.acl.user] %}checked{% endif %} />
</form>

Delete

delete postback

Parameter Description
id The id of the resource which must be deleted.
on_success Optional An action which will be performed after resource is deleted

About

Module to easily create, read, update and delete zotonic resources.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages