# -*- coding: utf-8 -*-
# (c) 2003 Centre de Recherche en Informatique ENSMP Fontainebleau <http://cri.ensmp.fr>
# (c) 2003 Benoît PIN <mailto:pin@cri.ensmp.fr>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.
#



from AccessControl import ClassSecurityInfo
from Globals import InitializeClass
from Products.CMFCore.permissions import View, ModifyPortalContent, AccessContentsInformation
from Products.CMFCore.PortalContent import PortalContent
from Products.CMFDefault.DublinCore import DefaultDublinCoreImpl
from Products.CMFCore.utils import getToolByName

from MosaicBlock import MosaicBlock

factory_type_information = (
    {'id': 'Mosaic Document',
     'title': '_mosaic_document_',
     'content_icon': 'mosaic_icon.png',
     'product': 'MosaicDocument',
     'factory': 'addMosaicDocument',
     'meta_type': 'Mosaic Document',
     'immediate_view': 'mosaicdocument_edit_form',
     'allow_discussion': 0,
     'filter_content_types': 0,
     'actions': ({'id': 'view',
                  'name': 'View',
                  'action': 'mosaicdocument_view',
                  'permissions': (View,)},
                 
                 {'id': 'edit',
                  'name': 'Edit',
                  'action': 'mosaicdocument_edit_form',
                  'permissions': (ModifyPortalContent,)},
                 
                 {'id': 'metadata',
                  'name': 'Metadata',
                  'action': 'metadata_edit_form',
                  'visible': 0,
                  'permissions': (ModifyPortalContent, )},

                 ),
     },
    )


class MosaicDocument(MosaicBlock, PortalContent, DefaultDublinCoreImpl) :
    """
    Mosaic Document
    """
    meta_type = 'Mosaic Document'
    _isRootBlock = 1
    isPrincipiaFolderish = 0

    security = ClassSecurityInfo()

    def indexObject(self) :
        if getattr(self, 'noIndex', None) : return
        PortalContent.indexObject(self)
        
    unindexObject = PortalContent.unindexObject
        
    def reindexObject(self, idxs=[]) :
        if getattr(self, 'noIndex', None) : return
        PortalContent.reindexObject(self, idxs=idxs)
    
    setTitle =          DefaultDublinCoreImpl.setTitle
    setDescription =    DefaultDublinCoreImpl.setDescription
    
    Title =             DefaultDublinCoreImpl.Title
    Description =       DefaultDublinCoreImpl.Description
    Type =              DefaultDublinCoreImpl.Type

    
    def __init__(self, id, rules=None, initSubObjects=None, **kw) :
        DefaultDublinCoreImpl.__init__(self, **kw)
        self.id = id
        self._blockRules = rules
        self._initSubObjects(initSubObjects)

    def _initSubObjects(self, subObjects) :
        pass

    security.declareProtected(View, 'SearchableText')
    def SearchableText(self) :
        """
        Sets new permissions/roles after this object changed levels
        or after one of its levels changed of reviewers/readers.
        """
        return '%s %s %s' % (self.title, self.description, MosaicBlock.SearchableText(self))
    

InitializeClass(MosaicDocument)
    
# factory
def addMosaicDocument(dispatcher, id, REQUEST=None, **kw) :
    """Ajoute Mosaic Document"""
    ob = MosaicDocument(id, **kw)
    dispatcher.Destination()._setObject(id, ob)
    if REQUEST :
        REQUEST.RESPONSE.redirect(dispatcher.DestinationURL() + 'manage_main')




from Products.CMFCore.Expression import Expression as CMFExpression

class Expression(CMFExpression) :
    def __call__(self, econtext=None) :
        """ render the result of expression if an expression context is given
            else the source text
        """
        if econtext :
            return CMFExpression.__call__(self, econtext)
        else :
            return self.text
    
    def __len__(self) : return 0