5 * Setup the core values in the database
10 require_once 'Pman.php';
11 class Pman_Xtuple_UpdateDatabase extends Pman
14 static $cli_desc = "Update SQL - Beta";
16 static $cli_opts = array(
18 'desc' => 'Source directory for json files.',
28 'desc' => 'Base Currency',
36 'json-accnt' => array(
37 'desc' => 'Accounts JSON file',
43 'json-curr-symbol' => array(
44 'desc' => 'Currency Symbols JSON file',
51 'json-location' => array(
52 'desc' => 'Location JSON file',
58 'json-terms' => array(
59 'desc' => 'Terms JSON file',
72 $ff = HTML_FlexyFramework::get();
73 if (!empty($ff->cli)) {
80 // called to ensure options are correct.
81 // before any data is imported / started..
82 function checkOpts($opts)
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 ");
92 function loadFromFile($db, $file)
94 if (!file_exists($file)) {
95 die("can not found account json file :$file \n");
98 $accounts = json_decode(file_get_contents($opt['source']),true);
100 DB_DataObject::factory('accnt')->importFromArray($this, $accounts);
106 function get($k = '',$opt)
113 foreach($this->opts as $o=>$v) {
114 if (!empty(preg_match('/^json-/', $o)) {
117 $type = str_replace('_', '-', substr($o,6));
118 $this->loadFromFile($type, $v);
123 if(empty($opt['source'])){
124 die("Missing Source directory for account json files! Try -f [JSON file path] \n");
127 if (!file_exists($opt['source'])) {
128 die("can not found account json file : {$opt['source']} \n");
131 $accounts = json_decode(file_get_contents($opt['source']),true);
133 DB_DataObject::factory('accnt')->importFromArray($this, $accounts);
138 if($k == 'curr_symbol'){
139 if(!empty($opt['base'])){
140 DB_DataObject::factory('curr_symbol')->setBase($this, $opt['base']);
144 if(!empty($opt['source'])){
146 if (!file_exists($opt['source'])) {
147 die("can not found currency json file : {$opt['source']} \n");
150 $currencies = json_decode(file_get_contents($opt['source']),true);
152 DB_DataObject::factory('curr_symbol')->importFromArray($this, $currencies);
156 die("Missing Base Currency or Source directory for Currency json files! Try -b [base currency] or -f [JSON file path]\n");
159 if($k == 'location'){
160 if(empty($opt['source'])){
161 die("Missing Source directory for location json file! Try -f [JSON file path] \n");
164 if (!file_exists($opt['source'])) {
165 die("can not found location json file : {$opt['source']} \n");
168 $locations = json_decode(file_get_contents($opt['source']),true);
170 DB_DataObject::factory('location')->importFromArray($this, $locations);
176 if(empty($opt['source'])){
177 die("Missing Source directory for terms json file! Try -f [JSON file path] \n");
180 if (!file_exists($opt['source'])) {
181 die("can not found terms json file : {$opt['source']} \n");
184 $terms = json_decode(file_get_contents($opt['source']),true);
186 DB_DataObject::factory('terms')->importFromArray($this, $terms);
192 $this->updateData($k);
199 function updateData($k='') {
200 //DB_DataObject::debugLevel(1);
201 $tables = empty($k) ? array(
203 // 'curr_symbol', //?? fixme
215 // 'location', //?? just check hat one exists.
219 $core_opts = HTML_FlexyFramework::get()->page->opts;
220 if (!empty($core_opts['init'])) {
221 DB_DataObject::factory('curr_symbol')->setBase($this, $core_opt['base']);
227 foreach($tables as $t) {
228 $cs = DB_DataObject::factory($t);
229 $cs->initDatabase($this);
233 // update data is called from main