| 1 | ################################################################################
|
|---|
| 2 | # ZMSTextformatManager.py
|
|---|
| 3 | #
|
|---|
| 4 | # This program is free software; you can redistribute it and/or
|
|---|
| 5 | # modify it under the terms of the GNU General Public License
|
|---|
| 6 | # as published by the Free Software Foundation; either version 2
|
|---|
| 7 | # of the License, or (at your option) any later version.
|
|---|
| 8 | #
|
|---|
| 9 | # This program is distributed in the hope that it will be useful,
|
|---|
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 12 | # GNU General Public License for more details.
|
|---|
| 13 | #
|
|---|
| 14 | # You should have received a copy of the GNU General Public License
|
|---|
| 15 | # along with this program; if not, write to the Free Software
|
|---|
| 16 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|---|
| 17 | ################################################################################
|
|---|
| 18 |
|
|---|
| 19 | # Imports.
|
|---|
| 20 | from App.Common import package_home
|
|---|
| 21 | import copy
|
|---|
| 22 | import os
|
|---|
| 23 | import urllib
|
|---|
| 24 | # Product Imports.
|
|---|
| 25 | import ZMSTextformat
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 | ################################################################################
|
|---|
| 29 | ################################################################################
|
|---|
| 30 | ###
|
|---|
| 31 | ### Class
|
|---|
| 32 | ###
|
|---|
| 33 | ################################################################################
|
|---|
| 34 | ################################################################################
|
|---|
| 35 | class ZMSTextformatManager:
|
|---|
| 36 |
|
|---|
| 37 | ############################################################################
|
|---|
| 38 | #
|
|---|
| 39 | # XML IM/EXPORT
|
|---|
| 40 | #
|
|---|
| 41 | ############################################################################
|
|---|
| 42 |
|
|---|
| 43 | # --------------------------------------------------------------------------
|
|---|
| 44 | # ZMSTextformatManager.importTextformatXml
|
|---|
| 45 | # --------------------------------------------------------------------------
|
|---|
| 46 |
|
|---|
| 47 | def _importTextformatXml(self, item, zms_system=0, createIfNotExists=1):
|
|---|
| 48 | id = item['key']
|
|---|
| 49 | dict = item['value']
|
|---|
| 50 | dict['default'] = dict.get('default',0)
|
|---|
| 51 | if id in self.textformats:
|
|---|
| 52 | i = self.textformats.index(id)
|
|---|
| 53 | self.textformats[i+1] = dict
|
|---|
| 54 | else:
|
|---|
| 55 | self.textformats.extend([id,dict])
|
|---|
| 56 | # Make persistent.
|
|---|
| 57 | self.textformats = copy.deepcopy(self.textformats)
|
|---|
| 58 |
|
|---|
| 59 | def importTextformatXml(self, xml, REQUEST=None, zms_system=0, createIfNotExists=1):
|
|---|
| 60 | if zms_system and not createIfNotExists:
|
|---|
| 61 | return
|
|---|
| 62 | v = self.parseXmlString(xml)
|
|---|
| 63 | if type(v) is list:
|
|---|
| 64 | for item in v:
|
|---|
| 65 | self._importTextformatXml(item,zms_system,createIfNotExists)
|
|---|
| 66 | else:
|
|---|
| 67 | self._importTextformatXml(v,zms_system,createIfNotExists)
|
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 | # --------------------------------------------------------------------------
|
|---|
| 71 | # ZMSTextformatManager.getRichttextFormatIds:
|
|---|
| 72 | # --------------------------------------------------------------------------
|
|---|
| 73 | def getRichtextFormatIds(self):
|
|---|
| 74 | ids = []
|
|---|
| 75 | filepath = package_home(globals())+'/plugins/rte/'
|
|---|
| 76 | for filename in os.listdir(filepath):
|
|---|
| 77 | path = filepath + os.sep + filename
|
|---|
| 78 | if os.path.isdir(path):
|
|---|
| 79 | ids.append(filename)
|
|---|
| 80 | return ids
|
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 | # --------------------------------------------------------------------------
|
|---|
| 84 | # ZMSTextformatManager.delTextformat:
|
|---|
| 85 | # --------------------------------------------------------------------------
|
|---|
| 86 | def delTextformat(self, id):
|
|---|
| 87 | i = self.textformats.index(id)
|
|---|
| 88 | del self.textformats[i]
|
|---|
| 89 | del self.textformats[i]
|
|---|
| 90 | # Make persistent.
|
|---|
| 91 | self.textformats = copy.deepcopy(self.textformats)
|
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 | # --------------------------------------------------------------------------
|
|---|
| 95 | # ZMSTextformatManager.setTextformat:
|
|---|
| 96 | # --------------------------------------------------------------------------
|
|---|
| 97 | def setTextformat(self, id, newId, newDisplay, newZMILang, newTag='', newSubtag='', newAttrs='', newRichedit=0):
|
|---|
| 98 | if id in self.textformats:
|
|---|
| 99 | i = self.textformats.index(id)
|
|---|
| 100 | else:
|
|---|
| 101 | i = len(self.textformats)
|
|---|
| 102 | self.textformats.extend([newId,{'display':{},'default':0}])
|
|---|
| 103 | dict = self.textformats[i+1]
|
|---|
| 104 | dict['display'][newZMILang] = newDisplay
|
|---|
| 105 | dict['tag'] = newTag
|
|---|
| 106 | dict['subtag'] = newSubtag
|
|---|
| 107 | dict['attrs'] = newAttrs
|
|---|
| 108 | dict['richedit'] = newRichedit
|
|---|
| 109 | self.textformats[i] = newId
|
|---|
| 110 | self.textformats[i+1] = dict
|
|---|
| 111 | # Make persistent.
|
|---|
| 112 | self.textformats = copy.deepcopy(self.textformats)
|
|---|
| 113 |
|
|---|
| 114 |
|
|---|
| 115 | # --------------------------------------------------------------------------
|
|---|
| 116 | # ZMSTextformatManager.getTextFormat:
|
|---|
| 117 | # --------------------------------------------------------------------------
|
|---|
| 118 | def getTextFormat(self, id, REQUEST):
|
|---|
| 119 | if id in self.textformats:
|
|---|
| 120 | i = self.textformats.index(id)
|
|---|
| 121 | return ZMSTextformat.ZMSTextformat(id,self.textformats[i+1],REQUEST)
|
|---|
| 122 | return None
|
|---|
| 123 |
|
|---|
| 124 |
|
|---|
| 125 | # --------------------------------------------------------------------------
|
|---|
| 126 | # ZMSTextformatManager.getTextFormats:
|
|---|
| 127 | # --------------------------------------------------------------------------
|
|---|
| 128 | def getTextFormats(self, REQUEST):
|
|---|
| 129 | l = map( lambda x: ZMSTextformat.ZMSTextformat(self.textformats[x*2],self.textformats[x*2+1],REQUEST), range(len(self.textformats)/2))
|
|---|
| 130 | l = map( lambda x: (x.getDisplay(), x), l)
|
|---|
| 131 | l.sort()
|
|---|
| 132 | return map(lambda x: x[1],l)
|
|---|
| 133 |
|
|---|
| 134 |
|
|---|
| 135 | # --------------------------------------------------------------------------
|
|---|
| 136 | # ZMSTextformatManager.setDefaultTextformat:
|
|---|
| 137 | # --------------------------------------------------------------------------
|
|---|
| 138 | def setDefaultTextformat(self, id):
|
|---|
| 139 | if len(id) > 0 and id in self.textformats:
|
|---|
| 140 | map( lambda x: self.operator_setitem(self.textformats[x*2+1],'default',0), range(len(self.textformats)/2))
|
|---|
| 141 | i = self.textformats.index(id)
|
|---|
| 142 | self.textformats[i+1]['default'] = 1
|
|---|
| 143 | # Make persistent.
|
|---|
| 144 | self.textformats = copy.deepcopy(self.textformats)
|
|---|
| 145 |
|
|---|
| 146 |
|
|---|
| 147 | # --------------------------------------------------------------------------
|
|---|
| 148 | # ZMSTextformatManager.getTextFormatDefault:
|
|---|
| 149 | # --------------------------------------------------------------------------
|
|---|
| 150 | def getTextFormatDefault(self):
|
|---|
| 151 | if len(self.textformats) == 0:
|
|---|
| 152 | return ''
|
|---|
| 153 | i = 0
|
|---|
| 154 | format_default = filter( lambda x: self.textformats[x*2+1].get('default',0)==1, range(len(self.textformats)/2))
|
|---|
| 155 | if len(format_default) == 1:
|
|---|
| 156 | i = format_default[0]*2
|
|---|
| 157 | elif 'body' in self.textformats:
|
|---|
| 158 | i = self.textformats.index('body')
|
|---|
| 159 | return self.textformats[i]
|
|---|
| 160 |
|
|---|
| 161 |
|
|---|
| 162 | ############################################################################
|
|---|
| 163 | # ZMSTextformatManager.manage_changeTextformat:
|
|---|
| 164 | #
|
|---|
| 165 | # Change text-formats.
|
|---|
| 166 | ############################################################################
|
|---|
| 167 | def manage_changeTextformat(self, lang, REQUEST, RESPONSE):
|
|---|
| 168 | """ ZMSTextformatManager.manage_changeTextformat """
|
|---|
| 169 | message = ''
|
|---|
| 170 | id = REQUEST.get('id','')
|
|---|
| 171 |
|
|---|
| 172 | # Change.
|
|---|
| 173 | # -------
|
|---|
| 174 | if REQUEST['btn'] == self.getZMILangStr('BTN_SAVE'):
|
|---|
| 175 | old_id = REQUEST['id']
|
|---|
| 176 | id = REQUEST['new_id'].strip()
|
|---|
| 177 | display = REQUEST['new_display'].strip()
|
|---|
| 178 | tag = REQUEST['new_tag'].strip()
|
|---|
| 179 | subtag = REQUEST['new_subtag'].strip()
|
|---|
| 180 | attrs = REQUEST['new_attrs'].strip()
|
|---|
| 181 | richedit = REQUEST.get('new_richedit',0)
|
|---|
| 182 | self.setTextformat(old_id,id,display,self.get_manage_lang(),tag,subtag,attrs,richedit)
|
|---|
| 183 | if REQUEST.has_key('new_default'):
|
|---|
| 184 | self.setDefaultTextformat(REQUEST['new_default'])
|
|---|
| 185 | id = ''
|
|---|
| 186 | message = self.getZMILangStr('MSG_CHANGED')
|
|---|
| 187 |
|
|---|
| 188 | # Delete.
|
|---|
| 189 | # -------
|
|---|
| 190 | elif REQUEST['btn'] == self.getZMILangStr('BTN_DELETE'):
|
|---|
| 191 | id = REQUEST['id']
|
|---|
| 192 | self.delTextformat( id)
|
|---|
| 193 | id = ''
|
|---|
| 194 | message = self.getZMILangStr('MSG_DELETED')%int(1)
|
|---|
| 195 |
|
|---|
| 196 | # Insert.
|
|---|
| 197 | # -------
|
|---|
| 198 | elif REQUEST['btn'] == self.getZMILangStr('BTN_INSERT'):
|
|---|
| 199 | id = REQUEST['_id'].strip()
|
|---|
| 200 | display = REQUEST['_display'].strip()
|
|---|
| 201 | self.setTextformat(None,id,display,self.get_manage_lang())
|
|---|
| 202 | message = self.getZMILangStr('MSG_CHANGED')
|
|---|
| 203 |
|
|---|
| 204 | # Export.
|
|---|
| 205 | # -------
|
|---|
| 206 | elif REQUEST['btn'] == self.getZMILangStr('BTN_EXPORT'):
|
|---|
| 207 | value = []
|
|---|
| 208 | ids = REQUEST.get('ids',[])
|
|---|
| 209 | fmts = self.textformats
|
|---|
| 210 | for i in range(len(fmts)/2):
|
|---|
| 211 | id = fmts[i*2]
|
|---|
| 212 | ob = fmts[i*2+1]
|
|---|
| 213 | if id in ids or len(ids) == 0:
|
|---|
| 214 | value.append({'key':id,'value':ob})
|
|---|
| 215 | if len(value)==1:
|
|---|
| 216 | value = value[0]
|
|---|
| 217 | content_type = 'text/xml; charset=utf-8'
|
|---|
| 218 | filename = 'export.textfmt.xml'
|
|---|
| 219 | export = self.getXmlHeader() + self.toXmlString(value,1)
|
|---|
| 220 | RESPONSE.setHeader('Content-Type',content_type)
|
|---|
| 221 | RESPONSE.setHeader('Content-Disposition','inline;filename="%s"'%filename)
|
|---|
| 222 | return export
|
|---|
| 223 |
|
|---|
| 224 | # Import.
|
|---|
| 225 | # -------
|
|---|
| 226 | elif REQUEST['btn'] == self.getZMILangStr('BTN_IMPORT'):
|
|---|
| 227 | f = REQUEST['file']
|
|---|
| 228 | if f:
|
|---|
| 229 | filename = f.filename
|
|---|
| 230 | self.importTextformatXml(xml=f)
|
|---|
| 231 | else:
|
|---|
| 232 | filename = REQUEST['init']
|
|---|
| 233 | createIfNotExists = 1
|
|---|
| 234 | self.importConf(filename, REQUEST, createIfNotExists)
|
|---|
| 235 | message = self.getZMILangStr('MSG_IMPORTED')%('<i>%s</i>'%filename)
|
|---|
| 236 |
|
|---|
| 237 | # Return with message.
|
|---|
| 238 | if RESPONSE:
|
|---|
| 239 | message = urllib.quote(message)
|
|---|
| 240 | return RESPONSE.redirect('manage_textformats?lang=%s&manage_tabs_message=%s&id=%s'%(lang,message,id))
|
|---|
| 241 |
|
|---|
| 242 | return message
|
|---|
| 243 |
|
|---|
| 244 | ################################################################################
|
|---|