protected function doImportDatasource($files) { $container = $this->get('kernel')->getContainer(); $uploadDir = str_replace("\\", "/", $container->getParameter('g6k_upload_directory')); $name = ''; $schemafile = ''; $datafile = ''; foreach ($files as $fieldname => $file) { if ($file && $file->isValid()) { $filePath = $uploadDir . "/" . $this->get('g6k.file_uploader')->upload($file); if ($fieldname == 'datasource-schema-file') { $schemafile = $filePath; } elseif ($fieldname == 'datasource-data-file') { $datafile = $filePath; $name = $file->getClientOriginalName(); if (preg_match("/^(.+)\\.json\$/", $name, $m)) { $name = trim($m[1]); } } } } if ($name != '' && $schemafile != '' && $datafile != '') { $driver = $container->getParameter('database_driver'); $parameters = array('database_driver' => $driver); if ($driver != 'pdo_sqlite') { if ($container->hasParameter('database_host')) { $parameters['database_host'] = $container->getParameter('database_host'); } if ($container->hasParameter('database_port')) { $parameters['database_port'] = $container->getParameter('database_port'); } if ($container->hasParameter('database_user')) { $parameters['database_user'] = $container->getParameter('database_user'); } if ($container->hasParameter('database_password')) { $parameters['database_password'] = $container->getParameter('database_password'); } } $converter = new JSONToSQLConverter($parameters); $form = $converter->convert($name, $schemafile, $datafile); $datasource = $this->doCreateDatasource($form); $dom = $datasource->ownerDocument; $tableid = 1; foreach ($form['datasource-tables'] as $tbl) { $table = $dom->createElement("Table"); $table->setAttribute('id', $tableid++); $table->setAttribute('name', $tbl['name']); $table->setAttribute('label', $tbl['label']); $descr = $dom->createElement("Description"); $descr->appendChild($dom->createCDATASection($tbl['description'])); $table->appendChild($descr); $columnid = 1; foreach ($tbl['columns'] as $col) { $column = $dom->createElement("Column"); $column->setAttribute('id', $columnid++); $column->setAttribute('name', $col['name']); $column->setAttribute('type', $col['type']); $column->setAttribute('label', $col['label']); $descr = $dom->createElement("Description"); $descr->appendChild($dom->createCDATASection($col['description'])); $column->appendChild($descr); if (isset($col['choices'])) { $choices = $dom->createElement("Choices"); $choiceid = 1; foreach ($col['choices'] as $ch) { $choice = $dom->createElement("Choice"); $choice->setAttribute('id', $choiceid++); $choice->setAttribute('value', $ch['value']); $choice->setAttribute('label', $ch['label']); $choices->appendChild($choice); } $column->appendChild($choices); } elseif (isset($col['source'])) { $choices = $dom->createElement("Choices"); $source = $dom->createElement("Source"); $source->setAttribute('id', 1); $source->setAttribute('datasource', $col['source']['datasource']); if (isset($col['source']['request'])) { $source->setAttribute('request', $col['source']['request']); } $source->setAttribute('returnType', $col['source']['returnType']); if (isset($col['source']['returnPath'])) { $source->setAttribute('returnPath', $col['source']['returnPath']); } $source->setAttribute('valueColumn', $col['source']['valueColumn']); $source->setAttribute('labelColumn', $col['source']['labelColumn']); $choices->appendChild($source); $column->appendChild($choices); } $table->appendChild($column); } $datasource->appendChild($table); } $this->saveDatasources($dom); } if ($schemafile != '') { unlink($schemafile); } if ($datafile != '') { unlink($datafile); } return new RedirectResponse($this->generateUrl('eureka_g6k_admin_datasource', array('dsid' => $datasource->getAttribute('id')))); }
public static function installDemo(Event $event) { $event->getIO()->write("Installing the demo database"); $extras = $event->getComposer()->getPackage()->getExtra(); $symfonyDir = dirname(dirname(dirname(dirname(__DIR__)))); $appDir = $symfonyDir . DIRECTORY_SEPARATOR . $extras['symfony-app-dir']; $configDir = $appDir . DIRECTORY_SEPARATOR . 'config'; $databasesDir = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'Resources' . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'databases'; if (($parameters = self::getParameters($event, $configDir)) === false) { return; } $name = 'demo'; $schemafile = $databasesDir . DIRECTORY_SEPARATOR . $name . '.schema.json'; $datafile = $databasesDir . DIRECTORY_SEPARATOR . $name . '.json'; $converter = new JSONToSQLConverter($parameters); $form = $converter->convert($name, $schemafile, $datafile); $datasource = self::doCreateDatasource($form); $dom = $datasource->ownerDocument; $tableid = 1; foreach ($form['datasource-tables'] as $tbl) { $table = $dom->createElement("Table"); $table->setAttribute('id', $tableid++); $table->setAttribute('name', $tbl['name']); $table->setAttribute('label', $tbl['label']); $descr = $dom->createElement("Description"); $descr->appendChild($dom->createCDATASection($tbl['description'])); $table->appendChild($descr); $columnid = 1; foreach ($tbl['columns'] as $col) { $column = $dom->createElement("Column"); $column->setAttribute('id', $columnid++); $column->setAttribute('name', $col['name']); $column->setAttribute('type', $col['type']); $column->setAttribute('label', $col['label']); $descr = $dom->createElement("Description"); $descr->appendChild($dom->createCDATASection($col['description'])); $column->appendChild($descr); if (isset($col['choices'])) { $choices = $dom->createElement("Choices"); $choiceid = 1; foreach ($col['choices'] as $ch) { $choice = $dom->createElement("Choice"); $choice->setAttribute('id', $choiceid++); $choice->setAttribute('value', $ch['value']); $choice->setAttribute('label', $ch['label']); $choices->appendChild($choice); } $column->appendChild($choices); } elseif (isset($col['source'])) { $choices = $dom->createElement("Choices"); $source = $dom->createElement("Source"); $source->setAttribute('id', 1); $source->setAttribute('datasource', $col['source']['datasource']); if (isset($col['source']['request'])) { $source->setAttribute('request', $col['source']['request']); } $source->setAttribute('returnType', $col['source']['returnType']); if (isset($col['source']['returnPath'])) { $source->setAttribute('returnPath', $col['source']['returnPath']); } $source->setAttribute('valueColumn', $col['source']['valueColumn']); $source->setAttribute('labelColumn', $col['source']['labelColumn']); $choices->appendChild($source); $column->appendChild($choices); } $table->appendChild($column); } $datasource->appendChild($table); } $xml = $dom->saveXML(null, LIBXML_NOEMPTYTAG); $dom = new \DOMDocument(); $dom->preserveWhiteSpace = false; $dom->formatOutput = true; $dom->loadXml($xml); $formatted = preg_replace_callback('/^( +)</m', function ($a) { return str_repeat("\t", intval(strlen($a[1]) / 2)) . '<'; }, $dom->saveXML(null, LIBXML_NOEMPTYTAG)); file_put_contents($databasesDir . "/DataSources.xml", $formatted); $parameters = (object) $parameters; $simusDir = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'Resources' . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'simulators'; if (file_exists($simusDir . DIRECTORY_SEPARATOR . 'demo-' . $parameters->locale . '.xml')) { rename($simusDir . DIRECTORY_SEPARATOR . 'demo-' . $parameters->locale . '.xml', $simusDir . DIRECTORY_SEPARATOR . 'demo.xml'); } foreach (glob($simusDir . DIRECTORY_SEPARATOR . "demo-*.xml") as $filename) { unlink($filename); } }