# A Vellum plugin: adds category support

# Remember, plugins that add an attribute to an Entry object can't
# assume that it's there; some entries may have been created before
# the plugin was installed.

import vellum.hooks,vellum.Entry

# Add a new blank category attribute to every Entry
vellum.Entry.Entry.category = ''

# Print an entry's category in the entry list
def printListCategory(entry):
    print '<td>'
    if hasattr(entry,"category"):
        print entry.category
    print '</td>'

vellum.hooks.register_hook("entry-listdisplay-print",printListCategory)

# Print a form element on the new entry screen
def printNewCategory():
    print '<div class="plugin">\n'
    print 'Category:<br>\n'
    print '<input type="text" name="category">\n'
    print '</div>'

vellum.hooks.register_hook("entry-new-print",printNewCategory)

# Print a form element on the edit entry screen
def printEditCategory(entry):
    if hasattr(entry,"category"):
        category = entry.category
    else:
        category = ''
    print '''<div class="plugin">
    Category:<br>
    <input type="text" name="category" value="%s">
    </div>
    ''' % category

vellum.hooks.register_hook("entry-edit-print",printEditCategory)

# Grab the value that our form element has provided and add it to the entry
def saveCategoryText(entry,form):
    if form.has_key("category"):
        category = form.getvalue("category")
        entry.category = category

vellum.hooks.register_hook("entry-new-save",saveCategoryText)
vellum.hooks.register_hook("entry-edit-save",saveCategoryText)

__pluginname__ = "Categories"
__description__ = """The categories plugin allows you to add categories
to your entries. An entry's category appears in its entry.category attribute
for reference in your templates."""