<commit_before><?php

class CM_FormField_Location extends CM_FormField_SuggestOne {

	/**
	 * @param string                     $name
	 * @param int|null                   $minLevel
	 * @param int|null                   $maxLevel
	 * @param CM_FormField_Distance|null $distance
	 */
	public function __construct($name, $minLevel = null, $maxLevel = null, CM_FormField_Distance $distance = null) {
		if (is_null($minLevel)) {
			$minLevel = CM_Model_Location::LEVEL_COUNTRY;
		}
		if (is_null($maxLevel)) {
			$maxLevel = CM_Model_Location::LEVEL_ZIP;
		}
		parent::__construct($name);
		$this->_options['levelMin'] = (int) $minLevel;
		$this->_options['levelMax'] = (int) $maxLevel;
		if ($distance) {
			$this->_options['distanceName'] = $distance->getName();
			$this->_options['distanceLevelMin'] = CM_Model_Location::LEVEL_CITY;
		}
	}

	public function getSuggestion($location, CM_Render $render) {
		$names = array();
		for ($level = $location->getLevel(); $level >= CM_Model_Location::LEVEL_COUNTRY; $level--) {
			$names[] = $location->getName($level);
		}
		return array('id' => $location->getLevel() . '.' . $location->getId(), 'name' => implode(', ', array_filter($names)),
			'img' => $render->getUrlResource('layout', 'img/flags/' . strtolower($location->getAbbreviation(CM_Model_Location::LEVEL_COUNTRY)) . '.png'));
	}

	public function validate($userInput, CM_Response_Abstract $response) {
		$value = parent::validate($userInput, $response);
		if (!preg_match('/^(\d+)\.(\d+)$/', $value, $matches)) {
			throw new CM_Exception_FormFieldValidation('Invalid input format');
		}
		$level = $matches[1];
		$id = $matches[2];
		if ($level < $this->_options['levelMin'] || $level > $this->_options['levelMax']) {
			throw new CM_Exception_FormFieldValidation('Invalid location level.');
		}
		return new CM_Model_Location($level, $id);
	}

	/**
	 * @param CM_Request_Abstract $request
	 */
	public function setValueByRequest(CM_Request_Abstract $request) {
		$requestLocation = $this->_getRequestLocationByRequest($request);
		if (null === $requestLocation) {
			return;
		}

		if ($requestLocation->getLevel() > $this->_options['levelMax']) {
			$requestLocation = $requestLocation->get($this->_options['levelMax']);
			if (null === $requestLocation) {
				return;
			}
		}

		if ($requestLocation->getLevel() < $this->_options['levelMin']) {
			$requestLocation = $requestLocation->get($this->_options['levelMin']);
			if (null === $requestLocation) {
				return;
			}
		}

		$this->setValue($requestLocation);
	}

	/**
	 * @param CM_Request_Abstract $request
	 * @return CM_Model_Location|null
	 */
	protected function _getRequestLocationByRequest(CM_Request_Abstract $request){
		$ip = $request->getIp();
		if (null === $ip) {
			return null;
		}

		return CM_Model_Location::findByIp($ip);
	}

	protected function _getSuggestions($term, array $options, CM_Render $render) {
		$ip = CM_Request_Abstract::getInstance()->getIp();
		$locations = new CM_Paging_Location_Suggestions($term, $options['levelMin'], $options['levelMax'], CM_Model_Location::findByIp($ip));
		$locations->setPage(1, 15);
		$out = array();
		foreach ($locations as $location) {
			$out[] = $this->getSuggestion($location, $render);
		}
		return $out;
	}
}
<commit_msg>Only try to get a location if location level ist bigger the max level. finding a higher level in a location is not possible.<commit_after><?php

class CM_FormField_Location extends CM_FormField_SuggestOne {

	/**
	 * @param string                     $name
	 * @param int|null                   $minLevel
	 * @param int|null                   $maxLevel
	 * @param CM_FormField_Distance|null $distance
	 */
	public function __construct($name, $minLevel = null, $maxLevel = null, CM_FormField_Distance $distance = null) {
		if (is_null($minLevel)) {
			$minLevel = CM_Model_Location::LEVEL_COUNTRY;
		}
		if (is_null($maxLevel)) {
			$maxLevel = CM_Model_Location::LEVEL_ZIP;
		}
		parent::__construct($name);
		$this->_options['levelMin'] = (int) $minLevel;
		$this->_options['levelMax'] = (int) $maxLevel;
		if ($distance) {
			$this->_options['distanceName'] = $distance->getName();
			$this->_options['distanceLevelMin'] = CM_Model_Location::LEVEL_CITY;
		}
	}

	public function getSuggestion($location, CM_Render $render) {
		$names = array();
		for ($level = $location->getLevel(); $level >= CM_Model_Location::LEVEL_COUNTRY; $level--) {
			$names[] = $location->getName($level);
		}
		return array('id' => $location->getLevel() . '.' . $location->getId(), 'name' => implode(', ', array_filter($names)),
			'img' => $render->getUrlResource('layout', 'img/flags/' . strtolower($location->getAbbreviation(CM_Model_Location::LEVEL_COUNTRY)) . '.png'));
	}

	public function validate($userInput, CM_Response_Abstract $response) {
		$value = parent::validate($userInput, $response);
		if (!preg_match('/^(\d+)\.(\d+)$/', $value, $matches)) {
			throw new CM_Exception_FormFieldValidation('Invalid input format');
		}
		$level = $matches[1];
		$id = $matches[2];
		if ($level < $this->_options['levelMin'] || $level > $this->_options['levelMax']) {
			throw new CM_Exception_FormFieldValidation('Invalid location level.');
		}
		return new CM_Model_Location($level, $id);
	}

	/**
	 * @param CM_Request_Abstract $request
	 */
	public function setValueByRequest(CM_Request_Abstract $request) {
		$requestLocation = $this->_getRequestLocationByRequest($request);
		if (null === $requestLocation || $requestLocation->getLevel() < $this->_options['levelMin']) {
			return;
		}

		if ($requestLocation->getLevel() > $this->_options['levelMax']) {
			$requestLocation = $requestLocation->get($this->_options['levelMax']);
			if (null === $requestLocation) {
				return;
			}
		}

		$this->setValue($requestLocation);
	}

	/**
	 * @param CM_Request_Abstract $request
	 * @return CM_Model_Location|null
	 */
	protected function _getRequestLocationByRequest(CM_Request_Abstract $request){
		$ip = $request->getIp();
		if (null === $ip) {
			return null;
		}

		return CM_Model_Location::findByIp($ip);
	}

	protected function _getSuggestions($term, array $options, CM_Render $render) {
		$ip = CM_Request_Abstract::getInstance()->getIp();
		$locations = new CM_Paging_Location_Suggestions($term, $options['levelMin'], $options['levelMax'], CM_Model_Location::findByIp($ip));
		$locations->setPage(1, 15);
		$out = array();
		foreach ($locations as $location) {
			$out[] = $this->getSuggestion($location, $render);
		}
		return $out;
	}
}

{'fraction_non_alphanumeric': 0.17183402745642448, 'fraction_numerical': 0.001542495758136665, 'mean_word_length': 4.687719298245614, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 67, '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': '4917926', 'n_tokens_mistral': 2263, 'n_tokens_neox': 2054, 'n_words': 392}