##########################################################################
#                                                                        #
#           copyright (c) 2003 ITB, Humboldt-University Berlin           #
#           written by: Raphael Ritz, r.ritz@biologie.hu-berlin.de       #
#                                                                        #
##########################################################################

"""Inproceedings reference main class"""

from AccessControl import ClassSecurityInfo
from Products.CMFCore.permissions import View
from Products.CMFCore.utils import getToolByName

from Products.CMFBibliographyAT.config import CMFBAT_USES_LINGUAPLONE
if CMFBAT_USES_LINGUAPLONE:
    from Products.LinguaPlone.public import Schema
else:
    from Products.Archetypes.public import Schema
from Products.Archetypes.public import registerType
    
from Products.CMFBibliographyAT.config import PROJECTNAME
from Products.CMFBibliographyAT.content.inbook import InbookReference
from Products.CMFBibliographyAT.content.schemata \
    import HeaderSchema, AuthorSchema, CoreSchema, TrailingSchema
from Products.CMFBibliographyAT.content.fields \
    import booktitleField, editorField, volumeField, numberField, \
	   publisherField, organizationField, addressField, \
           editionField, seriesField, chapterField, pagesField

SourceSchema = Schema((
   booktitleField,
   editorField,
   volumeField,
   numberField,
   chapterField,
   pagesField,
   publisherField,
   organizationField,
   addressField,
   editionField,
   seriesField,
))
						       
InproceedingsSchema = HeaderSchema.copy() + AuthorSchema.copy() + CoreSchema.copy() +  \
               SourceSchema.copy() + TrailingSchema.copy()
InproceedingsSchema.get('authors').required = 1
InproceedingsSchema.get('booktitle').required = 1

# the default AT 'description' field shall be invisible, it is kept in sync with the 'abstract' field
InproceedingsSchema.get('description').widget.visible = {'view': 'invisible', 'edit': 'invisible', }
								      
class InproceedingsReference(InbookReference):
    """ content type to make reference to a chapter within a proceedings volume.
    """
    security = ClassSecurityInfo()
    source_fields = ('booktitle', 'editor', 'volume', 'number',  'chapter', 'pages', 'publisher', 'organization', 'address', 'edition', 'series',)

    archetype_name = "Inproceedings Reference"
    schema = InproceedingsSchema

    security.declareProtected(View, 'Source')
    def Source(self):
	""" the default 'inproceedings' source format
        """
	try:

    	    return self.InproceedingsSource()

        except AttributeError:

	    booktitle        = self.getBooktitle()
	    editor           = self.getEditor()
	    volume           = self.getVolume()
    	    number           = self.getNumber()
	    chapter          = self.getChapter()
    	    pages            = self.getPages()
    	    publisher        = self.getPublisher()
	    organization     = self.getOrganization()
    	    address          = self.getAddress()
    	    edition          = self.getEdition()
    	    series           = self.getSeries()
    
	    bs_tool = getToolByName(self, 'portal_bibliostyles', None)
        
	    source = 'In: %s' % booktitle
	    if editor: source += ', ed. by %s' % editor
    	    if volume:
		source += ', vol. %s' % volume
        	if number: source += '(%s)' % number
	    if chapter: source += ', chap. %s' % chapter
	    if pages: source += ', pp. %s' % pages
    	    if organization: source += ', %s' % organization
    	    if address: source += ', %s' % address
    	    if publisher: source += ', %s' % publisher
            if edition:
                if bs_tool:
                    source += ', %s' % bs_tool.formatEdition(edition, abbreviate=True)
                else:
                    source += ',  %s ed.' % edition
	    
	    if source and (source[-1] not in '.!?'): source += '.'

	    if series: source += ' %s.' % series

	    return source

registerType(InproceedingsReference, PROJECTNAME)
