| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
require_once 'DB.php'; |
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
define( 'PATFORMS_CREATOR_DB_ERROR_NO_CONNECTION', 'patForms:Creator:DB:01' ); |
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
class patForms_Creator_DB extends patForms_Creator |
|---|
| 43 |
{ |
|---|
| 44 |
|
|---|
| 45 |
* Create a form from a database |
|---|
| 46 |
* |
|---|
| 47 |
* @access public |
|---|
| 48 |
* @param mixed $db Either a dsn or an existing DB object |
|---|
| 49 |
* @param mixed $source Either a table name or a result |
|---|
| 50 |
* @param array $options Any options the creator may need |
|---|
| 51 |
* @return object $form The patForms object, or a patError object on failure. |
|---|
| 52 |
*/ |
|---|
| 53 |
function &create( $db, $source, $options = array() ) |
|---|
| 54 |
{ |
|---|
| 55 |
if( is_string( $db ) ) |
|---|
| 56 |
{ |
|---|
| 57 |
$db =& DB::connect( $db ); |
|---|
| 58 |
|
|---|
| 59 |
if( DB::isError( $db ) ) |
|---|
| 60 |
{ |
|---|
| 61 |
return patErrorManager::raiseError( |
|---|
| 62 |
PATFORMS_CREATOR_DB_ERROR_NO_CONNECTION, |
|---|
| 63 |
'Could not connect to the specified database.', |
|---|
| 64 |
$db->getMessage()." >> ".$db->getUserInfo() |
|---|
| 65 |
); |
|---|
| 66 |
} |
|---|
| 67 |
} |
|---|
| 68 |
|
|---|
| 69 |
$form =& patForms::createForm( null, array( 'name' => 'patForms_Creator_Form' ) ); |
|---|
| 70 |
|
|---|
| 71 |
$info = $db->tableInfo( $source ); |
|---|
| 72 |
|
|---|
| 73 |
$cntElements = count( $info ); |
|---|
| 74 |
for( $i = 0; $i < $cntElements; $i++ ) |
|---|
| 75 |
{ |
|---|
| 76 |
$info[$i]['flags'] = explode( ' ', $info[$i]['flags'] ); |
|---|
| 77 |
|
|---|
| 78 |
$attribs = array( |
|---|
| 79 |
'edit' => 'yes', |
|---|
| 80 |
'title' => $info[$i]['name'], |
|---|
| 81 |
'label' => $info[$i]['name'], |
|---|
| 82 |
'name' => $info[$i]['name'] |
|---|
| 83 |
); |
|---|
| 84 |
|
|---|
| 85 |
switch( strtolower( $info[$i]['type'] ) ) |
|---|
| 86 |
{ |
|---|
| 87 |
case 'int': |
|---|
| 88 |
$type = 'Number'; |
|---|
| 89 |
break; |
|---|
| 90 |
case 'datetime': |
|---|
| 91 |
$type = 'Number'; |
|---|
| 92 |
break; |
|---|
| 93 |
case 'float': |
|---|
| 94 |
$type = 'Number'; |
|---|
| 95 |
break; |
|---|
| 96 |
case 'string': |
|---|
| 97 |
$type = 'String'; |
|---|
| 98 |
$attribs['maxlength'] = $info[$i]['len']; |
|---|
| 99 |
break; |
|---|
| 100 |
default: |
|---|
| 101 |
$type = 'String'; |
|---|
| 102 |
break; |
|---|
| 103 |
} |
|---|
| 104 |
|
|---|
| 105 |
if( in_array( 'not_null', $info[$i]['flags'] ) ) |
|---|
| 106 |
{ |
|---|
| 107 |
$attribs['required'] = 'yes'; |
|---|
| 108 |
} |
|---|
| 109 |
|
|---|
| 110 |
if( $type == 'Number' ) |
|---|
| 111 |
{ |
|---|
| 112 |
if( in_array( 'unsigned', $info[$i]['flags'] ) ) |
|---|
| 113 |
{ |
|---|
| 114 |
$attribs['min'] = 0; |
|---|
| 115 |
} |
|---|
| 116 |
} |
|---|
| 117 |
|
|---|
| 118 |
$form->addElement( $form->createElement( $info[$i]['name'], $type, $attribs ) ); |
|---|
| 119 |
} |
|---|
| 120 |
|
|---|
| 121 |
return $form; |
|---|
| 122 |
} |
|---|
| 123 |
|
|---|
| 124 |
} |
|---|
| 125 |
?> |
|---|