<commit_before><?php
/**
 * @package        Precincts
 * @category       modules
 * @author         <NAME> <<EMAIL>>
 * @copyright      Copyright (c) 2014, <NAME>
 * @license        MIT License, see license.txt
 */
namespace cs\modules\Precincts;

use
	cs\Cache\Prefix,
	cs\Config,
	cs\Language,
	cs\CRUD,
	cs\Singleton;

/**
 * @method static \cs\modules\Precincts\Precincts instance($check = false)
 */
class Precincts {
	use
		CRUD,
		Singleton;

	/**
	 * @var Prefix
	 */
	protected $cache;
	protected $table      = '[prefix]precincts';
	protected $data_model = [
		'id'         => 'int',
		'number'     => 'int',
		'address_uk' => 'string',
		'address_en' => 'string',
		'address_ru' => 'string',
		'lat'        => 'float',
		'lng'        => 'float',
		'district'   => 'int',
		'violations' => 'int'
	];

	protected function construct () {
		$this->cache = new Prefix('precincts');
	}
	protected function cdb () {
		return Config::instance()->module('Precincts')->db('precincts');
	}
	/**
	 * Add new precinct
	 *
	 * @param $number
	 * @param $address
	 * @param $lat
	 * @param $lng
	 * @param $district
	 *
	 * @return bool|int
	 */
	function add ($number, $address, $lat, $lng, $district) {
		$id = $this->create_simple([
			$number,
			$address,
			$address,
			$address,
			$lat,
			$lng,
			$district,
			0
		]);
		if ($id) {
			unset($this->cache->all);
			return $id;
		}
		return false;
	}
	/**
	 * Get precinct
	 *
	 * @param int|int[] $id
	 *
	 * @return array|array[]|bool
	 */
	function get ($id) {
		if (is_array($id)) {
			foreach ($id as &$i) {
				$i = $this->get($i);
			}
			return $id;
		}
		$clang = Language::instance()->clang;
		return $this->cache->get("$id/$clang", function () use ($id, $clang) {
			$data = $this->read_simple($id);
			if (!$data) {
				return false;
			}
			$data['id']      = (int)$data['id'];
			$data['lat']     = (float)$data['lat'];
			$data['lng']     = (float)$data['lng'];
			$data['address'] = $data["address_$clang"];
			unset(
				$data['address_uk'],
				$data['address_en'],
				$data['address_ru']
			);
			$data['district']   = (int)$data['district'];
			$data['violations'] = (int)$data['violations'];
			return $data;
		});
	}
	/**
	 * Get array of id of all precincts
	 *
	 * @return bool|int[]
	 */
	function get_all () {
		return $this->cache->get('all/ids', function () {
			return $this->db()->qfas(
				"SELECT `id`
				FROM `$this->table`"
			);
		});
	}<commit_msg>Small fix for absent translations of addresses to ru/en languages<commit_after><?php
/**
 * @package        Precincts
 * @category       modules
 * @author         <NAME> <<EMAIL>>
 * @copyright      Copyright (c) 2014, <NAME>
 * @license        MIT License, see license.txt
 */
namespace cs\modules\Precincts;

use
	cs\Cache\Prefix,
	cs\Config,
	cs\Language,
	cs\CRUD,
	cs\Singleton;

/**
 * @method static \cs\modules\Precincts\Precincts instance($check = false)
 */
class Precincts {
	use
		CRUD,
		Singleton;

	/**
	 * @var Prefix
	 */
	protected $cache;
	protected $table      = '[prefix]precincts';
	protected $data_model = [
		'id'         => 'int',
		'number'     => 'int',
		'address_uk' => 'string',
		'address_en' => 'string',
		'address_ru' => 'string',
		'lat'        => 'float',
		'lng'        => 'float',
		'district'   => 'int',
		'violations' => 'int'
	];

	protected function construct () {
		$this->cache = new Prefix('precincts');
	}
	protected function cdb () {
		return Config::instance()->module('Precincts')->db('precincts');
	}
	/**
	 * Add new precinct
	 *
	 * @param $number
	 * @param $address
	 * @param $lat
	 * @param $lng
	 * @param $district
	 *
	 * @return bool|int
	 */
	function add ($number, $address, $lat, $lng, $district) {
		$id = $this->create_simple([
			$number,
			$address,
			$address,
			$address,
			$lat,
			$lng,
			$district,
			0
		]);
		if ($id) {
			unset($this->cache->all);
			return $id;
		}
		return false;
	}
	/**
	 * Get precinct
	 *
	 * @param int|int[] $id
	 *
	 * @return array|array[]|bool
	 */
	function get ($id) {
		if (is_array($id)) {
			foreach ($id as &$i) {
				$i = $this->get($i);
			}
			return $id;
		}
		$clang = Language::instance()->clang;
		return $this->cache->get("$id/$clang", function () use ($id, $clang) {
			$data = $this->read_simple($id);
			if (!$data) {
				return false;
			}
			$data['id']      = (int)$data['id'];
			$data['lat']     = (float)$data['lat'];
			$data['lng']     = (float)$data['lng'];
			$data['address'] = $data["address_$clang"] ?: $data['address_uk'];
			unset(
				$data['address_uk'],
				$data['address_en'],
				$data['address_ru']
			);
			$data['district']   = (int)$data['district'];
			$data['violations'] = (int)$data['violations'];
			return $data;
		});
	}
	/**
	 * Get array of id of all precincts
	 *
	 * @return bool|int[]
	 */
	function get_all () {
		return $this->cache->get('all/ids', function () {
			return $this->db()->qfas(
				"SELECT `id`
				FROM `$this->table`"
			);
		});
	}
{'fraction_non_alphanumeric': 0.2094853683148335, 'fraction_numerical': 0.0020181634712411706, 'mean_word_length': 2.838884585592564, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 61, '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': '1072386', 'n_tokens_mistral': 2116, 'n_tokens_neox': 1837, 'n_words': 380}