예제 #1
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;
 }
예제 #2
0
$fragment->append_child(new AnewtXHTMLParagraph(new AnewtXHTMLEmphasis('Emphasis, '), new AnewtXHTMLStrong('strong, '), new AnewtXHTMLDefinition('defitinion, '), new AnewtXHTMLCode('code, '), new AnewtXHTMLSample('sample, '), new AnewtXHTMLKeyboard('keyboard, '), new AnewtXHTMLVariable('var, '), new AnewtXHTMLAbbreviation('abbr, '), 'and ', new AnewtXHTMLAcronym('acronym'), '.'));
$fragment->append_child(new AnewtXHTMLPreformatted('This is some preformatted text.'));
$fragment->append_child(new AnewtXHTMLBlockQuote(new AnewtXHTMLParagraph('This is a long quotation.'), new AnewtXHTMLParagraph('This is second paragraph of the long quotation.')));
$fragment->append_child(new AnewtXHTMLParagraph('This is a small quote: ', new AnewtXHTMLQuote('To be or not to be.')));
$fragment->append_child(new AnewtXHTMLParagraph('This paragraph is to test ', new AnewtXHTMLSubscript('subscripted'), ' and ', new AnewtXHTMLSuperscript('superscripted'), ' text.'));
/* Lists */
$fragment->append_child(new AnewtXHTMLHeader2('Lists'));
$fragment->append_child(new AnewtXHTMLHeader3('Ordered list'));
$ol = new AnewtXHTMLOrderedList(new AnewtXHTMLListItem('one'), new AnewtXHTMLListItem('two'), array('class' => 'foo'));
$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')));