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         'json-accnt' => array(
37             'desc' => 'Accounts JSON file',
38             'default' => '',
39             'min' => 1,
40             'max' => 1,
41             
42         ),
43         'json-curr-symbol' => array(
44             'desc' => 'Currency Symbols JSON file',
45             'default' => '',
46             'min' => 1,
47             'max' => 1,
48             
49         ),
50
51         'json-location' => array(
52             'desc' => 'Location JSON file',
53             'default' => '',
54             'min' => 1,
55             'max' => 1,
56             
57         ),
58         'json-terms' => array(
59             'desc' => 'Terms  JSON file',
60             'default' => '',
61             'min' => 1,
62             'max' => 1,
63             
64         ),
65         
66     );
67     
68     var $cli = false; 
69     function getAuth() {
70         
71         
72         $ff = HTML_FlexyFramework::get();
73         if (!empty($ff->cli)) {
74             $this->cli = true;
75             return true;
76         }
77         die("NOT ALLOWED");
78     }
79     
80     // called to ensure options are correct.
81     // before any data is imported / started..
82     function checkOpts($opts)
83     {
84         $opts = HTML_FlexyFramework::get()->page->opts;
85         if (!empty($opts['init']) && empty($opts['base'])) {
86             die("--base=XXX  must be set when using --init ");
87         }
88         
89        
90         
91         
92     }
93     function importFromArray($db, $file)
94     {
95         $data= json_decode(file_get_contents($file),true);
96         DB_DataObject::factory('$db')->importFromArray($this);
97     }
98     
99     
100     function get($k = '',$opt)
101     {
102         $k = strtolower($k);
103         
104         $this->opts = $opt;
105         
106         require_once 'Pman/Core/UpdateDatabase.php';
107         foreach($this->opts as $o=>$v) {
108             if (!preg_match('/^json-/', $o) || empty($v)) {
109                 continue;
110             }
111             $type = str_replace('_', '-', substr($o,6));
112             $this->importFromArray($type, $v);
113         }
114         
115          
116         
117           
118         
119         $this->updateData($k);
120         
121     }
122     
123     
124     
125     
126     function updateData($k='') {
127         //DB_DataObject::debugLevel(1);
128         $tables = empty($k) ? array(
129                 
130 //                'curr_symbol', //?? fixme
131                 'custtype',
132                 'plancode',
133                 'prodcat',
134                 'classcode',
135                 'curr_rate',
136                 'char',
137                 'taxzone',
138                 'taxtype',
139                 'expcat',
140 //                'terms', //?? add
141                 
142 //                'location', //?? just check hat one exists.
143         ) : array($k);
144         
145         
146         $core_opts = HTML_FlexyFramework::get()->page->opts;
147         if (!empty($core_opts['init'])) {
148             DB_DataObject::factory('curr_symbol')->setBase($this, $core_opt['base']);
149         }
150         
151         
152         
153         
154         foreach($tables as $t) {
155             $cs = DB_DataObject::factory($t);
156             $cs->initDatabase($this);
157         
158         }
159         
160         // update data is called from main
161         
162         
163         
164         
165     }
166     
167     
168     
169 }