/** * This is where all of the real work is done. * * Special method calls include: * <ul> * <li>append_ATTRIBUTENAME($value) (Same as mr_html_tag::prepend_attribute(ATTRIBUTENAME, $value))</li> * <li>prepend_ATTRIBUTENAME($value) (Same as mr_html_tag::prepend_attribute(ATTRIBUTENAME, $value))</li> * <li>remove_ATTRIBUTENAME() (Same as mr_html_tag::remove_attribute(ATTRIBUTENAME))</li> * <li>remove_ATTRIBUTENAME() (Same as mr_html_tag::get_attribute(ATTRIBUTENAME))</li> * </ul> * * How to use: * <code> * <?php * $tag = new mr_html_tag(); * $link = $tag->a('Click me!'); // This will return a new instance * // mr_html_tag and set the tag='a' * // and contents='Click me!' * $link->href('http://foo.bar'); // This will add an attribute to the a * // tag: href="http://foo.bar" (Same as * // mr_html_tag::add_attribute() * $html = $link->close(); // This will close the tag and return the HTML * * // All of the above calls can be stringed together using * // the fluent interface to get the HTML * $html = $tag->a('Click me!') * ->href('http://foo.bar') * ->close(); * ?> * </code> * * @param string $name The function name called * @param string $arguments The function arguments passed * @return mixed * @throws coding_exception * @example controller/default.php See more examples */ public function __call($name, $arguments) { $parts = explode('_', $name); // Special case, tag not yet defined if (is_null($this->tag)) { // Create a new tag instance and set it up $html = new mr_html_tag(); $html->init($name, $arguments); return $html; // Handle adding of an attribute } else { if (count($parts) == 1) { if (count($arguments) != 1) { throw new coding_exception("Invalid method call mr_html_tag::{$name}() - must pass an argument"); } return $this->add_attribute($name, $arguments[0]); // Handle attribute manipulation and retrieval } else { if (count($parts) == 2) { list($method, $attrname) = $parts; switch ($method) { case 'get': return $this->get_attribute($attrname); case 'append': case 'prepend': if (count($arguments) != 1) { throw new coding_exception("Invalid method call mr_html_tag::{$name}() - must pass an argument"); } $method = "{$method}_attribute"; return $this->{$method}($attrname, $arguments[0]); case 'remove': return $this->remove_attribute($attrname); } } } } throw new coding_exception("Call to non-existent method: mr_html_tag::{$name}()"); }
/** * Render mr_html_table * * @param mr_html_table $table mr_html_table instance * @return string */ protected function render_mr_html_table(mr_html_table $table) { $tag = new mr_html_tag(); $rows = $table->get_rows(); $columns = $table->get_columns(true); // Table setup $htmltable = new html_table(); $htmltable->data = array(); foreach ($table->get_attributes() as $name => $value) { if (property_exists($htmltable, $name)) { $htmltable->{$name} = $value; } else { $htmltable->attributes[$name] = $value; } } // Check if we have any column headings $haveheadings = false; foreach ($columns as $column) { if ($column->has_heading()) { $haveheadings = true; break; } } if ($haveheadings) { $htmltable->head = array(); foreach ($columns as $column) { // Must set sortable to false if table is not sort enabled or if empty $rows if (!$table->get_sortenabled() or empty($rows)) { $column->set_config('sortable', false); } $config = $column->get_config(); // Figure out column sort controls if ($config->sortable) { $icon = ''; $sortstr = get_string('asc'); $sortord = SORT_ASC; if ($table->get_sort() == $column->get_name()) { if ($table->get_order() == SORT_ASC) { $icon = $tag->img()->src($this->output->pix_url('t/down'))->alt(get_string('asc')); $sortstr = get_string('asc'); $sortord = SORT_DESC; } else { $icon = $tag->img()->src($this->output->pix_url('t/up'))->alt(get_string('desc')); } } $url = $table->get_url()->out(false, array('tsort' => $config->name, 'torder' => $sortord)); $heading = get_string('sortby') . ' ' . $config->heading . ' ' . $sortstr; $heading = $config->heading . get_accesshide($heading); $heading = $tag->a($heading)->href($url) . $icon; } else { $heading = $config->heading; } $cell = new html_table_cell($heading); $cell->attributes = array_merge($cell->attributes, $config->attributes); $htmltable->head[] = $cell; } } if (empty($rows)) { $cell = new html_table_cell($table->get_emptymessage()); $cell->colspan = count($htmltable->head); $htmltable->data[] = new html_table_row(array($cell)); } else { $htmltable->data = $this->convert_to_htmlrows($table); } return html_writer::tag('div', html_writer::table($htmltable), array('class' => 'mr_html_table')); }
public function test_nonexistent_method() { $this->expectException('coding_exception'); mr_html_tag::open()->strong()->something_crazy(); }