Personal tools
You are here: Home OST Help Center How-tos Add an Audience Filter
Document Actions

Add an Audience Filter

This How-to applies to: Any version.

These instructions allow you to add an "audience" attribute to any component and then use that audience to control what users see in different parts of your website. This was originally developed for Maine which houses multiple mini-sites. The example below adds Audience to the Event Calendar. Although each component is unique in some way, the basic steps should be transferable.

Summary

Add a new attribute, Audience, to the Events Calendar component to indicate who is allowed to see this item. This is a required field, so every item in the component will have an Audience. Then change the user display to show only those items that have a certain Audience setting.

Add the new Audience attribute

Note: if the Audience selection options are the same as what you've already created for another component, you can skip steps 1 and 2 below. You do not need to re-create these scripts/methods. You can share the existing ones. For example, if you add Audience to News and Events, both those components can use the same selection options.

  1. Create selection options for property sheets. Use the ZMI to go to the /Common folder (this is a good place for scripts that may be used in multiple places on your site) and add "Script (Python)".
    id = selectAudience
    Parameter list = leave blank
    Body:
    values= ['ptla','lse','mej']

    return values
  2. Create selection options for add and edit forms. Again, go to the /Common folder and this time add "DTML-Method".
    id = selectAudienceDropdown
    Body:
    <dtml-if name="readonly">
    <dtml-in audience><dtml-var "_['sequence-item']"><br /></dtml-in>
    <dtml-else>
    <dtml-if name="audience">
    <select name="audience:list" multiple size="10">
    <dtml-in "selectAudience()">
    <option value='<dtml-var "_['sequence-item']">'
    <dtml-try>
    <dtml-if expr="_['sequence-item'] in audience"> selected</dtml-if>
    </dtml-try>>
    &dtml-sequence-item;</option>
    </dtml-in>
    </select>
    <dtml-else>
    <select name="audience:list" multiple size="10">
    <dtml-in "selectAudience()">
    <option value='<dtml-var "_['sequence-item']">'>&dtml-sequence-item;</option>
    </dtml-in>
    </select>
    </dtml-if audience>
    </dtml-if readonly>

    Note that the above code automatically gets the Audience selection options from the python script you created in step 1. It is possible to instead manually list the selection options. This would be necessary if you want to display something different than the option value. For example:

    <dtml-if name="readonly">
    <dtml-var "audience">
    <dtml-else>
    <dtml-if name="audience">
    <select name='audience'>
    <option value="ptla" <dtml-if "audience=='ptla'">selected </dtml-if>>ptla</option>
    <option value="lse" <dtml-if "audience=='lse'">selected </dtml-if>>lse</option>
    <option value="mej" <dtml-if "audience=='mej'">selected </dtml-if>>MEJP</option>
    </select>
    <dtml-else>
    <select name='audience'>
    <option value="ptla">ptla</option>
    <option value="lse">lse</option>
    <option value="mej">mej</option>
    </select>
    </dtml-if audience>
    </dtml-if readonly>

    Note here that we display 'MEJP' even though the actual value is 'mej'. In most cases your names will match and so you'll be able to use the first code example above.

  3. Add the new Audience attribute to the component product.
    • Use the ZMI to go to Control_Panel/Products/EventCalendar/Event
    • Click on the PropertySheets tab and then select Basic
    • Add property: name=audience, type=selection, value=selectAudience. Note that the value 'selectAudience' exactly matches the ID of the python script from step 1 above.
  4. Update the catalog. This step makes the new Audience attribute usable to other parts of the site.
    • Go to Data/EventCalendar/Catalog
    • Go to the Metadata tab and add (using the form at the bottom) audience.
    • Go to the Indexes tab and add audience (type = keyword).
    • Go back to the Indexes tab, check the box next to audience, and click Reindex.
  5. Add the new Audience attribute to admin views.
    • Use the ZMI to go to both Control_Panel/Products/EventCalendar/Event/index_html and Home/Admin/EventCalendar/calendarFields and add the following code wherever you want the new Audience attribute to appear:
      <tr valign="top">
      <td><font class="label">Audience:</font></td>
      <td><font class="datatext"><dtml-var selectAudienceDropdown></font></td>
      </tr>
    • Go to Home/Admin/EventCalendar/calendarReqFields and add the following code. This makes the required * appear.
      context.addReqField(context.REQUEST, 'audience')
    • Go to Home/Admin/EventCalendar/calendarValFields and add the following code. This produces an error if the field is left blank. It also restricts the audience value length to 30, but that usually applies to free-form text fields. Just make sure the length specified here is long enough to handle your longest selection option.
      context.valNotEmpty(REQUEST, 'audience', 30)
  6. Add a filter to the user view so users see only those items they're supposed to see.
    <dtml-in expr="Data.EventCalendar.Catalog(audience='ptla', 
    ...leave other filters unchanged...)">
    Another variation is if you want users to see events with multiple audience settings. The following example displays events that have an audience setting of 'ptla' OR 'lse'.
    <dtml-in expr="Data.EventCalendar.Catalog(audience=['ptla'.'lse'], 
    ...leave other filters unchanged...)">

see also:

Add an Audience Filter PLUS Security
These instructions are very similar to the Add an Audience instructions, except that they tie the audience into your security model. For this enhancement you add an "audience" attribute to any component and then use that audience to control what users see by adding associated roles to the acl_users folder. The example below adds Audience to News. Although each component is unique in some way, the basic steps should be transferable.
by Judy Bennett last modified 2006-03-23 10:47 &copy; 2006 by Kaivo Software, Inc.

Powered by Plone, the Open Source Content Management System

This site conforms to the following standards: