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 ");
89 foreach($opts as $o=>$v) {
90 if (preg_match('/^json-/', $o) && !empty($v)) {
93 if (!file_exists($v)) {
94 die("File does not exist : OPTION --{$o} = {$v} \n");
100 function loadFromFile($db, $file)
104 $accounts = json_decode(file_get_contents($file),true);
106 DB_DataObject::factory('accnt')->importFromArray($this, $accounts);
114 function get($k = '',$opt)
121 foreach($this->opts as $o=>$v) {
122 if (!empty(preg_match('/^json-/', $o)) {
125 $type = str_replace('_', '-', substr($o,6));
126 $this->loadFromFile($type, $v);
131 if(empty($opt['source'])){
132 die("Missing Source directory for account json files! Try -f [JSON file path] \n");
135 if (!file_exists($opt['source'])) {
136 die("can not found account json file : {$opt['source']} \n");
139 $accounts = json_decode(file_get_contents($opt['source']),true);
141 DB_DataObject::factory('accnt')->importFromArray($this, $accounts);
146 if($k == 'curr_symbol'){
147 if(!empty($opt['base'])){
148 DB_DataObject::factory('curr_symbol')->setBase($this, $opt['base']);
152 if(!empty($opt['source'])){
154 if (!file_exists($opt['source'])) {
155 die("can not found currency json file : {$opt['source']} \n");
158 $currencies = json_decode(file_get_contents($opt['source']),true);
160 DB_DataObject::factory('curr_symbol')->importFromArray($this, $currencies);
164 die("Missing Base Currency or Source directory for Currency json files! Try -b [base currency] or -f [JSON file path]\n");
167 if($k == 'location'){
168 if(empty($opt['source'])){
169 die("Missing Source directory for location json file! Try -f [JSON file path] \n");
172 if (!file_exists($opt['source'])) {
173 die("can not found location json file : {$opt['source']} \n");
176 $locations = json_decode(file_get_contents($opt['source']),true);
178 DB_DataObject::factory('location')->importFromArray($this, $locations);
184 if(empty($opt['source'])){
185 die("Missing Source directory for terms json file! Try -f [JSON file path] \n");
188 if (!file_exists($opt['source'])) {
189 die("can not found terms json file : {$opt['source']} \n");
192 $terms = json_decode(file_get_contents($opt['source']),true);
194 DB_DataObject::factory('terms')->importFromArray($this, $terms);
200 $this->updateData($k);
207 function updateData($k='') {
208 //DB_DataObject::debugLevel(1);
209 $tables = empty($k) ? array(
211 // 'curr_symbol', //?? fixme
223 // 'location', //?? just check hat one exists.
227 $core_opts = HTML_FlexyFramework::get()->page->opts;
228 if (!empty($core_opts['init'])) {
229 DB_DataObject::factory('curr_symbol')->setBase($this, $core_opt['base']);
235 foreach($tables as $t) {
236 $cs = DB_DataObject::factory($t);
237 $cs->initDatabase($this);
241 // update data is called from main