<commit_before><?php
class SentencesController extends AppController{
	var $name = 'Sentences';
	var $components = array ('GoogleLanguageApi', 'Lucene', 'Permissions');
	var $helpers = array('Sentences', 'Html', 'Logs', 'Pagination', 'Comments', 'Navigation', 'Languages', 'Javascript');
	
	function beforeFilter() {
	    parent::beforeFilter();
		
		// setting actions that are available to everyone, even guests
	    $this->Auth->allowedActions = array('index','show','search', 'add_comment', 'random', 'goTo', 'statistics', 'count_unknown_language', 'get_translations' , 'check_translation', 'map');
	}

	
	function index(){
		$this->redirect('/sentences/show/random');
	}
	
	function show($id = null){
		$languages = array('ar', 'de', 'en', 'es', 'fi', 'fr', 'he', 'it', 'id', 'jp', 'ko', 'nl', 'pt', 'ru', 'vn', 'zh');
		$this->Sentence->recursive = 0;
		
		if($id == "random" OR $id == null){
			
			$resultMax = $this->Sentence->query('SELECT MAX(id) FROM sentences');
			$max = $resultMax[0][0]['MAX(id)'];
			
			$randId = rand(1, $max);
			$this->Session->write('random_lang_selected', "random");
			$this->redirect(array("action"=>"show", $randId));
			
		}elseif(in_array($id, $languages)){
			
			$conditions['Sentence.lang'] = $id;
			$random = $this->Sentence->find(
				'first', 
				array(
					'conditions' => $conditions,
					'order' => 'RAND()'
				)
			);
			$this->Session->write('random_lang_selected', $id);
			$this->redirect(array("action"=>"show", $random['Sentence']['id']));
			
		}else{
		
			$this->Sentence->id = $id;
			
			$sentence = $this->Sentence->read();
			$this->set('sentence', $sentence);

			// checking which options user can access to
			$specialOptions = $this->Permissions->getSentencesOptions($sentence['Sentence']['user_id'], $this->Auth->user('id'));
			$this->set('specialOptions',$specialOptions);
			
		}
	}
	
	function goTo(){
		$id = intval($this->params['url']['sentence_id']);
		if($id == 0){
			$id = 'random';
		}
		$this->redirect(array("action"=>"show", $id));
	}
	
	function add(){
		if(!empty($this->data)){
			// setting correctness of sentence
			if($this->Auth->user('group_id')){
				$this->data['Sentence']['correctness'] = Sentence::MAX_CORRECTNESS - $this->Auth->user('group_id');
			}else{
				$this->data['Sentence']['correctness'] = 1;
			}
			
			// detecting language
			$this->GoogleLanguageApi->text = $this->data['Sentence']['text'];
			$response = $this->GoogleLanguageApi->detectLang();
			if($response['isReliable']){
				$this->data['Sentence']['lang'] = $this->GoogleLanguageApi->google2TatoebaCode($response['language']);
			}else{
				$this->data['Sentence']['lang'] = null;
			}
			
			$this->data['Sentence']['user_id'] = $this->Auth->user('id');
			
			// saving
			if($this->Sentence->save($this->data)){
				$this->data = null;
				
				$this->set('sentence', $this->Sentence->read());
				
				// checking which options user can access to
				$specialOptions = $this->Permissions->getSentencesOptions($this->Auth->user('id'), $this->Auth->user('id'));
				$this->set('specialOptions',$specialOptions);
			}
		}
	}
	
	function delete($id){
		$this->Sentence->id = $id;
		
		// for the logs
		$this->Sentence->recursive = 1;
		$this->Sentence->read();
		$this->Sentence->data['User']['id'] = $this->Auth->user('id'); 
		
		//$this->Sentence->del($id, true); 
		// TODO : Deleting with del does not delete the right entries in sentences_translations.
		// But I didn't figure out how to solve that =_=;
		// So I'm just going to do something not pretty but whatever, I'm tired!!!
		$this->Sentence->query('DELETE FROM sentences WHERE id='.$id);
		$this->Sentence->query('DELETE FROM sentences_translations WHERE sentence_id='.$id);
		$this->Sentence->query('DELETE FROM sentences_translations WHERE translation_id='.$id);
		
		// need to call afterDelete() manually for the logs
		$this->Sentence->afterDelete();
		
		$this->flash('The sentence #'.$id.' has been deleted.', '/contributions/show/'.$id);
	}

	function edit($id){
		$this->Sentence->id = $id;
		if(empty($this->data)){
			$sentence = $this->Sentence->read();
			if($this->Auth->user('group_id') < 3 OR 
			$this->Auth->user('id') == $sentence['Sentence']['user_id']){
				$this->Sentence->recursive = 2;
				$this->data = $this->Sentence->read();
				$this->set('sentence', $this->data);
				$specialOptions = $this->Permissions->getSentencesOptions($this->data['Sentence']['user_id'], $this->Auth->user('id'));
				$this->set('specialOptions',$specialOptions);
			}else{
				$this->flash(
					__('This sentence was not added by you. You can only edit sentences you have added.',true),
					'/sentences/show/'.$id
				);
			}
		}else{
			$this->data['Sentence']['user_id'] = $this->Auth->user('id'); // for the logs
			if($this->Sentence->save($this->data)){
				// Confirmation message
				$this->flash(
					__('The sentence has been updated.',true),
					'/sentences/edit/'.$id
				);
			}
		}
	}
	
	function save_sentence(){
		if(isset($_POST['value'])){
			if(isset($_POST['id'])){
				if(preg_match("/[a-z]/", $_POST['id'])){
					$this->Sentence->id = substr($_POST['id'], 2);
					$this->data['Sentence']['lang'] = substr($_POST['id'], 0, 2); // language needed for the logs
				}else{
					$this->Sentence->id = $_POST['id'];
					$this->data['Sentence']['lang'] = null; // language needed for the logs
				}
				$this->data['Sentence']['text'] = rtrim($_POST['value']);
				$this->data['Sentence']['user_id'] = $this->Auth->user('id'); // for the logs
				
				if($this->Sentence->save($this->data)){
					Configure::write('debug',0);
					$this->layout = null;
					$this->set('sentence_text', rtrim($_POST['value']));
				}
			}else{
				// setting correctness of sentence
				if($this->Auth->user('group_id')){
					$this->data['Sentence']['correctness'] = Sentence::MAX_CORRECTNESS - $this->Auth->user('group_id');
				}else{
					$this->data['Sentence']['correctness'] = 1;
				}
				
				// detecting language
				$this->GoogleLanguageApi->text = $this->data['Sentence']['text'];
				$response = $this->GoogleLanguageApi->detectLang();
				if($response['isReliable']){
					$this->data['Sentence']['lang'] = $this->GoogleLanguageApi->google2TatoebaCode($response['language']);
				}else{
					$this->data['Sentence']['lang'] = null;
				}
				
				$this->data['Sentence']['user_id'] = $this->Auth->user('id');
				$this->data['Sentence']['text'] = $_POST['value'];
				
				// saving
				if($this->Sentence->save($this->data)){
					Configure::write('debug',0);
					$this->layout = null;
					$this->set('sentence', $this->Sentence->read());
					// checking which options user can access to
					$specialOptions = $this->Permissions->getSentencesOptions($this->Auth->user('id'), $this->Auth->user('id'));
					$this->set('specialOptions',$specialOptions);
				}
			}
		}
	}
	
	function adopt($id){
		$data['Sentence']['id'] = $id;
		$data['Sentence']['user_id'] = $this->Auth->user('id');
		if($this->Sentence->save($data)){
			$this->flash(
				__('You are now the owner of this sentence and can modify it as you wish. It is your responsibility to make sure that it doesn\'t have any mistake and, if possible, is not linked to wrong translations.',true),
				'/sentences/show/'.$id
			);
		}
	}
	
	function let_go($id){
		$data['Sentence']['id'] = $id;
		$data['Sentence']['user_id'] = null;
		if($this->Sentence->save($data)){
			$this->flash(
				__('You have abandonned your ownership for this sentence. Other people can now adopt it. If it was a mistake, you can just re-adopt it.',true),
				'/sentences/show/'.$id
			);
		}
	}

	
	// we always check the translation before we add it
	function check_translation(){
		if(isset($_POST['value']) AND rtrim($_POST['value']) != '' AND isset($_POST['id'])){
			$sentenceId = substr($_POST['id'], 2);
			$sourceLanguage = substr($_POST['id'], 0, 2); // language of the original sentence
			
			// detecting language of translation
			$this->GoogleLanguageApi->text = $_POST['value'];
			$response = $this->GoogleLanguageApi->detectLang();
			if($response['isReliable']){
				$this->data['Sentence']['lang'] = $this->GoogleLanguageApi->google2TatoebaCode($response['language']);
			}else{
				$this->data['Sentence']['lang'] = null;
			}
			
			// checking if same language...
			if ($sourceLanguage == $this->data['Sentence']['lang'] ) { 
				// it will display a warning
				$this->set('sentence_id', $sentenceId);
				$this->set('translation_text', $_POST['value']);
			}else{
				// we save
				$this->save_translation();
				// note : for some reason it won't redirect to save_translation.ctp...
				// so we have to do everything in the check_translation.ctp
			}		
		}
	}
	
	function save_translation(){
		if(isset($_POST['value']) AND rtrim($_POST['value']) != '' AND isset($_POST['id'])){
			$sentence_id = substr($_POST['id'], 2); // id of original sentence
			$this->data['Sentence']['sentence_lang'] = substr($_POST['id'], 0, 2); // language of original sentence, needed for the logs
			
			// If we want the "HasAndBelongsToMany" association to work, we need the two lines below :			
			$this->Sentence->id = $sentence_id;
			$this->data['Translation']['Translation'][] = $sentence_id;
			
			// And this is because the translations are reciprocal :
			$this->data['InverseTranslation']['InverseTranslation'][] = $sentence_id;
			
			$this->data['Sentence']['id'] = null; // so that it saves a new sentences, otherwise it's like editing
			
			// setting level of correctness
			if($this->Auth->user('group_id')){
				$this->data['Sentence']['correctness'] = Sentence::MAX_CORRECTNESS - $this->Auth->user('group_id');
			}else{
				$this->data['Sentence']['correctness'] = 1;
			}
			
			// In the case where the user had to confirm saving, $this->data['Sentence']['lang'] is not set 
			// and the language of the translation is the same as the language of the sentence.
			if(!isset($this->data['Sentence']['lang'])){
				$this->data['Sentence']['lang'] = $this->data['Sentence']['sentence_lang'];
			}
			
			$this->data['Sentence']['text'] = $_POST['value'];
			$this->data['Sentence']['user_id'] = $this->Auth->user('id');		 	
			
			if($this->Sentence->save($this->data)){
				Configure::write('debug',0);
				$this->set('translation_id', $this->Sentence->id);
				$this->set('translation_lang', $this->data['Sentence']['lang']);
				$this->set('translation_text', $_POST['value']);
			}
		}
	}<commit_msg>Fix for translation language being set to original language when language detection is not reliable. Remember : when $var == null, isset($var) returns false.<commit_after><?php
class SentencesController extends AppController{
	var $name = 'Sentences';
	var $components = array ('GoogleLanguageApi', 'Lucene', 'Permissions');
	var $helpers = array('Sentences', 'Html', 'Logs', 'Pagination', 'Comments', 'Navigation', 'Languages', 'Javascript');
	
	function beforeFilter() {
	    parent::beforeFilter();
		
		// setting actions that are available to everyone, even guests
	    $this->Auth->allowedActions = array('index','show','search', 'add_comment', 'random', 'goTo', 'statistics', 'count_unknown_language', 'get_translations' , 'check_translation', 'map');
	}

	
	function index(){
		$this->redirect('/sentences/show/random');
	}
	
	function show($id = null){
		$languages = array('ar', 'de', 'en', 'es', 'fi', 'fr', 'he', 'it', 'id', 'jp', 'ko', 'nl', 'pt', 'ru', 'vn', 'zh');
		$this->Sentence->recursive = 0;
		
		if($id == "random" OR $id == null){
			
			$resultMax = $this->Sentence->query('SELECT MAX(id) FROM sentences');
			$max = $resultMax[0][0]['MAX(id)'];
			
			$randId = rand(1, $max);
			$this->Session->write('random_lang_selected', "random");
			$this->redirect(array("action"=>"show", $randId));
			
		}elseif(in_array($id, $languages)){
			
			$conditions['Sentence.lang'] = $id;
			$random = $this->Sentence->find(
				'first', 
				array(
					'conditions' => $conditions,
					'order' => 'RAND()'
				)
			);
			$this->Session->write('random_lang_selected', $id);
			$this->redirect(array("action"=>"show", $random['Sentence']['id']));
			
		}else{
		
			$this->Sentence->id = $id;
			
			$sentence = $this->Sentence->read();
			$this->set('sentence', $sentence);

			// checking which options user can access to
			$specialOptions = $this->Permissions->getSentencesOptions($sentence['Sentence']['user_id'], $this->Auth->user('id'));
			$this->set('specialOptions',$specialOptions);
			
		}
	}
	
	function goTo(){
		$id = intval($this->params['url']['sentence_id']);
		if($id == 0){
			$id = 'random';
		}
		$this->redirect(array("action"=>"show", $id));
	}
	
	function add(){
		if(!empty($this->data)){
			// setting correctness of sentence
			if($this->Auth->user('group_id')){
				$this->data['Sentence']['correctness'] = Sentence::MAX_CORRECTNESS - $this->Auth->user('group_id');
			}else{
				$this->data['Sentence']['correctness'] = 1;
			}
			
			// detecting language
			$this->GoogleLanguageApi->text = $this->data['Sentence']['text'];
			$response = $this->GoogleLanguageApi->detectLang();
			if($response['isReliable']){
				$this->data['Sentence']['lang'] = $this->GoogleLanguageApi->google2TatoebaCode($response['language']);
			}else{
				$this->data['Sentence']['lang'] = null;
			}
			
			$this->data['Sentence']['user_id'] = $this->Auth->user('id');
			
			// saving
			if($this->Sentence->save($this->data)){
				$this->data = null;
				
				$this->set('sentence', $this->Sentence->read());
				
				// checking which options user can access to
				$specialOptions = $this->Permissions->getSentencesOptions($this->Auth->user('id'), $this->Auth->user('id'));
				$this->set('specialOptions',$specialOptions);
			}
		}
	}
	
	function delete($id){
		$this->Sentence->id = $id;
		
		// for the logs
		$this->Sentence->recursive = 1;
		$this->Sentence->read();
		$this->Sentence->data['User']['id'] = $this->Auth->user('id'); 
		
		//$this->Sentence->del($id, true); 
		// TODO : Deleting with del does not delete the right entries in sentences_translations.
		// But I didn't figure out how to solve that =_=;
		// So I'm just going to do something not pretty but whatever, I'm tired!!!
		$this->Sentence->query('DELETE FROM sentences WHERE id='.$id);
		$this->Sentence->query('DELETE FROM sentences_translations WHERE sentence_id='.$id);
		$this->Sentence->query('DELETE FROM sentences_translations WHERE translation_id='.$id);
		
		// need to call afterDelete() manually for the logs
		$this->Sentence->afterDelete();
		
		$this->flash('The sentence #'.$id.' has been deleted.', '/contributions/show/'.$id);
	}

	function edit($id){
		$this->Sentence->id = $id;
		if(empty($this->data)){
			$sentence = $this->Sentence->read();
			if($this->Auth->user('group_id') < 3 OR 
			$this->Auth->user('id') == $sentence['Sentence']['user_id']){
				$this->Sentence->recursive = 2;
				$this->data = $this->Sentence->read();
				$this->set('sentence', $this->data);
				$specialOptions = $this->Permissions->getSentencesOptions($this->data['Sentence']['user_id'], $this->Auth->user('id'));
				$this->set('specialOptions',$specialOptions);
			}else{
				$this->flash(
					__('This sentence was not added by you. You can only edit sentences you have added.',true),
					'/sentences/show/'.$id
				);
			}
		}else{
			$this->data['Sentence']['user_id'] = $this->Auth->user('id'); // for the logs
			if($this->Sentence->save($this->data)){
				// Confirmation message
				$this->flash(
					__('The sentence has been updated.',true),
					'/sentences/edit/'.$id
				);
			}
		}
	}
	
	function save_sentence(){
		if(isset($_POST['value'])){
			if(isset($_POST['id'])){
				if(preg_match("/[a-z]/", $_POST['id'])){
					$this->Sentence->id = substr($_POST['id'], 2);
					$this->data['Sentence']['lang'] = substr($_POST['id'], 0, 2); // language needed for the logs
				}else{
					$this->Sentence->id = $_POST['id'];
					$this->data['Sentence']['lang'] = null; // language needed for the logs
				}
				$this->data['Sentence']['text'] = rtrim($_POST['value']);
				$this->data['Sentence']['user_id'] = $this->Auth->user('id'); // for the logs
				
				if($this->Sentence->save($this->data)){
					Configure::write('debug',0);
					$this->layout = null;
					$this->set('sentence_text', rtrim($_POST['value']));
				}
			}else{
				// setting correctness of sentence
				if($this->Auth->user('group_id')){
					$this->data['Sentence']['correctness'] = Sentence::MAX_CORRECTNESS - $this->Auth->user('group_id');
				}else{
					$this->data['Sentence']['correctness'] = 1;
				}
				
				// detecting language
				$this->GoogleLanguageApi->text = $this->data['Sentence']['text'];
				$response = $this->GoogleLanguageApi->detectLang();
				if($response['isReliable']){
					$this->data['Sentence']['lang'] = $this->GoogleLanguageApi->google2TatoebaCode($response['language']);
				}else{
					$this->data['Sentence']['lang'] = null;
				}
				
				$this->data['Sentence']['user_id'] = $this->Auth->user('id');
				$this->data['Sentence']['text'] = $_POST['value'];
				
				// saving
				if($this->Sentence->save($this->data)){
					Configure::write('debug',0);
					$this->layout = null;
					$this->set('sentence', $this->Sentence->read());
					// checking which options user can access to
					$specialOptions = $this->Permissions->getSentencesOptions($this->Auth->user('id'), $this->Auth->user('id'));
					$this->set('specialOptions',$specialOptions);
				}
			}
		}
	}
	
	function adopt($id){
		$data['Sentence']['id'] = $id;
		$data['Sentence']['user_id'] = $this->Auth->user('id');
		if($this->Sentence->save($data)){
			$this->flash(
				__('You are now the owner of this sentence and can modify it as you wish. It is your responsibility to make sure that it doesn\'t have any mistake and, if possible, is not linked to wrong translations.',true),
				'/sentences/show/'.$id
			);
		}
	}
	
	function let_go($id){
		$data['Sentence']['id'] = $id;
		$data['Sentence']['user_id'] = null;
		if($this->Sentence->save($data)){
			$this->flash(
				__('You have abandonned your ownership for this sentence. Other people can now adopt it. If it was a mistake, you can just re-adopt it.',true),
				'/sentences/show/'.$id
			);
		}
	}

	
	// we always check the translation before we add it
	function check_translation(){
		if(isset($_POST['value']) AND rtrim($_POST['value']) != '' AND isset($_POST['id'])){
			$sentenceId = substr($_POST['id'], 2);
			$sourceLanguage = substr($_POST['id'], 0, 2); // language of the original sentence
			
			// detecting language of translation
			$this->GoogleLanguageApi->text = $_POST['value'];
			$response = $this->GoogleLanguageApi->detectLang();
			if($response['isReliable']){
				$this->data['Sentence']['lang'] = $this->GoogleLanguageApi->google2TatoebaCode($response['language']);
			}else{
				$this->data['Sentence']['lang'] = null;
			}
			
			// checking if same language...
			if ($sourceLanguage == $this->data['Sentence']['lang'] ) { 
				// it will display a warning
				$this->set('sentence_id', $sentenceId);
				$this->set('translation_text', $_POST['value']);
			}else{
				// we save
				$this->data['Sentence']['differentLang'] = true; // so we know the sentence is not same language as original
				$this->save_translation();
				// note : for some reason it won't redirect to save_translation.ctp...
				// so we have to do everything in the check_translation.ctp
			}		
		}
	}
	
	function save_translation(){
		if(isset($_POST['value']) AND rtrim($_POST['value']) != '' AND isset($_POST['id'])){
			$sentence_id = substr($_POST['id'], 2); // id of original sentence
			$this->data['Sentence']['sentence_lang'] = substr($_POST['id'], 0, 2); // language of original sentence, needed for the logs
			
			// If we want the "HasAndBelongsToMany" association to work, we need the two lines below :			
			$this->Sentence->id = $sentence_id;
			$this->data['Translation']['Translation'][] = $sentence_id;
			
			// And this is because the translations are reciprocal :
			$this->data['InverseTranslation']['InverseTranslation'][] = $sentence_id;
			
			$this->data['Sentence']['id'] = null; // so that it saves a new sentences, otherwise it's like editing
			
			// setting level of correctness
			if($this->Auth->user('group_id')){
				$this->data['Sentence']['correctness'] = Sentence::MAX_CORRECTNESS - $this->Auth->user('group_id');
			}else{
				$this->data['Sentence']['correctness'] = 1;
			}
			
			// In the case where the user had to confirm saving, $this->data['Sentence']['differentLang'] is not set 
			// and the language of the translation is the same as the language of the sentence.
			if(!isset($this->data['Sentence']['differentLang'])){
				$this->data['Sentence']['lang'] = $this->data['Sentence']['sentence_lang'];
			}
			
			$this->data['Sentence']['text'] = $_POST['value'];
			$this->data['Sentence']['user_id'] = $this->Auth->user('id');		 	
			
			if($this->Sentence->save($this->data)){
				Configure::write('debug',0);
				$this->set('translation_id', $this->Sentence->id);
				$this->set('translation_lang', $this->data['Sentence']['lang']);
				$this->set('translation_text', $_POST['value']);
			}
		}
	}
{'fraction_non_alphanumeric': 0.2143430812113195, 'fraction_numerical': 0.002456654225917702, 'mean_word_length': 3.983050847457627, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 430, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2352633', 'n_tokens_mistral': 8070, 'n_tokens_neox': 6707, 'n_words': 1398}