| 1 | from zope.interface import Interface |
|---|
| 2 | from zope.component import adapts |
|---|
| 3 | from zope.formlib import form |
|---|
| 4 | from zope.formlib.form import FormFields |
|---|
| 5 | from zope.interface import implements |
|---|
| 6 | from zope.schema import Bool |
|---|
| 7 | from zope.schema import Choice |
|---|
| 8 | |
|---|
| 9 | from Products.CMFCore.utils import getToolByName |
|---|
| 10 | from Products.CMFDefault.formlib.schema import SchemaAdapterBase |
|---|
| 11 | from Products.CMFPlone import PloneMessageFactory as _ |
|---|
| 12 | from Products.CMFPlone.interfaces import IPloneSiteRoot |
|---|
| 13 | |
|---|
| 14 | from plone.app.controlpanel.form import ControlPanelForm |
|---|
| 15 | from plone.app.controlpanel.widgets import DropdownChoiceWidget |
|---|
| 16 | |
|---|
| 17 | from zope.schema.vocabulary import SimpleTerm |
|---|
| 18 | from zope.schema.vocabulary import SimpleVocabulary |
|---|
| 19 | |
|---|
| 20 | ICON_VISIBILITY_CHOICES = { |
|---|
| 21 | _(u"Only for users who are logged in"): 'authenticated', |
|---|
| 22 | _(u"Never show icons"): 'disabled', |
|---|
| 23 | _(u"Always show icons"): 'enabled', |
|---|
| 24 | } |
|---|
| 25 | |
|---|
| 26 | ICON_VISIBILITY_VOCABULARY = SimpleVocabulary( |
|---|
| 27 | [SimpleTerm(v, v, k) for k, v in ICON_VISIBILITY_CHOICES.items()] |
|---|
| 28 | ) |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | class ISkinsSchema(Interface): |
|---|
| 32 | |
|---|
| 33 | theme = Choice(title=_(u'Default theme'), |
|---|
| 34 | description=_(u'''Select the default theme for the site.'''), |
|---|
| 35 | required=True, |
|---|
| 36 | missing_value=tuple(), |
|---|
| 37 | vocabulary="plone.app.vocabularies.Skins") |
|---|
| 38 | |
|---|
| 39 | mark_special_links = Bool(title=_(u'Mark external links'), |
|---|
| 40 | description=_(u"If enabled all external links " |
|---|
| 41 | "will be marked with link type " |
|---|
| 42 | "specific icons. If disabled " |
|---|
| 43 | "the 'external links open in new " |
|---|
| 44 | "window' setting has no effect."), |
|---|
| 45 | default=True) |
|---|
| 46 | |
|---|
| 47 | ext_links_open_new_window = Bool(title=_(u"External links open in new " |
|---|
| 48 | "window"), |
|---|
| 49 | description=_(u"If enabled all external " |
|---|
| 50 | "links in the content " |
|---|
| 51 | "region open in a new " |
|---|
| 52 | "window."), |
|---|
| 53 | default=False) |
|---|
| 54 | |
|---|
| 55 | icon_visibility = Choice(title=_(u'Show content type icons'), |
|---|
| 56 | description=_(u"If disabled the content icons " |
|---|
| 57 | "in folder listings and portlets " |
|---|
| 58 | "won't be visible."), |
|---|
| 59 | vocabulary=ICON_VISIBILITY_VOCABULARY) |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | class SkinsControlPanelAdapter(SchemaAdapterBase): |
|---|
| 63 | adapts(IPloneSiteRoot) |
|---|
| 64 | implements(ISkinsSchema) |
|---|
| 65 | |
|---|
| 66 | def __init__(self, context): |
|---|
| 67 | super(SkinsControlPanelAdapter, self).__init__(context) |
|---|
| 68 | self.context = getToolByName(context, 'portal_skins') |
|---|
| 69 | self.jstool = getToolByName(context, 'portal_javascripts') |
|---|
| 70 | ptool = getToolByName(context, 'portal_properties') |
|---|
| 71 | self.props = ptool.site_properties |
|---|
| 72 | |
|---|
| 73 | def get_theme(self): |
|---|
| 74 | return self.context.getDefaultSkin() |
|---|
| 75 | |
|---|
| 76 | def set_theme(self, value): |
|---|
| 77 | self.context.default_skin = value |
|---|
| 78 | |
|---|
| 79 | theme = property(get_theme, set_theme) |
|---|
| 80 | |
|---|
| 81 | def get_mark_special_links(self): |
|---|
| 82 | return self.jstool.getResource('mark_special_links.js').getEnabled() |
|---|
| 83 | |
|---|
| 84 | def set_mark_special_links(self, value): |
|---|
| 85 | if value: |
|---|
| 86 | self.jstool.getResource('mark_special_links.js').setEnabled(True) |
|---|
| 87 | else: |
|---|
| 88 | self.jstool.getResource('mark_special_links.js').setEnabled(False) |
|---|
| 89 | self.jstool.cookResources() |
|---|
| 90 | |
|---|
| 91 | mark_special_links = property(get_mark_special_links, |
|---|
| 92 | set_mark_special_links) |
|---|
| 93 | |
|---|
| 94 | def get_ext_links_open_new_window(self): |
|---|
| 95 | elonw = self.props.external_links_open_new_window |
|---|
| 96 | if elonw == 'true': |
|---|
| 97 | return True |
|---|
| 98 | return False |
|---|
| 99 | |
|---|
| 100 | def set_ext_links_open_new_window(self, value): |
|---|
| 101 | if value: |
|---|
| 102 | self.props.manage_changeProperties(external_links_open_new_window='true') |
|---|
| 103 | else: |
|---|
| 104 | self.props.manage_changeProperties(external_links_open_new_window='false') |
|---|
| 105 | self.jstool.cookResources() |
|---|
| 106 | |
|---|
| 107 | ext_links_open_new_window = property(get_ext_links_open_new_window, |
|---|
| 108 | set_ext_links_open_new_window) |
|---|
| 109 | |
|---|
| 110 | def get_icon_visibility(self): |
|---|
| 111 | return self.props.icon_visibility |
|---|
| 112 | |
|---|
| 113 | def set_icon_visibility(self, value): |
|---|
| 114 | self.props.manage_changeProperties(icon_visibility=value) |
|---|
| 115 | |
|---|
| 116 | icon_visibility = property(get_icon_visibility,set_icon_visibility) |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | class SkinsControlPanel(ControlPanelForm): |
|---|
| 120 | |
|---|
| 121 | form_fields = FormFields(ISkinsSchema) |
|---|
| 122 | form_fields['theme'].custom_widget = DropdownChoiceWidget |
|---|
| 123 | |
|---|
| 124 | label = _("Theme setting") |
|---|
| 125 | description = _("Settings that affect the site's look and feel.") |
|---|
| 126 | form_name = _("Theme settings") |
|---|
| 127 | |
|---|
| 128 | @form.action(_(u'label_save', default=u'Save'), name=u'save') |
|---|
| 129 | def handle_edit_action(self, action, data): |
|---|
| 130 | #Define custom folder and base props |
|---|
| 131 | self.custom = self.context.portal_skins.custom.aq_inner.aq_explicit |
|---|
| 132 | |
|---|
| 133 | if form.applyChanges(self.context, self.form_fields, data, |
|---|
| 134 | self.adapters): |
|---|
| 135 | self.status = _(u"Changes saved.") |
|---|
| 136 | |
|---|
| 137 | if hasattr(self.custom, 'base_properties'): |
|---|
| 138 | self.backup_base_properties(data) |
|---|
| 139 | |
|---|
| 140 | self.restore_base_properties(self.custom, data) |
|---|
| 141 | |
|---|
| 142 | else: |
|---|
| 143 | self.status = _(u"No changes made.") |
|---|
| 144 | |
|---|
| 145 | def backup_base_properties(self, data): |
|---|
| 146 | #backup base_properties on theme change |
|---|
| 147 | self.base_props = self.custom.base_properties |
|---|
| 148 | cur_theme_title = self.base_props.plone_skin |
|---|
| 149 | |
|---|
| 150 | #If base_props plone_skin value != new theme name, back up base_props |
|---|
| 151 | if data['theme'] != cur_theme_title: |
|---|
| 152 | #backup self.base_props |
|---|
| 153 | self.custom.manage_renameObjects([self.base_props.id], [self.base_props.id + '.' + cur_theme_title.lower().replace(' ', '')]) |
|---|
| 154 | |
|---|
| 155 | def restore_base_properties(self, custom, data): |
|---|
| 156 | #on theme change, restore backed up base_properties, if it exists |
|---|
| 157 | for id, obj in custom.items(): |
|---|
| 158 | if 'base_properties' in id: |
|---|
| 159 | if obj.plone_skin == data['theme']: |
|---|
| 160 | custom.manage_renameObjects([id], ['base_properties']) |
|---|
| 161 | self.context.changeSkin(data['theme'], self.context.REQUEST) |
|---|
| 162 | |
|---|