web: add utility functions for Bootstrap forms

Bootstrap forms require a bunch of <div>s and other stuff.
I wrapped these in functions like
form_start()
form_end()
form_submit()
form_select_multiple()
form_input_text()

General idea going forward:
put HTML (especially Bootstrap-specified) in utility functions,
e.g. in util.inc or bootstrap.inc.
This will make the higher-level code easier to read,
and will facilitate moving to CSS frameworks other than Bootstrap.
This commit is contained in:
David Anderson 2016-12-16 12:05:30 -08:00
parent fc4fb299a6
commit 91025d6b7a
3 changed files with 109 additions and 26 deletions

View File

@ -265,9 +265,9 @@ class BoincTeam {
$db = BoincDb::get(); $db = BoincDb::get();
return $db->update($this, 'team', $clause); return $db->update($this, 'team', $clause);
} }
static function enum($clause, $clause2=null) { static function enum($where_clause, $order_clause=null) {
$db = BoincDb::get(); $db = BoincDb::get();
return $db->enum('team', 'BoincTeam', $clause, $clause2); return $db->enum('team', 'BoincTeam', $where_clause, $order_clause);
} }
static function lookup($clause) { static function lookup($clause) {
$db = BoincDb::get(); $db = BoincDb::get();
@ -303,9 +303,9 @@ class BoincTeamDelta {
$db = BoincDb::get(); $db = BoincDb::get();
return $db->insert('team_delta', $clause); return $db->insert('team_delta', $clause);
} }
static function enum($clause) { static function enum($where_clause) {
$db = BoincDb::get(); $db = BoincDb::get();
return $db->enum('team_delta', 'BoincTeamDelta', $clause); return $db->enum('team_delta', 'BoincTeamDelta', $where_clause);
} }
} }
@ -322,9 +322,9 @@ class BoincHost {
$db = BoincDb::get(); $db = BoincDb::get();
return $db->delete($this, 'host'); return $db->delete($this, 'host');
} }
static function enum($clause, $clause2=null) { static function enum($where_clause, $order_clause=null) {
$db = BoincDb::get(); $db = BoincDb::get();
return $db->enum('host', 'BoincHost', $clause, $clause2); return $db->enum('host', 'BoincHost', $where_clause, $order_clause);
} }
static function enum_fields($fields, $where_clause, $order_clause=null) { static function enum_fields($fields, $where_clause, $order_clause=null) {
$db = BoincDb::get(); $db = BoincDb::get();
@ -343,9 +343,9 @@ class BoincResult {
$db = BoincDb::get(); $db = BoincDb::get();
return $db->count('result', $clause); return $db->count('result', $clause);
} }
static function enum($clause) { static function enum($where_clause) {
$db = BoincDb::get(); $db = BoincDb::get();
return $db->enum('result', 'BoincResult', $clause); return $db->enum('result', 'BoincResult', $where_clause);
} }
static function enum_fields($fields, $where_clause, $order_clause) { static function enum_fields($fields, $where_clause, $order_clause) {
$db = BoincDb::get(); $db = BoincDb::get();
@ -389,9 +389,9 @@ class BoincWorkunit {
if (!$ret) return $ret; if (!$ret) return $ret;
return $db->insert_id(); return $db->insert_id();
} }
static function enum($clause) { static function enum($where_clause) {
$db = BoincDb::get(); $db = BoincDb::get();
return $db->enum('workunit', 'BoincWorkunit', $clause); return $db->enum('workunit', 'BoincWorkunit', $where_clause);
} }
function update($clause) { function update($clause) {
$db = BoincDb::get(); $db = BoincDb::get();
@ -416,9 +416,9 @@ class BoincApp {
$db = BoincDb::get(); $db = BoincDb::get();
return $db->lookup('app', 'BoincApp', $clause); return $db->lookup('app', 'BoincApp', $clause);
} }
static function enum($clause) { static function enum($where_clause) {
$db = BoincDb::get(); $db = BoincDb::get();
return $db->enum('app', 'BoincApp', $clause); return $db->enum('app', 'BoincApp', $where_clause);
} }
static function insert($clause) { static function insert($clause) {
$db = BoincDb::get(); $db = BoincDb::get();
@ -437,9 +437,9 @@ class BoincApp {
} }
class BoincAppVersion { class BoincAppVersion {
static function enum($clause) { static function enum($where_clause) {
$db = BoincDb::get(); $db = BoincDb::get();
return $db->enum('app_version', 'BoincAppVersion', $clause); return $db->enum('app_version', 'BoincAppVersion', $where_clause);
} }
static function lookup($clause) { static function lookup($clause) {
$db = BoincDb::get(); $db = BoincDb::get();
@ -486,9 +486,9 @@ class BoincProfile {
$db = BoincDb::get(); $db = BoincDb::get();
return $db->insert('profile', $clause); return $db->insert('profile', $clause);
} }
static function enum($clause=null, $clause2=null) { static function enum($where_clause=null, $order_clause=null) {
$db = BoincDb::get(); $db = BoincDb::get();
return $db->enum('profile', 'BoincProfile', $clause, $clause2); return $db->enum('profile', 'BoincProfile', $where_clause, $order_clause);
} }
static function enum_fields($fields, $where_clause=null, $order_clause=null) { static function enum_fields($fields, $where_clause=null, $order_clause=null) {
$db = BoincDb::get(); $db = BoincDb::get();
@ -509,9 +509,9 @@ class BoincTeamAdmin {
$db = BoincDb::get(); $db = BoincDb::get();
return $db->insert('team_admin', $clause); return $db->insert('team_admin', $clause);
} }
static function enum($clause) { static function enum($where_clause) {
$db = BoincDb::get(); $db = BoincDb::get();
return $db->enum('team_admin', 'BoincTeamAdmin', $clause); return $db->enum('team_admin', 'BoincTeamAdmin', $where_clause);
} }
static function delete($clause) { static function delete($clause) {
$db = BoincDb::get(); $db = BoincDb::get();
@ -532,9 +532,9 @@ class BoincPrivateMessage {
$db = BoincDb::get(); $db = BoincDb::get();
return $db->update($this, 'private_messages', $clause); return $db->update($this, 'private_messages', $clause);
} }
static function enum($clause) { static function enum($where_clause) {
$db = BoincDb::get(); $db = BoincDb::get();
return $db->enum('private_messages', 'BoincPrivateMessage', $clause); return $db->enum('private_messages', 'BoincPrivateMessage', $where_clause);
} }
static function insert($clause) { static function insert($clause) {
$db = BoincDb::get(); $db = BoincDb::get();
@ -557,9 +557,9 @@ class BoincPrivateMessage {
} }
class BoincPlatform { class BoincPlatform {
static function enum($clause) { static function enum($where_clause) {
$db = BoincDb::get(); $db = BoincDb::get();
return $db->enum('platform', 'BoincPlatform', $clause); return $db->enum('platform', 'BoincPlatform', $where_clause);
} }
static function lookup_id($id) { static function lookup_id($id) {
$db = BoincDb::get(); $db = BoincDb::get();
@ -580,9 +580,9 @@ class BoincPlatform {
} }
class BoincHostAppVersion { class BoincHostAppVersion {
static function enum($clause) { static function enum($where_clause) {
$db = BoincDb::get(); $db = BoincDb::get();
return $db->enum('host_app_version', 'BoincHostAppVersion', $clause); return $db->enum('host_app_version', 'BoincHostAppVersion', $where_clause);
} }
static function lookup($host_id, $app_version_id) { static function lookup($host_id, $app_version_id) {
$db = BoincDb::get(); $db = BoincDb::get();

View File

@ -266,3 +266,74 @@ function grid($top_func, $left_func, $right_func, $left_width=6) {
</div> </div>
'; ';
} }
function form_start($action) {
echo '
<form class="form-horizontal" method="get" action="'.$action.'">
';
}
function form_input_hidden($name, $value) {
echo '<input type="hidden" name="'.$name.'" value="'.$value.'">
';
}
function form_end() {
echo '</form>
';
}
function form_input_text($label, $name, $value='', $type='text', $attrs='') {
echo '
<div class="form-group">
<label class="control-label col-sm-2" for="'.$name.'">'.$label.'</label>
<div class="col-sm-10">
<input '.$attrs.' type="'.$type.'" class="form-control" id="'.$name.'" name="'.$name.'" value="'.$value.'">
</div>
</div>
';
}
function form_select($label, $name, $items) {
echo '
<div class="form-group">
<label class="control-label col-sm-2" for="'.$name.'">'.$label.'</label>
<div class="col-sm-10">
<select class="form-control" id="'.$name.'" name="'.$name.'">
';
foreach ($items as $i) {
echo '<option value="'.$i[0].'">'.$i[1].'</option>
';
}
echo "</select></div></div>\n";
}
// same, for multiple select.
// flags, if non-null, says which ones are selected
//
function form_select_multiple($label, $name, $items, $flags) {
echo '
<div class="form-group">
<label class="control-label col-sm-2" for="'.$name.'">'.$label.'</label>
<div class="col-sm-10">
<select multiple class="form-control" id="'.$name.'" name="'.$name.'[]">
';
$n = 0;
foreach ($items as $i) {
$s = ($flags && $flags[$n])?'selected':'';
echo '<option '.$s.' value="'.$i[0].'">'.$i[1].'</option>
';
$n++;
}
echo "</select></div></div>\n";
}
function form_submit($text) {
echo '
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-success">'.$text.'</button>
</div>
</div>
';
}

View File

@ -470,12 +470,12 @@ function row_array($x) {
define ('ALIGN_RIGHT', 'style="text-align:right;"'); define ('ALIGN_RIGHT', 'style="text-align:right;"');
function row_heading_array($x, $attrs=null) { function row_heading_array($x, $attrs=null, $class='bg-primary') {
echo "<tr>"; echo "<tr>";
$i = 0; $i = 0;
foreach ($x as $h) { foreach ($x as $h) {
$a = $attrs?$attrs[$i]:""; $a = $attrs?$attrs[$i]:"";
echo "<th $a class=\"bg-primary\">$h</th>"; echo "<th $a class=\"$class\">$h</th>";
$i++; $i++;
} }
echo "</tr>\n"; echo "</tr>\n";
@ -672,6 +672,18 @@ function post_int($name, $optional=false) {
return $y; return $y;
} }
function get_array($name) {
if (isset($_GET[$name])) {
$x = $_GET[$name];
} else {
if (!$optional) {
error_page("missing or bad parameter: $name");
}
$x = null;
}
return $x;
}
function get_str($name, $optional=false) { function get_str($name, $optional=false) {
if (isset($_GET[$name])) { if (isset($_GET[$name])) {
$x = $_GET[$name]; $x = $_GET[$name];