Overview

Packages

  • CONTENIDO
  • Core
    • Authentication
    • Backend
    • Cache
    • CEC
    • Chain
    • ContentType
    • Database
    • Debug
    • Exception
    • Frontend
      • Search
      • URI
      • Util
    • GenericDB
      • Model
    • GUI
      • HTML
    • I18N
    • LayoutHandler
    • Log
    • Security
    • Session
    • Util
    • Validation
    • Versioning
    • XML
  • Module
    • ContentRssCreator
    • ContentSitemapHtml
    • ContentSitemapXml
    • ContentUserForum
    • NavigationTop
    • ScriptCookieDirective
  • mpAutoloaderClassMap
  • None
  • Plugin
    • ContentAllocation
    • CronjobOverview
    • FormAssistant
    • FrontendLogic
    • FrontendUsers
    • Linkchecker
    • ModRewrite
    • Newsletter
    • Repository
      • FrontendNavigation
      • KeywordDensity
    • SearchSolr
    • SmartyWrapper
    • UrlShortener
    • UserForum
    • Workflow
  • PluginManager
  • Setup
    • Form
    • GUI
    • Helper
      • Environment
      • Filesystem
      • MySQL
      • PHP
    • UpgradeJob
  • Smarty
    • Cacher
    • Compiler
    • Config
    • Debug
    • PluginsBlock
    • PluginsFilter
    • PluginsFunction
    • PluginsInternal
    • PluginsModifier
    • PluginsModifierCompiler
    • PluginsShared
    • Security
    • Template
    • TemplateResources
  • Swift
    • ByteStream
    • CharacterStream
    • Encoder
    • Events
    • KeyCache
    • Mailer
    • Mime
    • Plugins
    • Transport

Classes

  • Swift_FailoverTransport
  • Swift_LoadBalancedTransport
  • Swift_MailTransport
  • Swift_Plugins_Loggers_ArrayLogger
  • Swift_Plugins_Loggers_EchoLogger
  • Swift_SendmailTransport
  • Swift_SmtpTransport
  • Swift_Transport_AbstractSmtpTransport
  • Swift_Transport_Esmtp_Auth_CramMd5Authenticator
  • Swift_Transport_Esmtp_Auth_LoginAuthenticator
  • Swift_Transport_Esmtp_Auth_PlainAuthenticator
  • Swift_Transport_Esmtp_AuthHandler
  • Swift_Transport_EsmtpTransport
  • Swift_Transport_FailoverTransport
  • Swift_Transport_LoadBalancedTransport
  • Swift_Transport_MailTransport
  • Swift_Transport_SendmailTransport
  • Swift_Transport_SimpleMailInvoker
  • Swift_Transport_StreamBuffer

Interfaces

  • Swift_Plugins_Logger
  • Swift_Plugins_Pop_Pop3Exception
  • Swift_Transport
  • Swift_Transport_Esmtp_Authenticator
  • Swift_Transport_EsmtpHandler
  • Swift_Transport_IoBuffer
  • Swift_Transport_MailInvoker
  • Swift_Transport_SmtpAgent
  • Swift_TransportException
  • Overview
  • Package
  • Function
  • Todo
  • Download
  1: <?php
  2: 
  3: /**
  4:  * PIFA form importer.
  5:  *
  6:  * @package Plugin
  7:  * @subpackage FormAssistant
  8:  * @version SVN Revision $Rev:$
  9:  * @author marcus.gnass
 10:  * @copyright four for business AG
 11:  * @link http://www.4fb.de
 12:  */
 13: 
 14: // assert CONTENIDO framework
 15: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
 16: 
 17: /**
 18:  * This class allows for exporting a PIFA form as XML.
 19:  * The exported file contains the structure of the form and its fields.
 20:  * Optionally the export file may contain the forms gathered data.
 21:  *
 22:  * Example usage:
 23:  * <code>plugin_include('form_assistant', 'classes/class.pifa.importer.php');
 24:  * $imp = new PifaImporter();
 25:  * $imp->import($xml);</code>
 26:  *
 27:  * @author marcus.gnass
 28:  */
 29: class PifaImporter {
 30: 
 31:     /**
 32:      * The XML reader that is used to import a XML file.
 33:      *
 34:      * @var cXmlReader
 35:      */
 36:     private $_reader;
 37: 
 38:     /**
 39:      * PIFA form collection used to create new item.
 40:      *
 41:      * @var PifaFormCollection
 42:      */
 43:     private $_pifaFormColl;
 44: 
 45:     /**
 46:      * PIFA field collection used to create new item.
 47:      *
 48:      * @var PifaFieldCollection
 49:      */
 50:     private $_pifaFieldColl;
 51: 
 52:     /**
 53:      * Name of data table to create
 54:      *
 55:      * @var string
 56:      */
 57:     private $_tableName;
 58: 
 59:     /**
 60:      * Create an instance.
 61:      * Creates XML reader member instances.
 62:      */
 63:     public function __construct() {
 64:         $this->_reader = new cXmlReader();
 65:         $this->_pifaFormColl = new PifaFormCollection();
 66:         $this->_pifaFieldColl = new PifaFieldCollection();
 67:     }
 68: 
 69:     /**
 70:      * @param string $_tableName
 71:      */
 72:     public function setTableName($_tableName) {
 73:         $this->_tableName = $_tableName;
 74:     }
 75: 
 76:     /**
 77:      * Import the given XML.
 78:      *
 79:      * @param string $xml to import
 80:      * @throws PifaException if XML could not be loaded
 81:      * @throws PifaException if existance of table could not be determined
 82:      * @throws PifaException if table already exists
 83:      * @throws PifaException if table could not be created
 84:      */
 85:     public function import($xml) {
 86: 
 87:         // load XML
 88:         if (!$this->_reader->loadXML($xml)) {
 89:             throw new PifaException('XML could not be loaded');
 90:         }
 91: 
 92:         // import form
 93:         $formElem = $this->_reader->getXpathNode('/pifa/form');
 94:         if (is_null($this->_tableName)) {
 95:             $this->_tableName = $formElem->getAttribute('table');
 96:         }
 97:         $this->_checkTableName();
 98:         $pifaForm = $this->_createPifaForm($formElem);
 99: 
100:         // import fields
101:         $fieldElems = $this->_reader->getXpathNodeList('/pifa/form/field');
102:         foreach ($fieldElems as $fieldElem) {
103:             $pifaField = $this->_createPifaField($fieldElem, $pifaForm);
104:         }
105: 
106:         // create table
107:         $pifaForm->loadFields();
108:         $pifaForm->createTable('true' === $formElem->getAttribute('timestamp'));
109: 
110:         // import data
111:         $rowElems = $this->_reader->getXpathNodeList('/pifa/data/row');
112:         $db = cRegistry::getDb();
113:         foreach ($rowElems as $rowElem) {
114:             $rowPath = $rowElem->getNodePath();
115:             $fields = array();
116:             if ('true' == $formElem->getAttribute('timestamp')) {
117:                 $fields['pifa_timestamp'] = $rowElem->getAttribute('timestamp');
118:             }
119:             $colElems = $this->_reader->getXpathNodeList($rowPath . '/col');
120:             foreach ($colElems as $colElem) {
121:                 $fields[$colElem->getAttribute('name')] = $colElem->nodeValue;
122:             }
123:             $sql = $db->buildInsert($formElem->getAttribute('table'), $fields);
124:             $db->query($sql);
125:         }
126:     }
127: 
128:     /**
129:      * Create new PIFA form for current client and language.
130:      *
131:      * @param DOMElement $formElem to get data from
132:      * @return PifaForm
133:      */
134:     private function _createPifaForm(DOMElement $formElem) {
135:         return $this->_pifaFormColl->createNewItem(array(
136:             'idclient' => cRegistry::getClientId(),
137:             'idlang' => cRegistry::getLanguageId(),
138:             'name' => $formElem->getAttribute('name'),
139:             'data_table' => $this->_tableName,
140:             'method' => $formElem->getAttribute('method'),
141:             'with_timestamp' => (int) ('true' === $formElem->getAttribute('timestamp'))
142:         ));
143:     }
144: 
145:     /**
146:      * Create PIFA field for given PIFA form..
147:      *
148:      * @param DOMElement $formElem to create field for
149:      * @return PifaForm
150:      */
151:     private function _createPifaField(DOMElement $fieldElem, PifaForm $pifaForm) {
152: 
153:         // get XPATH of this element to access children
154:         $fieldPath = $fieldElem->getNodePath();
155: 
156:         // create PIFA field
157:         $data = array(
158:             'idform' => $pifaForm->get('idform'),
159:             'field_rank' => $fieldElem->getAttribute('rank'),
160:             'field_type' => $this->_getPifaFieldTypeId($fieldElem->getAttribute('type')),
161:             'column_name' => $fieldElem->getAttribute('column'),
162:             'obligatory' => (int) ('true' === $fieldElem->getAttribute('obligatory'))
163:         );
164: 
165:         // import default (optional)
166:         if ($fieldElem->hasAttribute('default')) {
167:             $data['default_value'] = $fieldElem->getAttribute('default');
168:         }
169: 
170:         // import label
171:         $label = $this->_reader->getXpathValue($fieldPath . '/label');
172:         $data['label'] = strip_tags($label);
173:         $labelElem = $this->_reader->getXpathNode($fieldPath . '/label');
174:         if ($labelElem)
175:         { $display = (int) ('true' === $labelElem->getAttribute('display')); $data['display_label'] = $display; }
176: 
177:         // import help (optional)
178:         if (0 < $this->_reader->countXpathNodes($fieldPath . '/help')) {
179:             $help = $this->_reader->getXpathValue($fieldPath . '/help');
180:             $help = $this->_unCdata($help);
181:             $data['help_text'] = $help;
182:         }
183: 
184:         // import error (optional)
185:         if (0 < $this->_reader->countXpathNodes($fieldPath . '/error')) {
186:             $error = $this->_reader->getXpathValue($fieldPath . '/error');
187:             $error = $this->_unCdata($error);
188:             $data['error_message'] = $error;
189:         }
190: 
191:         // import rule (optional)
192:         if (0 < $this->_reader->countXpathNodes($fieldPath . '/rule')) {
193:             $rule = $this->_reader->getXpathValue($fieldPath . '/rule');
194:             $rule = $this->_unCdata($rule);
195:             $data['rule'] = $rule;
196:         }
197: 
198:         // import classes
199:         $classElems = $this->_reader->getXpathNodeList($fieldPath . '/classes/class');
200:         $cssClass = array();
201:         foreach ($classElems as $classElem) {
202:             array_push($cssClass, $classElem->nodeValue);
203:         }
204:         $data['css_class'] = implode(',', $cssClass);
205: 
206:         // import options
207:         $optionsElem = $this->_reader->getXpathNode($fieldPath . '/options');
208:         if ($optionsElem) {
209:             if ($optionsElem->hasAttribute('source')) {
210:                 $data['option_class'] = $optionsElem->getAttribute('source');
211:             }
212:             $optionElems = $this->_reader->getXpathNodeList($fieldPath . '/options/option');
213:             $optionLabels = $optionValues = array();
214:             foreach ($optionElems as $optionElem) {
215:                 array_push($optionLabels, $optionElem->nodeValue);
216:                 array_push($optionValues, $optionElem->getAttribute('value'));
217:             }
218:             $data['option_labels'] = implode(',', $optionLabels);
219:             $data['option_values'] = implode(',', $optionValues);
220:         }
221: 
222:         return $this->_pifaFieldColl->createNewItem($data);
223:     }
224: 
225:     /**
226:      */
227:     private function _checkTableName() {
228:         $db = cRegistry::getDb();
229:         $sql = '-- _checkTableName()
230:             show tables
231:                 like "' . $db->escape($this->_tableName) . '"
232:             ;';
233:         $db->query($sql);
234:         if (0 < $db->numRows()) {
235:             throw new PifaDatabaseException("table $this->_tableName already exists");
236:         }
237:     }
238: 
239:     /**
240:      * Map a PIFA field name to a numeric ID that is used to identify the
241:      * appropriate database record.
242:      *
243:      * @param string $fieldTypeName to map
244:      */
245:     private function _getPifaFieldTypeId($fieldTypeName) {
246:         $fieldTypeName = strtoupper($fieldTypeName);
247:         $fieldTypeIds = array(
248:             'INPUTTEXT' => PifaField::INPUTTEXT,
249:             'TEXTAREA' => PifaField::TEXTAREA,
250:             'INPUTPASSWORD' => PifaField::INPUTPASSWORD,
251:             'INPUTRADIO' => PifaField::INPUTRADIO,
252:             'INPUTCHECKBOX' => PifaField::INPUTCHECKBOX,
253:             'SELECT' => PifaField::SELECT,
254:             'SELECTMULTI' => PifaField::SELECTMULTI,
255:             'DATEPICKER' => PifaField::DATEPICKER,
256:             'INPUTFILE' => PifaField::INPUTFILE,
257:             'PROCESSBAR' => PifaField::PROCESSBAR,
258:             'SLIDER' => PifaField::SLIDER,
259:             // 'CAPTCHA' => PifaField::CAPTCHA,
260:             'BUTTONSUBMIT' => PifaField::BUTTONSUBMIT,
261:             'BUTTONRESET' => PifaField::BUTTONRESET,
262:             // @deprecated use PifaField::BUTTON instead
263:             'BUTTONBACK' => PifaField::BUTTONBACK,
264:             'BUTTON' => PifaField::BUTTON,
265:             'MATRIX' => PifaField::MATRIX,
266:             'PARAGRAPH' => PifaField::PARA,
267:             'INPUTHIDDEN' => PifaField::INPUTHIDDEN,
268:             'FIELDSET_BEGIN' => PifaField::FIELDSET_BEGIN,
269:             'FIELDSET_END' => PifaField::FIELDSET_END,
270:             'BUTTONIMAGE' => PifaField::BUTTONIMAGE
271:         );
272:         $fieldTypeId = $fieldTypeIds[$fieldTypeName];
273:         return $fieldTypeId;
274:     }
275: 
276:     /**
277:      * Remove CDATA syntax from a string using a regular expression.
278:      *
279:      * @param string $str to handle
280:      * @throws PifaException
281:      * @return string
282:      */
283:     private function _unCdata($str) {
284:         $regex = '/<\!\[CDATA\[(.*)\]\]>/is';
285:         $match = preg_replace($regex, '$1', $str);
286:         if (is_null($match)) {
287:             throw new PifaException("could not _unCdata() '$str'");
288:         }
289:         return (string) $match;
290:     }
291: }
292: 
CMS CONTENIDO 4.9.7 API documentation generated by ApiGen