# -*- 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 Globals import InitializeClass, DTMLFile
from Products.CMFCore.utils import getToolByName

from AccessControl import ClassSecurityInfo
from Products.CMFCore.permissions import View, ModifyPortalContent
from Products.CMFCore.DynamicType import DynamicType
from Products.PageTemplates.Expressions import getEngine
from Products.PageTemplates.Expressions import SecureModuleImporter

class BaseSlot(DynamicType) :
    """Slot"""

    _isMosaicSlot = 1
    _editableFields = []
    _indexableFields = []
    
    security = ClassSecurityInfo()
  
    security.declarePublic('callAction')
    def callAction(self, actionId, *args, **kw) :
        """call action from action definitions"""
        typeTool = getToolByName(self, 'portal_types')

        # !!! id et meta_type doivent etre identiques dans la fti.
        typeInfo = typeTool.getTypeInfo(self)
        actionInfo = typeInfo.getActionInfo('object/%s' % actionId)
        zpt = getattr(self, actionInfo['url'])
        return zpt(object=self, block=self.aq_parent, *args, **kw)

    security.declareProtected(ModifyPortalContent, 'edit')
    def edit(self, **kw) :
        """ Edit Slot"""
        for fieldName in self._editableFields :
            if kw.has_key(fieldName) :
                setattr(self, fieldName, kw[fieldName])
    
    security.declareProtected(View, 'getBlock')
    def getBlock(self) :
        """ Return the block object where the slot is located """
        return self.aq_parent
                
    security.declareProtected(View, 'getExprContext')
    def getExprContext(self, REQUEST=None) :
        """Return an expression context customized for expressions properties from slot information"""
        block = self.aq_parent
        while block.meta_type != 'Mosaic Block' :
            block = block.aq_parent
        data = {
            'portal'    : self.portal_url.getPortalObject(),
            'slot'      : self,
            'block'     : self.aq_parent,
            'here'      : None,
            'request'   : REQUEST,
            'modules'   : SecureModuleImporter,
            'nothing'   : None,
            }
        return getEngine().getContext(data)
        
    security.declareProtected(View, 'SearchableText')
    def SearchableText(self) :
        """ Return full text for indexation """
        text = ''
        for fieldName in self._indexableFields :
            field = getattr(self, fieldName)
            if callable(field) :
                text += ' %s' % field()
            else :
                text += ' %s' % field
                
        return text
    
    def _initDefaultValues(self) :
        pass
    
    def indexObject(self): pass
    def unindexObject(self): pass
    def reindexObject(self, idxs=[]): pass
    def reindexObjectSecurity(self): pass
    def notifyWorkflowCreated(self): pass
        

InitializeClass(BaseSlot)
