Page Menu
Home
Code
Search
Configure Global Search
Log In
Files
F1196299
perso.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
8 KB
Subscribers
None
perso.php
View Options
<?php
/*
* Perso class
*
* 0.1 2010-01-27 00:39 Autogenerated by Pluton Scaffolding
*
* @package Zed
* @copyright Copyright (c) 2010, Dereckson
* @license Released under BSD license
* @version 0.1
*
*/
class
Perso
{
public
$id
;
public
$user_id
;
public
$name
;
public
$nickname
;
public
$race
;
public
$sex
;
public
$avatar
;
public
$location_global
;
public
$location_local
;
/*
* Initializes a new instance
* @param int $id the primary key
*/
function
__construct
(
$id
=
null
)
{
if
(
$id
)
{
$this
->
id
=
$id
;
$this
->
load_from_database
();
}
else
{
$this
->
generate_id
();
}
}
/*
* Loads the object Perso (ie fill the properties) from the $_POST array
*/
function
load_from_form
()
{
if
(
array_key_exists
(
'user_id'
,
$_POST
))
$this
->
user_id
=
$_POST
[
'user_id'
];
if
(
array_key_exists
(
'name'
,
$_POST
))
$this
->
name
=
$_POST
[
'name'
];
if
(
array_key_exists
(
'nickname'
,
$_POST
))
$this
->
nickname
=
$_POST
[
'nickname'
];
if
(
array_key_exists
(
'race'
,
$_POST
))
$this
->
race
=
$_POST
[
'race'
];
if
(
array_key_exists
(
'sex'
,
$_POST
))
$this
->
sex
=
$_POST
[
'sex'
];
if
(
array_key_exists
(
'avatar'
,
$_POST
))
$this
->
avatar
=
$_POST
[
'avatar'
];
if
(
array_key_exists
(
'location_global'
,
$_POST
))
$this
->
location_global
=
$_POST
[
'location_global'
];
if
(
array_key_exists
(
'location_local'
,
$_POST
))
$this
->
location_local
=
$_POST
[
'location_local'
];
}
/*
* Loads the object Perso (ie fill the properties) from the database
*/
function
load_from_database
()
{
global
$db
;
$sql
=
"SELECT * FROM "
.
TABLE_PERSOS
.
" WHERE perso_id = '"
.
$this
->
id
.
"'"
;
if
(
!(
$result
=
$db
->
sql_query
(
$sql
))
)
message_die
(
SQL_ERROR
,
"Unable to query persos"
,
''
,
__LINE__
,
__FILE__
,
$sql
);
if
(!
$row
=
$db
->
sql_fetchrow
(
$result
))
{
$this
->
lastError
=
"Perso unkwown: "
.
$this
->
id
;
return
false
;
}
$this
->
user_id
=
$row
[
'user_id'
];
$this
->
name
=
$row
[
'perso_name'
];
$this
->
nickname
=
$row
[
'perso_nickname'
];
$this
->
race
=
$row
[
'perso_race'
];
$this
->
sex
=
$row
[
'perso_sex'
];
$this
->
avatar
=
$row
[
'perso_avatar'
];
$this
->
location_global
=
$row
[
'location_global'
];
$this
->
location_local
=
$row
[
'location_local'
];
return
true
;
}
/*
* Saves to database
*/
function
save_to_database
()
{
global
$db
;
$id
=
$this
->
id
?
"'"
.
$db
->
sql_escape
(
$this
->
id
)
.
"'"
:
'NULL'
;
$user_id
=
$db
->
sql_escape
(
$this
->
user_id
);
$name
=
$db
->
sql_escape
(
$this
->
name
);
$nickname
=
$db
->
sql_escape
(
$this
->
nickname
);
$race
=
$db
->
sql_escape
(
$this
->
race
);
$sex
=
$db
->
sql_escape
(
$this
->
sex
);
$avatar
=
$db
->
sql_escape
(
$this
->
avatar
);
$location_global
=
$db
->
sql_escape
(
$this
->
location_global
);
$location_local
=
$db
->
sql_escape
(
$this
->
location_local
);
//Updates or inserts
$sql
=
"REPLACE INTO "
.
TABLE_PERSOS
.
" (`perso_id`, `user_id`, `perso_name`, `perso_nickname`, `perso_race`, `perso_sex`, `perso_avatar`, `location_global`, `location_local`) VALUES ($id, '$user_id', '$name', '$nickname', '$race', '$sex', '$avatar', '$location_global', '$location_local')"
;
if
(!
$db
->
sql_query
(
$sql
))
{
message_die
(
SQL_ERROR
,
"Unable to save"
,
''
,
__LINE__
,
__FILE__
,
$sql
);
}
if
(!
$id
)
{
//Gets new record id value
$this
->
id
=
$db
->
sql_nextid
();
}
}
/*
* Updates the specified field in the database record
*/
function
save_field
(
$field
)
{
global
$db
;
if
(!
$this
->
id
)
{
message_die
(
GENERAL_ERROR
,
"You're trying to update a record not yet saved in the database"
);
}
$id
=
$db
->
sql_escape
(
$this
->
id
);
$value
=
$db
->
sql_escape
(
$this
->
$field
);
$sql
=
"UPDATE "
.
TABLE_PERSOS
.
" SET `$field` = '$value' WHERE perso_id = '$id'"
;
if
(!
$db
->
sql_query
(
$sql
))
{
message_die
(
SQL_ERROR
,
"Unable to save $field field"
,
''
,
__LINE__
,
__FILE__
,
$sql
);
}
}
public
function
where
()
{
global
$db
;
//B00001001 -> B, 1, 1
list
(
$type
,
$code
,
$subcode
)
=
sscanf
(
$this
->
location_global
,
"%c%05d%03d"
);
switch
(
$type
)
{
case
'B'
:
//Gets body
$sql
=
"SELECT body_name FROM "
.
TABLE_BODIES
.
" WHERE body_code = $code"
;
if
(!
$result
=
$db
->
sql_query
(
$sql
))
{
message_die
(
SQL_ERROR
,
"Unable to query the table"
,
''
,
__LINE__
,
__FILE__
,
$sql
);
}
if
(!
$row
=
$db
->
sql_fetchrow
(
$result
))
{
message_die
(
GENERAL_ERROR
,
"B$code isn't defined as body."
,
"Unknown body"
);
}
$body
=
$row
[
'body_name'
]
?
$row
[
'body_name'
]
:
lang_get
(
'UnknownBody'
);
//Gets place
$sql
=
"SELECT place_name FROM "
.
TABLE_PLACES
.
" WHERE body_code = $code AND place_code = $subcode"
;
if
(!
$result
=
$db
->
sql_query
(
$sql
))
{
message_die
(
SQL_ERROR
,
"Unable to query the table"
,
''
,
__LINE__
,
__FILE__
,
$sql
);
}
if
(!
$row
=
$db
->
sql_fetchrow
(
$result
))
{
message_die
(
GENERAL_ERROR
,
"$this->location_global isn't defined as place."
,
"Unknown place"
);
}
$place
=
$row
[
'place_name'
]
?
$row
[
'place_name'
]
:
lang_get
(
'UnknownPlace'
);
break
;
case
'S'
:
message_die
(
GENERAL_ERROR
,
"Handle spaceship in where()"
,
"TODO"
,
__LINE__
,
__FILE__
);
//break;
default
:
message_die
(
GENERAL_ERROR
,
"Unknown location identifier: $type.<br />Expected: B or S."
);
}
return
sprintf
(
lang_get
(
'WherePlace'
),
$body
,
$place
);
}
public
static
function
is_available_id
(
$id
)
{
global
$db
;
$sql
=
"SELECT COUNT(*) FROM "
.
TABLE_PERSOS
.
" WHERE perso_id = $id LOCK IN SHARE MODE"
;
if
(!
$result
=
$db
->
sql_query
(
$sql
))
{
message_die
(
SQL_ERROR
,
"Can't access users table"
,
''
,
__LINE__
,
__FILE__
,
$sql
);
}
$row
=
$db
->
sql_fetchrow
(
$result
);
return
(
$row
[
0
]
==
0
);
}
/*
* Generates a unique ID for the current object
*/
private
function
generate_id
()
{
do
{
$this
->
id
=
rand
(
2001
,
5999
);
}
while
(!
Perso
::
is_available_id
(
$this
->
id
));
}
/*
* Checks if the nickname is available
* @param string $nickname the nickname to check
*/
public
static
function
is_available_nickname
(
$nickname
)
{
global
$db
;
$nickname
=
$db
->
sql_escape
(
$nickname
);
$sql
=
"SELECT COUNT(*) FROM "
.
TABLE_PERSOS
.
" WHERE perso_nickname LIKE '$nickname' LOCK IN SHARE MODE;"
;
if
(!
$result
=
$db
->
sql_query
(
$sql
))
{
message_die
(
SQL_ERROR
,
"Utilisateurs non parsable"
,
''
,
__LINE__
,
__FILE__
,
$sql
);
}
$row
=
$db
->
sql_fetchrow
(
$result
);
return
(
$row
[
0
]
==
0
);
}
/*
* Counts the perso a user have
*
* @param int user_id the user ID
*/
public
static
function
get_persos_count
(
$user_id
)
{
global
$db
;
$sql
=
"SELECT COUNT(*) FROM "
.
TABLE_PERSOS
.
" WHERE user_id = $user_id"
;
return
$db
->
sql_query_express
(
$sql
);
}
/*
* Gets the first perso a user have
* (typically to be used when get_persos_count returns 1 to autoselect)
*
* @param int user_id the user ID
*/
public
static
function
get_first_perso
(
$user_id
)
{
global
$db
;
$sql
=
"SELECT perso_id FROM "
.
TABLE_PERSOS
.
" WHERE user_id = $user_id LIMIT 1"
;
if
(
$perso_id
=
$db
->
sql_query_express
(
$sql
))
{
return
new
Perso
(
$perso_id
);
}
}
}
?>
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Aug 12 2025, 17:59 (4 w, 3 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
22156
Default Alt Text
perso.php (8 KB)
Attached To
rZED Zed
Event Timeline
Log In to Comment