1<?php
2# $Id$
3/**
4 * class to display the database scheme (for usage in upgrade.php) in Cli
5 *
6 * extends the "Shell" class
7 */
8
9class CliScheme extends Shell
10{
11    public $handler_to_use = "";
12    public $new = 0;
13
14
15    /**
16    * Execution method always used for tasks
17    */
18    public function execute()
19    {
20        $module = preg_replace('/Handler$/', '', $this->handler_to_use);
21        $module = strtolower($module);
22
23        $handler =  new $this->handler_to_use($this->new);
24        $struct = $handler->getStruct();
25
26        foreach (array_keys($struct) as $field) {
27            if ($field == 'created') {
28                $struct[$field]['db_code'] = '{DATE}';
29            } elseif ($field == 'modified') {
30                $struct[$field]['db_code'] = '{DATECURRENT}';
31            } else {
32                switch ($struct[$field]['type']) {
33                    case 'int':
34                        $struct[$field]['db_code'] = '{BIGINT}';
35                        break;
36                    case 'bool':
37                        $struct[$field]['db_code'] = '{BOOLEAN}';
38                        break;
39                    default:
40                        $struct[$field]['db_code'] = 'VARCHAR(255) {LATIN1} NOT NULL';
41                }
42            }
43        }
44
45        $this->out("For creating a new table with upgrade.php:");
46        $this->out("");
47
48        $this->out('db_query_parsed("');
49        $this->out('    CREATE TABLE {IF_NOT_EXISTS} " . table_by_key("' . $module . '") . " (');
50        # TODO: $module is not really correct - $handler->db_table would be
51
52        foreach (array_keys($struct) as $field) {
53            if ($struct[$field]['not_in_db'] == 0 && $struct[$field]['dont_write_to_db'] == 0) {
54                $this->out("        $field " . $struct[$field]['db_code'] . ",");
55            }
56        }
57
58        $this->out("        INDEX domain(domain,username), // <--- change as needed");
59        $this->out("        PRIMARY KEY (" . $handler->getId_field() . ")");
60        $this->out('    ) {MYISAM} ');
61        $this->out('");');
62
63        $this->out('');
64        $this->hr();
65        $this->out('For adding fields with upgrade.php:');
66        $this->out('');
67
68        $prev_field = '';
69        foreach (array_keys($struct) as $field) {
70            if ($struct[$field]['not_in_db'] == 0 && $struct[$field]['dont_write_to_db'] == 0) {
71                $this->out("        _db_add_field('$module', '$field',\t'" . $struct[$field]['db_code'] . "',\t'$prev_field');");
72                $prev_field = $field;
73            }
74        }
75
76        $this->out('');
77        $this->hr();
78        $this->out('Note that the above is only a template.');
79        $this->out('You might need to adjust some parts.');
80        return 0;
81    }
82
83    /**
84    * Displays help contents
85    */
86    public function help()
87    {
88        $module = preg_replace('/Handler$/', '', $this->handler_to_use);
89        $module = strtolower($module);
90
91        $this->out(
92"Usage:
93
94    postfixadmin-cli $module scheme
95
96        Print the $module database scheme in a way that can be
97        pasted into upgrade.php.
98
99"
100        );
101
102        $this->_stop(1);
103    }
104}
105/* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */
106