Enabling Members to Create and Edit Content

A website may have several types of users. Admin users can modify the site via the control panel, while visitors who are not logged can view public pages.

Member are a third category of user: people who have user accounts and are logged in, but as members, rather than admins. A member can be given access to view private content.

Members can edit their user profile, which can have whatever fields you'd like, but cannot, in general modify website content.

You can, however, enable members to create database items, and to edit database items they "own" (that is, that they created, or that an admin created and linked to that member).

Member Creation of Database Items

You can set up forms for members to create items, just as you can create forms for non-members (see Creating Forms for User Submissions). By putting the form on a member-only page, you can restrict access to members only.

Generally, you will want to associate the item with the member who created it. To do so, include a "related items" field in your custom user profile definition (see Creating Custom User Profiles), to associate the member with the type of item they will be creating. You'll also generally want to create the association from the item, by adding to the item type definition a related item field that associates the item with the webvanta_user_profile item type (the member profile).

To make the association when a new item is created, include a hidden field in the form with the appropriate name, which is of the form:

form-name[dymd][field-name][node_ids][ ]

Replace "form-name" with the css name of your submission form, and "field-name" with the name of the field that relates the member to the item. For example, if your form is called "story-submission" and the member profile has a related item field named "story", then the field would be named:

story-submission[dymd][story][node_ids][ ]

This field must be set to the profile ID of the member. Generally, the best way to get this is from the cookie that the Webvanta system creates when a user logs in. Webvanta injects a set of JavaScript utilities that includes a function you can use to access this cookie:

var pid = WebvantaAdmin.getUIRValue('pid');

So let's suppose you have a form that includes this code:

<input type="hidden" name="story-submission[dymd][story][node_ids][ ]" id="pid-field">

Then you would include JavaScript code like this:

$("#pid-field").val(WebvantaAdmin.getUIRValue("pid"))

to set the value of the hidden field to the profile ID fetched from the cookie. This associates the new item with the user who created it.

Member Editing of Database Items

To enable users to edit items that they own, you can create an "edit" version of the item form page.

Start with your item creation form, and add two hidden fields, with the following special names:

  • pid_field_name
  • form_name[item-type-name][id]

The first field tells the system what field in user profile relates the user to the item. You must set the value of this field to a combination of two values, with a hyphen between them: the current member's pid and the name of the field that relates the user to the item.

Following the previous example, the value for pid_field_name would be:

WebvantaAdmin.getUIRValue("pid") + "-story"

The second hidden field is also set to a combination of two values, separated by a hyphen. The values are the ID of the item being updated, and the MD5 hash that is automatically created for each item.

In general, your item edit page will be a page of type "item", so you can set the item context by wrapping the page is w:kb:item. Once this is done, creating the hidden field value is easy:

<w:id_number />-<w:md5 />

Finally, you'll want to initialize the values of the form with the current values of the database item, so items that the user isn't changing will remain the same.