<commit_before><?php

require_once 'Author.php';
require_once 'KeyTerm.php';
require_once 'StudyField.php';
require_once 'Publication.php';

class BrowseModel {

	private $db;
	private $allowed_types = array('author', 'key_term', 'study_field');	// TODO: use this!
	private $browse_type;
	private $browse_list = array();
	private $browse_result = array();
	private $browse_num;


	public function __construct($type, $id, Database $db) {

		$this -> db = $db;
		$this -> browse_type = $type;

		switch ($this -> browse_type) {

			case 'author':

				$this -> browse_list = $this -> fetchAuthors();
				$this -> browse_num = $this -> db -> getNumData();
				break;

			case 'key_term':

				if ($id > 0) {
					$this -> fetchPublications(array('key_term_id' => $id));
					$this -> browse_num = $this -> db -> getNumData();
					if ($this -> browse_num > 0) {
						$this -> fetchPublicationsAuthors();
					}
				}
				else {
					$this -> browse_list = $this -> fetchKeyTerms();
					$this -> browse_num = $this -> db -> getNumData();
				}
				break;
			
			case 'study_field':
				
				if ($id > 0) {
					$this -> fetchPublications(array('study_field_id' => $id));
					$this -> browse_num = $this -> db -> getNumData();
					if ($this -> browse_num > 0) {
						$this -> fetchPublicationsAuthors();
					}
				}
				else {
					$this -> browse_list = $this -> fetchStudyFields();
					$this -> browse_num = $this -> db -> getNumData();
				}
				break;

			// case 'year':
			// 	if ($id > 0) {

			// 		$this -> browse_list = $this -> fetchMonths();
			// 		$this -> fetchPublications(array('year' => $id));
			// 		$this -> browse_num = $this -> db -> getNumData();
			// 		if ($this -> browse_num > 0) {
			// 			$this -> fetchPublicationsAuthors();
			// 		}
			// 	}
			// 	else {
			// 		$this -> browse_list = $this -> fetchYears();
			// 		$this -> browse_num = $this -> db -> getNumData();
			// 	}
			// 	break;

			// case 'year:month':
			// 	if ($id > 0) {
			// 		$year = strtok($id, ':');
			// 		$month = strtok(':');

			// 		if (empty($month)) {
			// 			$this -> browse_list = $this -> fetchMonths();
			// 			$this -> fetchPublications(array('year' => $id));
			// 		}
			// 		else {
			// 			$this -> fetchPublications(array('year' => $year, 'month' => $month));
			// 		}

			// 		$this -> browse_num = $this -> db -> getNumData();
			// 		if ($this -> browse_num > 0) {
			// 			$this -> fetchPublicationsAuthors();
			// 		}
			// 	}
			// 	break;
			default:
				# code...
				break;
		}
	}


	public function getBrowseType() {
		return $this -> browse_type;
	}


	public function getBrowseList() {
		return $this -> browse_list;
	}


	public function getBrowseResult() {
		return $this -> browse_result;
	}


	public function getBrowseNum() {
		return $this -> browse_num;
	}


	public function isBrowseResult() {
		if (empty($this -> browse_result)) {
			return false;
		}
		else {
			return true;
		}
	}

	private function fetchAuthors() {
		$data = $this -> db -> fetchAuthors();

		foreach ($data as $key => $value) {
			$authors[] = new Author($value);
		}
<commit_msg>fixed behaviour when no browse result is found<commit_after><?php

require_once 'Author.php';
require_once 'KeyTerm.php';
require_once 'StudyField.php';
require_once 'Publication.php';

class BrowseModel {

	private $db;
	private $allowed_types = array('author', 'key_term', 'study_field');	// TODO: use this!
	private $browse_type;
	private $browse_list = array();
	private $browse_result = array();
	private $is_result = false;
	private $browse_num;


	public function __construct($type, $id, Database $db) {

		$this -> db = $db;
		$this -> browse_type = $type;

		switch ($this -> browse_type) {

			case 'author':

				$this -> browse_list = $this -> fetchAuthors();
				$this -> browse_num = $this -> db -> getNumData();
				break;

			case 'key_term':

				if ($id > 0) {
					$this -> is_result = true;
					$this -> fetchPublications(array('key_term_id' => $id));
					$this -> browse_num = $this -> db -> getNumData();
					if ($this -> browse_num > 0) {
						$this -> fetchPublicationsAuthors();
					}
				}
				else {
					$this -> browse_list = $this -> fetchKeyTerms();
					$this -> browse_num = $this -> db -> getNumData();
				}
				break;
			
			case 'study_field':
				
				if ($id > 0) {
					$this -> is_result = true;
					$this -> fetchPublications(array('study_field_id' => $id));
					$this -> browse_num = $this -> db -> getNumData();
					if ($this -> browse_num > 0) {
						$this -> fetchPublicationsAuthors();
					}
				}
				else {
					$this -> browse_list = $this -> fetchStudyFields();
					$this -> browse_num = $this -> db -> getNumData();
				}
				break;

			// case 'year':
			// 	if ($id > 0) {

			// 		$this -> browse_list = $this -> fetchMonths();
			// 		$this -> fetchPublications(array('year' => $id));
			// 		$this -> browse_num = $this -> db -> getNumData();
			// 		if ($this -> browse_num > 0) {
			// 			$this -> fetchPublicationsAuthors();
			// 		}
			// 	}
			// 	else {
			// 		$this -> browse_list = $this -> fetchYears();
			// 		$this -> browse_num = $this -> db -> getNumData();
			// 	}
			// 	break;

			// case 'year:month':
			// 	if ($id > 0) {
			// 		$year = strtok($id, ':');
			// 		$month = strtok(':');

			// 		if (empty($month)) {
			// 			$this -> browse_list = $this -> fetchMonths();
			// 			$this -> fetchPublications(array('year' => $id));
			// 		}
			// 		else {
			// 			$this -> fetchPublications(array('year' => $year, 'month' => $month));
			// 		}

			// 		$this -> browse_num = $this -> db -> getNumData();
			// 		if ($this -> browse_num > 0) {
			// 			$this -> fetchPublicationsAuthors();
			// 		}
			// 	}
			// 	break;
			default:
				# code...
				break;
		}
	}


	public function getBrowseType() {
		return $this -> browse_type;
	}


	public function getBrowseList() {
		return $this -> browse_list;
	}


	public function getBrowseResult() {
		return $this -> browse_result;
	}


	public function getBrowseNum() {
		return $this -> browse_num;
	}


	public function isBrowseResult() {
		return $this -> is_result;
	}

	private function fetchAuthors() {
		$data = $this -> db -> fetchAuthors();

		foreach ($data as $key => $value) {
			$authors[] = new Author($value);
		}

{'fraction_non_alphanumeric': 0.18897386870618227, 'fraction_numerical': 0.0025493945188017845, 'mean_word_length': 2.3246822033898304, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 153, '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': '7623466', 'n_tokens_mistral': 2812, 'n_tokens_neox': 2214, 'n_words': 493}