[Phpcms-plugins-cvs] formular4phpCMS/include class.form4phpcms.php,NONE,1.1
Brought to you by:
mjahn
From: Martin J. <mj...@us...> - 2004-07-28 10:54:35
|
Update of /cvsroot/phpcms-plugins/formular4phpCMS/include In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv20418/include Added Files: class.form4phpcms.php Log Message: email notification and datafile insert --- NEW FILE: class.form4phpcms.php --- <?php /** * Form4phpCMS - a simple form-engine for phpCMS * * <b>License</b> * * 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 * * @author Martin Jahn <mj...@us...> * @license http://opensource.org/licenses/gpl-license.php GNU Public License * @copyright Copyright (c) 2004, Martin Jahn * @version $Id: class.form4phpcms.php,v 1.1 2004/07/28 10:54:26 mjahn Exp $ * @package form4phpCMS **/ /* * $Log: class.form4phpcms.php,v $ * Revision 1.1 2004/07/28 10:54:26 mjahn * email notification and datafile insert * */ /** * include necessary PEAR-packages */ include_once ('XML/Serializer.php'); include_once ('XML/Unserializer.php'); include_once ('XML/Tree.php'); include_once ('HTML/QuickForm.php'); include_once ('Mail.php'); /** * @package form4phpCMS **/ class form4phpcms { var $_xml_options = array ('indent'=>"\t", 'linebreak'=>"\n", 'addDecl'=>true, 'tagName'=>'xml', 'defaultTagName'=>'entry', 'encoding'=>'iso-8859-15'); var $_configfile = ''; var $_datafile = ''; function form4phpcms ($configfile = '') { register_shutdown_function (array (&$this, '__destruct')); return $this->__construct ($configfile); } function loadConfigFile ($configfile) { $this->_configfile = $configfile; return $this->_loadConfigFile (); } function start () { $this->_createFormData (); if ($this->FORM->validate ()) { // process form-data $this->FORM->process (array (&$this, '_processFormData'), false); } $this->FORM->display (); } /** * @access private **/ function _loadConfigFile () { if (!file_exists ($this->_configfile)) { return false; } $XML =& new XML_Tree ($this->_configfile); $tree =& $XML->getTreeFromFile (); $num = count ($tree->children); for ($i = 0; $i < $num; $i++) { $this->_config [$tree->children [$i]->name] =& $tree->children [$i]; } } /** * @access private **/ function _saveConfigFile () { } /** * @access private **/ function _loadDataFile () { // check for existing datafile if (!file_exists ($this->_datafile) && !is_writeable (dirname ($this->_datafile))) { echo '1'; return false; } // create datafile if not exists if (!file_exists ($this->_datafile)) { $fh = fopen ($this->_datafile, 'wb'); fclose ($fh); $this->_data = array (); echo '2'; return true; } // parse XML data from file into array $XML =& new XML_Unserializer (); if (!$XML->unserialize ($this->_datafile, true)) { return false; } $this->_data =& $XML->getUnserializedData (); if (PEAR::isError ($this->_data)) { return false; } $this->_data = $this->_data ['entry']; return true; } /** * @access private **/ function _saveDataFile () { // check for existing data if (!isset ($this->_data)) { return false; } // check for existing datafile if (!file_exists ($this->_datafile) && !is_writeable (dirname ($this->_datafile))) { return false; } // check for write-permissions if (!is_writeable ($this->_datafile)) { return false; } // serialize the data into an XML-structure $XML =& new XML_Serializer ($this->_xml_options); $XML->serialize ($this->_data); $data = $XML->getSerializedData (); // write serialized data into file $fh = fopen ($this->_datafile, 'wb'); fwrite ($fh, $data); fclose ($fh); } /** * @access private **/ function _processFormData () { $this->_datafile = realpath (dirname (__FILE__).'/../form4phpcms.data.xml'); $this->_loadDataFile (); $this->_data [] = array_merge ($_POST ['KONTAKT'], array ('time'=>time ())); $this->_sendMail ($_POST ['KONTAKT'] ['subject'], $_POST ['KONTAKT'] ['message']); $this->_saveDataFile (); echo '<!--'; print_r ($this); echo '-->'; } function _sendMail ($subject, $text) { $MAIL =& MAIL::factory ('mail'); $MAIL->send ($this->_config ['mail:to']->content, array ('From'=>$this->_config ['mail:from']->content, 'Subject'=>$subject), $text); } /** * @access private **/ function _createFormData () { $this->FORM =& new HTML_QuickForm ('formmailer'); if (PEAR::isError ($this->FORM)) { echo '1'; return false; } $TPL =& $this->FORM->defaultRenderer(); $TPL->setFormTemplate('<form {attributes}>'."\n".'{content}</form>'."\n"); $TPL->setHeaderTemplate('<h2>{header}</h2>'."\n"); $TPL->setRequiredNoteTemplate(''); $TPL->setElementTemplate('<div <!-- BEGIN required -->class="required"<!-- END required -->>'."\n".' {label}<br />'."\n".' {element}'."\n".'<!-- BEGIN error --> <p class="error">{error}</p><!-- END error -->'."\n".'</div>'."\n"); $num = count ($this->_config ['formular']->children); for ($i = 0; $i < $num; $i++) { $this->_createFormElements ($this->_config ['formular']->children [$i]); } } /** * @access private **/ function _createFormElements (&$data) { switch ($data->name) { case 'select': $this->FORM->addElement('select', $data->attributes ['name'], $data->attributes ['label']); $select =& $this->FORM->getElement($data->attributes ['name']); $select->setsize(1); $select->setMultiple(false); $num = count ($data->children); for ($i = 0; $i < $num; $i++) { if ($i == 0) { $select->addOption($data->children [$i]->content, $data->children [$i]->content,'selected="selected"'); continue; } $select->addOption($data->children [$i]->content, $data->children [$i]->content); } break; case 'text': $this->FORM->addElement ('text', $data->attributes ['name'], $data->attributes ['label']); break; case 'textarea': $this->FORM->addElement ('textarea', $data->attributes ['name'], $data->attributes ['label'], 'cols="40" rows="10"'); break; case 'submit': $this->FORM->addElement ('submit', $data->attributes ['name'], $data->attributes ['label']); break; case 'check': $this->FORM->addElement ('checkbox', $data->attributes ['name'], '', $data->attributes ['label']); break; default:; } } /** * @access private **/ function __construct ($configfile = '') { if ($configfile == '') { echo '1'; return true; } return $this->loadConfigFile ($configfile); } /** * @access private **/ function __destruct () { } } ?> |