<?php
class PHPSIMPLEDB_extends_collection{
var $_oObjects = NULL;
var $_oParentObject = NULL;
var $_bLimit = false;
var $_iLimit_num_page = 1;
var $_iLimit_length = 10;
var $_sOrderBy = '';
var $_sOrderByAsc = true;
function __construct(){
}
public function setLimit( $iLimit_page, $iLength ){
$this->_bLimit = true;
$this->_iLimit_num_page = $iLimit_page;
$this->_iLimit_length = $iLength;
}
public function setOrderBy( $sOrder, $bAsc = true ){
$this->_sOrderBy = $sOrder;
$this->_sOrderByAsc = $bAsc;
}
public function count( $oData ){
if( is_array( $oData ) && count( $oData ) > 0 )
$sQuery = 'SELECT count( * ) as nbr FROM '.$this->_oInfos[ 'table'].' WHERE '.implode( ' AND ', $oData );
else if( is_string( $oData ) )
$sQuery = 'SELECT count( * ) as nbr FROM '.$this->_oInfos[ 'table'].' WHERE '.$oData;
else
$sQuery = 'SELECT count( * ) as nbr FROM '.$this->_oInfos[ 'table'];
$oQuery = $this->_oPHPSIMPLEDB->_oPDO->prepare( $sQuery );
$oQuery->execute();
$oResult = $oQuery->fetch( PDO::FETCH_ASSOC );
return $oResult[ 'nbr' ];
}
public function load( $oData = NULL ){
if( !is_array( $oData ) && !is_string( $oData ) && !is_null( $oData ) )
return false;
$sQuery = '';
$this->_oObjects = array();
if( is_array( $oData ) && count( $oData ) > 0 )
$sQuery = 'SELECT * FROM '.$this->_oInfos[ 'table'].' WHERE '.implode( ' AND ', $oData );
else if( is_string( $oData ) )
$sQuery = 'SELECT * FROM '.$this->_oInfos[ 'table'].' WHERE '.$oData;
else
$sQuery = 'SELECT * FROM '.$this->_oInfos[ 'table'];
if( $this->_sOrderBy != '' )
$sQuery .= ' order by '.addslashes( $this->_sOrderBy ).( $this->_sOrderByAsc ? ' ASC' : ' DESC' );
if( $this->_bLimit )
$sQuery .= ' LIMIT '.addslashes( ( ( $this->_iLimit_num_page - 1 ) * $this->_iLimit_length ) ).', '.addslashes( $this->_iLimit_length );
$oQuery = $this->_oPHPSIMPLEDB->_oPDO->prepare( $sQuery );
$oQuery->execute();
$oResult = $oQuery->fetchAll( PDO::FETCH_ASSOC );
if( !$oResult )
return false;
foreach( $oResult as $oValue ){
$oObject = $this->_oPHPSIMPLEDB->getObject( $this->_oInfos[ 'table'] );
$oObject->load( $oValue );
if( !is_null( $this->_oParentObject ) )
$oObject->_oParentObject = $this->_oParentObject;
$this->_oObjects[] = $oObject;
}
return true;
}
public function save(){
foreach( $this->_oObjects as $oObject )
$oObject->save();
}
public function delete( $bDeleteCollections = false ){
foreach( $this->_oObjects as $oObject )
$oObject->delete( $bDeleteCollections );
}
public function addObject( $oObject ){
if( $oObject->_oInfos[ 'table'] != $this->_oInfos[ 'table'] )
return false;
if( !is_null( $this->_oParentObject ) ){
//recherche du champ de link
foreach( $oObject->_oFields as $sKey=>$oValues ){
if( $oValues[ 'typefield' ] == 'object' && $this->_oParentObject->_oInfos[ 'table'] == $oValues[ 'linkobject' ] ){
$oObject->setData_object( $sKey, $this->_oParentObject );
break;
}
}
}
if( is_null( $this->_oObjects ) )
$this->_oObjects = array();
$this->_oObjects[] = $oObject;
return true;
}
public function delObject( $oObject, $bDeleteCollections = false ){
$oObjects = $this->_oObjects;
$this->_oObjects = array();
foreach( $oObjects as $oItemObject ){
if( $oItemObject === $oObject )
$oItemObject->delete( $bDeleteCollections );
else
$this->_oObjects[] = $oItemObject;
}
}
public function getObjects(){
return $this->_oObjects;
}
/*
* trie une collection sur un champ
* $sName = nom du champ
* $sBy = 'asc' || 'desc'
* $sType = SORT_NUMERIC || SORT_STRING
*/
function sortByField( $sName, $sBy = 'asc', $sType = SORT_NUMERIC ){
$oNew_oObjects = array();
$oAssoc_oObjects = array();
foreach( $this->_oObjects as $oObject ){
$sKey_assoc = count( $oAssoc_oObjects );
$oAssoc_oObjects[ $sKey_assoc ] = $oObject;
$oNew_oObjects[ $sKey_assoc ] = $oObject->getData_field( $sName );
}
if( $sBy == 'asc' )
asort ( $oNew_oObjects );
else
arsort ( $oNew_oObjects );
$this->_oObjects = array();
foreach( $oNew_oObjects as $sKey=>$sValue )
$this->_oObjects[] = $oAssoc_oObjects[ $sKey ];
}
/*
* monte ou descend une valeur d'un champ entier d'un objet
* en respectant l'ordre de liste
* $sName = nom du champ
* $iId = identifiant de l'objet
* $sBy = 'asc' || 'desc'
*/
function moveOrderField( $sName, $iId, $sBy = 'asc' ){
$oObjectItem = NULL;
$oAllPosition = array();
if( is_null( $this->_oObjects ) )
return false;
foreach( $this->_oObjects as $oObject ){
if( $oObject->getData_field( $this->_oInfos[ 'primarykey' ] ) == $iId )
$oObjectItem = $oObject;
$iPosition = $oObject->getData_field( $sName );
$oAllPosition[] = $iPosition;
}
if( is_null( $oObjectItem ) )
return false;
if( count( $oAllPosition ) == 1 )
return true;
$iPosFieldItem = $iPosNewFieldItem = $oObjectItem->getData_field( $sName );
if( !is_numeric( $iPosFieldItem ) )
return false;
sort( $oAllPosition, SORT_NUMERIC );
$iIndexPosition = 0;
for( $i = 0; $i < count( $oAllPosition ); $i++ )
if( $oAllPosition[ $i ] == $iPosFieldItem ){
$iIndexPosition = $i;
break;
}
if( $sBy == 'asc' ){
if( $iIndexPosition == 0 && $oAllPosition[ 0 ] != $oAllPosition[ 1 ] )
return true;
else if( count( $oAllPosition ) > ( $iIndexPosition + 1 ) && $oAllPosition[ $iIndexPosition ] == $oAllPosition[ $iIndexPosition + 1 ] )
$iPosNewFieldItem--;
else
$iPosNewFieldItem = $oAllPosition[ $iIndexPosition - 1 ];
}else{
if( ( $iIndexPosition + 1 ) == count( $oAllPosition ) && $oAllPosition[ count( $oAllPosition ) - 1 ] != $oAllPosition[ count( $oAllPosition ) - 2 ] )
return true;
else if( count( $oAllPosition ) > ( $iIndexPosition + 1 ) && $oAllPosition[ $iIndexPosition ] == $oAllPosition[ $iIndexPosition + 1 ] )
$iPosNewFieldItem++;
else
$iPosNewFieldItem = $oAllPosition[ $iIndexPosition + 1 ];
}
foreach( $this->_oObjects as $oObject )
if( $oObject->getData_field( $this->_oInfos[ 'primarykey' ] ) == $iId )
$oObject->setData_field( $sName, $iPosNewFieldItem );
else if( $oObject->getData_field( $sName ) == $iPosNewFieldItem )
$oObject->setData_field( $sName, $iPosFieldItem );
return true;
}
public function implodeField( $sSeparator, $sField ){
$sReturn = '';
foreach( $this->_oObjects as $oObject )
$sReturn .= $oObject->getData_field( $sField ).$sSeparator;
return substr( $sReturn, 0, strlen( $sReturn ) - strlen( $sSeparator ) );
}
}
?>