/** * The default function * * Set up some variables and add them to the view file data */ public function index() { /** * This part is the same as the previous example. For brevity comments are removed. */ $content = ''; TINA_MVC\include_helper('pagination'); global $wpdb; $base_sql = 'SELECT ID AS `Database ID`, user_login AS `User Login`, display_name AS `Display Name` FROM ' . $wpdb->users; $count_sql = 'SELECT COUNT(ID) FROM ' . $wpdb->users; $P = new TINA_MVC\pagination('my_paginator'); $P->set_count_from_sql($count_sql); $P->set_base_url(TINA_MVC\get_controller_url('page-test-3')); $P->filter_box_on_fields(array('User Login' => 'user_login', 'Display Name' => 'display_name', 'user_email' => 'user_email')); $P->set_base_sql($base_sql); $P->set_mid_range(9)->set_items_per_page(10)->set_default_sort_by('User Login')->set_default_sort_order('desc'); /** * We will get the results and format custom HTML rows rather than allow * the pagination helper to give us a generic table. You should use escaped * HTML here. * * In this case we are just building some HTML. You would normally use a * view file instead of putting HTML in your page controller. */ $rows = $P->get_sql_rows(); // TINA_MVC\prd( $rows ); /** * Iterate throught the results and build some HTML */ foreach ($rows as $i => &$r) { if ($i % 2) { $bg = '#333'; $fg = '#ccc'; } else { $bg = '#ccc'; $fg = '#333'; } $r->{'Database ID'} = '<span style="color:' . $fg . ';background:' . $bg . '">' . $wpdb->escape($r->{'Database ID'}) . '</span>'; $r->{'User Login'} = '<span style="color:' . $fg . ';background:' . $bg . '"><a href="#" title="' . $wpdb->escape($r->{'Display Name'}) . '">' . $r->{'User Login'} . '</a></span>'; $r->{'A non-DB field (non-sortable)'} = '<span style="color:' . $fg . ';background:' . $bg . '"><a href="#" title="' . $wpdb->escape($r->{'Display Name'}) . '">' . $i . '</a></span>'; /** * You can also unset() an entry here */ // $r->{'Display Name'} = '<span style="color:'.$fg.';background:'.$bg.'">'.$wpdb->escape($r->{'Display Name'}).'</span>'; unset($r->{'Display Name'}); } /** * Set the rows, overriding the use of the html table helper */ $P->set_html_rows($rows); /** * This will prevent the helper from adding sortable column HTML to the * column heading 'A non-DB field (non-sortable)'. * * You cannot sort on columns that do not come directly from the database. */ $P->suppress_sort(array('A non-DB field (non-sortable)')); $this->set_post_title('Pagination'); $this->set_post_content($P->get_html()); }
/** * Generate some random data and use the tina_mvc_html_table_helper to generate HTML * tables */ function index() { /** * Include the helper */ TINA_MVC\include_helper('table'); /** * Quick and dirty... the HTML we'll output * * No fancy view files here... */ $html_out = ''; /** * The first table * * Generate an array of data to format */ $table_headings = array('column_one', 'Column 2', '<col-3>'); $table_data = array(); for ($i = 0; $i < 5; $i++) { foreach ($table_headings as $j => $heading) { $table_data[$i][$heading] = rand(); } } $table = new TINA_MVC\table('first_table'); $table->set_data($table_data); $html_out .= '<h2>The First Table</h2>'; $html_out .= $table->get_html(); /** * All done */ unset($table); /** * The second table * * Generate an object of data to format */ $table_headings = array('<a href="#">column_one</a>', 'Column 2(€)', 'Now you see me -> <col-3> and now you don\'t -> <col-3>'); $table_data = new stdClass(); for ($i = 0; $i < 12; $i++) { foreach ($table_headings as $j => $heading) { $table_data->{$i}->{$heading} = rand(); } } $table = new TINA_MVC\table('second_table'); $table->set_data($table_data); /** * Because we have proper HTML in the headers, we don't want to escape the * table headings */ $table->do_not_esc_th(TRUE); $html_out .= '<h2>The Second Table</h2>'; $html_out .= $table->get_html(); /** * Method chaining is also supported. You can do * * $html_out .= $table->set_data( $table_data )->do_not_esc_th( TRUE )->get_html(); */ $this->set_post_title('My Beautiful Tables'); $this->set_post_content($html_out); }
/** * Edit the users personal data * * @todo do we need to implement the Wordpress 'password_reset' action hook */ public function edit() { $current_user = $this->get_current_user(); TINA_MVC\include_helper('form'); $f = new TINA_MVC\form('edit_data_form'); // $f_user_login = $f->add_field( 'user_login', 'text' ); $f_user_email = $f->add_field('user_email', 'text')->set_default_value($current_user->user_email); $f_first_name = $f->add_field('first_name', 'text')->set_default_value($current_user->user_firstname); $f_last_name = $f->add_field('last_name', 'text')->set_default_value($current_user->user_lastname); $f_display_name = $f->add_field('display_name', 'text')->set_default_value($current_user->display_name); $f_current_password = $f->add_field('current_password', 'password')->add_validation(array('required' => NULL)); $f_note = $f->add_text('f_note', "Leave 'New Password' blank to leave your password unchanged."); $f_new_password_1 = $f->add_field('new_password', 'password'); $f_new_password_2 = $f->add_field('new_password_again', 'password'); $f_submit = $f->add_field('submit', 'submit'); $f_cancel = $f->add_field('cancel', 'submit'); if ($f_cancel->get_posted_value()) { wp_redirect(TINA_MVC\get_controller_url('my-profile')); exit; } if ($data = $f->get_posted_data()) { $user_data = array(); $error = 0; // check the password for edits... if (is_wp_error(wp_authenticate($current_user->user_login, $data['current_password']))) { $f_current_password->add_validation_error('Incorrect password.'); $error++; } if ($data['user_email']) { if (!is_email($data['user_email'])) { $f_user_email->add_validation_error("'" . $f_user_email->get_caption . "' is not a valid email address."); $error++; } // make sure it isn't attached to another user... if ($test_user = get_user_by('email', $data['user_email'])) { if ($test_user->ID != $current_user->ID) { $f_user_email->add_validation_error('This email address belongs to another user.'); $error++; } } if (!$error) { $user_data['user_email'] = $data['user_email']; } } if ($data['first_name']) { $user_data['first_name'] = $data['first_name']; } if ($data['last_name']) { $user_data['last_name'] = $data['last_name']; } if ($data['display_name']) { $user_data['display_name'] = $data['display_name']; } if ($data['new_password'] or $data['new_password_again']) { if ($data['new_password'] != $data['new_password_again']) { $f_new_password_1->add_validation_error('New passwords must match.'); $error++; } else { $user_data['user_pass'] = $data['new_password']; } } if ($user_data and !$error) { $user_data['ID'] = $current_user->ID; wp_update_user($user_data); wp_redirect(TINA_MVC\get_controller_url('my-profile/index/saved')); exit; } } $this->add_var('edit_data_form', $f->render()); $this->set_post_title('Editing Profile for ' . $current_user->user_login); $this->set_post_content($this->load_view('my_profile')); }
/** * The default controller * @return void */ public function index() { /** * Load the helper file */ TINA_MVC\include_helper('form'); /** * Create the new form */ $form = new TINA_MVC\form('my_form'); /** * Open a fieldset */ $fset_open = $form->fieldset_open('fset_name_pw', 'Name and password please'); /** * Adds a text field */ $f_name = $form->add_field('your_name', $type = 'TEXT'); $f_name->add_validation(array('required' => NULL)); /** * Adds a password field */ $f_password = $form->add_field('the_password', $type = 'PASSWORD'); /** * Close a fieldset * * This is optional. If you do not close a fieldset, it is closed whenever you open a new one or * when you render the form */ $fset_close = $form->fieldset_close(); /** * add_field() and all field methods return a field object so you can use method chaining */ $f_hidden = $form->add_field('a_hidden_field', 'hidden')->set_value('Hidden field value'); /** * Some sample data for the select type */ $options = array(array('1', 'dog'), array('21', 'cat'), array('1234', 'mouse'), array('3', 'cake'), array('4', 'house'), array('5', 'meh')); /** * Adds the select field */ $f_select = $form->add_field('select_field', 'SELECT')->set_options($options); /** * Adds the radio field using the same options as for the select field above */ $f_radio = $form->add_field('radio_field', 'RADIO')->set_options($options); /** * A checkbox field */ $f_check = $form->add_field('check_field', $type = 'CHECKBOX', $caption = 'This is the checkbox caption'); /** * A textarea field */ $f_textarea = $form->add_field('textarea_field', $type = 'TEXTAREA', $caption = 'This is the textarea caption'); $f_textblock = $form->add_text('textblock_field', 'This is a block of text included in the form using the "textblock" field type.'); /** * A Googlemap location field * * You must enter your API key into app_settings.php */ if (TINA_MVC\get_tina_mvc_setting('google_api_key_v3')) { $f_map = $form->add_field('map_field', 'GOOGLEMAP'); /** * Sets the height and width of the page in CSS units */ $f_map->set_map_height(200); $f_map->set_map_height(400); } else { $f_gmap_note = $form->add_text('gmap_note', 'You must enter your Google API key (v3) into app_settings.php to use the Google Map location field type.'); } /** * A reCaptcha field * * The recaptcha field type requires your API keys to be set in app_settings.php * * @see http://www.reCaptcha.net */ if (TINA_MVC\get_tina_mvc_setting('recaptcha_pub_key') and TINA_MVC\get_tina_mvc_setting('recaptcha_pri_key')) { $f_recaptcha = $form->add_field('recaptcha_field', $type = 'RECAPTCHA', $caption = 'Are you human?'); } else { $f_recaptcha_note = $form->add_text('recaptcha_note', 'You must enter your reCaptcha API keys into app_settings.php to use the reCaptcha field type.'); } /** * A file upload field */ $f_file_upload = $form->add_field('a_file_upload', 'file'); /** * A second file upload field */ $f_second_file_upload = $form->add_field('a_second_file_upload', 'file'); /** * A submit button */ $f_submit = $form->add_field('submit_field', $type = 'SUBMIT', $caption = 'This is the submit caption'); /** * A reset button */ $f_reset = $form->add_field('reset_field', $type = 'RESET', $caption = 'This is the reset caption'); /** * Some data to pre-load into the form. * * This could come from a database query for example. */ $data = array('name' => 'Joe Bloggs', 'textarea_field' => "Some multiline\r\ntext", 'radio_field' => 3); /** * Loads the data into the form, overriding any default values previously set * * You would use this function to load a recordset from (for example a database query) into * the form. First, any keys in the $data array will match field names. After this any 'db_field' * values matching keys in $data have their values set too. */ $form->load_data($data); /** * Returns an array of posted data, FALSE if the form has not been posted. */ if ($posted_data = $form->get_posted_data()) { $content = "Posted Data<br />"; $content .= "<pre>" . print_r($posted_data, TRUE) . "</pre>"; /** * Generally you process your form here and finish off with a wp_redirect() and exit() */ // wp_redirect( 'wherever' ); // exit(); } else { /** * Returns the HTML required to disply the form */ $content = $form->render(); } /** * Assign as template data */ $this->add_var('view_html', $content); /** * Set the post (page) title and contents. */ $this->set_post_title('Tina MVC New Form Helper Example'); $this->set_post_content($this->load_view('test_form')); }
/** * The Form Helper - List of field types and validation rules */ function form_helper_fields_and_validation() { $classes_file = file_get_contents(\TINA_MVC\helpers_folder() . '/tina_mvc_form_helper_class.php'); TINA_MVC\include_helper('form'); $classes = \get_declared_classes(); // grab the field types $field_types = array(); $validate_types = array(); // loop through the classes and filter out the ones that begin with 'TINA_MVC\field_' or 'TINA_MVC\validate_' // this loop builds the two arrays - $field_types and $validate_types foreach ($classes as $c) { $class_type = FALSE; // have we a match? if (strpos(strtolower($c), 'tina_mvc\\field_') === 0) { $class_type = 'field'; } elseif (strpos(strtolower($c), 'tina_mvc\\validate_') === 0) { $class_type = 'validate'; } // yep - we do if ($class_type) { $short_c_name = str_replace('TINA_MVC\\', '', $c); $class_offset = strpos($classes_file, "class {$short_c_name}"); $classes_file_part = substr($classes_file, 0, $class_offset); $start_char = strrpos($classes_file_part, '/**'); $end_char = strrpos($classes_file_part, '*/'); $docBlockComment = substr($classes_file, $start_char, $end_char - $start_char + 2); $array_var_name = $class_type . '_types'; ${$array_var_name}[$short_c_name]['class'] = $short_c_name; ${$array_var_name}[$short_c_name]['type_for_helper'] = str_replace($class_type . '_', '', $short_c_name); ${$array_var_name}[$short_c_name]['docblock'] = $docBlockComment; } } $this->add_var_e('field_types', $field_types); $this->add_var_e('validate_types', $validate_types); $this->set_post_content($this->load_view('admin_pages_form_helper_fields_and_validation', $this->view_data, 'tina_mvc/admin_pages')); }
/** * The default function * * Set up some variables and add them to the view file data */ public function index() { $content = ''; TINA_MVC\include_helper('pagination'); global $wpdb; /** * We are using custom SQL here to demonstrate the use of the pager. * * Have a look at the Demo Data Creator plugin if you want to create many * dummy users for use with this example... or even better play with your own data * * The $base_sql is a properly escaped statement that will get you all the rows you want. ORDER BY and LIMIT clauses * are not set - they will be set automatically. * * Pay particular attention to the field names and their aliases. These are relevent when it comes to setting * sortable columns and a filter box (below). */ $base_sql = 'SELECT ID AS `Database ID`, user_login AS `User Login`, display_name AS `Display Name` FROM ' . $wpdb->users; /** * A SQL statement to get the total number of rows returned in the above statement. */ $count_sql = 'SELECT COUNT(ID) FROM ' . $wpdb->users; /** * The HTML table ID is set from the parameter you ppass to the constructor */ $P = new TINA_MVC\pagination('my_paginator'); $P->set_count_from_sql($count_sql); /** * The url required to get to the default table view */ $P->set_base_url(TINA_MVC\get_controller_url('page-test-1')); /** * Set up the filter. * * This allows a user to search on various fields (even if they are not * selected for display). This will output a form at the top of the table * of results. * * Parameter is array ( 'Display Name' => 'mysql_field_name' ) * * These must match the field names and aliases in $base_sql */ $P->filter_box_on_fields(array('User Login' => 'user_login', 'Display Name' => 'display_name', 'user_email' => 'user_email')); $P->set_base_sql($base_sql); /** * For the pagination links */ $P->set_mid_range(9)->set_items_per_page(10)->set_default_sort_by('User Login')->set_default_sort_order('desc'); /** * Grab the html. */ $content = $P->get_html(); /** * Sets the post title */ $this->set_post_title('Testing Pagination'); /** * Sets the post content. Note we are not using a view file for this example */ $this->set_post_content($content); }
/** * The default function * * Set up some variables and add them to the view file data */ public function index() { /** * This part is the same as the previous example. For brevity comments are removed. */ $content = ''; TINA_MVC\include_helper('pagination'); global $wpdb; $base_sql = 'SELECT ID AS `Database ID`, user_login AS `User Login`, display_name AS `Display Name` FROM ' . $wpdb->users; $count_sql = 'SELECT COUNT(ID) FROM ' . $wpdb->users; $P = new TINA_MVC\pagination('my_paginator'); $P->set_count_from_sql($count_sql); /** * OK, not quite the same. The base url is obviously different! */ $P->set_base_url(TINA_MVC\get_controller_url('page-test-2')); $P->filter_box_on_fields(array('User Login' => 'user_login', 'Display Name' => 'display_name', 'user_email' => 'user_email')); $P->set_base_sql($base_sql); $P->set_mid_range(9)->set_items_per_page(10)->set_default_sort_by('User Login')->set_default_sort_order('desc'); /** * We will get the results and format custom HTML rows rather than allow * the pagination helper to give us a generic table. You should use escaped * HTML here. */ $rows = $P->get_sql_rows(); // TINA_MVC\prd( $rows ); /** * Iterate throught the results and build some HTML */ foreach ($rows as $i => &$r) { //echo $r->{'User Login'}; /** * We'll use this is the view file to produce the alternating row styles */ $this->add_var('i', $i); /** * The row of data * * You need to be careful here if your array keys (or object variables) require escaping. */ $this->add_var_e('r', $r); /** * The view file here is only a HTML snippet. It renders only one row. * * It is the first example of using a view snippet to produce output. * * In this case, the view file is used to render headings or rows based on the value of $view_file_part */ $this->add_var('view_file_part', 'rows'); $r = $this->load_view('page_test_2'); } /** * Set the rows, overriding the use of the html table helper. * * This will also supress the table headings. */ $P->set_html_rows($rows); /** * Set the table headings. This is optional. * * In this case, the view file is used to render headings or rows based on the value of $view_file_part */ $this->add_var('view_file_part', 'headings'); $P->set_html_headings($this->load_view('page_test_2')); /** * Grab the html. */ $content = $P->get_html(); /** * Sets the post title */ $this->set_post_title('Testing Pagination 2'); /** * Sets the post content. Note we are not using a view file for this example */ $this->set_post_content($content); }
/** * The default controller * @return void */ public function index() { /** * Load the helper file */ TINA_MVC\include_helper('form'); /** * Create the new form */ $form = new TINA_MVC\form('my_form'); /** * Adds a text field */ $text_field_1 = $form->add_field('text_field_1', $type = 'TEXT'); /** * It is possible to retrieve the $_POST value for a field without checking if the form has been posted * using the get_posted_value() method. */ $text_message = $form->add_text('posted_message', 'The $_POST value for \'Text Field 1\' is: "' . $text_field_1->get_posted_value() . '"'); /** * Adds a validation rule * * All validation rules are of the form array( 'rule_name' => parameters ). Check the form helper file * for classes called validate_* for rules, parameters and for how to add your own validation rules. */ $text_field_1->add_validation(array('required' => NULL, 'min_length' => 3, 'max_length' => 7)); /** * The same using method chaining... */ $text_field_2 = $form->add_field('text_field_2', $type = 'TEXT')->add_validation(array('required' => NULL)); /** * Adding a field showing all available parameters: * * $name - the field name. Used as a key in the array of posted data returned by $form->get_posted_data(). * $type - see the field_* classes in the form helper file for a complete list of fields (or see * the samples in 08_form_helper_intro) * $caption - The label. Default label is based on the $name parameter. For example 'first_name' => 'First Name' * $db_table - used to group fields. For example, posted fields grouped according to $db_table = 'my_table' can * be retrieved using $form->get_posted_db_data('my_table') (gets data with keys based on the $db_field value) * or $form->get_posted_data('my_table') (gets data with keys based on the field $name value) * $db_field - the database field name (if different from the $name value). Use with $form->get_posted_db_data() * $default_value * $extra_attribs - a string (for example 'attribute="value"') or array of strings */ $text_field_3 = $form->add_field('text_field_3', 'text', 'Please enter a value for this field', 'my_table', 'my_field', 'Default value', 'style="color: white; background: black;"'); /** * Similar to above, but using method chaining and passing several extra html attributes to the input */ $text_field_4 = $form->add_field('field_4', $type = 'TEXT')->set_caption('Field Four')->set_db_table('my_table')->set_db_field('field_four')->set_default_value('Another default')->set_extra_attribs('style="background: #ccc;"')->set_extra_attribs('title="Like a tooltip, touched for the very first time..."'); /** * Extra attributes can be passed as an array too */ $text_field_5 = $form->add_field('field_5', $type = 'TEXT')->set_db_table('my_table')->set_extra_attribs(array('style="font-style: italic;"', 'title="Another title attribute"')); /** * A submit button */ $f_submit = $form->add_field('submit_field', $type = 'SUBMIT', $caption = 'This is the submit caption'); /** * A reset button */ $f_reset = $form->add_field('reset_field', $type = 'RESET', $caption = 'This is the resetcaption'); /** * Returns an array of posted data, FALSE if the form has not been posted. */ if ($posted_data_1 = $form->get_posted_data()) { /** * $posted_data_1 is an array of posted values. Keys are the field names. Values are taken from fields with a blank 'db_table' value */ $content = 'Posted Data: retrieved with $form->get_posted_data()<br />'; $content .= "<pre>" . print_r($posted_data_1, TRUE) . "</pre><br />"; /** * Returns an array of posted values. Keys are the field names. Values are taken from fields where 'db_table' = 'my_table' */ $posted_data_2 = $form->get_posted_data('my_table'); $content .= "Posted Data: retrieved with \$form->get_posted_data('my_table')<br />"; $content .= "<pre>" . print_r($posted_data_2, TRUE) . "</pre><br />"; /** * Returns an array of posted values. Keys are taken from 'db_field' value. Values are taken from fields with a blank 'db_table' value */ $posted_data_3 = $form->get_posted_db_data(); $content .= "Posted Data: retrieved with \$form->get_posted_db_data()<br />"; $content .= "<pre>" . print_r($posted_data_3, TRUE) . "</pre><br />"; /** * Returns an array of posted values. Keys are taken from 'db_field' value. Values where fields 'db_table' = 'my_table' */ $posted_data_4 = $form->get_posted_db_data('my_table'); $content .= "Posted Data: retrieved with \$form->get_posted_db_data('my_table')<br />"; $content .= "<pre>" . print_r($posted_data_4, TRUE) . "</pre><br />"; /** * Generally you process your form here and finish off with a wp_redirect() and exit() */ // wp_redirect( 'wherever' ); // exit(); } else { /** * Returns the HTML required to disply the form */ $content = $form->render(); } /** * Assign as template data */ $this->add_var('view_html', $content); /** * Set the post (page) title and contents. */ $this->set_post_title('Tina MVC New Form Helper Example'); $this->set_post_content($this->load_view('test_form_2')); }