<?
class
soModelTree
{
protected
$_aActivePath
=
array
();
protected
$_oDB
= NULL;
#-------------------------------------------------------#
function
__construct()
{
global
$oDB
;
$this
->_oDB =
$oDB
;
}
protected
function
_initActivePath(
$id
)
{
$this
->_aActivePath[] =
$id
;
$sSql
=
'SELECT * FROM `tree` WHERE published=1 AND id='
.
$id
;
$oRow
=
$this
->_getRow(
$sSql
);
if
(
$oRow
->id_parent)
{
$this
->_initActivePath(
$oRow
->id_parent);
}
return
$this
;
}
protected
function
_getRow(
$sSql
)
{
$this
->_oDB->setQuery(
$sSql
);
return
$this
->_oDB->loadObject();
}
protected
function
_getRows(
$sSql
)
{
$this
->_oDB->setQuery(
$sSql
);
return
$this
->_oDB->loadObjectsList();
}
protected
function
_getAllRecursively(
$id_parent
,
$iLevel
= 0)
{
$sSql
=
'SELECT * FROM `tree` WHERE published=1 AND id_parent='
.
$id_parent
.
' ORDER BY ordering'
;
$aRows
=
$this
->_getRows(
$sSql
);
$aRet
=
array
();
$iNextLevel
=
$iLevel
+1;
if
(
count
(
$aRows
))
{
foreach
(
$aRows
as
$oRow
)
{
$oRow
->__iLevel =
$iLevel
;
$aSubRows
=
$this
->_getAllRecursively(
$oRow
->id,
$iNextLevel
);
if
(
count
(
$aSubRows
))
{
$oRow
->__aSubLevel =
$aSubRows
;
}
if
(
count
(
$this
->_aActivePath))
{
if
(in_array(
$oRow
->id,
$this
->_aActivePath))
{
$oRow
->__isActive = TRUE;
}
}
$aRet
[] =
$oRow
;
}
}
return
$aRet
;
}
public
function
getItems(
$aOptions
)
{
$id_selected
= isset(
$aOptions
[
'id_selected'
])?
$aOptions
[
'id_selected'
]:0;
if
(
$id_selected
)
{
$this
->_initActivePath(
$id_selected
);
}
$aRet
=
$this
->_getAllRecursively(0, 0);
return
count
(
$aRet
)?
$aRet
:[];
}
}