| 1 | ################################################################################
|
|---|
| 2 | # ZMSCharformatManager.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 | import ZPublisher.HTTPRequest
|
|---|
| 21 | import copy
|
|---|
| 22 | import urllib
|
|---|
| 23 | # Product Imports.
|
|---|
| 24 | import _blobfields
|
|---|
| 25 | import _globals
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 | ################################################################################
|
|---|
| 29 | ################################################################################
|
|---|
| 30 | ###
|
|---|
| 31 | ### Class
|
|---|
| 32 | ###
|
|---|
| 33 | ################################################################################
|
|---|
| 34 | ################################################################################
|
|---|
| 35 | class ZMSCharformatManager:
|
|---|
| 36 |
|
|---|
| 37 | """
|
|---|
| 38 | ############################################################################
|
|---|
| 39 | #
|
|---|
| 40 | # XML IM/EXPORT
|
|---|
| 41 | #
|
|---|
| 42 | ############################################################################
|
|---|
| 43 | """
|
|---|
| 44 |
|
|---|
| 45 | # --------------------------------------------------------------------------
|
|---|
| 46 | # ZMSCharformatManager.importCharformatXml
|
|---|
| 47 | # --------------------------------------------------------------------------
|
|---|
| 48 |
|
|---|
| 49 | def _importCharformatXml(self, item, zms_system=0, createIfNotExists=1):
|
|---|
| 50 | if createIfNotExists == 1:
|
|---|
| 51 | newId = self.id_quote(item.get('display',''))
|
|---|
| 52 | if len(newId) == 0:
|
|---|
| 53 | newId = self.getNewId('fmt')
|
|---|
| 54 | newId = item.get('id',newId)
|
|---|
| 55 | newBtn = item.get('btn','')
|
|---|
| 56 | newDisplay = item.get('display','')
|
|---|
| 57 | newTag = item.get('tag','')
|
|---|
| 58 | newAttrs = item.get('attrs','')
|
|---|
| 59 | newJS = item.get('js','')
|
|---|
| 60 | self.setCharformat( None, newId, newBtn, newDisplay, newTag, newAttrs, newJS)
|
|---|
| 61 | # Make persistent.
|
|---|
| 62 | self.charformats = copy.deepcopy(self.charformats)
|
|---|
| 63 |
|
|---|
| 64 | def importCharformatXml(self, xml, REQUEST=None, zms_system=0, createIfNotExists=1):
|
|---|
| 65 | v = self.parseXmlString(xml)
|
|---|
| 66 | if type(v) is list:
|
|---|
| 67 | for item in v:
|
|---|
| 68 | self._importCharformatXml(item,zms_system,createIfNotExists)
|
|---|
| 69 | else:
|
|---|
| 70 | self._importCharformatXml(v,zms_system,createIfNotExists)
|
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 | # --------------------------------------------------------------------------
|
|---|
| 74 | # ZMSCharformatManager.getCharFormats:
|
|---|
| 75 | # --------------------------------------------------------------------------
|
|---|
| 76 | def getCharFormats(self):
|
|---|
| 77 | return self.charformats
|
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 | # ------------------------------------------------------------------------------
|
|---|
| 81 | # ZMSCharformatManager.moveCharformat:
|
|---|
| 82 | # ------------------------------------------------------------------------------
|
|---|
| 83 | def moveCharformat(self, id, pos):
|
|---|
| 84 | obs = self.charformats
|
|---|
| 85 | charformats = filter( lambda x: x['id'] == id, obs)
|
|---|
| 86 | if len(charformats) == 1:
|
|---|
| 87 | ob = charformats[0]
|
|---|
| 88 | self.charformats.remove(ob)
|
|---|
| 89 | self.charformats.insert(pos,ob)
|
|---|
| 90 | # Make persistent.
|
|---|
| 91 | self.charformats = copy.deepcopy(self.charformats)
|
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 | # ------------------------------------------------------------------------------
|
|---|
| 95 | # ZMSCharformatManager.delCharformat:
|
|---|
| 96 | # ------------------------------------------------------------------------------
|
|---|
| 97 | def delCharformat(self, id):
|
|---|
| 98 | obs = self.charformats
|
|---|
| 99 | charformats = filter( lambda x: x['id'] == id, obs)
|
|---|
| 100 | if len(charformats) > 0:
|
|---|
| 101 | ob = charformats[0]
|
|---|
| 102 | if ob.get('btn') in self.objectIds():
|
|---|
| 103 | self.manage_delObjects(ids=[ob['btn']])
|
|---|
| 104 | self.charformats.remove(ob)
|
|---|
| 105 | # Make persistent.
|
|---|
| 106 | self.charformats = copy.deepcopy(self.charformats)
|
|---|
| 107 | return ''
|
|---|
| 108 |
|
|---|
| 109 |
|
|---|
| 110 | # ------------------------------------------------------------------------------
|
|---|
| 111 | # ZMSCharformatManager.setCharformat:
|
|---|
| 112 | # ------------------------------------------------------------------------------
|
|---|
| 113 | def setCharformat(self, oldId, newId, newBtn, newDisplay, newTag='', newAttrs='', newJS=''):
|
|---|
| 114 | obs = self.charformats
|
|---|
| 115 | if oldId is None:
|
|---|
| 116 | oldId = newId
|
|---|
| 117 | oldCharformats = filter( lambda x: x['id'] == oldId, obs)
|
|---|
| 118 | if len(oldCharformats) > 0:
|
|---|
| 119 | i = obs.index( oldCharformats[0])
|
|---|
| 120 | else:
|
|---|
| 121 | i = len(obs)
|
|---|
| 122 | obs.append({})
|
|---|
| 123 | ob = obs[i]
|
|---|
| 124 | if isinstance( newBtn, _blobfields.MyImage):
|
|---|
| 125 | if ob.get('btn') in self.objectIds():
|
|---|
| 126 | self.manage_delObjects(ids=[ob['btn']])
|
|---|
| 127 | self.manage_addImage( id=newBtn.getFilename(), file=newBtn.getData(), title='', content_type=newBtn.getContentType())
|
|---|
| 128 | newBtn = newBtn.getFilename()
|
|---|
| 129 | else:
|
|---|
| 130 | newBtn = ob.get('btn')
|
|---|
| 131 | ob['id'] = newId
|
|---|
| 132 | ob['btn'] = newBtn
|
|---|
| 133 | ob['display'] = newDisplay
|
|---|
| 134 | ob['tag'] = newTag
|
|---|
| 135 | ob['attrs'] = newAttrs
|
|---|
| 136 | ob['js'] = newJS
|
|---|
| 137 | # Make persistent.
|
|---|
| 138 | self.charformats = copy.deepcopy(self.charformats)
|
|---|
| 139 | return newId
|
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 142 | ############################################################################
|
|---|
| 143 | # ZMSCharformatManager.manage_changeCharformat:
|
|---|
| 144 | #
|
|---|
| 145 | # Change char-formats.
|
|---|
| 146 | ############################################################################
|
|---|
| 147 | def manage_changeCharformat(self, lang, REQUEST, RESPONSE):
|
|---|
| 148 | """ ZMSCharformatManager.manage_changeCharformat """
|
|---|
| 149 | message = ''
|
|---|
| 150 | id = REQUEST.get('id','')
|
|---|
| 151 |
|
|---|
| 152 | # Change.
|
|---|
| 153 | # -------
|
|---|
| 154 | if REQUEST['btn'] == self.getZMILangStr('BTN_SAVE'):
|
|---|
| 155 | newId = REQUEST['new_id'].strip()
|
|---|
| 156 | newBtn = REQUEST.get('new_btn','')
|
|---|
| 157 | if isinstance(newBtn,ZPublisher.HTTPRequest.FileUpload) and newBtn.filename != '':
|
|---|
| 158 | newBtn = _blobfields.createBlobField(self,_globals.DT_IMAGE,newBtn)
|
|---|
| 159 | newDisplay = REQUEST['new_display'].strip()
|
|---|
| 160 | newTag = REQUEST['new_tag'].strip()
|
|---|
| 161 | newAttrs = REQUEST['new_attrs'].strip()
|
|---|
| 162 | newJS = REQUEST['new_js'].strip()
|
|---|
| 163 | id = self.setCharformat(id,newId,newBtn,newDisplay,newTag,newAttrs,newJS)
|
|---|
| 164 | message = self.getZMILangStr('MSG_CHANGED')
|
|---|
| 165 |
|
|---|
| 166 | # Delete.
|
|---|
| 167 | # -------
|
|---|
| 168 | elif REQUEST['btn'] in [ self.getZMILangStr('BTN_DELETE'), 'delete']:
|
|---|
| 169 | id = self.delCharformat(id)
|
|---|
| 170 | message = self.getZMILangStr('MSG_DELETED')%int(1)
|
|---|
| 171 |
|
|---|
| 172 | # Insert.
|
|---|
| 173 | # -------
|
|---|
| 174 | elif REQUEST['btn'] == self.getZMILangStr('BTN_INSERT'):
|
|---|
| 175 | fmts = self.getCharFormats()
|
|---|
| 176 | newId = REQUEST['_id'].strip()
|
|---|
| 177 | newBtn = REQUEST.get('_btn','')
|
|---|
| 178 | if isinstance(newBtn,ZPublisher.HTTPRequest.FileUpload) and newBtn.filename != '':
|
|---|
| 179 | newBtn = _blobfields.createBlobField(self,_globals.DT_IMAGE,newBtn)
|
|---|
| 180 | newDisplay = REQUEST['_display'].strip()
|
|---|
| 181 | id = self.setCharformat(None,newId,newBtn,newDisplay)
|
|---|
| 182 | message = self.getZMILangStr('MSG_INSERTED')%id
|
|---|
| 183 |
|
|---|
| 184 | # Export.
|
|---|
| 185 | # -------
|
|---|
| 186 | elif REQUEST['btn'] == self.getZMILangStr('BTN_EXPORT'):
|
|---|
| 187 | ids = REQUEST.get('ids',[])
|
|---|
| 188 | value = filter( lambda x: x['id'] in ids or len(ids) == 0, self.getCharFormats())
|
|---|
| 189 | value = map( lambda x: x.copy(), value)
|
|---|
| 190 | for x in value:
|
|---|
| 191 | if x.get('btn'):
|
|---|
| 192 | x['btn'] = _blobfields.createBlobField( self, _globals.DT_IMAGE, file={'data':getattr( self, x.get('btn')).data,'filename':x.get('btn')})
|
|---|
| 193 | if len(value)==1:
|
|---|
| 194 | value = value[0]
|
|---|
| 195 | content_type = 'text/xml; charset=utf-8'
|
|---|
| 196 | filename = 'export.charfmt.xml'
|
|---|
| 197 | export = self.getXmlHeader() + self.toXmlString(value,1)
|
|---|
| 198 | RESPONSE.setHeader('Content-Type',content_type)
|
|---|
| 199 | RESPONSE.setHeader('Content-Disposition','inline;filename="%s"'%filename)
|
|---|
| 200 | return export
|
|---|
| 201 |
|
|---|
| 202 | # Import.
|
|---|
| 203 | # -------
|
|---|
| 204 | elif REQUEST['btn'] == self.getZMILangStr('BTN_IMPORT'):
|
|---|
| 205 | f = REQUEST['file']
|
|---|
| 206 | if f:
|
|---|
| 207 | filename = f.filename
|
|---|
| 208 | self.importCharformatXml(xml=f)
|
|---|
| 209 | else:
|
|---|
| 210 | filename = REQUEST['init']
|
|---|
| 211 | createIfNotExists = 1
|
|---|
| 212 | self.importConf(filename, REQUEST, createIfNotExists)
|
|---|
| 213 | message = self.getZMILangStr('MSG_IMPORTED')%('<i>%s</i>'%filename)
|
|---|
| 214 |
|
|---|
| 215 | # Move to.
|
|---|
| 216 | # --------
|
|---|
| 217 | elif REQUEST['btn'] == 'move_to':
|
|---|
| 218 | pos = REQUEST['pos']
|
|---|
| 219 | id = int(id)
|
|---|
| 220 | self.moveCharformat( self, id, pos)
|
|---|
| 221 | message = self.getZMILangStr('MSG_MOVEDOBJTOPOS')%(("<i>%s</i>"%str(id)),(pos+1))
|
|---|
| 222 | id = ''
|
|---|
| 223 |
|
|---|
| 224 | # Return with message.
|
|---|
| 225 | message = urllib.quote(message)
|
|---|
| 226 | return RESPONSE.redirect('manage_charformats?lang=%s&manage_tabs_message=%s&id=%s'%(lang,message,id))
|
|---|
| 227 |
|
|---|
| 228 | ################################################################################
|
|---|