Projekt

Obecné

Profil

<?php defined('SYSPATH') or die('No direct script access.');
/*
* This file is part of open source system FreeNetIS
* and it is release under GPLv3 licence.
*
* More info about licence can be found:
* http://www.gnu.org/licenses/gpl-3.0.html
*
* More info about project can be found:
* http://www.freenetis.org/
*
*/

/**
* @package Model
*/
class Town_Model extends ORM
{
protected $has_many = array('address_points');

/**
* Check if town exists
* Exists - return it
* Not exists - create it a return it
*
* @author Michal Kliment
* @param $zip_code zip code
* @param $town town
* @param $quarter quarter
* @return Town_Model
*/
public function get_town($zip_code = NULL, $town = NULL, $quarter = NULL)
{
$towns = $this->where(array
(
'zip_code' => $zip_code,
'town' => $town,
'quarter' => $quarter
))->find_all();

if (count($towns) == 0)
{
$this->clear();
$this->zip_code = $zip_code;
$this->town = $town;
$this->quarter = $quarter;
$this->save();
return $this;
}
else if (count($towns) == 1)
{
return $towns->current();
}
else
{
return NULL;
}
}

/**
*
* @param integer $limit_from
* @param integer $limit_results
* @param string $order_by
* @param string $order_by_direction
* @param array $filter_values
* @return
*/
public function get_all_towns($limit_from = 0, $limit_results = 50,
$order_by = 'id', $order_by_direction = 'asc', $filter_values = array())
{
// order by check
if (!$this->has_column($order_by))
{
$order_by = 'id';
}
// order by direction check
$order_by_direction = strtolower($order_by_direction);
if ($order_by_direction != 'desc')
{
$order_by_direction = 'asc';
}
// query
return $this->db->query("
SELECT t.id, t.town, t.quarter, t.zip_code
FROM towns t
ORDER BY $order_by $order_by_direction
LIMIT ".intval($limit_from).", ".intval($limit_results)."
");
}

/**
* Gets count of all towns
*
* @return integer
*/
public function count_all_towns()
{
return $this->db->query("
SELECT COUNT(id) AS count
FROM towns t
")->current()->count;
}
/**
* Gets loist of towns for select box
*
* @return array[string]
*/
public function select_list_with_quater()
{
$towns = $this->db->query("
SELECT
id, quarter, town, zip_code
FROM towns
ORDER BY town, quarter, zip_code
");
$arr_towns = array();
foreach ($towns as $town)
{
$index = '' . $town->id . '';
$arr_towns[$index] = $town->town;
if (!empty($town->quarter))
{
$arr_towns[$index] .= ', ' . $town->quarter;
}
$arr_towns[$index] .= ', ' . $town->zip_code;
}
return $arr_towns;
}
}
(69-69/82)