public static function setUpBeforeClass()
 {
     if (defined('PHPUNIT_CREDENTIALS_MYSQL_SCHEMALESS_HOST')) {
         $configuration = new MySQLSchemalessConfiguration();
         $configuration->initDatabase(PHPUNIT_CREDENTIALS_MYSQL_SCHEMALESS_HOST, PHPUNIT_CREDENTIALS_MYSQL_SCHEMALESS_DBNAME, PHPUNIT_CREDENTIALS_MYSQL_SCHEMALESS_USERNAME, PHPUNIT_CREDENTIALS_MYSQL_SCHEMALESS_PASSWORD);
         $configuration->setCMDLFolder(__DIR__ . '/../../resources/ContentArchiveExample1/cmdl');
         $configuration->setRepositoryName('phpunit');
         $configuration->addContentTypes();
         $database = $configuration->getDatabase();
         $database->execute('DROP TABLE IF EXISTS _cmdl_');
         $database->execute('DROP TABLE IF EXISTS _counter_');
         $database->execute('DROP TABLE IF EXISTS phpunit$profiles');
         $connection = $configuration->createReadWriteConnection();
         $repository = new Repository('phpunit', $connection);
         $repository->selectContentType('profiles');
         $record = $repository->createRecord('dmc digital media center', 5);
         $repository->saveRecord($record);
         $record = $repository->createRecord('Agency 16', 16);
         $repository->saveRecord($record);
         $repository->selectWorkspace('live');
         $record = $repository->createRecord('dmc digital media center', 5);
         $repository->saveRecord($record);
         $repository->selectLanguage('de');
         $record = $repository->createRecord('dmc digital media center', 5);
         $repository->saveRecord($record);
         KVMLoggerFactory::createWithKLogger(__DIR__ . '/../../../tmp');
     }
 }
 public function exportXLSX(Repository $repository, $contentTypeName, $workspace = 'default', $language = 'default', $viewName = 'exchange')
 {
     $repository->selectContentType($contentTypeName);
     // Select view and fallback if necessary
     $contentTypeDefinition = $repository->getContentTypeDefinition();
     $viewDefinition = $contentTypeDefinition->getExchangeViewDefinition($viewName);
     $viewName = $viewDefinition->getName();
     $this->writeln('Connecting repository');
     $this->writeln('');
     $repository->selectWorkspace($workspace);
     $repository->selectLanguage($language);
     $repository->selectView($viewName);
     /** @var Record[] $records */
     $records = $repository->getRecords('', '.id', 1);
     if ($records !== false) {
         // Create new PHPExcel object
         $objPHPExcel = new \PHPExcel();
         // use temp folder for processing of large files
         $cacheMethod = \PHPExcel_CachedObjectStorageFactory::cache_to_phpTemp;
         $cacheSettings = array('memoryCacheSize' => '12MB');
         \PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);
         // Set document properties
         $objPHPExcel->getProperties()->setCreator("AnyContent CMCK")->setLastModifiedBy("AnyContent CMCK")->setTitle("Full Export from content type " . $contentTypeDefinition->getTitle())->setSubject("AnyContent Export")->setDescription("");
         $worksheet = $objPHPExcel->setActiveSheetIndex(0);
         $worksheet->setTitle('Export');
         $worksheet->setCellValueByColumnAndRow(0, 1, '.id');
         $worksheet->getStyleByColumnAndRow(0, 1)->getFont()->setBold(false)->setItalic(true);
         $worksheet->setCellValueByColumnAndRow(1, 1, '.revision');
         $worksheet->getStyleByColumnAndRow(1, 1)->getFont()->setBold(false)->setItalic(true);
         $row = 1;
         $column = 2;
         foreach ($contentTypeDefinition->getProperties($viewName) as $property) {
             $worksheet->setCellValueByColumnAndRow($column, $row, $property);
             $worksheet->getStyleByColumnAndRow($column, $row)->getFont()->setBold(true);
             $worksheet->getColumnDimensionByColumn($column)->setWidth(20);
             $column++;
         }
         $row++;
         foreach ($records as $record) {
             $this->writeln('Processing record ' . $record->getID() . ' - ' . $record->getName());
             $worksheet->setCellValueByColumnAndRow(0, $row, $record->getID());
             $worksheet->setCellValueByColumnAndRow(1, $row, $record->getRevision());
             $column = 2;
             foreach ($contentTypeDefinition->getProperties($viewName) as $property) {
                 $worksheet->setCellValueByColumnAndRow($column, $row, $record->getProperty($property));
                 $column++;
             }
             $row++;
         }
         $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
         ob_start();
         $objWriter->save('php://output');
         $excelOutput = ob_get_clean();
         return $excelOutput;
     }
     return false;
 }
 public function testSaveRecords()
 {
     $this->repository->selectContentType('example01');
     for ($i = 1; $i <= 5; $i++) {
         $record = $this->repository->createRecord('New Record ' . $i);
         $record->setProperty('article', 'Test ' . $i);
         $id = $this->repository->saveRecord($record);
         $this->assertEquals($i, $id);
     }
     $this->repository->selectLanguage('es');
     for ($i = 1; $i <= 5; $i++) {
         $record = $this->repository->createRecord('New Record ' . (5 + $i));
         $record->setProperty('article', 'Test ' . (5 + $i));
         $id = $this->repository->saveRecord($record);
         $this->assertEquals(5 + $i, $id);
     }
     $this->repository->selectWorkspace('live');
     for ($i = 1; $i <= 5; $i++) {
         $record = $this->repository->createRecord('New Record ' . (10 + $i));
         $record->setProperty('article', 'Test ' . (10 + $i));
         $id = $this->repository->saveRecord($record);
         $this->assertEquals(10 + $i, $id);
     }
     $this->repository->reset();
     $c = $this->repository->countRecords();
     $this->assertEquals(5, $c);
     $this->repository->selectLanguage('es');
     $c = $this->repository->countRecords();
     $this->assertEquals(5, $c);
     $this->repository->selectWorkspace('live');
     $c = $this->repository->countRecords();
     $this->assertEquals(5, $c);
     $this->repository->selectLanguage('default');
     $c = $this->repository->countRecords();
     $this->assertEquals(0, $c);
 }
 public static function setUpBeforeClass()
 {
     if (defined('PHPUNIT_CREDENTIALS_MYSQL_SCHEMALESS_HOST')) {
         $source = __DIR__ . '/../../resources/ContentArchiveExample1/cmdl';
         $target = __DIR__ . '/../../../tmp/MySqlSchemaLessCMDL';
         $fs = new Filesystem();
         if (file_exists($target)) {
             $fs->remove($target);
         }
         $fs->mirror($source, $target);
         $configuration = new MySQLSchemalessConfiguration();
         $configuration->initDatabase(PHPUNIT_CREDENTIALS_MYSQL_SCHEMALESS_HOST, PHPUNIT_CREDENTIALS_MYSQL_SCHEMALESS_DBNAME, PHPUNIT_CREDENTIALS_MYSQL_SCHEMALESS_USERNAME, PHPUNIT_CREDENTIALS_MYSQL_SCHEMALESS_PASSWORD);
         $configuration->setCMDLFolder($target);
         $configuration->setRepositoryName('phpunit');
         $configuration->addContentTypes();
         $database = $configuration->getDatabase();
         $database->execute('DROP TABLE IF EXISTS _cmdl_');
         $database->execute('DROP TABLE IF EXISTS _counter_');
         $database->execute('DROP TABLE IF EXISTS phpunit$profiles');
         $connection = $configuration->createReadWriteConnection();
         $repository = new Repository('phpunit', $connection);
         $repository->selectContentType('profiles');
         $record = $repository->createRecord('Agency 1', 1);
         $repository->saveRecord($record);
         $record = $repository->createRecord('Agency 2', 2);
         $repository->saveRecord($record);
         $record = $repository->createRecord('Agency 5', 5);
         $repository->saveRecord($record);
         $repository->selectWorkspace('live');
         $record = $repository->createRecord('Agency 1', 1);
         $repository->saveRecord($record);
         $record = $repository->createRecord('Agency 2', 2);
         $repository->saveRecord($record);
         KVMLoggerFactory::createWithKLogger(__DIR__ . '/../../../tmp');
     }
 }
 public function importXLSX(Repository $repository, $contentTypeName, $filename, $workspace = 'default', $language = 'default', $viewName = 'exchange')
 {
     $this->count = 0;
     $this->records = null;
     $this->stash = array();
     $this->error = false;
     $repository->selectContentType($contentTypeName);
     // Select view and fallback if necessary
     $contentTypeDefinition = $repository->getContentTypeDefinition();
     $viewDefinition = $contentTypeDefinition->getExchangeViewDefinition($viewName);
     $viewName = $viewDefinition->getName();
     $repository->selectWorkspace($workspace);
     $repository->selectLanguage($language);
     $repository->selectView($viewName);
     $objPHPExcel = \PHPExcel_IOFactory::load($filename);
     if ($objPHPExcel) {
         if ($this->isTruncateRecords()) {
             $this->deleteEffectiveRecords($repository, $workspace, $viewName, $language);
         }
         $objWorksheet = $objPHPExcel->getActiveSheet();
         $highestRow = $objWorksheet->getHighestRow();
         // e.g. 10
         $highestColumn = $objWorksheet->getHighestColumn();
         // e.g 'F'
         $highestColumnIndex = \PHPExcel_Cell::columnIndexFromString($highestColumn);
         // e.g. 5
         $idColumnIndex = null;
         $propertiesColumnIndices = array();
         for ($i = 0; $i <= $highestColumnIndex; $i++) {
             $value = trim($objWorksheet->getCellByColumnAndRow($i, 1)->getValue());
             if ($value != '') {
                 if (substr($value, 0, 1) == '.') {
                     if ($value == '.id') {
                         $idColumnIndex = $i;
                     }
                 } else {
                     $value = Util::generateValidIdentifier($value);
                     if ($contentTypeDefinition->hasProperty($value, $viewName)) {
                         $this->writeln('Detected valid property ' . $value);
                         $propertiesColumnIndices[$value] = $i;
                     }
                 }
             }
         }
         $this->writeln('');
         if (count($propertiesColumnIndices) != 0) {
             for ($row = 2; $row <= $highestRow; ++$row) {
                 $id = null;
                 if ($idColumnIndex !== null) {
                     if (!$this->isGenerateNewIDs()) {
                         $id = $objWorksheet->getCellByColumnAndRow($idColumnIndex, $row)->getValue();
                     }
                 }
                 $properties = array();
                 foreach ($propertiesColumnIndices as $property => $col) {
                     $value = $objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
                     $properties[$property] = $value;
                 }
                 $record = new Record($contentTypeDefinition, 'Imported Record', $viewName, $workspace, $language);
                 $record->setProperties($properties);
                 $record->setID($id);
                 $msg = $this->stashRecord($repository, $record, $workspace, $viewName, $language);
                 $this->writeln($msg);
             }
             $this->writeln('');
             $this->writeln('Found ' . $this->count . ' records to import');
             $this->writeln('');
             if ($this->count != 0) {
                 $this->writeln('Starting bulk import');
                 $this->writeln('');
                 $this->saveRecords($repository);
                 $this->writeln('');
                 $this->writeln('');
             }
         } else {
             $this->writeln('Excel does not contain matching property columns.');
         }
     } else {
         $this->writeln('Error parsing Excel file.');
         $this->error = true;
     }
     return !$this->error;
 }