/** * Generate search form * * generates a search form. * types allowed: select, multiselect, text (or input), datetime, newline * * Example of use: * - array for 'select' item type * $search[] = array('type' => 'select', // Type * 'name' => 'Search By', // Displayed title for item * 'id' => 'searchby', // Item id and name * 'width' => '120px', // (Optional) Item width * 'size' => '15', // (Optional) Maximum number of items to show in the menu (default 15) * 'value' => $vars['searchby'], // (Optional) Current value(-s) for item * 'values' => array('mac' => 'MAC Address', * 'ip' => 'IP Address')); // Array with option items * - array for 'multiselect' item type (array keys same as above) * $search[] = array('type' => 'multiselect', * 'name' => 'Priorities', * 'id' => 'priority', * 'width' => '150px', * 'subtext' => TRUE, // (Optional) Display items value right of the item name * 'value' => $vars['priority'], * 'values' => $priorities); * - array for 'text' or 'input' item type * $search[] = array('type' => 'text', * 'name' => 'Address', * 'id' => 'address', * 'width' => '120px', * 'value' => $vars['address']); * - array for 'datetime' item type * $search[] = array('type' => 'datetime', * 'id' => 'timestamp', * 'presets' => TRUE, // (optional) Show select field with timerange presets * 'min' => dbFetchCell('SELECT MIN(`timestamp`) FROM `syslog`'), // (optional) Minimum allowed date/time * 'max' => dbFetchCell('SELECT MAX(`timestamp`) FROM `syslog`'), // (optional) Maximum allowed date/time * 'from' => $vars['timestamp_from'], // (optional) Current 'from' value * 'to' => $vars['timestamp_to']); // (optional) Current 'to' value * - array for 'newline' item pseudo type * $search[] = array('type' => 'newline') * print_search($search, 'Title here'); * * @param array $data, string $title * @return none * */ function print_search($data, $title = NULL, $button = 'search') { // Form header $string = PHP_EOL . '<!-- START search form -->' . PHP_EOL; $string .= '<form method="POST" action="" class="form form-inline">' . PHP_EOL; $string .= '<div class="navbar">' . PHP_EOL; $string .= '<div class="navbar-inner">'; $string .= '<div class="container">'; if (isset($title)) { $string .= ' <a class="brand">' . $title . '</a>' . PHP_EOL; } $string .= '<div class="nav" style="margin: 5px 0 5px 0;">'; // Main foreach ($data as $item) { $string .= get_form_element($item); } $string .= '</div>'; // Form footer $string .= ' <ul class="nav pull-right"><li>' . PHP_EOL; $string .= ' <input type="hidden" name="search_form" value="1">' . PHP_EOL; $string .= ' <input type="hidden" name="pageno" value="1">' . PHP_EOL; switch ($button) { case 'update': $string .= ' <button type="submit" class="btn"><i class="icon-refresh"></i> Update</button>' . PHP_EOL; break; default: $string .= ' <button type="submit" class="btn"><i class="icon-search"></i> Search</button>' . PHP_EOL; } $string .= ' </li></ul>' . PHP_EOL; $string .= '</div></div></div></form>' . PHP_EOL; $string .= '<!-- END search form -->' . PHP_EOL . PHP_EOL; // Print search form echo $string; }
function print_form($data) { $form_id = 'form-' . strgen(); $form_class = $data['type'] == 'rows' ? 'form-inline' : 'form'; $base_class = $data['class'] ? $data['class'] : 'well'; $base_space = '5px'; $used_vars = array(); // Form elements if ($data['type'] == 'rows') { $row_style = ''; $string_elements = ''; foreach ($data['row'] as $k => $row) { $string_elements .= ' <div class="row" ' . $row_style . '> <!-- START row-' . $k . ' -->' . PHP_EOL; foreach ($row as $id => $element) { $used_vars[] = $id; $element['id'] = $id; $element['class'] = 'col-lg-2'; if ($element['right']) { $element['class'] .= ' pull-right'; } if ($id == 'search' && $data['url']) { // Add form_id here, for generate onclick action in submit button $element['form_id'] = $form_id; } $string_elements .= ' <div class="' . $element['class'] . '">' . PHP_EOL; $string_elements .= get_form_element($element); $string_elements .= ' </div>' . PHP_EOL; } $string_elements .= ' </div> <!-- END row-' . $k . ' -->' . PHP_EOL; $row_style = 'style="margin-top: ' . $base_space . ';"'; // Add space between rows } } // Remove old vars from url if ($data['url']) { foreach ($used_vars as $var) { $data['url'] = preg_replace('/' . $var . '=[^\\/]+\\/?/', '', $data['url']); } } // Form header $string = PHP_EOL . "<!-- START {$form_id} -->" . PHP_EOL; $string .= '<div class="' . $base_class . '" style="padding: ' . $base_space . ';">' . PHP_EOL; $string .= '<form method="POST" id="' . $form_id . '" action="' . $data['url'] . '" class="' . $form_class . '" style="margin-bottom:0;">' . PHP_EOL; if ($data['brand']) { $string .= ' <a class="brand">' . $data['brand'] . '</a>' . PHP_EOL; } // Form elements $string .= $string_elements; // Form footer $string .= '</form>' . PHP_EOL; $string .= '</div>' . PHP_EOL; $string .= "<!-- END {$form_id} -->" . PHP_EOL; // Print form echo $string; }