From 7f22a44867fdf10d2383595e6467923fd11e2b89 Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 27 Jan 2015 15:32:51 -0500 Subject: Scaffolding 20% --- framework/Wsat/TWsatBaseGenerator.php | 109 +++++++-------- framework/Wsat/TWsatScaffoldingGenerator.php | 195 +++++++++++++++------------ framework/Wsat/pages/TWsatScaffolding.php | 6 +- 3 files changed, 172 insertions(+), 138 deletions(-) diff --git a/framework/Wsat/TWsatBaseGenerator.php b/framework/Wsat/TWsatBaseGenerator.php index 1facb13f..763836a3 100644 --- a/framework/Wsat/TWsatBaseGenerator.php +++ b/framework/Wsat/TWsatBaseGenerator.php @@ -14,66 +14,69 @@ Prado::using('System.Data.Common.TDbMetaData'); class TWsatBaseGenerator { - /** - * @return TDbMetaData for retrieving metadata information, such as - * table and columns information, from a database connection. - */ - protected $_dbMetaData; + /** + * @return TDbMetaData for retrieving metadata information, such as + * table and columns information, from a database connection. + */ + protected $_dbMetaData; - /** - * Output folder where AR classes will be saved. - */ - protected $_opFile; + /** + * Output folder where AR classes will be saved. + */ + protected $_opFile; - function __construct() - { - if (!class_exists("TActiveRecordManager", false)) - throw new Exception("You need to enable the ActiveRecord module in your application configuration file."); - $ar_manager = TActiveRecordManager::getInstance(); - $_conn = $ar_manager->getDbConnection(); - $_conn->Active = true; - $this->_dbMetaData = TDbMetaData::getInstance($_conn); - } + function __construct() + { + if (!class_exists("TActiveRecordManager", false)) + throw new Exception("You need to enable the ActiveRecord module in your application configuration file."); + $ar_manager = TActiveRecordManager::getInstance(); + $_conn = $ar_manager->getDbConnection(); + $_conn->Active = true; + $this->_dbMetaData = TDbMetaData::getInstance($_conn); + } - public function setOpFile($op_file_namespace) - { - $op_file = Prado::getPathOfNamespace($op_file_namespace); - if (empty($op_file)) - throw new Exception("You need to fix your output folder namespace."); - if (!is_dir($op_file)) - mkdir($op_file, 0777, true); - $this->_opFile = $op_file; - } + public function setOpFile($op_file_namespace) + { + $op_file = Prado::getPathOfNamespace($op_file_namespace); + if (empty($op_file)) + throw new Exception("You need to fix your output folder namespace."); + if (!is_dir($op_file)) + mkdir($op_file, 0777, true); + $this->_opFile = $op_file; + } - public function renderAllTablesInformation() + public function renderAllTablesInformation() + { + foreach ($this->getAllTableNames() as $table_name) { - foreach ($this->getAllTableNames() as $table_name) - { - echo $table_name . "
"; - $tableInfo = $this->_dbMetaData->getTableInfo($table_name); - echo "Table info:" . "
"; - echo "
";
-                        print_r($tableInfo);
-                        echo "
"; - } + echo $table_name . "
"; + $tableInfo = $this->_dbMetaData->getTableInfo($table_name); + echo "Table info:" . "
"; + echo "
";
+            print_r($tableInfo);
+            echo "
"; } + } - public function getAllTableNames() - { - $tableNames = $this->_dbMetaData->findTableNames(); - $index = array_search('pradocache', $tableNames); - if($index) - array_splice($tableNames, $index, 1); - return $tableNames; - } + public function getAllTableNames() + { + $tableNames = $this->_dbMetaData->findTableNames(); + $index = array_search('pradocache', $tableNames); + if ($index) + array_splice($tableNames, $index, 1); + return $tableNames; + } - public static function pr($data) - { - echo "
";
-                print_r($data);
-                echo "
"; - } + public static function pr($data) + { + echo "
";
+        print_r($data);
+        echo "
"; + } -} + protected function eq($data) + { + return '"' . $data . '"'; + } -?> +} diff --git a/framework/Wsat/TWsatScaffoldingGenerator.php b/framework/Wsat/TWsatScaffoldingGenerator.php index b1c54bc3..9e1e17ad 100644 --- a/framework/Wsat/TWsatScaffoldingGenerator.php +++ b/framework/Wsat/TWsatScaffoldingGenerator.php @@ -13,110 +13,139 @@ Prado::using("System.Wsat.TWsatBaseGenerator"); class TWsatScaffoldingGenerator extends TWsatBaseGenerator { - /** - * Const View Types for generation - */ - const LIST_TYPE = 0; - const ADD_TYPE = 1; - const SHOW_TYPE = 2; - - /** - * Bootstrap option - */ - private $_bootstrap; - - function __construct() + /** + * Const View Types for generation + */ + const LIST_TYPE = 0; + const ADD_TYPE = 1; + const SHOW_TYPE = 2; + + /** + * Bootstrap option + */ + private $_bootstrap; + + function __construct() + { + parent::__construct(); + } + + /** + * Generates CRUD Operations for a single DB table + * @param type $tableName + */ + public function generateCRUD($tableName) + { + $this->generate($tableName, self::ADD_TYPE); + $this->generate($tableName, self::LIST_TYPE); + $this->generate($tableName, self::SHOW_TYPE); + } + + //--------------------------------------------------------------------- + // + public function generate($tableName, $viewType) + { + switch ($viewType) { - parent::__construct(); + default: + case self::LIST_TYPE: + $unitName = "list" . ucfirst($tableName); + break; + + case self::ADD_TYPE: + $unitName = "add" . ucfirst($tableName); + break; + + case self::SHOW_TYPE: + $unitName = "show" . ucfirst($tableName); + break; } - public function generate() - { - $unitName = "Example"; - $class = $this->generateClass($unitName); - $outputClass = $this->_opFile . DIRECTORY_SEPARATOR . $unitName . ".php"; - file_put_contents($outputClass, $class); - - $outputPage = $this->_opFile . DIRECTORY_SEPARATOR . $unitName . ".page"; - $page = $this->generatePage("student", self::ADD_TYPE); - file_put_contents($outputPage, $page); - } + $class = $this->generateClass($unitName); + $outputClass = $this->_opFile . DIRECTORY_SEPARATOR . $unitName . ".php"; + file_put_contents($outputClass, $class); - // - private function generatePage($tableName, $type, $tContentId = "Content") - { - $pageContent = $this->getPageContent($tableName, $type); - return <<_opFile . DIRECTORY_SEPARATOR . $unitName . ".page"; + $page = $this->generatePage($tableName, $viewType); + file_put_contents($outputPage, $page); + } + + private function generatePage($tableName, $type, $tContentId = "Content") + { + $pageContent = $this->getPageContent($tableName, $type); + return << $pageContent EOD; - } + } - private function getPageContent($tableName, $type) + private function getPageContent($tableName, $type) + { + $code = ""; + $tableInfo = $this->_dbMetaData->getTableInfo($tableName); + switch ($type) { - $code = ""; - $tableInfo = $this->_dbMetaData->getTableInfo($tableName); - switch ($type) + case self::LIST_TYPE: + break; + case self::ADD_TYPE: + foreach ($tableInfo->getColumns() as $colField => $colMetadata) + { + if (!$colMetadata->IsPrimaryKey && !$colMetadata->IsForeignKey) + { + $code .= $this->generateControl($colMetadata); + $code .= $this->generateValidators($colMetadata); + $code .= "\n"; + } + } + foreach ($tableInfo->getForeignKeys() as $colField => $colMetadata) { - case self::LIST_TYPE: - break; - case self::ADD_TYPE: - foreach ($tableInfo->getColumns() as $colField => $colMetadata) - { - if (!$colMetadata->IsPrimaryKey && !$colMetadata->IsForeignKey) - { - $code .= $this->generateControl($colMetadata); - $code .= $this->generateValidators($colMetadata); - $code .= "\n"; - } - } - foreach ($tableInfo->getForeignKeys() as $colField => $colMetadata) - { - $colField = '"' . $colMetadata["table"] . '"'; - $code .= "\t\n"; - $code .= "\n"; - // TWsatBaseGenerator::pr($tableInfo); - } - - case self::SHOW_TYPE: - break; + $colField = $this->eq($colMetadata["table"]); + $code .= "\t\n"; + $code .= "\n"; + // TWsatBaseGenerator::pr($tableInfo); } - return $code; + $code .= "\t\n"; + + case self::SHOW_TYPE: + break; } + return $code; + } - private function generateControl($colMetadata) + private function generateControl($colMetadata) + { + $controlType = "TTextBox"; + switch ($colMetadata->DbType) { - $controlType = "TTextBox"; - switch ($colMetadata->DbType){ - - } - $controlId = $colMetadata->ColumnId; - return "\t\n"; + } - - private function generateValidators($colMetadata) + $controlId = $colMetadata->ColumnId; + return "\t\n"; + } + + private function generateValidators($colMetadata) + { + $controlId = $colMetadata->ColumnId; + $code = ""; + if (!$colMetadata->AllowNull) { - $controlId = $colMetadata->ColumnId; - $code = ""; - if (!$colMetadata->AllowNull) - { - $code .= "\t\n"; - } - return $code; + $code .= "\t\n"; } + return $code; + } // - //--------------------------------------------------------------------- - // - private function generateClass($classname) - { - $date = date('Y-m-d h:i:s'); - $env_user = getenv("username"); - return << + private function generateClass($classname) + { + $date = date('Y-m-d h:i:s'); + $env_user = getenv("username"); + return << } - -?> diff --git a/framework/Wsat/pages/TWsatScaffolding.php b/framework/Wsat/pages/TWsatScaffolding.php index 2dc7d288..aa7599ca 100644 --- a/framework/Wsat/pages/TWsatScaffolding.php +++ b/framework/Wsat/pages/TWsatScaffolding.php @@ -35,6 +35,10 @@ class TWsatScaffolding extends TPage } } + /** + * Generate Scaffolding code for selected tables + * @param type $sender + */ public function generate($sender) { if ($this->IsValid) @@ -43,7 +47,7 @@ class TWsatScaffolding extends TPage { $scf_generator = new TWsatScaffoldingGenerator(); $scf_generator->setOpFile($this->output_folder->Text); - $scf_generator->generate(); + $scf_generator->generateCRUD("estudiante"); $this->feedback_panel->CssClass = "green_panel"; $this->generation_msg->Text = "The code has been generated successfully."; } catch (Exception $ex) -- cgit v1.2.3