Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Stefan Henzen
committed
Jul 13, 2009
1 parent
5ebae5d
commit c24f742
Showing
21 changed files
with
307 additions
and
85 deletions.
There are no files selected for viewing
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,73 @@ | ||
<? | ||
class HTMLForm extends MicroObject{ | ||
|
||
function __construct($object, $fields, $url='', $method = 'POST'){ | ||
$this->object = $object; | ||
/** | ||
* $fields contains a hash with field names and field properties: | ||
* name : a name for the field | ||
* id [optional] : an html id, default: name | ||
* type [optional] default: string, supported (extensible): string, password, textarea | ||
* value [optional] default: null | ||
* title [optional] default: field name | ||
* error [optional] default: null | ||
*/ | ||
function __construct($fields, $url='', $method = 'POST'){ | ||
$this->fields = $fields; | ||
$this->url = $url; | ||
$this->method = $method; | ||
$this->db_fields = $object->fields(); | ||
|
||
$this->render_prefix(); | ||
} | ||
|
||
function render_all(){ | ||
$this->render_form_prefix(); | ||
$this->render_fields(); | ||
$this->render_suffix(); | ||
$this->render_form_suffix(); | ||
$this->render_field_extras(); | ||
} | ||
|
||
function render_prefix(){echo html_form_open($this->url, $this->method);} | ||
function render_form_prefix(){echo "\n".html_form_open($this->url, $this->method);} | ||
function render_fields(){ | ||
foreach($this->fields as $fieldname => $field){ | ||
if(!$this->do_callback("render_$fieldname") | ||
&& !$this->do_callback("render_field", $field)){ | ||
echo html_input($fieldname, $object->$fieldname()); | ||
} | ||
echo "\n"; | ||
foreach($this->fields as $props){ | ||
if(!$this->done_by_callback('input', $props)) echo 'dunno'; | ||
echo "\n"; | ||
} | ||
} | ||
function render_suffix(){echo '</form>';} | ||
|
||
function done_by_callback($part, $props){ | ||
return $this->do_callback("render_{$props['name']}_{$part}", $props) | ||
|| $this->do_callback("render_".array_try($props, 'type', 'string')."_{$part}", $props) | ||
|| $this->do_callback("render_{$part}", $props); | ||
} | ||
|
||
function render_string_input($props){ | ||
$id = array_try($props, 'id', $props['name']); | ||
echo '<p>'; | ||
echo html_label($id, array_try($props, 'title', util_humanize($props['name']))); | ||
echo html_input($props['name'], array_try($props, 'value', ''), array('id' => $id)); | ||
if($error = array_try($props, 'error', false)) | ||
echo html_span($error, array('class'=>'error')); | ||
echo '</p>'; | ||
} | ||
function render_password_input($props){ | ||
$id = array_try($props, 'id', $props['name']); | ||
echo '<p>'; | ||
echo html_label($id, array_try($props, 'title', util_humanize($props['name']))); | ||
echo html_password($props['name'], array('id' => $id)); | ||
if($error = array_try($props, 'error', false)) | ||
echo html_span($error, array('class'=>'error')); | ||
echo '</p>'; | ||
} | ||
function render_textarea_input($props){ | ||
$id = array_try($props, 'id', $props['name']); | ||
echo '<p class="textarea">'; | ||
echo html_label($id, array_try($props, 'title', util_humanize($props['name']))); | ||
if($error = array_try($props, 'error', false)) | ||
echo html_span($error, array('class'=>'error')); | ||
echo html_textarea($props['name'], array_try($props, 'value', ''), array('id' => $id)); | ||
echo '</p>'; | ||
} | ||
function render_form_suffix(){echo "</form>\n";} | ||
|
||
function render_field_extras(){ | ||
foreach($this->fields as $props) | ||
$this->done_by_callback('extras', $props); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<? | ||
function f_string($object, $field, $title = null){ | ||
return array('name' => get_class($object)."[$field]", | ||
'id' => util_under_score(get_class($object))."-$field", | ||
'title' => ($title ? $title : util_humanize($field)), | ||
'value' => $object->attributes[$field], | ||
'error' => array_try($object->errors, $field, '') | ||
); | ||
} | ||
|
||
function f_password($object, $field, $title = null){ | ||
return array_merge(array('type' => 'password'), | ||
f_string($object, $field, $title)); | ||
} | ||
|
||
function f_textarea($object, $field, $title = null){ | ||
return array_merge(array('type' => 'textarea'), | ||
f_string($object, $field, $title)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,82 @@ | ||
<? | ||
class HTMLTable extends MicroObject{ | ||
|
||
/** | ||
* $columns contains a hash with column properties: | ||
* column : the name of the column | ||
* type [optional] default: string, supported (extensible): string, icon, link | ||
* title [optional] default: column | ||
* for type icon (is also a link by default): | ||
* icon_name [optional] default: column | ||
* link_field [optional] default: $row[id] | ||
* link_as [optional] default: id | ||
* url | ||
* for type link: | ||
* link_field [optional] default: $row[id] | ||
* link_as [optional] default: id | ||
* url | ||
* rows is an array of arrays | ||
*/ | ||
function __construct($columns, $rows=array()){ | ||
$this->columns = array_flatten($columns); | ||
$this->columns = $columns; | ||
$this->rows = $rows; | ||
|
||
$this->render_prefix(); | ||
} | ||
|
||
function render_all(){ | ||
echo '<table>'; | ||
$this->render_cols(); | ||
$this->render_head(); | ||
$this->render_body(); | ||
$this->render_suffix(); | ||
echo '</table>'; | ||
} | ||
|
||
function done_by_callback($part, $props){ | ||
return $this->do_callback("render_{$props['column']}_{$part}", $props) | ||
|| $this->do_callback("render_".array_try($props, 'type', 'string')."_{$part}", $props) | ||
|| $this->do_callback("render_{$part}", $props); | ||
} | ||
|
||
function render_prefix(){echo '<table>';} | ||
function render_cols(){ | ||
foreach($this->columns as $column){ | ||
if(!($this->do_callback("render_{$column}_col") | ||
|| $this->do_callback("render_col", $column))) | ||
foreach($this->columns as $props){ | ||
if(!$this->done_by_callback("col", $props)) | ||
echo '<col/>'; | ||
} | ||
} | ||
function render_head(){ | ||
echo '<thead><tr>'; | ||
foreach($this->columns as $column){ | ||
if(!($this->do_callback("render_{$column}_header") | ||
|| $this->do_callback("render_header", $column))) | ||
echo "<th>$column</th>"; | ||
foreach($this->columns as $props){ | ||
if(!$this->done_by_callback("header", $props)) | ||
echo "<th>".array_try($props, 'title', $props['column'])."</th>"; | ||
} | ||
echo '</tr></thead>'; | ||
} | ||
function render_body(){ | ||
echo '<tbody>'; | ||
echo "\n<tbody>\n"; | ||
foreach($this->rows as $row){ | ||
if(!$this->do_callback("render_row_prefix") echo '<tr>'; | ||
foreach($this->columns as $column){ | ||
if(!($this->do_callback("render_{$column}_cell" $row) | ||
|| $this->do_callback("render_cell", $column, $row))) | ||
echo "<td>{$row[$column]}</td>"; | ||
echo '<tr>'; | ||
foreach($this->columns as $props){ | ||
if(!($this->do_callback("render_{$props['column']}_cell", $props, $row) | ||
|| $this->do_callback("render_".array_try($props, 'type', 'string')."_cell", $props, $row) | ||
|| $this->do_callback("render_cell", $props, $row))) | ||
echo "<td>".$row[$props['column']]."</td>"; | ||
} | ||
if(!$this->do_callback("render_row_suffix") echo '</tr>'; | ||
echo "</tr>\n"; | ||
} | ||
echo '</tr></thead>'; | ||
echo "</tbody>\n"; | ||
} | ||
|
||
function render_icon_cell($props, $row){ | ||
echo "<td>".linkify( | ||
iconify(array_try($props, 'icon_name', $props['column'])), | ||
$props['url'], | ||
array(array_try($props, 'link_as', 'id') | ||
=> $row[array_try($props, 'link_field', 'id')] | ||
))."</td>"; | ||
} | ||
|
||
function render_link_cell($props, $row){ | ||
echo "<td>".linkify($row[$props['column']], $props['url'], | ||
array(array_try($props, 'link_as', 'id') | ||
=> $row[array_try($props, 'link_field', 'id')] | ||
))."</td>"; | ||
} | ||
function render_suffix(){echo '</table>';} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<? | ||
function t_string($column, $title = null){ | ||
return array('column' => $column, | ||
'title' => ($title ? $title : util_humanize($column)) | ||
); | ||
} | ||
|
||
function t_link($column, $url, $title = null){ | ||
return array_merge( | ||
array('type' => 'link', 'url' => $url), | ||
t_string($column, $title)); | ||
} | ||
|
||
function t_icon($column, $url, $title = null){ | ||
return array_merge( | ||
t_string($column), | ||
array('type' => 'icon', 'url' => $url, 'title' => ($title ? $title : ''))); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Oops, something went wrong.