/**
  * @param string $projectId
  * @param string $userId
  * @return array - the DTO array
  */
 public static function encode($projectId, $userId)
 {
     $data = array();
     $user = new UserProfileModel($userId);
     $project = new LexiconProjectModel($projectId);
     $config = JsonEncoder::encode($project->config);
     $config['inputSystems'] = JsonEncoder::encode($project->inputSystems);
     $data['config'] = $config;
     // comment out at the moment until a refactor can be done that is more efficient (language data in the database?)
     /*
     $interfaceLanguageCode = $project->interfaceLanguageCode;
     if ($user->interfaceLanguageCode) {
         $interfaceLanguageCode = $user->interfaceLanguageCode;
     }
     $options = self::getInterfaceLanguages(APPPATH . 'angular-app/languageforge/lexicon/lang');
     asort($options);    // sort by language name
     $selectInterfaceLanguages = array(
         'optionsOrder' => array_keys($options),
         'options' => $options
     );
     $data['interfaceConfig'] = array('userLanguageCode' => $interfaceLanguageCode);
     $data['interfaceConfig']['selectLanguages'] = $selectInterfaceLanguages;
     */
     // a stand in for the code above
     $data['interfaceConfig'] = array('userLanguageCode' => 'en', 'selectLanguages' => array('options' => array('en' => 'English'), 'optionsOrder' => array('en')));
     $optionlistListModel = new LexOptionListListModel($project);
     $optionlistListModel->read();
     $data['optionlists'] = $optionlistListModel->entries;
     return $data;
 }
 public function testUpdateList_newList_createsOK()
 {
     $e = new LexiconMongoTestEnvironment();
     $e->clean();
     $project = $e->createProject(SF_TESTPROJECT, SF_TESTPROJECTCODE);
     $optionlists = new LexOptionListListModel($project);
     $optionlists->read();
     // Initial project has no optionlists populated
     $this->assertEqual($optionlists->count, 0);
     // Initialized project has part of speech optionlist defined
     $project->initializeNewProject();
     $optionlists->read();
     $this->assertEqual($optionlists->count, 1);
     $initialValue = $optionlists->entries[0]['items'][0]['value'];
     $this->assertEqual($initialValue, 'Adjective (adj)');
     // Swap first and last items of parts of speech list
     $count = count($optionlists->entries[0]['items']);
     $swap = $optionlists->entries[0]['items'][0];
     $optionlists->entries[0]['items'][0] = $optionlists->entries[0]['items'][$count - 1];
     $optionlists->entries[0]['items'][$count - 1] = $swap;
     LexOptionListCommands::updateList($project->id->asString(), $optionlists->entries[0]);
     $optionlists->read();
     $newValue = $optionlists->entries[0]['items'][0]['value'];
     $this->assertEqual($newValue, 'Verb (v)');
     // Create part of speech list for fruits
     $fruits = array(array('key' => 'a', 'value' => 'apple'), array('key' => 'b', 'value' => 'berry'), array('key' => 'c', 'value' => 'cherry'), array('key' => 'g', 'value' => 'grape'), array('key' => 'm', 'value' => 'mango'), array('key' => 'p', 'value' => 'pineapple'));
     $data = array('id' => '', 'name' => 'List of Fruits', 'code' => 'fruits', 'items' => $fruits, 'canDelete' => false);
     LexOptionListCommands::updateList($project->id->asString(), $data);
     $optionlists->read();
     $this->assertEqual($optionlists->count, 2);
 }
 public function testInitializeNewProject_defaultPartOfSpeechOptionListExists()
 {
     $project = self::$environ->createProject(SF_TESTPROJECT, SF_TESTPROJECTCODE);
     $optionLists = new LexOptionListListModel($project);
     $optionLists->read();
     $this->assertCount(0, $optionLists->entries);
     $project = ProjectModel::getById($project->id->asString());
     $project->initializeNewProject();
     $optionLists->read();
     $this->assertTrue(count($optionLists->entries) > 0);
     $this->assertEquals('adj', $optionLists->entries[0]['items'][0]['key']);
 }
 public function testImportProjectZip_7zFile_StatsOkAndCustomFieldsImported()
 {
     $project = $this->environ->createProject(SF_TESTPROJECT, SF_TESTPROJECTCODE);
     $projectId = $project->id->asString();
     $fileName = 'TestLangProj.7z';
     // Ken Zook's test data
     $tmpFilePath = $this->environ->uploadFile(TestPath . "common/{$fileName}", $fileName);
     $userId = $this->environ->createUser('bob', 'bob', '*****@*****.**');
     $project->addUser($userId, LexiconRoles::OBSERVER);
     $project->config->userViews[$userId] = clone $project->config->roleViews[LexiconRoles::OBSERVER];
     $project->write();
     $this->assertFalse($project->config->entry->fieldOrder->array_search('customField_entry_Cust_Single_Line_All'), "custom field entry config doesn't yet exist");
     $this->assertFalse(array_key_exists('customField_entry_Cust_Single_Line_All', $project->config->entry->fields), "custom field entry config doesn't yet exist");
     $this->assertFalse(array_key_exists('customField_entry_Cust_Single_Line_All', $project->config->roleViews[LexiconRoles::OBSERVER]->fields), "custom field roleView config doesn't yet exist");
     $this->assertFalse(array_key_exists('customField_entry_Cust_Single_Line_All', $project->config->roleViews[LexiconRoles::MANAGER]->fields), "custom field roleView config doesn't yet exist");
     $this->assertFalse(array_key_exists('customField_entry_Cust_Single_Line_All', $project->config->userViews[$userId]->fields), "custom field userView config doesn't yet exist");
     $response = LexUploadCommands::importProjectZip($projectId, 'import-zip', $tmpFilePath);
     $project->read($project->id->asString());
     $filePath = $project->getAssetsFolderPath() . '/' . $response->data->fileName;
     $projectSlug = $project->databaseName();
     $entryList = new LexEntryListModel($project);
     $entryList->read();
     $entries = $entryList->entries;
     $entriesByGuid = $this->environ->indexItemsBy($entries, 'guid');
     $entryA = $entriesByGuid['05c54cf0-4e5a-4bf2-99f8-ec787e4113ac'];
     $entryB = $entriesByGuid['1a705846-a814-4289-8594-4b874faca6cc'];
     $entryBSensesByLiftId = $this->environ->indexItemsBy($entryB['senses'], 'liftId');
     $entryBSenseA = $entryBSensesByLiftId['eea9c29f-244f-4891-81db-c8274cd61f0c'];
     $optionListList = new LexOptionListListModel($project);
     $optionListList->read();
     $optionListByCodes = $this->environ->indexItemsBy($optionListList->entries, 'code');
     // stats OK?
     $this->assertTrue($response->result, 'Import should succeed');
     $this->assertPattern("/lexicon\\/{$projectSlug}/", $response->data->path, 'Uploaded zip file path should be in the right location');
     $this->assertEqual($fileName, $response->data->fileName, 'Uploaded zip fileName should have the original fileName');
     $this->assertTrue(file_exists($filePath), 'Uploaded zip file should exist');
     $this->assertEqual($response->data->stats->existingEntries, 0);
     $this->assertEqual($response->data->stats->importEntries, 64);
     $this->assertEqual($response->data->stats->newEntries, 64);
     $this->assertEqual($response->data->stats->entriesMerged, 0);
     $this->assertEqual($response->data->stats->entriesDuplicated, 0);
     $this->assertEqual($response->data->stats->entriesDeleted, 0);
     // custom fields imported?
     $this->assertEqual($entryList->count, 64);
     $this->assertEqual($optionListList->count, 24);
     $this->assertTrue(array_key_exists('grammatical-info', $optionListByCodes));
     $this->assertFalse(array_key_exists('semantic-domain-ddp4', $optionListByCodes));
     $this->assertEqual($entryA['lexeme']['qaa-fonipa-x-kal']['value'], '-kes');
     $this->assertEqual($entryA['customFields']['customField_entry_Cust_Single_Line_All']['en']['value'], '635459584141806142kes.wav');
     $this->assertTrue($project->config->entry->fieldOrder->array_search('customField_entry_Cust_Single_Line_All'), "custom field entry config exists");
     $this->assertTrue(array_key_exists('customField_entry_Cust_Single_Line_All', $project->config->entry->fields), "custom field entry config exists");
     $this->assertEqual($project->config->entry->fields['customField_entry_Cust_Single_Line_All']->label, 'Cust Single Line All');
     $this->assertEqual($project->config->entry->fields['customField_entry_Cust_Single_Line_All']->type, 'multitext');
     $this->assertTrue($project->config->entry->fields['customField_entry_Cust_Single_Line_All']->inputSystems->array_search('en'));
     $this->assertTrue(array_key_exists('customField_entry_Cust_Single_Line_All', $project->config->roleViews[LexiconRoles::OBSERVER]->fields), "custom field roleView config exists");
     $this->assertTrue(array_key_exists('customField_entry_Cust_Single_Line_All', $project->config->roleViews[LexiconRoles::MANAGER]->fields), "custom field roleView config exists");
     $this->assertTrue($project->config->roleViews[LexiconRoles::OBSERVER]->fields['customField_entry_Cust_Single_Line_All']->show);
     $this->assertTrue($project->config->roleViews[LexiconRoles::MANAGER]->fields['customField_entry_Cust_Single_Line_All']->show);
     $this->assertTrue(array_key_exists('customField_entry_Cust_Single_Line_All', $project->config->userViews[$userId]->fields), "custom field userView config doesn't yet exist");
     $this->assertTrue($project->config->userViews[$userId]->fields['customField_entry_Cust_Single_Line_All']->show);
     $this->assertEqual($entryB['lexeme']['qaa-fonipa-x-kal']['value'], 'zitʰɛstmen');
     $this->assertEqual($entryB['customFields']['customField_entry_Cust_Single_ListRef']['value'], 'comparative linguistics');
     $this->assertEqual(count($entryBSenseA['customFields']['customField_senses_Cust_Multi_ListRef']['values']), 2);
     $this->assertEqual($entryBSenseA['customFields']['customField_senses_Cust_Multi_ListRef']['values'][0], 'First Custom Item');
     $this->assertEqual($entryBSenseA['customFields']['customField_senses_Cust_Multi_ListRef']['values'][1], 'Second Custom Item');
     $this->assertEqual($entryBSenseA['examples'][0]['customFields']['customField_examples_Cust_Example']['qaa-x-kal']['value'], 'Custom example');
     /*
     echo '<pre style="height:500px; overflow:auto">';
     echo $response->data->importErrors;
     echo '</pre>';
     */
 }