## $Id: viewlets.py 7714 2012-02-28 10:34:16Z henrik $
##
## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## 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
##
import grok
from hurry.workflow.interfaces import IWorkflowState
from waeup.sirp.interfaces import ISIRPObject
from waeup.sirp.students.viewlets import PrimaryStudentNavTab
from waeup.sirp.browser.viewlets import ManageActionButton, PrimaryNavTab
from waeup.sirp.applicants.interfaces import (
    IApplicant, IApplicantsRoot, IApplicantsContainer,
    IApplicantOnlinePayment,
    )
from waeup.sirp.applicants.browser import (
    ApplicantsRootPage, ApplicantsContainerPage, ApplicantManageFormPage,
    ApplicantDisplayFormPage, OnlinePaymentDisplayFormPage
    )

from waeup.sirp.interfaces import MessageFactory as _

grok.context(ISIRPObject) # Make ISIRPObject the default context
grok.templatedir('browser_templates')

class ApplicantsAuthTab(PrimaryNavTab):
    """Applicants tab in primary navigation.
    """
    grok.context(ISIRPObject)
    grok.order(3)
    grok.require('waeup.viewApplicantsTab')
    pnav = 3
    tab_title = _(u'Applicants')

    @property
    def link_target(self):
        return self.view.application_url('applicants')

class ApplicantsAnonTab(ApplicantsAuthTab):
    """Applicants tab in primary navigation.

    Display tab only for anonymous. Authenticated users can call the
    form from the user navigation bar.
    """
    grok.require('waeup.Anonymous')
    tab_title = _(u'Application')

    # Also zope.manager has role Anonymous.
    # To avoid displaying this tab, we have to check the principal id too.
    @property
    def link_target(self):
        if self.request.principal.id == 'zope.anybody':
            return self.view.application_url('applicants')
        return

class MyApplicationDataTab(PrimaryStudentNavTab):
    """MyData-tab in primary navigation.
    """
    grok.order(3)
    grok.require('waeup.viewMyApplicationDataTab')
    pnav = 3
    tab_title = _(u'My Data')

    @property
    def link_target(self):
        try:
            container, application_number = self.request.principal.id.split('_')
        except ValueError:
            return
        rel_link = '/applicants/%s/%s' % (container, application_number)
        return self.view.application_url() + rel_link

class ManageApplicantsRootActionButton(ManageActionButton):
    grok.context(IApplicantsRoot)
    grok.view(ApplicantsRootPage)
    grok.require('waeup.manageApplication')
    text = _('Manage application section')

class ApplicantsContainerManageActionButton(ManageActionButton):
    grok.order(1)
    grok.context(IApplicantsContainer)
    grok.view(ApplicantsContainerPage)
    grok.require('waeup.manageApplication')
    text = _('Manage applicants container')

class ApplicantRegisterActionButton(ManageActionButton):
    grok.order(2)
    grok.context(IApplicantsContainer)
    grok.view(ApplicantsContainerPage)
    grok.require('waeup.Anonymous')
    icon = 'actionicon_login.png'
    text = _('Register for application')
    target = 'register'

class ApplicantViewActionButton(ManageActionButton):
    grok.context(IApplicant)
    grok.view(ApplicantManageFormPage)
    grok.require('waeup.viewApplication')
    icon = 'actionicon_view.png'
    text = _('View application record')
    target = 'index'

class ApplicantManageActionButton(ManageActionButton):
    grok.order(1)
    grok.context(IApplicant)
    grok.view(ApplicantDisplayFormPage)
    grok.require('waeup.manageApplication')
    text = _('Manage application record')
    target = 'manage'

class ApplicantEditActionButton(ManageActionButton):
    grok.order(2)
    grok.context(IApplicant)
    grok.view(ApplicantDisplayFormPage)
    grok.require('waeup.handleApplication')
    text = _('Edit application record')
    target ='edit'

    @property
    def target_url(self):
        """Get a URL to the target...
        """
        if self.context.locked:
            return
        return self.view.url(self.view.context, self.target)

class PDFActionButton(ManageActionButton):
    grok.order(3)
    grok.context(IApplicant)
    grok.require('waeup.viewApplication')
    icon = 'actionicon_pdf.png'
    text = _('Download application slip')
    target = 'application_slip.pdf'

class StudentCreateActionButton(ManageActionButton):
    grok.order(4)
    grok.context(IApplicant)
    grok.require('waeup.manageApplication')
    icon = 'actionicon_entrance.png'
    text = _('Create student record')
    target ='createstudent'

    @property
    def target_url(self):
        """Get a URL to the target...
        """
        if IWorkflowState(self.context).getState() != 'admitted':
            return
        return self.view.url(self.view.context, self.target)

class PaymentReceiptActionButton(ManageActionButton):
    grok.order(1)
    grok.context(IApplicantOnlinePayment)
    grok.view(OnlinePaymentDisplayFormPage)
    grok.require('waeup.viewApplication')
    icon = 'actionicon_pdf.png'
    text = _('Download payment receipt')
    target = 'payment_receipt.pdf'

    @property
    def target_url(self):
        if self.context.p_state != 'paid':
            return ''
        return self.view.url(self.view.context, self.target)

class RequestCallbackActionButton(ManageActionButton):
    grok.order(2)
    grok.context(IApplicantOnlinePayment)
    grok.view(OnlinePaymentDisplayFormPage)
    grok.require('waeup.payApplicant')
    icon = 'actionicon_call.png'
    text = _('Request callback')
    target = 'callback'

    @property
    def target_url(self):
        if self.context.p_state != 'unpaid':
            return ''
        return self.view.url(self.view.context, self.target)