Form.php 2.64 KB
Newer Older
doctrine's avatar
doctrine committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
<?php
class Doctrine_Form implements Iterator {
    protected $record;
    protected $elements = array();
    protected $columns;
    protected $current;
    protected $keys;
    protected $index;
    protected $count;
    public function __construct(Doctrine_Record $record) {
        $this->record = $record;
        $this->columns = $record->getTable()->getColumns();
        $this->keys    = array_keys($this->columns);
        $this->index   = 0;
        $this->count   = count($this->keys);
    }
    public function current() {
        $i = $this->index;
        $column = $this->keys[$i];

        $definitions = $this->columns[$column];

        $e    = explode("|",$definitions[2]);
doctrine's avatar
doctrine committed
24 25
        
        
doctrine's avatar
doctrine committed
26
        $enum = false;
doctrine's avatar
doctrine committed
27 28 29 30
        
        if($definitions[0] == "enum")
            $enum = $this->record->getTable()->getEnumValues($column);

doctrine's avatar
doctrine committed
31 32 33 34
        $length = $definitions[1];
        if( ! in_array("autoincrement",$e) && ! in_array("protected",$e)) {
            if($enum) {
                $elements[$column] = "<select name='data[$column]'>\n";
doctrine's avatar
doctrine committed
35
				$elements[$column] .= "	  <option value='-'>-</option>\n";
doctrine's avatar
doctrine committed
36
                foreach($enum as $k => $v) {
doctrine's avatar
doctrine committed
37
                    if($this->record->get($column) == $v) {
doctrine's avatar
doctrine committed
38 39 40 41
                        $str = 'selected';
                    } else
                        $str = '';

doctrine's avatar
doctrine committed
42
                    $elements[$column] .= "    <option value='$v' $str>$v</option>\n";
doctrine's avatar
doctrine committed
43 44 45 46 47
                }
                $elements[$column] .= "</select>\n";
            } else {
                if($length <= 255) {
                    $elements[$column] = "<input name='data[$column]' type='text' value='".$this->record->get($column)."' maxlength=$length \>\n";
48
                } elseif($length <= 4000) {
doctrine's avatar
doctrine committed
49
                    $elements[$column] = "<textarea name='data[$column]' cols=40 rows=10>".$this->record->get($column)."</textarea>\n";
50
                } else {
51
                    $elements[$column] = "<textarea name='data[$column]' cols=80 rows=25>".$this->record->get($column)."</textarea>\n";
doctrine's avatar
doctrine committed
52
                }
53
                
doctrine's avatar
doctrine committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
            }
            return $elements[$column];
        } else {
            $this->index++;

            if($this->index < $this->count)
                return self::current();
        }
    }
    public function key() {
        $i = $this->index;
        return $this->keys[$i];
    }
    public function next() {
        $this->index++;
    }
    public function rewind() {
        $this->index = 0;
    }
    public function valid() {
        if($this->index >= $this->count)
            return false;
            
        return true;
    }
}
80