3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php class E_View_Helper { private $form_array_source = array(); /** * Standard contructor * * @param array $a_form_array_source * - controls container, must look like: * $form['form_name1']['label'] * $form['form_name1']['form'] * $form['form_name_2']['label'] * ... */ public function __construct(&$a_form_array_source) { if( !is_array($a_form_array_source) ) return FALSE; $this->form_array_source = &$a_form_array_source; } /** * Prints formatted input for datepicker with labels * * @param string $a_form_name * - name of input * @param string $a_align * - input alignment: right, left or center */ public function input_date_form($a_form_name, $a_align = FALSE) { if( isset($this->form_array_source[$a_form_name]['form']) ) { switch( $a_align ) { case 'right': case 'center': case 'left': $a_align = 'text-' . strtolower($a_align); break; default : $a_align = ''; } ?> <label for="<?php print $a_form_name; ?>" class="<?php print $a_align; ?>"> <?php print $this->form_array_source[$a_form_name]['label'];?> <?php print $this->form_array_source[$a_form_name]['form']; ?> </label> <?php } } /** * Prints formatted group of date range inputs ("from" and "to") * * @param string $a_form_title * - title (label) above all inputs * @param string $a_from_form_name * - "from" input name * @param string $a_to_form_name * - "to" input name */ public function date_range_group($a_form_title, $a_from_form_name, $a_to_form_name) { if( isset($this->form_array_source[$a_from_form_name]['form']) or isset($this->form_array_source[$a_to_form_name]['form']) ) { ?> <div class="datepicker_container"> <?php if ($a_form_title AND isset($this->form_array_source[$a_form_title]['title'])) { ?> <label><?php print $this->form_array_source[$a_form_title]['title'];?></label> <?php } else { ?> <label>&nbsp;</label> <?php } ?> <?php print $this->input_date_form($a_from_form_name, 'right'); ?> <?php print $this->input_date_form($a_to_form_name, 'right'); ?> </div> <?php } } /** * Prints standard input form * * @param string $a_form_name * - name of input * @param string $a_label_class * - CSS class of label above input */ public function input_form($a_form_name, $a_label_class = '') { if( isset($this->form_array_source[$a_form_name]['form']) ) { $mandatory = isset($this->form_array_source[$a_form_name]['label']['mandatory']) ? ' <span class="mandatory">&lowast;</span>' : ''; ?> <label class="<?php print $a_label_class?>" for="<?php print $a_form_name; ?>"> <?php print empty($this->form_array_source[$a_form_name]['label']) ? '&nbsp;' : $this->form_array_source[$a_form_name]['label'].$mandatory;?> </label> <?php print $this->form_array_source[$a_form_name]['form']; ?> <?php } } /** * Prints inline input like checkbox/radio * * @param string $a_form_name * - name of input * @param string $a_label_class * - CSS class of label above input */ public function input_inline($a_form_name, $label_class = 'radio') { if( isset($this->form_array_source[$a_form_name]['form']) ) { $mandatory = isset($this->form_array_source[$a_form_name]['label']['mandatory']) ? ' <span class="mandatory">&lowast;</span>' : ''; ?> <label class="<?php print $label_class?>" for="<?php print $a_form_name; ?>"> <?php print $this->form_array_source[$a_form_name]['form']; ?> <?php print empty($this->form_array_source[$a_form_name]['label']) ? '&nbsp;' : $this->form_array_source[$a_form_name]['label'].$mandatory;?> </label> <?php } } } /** * Provides unified display of index pages. * Base abstract class, to be inherited * * * @author Michal K. * */ abstract class Index_View_Helper extends Printable_Section { // optional custom table printer public $table_printer = NULL; // optional custom search box printer public $search_box_printer = NULL; // optional flags public $is_add_button_printed = true; public $is_message_printed = true; public $is_flash_message_printed = true; public $is_pagination_printed = true; /** * * @param array $data_array * - data array with standard variables: title, $flash_status, $flash etc * @param string $form_array_source * array with inputs for search box * @param string $table_array_source * array with elements for index table */ public function __construct(array $data_array) { // data for access inside functions parent::__construct($data_array); $this->search = $this->data['search']; } public function render_all() { $this->print_all(); } public function print_page_header() { if( isset($this->data['title']) ) { print '<div class="page-header"><h2>' . $this->data['title'] . '</h2></div>'; } } protected function print_message() { if( $this->is_message_printed ) print '<div id="message"></div>'; } protected function print_flash_message() { if( $this->is_flash_message_printed ) { if( isset($this->data->flash) and $this->data->flash ) { print '<input type="hidden" name="' . ( isset($this->data->flash_status) ? $this->data->flash_status : '' ) . '" id="flash" value="' . $this->data->flash . '" /> '; } } } public function print_search_form() { if( $this->search_box_printer != NULL ) { $this->search_box_printer->print_all(); } else { $this->search_box_printer = new Standard_Search_Form_Printer($this->data); $this->search_box_printer->print_all(); } } public function print_pagination() { if( $this->is_pagination_printed ) { print '<div class="row">' . ( $this->is_add_button_printed ? $this->data['add'] : '' ); print '<div class="total_records span">' . $this->lang->txt('total') . ' ' . $this->data['pagination_config']['total_rows'] . '</div>'; print '<div class="records_per_page pull-right">' . $this->data['limit'] . '</div>'; print '<div class="pull-right pagination_numbers">' . $this->data['pagination'] . '</div>'; print '</div>'; } } public function print_index_table() { if( $this->table_printer != NULL ) { $this->table_printer->print_all(); } else { $this->table_printer = new Standard_Table_Printer($this->data); $this->table_printer->print_all(); } } protected function print_information() { } } abstract class HTMlElement { public $css = 0, $name = 0, $id = 0; protected $tag; public function __construct($tag) { $this->tag = $tag; } public function begin() { $attrs = $this->_attrs(); if( $attrs != '' ) { print "<$this->tag $attrs >"; } else { print "<$this->tag>"; } } public function end() { print "</$this->tag>"; } public abstract function print_all(); protected function _attrs() { $attrs = ''; if( $this->name ) $attrs .= 'name="' . $this->name . '" '; if( $this->id ) $attrs .= 'id="' . $this->id . '" '; if( $this->css ) $attrs .= 'class="' . $this->css . '" '; return $attrs; } } abstract class Printable_Section extends HTMlElement { // helper variables protected $data = array(); protected $lang = false; protected $parent = false; public function __construct(array $data, $tag = 'div') { parent::__construct($tag); $this->data = $data; $this->parent = &get_instance(); $this->lang = $this->parent->lang; } public function print_all() { $this->before_content(); $this->begin(); $this->print_section_content(); $this->end(); $this->after_content(); } public abstract function print_section_content(); protected function before_content() { } protected function after_content() { } } class Standard_Table_Printer extends Printable_Section { // indexes of columns to be printed. empty = all // must be in correct format public $table_headers = array(); // optional flags public $is_num_printed = true; // helper variables protected $table = array(); public function __construct(array $data) { parent::__construct($data, 'table'); $this->table = $data['table']; $this->id = "records"; $this->css = "table table-striped table-hover"; } public function print_section_content() { if( count($this->table['elements']) > 0 ) { if( !empty($this->table_headers) ) { $this->table['headers'] = $this->table_headers; } $this->print_table_head(); $this->print_table_body(); } else { print '<div class="alert alert-error no_records">' . $this->lang->txt('no_records') . '</div>'; } } protected function print_table_head() { print '<thead>'; $this->print_table_headers(); print '</thead>'; } protected function print_table_headers() { print '<tr class="headers">'; if( $this->is_num_printed ) { print '<th class="header_nr">' . $this->lang->txt('nr') . '</th>'; } foreach( $this->table['headers'] as $header => $value ) { if( $value ) { $selected = $sortby = ''; $selected = ( $this->table['sorting']['sort'] == $header and $sortby = $this->table['sorting']['by'] ) ? 'selected' : ''; // print '<th class="header_'.$header.' '.$selected.'"><a onClick="javascript:sortby(\''.$header.'\')">'.$value.'</a> '.$sortby.'</th>'; print '<th class="header_' . $header . ' ' . $selected . ' "><a rel="' . $header . '" class="' . $selected . ' ' . $sortby . '">' . ( $value === 1 ? $this->lang->txt($header) : $value ) . '</a></th>'; } else print '<th class="header_' . $header . '">' . $this->lang->txt($header) . '</th>'; } print '</tr>'; } protected function print_table_body() { print '<tbody>'; $this->print_table_rows(); print '</tbody>'; } protected function print_table_rows() { $nr = $this->data['pagination_config']['cur_page'] + 1; foreach( $this->table['elements'] as $element ) { $this->print_formatted_row($element, $nr); $nr++; } } protected function print_formatted_row($element, $nr) { print '<tr>'; if( $this->is_num_printed ) { print '<td class="header_nr">' . $nr . '</td>'; } foreach( $this->table['headers'] as $header => $value ) { $element_value = ''; if( isset($element->$header) ) $element_value = $element->$header; if( $header == 'datecreated' or $header == 'dateupdated' ) $element_value = ( $element_value != 0 and $element_value != 1 ) ? format_date_local($element_value) : ''; else $element_value = ( in_array($element_value, array( '0', '1' )) ) ? $this->lang->txt(set_yesno($element_value)) : $element_value; $element_value = ( $element_value ) ? $element_value : '-'; if( $header == 'options' and isset($this->table['options']['links']) and is_array($this->table['options']['links']) ) { $links = array(); foreach( $this->table['options']['links'] as $key => $val ) { $links[$key] = $val . $element->rid; } $element_value = $this->generate_buttons($links, $this->table['options']['translations']); } $selected = ''; $selected = ( $this->table['sorting']['sort'] == $header ) ? 'td_selected' : ''; print '<td class="element_' . $header . ' ' . $selected . '">' . $element_value . '</td>'; } print '</tr>'; } protected function generate_buttons($links, $translations) { return set_options($links, $translations); } } class Standard_Search_Form_Printer extends Printable_Section { // helper variables protected $search = false; public function __construct(array $data) { parent::__construct($data, 'div'); $this->search = $data['search']; $this->id = ""; $this->css = "search well"; } public function print_section_content() { print '<div class="search_options_block row">'; // form start print $this->data['search']['open']; print '<fieldset>'; $this->print_search_fields(); print '</fieldset>'; // form end print $this->data['search']['close']; print '</div>'; } protected function print_search_fields() { print $this->search['hidden']; if( $this->search['options'] ) print '<div class="search_button span"><label>&nbsp;</label>' . $this->search['options'] . '</div>'; print '<div class="control-group span">'; foreach( $this->search['elements'] as $key => $element ) { if( isset($element['form']) ) { $mandatory = isset($element['label']['mandatory']) ? '<span class="mandatory">*</span>' : ''; print '<div class="span controls"><label class="control-label" for="' . $key . '">' . $element['label'] . '</label>' . $element['form'] . '</div>'; } } print '</div>'; } } // //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /** * Provides unified display of index pages. * How to use: * * $index = new Standard_Index_Page($this->data); * $index->render_all(); * * to customize extend this class and overwrite necessary methods. For example: * * class User_Index_Page extends Standard_Index_Page { * * protected function generate_buttons($links, $translations) * { * return button_dropdown($links, $translations, 'Action'); * } * * } * or add new functionality here. * * $index = new User_Index_Page($this->data); * $index->render_all(); * * @author Michal K. * */ class Standard_Index_Page extends Index_View_Helper { public function print_section_content() { // title / header $this->print_page_header(); // page body start print '<div class="page_body">'; // message container for javascript $this->print_message(); // flash message $this->print_flash_message(); // print search form, inputs, buttons $this->print_search_form(); print '<form id="sorting_form" action="' . current_url() . '" method="post">'; print '<input type="hidden" name="sorting" id="sorting" value="" />'; // print pagination and/or add button $this->print_pagination(); // print table with selected columns. no select = all columns $this->print_index_table(); print '</form>'; // page body end print '</div>'; $this->print_information(); } } print 'FINISH';
Output for 5.3.0 - 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
FINISH