/**
  * Should carry out any processing required by the plugin.
  */
 public function handle()
 {
     global $INPUT;
     global $ID;
     global $config_cascade;
     $config_file_path = end($config_cascade['main']['local']);
     // form submit
     $table = Schema::cleanTableName($INPUT->str('table'));
     if ($table && $INPUT->bool('save') && checkSecurityToken()) {
         $builder = new SchemaBuilder($table, $INPUT->arr('schema'));
         if (!$builder->build()) {
             msg('something went wrong while saving', -1);
         }
         touch($config_file_path);
     }
     // export
     if ($table && $INPUT->bool('export')) {
         $builder = new Schema($table);
         header('Content-Type: application/json');
         header("Content-Disposition: attachment; filename={$table}.struct.json");
         echo $builder->toJSON();
         exit;
     }
     // import
     if ($table && $INPUT->bool('import')) {
         if (isset($_FILES['schemafile']['tmp_name'])) {
             $json = io_readFile($_FILES['schemafile']['tmp_name'], false);
             if (!$json) {
                 msg('Something went wrong with the upload', -1);
             } else {
                 $builder = new SchemaImporter($table, $json, $INPUT->bool('lookup'));
                 if (!$builder->build()) {
                     msg('something went wrong while saving', -1);
                 }
                 touch($config_file_path);
             }
         }
     }
     // delete
     if ($table && $INPUT->bool('delete')) {
         if ($table != $INPUT->str('confirm')) {
             msg($this->getLang('del_fail'), -1);
         } else {
             try {
                 $schema = new Schema($table);
                 $schema->delete();
                 msg($this->getLang('del_ok'), 1);
                 touch($config_file_path);
                 send_redirect(wl($ID, array('do' => 'admin', 'page' => 'struct_schemas'), true, '&'));
             } catch (StructException $e) {
                 msg(hsc($e->getMessage()), -1);
             }
         }
     }
 }
 public function test_import_export()
 {
     $sb = new meta\SchemaImporter('foobar', file_get_contents(__DIR__ . '/json/schema1.struct.json'));
     $this->assertTrue((bool) $sb->build());
     $schema = new meta\Schema('foobar');
     $expect = json_decode(file_get_contents(__DIR__ . '/json/schema1.struct.json'), true);
     $actual = json_decode($schema->toJSON(), true);
     // we don't expect this to match
     unset($expect['structversion']);
     unset($actual['structversion']);
     $expect['schema'] = 'foobar';
     // we exported the new schema
     $this->assertEquals($expect, $actual);
 }
 /**
  * Creates a schema from one of the available schema files
  *
  * @param string $schema
  * @param string $json base name of the JSON file optional, defaults to $schema
  * @param int $rev allows to create schemas back in time
  * @param bool $lookup create as a lookup schema
  */
 protected function loadSchemaJSON($schema, $json = '', $rev = 0, $lookup = false)
 {
     if (!$json) {
         $json = $schema;
     }
     $file = __DIR__ . "/json/{$json}.struct.json";
     if (!file_exists($file)) {
         throw new \RuntimeException("{$file} does not exist");
     }
     $importer = new SchemaImporter($schema, file_get_contents($file), $lookup);
     if (!$importer->build($rev)) {
         throw new \RuntimeException("build of {$schema} from {$file} failed");
     }
 }
 public function test_filtervars_struct()
 {
     global $ID;
     $ID = 'foo:bar:baz';
     // prepare some struct data
     $sb = new meta\SchemaImporter('schema1', file_get_contents(__DIR__ . '/json/schema1.struct.json'));
     $sb->build();
     $schemaData = meta\AccessTable::byTableName('schema1', $ID, time());
     $schemaData->saveData(array('first' => 'test', 'second' => array('multi1', 'multi2')));
     $searchConfig = new SearchConfig(array('schemas' => array(array('schema1', 'alias'))));
     $this->assertEquals('test', $searchConfig->applyFilterVars('$STRUCT.first$'));
     $this->assertEquals('test', $searchConfig->applyFilterVars('$STRUCT.alias.first$'));
     $this->assertEquals('test', $searchConfig->applyFilterVars('$STRUCT.schema1.first$'));
     $this->assertEquals('pretestpost', $searchConfig->applyFilterVars('pre$STRUCT.first$post'));
     $this->assertEquals('pretestpost', $searchConfig->applyFilterVars('pre$STRUCT.alias.first$post'));
     $this->assertEquals('pretestpost', $searchConfig->applyFilterVars('pre$STRUCT.schema1.first$post'));
     $this->assertEquals(array('multi1', 'multi2'), $searchConfig->applyFilterVars('$STRUCT.second$'));
     $this->assertEquals(array('multi1', 'multi2'), $searchConfig->applyFilterVars('$STRUCT.alias.second$'));
     $this->assertEquals(array('multi1', 'multi2'), $searchConfig->applyFilterVars('$STRUCT.schema1.second$'));
     $this->assertEquals(array('premulti1post', 'premulti2post'), $searchConfig->applyFilterVars('pre$STRUCT.second$post'));
     $this->assertEquals(array('premulti1post', 'premulti2post'), $searchConfig->applyFilterVars('pre$STRUCT.alias.second$post'));
     $this->assertEquals(array('premulti1post', 'premulti2post'), $searchConfig->applyFilterVars('pre$STRUCT.schema1.second$post'));
     $this->assertEquals('', $searchConfig->applyFilterVars('$STRUCT.notexisting$'));
 }