root/trunk/patForms/Creator/DB.php

Revision 307, 2.6 kB (checked in by schst, 3 years ago)

Updated doc blocks: removed @todo and added notice that this should not be used

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 <?php
2 /**
3  * patForms Creator DB
4  *
5  * This is an experiment and should not be used in
6  * production. The Creator classes will be replaced
7  * by the Definition classes in v1.1.0.
8  *
9  * $Id$
10  *
11  * @package        patForms
12  * @subpackage    Creator
13  * @deprecated
14  */
15
16 /**
17  * PEAR::DB class
18  */
19  require_once 'DB.php';
20
21 /**
22  * Error: could not connect to the database
23  */
24  define( 'PATFORMS_CREATOR_DB_ERROR_NO_CONNECTION', 'patForms:Creator:DB:01' );
25  
26 /**
27  * patForms Creator DB
28  *
29  * This is an experiment and should not be used in
30  * production. The Creator classes will be replaced
31  * by the Definition classes in v1.1.0.
32  *
33  * $Id$
34  *
35  * @deprecated
36  * @package        patForms
37  * @subpackage    Creator
38  * @author        Stephan Schmidt <schst@php-tools.net>
39  * @license        LGPL, see license.txt for details
40  * @link        http://www.php-tools.net
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 ?>
Note: See TracBrowser for help on using the browser.