* @license LGPL, see license.txt for details * @link http://www.php-tools.net */ /** * Main examples prepend file, needed *only* for the examples framework! */ include_once 'patExampleGen/prepend.php'; $exampleGen->displayHead( 'Example' ); // EXAMPLE START ------------------------------------------------------ // change this according to your setup $pathToBookstore = dirname(__FILE__) . '/propel'; // omit 'bookstore/' here // change these according to your propel settings $pathToCache = './cache'; $propelConfFilename = 'conf/bookstore-conf.php'; $classname = 'Book'; /** * setup include path */ set_include_path(get_include_path() . PATH_SEPARATOR . $pathToBookstore); /** * include needed files */ require_once 'propel/Propel.php'; require_once $neededFiles['patForms']; require_once $neededFiles['patErrorManager']; require_once $neededFiles['patI18n_configure']; require_once $neededFiles['patTemplate']; require_once 'patForms/Definition/Propel.php'; require_once 'bookstore/Book.php'; require_once 'bookstore/Author.php'; require_once 'bookstore/Publisher.php'; Propel::init($propelConfFilename); $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : null; switch($action) { default: case 'list': { displayPropelList(array( 'classname' => $classname )); break; } case 'details': { displayPropelDetails(array( 'primary' => (isset($_REQUEST['Id']) ? array('Id' => $_REQUEST['Id']) : array()), 'classname' => $classname )); break; } case 'form': { displayPropelForm(array( 'primary' => (isset($_REQUEST['Id']) ? array('Id' => $_REQUEST['Id']) : array()), 'classname' => $classname, 'path' => $pathToCache, )); break; } } exit; function displayPropelList($args) { extract($args); $callable = array($classname . 'Peer', 'doSelect'); $list = call_user_func_array($callable, array(new Criteria())); echo '

'; echo 'Add'; echo '

'; echo ''; $row = ''; foreach($list as $book) { if ($author = $book->getAuthor()) { $authorname = $author->getLastname() . '(' . $author->getFirstname() . ')'; } else { $authorname = '-'; } $line = sprintf($row, basename($_SERVER['PHP_SELF']) . '?action=details&Id=' . $book->getId(), $book->getTitle(), $book->getIsbn(), $authorname, basename($_SERVER['PHP_SELF']) . '?action=form&Id=' . $book->getId() ); echo $line; } echo '
%s%s%sedit
'; } function displayPropelDetails($args) { extract($args); $c = new Criteria(); $pk = ''; foreach ($primary as $key => $value) { $c->add('book.' . $key, $value); $pk .= '&' . $key . '=' . $value; } $callable = array($classname . 'Peer', 'doSelectOne'); $book = call_user_func_array($callable, array($c)); echo '

'; echo 'List · '; echo 'Edit'; echo '

'; echo ''; $row = ''; foreach($book->toArray() as $key => $value) { if ($key == 'AuthorId') { $author = $book->getAuthor(); $key = 'Author'; $value = ($author ? $author->getLastName() : ''); } if ($key == 'PublisherId') { $publisher = $book->getPublisher(); $key = 'Publisher'; $value = ($publisher ? $publisher->getName() : ''); } $line = sprintf($row, $key, $value); echo $line; } echo '
%s%s
'; } function displayPropelForm($args) { extract($args); $form = patForms_Factory_Propel::create($args); $tpl = &$form->renderForm(array( 'tmplDir' => dirname(__FILE__) . '/templates', 'tmplFile' => 'example_storage_propel.tmpl', 'tmplName' => 'form', 'elementTemplate' => 'element', 'errorTemplateContainer' => 'errors', 'errorTemplate' => 'error' )); $pk = ''; foreach ((array) $primary as $key => $value) { $pk .= '&' . $key . '=' . $value; } echo '

'; echo 'List'; if ($pk) { echo ' · '; echo 'Details'; } echo '

'; if (!$form->isSubmitted()) { $tpl->displayParsedTemplate(); } elseif (!$form->valid) { displayErrors($form); $tpl->displayParsedTemplate(); } else { echo 'Save successful'; } } // could this probably make its way into patForms? class patForms_Factory_Propel { static public function create($conf) { extract($conf); // create a patTemplate renderer $renderer =& patForms::createRenderer('patTemplate'); $renderer->setPlaceholder('PATFORMS_ELEMENT', null); $renderer->setAttributes(array('display', 'description')); // create a form definition $definition = patForms_Definition_Propel::create(array( 'name' => $classname, 'filename' => $path . '/form.' . $classname . '.xml', )); // create a form $form = &patForms::createCreator('Definition')->create($definition); $form->enableOption('scripts'); $form->setAutoValidate('save'); $form->setRenderer($renderer); // create a storage $storage = patForms::createStorage('Propel'); $storage->setStorageLocation($classname . 'peer'); $form->setStorage($storage); // set data if (isset($primary)) { $form->setValues($primary); } return $form; } } ?>