PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/inc/patForms/Creator/DB.php

https://github.com/chregu/fluxcms
PHP | 115 lines | 67 code | 14 blank | 34 comment | 7 complexity | 20752f6977746fdd5ef1cf1db6b54ece MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, Apache-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * patForms Creator DB
  4. *
  5. * $Id$
  6. *
  7. * @package patForms
  8. * @subpackage Creator
  9. */
  10. /**
  11. * PEAR::DB class
  12. */
  13. require_once 'DB.php';
  14. /**
  15. * Error: could not connect to the database
  16. */
  17. define( 'PATFORMS_CREATOR_DB_ERROR_NO_CONNECTION', 'patForms:Creator:DB:01' );
  18. /**
  19. * patForms Creator DB
  20. *
  21. * @access protected
  22. * @package patForms
  23. * @subpackage Creator
  24. * @author Stephan Schmidt <schst@php-tools.net>
  25. * @license LGPL, see license.txt for details
  26. * @link http://www.php-tools.net
  27. */
  28. class patForms_Creator_DB extends patForms_Creator
  29. {
  30. /**
  31. * Create a form from a database
  32. *
  33. * @access public
  34. * @param mixed $db Either a dsn or an existing DB object
  35. * @param mixed $source Either a table name or a result
  36. * @param array $options Any options the creator may need
  37. * @return object $form The patForms object, or a patError object on failure.
  38. * @todo build code generator
  39. */
  40. function &create( $db, $source, $options = array() )
  41. {
  42. if( is_string( $db ) )
  43. {
  44. $db =& DB::connect( $db );
  45. if( DB::isError( $db ) )
  46. {
  47. return patErrorManager::raiseError(
  48. PATFORMS_CREATOR_DB_ERROR_NO_CONNECTION,
  49. 'Could not connect to the specified database.',
  50. $db->getMessage()." >> ".$db->getUserInfo()
  51. );
  52. }
  53. }
  54. $form =& patForms::createForm( null, array( 'name' => 'patForms_Creator_Form' ) );
  55. $info = $db->tableInfo( $source );
  56. $cntElements = count( $info );
  57. for( $i = 0; $i < $cntElements; $i++ )
  58. {
  59. $info[$i]['flags'] = explode( ' ', $info[$i]['flags'] );
  60. $attribs = array(
  61. 'edit' => 'yes',
  62. 'title' => $info[$i]['name'],
  63. 'label' => $info[$i]['name'],
  64. 'name' => $info[$i]['name']
  65. );
  66. switch( strtolower( $info[$i]['type'] ) )
  67. {
  68. case 'int':
  69. $type = 'Number';
  70. break;
  71. case 'datetime':
  72. $type = 'Number';
  73. break;
  74. case 'float':
  75. $type = 'Number';
  76. break;
  77. case 'string':
  78. $type = 'String';
  79. $attribs['maxlength'] = $info[$i]['len'];
  80. break;
  81. default:
  82. $type = 'String';
  83. break;
  84. }
  85. if( in_array( 'not_null', $info[$i]['flags'] ) )
  86. {
  87. $attribs['required'] = 'yes';
  88. }
  89. if( $type == 'Number' )
  90. {
  91. if( in_array( 'unsigned', $info[$i]['flags'] ) )
  92. {
  93. $attribs['min'] = 0;
  94. }
  95. }
  96. $form->addElement( $form->createElement( $info[$i]['name'], $type, $attribs ) );
  97. }
  98. return $form;
  99. }
  100. }
  101. ?>