Example #1
0
 /**
  * IMPORTANT: This function is not required for setup. It just imports *.json -> DB
  *
  * imports wormhole static data for "shattered" systems
  * into table "system_wormhole"
  * -> a *.csv dump of this *.json file can e found under /export/csv
  * @param \Base $f3
  * @throws \Exception
  */
 protected function importSystemWormholesFromJson(\Base $f3)
 {
     $path = $f3->get('EXPORT') . 'json/statics.json';
     $pfDB = $this->getDB('PF');
     $ccpDB = $this->getDB('CCP');
     $content = file_get_contents($path);
     $jsonIterator = new \RecursiveIteratorIterator(new \RecursiveArrayIterator(json_decode($content, TRUE)), \RecursiveIteratorIterator::SELF_FIRST);
     $staticNames = [];
     $data = [];
     $tmpVal = (object) [];
     foreach ($jsonIterator as $key => $val) {
         if (is_array($val)) {
             if (isset($tmpVal->name)) {
                 $data[] = $tmpVal;
             }
             $tmpVal = (object) [];
             $tmpVal->name = $key;
         } else {
             $tmpVal->wh = isset($tmpVal->wh) ? array_merge($tmpVal->wh, [$val]) : [$val];
             $staticNames[] = $val;
         }
     }
     $data[] = $tmpVal;
     // get static IDs by name ------------------------------
     $staticNames = array_unique($staticNames);
     $staticNames = array_flip($staticNames);
     foreach ($staticNames as $name => $index) {
         $result = $pfDB->exec("\n                            SELECT\n                              id\n                            FROM " . $pfDB->quotekey(Model\BasicModel::getNew('WormholeModel')->getTable()) . "\n                            WHERE " . $pfDB->quotekey('name') . " = :name", [':name' => $name]);
         $id = (int) $result[0]['id'];
         if ($id) {
             $staticNames[$name] = (int) $result[0]['id'];
         } else {
             $f3->error(500, 'Wormhole data missing in table "wormhole" for "name" = "' . $name . '"');
         }
     }
     // import data -----------------------------------------
     $systemWormhole = Model\BasicModel::getNew('SystemWormholeModel');
     foreach ($data as $staticData) {
         $result = $ccpDB->exec("\n                            SELECT\n                              solarSystemID\n                            FROM " . $ccpDB->quotekey('mapSolarSystems') . "\n                            WHERE\n                                " . $ccpDB->quotekey('solarSystemName') . " = :systemName", [':systemName' => $staticData->name]);
         $solarSystemID = (int) $result[0]['solarSystemID'];
         if ($solarSystemID) {
             foreach ($staticData->wh as $wh) {
                 $staticId = (int) $staticNames[$wh];
                 if ($staticId) {
                     // check if entry already exists
                     $systemWormhole->load(['systemId=? AND wormholeId=?', $solarSystemID, $staticId]);
                     if ($systemWormhole->dry()) {
                         $systemWormhole->systemId = $solarSystemID;
                         $systemWormhole->wormholeId = $staticId;
                         $systemWormhole->save();
                         $systemWormhole->reset();
                     }
                 } else {
                     $f3->error(500, 'Wormhole data missing for "name" = "' . $wh . '"');
                 }
             }
         } else {
             $f3->error(500, 'System "' . $staticData->name . '" not found on CCP´s [SDE] database');
         }
     }
 }