[
class tree: tangra_lib
] [
index: tangra_lib
] [
all elements
]
tangra_lib
Packages:
tangra_lib
Source for file grid.class.php
Documentation is available at
grid.class.php
<?php
// *** Tangra (Application Framework and Tools for PHP)
// $Id$
//
/**
* Contains class Grid
*
*
@package
tangra_lib
*
@subpackage
grids
*/
/**
*
*/
require_once
(
TANGRA_MAIN_DIR
.
'web_site/web_context.class.php'
)
;
/**
*
*/
require_once
(
TANGRA_MAIN_DIR
.
'exceptions/te_not_an_array.class.php'
)
;
/**
*
*/
require_once
(
TANGRA_MAIN_DIR
.
'exceptions/te_grid_column_count_not_matched.class.php'
)
;
/**
*
*/
require_once
(
TANGRA_MAIN_DIR
.
'exceptions/te_grid_no_such_row.class.php'
)
;
/**
* Grids are used to present data in table format
* Grid is base class for all grids.
*
*
@package
tangra_lib
*
@subpackage
grids
*/
abstract
class
Grid
extends
Tangra_Class
{
/**
* Data rows
*
*
@var
array
*
@internal
*/
protected
$rows
=
array
(
)
;
/**
* Name of the grid
*
*
@var
string
*
@internal
*/
private
$name
;
/**
* How many coluns will grid have
*
*
@var
integer
*/
private
$columns_number
=
0
;
/**
* Constructor
*
*
@param
string
$name
Name of the grid
*
@param
integer
$columns_number
How many coluns will grid have
*/
function
__construct
(
$name
,
$columns_number
)
{
$this
->
set_name
(
$name
)
;
$this
->
set_columns_number
(
$columns_number
)
;
}
/**
* Sets grid name
*
*
@param
string
$name
*
@throws
Tangra_Exception
*/
public
function
set_name
(
$name
)
{
if
(
ereg
(
"^[a-z]{1}[a-z0-9_]{1,50}$"
,
$name
))
{
$this
->
name
=
strtolower
(
$name
)
;
}
else
{
throw
new
Tangra_Exception
(
'Invalid grid name. Must be alphanumeric (underscore allowed), starting with letter, 50 characters max.'
)
;
}
}
/**
* Returns grid name
*
*
@return
string
*/
public
function
get_name
(
)
{
return
$this
->
name
;
}
/**
* Sets how many columns grid will have
*
*
@param
integer
$number
*/
public
function
set_columns_number
(
$number
)
{
tangra_if_not_int_throw_e
(
$number
)
;
$this
->
columns_number
=
$number
;
}
/**
* Returns how many columns grid has
*
*
@return
integer
*/
public
function
get_columns_number
(
)
{
return
$this
->
columns_number
;
}
/**
* Adds new row
*
*
@param
array
$row
*
@throws
TE_Grid_Column_Count_Not_Matched, TE_Not_An_Array
*/
public
function
add_row
(
$row
)
{
if
(
is_array
(
$row
))
{
if
(
count
(
$row
)
==
$this
->
get_columns_number
(
))
{
array_push
(
$this
->
rows
,
$row
)
;
}
else
{
throw
new
TE_Grid_Column_Count_Not_Matched
(
'Columns in data: '
.
count
(
$row
)
.
', Columns in grid: '
.
$this
->
get_columns_number
(
))
;
}
}
else
{
throw
new
TE_Not_An_Array
(
$row
)
;
}
}
/**
* Removes a row
*
*
@param
integer
$row_num
Row index
*/
public
function
remove_row
(
$row_num
)
{
tangra_if_not_int_throw_e
(
$row_num
)
;
if
(
array_key_exists
(
$row_num
,
$this
->
rows
))
{
unset
(
$this
->
rows
[
$row_num
]
)
;
$this
->
rows
=
array_values
(
$this
->
rows
)
;
}
else
{
$this
->
ask_for_forgivenes
(
new
TE_Grid_No_Such_Row
(
$row_num
))
;
}
}
/**
* Sets all rows at once
*
*
@param
array
$rows
*
@throws
TE_Not_An_Array
*/
public
function
set_rows
(
&
$rows
)
{
if
(
is_array
(
$rows
))
{
foreach
(
$rows
as
$key
=>
$row
)
{
if
(
is_array
(
$row
) )
{
$this
->
add_row
(
$row
)
;
}
else
{
throw
new
TE_Not_An_Array
(
"
Row:
$key
is not an array. Value =
"
.
$row
)
;
}
}
}
else
{
throw
new
TE_Not_An_Array
(
$row
)
;
}
}
/**
* Removes all rows
*
*/
public
function
remove_all_rows
(
)
{
$this
->
rows
=
array
(
)
;
}
/**
* Returns reference to rows
*
*
@return
unknown
*/
public
function
&
get_rows
(
)
{
return
$this
->
rows
;
}
}