summaryrefslogtreecommitdiff
path: root/framework/Wsat/TWsatScaffoldingGenerator.php
blob: 1490f23e063d6160bd175690d2e5685a60942e9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php

/**
 * @author Daniel Sampedro Bello <darthdaniel85@gmail.com>
 * @link https://github.com/pradosoft/prado
 * @copyright Copyright &copy; 2005-2015 The PRADO Group
 * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
 * @version $Id$
 * @since 4.0
 * @package System.Wsat
 */
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()
    {
        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);
    }

    //---------------------------------------------------------------------
    // <editor-fold defaultstate="collapsed" desc="Page Generation">
    public function generate($tableName, $viewType)
    {
        switch ($viewType)
        {
            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;
        }

        $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($tableName, $viewType);
        file_put_contents($outputPage, $page);
    }

    private function generatePage($tableName, $type, $tContentId = "Content")
    {
        $pageContent = $this->getPageContent($tableName, $type);
        return <<<EOD
<com:TContent ID="$tContentId">   
                     
       $pageContent
               
</com:TContent>
EOD;
    }

    private function getPageContent($tableName, $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)
                {
                    $colField = $this->eq($colMetadata["table"]);
                    $code .= "\t<com:TTextBox ID=$colField />\n";
                    $code .= "\n";
                    //  TWsatBaseGenerator::pr($tableInfo);
                }
                $code .= "\t<com:TButton Text=\"Accept\" />\n";

            case self::SHOW_TYPE:
                break;
        }
        return $code;
    }

    private function generateControl($colMetadata)
    {
        $controlType = "TTextBox";
        switch ($colMetadata->DbType)
        {
            
        }
        $controlId = $colMetadata->ColumnId;
        return "\t<com:$controlType ID=\"$controlId\" />\n";
    }

    private function generateValidators($colMetadata)
    {
        $controlId = $colMetadata->ColumnId;
        $code = "";
        if (!$colMetadata->AllowNull)
        {
            $code .= "\t<com:TRequiredFieldValidator ControlToValidate=$controlId ValidationGroup=\"addGroup\" Text=\"Field $controlId is required.\" Display=\"Dynamic\" />\n";
        }
        return $code;
    }

// </editor-fold>
    //---------------------------------------------------------------------
    // <editor-fold defaultstate="collapsed" desc="Code Behind Generation">
    private function generateClass($classname)
    {
        $date = date('Y-m-d h:i:s');
        $env_user = getenv("username");
        return <<<EOD
<?php
/**
 * Auto generated by PRADO - WSAT on $date.
 * @author $env_user
 */
class $classname extends TPage
{

}
EOD;
    }

// </editor-fold>
}