# -*- coding: utf-8 # Copyright 2006 by Meelis Mets from Products.Archetypes.public import BaseSchema, Schema from Products.Archetypes.public import StringField, LinesField, BooleanField from Products.Archetypes.public import LinesWidget, TextAreaWidget, IdWidget, StringWidget, SelectionWidget, BooleanWidget from Products.Archetypes.public import BaseContent, registerType from Globals import InitializeClass from Products.CMFCore.utils import getToolByName import random import urllib import time from AccessControl import ClassSecurityInfo, Unauthorized from config import PROJECT_NAME schema = BaseSchema + Schema(( StringField('help', widget=TextAreaWidget( label="Help text", description="Add help text here, this text should describe how to fill form or something like that", label_msgid='label_help', description_msgid='help_title', i18n_domain="application"), ), BooleanField('sendmail', widget=BooleanWidget( label="Send reminder email", description="Check this checkbox if you want to send reminder email to users who has filled the form", label_msgid='label_help', description_msgid='help_title', i18n_domain="application"), ), StringField('emailtext', default = "Thank you, {link} is URL to your form", widget=TextAreaWidget( label="Content of E-Mail", description="Content of reminder email which is sent to users who has filled this form, you can modify it as you want, but do not remove {link} this tag will be replaced with URL to users form", label_msgid='label_help', description_msgid='help_title', i18n_domain="application"), ), )) class Application(BaseContent): """ Application product """ meta_type = "Application" archetype_name = "Application" global_allow = 1 allowed_content_types = [] security = ClassSecurityInfo() security.declareObjectPublic() schema = schema actions = ( { 'id':'view', 'name':'View', 'action':'string:${object_url}/app_view', 'permissions': ('View',), }, { 'id':'edit', 'name':'Edit', 'action':'string:${object_url}/base_edit', 'permissions': ('Manage Portal',), }, { 'id':'metadata', 'name':'Properties', 'action':'string:${object_url}/base_metadata', 'permissions': ('Manage Portal',), }, { 'id':'create', 'name':'Create', 'action':'string:${object_url}/app_edit', 'permissions': ('Manage Portal',), }, { 'id':'print', 'name':'Print', 'action':'string:${object_url}/app_print', 'permissions': ('Modify portal content',), }, { 'id':'data', 'name':'Data', 'action':'string:${object_url}/app_data', 'permissions': ('Manage Portal',), }, ) def __init__(self, id): self.id = id self.appFields = [] self.appData = [] self.appCopy = 0 self.motherId = id def getCopy(self): """ get Copy """ return self.appCopy def getEmailText(self): """ get Copy """ text = self.emailtext result = text.split("{link}") if (len(result)<2): result.append("") return result def getFields(self): """ get Fields """ if (self.appCopy == 0): return self.appFields else: query_string = {'meta_type': 'Application'} pcatalog = getToolByName(self, 'portal_catalog') results = pcatalog.searchResults(query_string) for ob in results: if (ob.id == self.motherId): mother = ob self.appFields = mother.getObject().appFields return self.appFields def arrayToString(self, array): """ array to string splittable by \n """ string = '' for value in array: string = string+value+'\n' return string def arrayToBr(self, array): """ array to string splittable by
""" string = '' for value in array: string = string+value+'
' return string def countLines(self, text): """ count lines """ counter = 0 splitter = text.split("\n") counter = len(splitter) return counter def makeId(self, REQUEST): """ generates new id """ newid = str(random.randrange(10000,99999,1))+str(time.time()) def addFields(self, REQUEST): """ add Fields """ title = REQUEST.get('title') help = REQUEST.get('help') type = REQUEST.get('type') required = REQUEST.get('required') size = REQUEST.get('size') edit = REQUEST.get('edit_mode') options = REQUEST.get('options') if(type=='subtitle'): size = REQUEST.get('format') id = REQUEST.get('id') if (id == 'none' or id == ''): id = str(random.randrange(10000,99999,1))+str(time.time()) if (size == '' and type == 'text'): size = 15 if (size == '' and type == 'textarea'): size = 3 field = {'id': id, 'title': title, 'help': help, 'type': type, 'required': required, 'size': size, 'options': options} data = {'id': id, 'value': ''} if (edit=='none'): if(title != ''): self.appData.append(data) self.appFields.append(field) else: i=0 for f in self.appFields: if (edit == f['id']): if(title != ''): self.appFields[i]=field i=i+1 self._p_changed = 1 def getRequired(self): """req""" req = "[" for field in self.appFields: if(field['required']=='on'): req=req+"\'"+field['id']+"\'," req = req[:-1] return req+"]" def removeFields(self, REQUEST): """ remove Fields """ newAppFields = [] nr=0 id = REQUEST.get('idd') for field in self.appFields: if (id != field['id']): newAppFields.append(self.appFields[nr]) nr=nr+1 self.appFields = newAppFields self._p_changed = 1 def moveFields(self, REQUEST): """ move Fields """ nr=0 id = REQUEST.get('idd') where = REQUEST.get('where') for field in self.appFields: if (id == field['id']): if (where == 'up' and nr>0): temp = self.appFields[nr-1] self.appFields[nr-1] = self.appFields[nr] self.appFields[nr] = temp self._p_changed = 1 return 1 if (where == 'down' and nr"+str(dataObject.Creator())+"" for field in self.appFields: noData = 0 for data in dataObject.appData: if(str(field['id'])==str(data['id'])): noData = 1 if str(field['type']) in ['text', 'textarea', 'dropdown']: html = html+""+str(data['value'])+"" if str(field['type']) in ['single selection']: if str(data['value']) != 'None': html = html+""+str(field['options'][int(str(data['value']))])+"" else: html = html+" " if str(field['type']) in ['multiple selection']: if (data['value'] == None): html = html+"" elif (len(data['value'])==1): html = html+""+str(field['options'][int(str(data['value']))])+"" else: val = '' for i in data['value']: val = val + str(field['options'][int(str(i))]) + ", " val = val[:-2] html = html+""+val+"" if str(field['type']) in ['link']: if (data['value'] == None): html = html+"" #elif (len(data['value'])==1): # html = html+""+str(data['value'])+"" else: val = '' for i in data['value']: val = val + "" + str(i) + ", " val = val[:-2] html = html + "" + val + "" if(noData==0): html = html + " " html = html+"" return html def mailLink(self, forgotten_userid, REQUEST, link): """ Send reminder Email to a member. o Raise an exception if user ID is not found. """ membership = getToolByName(self, 'portal_membership') member = membership.getMemberById(forgotten_userid) if member is None: raise ValueError('The username you entered could not be found.') # assert that we can actually get an email address, otherwise # the template will be made with a blank To:, this is bad if not member.getProperty('email'): raise ValueError('That user does not have an email address.') #check, msg = _checkEmail(member.getProperty('email')) #if not check: # raise ValueError, msg # Rather than have the template try to use the mailhost, we will # render the message ourselves and send it from here (where we # don't need to worry about 'UseMailHost' permissions). mail_text = self.mail_link_template( self , REQUEST , member=member , link=link ) host = self.MailHost host.send( mail_text ) return self.mail_password_response( self, REQUEST ) def copyApplication(self, REQUEST): """ Copies already made form to users forms folder """ self.saveData(REQUEST) user = str(REQUEST.AUTHENTICATED_USER) homeFolder = self.portal_membership.getHomeFolder(user) id = str(self.id)+'-copy' if hasattr(homeFolder, id): copiedForm = getattr(homeFolder,id) copiedForm.appFields = self.appFields copiedForm.appData = self.appData self.appData = [] for field in self.appFields: data = {'id': field['id'], 'value': ''} self.appData.append(data) else: typestool = getToolByName(self, 'portal_types') puf_type = getattr(typestool, 'Application', None) if not puf_type: return 0 puf_type.global_allow = 1 id = homeFolder.invokeFactory("Application", id=str(self.id)+'-copy', title=self.title) copiedForm = getattr(homeFolder,id) copiedForm.appCopy = 1 copiedForm.appFields = self.appFields copiedForm.appData = self.appData copiedForm.motherId = self.id puf_type.global_allow = 0 self.appData = [] for field in self.appFields: data = {'id': field['id'], 'value': ''} self.appData.append(data) if(self.sendmail==True): self.mailLink(user, REQUEST, copiedForm.absolute_url()) self._p_changed = 1 return copiedForm security.declareProtected('Modify portal content', 'getHTML') def getHTML(self): """ HTML """ a = str(self.app_print()) b = a.split("") begin = "\n\n\n\n\n\n" end = "\n" html = begin + b[1] + b[3] +end return html registerType(Application, PROJECT_NAME)