示例#1
0
/**
 * Create a new fragment node.
 *
 * \param $children
 *   The content of the fragment node.
 *
 * \see AnewtXHTMLFragment
 */
function ax_fragment($children = null)
{
    $node = new AnewtXHTMLFragment();
    $children = func_get_args();
    if ($children) {
        $node->append_children($children);
    }
    return $node;
}
示例#2
0
 /** \{
  * \name Rendering methods
  */
 function build_widget()
 {
     $name = $this->get('name');
     $id = $this->get('id');
     $multiple = $this->_get('multiple');
     $threshold = $this->_get('threshold');
     $num_options = count($this->_options);
     /* Decide how to render. The values 0 and -1 are special, and if there
      * are no options, always render a single element (perhaps JavaScript is
      * used to populate the control). */
     if ($num_options === 0) {
         $render_many_elements = false;
     } elseif ($threshold === -1) {
         $render_many_elements = true;
     } elseif ($threshold === 0) {
         $render_many_elements = false;
     } elseif ($num_options <= $threshold) {
         $render_many_elements = true;
     } else {
         $render_many_elements = false;
     }
     if ($render_many_elements) {
         /* Render listing of checkboxes or radio buttons */
         $out = new AnewtXHTMLFragment();
         foreach ($this->_options as $option) {
             if ($multiple) {
                 $child = $option->_build_checkbox();
             } else {
                 $child = $option->_build_radiobutton();
             }
             $out->append_child($child);
         }
         /* Set the id property on the first child (this is the radio button
          * or checkbox) of the first option. */
         if ($this->_options) {
             $out->child_nodes[0]->child_nodes[0]->set_attribute('id', $id);
         }
     } else {
         /* Render single select element */
         if ($multiple) {
             $out = new AnewtXHTMLSelect(null, array('name' => sprintf('%s[]', $name), 'multiple' => 'multiple'));
         } else {
             $out = new AnewtXHTMLSelect(null, array('name' => $name));
         }
         /* Set the id property. */
         $out->set_attribute('id', $id);
         /* Set the height if provided. If the 'multiple' property is not
          * set, this forces a drop down select box to be displayed as
          * a multiline selection list. */
         $size = $this->_get('size');
         if (!is_null($size)) {
             assert('is_int($size);');
             $out->set_attribute('size', (string) $size);
         }
         if ($this->_get('disabled')) {
             $out->set_attribute('disabled', 'disabled');
         }
         foreach ($this->_options as $option_or_option_group) {
             $out->append_child($option_or_option_group->_build_option());
         }
     }
     return $out;
 }
示例#3
0
 /**
  * Render the grid into a valid XHTML snippet.
  *
  * \return
  *   XHTML string that can be used directly for output
  */
 function render()
 {
     /* Create the table */
     $table = new AnewtXHTMLTable();
     $table->set_class('grid');
     $extra_class = $this->_get('class');
     if (!is_null($extra_class)) {
         $table->add_class($extra_class);
     }
     /* Which columns do we need to process? */
     $visible_columns = $this->visible_columns();
     $number_of_expanded_visible_columns = $this->_number_of_expanded_visible_columns();
     /* Render colgroup and col elements */
     foreach ($visible_columns as $column) {
         $column_group = new AnewtXHTMLTableColumnGroup();
         $column_group->set_class($column->id);
         $cell_renderers = $column->cell_renderers();
         foreach ($cell_renderers as $cell_renderer) {
             $column_group->append_child(new AnewtXHTMLTableColumn(null, array('class' => $cell_renderer->id)));
         }
         $table->append_child($column_group);
         unset($column_group);
     }
     /* Render the header */
     if ($this->_get('show-header')) {
         $table_head = new AnewtXHTMLTableHead();
         $header_cells = new AnewtXHTMLFragment();
         $sub_header_cells = new AnewtXHTMLFragment();
         /* Find out in advance whether we need to show a subheader (a second
          * row of header cells with cellrenderer titles instead of column
          * titles.) */
         $show_sub_header = false;
         foreach ($visible_columns as $column) {
             foreach ($column->cell_renderers() as $cell_renderer) {
                 $cell_renderer_title = $cell_renderer->_get('title');
                 if (!is_null($cell_renderer_title)) {
                     /* Okay, there is at least one cell renderer with
                      * a non-empty title. */
                     $show_sub_header = true;
                     break 2;
                 }
             }
         }
         foreach ($visible_columns as $column) {
             $column_title = $column->_get('title');
             $header_cell = new AnewtXHTMLTableHeaderCell($column_title);
             /* Allow CSS styling and highlighting on a column level */
             $header_cell->set_class(sprintf('column-%s', $column->id));
             if ($column->_get('highlight')) {
                 $header_cell->add_class('highlight');
             }
             $cell_renderers = $column->cell_renderers();
             $n_cell_renderers = count($cell_renderers);
             /* If we need to show a subheader row, we loop over the cell
              * renderers and use their titles, if set. If no subheader is
              * shown anyway, we jus skip over this part. */
             $column_has_sub_header_titles = false;
             if ($show_sub_header) {
                 $sub_header_cells_for_column = new AnewtXHTMLFragment();
                 foreach ($cell_renderers as $cell_renderer) {
                     $cell_title = $cell_renderer->_get('title');
                     $sub_header_cell = new AnewtXHTMLTableHeaderCell($cell_title);
                     $sub_header_cell->set_class(sprintf('column-%s cell-%s', $column->id, $cell_renderer->id));
                     if ($column->_get('highlight')) {
                         $sub_header_cell->add_class('highlight');
                     }
                     $sub_header_cells_for_column->append_child($sub_header_cell);
                     if (strlen($cell_title) > 0) {
                         $column_has_sub_header_titles = true;
                     }
                     unset($sub_header_cell);
                 }
                 /* Only add the sub header cells if there are any. Otherwise
                  * we set a row span to make the header span two rows, since
                  * other rows use the second row to display the sub header
                  * cells. */
                 if ($column_has_sub_header_titles) {
                     $sub_header_cells->append_child($sub_header_cells_for_column);
                 } else {
                     $header_cell->set_attribute('rowspan', (string) 2);
                 }
             }
             /* We need to set a colspan if the column contains multiple cell
              * renderers, because each cell renderer results in a td element
              * in the final output, and the header should span all of the
              * child cells. */
             if ($n_cell_renderers >= 2) {
                 $header_cell->set_attribute('colspan', (string) $n_cell_renderers);
             }
             $column_title = $column->_getdefault('title', '');
             $header_cells->append_child($header_cell);
             unset($header_cell);
         }
         /* Now add the rows to the header. FIXME: should create
          * AnewtXHTMLTableHead early on instead. */
         $table_head->append_child(new AnewtXHTMLTableRow($header_cells));
         if ($show_sub_header) {
             $table_head->append_child(new AnewtXHTMLTableRow($sub_header_cells));
         }
         $table->append_child($table_head);
     }
     /* Grid summary text in table foot */
     if ($this->_get('show-summary')) {
         $table_foot = new AnewtXHTMLTableFoot();
         $table_foot->append_child(new AnewtXHTMLTableRow(new AnewtXHTMLTableCell($this->_get('summary-text'), array('class' => 'summary', 'colspan' => (string) $number_of_expanded_visible_columns))));
         $table->append_child($table_foot);
     }
     /* Make alternating row colors possible (zebra pattern) */
     $generator = $this->get('generator');
     if (is_null($generator)) {
         anewt_include('generator');
         $generator = new AnewtGenerator('odd', 'even');
     }
     assert('$generator instanceof AnewtGenerator');
     /* Render the row data itself */
     $table_body = new AnewtXHTMLTableBody();
     foreach ($this->_rows as $data) {
         /* This array will hold the rendered cells */
         $cells = array();
         /* Iterate over all visible columns of the Grid */
         foreach (array_keys($visible_columns) as $column_id) {
             $column = $visible_columns[$column_id];
             /* Iterate over the cell renderers */
             $cell_renderers = $column->cell_renderers();
             foreach (array_keys($cell_renderers) as $cell_renderer_key) {
                 $rendered_cell = $cell_renderers[$cell_renderer_key]->render_cell($data);
                 $cells[] = $rendered_cell;
             }
         }
         $row_attr = array('class' => $generator->next());
         $table_body->append_child(new AnewtXHTMLTableRow($cells, $row_attr));
     }
     $table->append_child($table_body);
     return $table;
 }
示例#4
0
$ol->append_child(new AnewtXHTMLListItem('three', ' and last', array('class' => 'last')));
$fragment->append_child($ol);
$fragment->append_child(new AnewtXHTMLHeader3('Definition list'));
$fragment->append_child(new AnewtXHTMLDefinitionList(new AnewtXHTMLDefinitionTerm('foo'), new AnewtXHTMLDefinitionDescription('bar'), new AnewtXHTMLDefinitionTerm('quux'), new AnewtXHTMLDefinitionDescription('baz')), array('class' => 'definitions'));
/* Tables */
$fragment->append_child(new AnewtXHTMLHeader2('Tables'));
$table = new AnewtXHTMLTable();
$table_head = new AnewtXHTMLTableHead(new AnewtXHTMLTableRow(ax_fragment(new AnewtXHTMLTableHeaderCell('Column 1'), new AnewtXHTMLTableHeaderCell('Column 2'))));
$table->append_child($table_head);
$table_body = new AnewtXHTMLTableBody(ax_fragment(new AnewtXHTMLTableRow(ax_fragment(new AnewtXHTMLTableCell('r1c1'), new AnewtXHTMLTableCell('r1c2')), new AnewtXHTMLTableRow(ax_fragment(new AnewtXHTMLTableCell('r2c1'), new AnewtXHTMLTableCell('r2c2'))))));
$table->append_child($table_body);
$fragment->append_child($table);
/* Forms */
$fragment->append_child(new AnewtXHTMLHeader2('Forms'));
$form = new AnewtXHTMLForm(null, array('method' => 'GET'));
$input_fragment = new AnewtXHTMLFragment();
$input_fragment->append_child(new AnewtXHTMLLabel('Label: ', array('for' => 'test')));
$input_fragment->append_child(new AnewtXHTMLInput(null, array('name' => 'test', 'id' => 'test', 'type' => 'text')));
$form->append_child(new AnewtXHTMLParagraph($input_fragment));
$select = new AnewtXHTMLSelect(null, array('name' => 'select'));
$select->append_child(new AnewtXHTMLOption('First', array('value' => 'first')));
$select->append_child(new AnewtXHTMLOption('Second', array('value' => 'second')));
$select->append_child(new AnewtXHTMLOption('Third', array('value' => 'third')));
$form->append_child(new AnewtXHTMLParagraph($select));
$fragment->append_child($form);
$form->append_child(new AnewtXHTMLParagraph(new AnewtXHTMLInput(null, array('type' => 'submit'))));
/* Convenience API */
$r = array();
$r[] = ax_h2('Convenience API');
$r[] = ax_p('Test with some <& special characters.', array('style' => 'color: #ccc;'));
$r[] = ax_p_class(ax_raw('This is <strong>strong</strong>'), 'someclass');