def unset_navigation_root(self):
     context = aq_inner(self.context)
     if INavigationRoot.providedBy(context):
         noLongerProvides(context, INavigationRoot)
         logger.info("Deactivated navigation root at %s",
                     context.absolute_url())
         utils = getToolByName(context, "plone_utils")
         utils.addPortalMessage(_(u"Deactivated navigation root."))
     return self.request.RESPONSE.redirect(context.absolute_url())
    def update(self):
        """Set selected skin as the default for the current folder."""
        context = aq_inner(self.context)
        utils = getToolByName(context, "plone_utils")

        # Which skin is requested?
        skin_name = self.request.form.get("skin_name", None)
        if skin_name is not None:
            skins_tool = getToolByName(context, 'portal_skins')
            if skin_name not in skins_tool.getSkinSelections():
                skin_name = None

        # Which skin is currently used as default skin, if any?
        current_skin = get_selected_default_skin(context)

        # Determine what needs to be done, and create or remove a
        # local site hook when needed.
        if skin_name is None and current_skin is None:
            utils.addPortalMessage(_(u"Nothing changed."))
        elif skin_name == current_skin:
            utils.addPortalMessage(_(u"Nothing changed."))
        elif skin_name is None and current_skin is not None:
            # Need to remove the hook.
            utils.addPortalMessage(_(u"No default skin selected anymore."))
            remove_hook(context)
        else:
            # The normal case: a change to the default skin.
            utils.addPortalMessage(_(u"Skin changed."))
            add_hook(context)

        # Finally set the default skin.  Note that this is safe to
        # call when skin_name is None as well, as this cleans up a
        # possible earlier setting.
        set_selected_default_skin(context, skin_name)

        return self.request.RESPONSE.redirect(context.absolute_url())
    def getMenuItems(self, context, request):
        """Return menu item entries in a TAL-friendly form."""
        results = []

        context_state = getMultiAdapter((context, request),
                                        name='plone_context_state')
        folder = context
        if not context_state.is_structural_folder():
            folder = utils.parent(context)
        url = folder.absolute_url()
        current_skin = get_selected_default_skin(folder)

        skins_tool = getToolByName(context, "portal_skins")
        skin_selections = skins_tool.getSkinSelections()

        # Only add menu items for skins when we have a choice or when
        # we have previously selected a default skin.  It could be
        # that this default skin has been removed and then we need a
        # way to unset it.
        tools = getMultiAdapter((context, request), name='plone_tools')
        if tools.membership().checkPermission(SetDefaultSkin, folder) and (
                len(skin_selections) > 1 or current_skin):
            if current_skin and current_skin not in skin_selections:
                # Skin has been removed.
                skin = current_skin
                skin_title = utils.safe_unicode(skin)
                skin_id = utils.normalizeString(skin, folder, "utf-8")
                results.append(
                    {"title": _(u"Warning: ${skin}",
                                mapping={'skin': skin_title}),
                     "description": _(
                         u"Skin '${skin}' no longer exists. Selecting this "
                         u"again will result in using the site default.",
                         mapping=dict(skin=skin_title)),
                     "action": "%s/@@switchDefaultSkin?skin_name=%s" % (
                         url, skin),
                     "selected": True,
                     "extra": {
                         "is_skin_option": True,
                         "id": "collective.editskinswitcher-skin-%s" % skin_id,
                         "separator": False,
                         "class": 'actionMenuSelected'},
                     "submenu": None,
                     "icon": None,
                     })

            for skin in skin_selections:
                skin_id = utils.normalizeString(skin, folder, "utf-8")
                skin_title = utils.safe_unicode(skin)
                selected = skin == current_skin
                cssClass = selected and "actionMenuSelected" or "actionMenu"
                results.append(
                    {"title": skin,
                     "description": _(u"Use '${skin}' skin for this folder",
                                      mapping=dict(skin=skin_title)),
                     "action": "%s/@@switchDefaultSkin?skin_name=%s" % (
                         url, skin),
                     "selected": selected,
                     "extra": {
                         "is_skin_option": True,
                         "id": "collective.editskinswitcher-skin-%s" % skin_id,
                         "separator": False,
                         "class": cssClass},
                     "submenu": None,
                     "icon": None,
                     })

        # Add option to reset the default.
        if current_skin:
            # Use a fake id that is unlikely to be the id of an actual skin.
            skin_id = 'collective_set_default_editor_use_site_default'
            results.append(
                {"title": _(u"Use site default"),
                 "description": u"",
                 "action": "%s/@@switchDefaultSkin?skin_name=%s" % (
                     url, skin_id),
                 "selected": False,
                 "extra": {
                     "is_skin_option": True,
                     "id": "collective.editskinswitcher-skin-%s" % skin_id,
                     "separator": 'actionSeparator',
                     "class": 'actionMenu'},
                 "submenu": None,
                 "icon": None,
                 })

        if tools.membership().checkPermission(SetNavigationRoot, folder):
            # Now add an option to set/unset the navigation root.
            menu_item = {
                "title": _(u"Navigation root"),
                "extra": {
                    "is_skin_option": False,
                    "id": "collective.editskinswitcher-set-navigation-root",
                    "separator": 'actionSeparator',
                    },
                "submenu": None,
                "icon": None,
                }
            if INavigationRoot.providedBy(folder):
                menu_item["selected"] = True
                menu_item["cssClass"] = "actionMenuSelected"
                menu_item["description"] = _(
                    u"No longer use this folder as a navigation root.")
                menu_item["action"] = "%s/@@unset-navigation-root" % (url)
            else:
                menu_item["selected"] = False
                menu_item["cssClass"] = "actionMenu"
                menu_item["description"] = _(
                    u"Start using this folder as a navigation root.")
                menu_item["action"] = "%s/@@set-navigation-root" % (url)
            results.append(menu_item)

        return results