[dev] varrenderer question
Andrew Coleman
mercury at appisolutions.net
Fri Jul 23 15:19:48 PDT 2004
Quoting Chuck Hagenbuch <chuck at horde.org>:
> Not without seeing the code you're using - can you post just the snippet that
> results in this?
Here is the code for the form:
function StockForm(&$vars)
{
parent::Horde_Form($vars, _("Edit Inventory Item"));
// Prepare the categories
$categories = $GLOBALS['backend']->getCategories();
foreach ($categories as $c) {
$cat[$c['category_id']] = $c['category'];
}
// Buttons and hidden configuration
$this->appendButtons(_("Save"));
$this->addHidden('', 'actionId', 'text', true);
// Basic variables for any stock item
$stockId = &$this->addVariable(_("Stock ID"), 'stock_id', 'int',
false, false);
$this->addVariable(_("Name"), 'stock_name', 'text', true, false);
$categoryVar = &$this->addVariable(_("Category"), 'category_id',
'multienum', false, false, null, array($cat));
$categoryId = $vars->get('category_id');
if ($categoryId >= 0) {
$properties =
$GLOBALS['backend']->getPropertiesForCategories($categoryId);
if (!is_a($properties, 'PEAR_Error')) {
foreach ($properties as $property) {
$fieldname = 'property[' .
$property['property_id'] . ']';
$fieldtitle = $property['property'];
$fielddesc = !empty($property['unit']) ?
$property['description'] . ' -- unit: ' .
$property['unit'] :
$property['description'];
$fieldtype = $property['datatype'];
$this->addVariable($fieldtitle, $fieldname, $fieldtype,
false, false, $fielddesc);
}
}
}
$this->addVariable(_("Note"), 'stock_note', 'longtext', false);
// Default action
$action = &Horde_Form_Action::factory('submit');
$action2 = &Horde_Form_Action::factory('conditional_enable', array(
'target' => 'actionId',
'enabled' => true,
'values' => 'add_stock'));
$categoryVar->setAction($action);
$categoryVar->setOption('trackchange', true);
$stockId->setAction($action2);
}
here is the code in the front-end:
$stock = $backend->fetch($stock_id);
$categories = $backend->getCategories($stock_id);
$properties = $backend->getPropertiesForStock($stock_id);
$vars = &Variables::getDefaultVariables();
$formname = $vars->get('formname');
if (empty($formname)) {
// Configure attributes
if ($stock) {
foreach ($stock as $key => $val) {
$vars->set($key, $val);
}
}
// Set up categories
if ($categories) {
$vars->set('category_id', $categories[0]['category_id']);
}
// Properties for categories
$p = array();
foreach ($properties as $property) {
$p[$property['property_id']] = $property['txt_datavalue'];
}
$vars->set('property', $p);
$vars->set('actionId', $actionId);
}
// Set up form variables
$renderer = &new Horde_Form_Renderer();
$form = &Horde_Form::singleton('StockForm', $vars);
$form->setTitle(_("Edit Inventory Item"));
$valid = $form->validate($vars);
if ($valid) {
// Update the stock item
$result = $backend->modify($stock_id, array(
'stock_name' => $vars->get('stock_name'),
'note' => $vars->get('note')));
// Update categories for the stock item
$category = $vars->get('category');
if (!empty($category)) {
$backend->updateCategoriesForStock($stock_id, $category);
$backend->clearPropertiesForStock($stock_id, $category);
}
// Update properties
$property = $vars->get('property');
if (count($property)) {
$backend->updatePropertiesForStock($stock_id, $property);
}
if (!is_a($result, 'PEAR_Error')) {
$notification->push(_("The stock item was successfully updated."),
'horde.success');
} else {
$notification->push(sprintf(
_("There was a problem updating the inventory: %s"),
$result->getMessage()), 'horde.error');
}
// Return to previous view
$returnto = $vars->get('returnto');
if (!empty($returnto)) {
header('Location: ' . Horde::applicationUrl($url, true));
exit;
}
}
<snipsnip>
// Begin page display
require PAPER_TEMPLATES . '/common-header.inc';
Paper::menu();
// Render available forms
$form->open($renderer, $vars, 'stock.php', 'post');
if ($active) {
$renderer->beginActive($form->getTitle());
$renderer->renderFormActive($form, $vars);
$renderer->submit();
} else {
$renderer->beginInactive($form->getTitle());
$renderer->renderFormInactive($form, $vars);
}
$renderer->end();
$form->close($renderer);
looks a lot like sesha code... cos it mostly is
--
Andrew Coleman
mercury at appisolutions.net
/-\ The ASCII
\ / Ribbon Campaign
X Against HTML
/ \ E-Mail!
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-keys
Size: 1430 bytes
Desc: PGP Public Key
Url : http://lists.horde.org/archives/dev/attachments/20040723/5bb7d8cc/attachment.bin
More information about the dev
mailing list