mirror of
				https://github.com/Alamantus/Lexiconga.git
				synced 2025-11-04 10:17:01 +01:00 
			
		
		
		
	Enable words to be deleted.
This commit is contained in:
		
							parent
							
								
									662d133ca7
								
							
						
					
					
						commit
						744506bdc3
					
				
					 4 changed files with 67 additions and 3 deletions
				
			
		| 
						 | 
					@ -189,10 +189,12 @@ WHERE dictionary=$dictionary";
 | 
				
			||||||
    return array();
 | 
					    return array();
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  public function setWords ($dictionary, $words = array()) {
 | 
					  public function setWords ($user, $dictionary, $words = array()) {
 | 
				
			||||||
    $query = 'INSERT INTO words (dictionary, word_id, name, pronunciation, part_of_speech, definition, details, last_updated, created_on) VALUES ';
 | 
					    $query = 'INSERT INTO words (dictionary, word_id, name, pronunciation, part_of_speech, definition, details, last_updated, created_on) VALUES ';
 | 
				
			||||||
    $params = array();
 | 
					    $params = array();
 | 
				
			||||||
 | 
					    $word_ids = array();
 | 
				
			||||||
    foreach($words as $word) {
 | 
					    foreach($words as $word) {
 | 
				
			||||||
 | 
					      $word_ids[] = $word['id'];
 | 
				
			||||||
      $query .= "(?, ?, ?, ?, ?, ?, ?, ?, ?), ";
 | 
					      $query .= "(?, ?, ?, ?, ?, ?, ?, ?, ?), ";
 | 
				
			||||||
      $params[] = $dictionary;
 | 
					      $params[] = $dictionary;
 | 
				
			||||||
      $params[] = $word['id'];
 | 
					      $params[] = $word['id'];
 | 
				
			||||||
| 
						 | 
					@ -213,6 +215,29 @@ details=VALUES(details),
 | 
				
			||||||
last_updated=VALUES(last_updated)';
 | 
					last_updated=VALUES(last_updated)';
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    $results = $this->db->execute($query, $params);
 | 
					    $results = $this->db->execute($query, $params);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if ($results) {
 | 
				
			||||||
 | 
					      $database_words = $this->getWords($user, $dictionary);
 | 
				
			||||||
 | 
					      $database_ids = array_map(function($database_word) { return $database_word['id']; }, $database_words);
 | 
				
			||||||
 | 
					      $words_to_delete = array_filter($database_ids, function($database_id) use($word_ids) { return !in_array($database_id, $word_ids); });
 | 
				
			||||||
 | 
					      if ($words_to_delete) {
 | 
				
			||||||
 | 
					        $delete_results = $this->deleteWords($dictionary, $words_to_delete);
 | 
				
			||||||
 | 
					        return $delete_results;
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return $results;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  public function deleteWords ($dictionary, $word_ids) {
 | 
				
			||||||
 | 
					    $query = 'DELETE FROM words WHERE dictionary=? AND word_id IN (';
 | 
				
			||||||
 | 
					    $params = array($dictionary);
 | 
				
			||||||
 | 
					    foreach($word_ids as $word_id) {
 | 
				
			||||||
 | 
					      $query .= '?, ';
 | 
				
			||||||
 | 
					      $params[] = $word_id;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    $query = trim($query, ', ') . ')';
 | 
				
			||||||
 | 
					    $results = $this->db->execute($query, $params);
 | 
				
			||||||
    return $results;
 | 
					    return $results;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -203,8 +203,23 @@ VALUES (?, ?, ?, ?, ?, ?)';
 | 
				
			||||||
    $user_data = $this->token->decode($token);
 | 
					    $user_data = $this->token->decode($token);
 | 
				
			||||||
    if ($user_data !== false) {
 | 
					    if ($user_data !== false) {
 | 
				
			||||||
      $dictionary = $user_data->dictionary;
 | 
					      $dictionary = $user_data->dictionary;
 | 
				
			||||||
      $updated_words = $this->dictionary->setWords($dictionary, $words);
 | 
					      $user = $user_data->id;
 | 
				
			||||||
      if ($updated_words > 0) {
 | 
					      $updated_words = $this->dictionary->setWords($user, $dictionary, $words);
 | 
				
			||||||
 | 
					      if ($updated_words) {
 | 
				
			||||||
 | 
					        return true;
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return false;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  public function deleteWordFromCurrentDictionary ($token, $word_id) {
 | 
				
			||||||
 | 
					    // Useful even for just one word
 | 
				
			||||||
 | 
					    $user_data = $this->token->decode($token);
 | 
				
			||||||
 | 
					    if ($user_data !== false) {
 | 
				
			||||||
 | 
					      $dictionary = $user_data->dictionary;
 | 
				
			||||||
 | 
					      $user = $user_data->id;
 | 
				
			||||||
 | 
					      $deleted_word = $this->dictionary->deleteWords($dictionary, array($word_id));
 | 
				
			||||||
 | 
					      if ($deleted_word) {
 | 
				
			||||||
        return true;
 | 
					        return true;
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -244,6 +244,26 @@ switch ($action) {
 | 
				
			||||||
      'error' => true,
 | 
					      'error' => true,
 | 
				
			||||||
    ), 400);
 | 
					    ), 400);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					  case 'delete-word': {
 | 
				
			||||||
 | 
					    if ($token !== false && isset($request['word'])) {
 | 
				
			||||||
 | 
					      $user = new User();
 | 
				
			||||||
 | 
					      $delete_word_success = $user->deleteWordFromCurrentDictionary($token, $request['word']);
 | 
				
			||||||
 | 
					      if ($delete_word_success !== false) {
 | 
				
			||||||
 | 
					        return Response::json(array(
 | 
				
			||||||
 | 
					          'data' => 'Deleted successfully',
 | 
				
			||||||
 | 
					          'error' => false,
 | 
				
			||||||
 | 
					        ), 200);
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					      return Response::json(array(
 | 
				
			||||||
 | 
					        'data' => 'Could not delete word: invalid token',
 | 
				
			||||||
 | 
					        'error' => true,
 | 
				
			||||||
 | 
					      ), 401);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return Response::json(array(
 | 
				
			||||||
 | 
					      'data' => 'Could not delete word: required data missing',
 | 
				
			||||||
 | 
					      'error' => true,
 | 
				
			||||||
 | 
					    ), 400);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  default: {
 | 
					  default: {
 | 
				
			||||||
    return Response::html('Hi!');
 | 
					    return Response::html('Hi!');
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -73,6 +73,10 @@ export class Word {
 | 
				
			||||||
    return wordDb.words.delete(wordId)
 | 
					    return wordDb.words.delete(wordId)
 | 
				
			||||||
    .then(() => {
 | 
					    .then(() => {
 | 
				
			||||||
      console.log('Word deleted successfully');
 | 
					      console.log('Word deleted successfully');
 | 
				
			||||||
 | 
					      request('delete-word', {
 | 
				
			||||||
 | 
					        token: store.get('LexicongaToken'),
 | 
				
			||||||
 | 
					        word: wordId,
 | 
				
			||||||
 | 
					      }, response => console.log(response));
 | 
				
			||||||
    })
 | 
					    })
 | 
				
			||||||
    .catch(error => {
 | 
					    .catch(error => {
 | 
				
			||||||
      console.error(error);
 | 
					      console.error(error);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue