Ejemplo n.º 1
0
 /**
  * Render this page into XHTML.
  *
  * This methods renders the whole page into a complete XHTML page. Usually
  * you want to use flush() to output the page to the browser.
  *
  * \return
  *   The rendered page as a string.
  *
  * \see AnewtPage::flush
  */
 public function render()
 {
     /* Create basic element nodes */
     $head = new AnewtXMLDomElement('head');
     $body = new AnewtXMLDomElement('body');
     $head->always_render_closing_tag = true;
     $body->always_render_closing_tag = true;
     /* Content-type in meta tag. This must be the first element inside the
      * <head>...</head> element. */
     $head->append_child(ax_meta(array('http-equiv' => 'Content-type', 'content' => $this->build_content_type_charset())));
     /* Base URI */
     $base_uri = $this->_get('base-uri');
     if (!is_null($base_uri)) {
         assert('is_string($base_uri); // base-uri must be a simple string');
         /* Employ a simple heuristic to make sure we use an absolute URI, as
          * is required by the HTML specification. */
         if (!str_contains($base_uri, '://')) {
             $base_uri = AnewtRequest::canonical_base_url() . $base_uri;
         }
         $base = new AnewtXHTMLBase();
         $base->set_attribute('href', $base_uri);
         $head->append_child($base);
     }
     /* Page title (always include, even if empty, since this is required by
      * the HTML specification) */
     $title = $this->_get('title');
     $head->append_child(ax_title($title));
     /* Dublin Core metadata. See http://dublincore.org/documents/dcq-html/ * */
     if ($this->_get('show-dublin-core')) {
         $head->append_child(ax_link(array('rel' => 'schema.DC', 'href' => 'http://purl.org/dc/elements/1.1/')));
         $head->append_child(ax_meta_name_content('DC.language', $this->_get('language')));
         if (!is_null($title)) {
             $head->append_child(ax_meta_name_content('DC.title', $title));
         }
         if ($this->_isset('creator')) {
             $head->append_child(ax_meta_name_content('DC.creator', $this->_get('creator')));
         }
         if ($this->_isset('description')) {
             $head->append_child(ax_meta_name_content('DC.description', $this->_get('description')));
         }
         if ($this->_isset('date')) {
             $date = $this->get('date');
         } else {
             $date = AnewtDateTime::now();
         }
         $head->append_child(ax_meta_name_content('DC.date', AnewtDateTime::date($date)));
     }
     /* Powered by Anewt! */
     $generator = $this->_get('generator');
     if (!is_null($generator)) {
         assert('is_string($generator);');
         $head->append_child(ax_meta_name_content('generator', $generator));
     }
     /* Robots */
     $robots = $this->_get('robots');
     if (!is_null($robots)) {
         assert('is_string($robots);');
         $head->append_child(ax_meta(array('name' => 'robots', 'content' => $robots)));
     }
     /* Links (including stylesheets) and JavaScripts */
     $head->append_children($this->_links);
     $head->append_children($this->_javascripts);
     /* Favicon */
     $favicon = $this->_get('favicon');
     if (!is_null($favicon)) {
         assert('is_string($favicon);');
         $head->append_child(ax_link_favicon($favicon));
     }
     /* Body content */
     if ($this->_get('blocks')) {
         /* This is a page using div blocks */
         if ($this->_content && $this->_content->has_child_nodes()) {
             throw new AnewtException('Pages using blocks should not have content outside blocks.');
         }
         /* The buffer holding all content is either a wrapper div or an
          * invisible fragment (both XML nodes, so the API is the same). */
         if ($this->_get('use-wrapper-div')) {
             $buffer = ax_div_id(null, $this->_get('wrapper-div-id'));
         } else {
             $buffer = ax_fragment();
         }
         /* Add the content */
         foreach ($this->_get('blocks') as $block_name) {
             $block = $this->get_block($block_name);
             $buffer->append_child(ax_div_id($block, $block_name));
             unset($block);
             unset($div);
         }
         $body->append_child($buffer);
     } else {
         /* This page has no blocks, so we use the nodes in _content instead
          * (if any) */
         if ($this->_blocks) {
             throw new AnewtException('Pages not using blocks should not have content in blocks');
         }
         if ($this->_content) {
             $body->append_child($this->_content);
         }
     }
     /* Assemble the top level elements */
     $document = new AnewtXMLDomDocument();
     $document->set_document_type($this->_get('document-type'));
     $document->set_content_type($this->_get('content-type'));
     $document->set_encoding($this->_get('encoding'));
     $html = new AnewtXMLDomElement('html', array('xmlns' => 'http://www.w3.org/1999/xhtml', 'xml:lang' => $this->_get('language'), 'lang' => $this->_get('language')));
     $html->append_child($head);
     $html->append_child($body);
     $document->append_child($html);
     return $document;
 }
Ejemplo n.º 2
0
 /**
  * Dispatch an URL to the correct handlers. See the documentation on
  * URLDispatcher::dispatch() for more information on the parameters.
  *
  * \param $url
  *   The url to dispatch (optional, defaults to null).
  *
  * \see URLDispatcher::dispatch
  */
 function dispatch($url = null)
 {
     if (is_null($url)) {
         $url = AnewtRequest::relative_url();
     }
     assert('is_string($url)');
     /* Get the default settings */
     $module_name = $this->_getdefault('default-module', null);
     $class_name = $this->_get('default-class');
     $skip_path_components = 0;
     /* Iterate over the mappings and override the default if the mapping matches */
     $test_url = str_strip_prefix($url, '/');
     foreach ($this->prefix_to_dispatcher_mapping as $prefix => $mapping) {
         /* Try to match the prefix. Add a trailing slash, otherwise the url
          * /newsxyz would also match the /news mapping, which is not
          * intended behaviour. */
         $test_prefix = $prefix . '/';
         if (str_has_prefix($test_url, $test_prefix)) {
             /* The prefix matches the url */
             list($module_name, $class_name) = $mapping;
             $skip_path_components = count(explode('/', $prefix));
             break;
         }
     }
     /* Load module (if supplied) */
     if (!is_null($module_name)) {
         $this->load_module($module_name);
     }
     /* Create and invoke dispatcher */
     $dispatcher =& new $class_name();
     $dispatcher->dispatch($url, $skip_path_components);
 }
Ejemplo n.º 3
0
        $ctx1->draw_filled_rectangle_size(3, 3, 4, 3);
        $ctx1->set('color', $img->color_from_string('#969'));
        $ctx1->draw_rectangle(3, 3, 6, 7);
        $ctx1->draw_filled_rectangle_size(15, 2, 2, 2);
        $ctx1->draw_filled_rectangle_size(15, 2, -1, -1);
        $col = $img->color_from_string('#36c');
        $ctx1->set('color', $col);
        $ctx1->draw_filled_rectangle(20, 2, 18, 3);
        assert('$ctx1->color_at(19, 2) == $col');
        /* Blow up so we can count the pixels in the result */
        $img->resize_relative(10, false);
        $img->flush_png();
        $img->destroy();
    }
}
$test = AnewtRequest::get_string('test');
if (is_null($test)) {
    /* Show test chooser */
    anewt_include('page');
    $p = new AnewtPage();
    $p->set('title', 'Choose a test');
    $p->append(ax_h1('Choose a test'));
    foreach (get_class_methods('AnewtImageTestCases') as $name) {
        $url = AnewtURL::build(AnewtRequest::relative_url(), array('test' => $name));
        $p->append(ax_p(ax_a_href(sprintf('Test: %s', $name), $url)));
    }
    $p->flush();
} else {
    /* Invoke test function */
    AnewtImageTestCases::$test();
}
Ejemplo n.º 4
0
 /**
  * Build an XML document for this channel
  */
 private function build_document()
 {
     /* Build the channel and description properties*/
     $channel = new AnewtXMLDomElement('channel');
     foreach ($this->properties as $property => $property_spec) {
         list($tagname, $status, $type) = $property_spec;
         $element = AnewtRssChannel::_build_rss_element($this, $property, $tagname, $status, $type);
         if (is_null($element)) {
             continue;
         }
         $channel->append_child($element);
         unset($element);
     }
     /* Always add 'docs' element */
     $d = new AnewtXMLDomElement('docs');
     $d->append_child(new AnewtXMLDomText('http://www.rssboard.org/rss-specification'));
     $channel->append_child($d);
     /* Add an atom:link element. Only call the canonical_url() method if no
      * explicit url was provided. */
     $url = $this->_get('url');
     if (is_null($url)) {
         $url = AnewtRequest::canonical_url();
     }
     $channel->append_child(new AnewtXMLDomElement('atom:link', array('href' => $url, 'rel' => 'self', 'type' => $this->_get('content-type'))));
     /* Loop over items in the channel and append those */
     foreach ($this->items as $item) {
         $channel->append_child($item->_build_element());
     }
     /* Final output */
     $document = new AnewtXMLDomDocument();
     $document->set_content_type($this->_get('content-type'));
     $document->set_encoding($this->_get('encoding'));
     $rss = new AnewtXMLDomElement('rss', array('version' => '2.0', 'xmlns:atom' => 'http://www.w3.org/2005/Atom'));
     $rss->append_child($channel);
     $document->append_child($rss);
     return $document;
 }
Ejemplo n.º 5
0
<?php

require_once dirname(__FILE__) . '/../anewt.lib.php';
anewt_include('xml/dom');
$doc = new AnewtXMLDomDocument();
if (AnewtRequest::get_bool('debug')) {
    $doc->set_content_type('text/plain');
}
$root = $doc->create_element('Toplevel', array('foo' => 'bar', 'bar' => '&baz'));
$root->set_attribute('foo', null);
// remove attribute
$doc->append_child($root);
$element1 = $root->append_child($doc->create_element('SomeElement'));
$element1->append_child($doc->create_text_node('Some & text.'));
$element1->append_child($doc->create_raw_node(' Some <b>previously marked up</b> text.'));
$element1->append_child_raw(' Some more text with <b>markup</b>.');
$element1->append_child($doc->create_comment('This is a comment -- containing << strange characters >>.'));
$element1->append_child($doc->create_text_node(' Even more text.'));
$element2 = $root->append_child($doc->create_element('SomeElement'));
$frag = $doc->create_document_fragment();
$frag->append_child($doc->create_text_node('AAA.'));
$frag->append_child($doc->create_text_node('BBB.'));
$frag->append_child($doc->create_text_node('CCC.'));
$element2->append_child($frag);
$sub1 = $element2->append_child($doc->create_element('Sub1'));
$sub1->append_child($doc->create_element('Sub2'));
$sub2 = $sub1->append_child($doc->create_element('Sub2'));
$sub3 = $sub2->append_child($doc->create_element('Sub3'));
$sub3->append_child($doc->create_text_node('First content in sub 3'));
$sub4 = $sub3->append_child($doc->create_element('Sub4'));
$sub4->append_child($doc->create_text_node('Contents of sub 4'));
Ejemplo n.º 6
0
 /**
  * (Really) dispatch an URL to the correct handlers.
  *
  * This method does the actual magic, such as URL parsing, matching and
  * command invocation. You can optionally provide a custom URL and tell the
  * dispatcher that some parts of the URL should be skipped when handling
  * this request.
  *
  * \param $url
  * \param $prefix
  * \see AnewtURLDispatcher::dispatch
  */
 private function real_dispatch($url = null, $prefix = null)
 {
     /* Use the current URL if no explicit url was given */
     if (is_null($url)) {
         $url = AnewtRequest::relative_url();
     }
     /* Figure out the right base location if no prefix was given. If the URL
      * starts with the PHP script name, we assume no htaccess file has been
      * setup to beautify the website URLs. In this case the relevant parts
      * of the URL are added after the PHP script name. Example URL of such
      * a setup is http://.../dispatch.php/a/b/c/. Otherwise, it is quite
      * likely a htaccess file is used to point all requests to a script that
      * invokes the dispatcher. We assume this script is placed in the
      * toplevel directory, so we use that directory as the prefix. */
     if (is_null($prefix)) {
         if (str_has_prefix($url, $_SERVER['SCRIPT_NAME'])) {
             $prefix = $_SERVER['SCRIPT_NAME'];
         } else {
             $prefix = dirname($_SERVER['SCRIPT_NAME']);
         }
     }
     assert('is_string($url)');
     assert('is_string($prefix)');
     /* Strip off the GET parameters from the URL */
     $get_params = '';
     $question_mark_pos = strpos($url, '?');
     if ($question_mark_pos !== false) {
         $get_params = substr($url, $question_mark_pos);
         $url = substr($url, 0, $question_mark_pos);
     }
     /* Redirect GET requests when trailing slash is required but missing */
     if (!str_has_suffix($url, '/') && $this->force_trailing_slash && AnewtRequest::is_get() && !preg_match('#^.*\\.[^\\/]*$#', $url)) {
         redirect(sprintf('%s/%s', $url, $get_params), HTTP_STATUS_MOVED_PERMANENTLY);
     }
     /* Strip off prefix and slashes */
     $this->request_url_full = $url;
     $url = str_strip_prefix($url, $prefix);
     $url = str_strip_prefix($url, '/');
     $url = str_strip_suffix($url, '/');
     $this->request_url = $url;
     /* Try to find a matching route and extract the parameters */
     $found_route = false;
     $command = null;
     $parameters = array();
     $url_parts = strlen($url) > 0 ? explode('/', $url) : array();
     foreach ($this->routes as $route) {
         $route_type = array_shift($route);
         $route_command = array_shift($route);
         $route_parameters = array();
         /* Type I: Routes using regular expression */
         if ($route_type == ANEWT_URL_DISPATCHER_ROUTE_TYPE_REGEX) {
             list($pattern) = $route;
             /* Try both with and without trailing slash */
             if (preg_match($pattern, $url, $route_parameters) || preg_match($pattern, sprintf('%s/', $url), $route_parameters)) {
                 /* We don't care about $parameters[0] (it contains the full match) */
                 array_shift($route_parameters);
                 $route_parameters = array_map('urldecode', $route_parameters);
                 $command = $route_command;
                 $parameters = $route_parameters;
                 $found_route = true;
                 break;
             }
         } elseif ($route_type == ANEWT_URL_DISPATCHER_ROUTE_TYPE_URL_PARTS) {
             list($route_url, $additional_constraints) = $route;
             /* Route URL can be a string or an array */
             if (is_string($route_url)) {
                 $route_url = str_strip_prefix($route_url, '/');
                 $route_url = str_strip_suffix($route_url, '/');
                 $route_url_parts = strlen($route_url) > 0 ? explode('/', $route_url) : array();
             } elseif (is_numeric_array($route_url)) {
                 $route_url_parts = $route_url;
             } else {
                 throw new AnewtException('Invalid url route: %s', $route_url);
             }
             /* Match the URL parts against the route URL parts */
             if (count($url_parts) != count($route_url_parts)) {
                 continue;
             }
             $constraints = array_merge($this->url_part_constraints, $additional_constraints);
             for ($i = 0; $i < count($url_parts); $i++) {
                 /* If the URL starts with a ':' character it is
                  * a parameter... */
                 if ($route_url_parts[$i][0] === ':') {
                     $parameter_name = substr($route_url_parts[$i], 1);
                     $parameter_value = $url_parts[$i];
                     /* If there is a constraint for this parameter, the
                      * value must match the constraint. If not, this route
                      * cannot be used. */
                     if (array_key_exists($parameter_name, $constraints)) {
                         $pattern = $constraints[$parameter_name];
                         if (!preg_match($pattern, $parameter_value)) {
                             continue 2;
                         }
                     }
                     $route_parameters[$parameter_name] = urldecode($parameter_value);
                 } elseif ($url_parts[$i] !== $route_url_parts[$i]) {
                     continue 2;
                 }
             }
             /* If this code is reached, we found a matching route with all
              * the constraints on the URL parts satisfied. */
             $command = $route_command;
             $parameters = $route_parameters;
             $found_route = true;
             break;
         } else {
             assert('false; // not reached');
         }
     }
     /* If no route matches, try an automatic route. Only the first URL part
      * is considered for this. */
     if (!$found_route && $this->use_automatic_commands) {
         $url_parts = explode('/', $url, 2);
         if ($url_parts) {
             $command = array($this, sprintf('command_%s', $url_parts[0]));
             list($found_route, $error_message_to_ignore) = $this->is_valid_command($command);
         }
     }
     /* As a last resort try the default handler, if one was set. */
     $default_command = $this->default_command;
     if (!$found_route && !is_null($default_command)) {
         $command = $default_command;
         $command = $this->validate_command($command);
         $found_route = true;
     }
     /* If we still don't have a command, we give up. Too bad... not found */
     if (!$found_route) {
         throw new AnewtHTTPException(HTTP_STATUS_NOT_FOUND);
     }
     /* Check the command for validity. In most cases we already know the
      * command exists since that is already checked in the add_route_*()
      * methods or in the code above, except for lazily loaded commands, so
      * we try to load them and check for validity afterwards. */
     if (is_array($command) && is_string($command[0])) {
         $this->include_command_class($command[0]);
         $command = $this->validate_command($command);
     }
     /* Finally... run the command and the pre and post command hooks. */
     $this->pre_command($parameters);
     call_user_func($command, $parameters);
     $this->post_command($parameters);
 }
Ejemplo n.º 7
0
<?php

require_once dirname(__FILE__) . '/../anewt.lib.php';
define('ANEWT_TEXTILE_DEVELOPMENT', 1);
anewt_include('page');
$p = new AnewtPage();
$p->set('title', 'Textile formatting test');
if (AnewtRequest::get_bool('debug')) {
    header('Content-type: text/plain');
    $p->set('content_type', 'text/plain');
} else {
    list($base_url, $params) = AnewtUrl::parse(AnewtRequest::url());
    $params['debug'] = '1';
    $debug_url = AnewtUrl::build(array($base_url), $params);
    $p->append(ax_p(ax_a_href('(Page source for debugging)', $debug_url)));
}
anewt_include('textformatting');
anewt_include('textile');
$text = file_get_contents(dirname(__FILE__) . '/sample-text.txt');
$formatted_text = TextFormatter::format($text, 'textile');
$p->append(ax_raw($formatted_text));
$p->flush();
Ejemplo n.º 8
0
 /**
  * Check if the request is a HTTP POST request.
  *
  * \return
  *   True if the request is a POST request, false otherwise.
  * 
  * \see AnewtRequest::is_get
  * \see AnewtRequest::method
  */
 public static function is_post()
 {
     return AnewtRequest::method() === 'POST';
 }
Ejemplo n.º 9
0
 function TestForm()
 {
     parent::__construct();
     /* General form setup and test some properties */
     $this->setup('test-name', ANEWT_FORM_METHOD_POST, AnewtRequest::url(true));
     $this->set('error', 'This is a general form error.');
     $this->set('description', 'This is the form\'s description.');
     /* Test text entry controls */
     $text1 = new AnewtFormControlText('text1');
     $text1->set('label', 'Text field:');
     $text1->set('secondary-label', 'Secondary label');
     $text1->set('value', 'Short string with & spécial chars');
     $text1->set('help', 'You can type some text here.');
     $text1->set('description', 'Type at least 3 characters here.');
     $text1->add_validator(new AnewtValidatorLength(3, null), 'Too short (minimum number of characters is 3).');
     $this->add_control($text1);
     $text2 = new AnewtFormControlTextMultiline('text2');
     $this->add_control($text2);
     /* Set properties after adding to test references */
     $text2->set('description', 'You may enter some longer text here.');
     $text2->set('value', "Some readonly text\nwith multiple\nlines\nand some spécial characters\nlike &, ', < and >.");
     $text2->set('label', 'Multiline textfield:');
     $text2->set('readonly', true);
     $text3 = new AnewtFormControlTextMultiline('text3');
     $text3->set('value', "A larger control with a\nbit more space (4 lines!)\nto type some text, if only\nit wasn't disabled…");
     $text3->set('label', 'Larger multiline:');
     $text3->set('secondary-label', 'Secondary label');
     $text3->set('rows', 4);
     $text3->set('columns', 40);
     $text3->set('disabled', true);
     $text3->set('error', 'This is an error description.');
     $this->add_control($text3);
     $text4 = new AnewtFormControlTextMultiline('text4');
     $text4->set('value', 'Okay, this is multiline text you can edit yourself…');
     $text4->set('label', 'Another multiline:');
     $text4->set('rows', 2);
     $text4->set('columns', 50);
     $this->add_control($text4);
     /* Test adding custom nodes to the form */
     $this->add_node(ax_p('This is a normal paragraph inside the form.'));
     $this->add_node(ax_raw('<p><strong>Formatted text</strong> can be here as well.</p>'));
     /* Some more text controls with various property combinations */
     $text5 = new AnewtFormControlText('text5');
     $text5->set('label', 'A number between 1 and 10:');
     $text5->set('description', 'This should be 8 by default. You should not be able to type more than 2 characters into this field.');
     $text5->set('value', '7');
     // Set again to 8 later using set_control_value!
     $text5->set('help', 'Enter a number between 1 and 10 inclusive, please.');
     $text5->set('size', 2);
     $text5->set('maxlength', 2);
     $text5->add_validator(new AnewtValidatorInteger(1, 10), 'This number is not valid.');
     $this->add_control($text5);
     $text6 = new AnewtFormControlPassword('text6');
     $text6->set('label', 'Password:'******'description', 'This control can hold only 8 characters. This field is not required and will give a NULL value when left empty.');
     $text6->set('size', 8);
     $text6->set('maxlength', 8);
     $text6->set('value', 'not-shown');
     $text6->set('required', false);
     $text6->set('help', 'Enter a password, please.');
     $this->add_control($text6);
     $text7 = new AnewtFormControlPassword('text7');
     $text7->set('label', 'Echoed password:'******'description', 'This password is echoed back when the form is submitted.');
     $text7->set('value', 'shown');
     $text7->set('show-value', true);
     $this->add_control($text7);
     $text8 = new AnewtFormControlText('text8');
     $text8->set('label', 'Email:');
     $text8->set('value', '*****@*****.**');
     $text8->add_validator(new AnewtValidatorEmail(), 'This does not look like an email address.');
     $this->add_control($text8);
     /* Check box */
     $check1 = new AnewtFormControlCheckbox('check1');
     $check1->set('label', 'Checkbox:');
     $check1->set('secondary-label', 'Check me');
     $check1->set('help', 'Feel free to check me!');
     $this->add_control($check1);
     $check2 = new AnewtFormControlCheckbox('check2');
     $check2->set('label', 'Disabled checkbox:');
     $check2->set('disabled', true);
     $this->add_control($check2);
     $check3 = new AnewtFormControlCheckbox('check3');
     $check3->set('label', 'Another checkbox:');
     $check3->set('secondary-label', 'Check me too');
     $check3->set('value', true);
     $this->add_control($check3);
     /* Choice control, multiple same values */
     $choice_same_values = new AnewtFormControlChoice('choice-same-values');
     $choice_same_values->set('label', 'Choose:');
     $choice_same_values->set('description', 'Multiple options have the same value. Only the first should be selected.');
     $choice_same_values->add_option_value_label('same-value', 'Option 1 (same value as option 2');
     $choice_same_values->add_option_value_label('same-value', 'Option 2 (same value as option 1');
     $choice_same_values->add_option_value_label('other-value', 'Option 3');
     $choice_same_values->set('value', 'same-value');
     $this->add_control($choice_same_values);
     /* Choice control, single select */
     $single_choice_fieldset = new AnewtFormFieldset('single-choice');
     $single_choice_fieldset->set('label', 'Single select');
     $single_choice_fieldset->set('description', 'You can select a single value here. By default, the second option is selected in both cases, and the first option is disabled.');
     $choice1 = new AnewtFormControlChoice('choice1');
     $choice1->set('label', 'Make a choice:');
     $option = new AnewtFormOption('first', 'First option');
     $option->set('disabled', true);
     $choice1->add_option($option);
     unset($option);
     $choice1->add_option_value_label('second', 'Second option');
     $choice1->add_option_value_label('third', 'Third option');
     $choice1->add_option_value_label('fourth', 'Fourth option');
     $choice1->set('value', 'second');
     $single_choice_fieldset->add_control($choice1);
     $choice2 = new AnewtFormControlChoice('choice2');
     $choice2->set('threshold', 3);
     $choice2->set('label', 'Make a choice:');
     $option = new AnewtFormOption('first', 'First option');
     $option->set('disabled', true);
     $choice2->add_option($option);
     unset($option);
     $choice2->add_option_value_label('second', 'Second option');
     $choice2->add_option_value_label('third', 'Third option');
     $choice2->add_option_value_label('fourth', 'Fourth option');
     $choice2->set('value', 'second');
     $single_choice_fieldset->add_control($choice2);
     $choice3 = new AnewtFormControlChoice('choice3');
     $choice3->set('threshold', 3);
     $choice3->set('size', 4);
     $choice3->set('label', 'Make a choice:');
     $option = new AnewtFormOption('first', 'First option');
     $option->set('disabled', true);
     $choice3->add_option($option);
     unset($option);
     $choice3->add_option_value_label('second', 'Second option');
     $choice3->add_option_value_label('third', 'Third option');
     $choice3->add_option_value_label('fourth', 'Fourth option');
     $choice3->set('value', 'second');
     $single_choice_fieldset->add_control($choice3);
     $single_choice_fieldset->add_node(ax_p('This is a normal paragraph inside a fieldset.'));
     $this->add_fieldset($single_choice_fieldset);
     /* Choice control, multiple select */
     $multiple_choice_fieldset = new AnewtFormFieldset('multiple-choice');
     $multiple_choice_fieldset->set('label', 'Multiple select');
     $multiple_choice_fieldset->set('description', 'You can select multiple values here. By default, the second and third options are selected in both cases, and the first option is disabled.');
     $choice4 = new AnewtFormControlChoice('choice4');
     $choice4->set('multiple', true);
     $choice4->set('label', 'Make a choice:');
     $option = new AnewtFormOption('first', 'First option');
     $option->set('disabled', true);
     $choice4->add_option($option);
     unset($option);
     $choice4->add_option_value_label('second', 'Second option');
     $choice4->add_option_value_label('third', 'Third option');
     $choice4->add_option_value_label('fourth', 'Fourth option');
     /* Value is deliberately set twice to test unsetting of old values */
     $choice4->set('value', array('first', 'second'));
     $choice4->set('value', array('second', 'third'));
     $multiple_choice_fieldset->add_control($choice4);
     $choice5 = new AnewtFormControlChoice('choice5');
     $choice5->set('multiple', true);
     $choice5->set('threshold', 3);
     $choice5->set('label', 'Make a choice:');
     $option = new AnewtFormOption('first', 'First option');
     $option->set('disabled', true);
     $choice5->add_option($option);
     unset($option);
     $choice5->add_option_value_label('second', 'Second option');
     $choice5->add_option_value_label('third', 'Third option');
     $choice5->add_option_value_label('fourth', 'Fourth option');
     $choice5->set('value', array('second', 'third'));
     $multiple_choice_fieldset->add_control($choice5);
     $this->add_fieldset($multiple_choice_fieldset);
     /* Choice control, with option groups */
     $option_groups_fieldset = new AnewtFormFieldset('option-groups-fieldset');
     $option_groups_fieldset->set('label', 'Option groups');
     $option_groups_fieldset->set('description', 'Single and multiple select controls with option groups.');
     $choice6 = new AnewtFormControlChoice('choice6');
     $og = new AnewtFormOptionGroup('First group');
     $og->add_option_value_label('1a', 'A');
     $og->add_option_value_label('1b', 'B');
     $choice6->add_option_group($og);
     $og = new AnewtFormOptionGroup('Second group');
     $og->add_option_value_label('2a', 'A');
     $og->add_option_value_label('2b', 'B');
     $choice6->add_option_group($og);
     $choice6->set('label', 'Make a choice:');
     $choice6->set('description', 'Choice 2a selected by default.');
     $choice6->set('value', '2a');
     $option_groups_fieldset->add_control($choice6);
     $choice7 = new AnewtFormControlChoice('choice7');
     $choice7->add_option_value_label('o1', 'First option (not in a group)');
     $option_group_1 = new AnewtFormOptionGroup('First group');
     $option = new AnewtFormOption('g1o1', 'First disabled suboption in first group');
     $option->set('disabled', true);
     $option_group_1->add_option($option);
     $option_group_1->add_option_value_label('g1o2', 'Second suboption in first group');
     $option_group_1->add_option_value_label('g1o3', 'Third suboption in first group');
     $choice7->add_option_group($option_group_1);
     $option_group_2 = new AnewtFormOptionGroup('Second group');
     $option_group_2->add_option_value_label('g2o1', 'First suboption in second group');
     $option_group_2->add_option_value_label('g2o2', 'Second suboption in second group');
     $choice7->add_option_group($option_group_2);
     $choice7->add_option_value_label('o3', 'Third option (not in a group)');
     $option_group_3 = new AnewtFormOptionGroup('Third group (completely disabled)');
     $option_group_3->set('disabled', true);
     $option_group_3->add_option_value_label('g3o1', 'Only suboption in third group');
     $choice7->add_option_group($option_group_3);
     $choice7->add_option_value_label('04', 'Fourth option, not in a group');
     $choice7->set('label', 'Make multiple choices:');
     $choice7->set('description', 'Options 1 and group 2 option 1 are selected by default.');
     $choice7->set('multiple', true);
     $choice7->set('value', array('g2o1', 'o1'));
     $option_groups_fieldset->add_control($choice7);
     $choice8 = new AnewtFormControlChoice('choice8');
     $choice8->set('label', 'No selection here:');
     $choice8->set('description', 'This choice control only contains disabled options and disabled or empty option groups. The first one should be forcibly selected. Yes, this is a really nasty edge case.');
     $og = new AnewtFormOptionGroup('Empty group 1');
     $choice8->add_option_group($og);
     $og = new AnewtFormOptionGroup('Disabled group 2');
     $og->set('disabled', true);
     $og->add_option_value_label('none', '1a');
     $og->add_option_value_label('none', '1b');
     $choice8->add_option_group($og);
     $option = new AnewtFormOption('none', 'Disabled option');
     $option->set('disabled', true);
     $choice8->add_option($option);
     $choice8->set('value', 'too bad this is totally invalid');
     $option_groups_fieldset->add_control($choice8);
     $this->add_fieldset($option_groups_fieldset);
     /* Hidden controls */
     $hidden1 = new AnewtFormControlHidden('hidden1');
     $hidden1->set('value', 'sekr1t!');
     $this->add_control($hidden1);
     $this->add_hidden_control('hidden2', 'another && śèkŕ1t');
     /* Buttons */
     $button_fieldset = new AnewtFormFieldset('buttons');
     $button_fieldset->set('label', 'Button fieldset');
     $submit = new AnewtFormControlButtonSubmit('submit');
     $submit->set('label', 'Submit form');
     $submit->set('help', 'Press this button to submit this form');
     $button_fieldset->add_control($submit);
     $submit2 = new AnewtFormControlButtonSubmit('submit2');
     $submit2->set('label', 'Disabled submit button');
     $submit2->set('disabled', true);
     $button_fieldset->add_control($submit2);
     $submit = new AnewtFormControlButtonSubmit('submit3');
     $submit->set('label', 'Another submit button');
     $button_fieldset->add_control($submit);
     $reset = new AnewtFormControlButtonReset('reset');
     $reset->set('label', 'Reset to default values');
     $reset->set('help', 'Reset the values of the form fields to their original values');
     $button_fieldset->add_control($reset);
     $extra = new AnewtFormControlButton('extra-button');
     $extra->set('label', 'Extra button that does not do anything');
     $button_fieldset->add_control($extra);
     $this->add_fieldset($button_fieldset);
 }
Ejemplo n.º 10
0
 /**
  * Fill form automatically from query data. If the form uses the GET method
  * the data comes from <code>$_GET</code>. If the form uses the POST method
  * the data comes from <code>$_POST</code>.
  *
  * The return value (see also fill() for an explanation) can be used to
  * detect incomplete \c $_GET or \c $_POST values. This may happen when
  * handling different form flavours with different controls using a single
  * form, e.g. a simple and advanced search form pointing to the same page in
  * which an instance of the advanced flavour of the form handles the
  * submitted data. It may also happen when users are messing with the
  * submitted data. You may then decide to process() the form only if all
  * values were provided. Example:
  * <code>if ($form->autofill() && $form->process()) { ...} else { ...}</code>
  *
  * \return
  *   True if this fill could be automatically filled from \c $_GET or \c
  *   $_POST, false if this was not the case.
  *
  * \see AnewtForm::fill()
  */
 function autofill()
 {
     $form_method = $this->_get('method');
     if (AnewtRequest::is_get() && $form_method == ANEWT_FORM_METHOD_GET) {
         return $this->fill($_GET);
     } elseif (AnewtRequest::is_post() && $form_method == ANEWT_FORM_METHOD_POST) {
         return $this->fill($_POST);
     }
     return false;
 }