Skip to content

Commit

Permalink
many updates and added features
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Henzen committed Jul 13, 2009
1 parent 5ebae5d commit c24f742
Show file tree
Hide file tree
Showing 21 changed files with 307 additions and 85 deletions.
Empty file modified .gitignore 100644 → 100755
Empty file.
Empty file modified .htaccess 100644 → 100755
Empty file.
Empty file modified MIT-LICENSE 100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion README 100644 → 100755
Expand Up @@ -17,7 +17,7 @@ To set up a project using micro-framework you have to make a directory which con

* configure access with [project]/.htaccess and [project]/public/.htaccess

On windows: I have no idea
On windows: I have no idea [please help]

== Routing
All requests must be made to [project]/index.php. They are routed to a controller in [project]/controllers based on their url. For example, a request to [project]/index.php/static/index results in a call to the index method of a StaticController instance. A controller can use models in [project]/models to access a database. It can render a view from [project]/views/[controllername]/.
Expand Down
2 changes: 2 additions & 0 deletions boot.php 100644 → 100755
Expand Up @@ -4,6 +4,8 @@
require('utils.php');
require('model_utils.php');
require('html_utils.php');
require('html_form_helpers.php');
require('html_table_helpers.php');

// autoloading of classes
function __autoload($class_name) {
Expand Down
Empty file added config.php
Empty file.
Empty file modified controller.php 100644 → 100755
Empty file.
Empty file modified framework.php 100644 → 100755
Empty file.
75 changes: 61 additions & 14 deletions html_form.php 100644 → 100755
@@ -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);
}
}
19 changes: 19 additions & 0 deletions html_form_helpers.php
@@ -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));
}
80 changes: 58 additions & 22 deletions html_table.php 100644 → 100755
@@ -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>';}
}
18 changes: 18 additions & 0 deletions html_table_helpers.php
@@ -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 : '')));
}
45 changes: 31 additions & 14 deletions html_utils.php 100644 → 100755
Expand Up @@ -7,13 +7,14 @@ function html_dl($items, $attributes=array()){
}

function html_input($name, $value, $attributes=array()){
$attributes['name'] = $name;
$attributes['value'] = $value;
$attributes = array_merge(
array('name' => $name, 'value' => $value), $attributes);

return '<input type="text" '.xml_attributify($attributes).'>';
}

function html_password($name, $attributes=array()){
$attributes['name'] = $name;
$attributes = array_merge(array('name' => $name), $attributes);
return '<input type="password" '.xml_attributify($attributes).'>';
}

Expand All @@ -28,43 +29,59 @@ function html_select($name, $options, $selected=false, $attributes=array()){
if($selected && $selected == $key) $option_attributes['selected'] = 'selected';
$result[] = '<option '.xml_attributify($option_attributes).">$value</option>";
}
$attributes['name'] = $name;
$attributes = array_merge(array('name' => $name), $attributes);
return '<select '.xml_attributify($attributes).'>'.implode("\n", $result)."</select>";
}

function html_textarea($name, $value, $attributes = array()){
$attributes = array_merge(array(
'rows' => 10, 'cols'=>60, 'name'=> $name), $attributes);

return '<textarea '.xml_attributify($attributes).'>'.$value.'</textarea>';
}

function html_span($content, $attributes=array()){
return '<span '.xml_attributify($attributes).'>'.$content.'</span> ';
}

function html_form_open($url, $method='POST', $attributes=array()){
$attributes['action'] = $url;
$attributes['method'] = $method;
$attributes = array_merge(
array('action' => $url, 'method' => $method), $attributes);
return '<form '.xml_attributify($attributes).'>';
}
function html_label($for, $text, $attributes = array()){
$attributes = array_merge(
array('for' => $for), $attributes);
return '<label '.xml_attributify($attributes).'>'.$text.'</label> ';
}

function html_js_link($file, $attributes=array()){
if(substr($file, -3) != '.js') $file .= '.js';
$attributes['src'] = URL_BASE."/public/$file";
$attributes['type'] = 'text/javascript';
$attributes['charset'] = 'utf-8';
$attributes = array_merge(
array('src' => URL_BASE."/public/$file",
'type' => 'text/javascript',
'charset' => 'utf-8'), $attributes);

return '<script '.xml_attributify($attributes).'></script>';
}

function html_css_link($file, $attributes=array()){
if(substr($file, -4) != '.css') $file .= '.css';
$attributes['rel'] = "stylesheet";
$attributes['href'] = URL_BASE."/public/$file";
$attributes['type'] = "text/css";
$attributes = array_merge(array(
'rel' => "stylesheet",
'href' => URL_BASE."/public/$file",
'type' => "text/css"), $attributes);

return '<link '.xml_attributify($attributes).'/>';
}

function linkify($text, $path, $params = array(), $attributes = array()){
return "<a href=\"".urlify($path, $params)."\" ".attributify($attributes).">$text</a>";
return "<a href=\"".urlify($path, $params)."\" ".xml_attributify($attributes).">$text</a>";
}

function iconify($icon, $attributes=array()){
$path = URL_BASE."/public/icons/$icon.png";
$attributes['src'] = $path;
$attributes = array_merge(array('src' => $path), $attributes);
return '<img '.xml_attributify($attributes).'/>';
}

Expand Down
Empty file modified micro_object.php 100644 → 100755
Empty file.

0 comments on commit c24f742

Please sign in to comment.