UpdateDatabase.php
[Pman.Xtuple] / UpdateDatabase.php
1 <?php
2
3 /**
4  *
5  * Setup the core values in the database
6  *
7  *
8  */
9
10 require_once 'Pman.php';
11 class Pman_Xtuple_UpdateDatabase extends Pman
12 {
13     
14     static $cli_desc = "Update SQL - Beta";
15     
16     static $cli_opts = array(
17         'source' => array(
18             'desc' => 'Source directory for json files.',
19             'short' => 'f',
20             'default' => '',
21             'min' => 1,
22             'max' => 1,
23         ),
24         
25         
26         
27         'base' => array(
28             'desc' => 'Base Currency',
29             'default' => '',
30             'short' => 'b',
31             'min' => 1,
32             'max' => 1,
33             
34         ),
35  
36
37         
38         
39     );
40     
41     var $cli = false; 
42     function getAuth() {
43         
44         
45         $ff = HTML_FlexyFramework::get();
46         if (!empty($ff->cli)) {
47             $this->cli = true;
48             return true;
49         }
50         die("NOT ALLOWED");
51     }
52     
53     // called to ensure options are correct.
54     // before any data is imported / started..
55     function checkOpts()
56     {
57         
58         
59         
60     }
61     
62     
63     function get($k = '',$opt)
64     {
65         $k = strtolower($k);
66         
67         $this->opts = $opt;
68         
69         
70         
71         
72         
73         if($k == 'accnt'){
74             if(empty($opt['source'])){
75                 die("Missing Source directory for account json files! Try -f [JSON file path] \n");
76             }
77             
78             if (!file_exists($opt['source'])) {
79                 die("can not found account json file : {$opt['source']} \n");
80             }
81
82             $accounts = json_decode(file_get_contents($opt['source']),true);
83             
84             DB_DataObject::factory('accnt')->importFromArray($this, $accounts);
85             
86             die("DONE! \n");
87         }
88         
89         if($k == 'curr_symbol'){
90             if(!empty($opt['base'])){
91                 DB_DataObject::factory('curr_symbol')->setBase($this, $opt['base']);
92                 die("DONE! \n");
93             }
94             
95             if(!empty($opt['source'])){
96                 
97                 if (!file_exists($opt['source'])) {
98                     die("can not found currency json file : {$opt['source']} \n");
99                 }
100                 
101                 $currencies = json_decode(file_get_contents($opt['source']),true);
102                 
103                 DB_DataObject::factory('curr_symbol')->importFromArray($this, $currencies);
104                 die("DONE! \n");
105             }
106             
107             die("Missing Base Currency or Source directory for Currency json files! Try -b [base currency] or -f [JSON file path]\n");
108         }
109         
110         if($k == 'location'){
111             if(empty($opt['source'])){
112                 die("Missing Source directory for location json file! Try -f [JSON file path] \n");
113             }
114             
115             if (!file_exists($opt['source'])) {
116                 die("can not found location json file : {$opt['source']} \n");
117             }
118             
119             $locations = json_decode(file_get_contents($opt['source']),true);
120             
121             DB_DataObject::factory('location')->importFromArray($this, $locations);
122             
123             die("DONE! \n");
124         }
125         
126         if($k == 'terms'){
127             if(empty($opt['source'])){
128                 die("Missing Source directory for terms json file! Try -f [JSON file path] \n");
129             }
130             
131             if (!file_exists($opt['source'])) {
132                 die("can not found terms json file : {$opt['source']} \n");
133             }
134             
135             $terms = json_decode(file_get_contents($opt['source']),true);
136             
137             DB_DataObject::factory('terms')->importFromArray($this, $terms);
138             
139             die("DONE! \n");
140         }
141         
142         
143         $this->updateData($k);
144         
145     }
146     
147     
148     
149     
150     function updateData($k='') {
151         //DB_DataObject::debugLevel(1);
152         $tables = empty($k) ? array(
153                 
154 //                'curr_symbol', //?? fixme
155                 'custtype',
156                 'plancode',
157                 'prodcat',
158                 'classcode',
159                 'curr_rate',
160                 'char',
161                 'taxzone',
162                 'taxtype',
163                 'expcat',
164 //                'terms', //?? add
165                 
166 //                'location', //?? just check hat one exists.
167         ) : array($k);
168         
169         
170         $core_opts = HTML_FlexyFramework::get()->page->opts;
171         if (!empty($core_opts['init']) && empty($core_opts['base'])) {
172             die("init must be used before base!");
173         }
174         
175         
176         foreach($tables as $t) {
177             $cs = DB_DataObject::factory($t);
178             $cs->initDatabase($this);
179         
180         }
181         
182         // update data is called from main
183         
184         
185         
186         
187     }
188     
189     
190     
191 }