Back to index

plone3  3.1.7
search.py
Go to the documentation of this file.
00001 from zope.interface import Interface
00002 from zope.component import adapts
00003 from zope.formlib.form import FormFields
00004 from zope.interface import implements
00005 from zope.schema import Bool
00006 from zope.schema import Choice
00007 from zope.schema import Tuple
00008 
00009 from Products.CMFCore.utils import getToolByName
00010 from Products.CMFDefault.formlib.schema import SchemaAdapterBase
00011 from Products.CMFPlone import PloneMessageFactory as _
00012 from Products.CMFPlone.interfaces import IPloneSiteRoot
00013 
00014 from plone.app.vocabularies.types import BAD_TYPES
00015 
00016 from form import ControlPanelForm
00017 from widgets import MultiCheckBoxThreeColumnWidget
00018 
00019 
00020 class ISearchSchema(Interface):
00021 
00022     enable_livesearch = Bool(title=_(u'Enable LiveSearch'),
00023                              description=_(u"Enables the LiveSearch feature, "
00024                                             "which shows live results if the "
00025                                             "browser supports JavaScript."),
00026                              default=False,
00027                              required=True)
00028 
00029     types_not_searched = Tuple(title=_(u"Define the types to be shown in the "
00030                                         "site and searched"),
00031                                description=_(u"Define the types that should be "
00032                                              "searched and be available in the "
00033                                              "user facing part of the site. "
00034                                              "Note that if new content types "
00035                                              "are installed, they will be "
00036                                              "enabled by default unless "
00037                                              "explicitly turned off here or "
00038                                              "by the relevant installer."),
00039                                required=True,
00040                                missing_value=tuple(),
00041                                value_type=Choice(
00042                                    vocabulary="plone.app.vocabularies.ReallyUserFriendlyTypes"))
00043 
00044 
00045 class SearchControlPanelAdapter(SchemaAdapterBase):
00046 
00047     adapts(IPloneSiteRoot)
00048     implements(ISearchSchema)
00049 
00050     def __init__(self, context):
00051         super(SearchControlPanelAdapter, self).__init__(context)
00052         pprop = getToolByName(context, 'portal_properties')
00053         self.context = pprop.site_properties
00054         self.jstool = getToolByName(context, 'portal_javascripts')
00055         self.ttool = getToolByName(context, 'portal_types')
00056 
00057     def get_enable_livesearch(self):
00058         return self.context.enable_livesearch
00059 
00060     def set_enable_livesearch(self, value):
00061         if value:
00062             self.context.manage_changeProperties(enable_livesearch=True)
00063             self.jstool.getResource('livesearch.js').setEnabled(True)
00064         else:
00065             self.context.manage_changeProperties(enable_livesearch=False)
00066             self.jstool.getResource('livesearch.js').setEnabled(False)
00067         self.jstool.cookResources()
00068 
00069     enable_livesearch = property(get_enable_livesearch, set_enable_livesearch)
00070 
00071     def get_types_not_searched(self):
00072         return [t for t in self.ttool.listContentTypes()
00073                         if t not in self.context.types_not_searched and
00074                            t not in BAD_TYPES]
00075 
00076     def set_types_not_searched(self, value):
00077         value = [t for t in self.ttool.listContentTypes() if t not in value
00078                    and t not in BAD_TYPES]
00079         self.context._updateProperty('types_not_searched', value)
00080 
00081     # This also defines the user friendly types
00082     types_not_searched = property(get_types_not_searched,
00083                                   set_types_not_searched)
00084 
00085 
00086 class SearchControlPanel(ControlPanelForm):
00087 
00088     form_fields = FormFields(ISearchSchema)
00089     form_fields['types_not_searched'].custom_widget = MultiCheckBoxThreeColumnWidget
00090     form_fields['types_not_searched'].custom_widget.cssClass='label'
00091 
00092     label = _("Search settings")
00093     description = _("Search settings for this site.")
00094     form_name = _("Search settings")