freenetis-github/application/models/enum_type.php @ master
8baed187 | Michal Kliment | <?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/
|
|||
*
|
|||
*/
|
|||
/**
|
|||
* Enum type
|
|||
*
|
|||
* @package Model
|
|||
*
|
|||
* @property integer $id
|
|||
* @property integer $type_id
|
|||
* @property Enum_type_Model $enum_type_name
|
|||
* @property string $value
|
|||
* @property boolean $read_only
|
|||
* @property ORM_Iterator $device_templates
|
|||
c1bdc1c4 | Michal Kliment | * @property ORM_Iterator $connection_requests
|
|
8baed187 | Michal Kliment | */
|
|
class Enum_type_Model extends ORM
|
|||
{
|
|||
/* enum types names (same as enum type name IDs) ==> */
|
|||
// types for device, member, user, contact, fee
|
|||
c1bdc1c4 | Michal Kliment | const MEMBER_TYPE_ID = 1;
|
|
const DEVICE_TYPE_ID = 2;
|
|||
8baed187 | Michal Kliment | const USER_TYPE_ID = 3;
|
|
const CONTACT_TYPE_ID = 4;
|
|||
const FEE_TYPE_ID = 6;
|
|||
// types for devices
|
|||
const MODE_TYPE_ID = 8;
|
|||
const NORM_TYPE_ID = 9;
|
|||
const ANTENNA_TYPE_ID = 10;
|
|||
const POLARIZATION_TYPE_ID = 11;
|
|||
c1bdc1c4 | Michal Kliment | const MEDIUM_TYPE_ID = 12;
|
|
8baed187 | Michal Kliment | // types for redirection
|
|
const REDIRECT_DURATION_ID = 13;
|
|||
const REDIRECT_DESTINATION_ID = 14;
|
|||
c1bdc1c4 | Michal Kliment | const REDIRECT_ACTION_ID = 15;
|
|
8baed187 | Michal Kliment | // types for bacup
|
|
const BACUP_ID = 16;
|
|||
/* <== enum types names */
|
|||
const READ_ONLY = 1;
|
|||
c1bdc1c4 | Michal Kliment | const READ_WRITE = 0;
|
|
8baed187 | Michal Kliment | ||
protected $belongs_to = array
|
|||
(
|
|||
'type_id' => 'enum_type_name'
|
|||
);
|
|||
protected $has_many = array
|
|||
(
|
|||
c1bdc1c4 | Michal Kliment | 'device_templates', 'connection_requests'
|
|
8baed187 | Michal Kliment | );
|
|
/**
|
|||
* enum_types array supplements a database table, holding the names for
|
|||
* all enumeration types for dropdown fields.
|
|||
* Such a table is not necessary, because inserting a new type name into
|
|||
* the table would have to be accompanied by modifying the PHP code which
|
|||
* takes care about displaying as a new dropdown field.
|
|||
*
|
|||
* @var array of integers, where index is a string
|
|||
*/
|
|||
private static $enum_types = array
|
|||
(
|
|||
1 => 'Member types',
|
|||
2 => 'Device types',
|
|||
3 => 'User types',
|
|||
4 => 'Contact types',
|
|||
);
|
|||
/**
|
|||
* Localizated compare string
|
|||
*
|
|||
* @param string $a
|
|||
* @param string $b
|
|||
* @return integer
|
|||
*/
|
|||
protected function cmp_utf($a, $b)
|
|||
{
|
|||
return strcoll($a,$b);
|
|||
}
|
|||
/**
|
|||
* Get translation of type
|
|||
*
|
|||
* @param integer $type_id
|
|||
* @return Translation_Model
|
|||
*/
|
|||
public function get_value($type_id = NULL)
|
|||
{
|
|||
if ($type_id === NULL && $this->id)
|
|||
{
|
|||
$type_id = $this->id;
|
|||
}
|
|||
$type = $this->where('id', $type_id)->find();
|
|||
$translation_model = new Translation_Model();
|
|||
return $translation_model->get_translation($type->value);
|
|||
}
|
|||
/**
|
|||
* Gets all enum types ordered
|
|||
*
|
|||
* @param string $order_by
|
|||
* @param string $order_by_direction
|
|||
* @return Mysql_Result
|
|||
*/
|
|||
public function get_all($order_by, $order_by_direction)
|
|||
{
|
|||
c1bdc1c4 | Michal Kliment | // order by check
|
|
if (!$this->has_column($order_by))
|
|||
{
|
|||
$order_by = 'id';
|
|||
}
|
|||
// order by direction check
|
|||
if (strtolower($order_by_direction) != 'desc')
|
|||
{
|
|||
$order_by_direction = 'asc';
|
|||
}
|
|||
// query
|
|||
return $this->db->query("
|
|||
SELECT et.id, etn.type_name as type, et.value, et.read_only AS readonly
|
|||
FROM enum_types et
|
|||
JOIN enum_type_names etn ON et.type_id = etn.id
|
|||
LEFT JOIN translations t ON etn.type_name = t.original_term AND t.lang = ?
|
|||
ORDER BY $order_by $order_by_direction
|
|||
", Config::get('lang'));
|
|||
8baed187 | Michal Kliment | }
|
|
/**
|
|||
* get_values returns all values for enumeration type identified by
|
|||
* $type_id from database. It translates the values using i18n,
|
|||
* and finally sorts them according to current locale.
|
|||
*
|
|||
* Note: the sort function does not work on Windows, because
|
|||
* windows do not support the UTF locales.
|
|||
*
|
|||
* @param integer $type_id
|
|||
* @return array
|
|||
*/
|
|||
public function get_values($type_id, $read_only = NULL)
|
|||
{
|
|||
if ($read_only === NULL)
|
|||
$types = $this->where('type_id', $type_id)->find_all();
|
|||
else
|
|||
{
|
|||
$types = $this->where(array
|
|||
(
|
|||
'type_id' => $type_id,
|
|||
'read_only' => $read_only
|
|||
))->find_all();
|
|||
}
|
|||
$arr_types = array();
|
|||
$translation_model = new Translation_Model();
|
|||
foreach ($types as $type)
|
|||
{
|
|||
$arr_types[$type->id] = $translation_model->get_translation($type->value);
|
|||
}
|
|||
uasort($arr_types, array($this, "cmp_utf"));
|
|||
return $arr_types;
|
|||
}
|
|||
/**
|
|||
* Get name of enum types
|
|||
*
|
|||
* @param integer $name_id
|
|||
* @return string
|
|||
*/
|
|||
public static function get_name($name_id)
|
|||
{
|
|||
if (array_key_exists($name_id, self::$enum_types))
|
|||
{
|
|||
return self::$enum_types[$name_id];
|
|||
}
|
|||
return null;
|
|||
}
|
|||
/**
|
|||
* It gets enum_type id of value string.
|
|||
*
|
|||
* @author Jiri Svitak
|
|||
* @param $value
|
|||
* @return integer
|
|||
*/
|
|||
c2e44ab0 | Michal Kliment | public function get_type_id($value, $type_id = NULL)
|
|
8baed187 | Michal Kliment | {
|
|
c2e44ab0 | Michal Kliment | $where_type = '';
|
|
if (!is_null($type_id))
|
|||
{
|
|||
$where_type = "AND type_id = " . intval($type_id);
|
|||
}
|
|||
8baed187 | Michal Kliment | $result = $this->db->query("
|
|
SELECT id
|
|||
FROM enum_types
|
|||
c2e44ab0 | Michal Kliment | WHERE value LIKE ? $where_type
|
|
8baed187 | Michal Kliment | ", $value);
|
|
if ($result && $result->count())
|
|||
{
|
|||
return $result->current()->id;
|
|||
}
|
|||
return false;
|
|||
}
|
|||
}
|