public function preFillFromSourceLanguage($useGoogleTranslateData = true)
 {
     $path = APPPATH . "resources/languageforge/semdomtrans/GoogleTranslateHarvester/semdom-google-translate-{$this->languageIsoCode}.txt.gz";
     $googleTranslateData = [];
     if ($useGoogleTranslateData && file_exists($path)) {
         $lines = gzfile($path);
         foreach ($lines as $line) {
             $splitLine = explode("|", $line);
             if (count($splitLine) == 2) {
                 $googleTranslateData[$splitLine[0]] = $splitLine[1];
             }
         }
     }
     // cjh review: we may actually want to only prefill from English, if in the future we allow creating projects from incomplete source projects
     $sourceProject = new SemDomTransProjectModel($this->sourceLanguageProjectId->asString());
     $this->copyXmlToAssets($sourceProject->xmlFilePath);
     $sourceItems = new SemDomTransItemListModel($sourceProject);
     $sourceItems->read();
     foreach ($sourceItems->entries as $item) {
         $newItem = new SemDomTransItemModel($this);
         // if Google translation exists for given name exists, use it
         if (array_key_exists($item['name']['translation'], $googleTranslateData)) {
             $newItem->name->translation = $googleTranslateData[$item['name']['translation']];
             $newItem->name->status = SemDomTransStatus::Suggested;
         }
         // if Google translation exists for given description exists, use it
         if (array_key_exists($item['description']['translation'], $googleTranslateData)) {
             $newItem->description->translation = $googleTranslateData[$item['description']['translation']];
             $newItem->description->status = SemDomTransStatus::Suggested;
         }
         $newItem->key = $item['key'];
         for ($x = 0; $x < count($item['questions']); $x++) {
             $q = new SemDomTransQuestion();
             // if Google translation exists for given question, use it
             if (array_key_exists($item['questions'][$x]['question']['translation'], $googleTranslateData)) {
                 $q->question->translation = $googleTranslateData[$item['questions'][$x]['question']['translation']];
                 $q->question->status = SemDomTransStatus::Suggested;
             }
             // if Google translation exists for given question term, use it
             if (array_key_exists($item['questions'][$x]['terms']['translation'], $googleTranslateData)) {
                 $q->terms->translation = $googleTranslateData[$item['questions'][$x]['terms']['translation']];
                 $q->terms->status = SemDomTransStatus::Suggested;
             }
             $newItem->questions[] = $q;
         }
         for ($x = 0; $x < count($item['searchKeys']); $x++) {
             $sk = new SemDomTransTranslatedForm();
             // if Google translation exists for given search key, use it
             if (array_key_exists($item['searchKeys'][$x]['translation'], $googleTranslateData)) {
                 $sk->translation = $googleTranslateData[$item['searchKeys'][$x]['translation']];
                 $sk->status = SemDomTransStatus::Suggested;
             }
             $newItem->searchKeys[] = $sk;
         }
         $newItem->xmlGuid = $item['xmlGuid'];
         $newItem->write();
     }
 }
 public function testCreateProject_englishProjectExists_newProjectCreatedAndPrefilledFromSourceProject()
 {
     $environ = new SemDomMongoTestEnvironment();
     $environ->cleanPreviousProject('es');
     $user1Id = $environ->createUser('u', 'u', 'u');
     $englishProject = $environ->getEnglishProjectAndCreateIfNecessary();
     $newProject = $environ->createSemDomProject('es', 'Spanish', $user1Id);
     $this->assertEquals($englishProject->id->asString(), $newProject->sourceLanguageProjectId->asString());
     $this->assertEquals(false, $newProject->isSourceLanguage);
     $listModel = new SemDomTransItemListModel($newProject);
     $listModel->read();
     $this->assertEquals(36, $listModel->count);
 }
 public function testImportFromFile_nonEnglishProject_importsOk()
 {
     $environ = new SemDomMongoTestEnvironment();
     $environ->cleanPreviousProject('es');
     $user1Id = $environ->createUser('u', 'u', 'u');
     $environ->getEnglishProjectAndCreateIfNecessary();
     $project = $environ->createSemDomProject('es', 'Spanish', $user1Id);
     $listModel = new SemDomTransItemListModel($project);
     $listModel->read();
     $xmlFilePath = __DIR__ . '/testFiles/LocalizedLists-es.xml';
     $project->importFromFile($xmlFilePath);
     $listModel->read();
     $this->assertEquals(1792, $listModel->count);
 }
 public function testImportFromFile_nonEnglishProject_importsOk()
 {
     $e = new SemDomMongoTestEnvironment();
     $e->cleanPreviousProject('es');
     $user1Id = $e->createUser('u', 'u', 'u');
     $e->getEnglishProjectAndCreateIfNecessary();
     $project = $e->createSemDomProject('es', "Spanish", $user1Id);
     $listModel = new SemDomTransItemListModel($project);
     $listModel->read();
     $xmlFilePath = TestPath . "languageforge/semdomtrans/testFiles/LocalizedLists-es.xml";
     $project->importFromFile($xmlFilePath);
     $listModel->read();
     $this->assertEqual($listModel->count, 1792);
 }
 public function testEncode_SourceProjectAndTargetProjectHaveItems_DtoAsExpected()
 {
     self::$environ = new SemDomMongoTestEnvironment();
     self::$environ->cleanPreviousProject('es');
     /** @noinspection PhpUnusedLocalVariableInspection */
     $sourceProject = self::$environ->getEnglishProjectAndCreateIfNecessary();
     $user1Id = self::$environ->createUser('u', 'u', 'u');
     $targetProject = self::$environ->createSemDomProject('es', 'Spanish', $user1Id);
     /*
     // insert dummy models
     $sourceItemModel = new SemDomTransItemModel($sourceProject);
     $sourceItemModel->key = '1';
     $sourceItemModel->name = new SemDomTransTranslatedForm('universe');
     $sourceItemModel->description = new SemDomTransTranslatedForm('Universe description');
     $sq = new SemDomTransQuestion('A universe question', 'A universe question term');
     $sourceItemModel->questions = new ArrayOf(function ($data) {
         return new SemDomTransQuestion();
     });
     $sourceItemModel->questions[] = $sq;
     $sourceItemModel->write();
     */
     $targetItemsModel = new SemDomTransItemListModel($targetProject);
     $targetItemsModel->read();
     $targetItems = $targetItemsModel->entries;
     $targetItemModel = new SemDomTransItemModel($targetProject);
     $targetItemModel->readByProperty('xmlGuid', $targetItems[0]['xmlGuid']);
     $targetItemModel->key = '1';
     $targetItemModel->name = new SemDomTransTranslatedForm('wszechswiat');
     $targetItemModel->description = new SemDomTransTranslatedForm('Opis wszechswiata');
     $tq = new SemDomTransQuestion('Pytanie wszechswiata', 'Termin zwiazany z wszechswiatem');
     $targetItemModel->questions = new ArrayOf(function () {
         return new SemDomTransQuestion();
     });
     $targetItemModel->questions[] = $tq;
     $targetItemModel->write();
     // call dto
     //$loadTargetProject = new SemDomTransProjectModel($prId->asString());
     //$loadSourceProject = new SemDomTransProjectModel($loadTargetProject->sourceLanguageProjectId);
     $prId = $targetProject->id;
     $result = SemDomTransEditDto::encode($prId->asString(), null);
     // print_r($result);
     // check dto returns expected results
     $entries = $result['entries'];
     $this->assertTrue($entries != null);
     $this->assertTrue(count($entries) > 0);
     $firstObject = $entries[0];
     $this->assertNotNull($firstObject['key']);
     $this->assertEquals('1', $firstObject['key']);
     $this->assertNotNull($firstObject['name']);
     $this->assertEquals('Universe, creation', $firstObject['name']['source']);
     $this->assertEquals('wszechswiat', $firstObject['name']['translation']);
     $this->assertEquals(SemDomTransStatus::Draft, $firstObject['name']['status']);
     $this->assertNotNull($firstObject['description']);
     $this->assertEquals('Opis wszechswiata', $firstObject['description']['translation']);
     $this->assertEquals(SemDomTransStatus::Draft, $firstObject['description']['status']);
     $this->assertNotNull($firstObject['questions']);
     $this->assertNotNull($firstObject['questions'][0]);
     $this->assertNotNull($firstObject['questions'][0]['question']);
     $this->assertNotNull($firstObject['questions'][0]['terms']);
     $this->assertEquals('(1) What words refer to everything we can see?', $firstObject['questions'][0]['question']['source']);
     $this->assertEquals('Pytanie wszechswiata', $firstObject['questions'][0]['question']['translation']);
     $this->assertEquals('universe, creation, cosmos, heaven and earth, macrocosm, everything that exists', $firstObject['questions'][0]['terms']['source']);
     $this->assertEquals('Termin zwiazany z wszechswiatem', $firstObject['questions'][0]['terms']['translation']);
     // this test messes with the English source project
     self::$environ->clean();
     self::$environ->cleanPreviousProject('en');
 }
 /**
  * Returns encoded Semantic Domain Editor Dto:
  * 1) Loads all items of target project (langauge we are translating into)
  * 2) Loads all items of source project (language we are translating from)
  * 3) Sets values from source projects as sources on target items
  * @param string $projectId
  * @param string $userId
  * @param string $lastFetchTime
  * @param int $offset
  * @return array multitype:boolean number multitype: multitype:string  Ambigous <string;, multitype:, string, Ambigous <multitype:, multitype:multitype: unknown >, Ambigous <multitype:, \stdClass, multitype:multitype: unknown >, Ambigous <string, multitype:NULL string >>
  */
 public static function encode($projectId, $userId, $lastFetchTime = null, $offset = 0)
 {
     $data = array();
     // load target project
     $project = new SemDomTransProjectModel($projectId);
     // load source project - if source language is not specified, set it to english
     if ($project->sourceLanguageProjectId == null) {
         $sourceProject = new SemDomTransProjectModel();
         $sourceProject->projectCode = "semdom-en-{$project->semdomVersion}";
         $sourceProject->readByProperty("projectCode", $sourceProject->projectCode);
     } else {
         $sourceProject = new SemDomTransProjectModel($project->sourceLanguageProjectId->asString());
     }
     // load target project items
     $items = new SemDomTransItemListModel($project, $lastFetchTime);
     $items->read();
     $targetItems = $items->entries;
     $sourceItemsModel = new SemDomTransItemListModel($sourceProject);
     $sourceItemsModel->read();
     $sourceItems = $sourceItemsModel->entries;
     //print_r($sourceItems);
     $sourceItemsByKey = array();
     foreach ($sourceItems as $item) {
         $sourceItemsByKey[$item['key']] = $item;
     }
     // suplement the target language data with source language values
     $sourceLanguageIsIncomplete = false;
     // loop over all target items
     foreach ($targetItems as $i => $item) {
         // loop over all properties of target item
         foreach ($item as $outerProp => $outerValue) {
             // make sure that the outer value is an array and that the translation for it exists
             if (is_array($outerValue) && key_exists('translation', $outerValue)) {
                 // source item has translation that is non empty, than set it as source of arget item
                 if ($sourceItemsByKey[$item['key']][$outerProp]['translation'] != '') {
                     $targetItems[$i][$outerProp]['source'] = $sourceItemsByKey[$item['key']][$outerProp]['translation'];
                 } else {
                     $sourceLanguageIsIncomplete = true;
                 }
             }
             // handle special case of search keys
             if ($outerProp == 'searchKeys') {
                 // iterate over all search keys
                 foreach ($outerValue as $innerProp => $innerValue) {
                     // check if corresponding source item has search keys
                     if (array_key_exists($innerProp, $sourceItemsByKey[$item['key']][$outerProp])) {
                         // if source item has search keys, and given search key has translation set on it
                         // use that translation as source of target item search key
                         if ($sourceItemsByKey[$item['key']][$outerProp][$innerProp]['translation'] != '') {
                             $targetItems[$i][$outerProp][$innerProp]['source'] = $sourceItemsByKey[$item['key']][$outerProp][$innerProp]['translation'];
                         } else {
                             $sourceLanguageIsIncomplete = true;
                         }
                     }
                 }
             } else {
                 if ($outerProp == 'questions') {
                     // iterate over all questions
                     foreach ($outerValue as $innerProp => $innerValue) {
                         // check that 'questions' property exists on source item
                         if (array_key_exists($innerProp, $sourceItemsByKey[$item['key']][$outerProp])) {
                             // if source item has questions, and given question has translation set on it
                             // use that translation as source of target item question
                             if ($sourceItemsByKey[$item['key']][$outerProp][$innerProp]['question']['translation'] != '') {
                                 $targetItems[$i][$outerProp][$innerProp]['question']['source'] = $sourceItemsByKey[$item['key']][$outerProp][$innerProp]['question']['translation'];
                             } else {
                                 $sourceLanguageIsIncomplete = true;
                             }
                             // if source item has questions, and given question term has translation set on it
                             // use that translation as source of target item question term
                             if ($sourceItemsByKey[$item['key']][$outerProp][$innerProp]['terms']['translation'] != '') {
                                 $targetItems[$i][$outerProp][$innerProp]['terms']['source'] = $sourceItemsByKey[$item['key']][$outerProp][$innerProp]['terms']['translation'];
                             } else {
                                 $sourceLanguageIsIncomplete = true;
                             }
                         } else {
                             $sourceLanguageIsIncomplete = true;
                         }
                     }
                 }
             }
         }
     }
     $data['sourceLanguageIsIncomplete'] = $sourceLanguageIsIncomplete;
     // load comments
     $commentsModel = new LexCommentListModel($project, $lastFetchTime);
     $commentsModel->readAsModels();
     $encodedModels = LexDbeDtoCommentsEncoder::encode($commentsModel);
     $data['comments'] = $encodedModels["entries"];
     // load working sets
     $workingSets = new SemDomTransWorkingSetListModel($project, $lastFetchTime);
     $workingSets->read();
     $data["workingSets"] = $workingSets->entries;
     /*  if (!is_null($lastFetchTime)) {
             $deletedCommentsModel = new LexDeletedCommentListModel($project, $lastFetchTime);
             $deletedCommentsModel->read();
             $data['deletedCommentIds'] = array_map(function ($c) {return $c['id']; }, $deletedCommentsModel->entries);
         }
         */
     $data['entries'] = $targetItems;
     $data['timeOnServer'] = time();
     // future use for offline syncing
     $data["statuses"] = SemDomTransStatus::getSemdomStatuses();
     return $data;
 }
 public function testEncode_SourceProjectAndTargetProjectHaveItems_DtoAsExpected()
 {
     $e = new SemDomMongoTestEnvironment();
     $e->cleanPreviousProject('es');
     $sourceProject = $e->getEnglishProjectAndCreateIfNecessary();
     $user1Id = $e->createUser('u', 'u', 'u');
     $targetProject = $e->createSemDomProject('es', "Spanish", $user1Id);
     /*
     // insert dummy models
     $sourceItemModel = new SemDomTransItemModel($sourceProject);
     $sourceItemModel->key = "1";
     $sourceItemModel->name = new SemDomTransTranslatedForm("universe");
     $sourceItemModel->description = new SemDomTransTranslatedForm("Universe description");
     $sq = new SemDomTransQuestion("A universe question", "A universe question term");
     $sourceItemModel->questions = new ArrayOf(function ($data) {
         return new SemDomTransQuestion();
     });
     $sourceItemModel->questions[] = $sq;
     $sourceItemModel->write();
     */
     $targetItemsModel = new SemDomTransItemListModel($targetProject);
     $targetItemsModel->read();
     $targetItems = $targetItemsModel->entries;
     $targetItemModel = new SemDomTransItemModel($targetProject);
     $targetItemModel->readByProperty('xmlGuid', $targetItems[0]['xmlGuid']);
     $targetItemModel->key = "1";
     $targetItemModel->name = new SemDomTransTranslatedForm("wszechswiat");
     $targetItemModel->description = new SemDomTransTranslatedForm("Opis wszechswiata");
     $tq = new SemDomTransQuestion("Pytanie wszechswiata", "Termin zwiazany z wszechswiatem");
     $targetItemModel->questions = new ArrayOf(function ($data) {
         return new SemDomTransQuestion();
     });
     $targetItemModel->questions[] = $tq;
     $targetItemModel->write();
     // call dto
     //$loadTargetProject = new SemDomTransProjectModel($prId->asString());
     //$loadSourceProject = new SemDomTransProjectModel($loadTargetProject->sourceLanguageProjectId);
     $prId = $targetProject->id;
     $result = SemDomTransEditDto::encode($prId->asString(), null);
     // print_r($result);
     // check dto returns expected results
     $entries = $result["entries"];
     $this->assertTrue($entries != null);
     $this->assertTrue(count($entries) > 0);
     $firstObject = $entries[0];
     $this->assertNotEqual($firstObject["key"], null);
     $this->assertEqual($firstObject["key"], "1");
     $this->assertNotEqual($firstObject["name"], null);
     $this->assertEqual($firstObject["name"]["source"], "Universe, creation");
     $this->assertEqual($firstObject["name"]["translation"], "wszechswiat");
     $this->assertEqual($firstObject["name"]["status"], SemDomTransStatus::Draft);
     $this->assertNotEqual($firstObject["description"], null);
     $this->assertEqual($firstObject["description"]["translation"], "Opis wszechswiata");
     $this->assertEqual($firstObject["description"]["status"], SemDomTransStatus::Draft);
     $this->assertNotEqual($firstObject["questions"], null);
     $this->assertNotEqual($firstObject["questions"][0], null);
     $this->assertNotEqual($firstObject["questions"][0]["question"], null);
     $this->assertNotEqual($firstObject["questions"][0]["terms"], null);
     $this->assertEqual($firstObject["questions"][0]["question"]["source"], "(1) What words refer to everything we can see?");
     $this->assertEqual($firstObject["questions"][0]["question"]["translation"], "Pytanie wszechswiata");
     $this->assertEqual($firstObject["questions"][0]["terms"]["source"], "universe, creation, cosmos, heaven and earth, macrocosm, everything that exists");
     $this->assertEqual($firstObject["questions"][0]["terms"]["translation"], "Termin zwiazany z wszechswiatem");
     // this test messes with the English source project
     $e->clean();
     $e->cleanPreviousProject('en');
 }