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         $core_opts = HTML_FlexyFramework::get()->page->opts;
58         if (!empty($core_opts['init']) && empty($core_opts['base'])) {
59             die("init must be used before base!");
60         }
61         
62         
63         
64     }
65     
66     
67     function get($k = '',$opt)
68     {
69         $k = strtolower($k);
70         
71         $this->opts = $opt;
72         
73         
74         
75         
76         
77         if($k == 'accnt'){
78             if(empty($opt['source'])){
79                 die("Missing Source directory for account json files! Try -f [JSON file path] \n");
80             }
81             
82             if (!file_exists($opt['source'])) {
83                 die("can not found account json file : {$opt['source']} \n");
84             }
85
86             $accounts = json_decode(file_get_contents($opt['source']),true);
87             
88             DB_DataObject::factory('accnt')->importFromArray($this, $accounts);
89             
90             die("DONE! \n");
91         }
92         
93         if($k == 'curr_symbol'){
94             if(!empty($opt['base'])){
95                 DB_DataObject::factory('curr_symbol')->setBase($this, $opt['base']);
96                 die("DONE! \n");
97             }
98             
99             if(!empty($opt['source'])){
100                 
101                 if (!file_exists($opt['source'])) {
102                     die("can not found currency json file : {$opt['source']} \n");
103                 }
104                 
105                 $currencies = json_decode(file_get_contents($opt['source']),true);
106                 
107                 DB_DataObject::factory('curr_symbol')->importFromArray($this, $currencies);
108                 die("DONE! \n");
109             }
110             
111             die("Missing Base Currency or Source directory for Currency json files! Try -b [base currency] or -f [JSON file path]\n");
112         }
113         
114         if($k == 'location'){
115             if(empty($opt['source'])){
116                 die("Missing Source directory for location json file! Try -f [JSON file path] \n");
117             }
118             
119             if (!file_exists($opt['source'])) {
120                 die("can not found location json file : {$opt['source']} \n");
121             }
122             
123             $locations = json_decode(file_get_contents($opt['source']),true);
124             
125             DB_DataObject::factory('location')->importFromArray($this, $locations);
126             
127             die("DONE! \n");
128         }
129         
130         if($k == 'terms'){
131             if(empty($opt['source'])){
132                 die("Missing Source directory for terms json file! Try -f [JSON file path] \n");
133             }
134             
135             if (!file_exists($opt['source'])) {
136                 die("can not found terms json file : {$opt['source']} \n");
137             }
138             
139             $terms = json_decode(file_get_contents($opt['source']),true);
140             
141             DB_DataObject::factory('terms')->importFromArray($this, $terms);
142             
143             die("DONE! \n");
144         }
145         
146         
147         $this->updateData($k);
148         
149     }
150     
151     
152     
153     
154     function updateData($k='') {
155         //DB_DataObject::debugLevel(1);
156         $tables = empty($k) ? array(
157                 
158 //                'curr_symbol', //?? fixme
159                 'custtype',
160                 'plancode',
161                 'prodcat',
162                 'classcode',
163                 'curr_rate',
164                 'char',
165                 'taxzone',
166                 'taxtype',
167                 'expcat',
168 //                'terms', //?? add
169                 
170 //                'location', //?? just check hat one exists.
171         ) : array($k);
172         
173         
174         $core_opts = HTML_FlexyFramework::get()->page->opts;
175         if (!empty($core_opts['init']) && empty($core_opts['base'])) {
176             die("init must be used before base!");
177         }
178         
179         
180         foreach($tables as $t) {
181             $cs = DB_DataObject::factory($t);
182             $cs->initDatabase($this);
183         
184         }
185         
186         // update data is called from main
187         
188         
189         
190         
191     }
192     
193     
194     
195 }