/** * Display form * * @param integer $form_id Form id dispaly * @param string $target_url Target url to link form to */ public function display($form_id, $target_url = null) { /* Get the stored form information from the backend. */ try { $form_info = $GLOBALS['injector']->getInstance('Ulaform_Factory_Driver')->create()->getForm($form_id, Horde_Perms::READ); } catch (Horde_Exception $e) { throw new Ulaform_Exception($e->getMessage()); } if (!empty($form_info['form_params']['language'])) { Horde_Nls::setLanguageEnvironment($form_info['form_params']['language']); } $vars = Horde_Variables::getDefaultVariables(); $vars->set('form_id', $form_id); $form = new Horde_Form($vars); $form->addHidden('', 'form_id', 'int', false); $form->addHidden('', 'user_uid', 'text', false); $form->addHidden('', 'email', 'email', false); $vars->set('user_uid', $GLOBALS['registry']->getAuth()); $vars->set('email', $GLOBALS['prefs']->getValue('from_addr')); try { $fields = $GLOBALS['injector']->getInstance('Ulaform_Factory_Driver')->create()->getFields($form_id); } catch (Ulaform_Exception $e) { throw new Ulaform_Exception($e->getMessage()); } foreach ($fields as $field) { /* In case of these types get array from stringlist. */ if ($field['field_type'] == 'link' || $field['field_type'] == 'enum' || $field['field_type'] == 'multienum' || $field['field_type'] == 'mlenum' || $field['field_type'] == 'radio' || $field['field_type'] == 'set' || $field['field_type'] == 'sorter') { $field['field_params']['values'] = Ulaform::getStringlistArray($field['field_params']['values']); } /* Setup the field with all the parameters. */ $form->addVariable($field['field_label'], $field['field_name'], $field['field_type'], $field['field_required'], $field['field_readonly'], $field['field_desc'], $field['field_params']); } /* Check if submitted and validate. */ $result = array('title' => $form_info['form_name']); if ($form->validate()) { $form->getInfo(null, $info); try { $GLOBALS['ulaform_driver']->submitForm($info); return true; } catch (Horde_Exception $e) { throw new Ulaform_Exception(sprintf(_("Error submitting form. %s."), $e->getMessage())); } } if (is_null($target_url)) { $target_url = Horde::selfUrl(true); } Horde::startBuffer(); $form->renderActive(null, null, $target_url, 'post', 'multipart/form-data'); return array('title' => $form_info['form_name'], 'form' => Horde::endBuffer()); }
/** * Returns a list of available action drivers. * * @return array An array of available drivers. */ public static function getDrivers() { static $drivers = array(); if (!empty($drivers)) { return $drivers; } $driver_path = __DIR__ . '/Action/'; $drivers = array(); if ($driver_dir = opendir($driver_path)) { while (false !== ($file = readdir($driver_dir))) { /* Hide dot files and non .php files. */ if (substr($file, 0, 1) != '.' && substr($file, -4) == '.php') { $driver = substr($file, 0, -4); $driver_info = Ulaform::getActionInfo($driver); $drivers[$driver] = $driver_info['name']; } } closedir($driver_dir); } return $drivers; }
protected function _content() { $vars = Horde_Variables::getDefaultVariables(); $formname = $vars->get('formname'); $done = false; $form = new Horde_Form($vars); $fields = $GLOBALS['injector']->getInstance('Ulaform_Factory_Driver')->create()->getFields($this->_params['form_id']); foreach ($fields as $field) { /* In case of these types get array from stringlist. */ if ($field['field_type'] == 'link' || $field['field_type'] == 'enum' || $field['field_type'] == 'multienum' || $field['field_type'] == 'radio' || $field['field_type'] == 'set' || $field['field_type'] == 'sorter') { $field['field_params']['values'] = Ulaform::getStringlistArray($field['field_params']['values']); } /* Setup the field with all the parameters. */ $form->addVariable($field['field_label'], $field['field_name'], $field['field_type'], $field['field_required'], $field['field_readonly'], $field['field_desc'], $field['field_params']); } if ($formname) { $form->validate($vars); if ($form->isValid() && $formname) { $form->getInfo($vars, $info); $info['form_id'] = $this->_params['form_id']; try { $submit = $GLOBALS['ulaform_driver']->submitForm($info); $GLOBALS['notification']->push(_("Form submitted successfully."), 'horde.success'); $done = true; } catch (Horde_Exception $e) { $GLOBALS['notification']->push(sprintf(_("Error submitting form. %s."), $e->getMessage()), 'horde.error'); } } } /* Render active or inactive, depending if submitted or * not. */ $render_type = $done ? 'renderInactive' : 'renderActive'; /* Render the form. */ $renderer = new Horde_Form_Renderer(); $renderer->showHeader(false); Horde::startBuffer(); $form->{$render_type}($renderer, $vars, Horde::selfUrl(true), 'post'); return Horde::endBuffer(); }
$fieldform->addHidden('', 'form_id', 'int', false); $fieldform->addHidden('', 'field_order', 'int', false); $fieldform->addHidden('', 'old_field_type', 'text', false); $fieldform->addVariable(_("Label"), 'field_label', 'text', true); $fieldform->addVariable(_("Internal name"), 'field_name', 'text', false, false, _("Set this name if you have a particular reason to override the automatic internal naming of fields.")); /* Set up the field type selection, with a submit action. */ $fields = Ulaform::getFieldTypes(); $v = $fieldform->addVariable(_("Type"), 'field_type', 'enum', true, false, null, array($fields, true)); $v->setAction(Horde_Form_Action::factory('submit')); $v->setOption('trackchange', true); $fieldform->addVariable(_("Required"), 'field_required', 'boolean', false); $fieldform->addVariable(_("Read only"), 'field_readonly', 'boolean', false); $fieldform->addVariable(_("Description"), 'field_desc', 'longtext', false, false, '', array(3, 40)); /* Check if the submitted field type has extra parameters and set them up. */ $field_type = $vars->get('field_type'); $available_params = Ulaform::getFieldParams($field_type); if (!is_null($vars->get('formname')) && $vars->get($v->getVarName()) != $vars->get('__old_' . $v->getVarName()) && !empty($available_params)) { $notification->push(_("This field type has extra parameters."), 'horde.message'); } foreach ($available_params as $name => $param) { $field_id = 'field_params[' . $name . ']'; $param['required'] = isset($param['required']) ? $param['required'] : null; $param['readonly'] = isset($param['readonly']) ? $param['readonly'] : null; $param['desc'] = isset($param['desc']) ? $param['desc'] : null; $fieldform->addVariable($param['label'], $field_id, $param['type'], $param['required'], $param['readonly'], $param['desc']); } /* Set the current field type to the old field type var. */ $vars->set('old_field_type', $field_type); if ($fieldform->validate($vars)) { /* Save field if valid and the current and old field type match. */ $fieldform->getInfo($vars, $info);
if ($form_id && !$formname) { $vars = new Horde_Variables($ulaform_driver->getForm($form_id)); } /* Get the form action var, whether from edit or new. */ $form_action = $vars->get('form_action'); /* Get details for this action. */ $actions = Ulaform_Action::getDrivers(); /* Check if user changed action. */ if ($form_action != $old_form_action && $formname) { $changed_action = true; $notification->push(_("Changed action driver."), 'horde.message'); } /* Selected a action so get the info and parameters for this action. */ if ($form_action) { $action_info = Ulaform::getActionInfo($form_action); $action_params = Ulaform::getActionParams($form_action); } /* Set up the form. */ $form = new Horde_Form($vars, _("Form Details")); $form->setButtons(empty($form_id) ? _("Create") : _("Modify"), true); $form->addHidden('', 'form_id', 'int', false); $form->addHidden('', 'old_form_action', 'text', false); $form->addVariable(_("Name"), 'form_name', 'text', true); /* Selectable action drivers and update form based on selection. */ $v = $form->addVariable(_("Action"), 'form_action', 'enum', true, false, null, array(array('' => _("-- select --")) + $actions)); $v->setAction(Horde_Form_Action::factory('submit')); $v->setHelp('form-action'); if (!empty($action_params)) { foreach ($action_params as $id => $param) { $param['required'] = isset($param['required']) ? $param['required'] : true; $param['readonly'] = isset($param['readonly']) ? $param['readonly'] : false;
/** * Fetches the a list of available forms and the basic data. * * @return array An array of the available forms. * @throws Ulaform_Exception */ public function getForms($form_id = null) { $wsql = ''; $values = array(); if (!is_null($form_id)) { $wsql = ' WHERE form_id = ?'; $values[] = (int) $form_id; } /* Get the forms. */ $sql = 'SELECT form_id, user_uid, form_name, form_action, form_params,' . ' form_onsubmit FROM ulaform_forms' . $wsql; try { $result = $this->_db->selectAll($sql, $values); } catch (Horde_Db_Exception $e) { throw new Ulaform_Exception($e->getMessage()); } return Ulaform::checkPermissions($result, 'form', Horde_Perms::SHOW, 'form_id'); }
} } catch (Horde_Exception $e) { $notification->push(sprintf(_("Could not fetch form ID \"%s\". %s"), $form_id, $e->getMessage()), 'horde.error'); $error = true; } /* Add form variables. */ $form = new Horde_Form($vars); $form->addHidden('', 'form_id', 'int', false); $fields = $injector->getInstance('Ulaform_Factory_Driver')->create()->getFields($form_id); foreach ($fields as $field) { /* In case of these types get array from stringlist. */ if ($field['field_type'] == 'link' || $field['field_type'] == 'enum' || $field['field_type'] == 'multienum' || $field['field_type'] == 'mlenum' || $field['field_type'] == 'radio' || $field['field_type'] == 'set' || $field['field_type'] == 'sorter') { $field['field_params']['values'] = Ulaform::getStringlistArray($field['field_params']['values']); } if ($field['field_type'] == 'matrix') { $field['field_params']['cols'] = Ulaform::getStringlistArray($field['field_params']['cols']); } /* Setup the field with all the parameters. */ $form->addVariable($field['field_label'], $field['field_name'], $field['field_type'], $field['field_required'], $field['field_readonly'], $field['field_desc'], $field['field_params']); } /* Check if submitted and validate. */ if ($form->validate($vars)) { $form->getInfo($vars, $info); try { $submit = $ulaform_driver->submitForm($info); $notification->push(_("Form submitted successfully."), 'horde.success'); $done = true; } catch (Horde_Exception $e) { $notification->push(sprintf(_("Error submitting form. %s."), $e->getMessage()), 'horde.error'); } }