<?php
class PagesAdmin extends Pages{
	function __construct( ){
		$this->generateCache( );
	}
	function throwPagesAdminList( $aWhere = Array( ), $bGetFullPath = false ){
		if( isset( $this->aPagesParentsTypes ) ){
			$aReturn = null;
			foreach( $this->aPagesParentsTypes as $iType => $aPages ){
				$sType = $GLOBALS['aMenuTypes'][$iType];
				foreach( $aPages as $iPage ){
					$aPage = $this->aPages[$iPage];
					if( $bGetFullPath ) $aPage['sFullName'] = $sType . ' &raquo; ' . $aPage['sName'];
					$aPage['iDepth'] = 0;
					$aPage['iToSelection'] = 1;
					foreach( $aWhere as $sField => $mValue ){
						if( $aPage[$sField] != $mValue ){
							$aPage['iToSelection'] = 0;
						}
					}
					$aReturn[] = $aPage;
					if( isset( $this->aPagesChildren[$aPage['iPage']] ) ){
/*						if( $aPage['iSubpagesShow'] == 3 ){
							$this->throwSubpagesNewsAdmin( $aReturn, $iPage, 1, $aWhere, $bGetFullPath, $aPage['sFullName'] . ' &raquo; ' );
						} else{*/
							$this->throwSubpagesAdmin( $aReturn, $iPage, 1, $aWhere, $bGetFullPath, $aPage['sFullName'] . ' &raquo; ' );
/*						}*/
					}
				}
			}
			return $aReturn;
		}
	}
	function throwSubpagesAdmin(&$aReturn, $iPageParent, $iDepth, $aWhere = Array( ), $bGetFullPath = false, $sPageParent = '' ){
		foreach( $this->aPagesChildren[$iPageParent] as $iPageChild ){
			$aPage = $this->aPages[$iPageChild];
			if( $bGetFullPath ) $aPage['sFullName'] = $sPageParent . $aPage['sName'];
			$aPage['iDepth'] = $iDepth;
			$aPage['iToSelection'] = 1;
			foreach( $aWhere as $sField => $mValue ){
				if( $aPage[$sField] != $mValue ){
					$aPage['iToSelection'] = 0;
				}
			}
			$aReturn[] = $aPage;
			if( isset( $this->aPagesChildren[$aPage['iPage']] ) ){
				if( $aPage['iSubpagesShow'] == 3 ){
					$this->throwSubpagesNewsAdmin( $aReturn, $iPageChild, $iDepth + 1, $aWhere, $bGetFullPath, $aPage['sFullName'] . ' &raquo; ' );
				} else{
					$this->throwSubpagesAdmin( $aReturn, $iPageChild, $iDepth + 1, $aWhere, $bGetFullPath, $aPage['sFullName'] . ' &raquo; ' );
				}
			}
		}
	}
	function throwSubpagesNewsAdmin(&$aReturn, $iPageParent, $iDepth, $aWhere = Array( ), $bGetFullPath = false, $sPageParent = '' ){
		$aSort = Array( );
		foreach( $this->aPagesChildren[$iPageParent] as $iPageChild ){
			$aSort[] = Array( 0 => $this->aPages[$iPageChild]['iTime'], 1 => $iPageChild );
		}
		rsort( $aSort );
		foreach( $aSort as $aPageChild ){
			$aPage = $this->aPages[$aPageChild[1]];
			if( $bGetFullPath ) $aPage['sFullName'] = $sPageParent . $aPage['sName'];
			$aPage['iDepth'] = $iDepth;
			$aPage['iToSelection'] = 1;
			foreach( $aWhere as $sField => $mValue ){
				if( $aPage[$sField] != $mValue ){
					$aPage['iToSelection'] = 0;
				}
			}
			$aReturn[] = $aPage;
			if( isset( $this->aPagesChildren[$aPage['iPage']] ) ){
				if( $aPage['iSubpagesShow'] == 3 ){
					$this->throwSubpagesNewsAdmin( $aReturn, $aPage['iPage'], $iDepth + 1, $aWhere, $bGetFullPath, $aPage['sFullName'] . ' &raquo; ' );
				} else{
					$this->throwSubpagesAdmin( $aReturn, $aPage['iPage'], $iDepth + 1, $aWhere, $bGetFullPath, $aPage['sFullName'] . ' &raquo; ' );
				}
			}
		}
	}
	function throwPagesAdmin( $iPageParent = null, $iPageType = null ){
		if( isset( $iPageParent ) ){
			foreach( $this->aPagesChildren[$iPageParent] as $iPage ){
				$aPage = Array( 'iPage' => $this->aPages[$iPage]['iPage'], 'sName' => $this->aPages[$iPage]['sName'] );
				if( isset( $this->aPagesChildren[$iPage] ) ){
					$aPage['aChildren'] = $this->throwPagesAdmin( $iPage );
				}
				if( isset( $aPage ) ) $aResult[] = $aPage;
			}
			return $aResult;
		} else{
			$aResult = Array( );
			if( is_array( $this->aPagesParentsTypes ) ) foreach( $this->aPagesParentsTypes as $iType => $aPages ){
				if( !isset( $iPageType ) || $iType == $iPageType ){
					$sType = $GLOBALS['aMenuTypes'][$iType];
					$aResult[] = Array( 'iPage' => 0, 'sName' => $sType );
					foreach( $aPages as $iPage ){
						$aPage = null;
						$aPage = Array( 'iPage' => $this->aPages[$iPage]['iPage'], 'sName' => $this->aPages[$iPage]['sName'] );
						if( isset( $this->aPagesChildren[$iPage] ) ){
							$aPage['aChildren'] = $this->throwPagesAdmin( $iPage );
						}
						if( isset( $aPage ) ) $aResult[] = $aPage;
					}
				}
			}
			return $aResult;
		}
	}
	function throwProductsPagesAdmin( $bShowDisabled = true, $iPageParent = null ){
		if( isset( $iPageParent ) ){
			foreach( $this->aPagesChildren[$iPageParent] as $iPage ){
				$aPage = null;
				if( $this->aPages[$iPage]['iProducts'] == 1 ){
					$aPage = Array( 'iPage' => $this->aPages[$iPage]['iPage'], 'sName' => $this->aPages[$iPage]['sName'] );
				} elseif( $bShowDisabled ){
					$aPage = Array( 'iPage' => 'd', 'sName' => $this->aPages[$iPage]['sName'] );
				}
				if( isset( $this->aPagesChildren[$iPage] ) ){
					$aPage['aChildren'] = $this->throwProductsPagesAdmin( $bShowDisabled, $iPage );
				}
				if( isset( $aPage ) ) $aResult[] = $aPage;
			}
			return $aResult;
		} elseif( isset( $this->aPagesParentsTypes ) ){
			$aResult = Array( );
			if( is_array( $this->aPagesParentsTypes ) ) foreach( $this->aPagesParentsTypes as $iType => $aPages ){
				$sType = $GLOBALS['aMenuTypes'][$iType];
				$aResult[] = Array( 'iPage' => 0, 'sName' => $sType );
				foreach( $aPages as $iPage ){
					$aPage = null;
					if( $this->aPages[$iPage]['iProducts'] == 1 ){
						$aPage = Array( 'iPage' => $this->aPages[$iPage]['iPage'], 'sName' => $this->aPages[$iPage]['sName'] );
					} elseif( $bShowDisabled ){
						$aPage = Array( 'iPage' => 'd', 'sName' => $this->aPages[$iPage]['sName'] );
					}
					if( isset( $this->aPagesChildren[$iPage] ) ){
						$aPage['aChildren'] = $this->throwProductsPagesAdmin( $bShowDisabled, $iPage );
					}
					if( isset( $aPage ) ) $aResult[] = $aPage;
				}
			}
			return $aResult;
		}
	}
	function throwProductsPagesAdminFull( $iPageParent = null, $sPageParent = null, $bProductsOnly = true ){
		$aResult = Array( );
		if( isset( $iPageParent ) ){
			foreach( $this->aPagesChildren[$iPageParent] as $iPage ){
				$aPage = null;
				if( !$bProductsOnly || $this->aPages[$iPage]['iProducts'] == 1 ){
					$aPage = Array( 'iPage' => $this->aPages[$iPage]['iPage'], 'sName' => $sPageParent . ' &raquo; ' . $this->aPages[$iPage]['sName'] );
				}
				if( isset( $this->aPagesChildren[$iPage] ) ){
					$aResult = array_merge( $aResult, $this->throwProductsPagesAdminFull( $iPage, $aPage['sName'], $bProductsOnly ) );
				}
				if( isset( $aPage ) ) $aResult['p' . $aPage['iPage']] = $aPage['sName'];
			}
		} elseif( isset( $this->aPagesParentsTypes ) ){
			if( is_array( $this->aPagesParentsTypes ) ) foreach( $this->aPagesParentsTypes as $iType => $aPages ){
				$sType = $GLOBALS['aMenuTypes'][$iType];
				foreach( $aPages as $iPage ){
					$aPage = null;
					if( !$bProductsOnly || $this->aPages[$iPage]['iProducts'] == 1 ){
						$aPage = Array( 'iPage' => $this->aPages[$iPage]['iPage'], 'sName' => $sType . ' &raquo; ' . $this->aPages[$iPage]['sName'] );
					}
					if( isset( $this->aPagesChildren[$iPage] ) ){
						$aResult = array_merge( $aResult, $this->throwProductsPagesAdminFull( $iPage, $aPage['sName'], $bProductsOnly ) );
					}
					if( isset( $aPage ) ) $aResult['p' . $aPage['iPage']] = $aPage['sName'];
				}
			}
		}
		if( !isset( $iPageParent ) ){
			$aResultNum = Array( );
			foreach( $aResult as $sNum => $sVal ){
				$aResultNum[intval( str_replace( 'p', '', $sNum ) ) ] = $sVal;
			}
			return $aResultNum;
		}
		return $aResult;
	}
	function deletePage( $iPage ){
		$oFile = FilesAdmin::getInstance( );
		$oDB = DataBase::getInstance( );
		$this->mData[$iPage] = true;
		if( isset( $this->aPagesChildren[$iPage] ) ){
			$this->throwSubpagesIdAdmin( $iPage );
		}
		$oDB->deleteInFile( DB_PAGES, $this->mData, 'iPage' );
		$oDB->deleteInFile( DB_PAGES_EXT, $this->mData, 'iPage' );
		$oFile->deleteFiles( $this->mData, 1, 'iPage' );
		$oDB->deleteInFile( DB_USERS_PAGES, $this->mData, 'iPage' );
		$oDB->deleteInFile( DB_PAGES_COMMENTS, $this->mData, 'iLink' );
	}
	function throwSubpagesIdAdmin( $iPage ){
		foreach( $this->aPagesChildren[$iPage] as $iPageChild ){
			$this->mData[$iPageChild] = true;
			if( isset( $this->aPagesChildren[$iPageChild] ) ){
				$this->throwSubpagesIdAdmin( $iPageChild );
			}
		}
	}
	function savePage( $aForm ){
		$oDB = DataBase::getInstance( );
		$oFile = FilesAdmin::getInstance( );
		if( !isset( $aForm['iPage'] ) || !is_numeric( $aForm['iPage'] ) || !isset( $this->aPages[$aForm['iPage']] ) ){
			$sParam = null;
		} else{
			$sParam = 'iPage';
			if( isset( $_SESSION['iAdminId'] ) && $_SESSION['iAdminId'] > 0 && isset( $sParam ) && isset( $this->aPages[$aForm['iPage']]['iAdmin'] ) && $this->aPages[$aForm['iPage']]['iAdmin'] != $_SESSION['iAdminId'] ){
				$aForm['iAdmin'] = $this->aPages[$aForm['iPage']]['iAdmin'];
			}
		}
		if( !empty( $aForm['iPageParent'] ) && ( empty( $aForm['iPageParent'] ) || $aForm['iPageParent'] != $aForm['iPage'] ) ) if( $aForm['iPageParent'] > 0 && isset( $this->aPages[$aForm['iPageParent']] ) ){
			$aForm['iType'] = $this->aPages[$aForm['iPageParent']]['iType'];
		} else{
			$aForm['iPageParent'] = 0;
		}
		if( !isset( $aForm['iAuthorized'] ) ) $aForm['iAuthorized'] = 0;
		$aForm['iLastModified'] = time( );
		if( !isset( $aForm['iPosition'] ) || !is_numeric( $aForm['iPosition'] ) || $aForm['iPosition'] < - 99 || $aForm['iPosition'] > 999 ) $aForm['iPosition'] = 0;
		if( !isset( $aForm['iProducts'] ) ) $aForm['iProducts'] = '';
		if( !isset( $aForm['iRss'] ) ) $aForm['iRss'] = 0;
		if( !isset( $aForm['iStatus'] ) ) $aForm['iStatus'] = 0;
		$aForm['iTime'] = (int)abs( ( !empty( $aForm['sDate'] ) ) ? dateToTime( $aForm['sDate'] ) : time( ) );
		$aForm = changeMassTxt( $aForm, '', Array( 'sDescriptionShort', 'Nds' ), Array( 'sDescriptionFull', 'Nds' ), Array( 'sMetaDescription', 'Nds' ) );
		$aForm['iPage'] = $oDB->save( DB_PAGES, $aForm, $sParam, 'sort' );
		$this->updateSubpagesType( $aForm['iPage'], $aForm['iType'] );
		$oDB->save( DB_PAGES_EXT, $aForm, $sParam );
		$oDB->delete( DB_PAGES_TAGS, Array( 'iPage' => $aForm['iPage'] ) );
		if( isset( $aForm['Tags'] ) && $aForm['Tags'] != "" ){
			foreach( explode( ',', $aForm['Tags'] ) as $sTag ){
				$sTag = trim( $sTag );
				if( !empty( $sTag ) ){
					$oDB->executeSQL( 'INSERT INTO `' . $oDB->throwTableName( DB_PAGES_TAGS ) . '` ( `iPage`, `sTag` ) VALUES ( ' . intval( $aForm['iPage'] ) . ', \'' . $oDB->escape( $sTag ) . '\' )' );
				}
			}
		}
		
		$oDB->delete( DB_PAGES_ZBOZI, Array( 'iPage' => $aForm['iPage'] ) );
		if( isset( $aForm['zboziczCategories'] ) && !empty( $aForm['zboziczCategories'] ) ){
			foreach( $aForm['zboziczCategories'] as $sCategory ){
				$sCategory = trim( $sCategory );
				if( !empty( $sCategory ) ){
					$oDB->executeSQL( 'INSERT INTO `' . $oDB->throwTableName( DB_PAGES_ZBOZI ) . '` ( `iPage`, `sCategory` ) VALUES ( ' . intval( $aForm['iPage'] ) . ', \'' . $oDB->escape( $sCategory ) . '\' )' );
				}
			}
		}
		
		if( empty( $aForm['aBlocks'] ) ){
			$oDB->executeSQL( 'DELETE FROM `' . $oDB->throwTableName( DB_PAGES_BLOCKS ) . '` WHERE `iPage` = ' . intval( $aForm['iPage'] ) );
		} else{
			$oDB->executeSQL( 'DELETE FROM `' . $oDB->throwTableName( DB_PAGES_BLOCKS ) . '` WHERE `iPage` = ' . intval( $aForm['iPage'] ) . ' AND `iElement` NOT IN ( ' . implode( ', ', array_keys( $aForm['aBlocks'] ) ) . ' )' );
		}
		if( is_array( $aForm['aBlocks'] ) ) foreach( $aForm['aBlocks'] as $aBlock['iElement'] => $aBlock ){
			$aBlock['iPage'] = $aForm['iPage'];
			$iSubpagesShow = 0;
			if( is_array( $aBlock['iSubpagesShow'] ) ) foreach( $aBlock['iSubpagesShow'] as $bit => $val ) $iSubpagesShow += $bit * $val;
			$aBlock['iSubpagesShow'] = $iSubpagesShow;
			$oDB->save( DB_PAGES_BLOCKS, $aBlock, 'iElement' );
		}
		if( is_array( $aForm['aNewBlocks'] ) ) foreach( $aForm['aNewBlocks'] as $aBlock ){
			$aBlock['iPage'] = $aForm['iPage'];
			$iSubpagesShow = 0;
			if( is_array( $aBlock['iSubpagesShow'] ) ) foreach( $aBlock['iSubpagesShow'] as $bit => $val ) $iSubpagesShow += $bit * $val;
			$aBlock['iSubpagesShow'] = $iSubpagesShow;
			$oDB->save( DB_PAGES_BLOCKS, $aBlock );
		}
		$oDB->deleteInFile( DB_USERS_PAGES, $aForm['iPage'], 'iPage' );
		if( is_array( $aForm['aGroups'] ) ) foreach( $aForm['aGroups'] as $iGroup ){
			$oDB->save( DB_USERS_PAGES, Array( 'iPage' => $aForm['iPage'], 'iGroup' => $iGroup ) );
		}
		if( isset( $aForm['aFilesDelete'] ) ) $oFile->deleteSelectedFiles( $aForm['aFilesDelete'], 1 );
		if( isset( $aForm['aFilesDescription'] ) ) $oFile->saveFiles( $aForm, $aForm['iPage'], 1 );
		if( isset( $_FILES['aNewFiles'] ) ) $oFile->addFilesUploadedMulti( $aForm['iPage'], 1, 'iPage' );
		if( isset( $aForm['aDirFiles'] ) ) $oFile->addFilesFromServer( $aForm, $aForm['iPage'], 1, 'iPage' );
		if( isset( $sParam ) && $aForm['iStatus'] == 0 && $aForm['iStatus'] != $this->aPages[$aForm['iPage']]['iStatus'] && isset( $this->aPagesChildren[$aForm['iPage']] ) ){
			$this->mData = null;
			$this->throwSubpagesIdAdmin( $aForm['iPage'] );
			foreach( $this->mData as $iPage => $bValue ){
				$aChange[$iPage][1] = 0;
			}
			if( isset( $aChange ) ){
				$this->savePagesData( $aChange );
			}
		}
		$this->generateCache( );

		if( $aForm['bUpdateMargins'] == 1 ){
			$oDB->executeSQL( 'UPDATE `' . $oDB->throwTableName( DB_PRODUCTS ) .'` `p` SET `fPrice` = ROUND( `fBuyPrice` ' . ( strpos( $aForm['sMargin'], '%' ) !== false ? '* ( 1 + '.str_replace( '%', '', $aForm['sMargin'] ).'/100 )' : '+ '.$aForm['sMargin'] ) . ', 2 ) WHERE EXISTS( SELECT 1 FROM `' . $oDB->throwTableName( DB_PRODUCTS_PAGES ) . '` `pp` WHERE `iPage` = ' . $aForm['iPage'] . ' AND `iMainCategory` = 1 AND `p`.`iProduct` = `pp`.`iProduct` ) AND ( `sMargin` IS NULL OR `sMargin` = \'\' ) AND `fBuyPrice` > 0' );
			$_SESSION['iUpdatedProducts'] = $oDB->affectedRows( );
			$oDB->executeSQL( 'UPDATE `' . $oDB->throwTableName( DB_PRODUCTS_VARIANTS ) .'` `pv` INNER JOIN `' . $oDB->throwTableName( DB_PRODUCTS ) .'` `p` USING( `iProduct` ) SET `pv`.`fPrice` = ROUND( `pv`.`fBuyPrice` ' . ( strpos( $aForm['sMargin'], '%' ) !== false ? '* ( 1 + '.str_replace( '%', '', $aForm['sMargin'] ).'/100 )' : '+ '.$aForm['sMargin'] ) . ', 2 ) WHERE EXISTS( SELECT 1 FROM `' . $oDB->throwTableName( DB_PRODUCTS_PAGES ) . '` `pp` WHERE `iPage` = ' . $aForm['iPage'] . ' AND `iMainCategory` = 1 AND `p`.`iProduct` = `pp`.`iProduct` ) AND ( `pv`.`sMargin` IS NULL OR `pv`.`sMargin` = \'\' ) AND ( `p`.`sMargin` IS NULL OR `p`.`sMargin` = \'\' ) AND `pv`.`fBuyPrice` > 0' );
			$_SESSION['iUpdatedVariants'] = $oDB->affectedRows( );
		}

		return $aForm['iPage'];
	}
	private function updateSubpagesType( $iPage, $iType ){
		$oDB = DataBase::getInstance( );
		if( is_array( $this->aPagesChildren[$iPage] ) ) foreach( $this->aPagesChildren[$iPage] as $iSubPage ){
			$oDB->update( DB_PAGES, Array( 'iType' => $iType ), Array( 'iPage' => $iSubPage ) );
			$this->updateSubpagesType( $iSubPage, $iType );
		}
	}
	function savePages( $aForm ){
		if( isset( $aForm['aPositions'] ) && is_array( $aForm['aPositions'] ) ){
			foreach( $this->aPages as $iPage => $aData ){
				if( isset( $aForm['aPositions'][$iPage] ) ){
					$aForm['aPositions'][$iPage] = trim( $aForm['aPositions'][$iPage] );
					if( is_numeric( $aForm['aPositions'][$iPage] ) && $aForm['aPositions'][$iPage] != $aData['iPosition'] ){
						$aChange[$iPage][0] = $aForm['aPositions'][$iPage];
					}
					$iStatus = isset( $aForm['aStatus'][$iPage] ) ? 1 : 0;
					if( !isset( $aChange[$iPage][1] ) && $iStatus != $this->aPages[$iPage]['iStatus'] ){
						$aChange[$iPage][1] = $iStatus;
						if( $iStatus == 0 && isset( $this->aPagesChildren[$iPage] ) ){
							$this->mData = null;
							$this->throwSubpagesIdAdmin( $iPage );
							foreach( $this->mData as $iPage => $bValue ){
								$aChange[$iPage][1] = 0;
							}
						}
					}
				}
			}
			if( isset( $aChange ) ){
				$this->savePagesData( $aChange );
				$this->generateCache( );
			}
		}
	}
	function savePagesData( $aChange ){
		$oDB = DataBase::getInstance( );
		$iCount = count( $aChange );
		$i = 1;
		foreach( $aChange as $iPage => $aData ){
			$aSave = $this->aPages[$iPage];
			if( isset( $aData[0] ) ) $aSave['iPosition'] = $aData[0];
			if( isset( $aData[1] ) ) $aSave['iStatus'] = $aData[1];
			if( $i == $iCount ) $oDB->save( DB_PAGES, $aSave, 'iPage', 'sort' );
			else $oDB->save( DB_PAGES, $aSave, 'iPage' );
			$i++;
		}
	}
	function throwPagesAuthorized( $iType = null, $iPageParent = null ){
		$aResult = Array( );
		if( !isset( $iType ) ){
			if( isset( $this->aPagesParentsTypes ) ){
				if( is_array( $this->aPagesParentsTypes ) ) foreach( $this->aPagesParentsTypes as $iType => $aPages ){
					$sType = $GLOBALS['aMenuTypes'][$iType];
					$aResult[] = Array( 'iPage' => 0, 'sName' => $sType, 'aChildren' => $this->throwPagesAuthorized( $iType ) );
				}
			}
		} else{
			if( !isset( $iPageParent ) ){
				$aPages = $this->aPagesParentsTypes[$iType];
			} else{
				$aPages = $this->aPagesChildren[$iPageParent];
			}
			if( is_array( $aPages ) ) foreach( $aPages as $iPage ){
				$aResult[] = Array( 'iPage' => ( isset( $this->aPages[$iPage]['iAuthorized'] ) && $this->aPages[$iPage]['iAuthorized'] == 1 ) ? $this->aPages[$iPage]['iPage'] : 0, 'sName' => $this->aPages[$iPage]['sName'], 'aChildren' => $this->throwPagesAuthorized( $iType, $iPage ) );
			}
		}
		return $aResult;
	}
	function clonePage( $iPage, $iStatus = 0 ){
		$oDB = & DataBase::getInstance( );
		$sql = 'SELECT * FROM `' . $oDB->throwTableName( DB_PAGES ) . '` NATURAL JOIN `' . $oDB->throwTableName( DB_PAGES_EXT ) . '` WHERE `iPage` = ' . intval( $iPage );
		$aData = $oDB->executeSQL( $sql, true, DataBase::SINGLE_RECORD );
		$aData['iPage'] = null;
		if( isset( $iStatus ) ){
			$aData['iStatus'] = $iStatus;
		}
		$aData['iPage'] = $oDB->save( DB_PAGES, $aData );
		$oDB->save( DB_PAGES_EXT, $aData );
		//Products files and photos (without copying files, only copies links, for new file manager)
		$sql = 'INSERT INTO `' . $oDB->throwTableName( DB_PAGES_FILES ) . '` ( `iPage`, `sFileName`, `sDescription`, `iPhoto`, `iPosition`, `iType`, `iSize` ) SELECT ' . intval( $aData['iPage'] ) . ', `sFileName`, `sDescription`, `iPhoto`, `iPosition`, `iType`, `iSize` FROM `' . $oDB->throwTableName( DB_PAGES_FILES ) . '` WHERE `iPage` = ' . intval( $iPage );
		$oDB->executeSQL( $sql );
		//Pages blocks
		$sql = 'INSERT INTO `' . $oDB->throwTableName( DB_PAGES_BLOCKS ) . '` ( `iPage`, `sType`, `iPosition`, `sTitle`, `sText`, `iColumnWidth`, `iForm`, `iMinSize`, `iPositionMin`, `iPositionMax`, `iAdditionalPage`, `iCount`, `iSubpagesShow`, `iPoll`, `sProductsShow`, `sHlFunction`, `iBox`, `iWidthType` ) SELECT ' . intval( $aData['iPage'] ) . ', `sType`, `iPosition`, `sTitle`, `sText`, `iColumnWidth`, `iForm`, `iMinSize`, `iPositionMin`, `iPositionMax`, `iAdditionalPage`, `iCount`, `iSubpagesShow`, `iPoll`, `sProductsShow`, `sHlFunction`, `iBox`, `iWidthType` FROM `' . $oDB->throwTableName( DB_PAGES_BLOCKS ) . '` WHERE `iPage` = ' . intval( $iPage );
		$oDB->executeSQL( $sql );
		//Users groups
		$sql = 'INSERT INTO `' . $oDB->throwTableName( DB_USERS_PAGES ) . '` ( `iPage`, `iGroup` ) SELECT ' . intval( $aData['iPage'] ) . ', `iGroup` FROM `' . $oDB->throwTableName( DB_USERS_PAGES ) . '` WHERE `iPage` = ' . intval( $iPage );
		$oDB->executeSQL( $sql );
		return $aData['iPage'];
	}	
};
?>