Add methods for getting current dictionary

This commit is contained in:
Robbie Antenesse 2018-01-04 23:03:24 -07:00
parent 7af83732eb
commit 782559fd2c
2 changed files with 66 additions and 0 deletions

View File

@ -50,4 +50,57 @@ class Dictionary {
}
return array();
}
public function getDetails ($user, $dictionary) {
$query = "SELECT * FROM dictionaries JOIN dictionary_linguistics ON dictionary = id WHERE user=$user AND id=$dictionary";
$result = $this->db->query($query)->fetch();
if ($result) {
return array(
'id' => $this->token->hash($result['id']),
'name' => $result['name'],
'specification' => $result['specification'],
'description' => $result['description'],
'partsOfSpeech' => json_decode($result['parts_of_speech']),
'details' => array(
'phonology' => json_decode($result['phonology']),
'orthography' => array(
'notes' => $result['orthography_notes'],
),
'grammar' => array(
'notes' => $result['grammar_notes'],
),
),
'settings' => array(
'allowDuplicates' => $result['allow_duplicates'] === '1' ? true : false,
'caseSensitive' => $result['case_sensitive'] === '1' ? true : false,
'sortByDefinition' => $result['sort_by_definition'] === '1' ? true : false,
'isComplete' => $result['is_complete'] === '1' ? true : false,
'isPublic' => $result['is_public'] === '1' ? true : false,
),
'lastUpdated' => $result['last_updated'],
'createdOn' => $result['created_on'],
);
}
return false;
}
public function getWords ($user, $dictionary) {
$query = "SELECT words.* FROM words JOIN dictionaries ON id = dictionary WHERE dictionary=$dictionary AND user=$user";
$results = $this->db->query($query)->fetchAll();
if ($results) {
return array_map(function ($row) {
return array(
'id' => $result['word_id'],
'name' => $result['name'],
'pronunciation' => $result['pronunciation'],
'partOfSpeech' => $result['part_of_speech'],
'definition' => $result['definition'],
'details' => $result['details'],
'lastUpdated' => $result['last_updated'],
'createdOn' => $result['created_on'],
);
}, $results);
}
return false;
}
}

View File

@ -89,6 +89,19 @@ class User {
return false;
}
public function getCurrentDictionary ($token) {
$user_data = $this->token->decode($token);
if ($user_data !== false) {
$user = $user_data->id;
$dictionary = $user_data->current_dictionary;
return array(
'details' => $this->dictionary->getDetails($user, $dictionary),
'words' => $this->dictionary->getWords($user, $dictionary),
);
}
return false;
}
private function generateUserToken ($user_id, $dictionary_id) {
$user_data = array(
'id' => intval($user_id),