class AddForm(base.AddForm):
    """Portlet add form.

    This is registered in configure.zcml. The form_fields variable tells
    zope.formlib which fields to display. The create() method actually
    constructs the assignment that is being added.
    """
    if PLONE4:
        form_fields = form.Fields(IRelatedItems)
    else:
        schema = IRelatedItems

    label = _(u"Add Related Items Portlet")
    description = _(u"This portlet displays recent Related Items.")

    def create(self, data):
        return Assignment(
            portlet_title=data.get('portlet_title', u'Related Items'),
            count=data.get('count', 5),
            states=data.get('states', ('published',)),
            allowed_types=data.get('allowed_types',
                                   DEFAULT_ALLOWED_TYPES),
            #show_recent_items=data.get('show_recent_items', False),
            only_subject=data.get('only_subject', False),
            show_all_types=data.get('show_all_types', False),
            display_all_fallback=data.get('display_all_fallback', True),
            display_description=data.get('display_description', True),
        )
class EditForm(base.EditForm):
    """Portlet edit form.

    This is registered with configure.zcml. The form_fields variable tells
    zope.formlib which fields to display.
    """
    if PLONE4:
        form_fields = form.Fields(IRelatedItems)
    else:
        schema = IRelatedItems

    label = _(u"Edit Related Items Portlet")
    description = _(u"This portlet displays related items based on "
                     "keywords matches, title and related items.")
 def title(self):
     """This property is used to give the title of the portlet in the
     "manage portlets" screen.
     """
     return self.portlet_title or _(u"Related Items")
class IRelatedItems(IPortletDataProvider):
    """A portlet

    It inherits from IPortletDataProvider because for this portlet, the
    data that is being rendered and the portlet assignment itself are the
    same.
    """

    portlet_title = schema.TextLine(
        title=_(u'Portlet title'),
        description=_(u'Title in portlet.'),
        required=True,
        default=_(u'Related Items')
    )

    count = schema.Int(
        title=_(u'Number of related items to display'),
        description=_(u'How many related items to list.'),
        required=True,
        default=10
    )

    states = schema.Tuple(
        title=_(u"Workflow state"),
        description=_(u"Items in which workflow state to show."),
        default=('published', ),
        required=True,
        value_type=schema.Choice(
            vocabulary="plone.app.vocabularies.WorkflowStates"
        )
    )

    allowed_types = schema.Tuple(
        title=_(u"Allowed Types"),
        description=_(u"Select the content types that should be shown."),
        default=DEFAULT_ALLOWED_TYPES,
        required=True,
        value_type=schema.Choice(
            vocabulary="plone.app.vocabularies.ReallyUserFriendlyTypes"
        )
    )

    sorting = schema.Choice(
        title=_(u"Sorting criteria"),
        description=_(u"Select the sorting criteria for the collected contents."),
        default='modified',
        required=True,
        vocabulary=SORTING_CRITERIA,
    )

    show_all_types = schema.Bool(
        title=_(u"Show all types in 'more' link"),
        description=_(u"If selected, the 'more' link will display "
                       "results from all content types instead of "
                       "restricting to the 'Allowed Types'."),
        default=False,
    )

    only_subject = schema.Bool(
        title=_(u"Search only on subject"),
        description=_(u"If selected, we will search only on content subject."),
        default=False,
    )

    display_all_fallback = schema.Bool(
        title=_(u"Display recent items on no results fallback"),
        description=_(u"If selected, we will display all "
                      "allowed items where there are no results for "
                      "our current related items query"),
        default=True,
    )

    display_description = schema.Bool(
        title=_(u"Display description"),
        description=_(
            u"If selected, we will show the content short description"),
        default=True,
    )
    """"
 def title(self):
     """This property is used to give the title of the portlet in the
     "manage portlets" screen.
     """
     return self.portlet_title or _(u"Related Items")
    from zope.formlib import form


_N_CHAR = 100

DEFAULT_ALLOWED_TYPES = (
    'News Item',
    'Document',
    'Event',
    'File',
    'Image',
)


SORTING_CRITERIA = SimpleVocabulary.fromItems((
    (_(u"Modification date"), "modified"),
    (_(u"Effective date"), "effective"),
    (_(u"Creation date"), "created"),
))


# used to sanitize search
def quotestring(s):
    return '"%s"' % s


def quote_bad_chars(s):
    bad_chars = ["(", ")"]
    for char in bad_chars:
        s = s.replace(char, quotestring(char))
    return s
    from collective.contentleadimage.leadimageprefs import ILeadImagePrefsForm
    from zope.component import getUtility
    from Products.CMFPlone.interfaces import IPloneSiteRoot

    LEADIMAGE_EXISTS = True
except ImportError:
    LEADIMAGE_EXISTS = False


_N_CHAR = 100

DEFAULT_ALLOWED_TYPES = ("News Item", "Document", "Event", "File", "Image")


SORTING_CRITERIA = SimpleVocabulary.fromItems(
    ((_(u"Modification date"), "modified"), (_(u"Effective date"), "effective"), (_(u"Creation date"), "created"))
)


# used to sanitize search
def quotestring(s):
    return '"%s"' % s


def quote_bad_chars(s):
    bad_chars = ["(", ")"]
    for char in bad_chars:
        s = s.replace(char, quotestring(char))
    return s