/** * * @return Am_Form * */ public function createNewTicketForm() { $form = parent::createNewTicketForm(); $element = HTML_QuickForm2_Factory::createElement('text', 'loginOrEmail'); $element->setId('loginOrEmail')->setLabel(___('E-Mail Address or Username'))->addRule('callback', ___('Can not find user with such username or email'), array($this, 'checkUser')); //prepend element to form $formElements = $form->getElements(); $form->insertBefore($element, $formElements[0]); $from = HTML_QuickForm2_Factory::createElement('select', 'from'); $from->setLabel(___('Create ticket as')); $from->loadOptions(array('admin' => ___('Admin'), 'user' => ___('Customer'))); $form->insertBefore($from, $element); $form->addScript('script')->setScript(<<<CUT \$("input#loginOrEmail").autocomplete({ minLength: 2, source: window.rootUrl + "/admin-users/autocomplete" }); CUT ); $id = $form->getId(); $snippets = ___('Snippets'); $form->addScript('snippets')->setScript(<<<CUT \$('#{$id}').find('textarea[name=content]').after('<br /><br /><a href="javascript:;" id="snippets-link" class="local">{$snippets}</a>') \$('#snippets-link').bind('click', function(){ var \$this = \$(this); var div = \$('<div></div>'); div.load(window.rootUrl + '/helpdesk/admin/p/view/displaysnippets', {}, function(){ div.dialog({ autoOpen: true, modal : true, title : "", width : 700, position : ['center', 'center'] }); div.find('.grid-wrap').bind('load', function() { \$(this).find('a.am-helpdesk-insert-snippet').unbind().click(function(){ var \$target = \$this.closest('div.am-form').find('textarea[name=content]'); \$target.insertAtCaret(\$(this).data('snippet-content')) div.dialog('close'); }) }) }) }) CUT ); return $form; }
public function createNewTicketForm() { $form = parent::createNewTicketForm(); $member = Am_Di::getInstance()->userTable->load($this->getUserId()); $text = HTML_QuickForm2_Factory::createElement('html', 'loginOrEmail'); $text->setLabel(___('User'))->setHtml(sprintf('<div>%s %s (%s)</div>', $member->name_f, $member->name_l, $member->login)); $text->toggleFrozen(true); $form->insertBefore($text, $form->getElementById('loginOrEmail')); $form->removeChild($form->getElementById('loginOrEmail')); $loginOrEmail = HTML_QuickForm2_Factory::createElement('hidden', 'loginOrEmail'); $loginOrEmail->setValue($member->login); $form->addElement($loginOrEmail); $user_id = HTML_QuickForm2_Factory::createElement('hidden', 'user_id'); $user_id->setValue($member->pk()); $form->addElement($user_id); return $form; }
/** * * @return Am_Form * */ public function createNewTicketForm() { $form = parent::createNewTicketForm(); $element = HTML_QuickForm2_Factory::createElement('text', 'loginOrEmail'); $element->setId('loginOrEmail')->setLabel(___('E-Mail Address or Username'))->addRule('callback', ___('Can not find user with such username or email'), array($this, 'checkUser')); //prepend element to form $formElements = $form->getElements(); $form->insertBefore($element, $formElements[0]); $from = HTML_QuickForm2_Factory::createElement('select', 'from'); $from->setLabel(___('Create ticket as')); $from->loadOptions(array('admin' => ___('Admin'), 'user' => ___('Customer'))); $form->insertBefore($from, $element); $script = <<<CUT \$("input#loginOrEmail").autocomplete({ minLength: 2, source: window.rootUrl + "/admin-users/autocomplete" }); CUT; $form->addScript('script')->setScript($script); return $form; }
public function toArray() { $id = $this->getId(); $name = $this->getName(); $selectFrom = new HTML_QuickForm2_Element_Select("_{$name}", array('id' => "{$id}-from") + $this->attributes); $selectTo = new HTML_QuickForm2_Element_Select($name, array('id' => "{$id}-to") + $this->attributes); $strValues = array_map('strval', $this->values); foreach ($this->optionContainer as $option) { // We don't do optgroups here if (!is_array($option)) { continue; } $value = $option['attr']['value']; unset($option['attr']['value']); if (in_array($value, $strValues, true)) { $selectTo->addOption($option['text'], $value, empty($option['attr']) ? null : $option['attr']); } else { $selectFrom->addOption($option['text'], $value, empty($option['attr']) ? null : $option['attr']); } } $buttonFromTo = HTML_QuickForm2_Factory::createElement('button', "{$name}_fromto", array('type' => 'button', 'id' => "{$id}-fromto") + (empty($this->data['from_to']['attributes']) ? array() : self::prepareAttributes($this->data['from_to']['attributes'])), array('content' => empty($this->data['from_to']['content']) ? ' >> ' : $this->data['from_to']['content'])); $buttonToFrom = HTML_QuickForm2_Factory::createElement('button', "{$name}_tofrom", array('type' => 'button', 'id' => "{$id}-tofrom") + (empty($this->data['to_from']['attributes']) ? array() : self::prepareAttributes($this->data['to_from']['attributes'])), array('content' => empty($this->data['to_from']['content']) ? ' << ' : $this->data['to_from']['content'])); return array('select_from' => $selectFrom->__toString(), 'select_to' => $selectTo->__toString(), 'button_from_to' => $buttonFromTo->__toString(), 'button_to_from' => $buttonToFrom->__toString()); }
/** * Appends an element to the container (possibly creating it first) * * If the first parameter is an instance of HTML_QuickForm2_Node then all * other parameters are ignored and the method just calls {@link appendChild()}. * In the other case the element is first created via * {@link HTML_QuickForm2_Factory::createElement()} and then added via the * same method. This is a convenience method to reduce typing and ease * porting from HTML_QuickForm. * * @param string|HTML_QuickForm2_Node Either type name (treated * case-insensitively) or an element instance * @param mixed Element name * @param mixed Element attributes * @param array Element-specific data * @return HTML_QuickForm2_Node Added element * @throws HTML_QuickForm2_InvalidArgumentException * @throws HTML_QuickForm2_NotFoundException */ public function addElement($elementOrType, $name = null, $attributes = null, array $data = array()) { if ($elementOrType instanceof HTML_QuickForm2_Node) { return $this->appendChild($elementOrType); } else { return $this->appendChild(HTML_QuickForm2_Factory::createElement($elementOrType, $name, $attributes, $data)); } }
public function testCreateElementValid() { HTML_QuickForm2_Factory::registerElement('fakeelement', 'FakeElement', dirname(__FILE__) . '/_files/FakeElement.php'); $el = HTML_QuickForm2_Factory::createElement('fakeelement', 'fake', 'attributes', array('options' => '', 'label' => 'fake label')); $this->assertType('FakeElement', $el); $this->assertEquals('fake', $el->name); $this->assertEquals(array('options' => '', 'label' => 'fake label'), $el->data); $this->assertEquals('attributes', $el->attributes); }
/** * Class constructor, form's "id" and "method" attributes can only be set here * * @param string "id" attribute of <form> tag * @param string HTTP method used to submit the form * @param mixed Additional attributes (either a string or an array) * @param bool Whether to track if the form was submitted by adding * a special hidden field */ public function __construct($id, $method = 'post', $attributes = null, $trackSubmit = true) { $method = 'GET' == strtoupper($method) ? 'get' : 'post'; $trackSubmit = empty($id) ? false : $trackSubmit; $this->attributes = array_merge(self::prepareAttributes($attributes), array('method' => $method)); parent::setId(empty($id) ? null : $id); if (!isset($this->attributes['action'])) { $this->attributes['action'] = $_SERVER['PHP_SELF']; } if ($trackSubmit && isset($_REQUEST['_qf__' . $id]) || !$trackSubmit && ('get' == $method && !empty($_GET) || 'post' == $method && (!empty($_POST) || !empty($_FILES)))) { $this->addDataSource(new HTML_QuickForm2_DataSource_SuperGlobal($method, get_magic_quotes_gpc())); } if ($trackSubmit) { $this->appendChild(HTML_QuickForm2_Factory::createElement('hidden', '_qf__' . $id)); } }
public function testRenderStaticLabels() { $element = HTML_QuickForm2_Factory::createElement('text', 'static')->setLabel(array('a label', 'another label', 'foo' => 'named label')); $renderer = HTML_QuickForm2_Renderer::factory('array')->setOption('static_labels', false); $array = $element->render($renderer)->toArray(); $this->assertInternalType('array', $array['label']); $array = $element->render($renderer->setOption('static_labels', true)->reset())->toArray(); $this->assertEquals('a label', $array['label']); $this->assertEquals('another label', $array['label_2']); $this->assertEquals('named label', $array['label_foo']); }
public function testRenderGroupedElementsWithSeparators() { $group = HTML_QuickForm2_Factory::createElement('group', 'foo', array('id' => 'testSeparators')); $element1 = $group->addElement('text', 'bar'); $element2 = $group->addElement('text', 'baz'); $element3 = $group->addElement('text', 'quux'); $renderer = HTML_Quickform2_Renderer::factory('callback')->setCallbackForId('testSeparators', array(get_class($this), '_renderTestSeparators'))->setElementCallbackForGroupId('testSeparators', 'HTML_QuickForm2_Element_InputText', array(get_class($this), '_renderTestSeparators2')); $this->assertEquals('<foo>' . $element1 . '</foo><foo>' . $element2 . '</foo><foo>' . $element3 . '</foo>', $group->render($renderer->reset())->__toString()); $group->setSeparator(' '); $this->assertEquals('<foo>' . $element1 . '</foo> <foo>' . $element2 . '</foo> <foo>' . $element3 . '</foo>', $group->render($renderer->reset())->__toString()); $group->setSeparator(array('<br />', ' ')); $this->assertEquals('<foo>' . $element1 . '</foo><br /><foo>' . $element2 . '</foo> <foo>' . $element3 . '</foo>', $group->render($renderer->reset())->__toString()); }
/** * read_quickform set a HTML_Quickform2_Element-object to $quickform according * to the $type * * @param array $options array containing parameters for the input-tag * @param bool $defaults text-fields with default-values if true * @return string the value of the "id"-parameter of the input-tag */ public function read_quickform($options = array(), $defaults = false) { // prepare return $element = null; // prepare ids $element_ids = ''; // check type if ($this->get_type() == 'text') { // check defaults if ($defaults === true) { // field-group $element = HTML_QuickForm2_Factory::createElement('group', $this->get_table() . '-' . $this->get_id(), $options); $element->setLabel($this->get_name() . ':'); // add select $select = $element->addElement('select', 'defaults', array()); $select->setLabel(parent::lang('class.Field#element#label#textarea.defaults')); // add textarea $textarea = $element->addElement('textarea', 'manual', array()); $textarea->setLabel(parent::lang('class.Field#element#label#textarea.manual')); // add options $this->read_defaults($select); } else { // textarea $element = HTML_QuickForm2_Factory::createElement('textarea', $this->get_table() . '-' . $this->get_id(), $options); $element->setLabel($this->get_name() . ':'); // add rules if ($this->get_required() == 1) { $element->addRule('required', parent::lang('class.Field#element#rule#required.text')); } $element->addRule('regex', parent::lang('class.Field#element#rule#regexp.allowedChars') . ' [' . $_SESSION['GC']->get_config('textarea.desc') . ']', $_SESSION['GC']->get_config('textarea.regexp')); } // add id to return $element_ids = $this->get_table() . '-' . $this->get_id(); } elseif ($this->get_type() == 'date') { // date in input-text for use with jquery $element = HTML_QuickForm2_Factory::createElement('text', $this->get_table() . '-' . $this->get_id(), $options); $element->setLabel($this->get_name() . ':'); // add rules if ($this->get_required() == 1) { $element->addRule('required', parent::lang('class.Field#element#rule#required.date')); } $element->addRule('callback', parent::lang('class.Field#element#rule#check.date'), array($this, 'callback_check_date')); // add id to return $element_ids = $this->get_table() . '-' . $this->get_id(); } elseif ($this->get_type() == 'checkbox') { // checkbox $element = HTML_QuickForm2_Factory::createElement('checkbox', $this->get_table() . '-' . $this->get_id(), $options); $element->setLabel($this->get_name() . ':'); // add rules if ($this->get_required() == 1) { $element->addRule('required', parent::lang('class.Field#element#rule#required.checkbox')); } // add id to return $element_ids = $this->get_table() . '-' . $this->get_id(); } elseif ($this->get_type() == 'dbselect') { // read config $config = $this->get_config(); // merge options $options = array_merge($options, $config['options']); // select $element = HTML_QuickForm2_Factory::createElement('select', $this->get_table() . '-' . $this->get_id(), $options); $element->setLabel($this->get_name() . ':'); // add rules if ($this->get_required() == 1) { $element->addRule('required', parent::lang('class.Field#element#rule#required.checkbox')); $element->addRule('callback', parent::lang('class.Field#entry#rule#check.select'), array($this, 'callback_check_select')); } // add id to return $element_ids = $this->get_table() . '-' . $this->get_id(); // get options from field-config $field_options = array('--'); $this->dbselect_options($field_options); // load options $element->loadOptions($field_options); } elseif ($this->get_type() == 'dbhierselect') { // select $element = HTML_QuickForm2_Factory::createElement('hierselect', $this->get_table() . '-' . $this->get_id(), $options); $element->setLabel($this->get_name() . ':'); // add rules if ($this->get_required() == 1) { $element->addRule('required', parent::lang('class.Field#element#rule#required.checkbox')); $element->addRule('callback', parent::lang('class.Field#entry#rule#check.hierselect'), array($this, 'callback_check_hierselect')); } // add id to return $element_ids = $this->get_table() . '-' . $this->get_id(); // get options from field-config $field_options_first[0] = '--'; $field_options_second[0][0] = '--'; $this->dbhierselect_options($field_options_first, $field_options_second); // load options $element->loadOptions(array($field_options_first, $field_options_second)); } // set $this->set_quickform($element); // return return $element_ids; }
public function testPrependGroupNameOnInsertBefore() { $foo = new HTML_QuickForm2_Container_Group('foo'); $fooBar = $foo->insertBefore(HTML_QuickForm2_Factory::createElement('text', 'bar')); $this->assertEquals('foo[bar]', $fooBar->getName()); $fooBaz = $foo->insertBefore(HTML_QuickForm2_Factory::createElement('text', 'baz'), $fooBar); $this->assertEquals('foo[baz]', $fooBaz->getName()); }
/** * edit_row edits the row for the given rid * * @param string $table table to work on * @param int $rid id of row to edit * @return string HTML-string for the form or message */ private function edit_row($table, $rid) { // prepare return $return = ''; // get url-parameters $link = ''; if ($table == 'defaults') { $link = 'administration.php?id=' . $this->get('id'); } else { $link = 'administration.php?id=' . $this->get('id') . '&field=' . $table; } // get db-object $db = Db::newDb(); // prepare statement $sql = "SELECT * FROM {$table} WHERE id={$rid}"; // execute $result = $db->query($sql); // fetch result $row = $result->fetch_array(MYSQL_ASSOC); // prepare form $form = new HTML_QuickForm2('edit_field', 'post', array('name' => 'edit_field', 'action' => $link . '&action=edit&rid=' . $rid)); // renderer $renderer = HTML_QuickForm2_Renderer::factory('default'); $renderer->setOption('required_note', parent::lang('class.AdministrationView#edit_row#form#requiredNote')); // get values and fields $i = 0; $datasource = array(); $fields = array(); foreach ($row as $col => $value) { // check translation $translated_name = ''; if (parent::lang('class.AdministrationView#tableRows#name#' . $col) != "class.AdministrationView#tableRows#name#{$col} not translated") { $translated_col = parent::lang('class.AdministrationView#tableRows#name#' . $col); } else { $translated_col = $col; } // check category if ($col == 'category') { // get options $cat_sql = "SELECT id,name FROM category WHERE valid=1"; $cat_result = $db->query($cat_sql); $options = array('--'); while (list($id, $name) = $cat_result->fetch_array(MYSQL_NUM)) { $options[$id] = $name; } // add value $datasource[$col] = $value; // select $field = $form->addElement('select', $col, array()); $field->setLabel($translated_col . ':'); // load options $field->loadOptions($options); // add rules if ($table == 'defaults') { $field->addRule('required', parent::lang('class.AdministrationView#edit_row#rule#requiredSelect')); $field->addRule('callback', parent::lang('class.AdministrationView#edit_row#rule#checkSelect'), array($this, 'callback_check_select')); } } else { // check id or valid if ($col != 'id' && $col != 'valid') { // get fieldconfig // 252 = text, 253 = varchar; 1 = tinyint(boolean); 3 = int $field_config = $result->fetch_field_direct($i); // add value $datasource[$col] = $value; // add field $field = null; // check type if ($field_config->type == 252) { // textarea $field = HTML_QuickForm2_Factory::createElement('textarea', $col, array()); $field->setLabel($translated_col . ':'); // add rule $field->addRule('regex', parent::lang('class.AdministrationView#edit_row#rule#regexp.allowedChars') . ' [' . $_SESSION['GC']->get_config('textarea.desc') . ']', $_SESSION['GC']->get_config('textarea.regexp')); // required if ($table == 'defaults') { $field->addRule('required', parent::lang('class.AdministrationView#edit_row#rule#required')); } } elseif ($field_config->type == 253 || $field_config->type == 3) { // input $field = HTML_QuickForm2_Factory::createElement('text', $col, array()); $field->setLabel($translated_col . ':'); // add rule $field->addRule('regex', parent::lang('class.AdministrationView#edit_row#rule#regexp.allowedChars') . ' [' . $_SESSION['GC']->get_config('textarea.desc') . ']', $_SESSION['GC']->get_config('textarea.regexp')); // required if ($table == 'defaults') { $field->addRule('required', parent::lang('class.AdministrationView#edit_row#rule#required')); } } elseif ($field_config->type == 1) { // input $field = HTML_QuickForm2_Factory::createElement('checkbox', $col, array()); $field->setLabel($translated_col . ':'); } $fields[] = $field; } } // increment field-counter $i++; } // add datasource $form->addDataSource(new HTML_QuickForm2_DataSource_Array($datasource)); // add fields foreach ($fields as $field) { $form->appendChild($field); } // submit-button $form->addSubmit('submit', array('value' => parent::lang('class.AdministrationView#edit_row#form#submitButton'))); // validate if ($form->validate()) { // set output $return .= $this->p(' class="edit_caption"', parent::lang('class.AdministrationView#edit_row#caption#done') . ': "' . $rid . '"'); // get data $data = $form->getValue(); // prepare statement $sql = "UPDATE {$table} SET "; foreach ($data as $field => $value) { // check translation $translated_field = ''; if (parent::lang('class.AdministrationView#tableRows#name#' . $field) != "class.AdministrationView#tableRows#name#{$field} not translated") { $translated_field = parent::lang('class.AdministrationView#tableRows#name#' . $field); } else { $translated_field = $field; } // check field if (substr($field, 0, 5) != '_qf__' && $field != 'submit') { // add fields to sql $sql .= "{$field}='{$value}', "; // add fields to output $return .= $this->p('', "{$translated_field} = '" . nl2br(htmlentities(utf8_decode($value))) . "'"); } } $sql = substr($sql, 0, -2); $sql .= " WHERE id={$rid}"; // execute $result = $db->query($sql); // add table content $return .= $this->list_table_content($table, $this->get('page')); } else { $return .= $this->p('', parent::lang('class.AdministrationView#edit_row#caption#edit') . ': "' . $rid . '"'); $return .= $form->render($renderer); } // return return $return; }
/** * File should check that the form has POST method, set enctype to multipart/form-data * @see http://pear.php.net/bugs/bug.php?id=16807 */ public function testRequest16807() { $form = new HTML_QuickForm2('broken', 'get'); try { $form->addFile('upload', array('id' => 'upload')); $this->fail('Expected HTML_QuickForm2_InvalidArgumentException was not thrown'); } catch (HTML_QuickForm2_InvalidArgumentException $e) { } $group = HTML_QuickForm2_Factory::createElement('group', 'fileGroup'); $group->addFile('upload', array('id' => 'upload')); try { $form->appendChild($group); $this->fail('Expected HTML_QuickForm2_InvalidArgumentException was not thrown'); } catch (HTML_QuickForm2_InvalidArgumentException $e) { } $post = new HTML_QuickForm2('okform', 'post'); $this->assertNull($post->getAttribute('enctype')); $post->addFile('upload'); $this->assertEquals('multipart/form-data', $post->getAttribute('enctype')); }
/* @var $repeatFs HTML_QuickForm2_Container_Repeat */ $repeatFs = $fsOne->addRepeat()->setPrototype(HTML_QuickForm2_Factory::createElement('fieldset'))->setId('repeat-fieldset')->setLabel('Shipping addresses'); $countries = array('' => "-- please select --", 4 => "Afghanistan", 148 => "Chad", 180 => "Congo, Democratic Republic of", 368 => "Iraq", 706 => "Somalia", 736 => "Sudan", 716 => "Zimbabwe"); $country = $repeatFs->addSelect('country')->loadOptions($countries)->setLabel('Country:'); $repeatFs->addText('region', array('style' => 'width: 20em;'))->setLabel('Region:'); $street = $repeatFs->addText('street', array('style' => 'width: 20em;'))->setLabel('Street address:'); $repeatFs->addCheckbox('default')->setContent('default shipping address'); // button to remove a repeated item from a repeat, enabled automatically $repeatFs->addButton('remove', array('type' => 'button'))->setContent('remove this address')->addClass('repeatRemove'); // setting rules for repeated elements, these will work properly server-side and client-side $country->addRule('required', 'Please select a country', null, HTML_QuickForm2_Rule::ONBLUR_CLIENT_SERVER); $street->addRule('required', 'Please input street address', null, HTML_QuickForm2_Rule::ONBLUR_CLIENT_SERVER); /* @var $fsTwo HTML_QuickForm2_Container_Fieldset */ $fsTwo = $form->addFieldset()->setLabel('Group-based repeat element'); /* @var $repeatGroup HTML_QuickForm2_Container_Repeat */ $repeatGroup = $fsTwo->addRepeat(null, array('id' => 'repeat-group'), array('prototype' => HTML_QuickForm2_Factory::createElement('group', 'links')->setLabel('A link:')->setSeparator(' ')))->setIndexField('links[title]')->setLabel('Links'); $repeatGroup->addText('title', array('style' => 'width: 15em;')); // specially crafted value attribute to prevent adding index to name $repeatGroup->addRadio('main', array('value' => 'yes_:idx:'))->setContent('main'); // button to remove a repeated item from a repeat $repeatGroup->addButton('remove', array('type' => 'button'))->setContent('X')->addClass('repeatRemove'); // a button for adding repeated elements, with an explicit onclick $fsTwo->addButton('add', array('type' => 'button', 'onclick' => "document.getElementById('repeat-group').repeat.add(); return false;"))->setContent('Add another link'); $form->addSubmit('submit', array('value' => 'Send this form')); /* @var $renderer HTML_QuickForm2_Renderer_Default */ $renderer = HTML_QuickForm2_Renderer::factory('default'); // a custom template for first repeat element, a link for adding repeated // elements there will be automatically made active due to repeatAdd class $renderer->setTemplateForId('repeat-fieldset', <<<HTML <div class="row repeat" id="{id}"> <qf:label><p>{label}</p></qf:label>
/** * Class constructor, form's "id" and "method" attributes can only be set here * * @param string "id" attribute of <form> tag * @param string HTTP method used to submit the form * @param mixed Additional attributes (either a string or an array) * @param bool Whether to track if the form was submitted by adding * a special hidden field */ public function __construct($id, $method = 'post', $attributes = null, $trackSubmit = true) { /*$id = @$params['id'] ? $params['id'] : '';//'' $method= @$params['method'] ? $params['method'] : 'post' ;//post $attributes= @$params['attributes'] ? $params['attributes'] : null ;//null; $trackSubmit = @$params['trackSubmit'] ? $params['trackSubmit'] : true;//false*/ $method = 'GET' == strtoupper($method) ? 'get' : 'post'; if (empty($id)) { $id = self::generateId(''); $trackSubmit = false; } else { self::storeId($id); } $this->attributes = array_merge(self::prepareAttributes($attributes), array('id' => (string) $id, 'method' => $method)); if (!isset($this->attributes['action'])) { $this->attributes['action'] = $_SERVER['PHP_SELF']; } if ($trackSubmit && isset($_REQUEST['_qf__' . $id]) || !$trackSubmit && ('get' == $method && !empty($_GET) || 'post' == $method && (!empty($_POST) || !empty($_FILES)))) { $this->addDataSource(new HTML_QuickForm2_DataSource_SuperGlobal($method, get_magic_quotes_gpc())); } if ($trackSubmit) { $this->appendChild(HTML_QuickForm2_Factory::createElement('hidden', '_qf__' . $id)); } }