<?php
/*
 * This file is part of the easy-CMS project.
 * easy-CMS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * easy-CMS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with easy-CMS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

/**
 * @package Core
 */
 
include_once('config.inc.php');
include_once('classes/core/CoreObject.class.php');
include_once('classes/db/DBConnection.class.php');

/**
 * Abstract class for DB Manipulators (different for each DBMS).
 *
 * @access  private
 * @author	Cedric Francoys <cedric.francoys@easy-cms.org>
 * @version	DBManipulator.class, Sun Nov 14 16:50:15 CET 2004
 * @package	Core
 */
class DBManipulator extends CoreObject {

  var $dbConnection;

  var $last_query;

	/**
	 * Class constructor.
	 * Initialize the DBConnection object for SQL transactions.
	 * 
	 * @access public
 	 * @return void
	 */	    
	function DBManipulator(&$dbConnection) {
    $this->dbConnection = &$dbConnection;
	}

	function &getDBConnection() {
    return $this->dbConnection;
	}

  function quoteSmart($value) {
  }
  
	/**
	 * Open the DBMS connection.
	 *
	 * @return boolean status of the connect function call.
	 */
	function connect() {
    $this->debug('DBManipulator::connect, status : function invoked', 10, DBG_OPEN);
    $result = $this->dbConnection->connect();
    $this->debug('DBManipulator::connect, status : function completed', 10, DBG_CLOSE);
    return $result;
	}
	
	/**
	 * Close the DBMS connection.
	 *
	 * @return boolean status of the close function call.
	 */
	function disconnect() {
    $this->debug('DBManipulator::disconnect, status : function invoked', 10, DBG_OPEN);
    $result = $this->dbConnection->disconnect();
    $this->debug('DBManipulator::disconnect, status : function completed', 10, DBG_CLOSE);
    return $result;
	}


	function getLastError() {
    $this->debug('DBManipulator::getLastError, status : function invoked', 10, DBG_OPEN);
    $result = $this->dbConnection->getLastError();
    $this->debug('DBManipulator::getLastError, status : function completed', 10, DBG_CLOSE);
    return $result;
	}

	/**
	 * Gets Error Number
	 *
	 * Error code generated by last query
	 *
	 * @return integer
	 */
	function getErrNo() {
    $this->debug('DBManipulator::getErrNo, status : function invoked', 10, DBG_OPEN);
    $result = $this->dbConnection->getErrNo();
    $this->debug('DBManipulator::getErrNo, status : function completed', 10, DBG_CLOSE);
    return $result;
	}

	/**
	 * Gets Error Message
	 *
	 * Error message generated by last query
	 *
	 * @return string
	 */
	function getErrMsg() {
    $this->debug('DBManipulator::getErrMsg, status : function invoked', 10, DBG_OPEN);
    $result = $this->dbConnection->getErrMsg();
    $this->debug('DBManipulator::getErrMsg, status : function completed', 10, DBG_CLOSE);
    return $result;
	}

	/**
	 * Gets Number of Rows
	 *
	 * Number of rows returned by last query
	 *
	 * @return integer
	 */
	function getNumRows() {
    $this->debug('DBManipulator::getNumRows, status : function invoked', 10, DBG_OPEN);
    $result = $this->dbConnection->getNumRows();
    $this->debug('DBManipulator::getNumRows, status : function completed', 10, DBG_CLOSE);
    return $result;
	}

	/**
	 * Gets Affected Rows
	 *
	 * Number of affected rows by last query
	 *
	 * @return integer
	 */
	function getAffectedRows(){
    $this->debug('DBManipulator::getAffectedRows, status : function invoked', 10, DBG_OPEN);
    $result = $this->dbConnection->getAffectedRows();
    $this->debug('DBManipulator::getAffectedRows, status : function completed', 10, DBG_CLOSE);
    return $result;
	}

	/**
	 * Gets Last Id
	 *
	 * Last auto incremented Id
	 *
	 * @return integer
	 */
	function getLastId(){
    $this->debug('DBManipulator::getLastId, status : function invoked', 10, DBG_OPEN);
    $result = $this->dbConnection->getLastId();
    $this->debug('DBManipulator::getLastId, status : function completed', 10, DBG_CLOSE);
    return $result;
	}

	/**
	 * Sends Query
	 *
	 * Executes SQL statement
	 *
	 * @param	 string	$sql
	 * @return mixed resource or false
	 */
	function sendQuery($sql) {
		static $numQueries = 0;
    $this->last_query = $sql;
    $this->debug('DBManipulator::sendQuery, status : function invoked ('.(++$numQueries).', '.$sql.')', 10, DBG_OPEN);
    $result = $this->dbConnection->sendQuery($sql);
    $this->debug('DBManipulator::sendQuery, status : function completed', 10, DBG_CLOSE);
    return $result;
	}

	/**
	 * Returns row from query results.
	 *
	 * @param	resource $query reference to query resource.
	 * @return	array
	 * @abstract
	 */
	function fetchRow($resource) {
    $this->debug('DBManipulator::fetchRow, status : function invoked', 10, DBG_OPEN);
    $result = $this->dbConnection->fetchRow($resource);
    $this->debug('DBManipulator::fetchRow, status : function completed', 10, DBG_CLOSE);
    return $result;
	}

	/**
	 * Returns row from query results.
	 *
	 * @param	resource $query reference to query resource.
	 * @return	array
	 * @abstract
	 */
	function fetchArray($resource) {
    $this->debug('DBManipulator::fetchArray, status : function invoked', 10, DBG_OPEN);
    $result = $this->dbConnection->fetchArray($resource);
    $this->debug('DBManipulator::fetchArray, status : function completed', 10, DBG_CLOSE);
    return $result;
	}

	/**
	 *
	 * @param	string	$table
	 * @return 	array	
	 * @abstract
	 */
	function &getColumns($table) {
	}
	
	/**
	 * @return	integer
	 */
  function getCMSType($dbFieldType) {
	}

	/**
	 * @return	string
	 */
	function getDBMSType($cmsFieldType) {
	}

	/**
	 * @param	string $table name of table in database.
	 * @return 	array reference to associative array of fields and types.
	 */
	function &getFieldsArray($table) {
	}
	
	/**
	 * Get record from table.
	 *
	 * @param	string $tableName name of table.
	 * @param	string $id record id.
	 * @param	string $cond SQL WHERE clause.
	 * @param	string $idField record id field.
	 * @param	string $order order field.
	 * @return	resource reference to query resource
	 */
	function getRecord($table_name, $id = -1, $cond = '', $id_field = 'id', $order='') {
	}

	/**
	 * Update record in table.
	 *
	 * @param	string $tableName name of table.
	 * @param	string $id record id.
	 * @param	array $fieldsArray associative array of fields and values.
	 * @param	string $cond SQL WHERE clause.
	 * @param	string $idField record id field.
	 * @return	resource reference to query resource.
	 */
	function setRecord($tableName, $id, $fieldsArray, $cond = "", $idField = "id") {
	}
	
	/**
	 * Add record to table.
	 *
	 * @param	string $tableName name of table to add record into.
	 * @param	array $fieldsArray associative array of fields and values.
	 * @return	resource reference to query resource.
	 */
	function addRecord($tableName, $fieldsArray) {
	}

	/**
	 * Delete record from table.
	 *
	 * @param	string $tableName name of table.
	 * @param	string $id record id.
	 * @param	string $cond SQL WHERE clause.
	 * @param	string $idField record id field.
	 * @return	resource reference to query resource.
	 */
	function delRecord($tableName, $id, $cond = "", $idField = "id") {
	}

	/**
	 * Returns `Field` value from SQL SHOW FULL COLUMNS statement.
	 *
	 * @param	array $fieldArray associative array of fields and values.
	 * @return	string value of `Field` in SQL SHOW statement.
	 */
	function getDBFieldName($fieldArray) {
	}

	/**
	 * Returns `Type` value from SQL SHOW FULL COLUMNS statement.
	 *
	 * @param	array $fieldArray associative array of fields and values.
	 * @return 	string value of `Type` in SQL SHOW statement.
	 */
	function getDBFieldType($fieldArray) {
	} 

	/**
	 * Returns `Key` value from SQL SHOW FULL COLUMNS statement.
	 *
	 * @param	array $fieldArray associative array of fields and values.
	 * @return	string valueof `Key` in SQL SHOW statement.
	 */
	function getDBFieldKey($fieldArray) {
	}

	/**
	 * Returns resource to "SHOW TABLES LIKE '$prefix'" query.
	 *
	 * @param	string $prefix prefix in all tables name in database.
	 * @return	resource reference to query resource.
	 */
	function getTables($prefix) {
	}

	/**
	 * Returns resource to "SHOW TABLES LIKE '$tableName'" query.
	 * 
	 * @param	string $tableName name of table.
	 * @return	resource reference to query resource.
	 */
  function getTable($tableName)	{
	}
	
	/**
	 * Returns number of "content_type" tables in database.
	 *
	 * @return	integer number
	 */
  function countTypeTables() {
	}	
	
	/**
	 * Check if table exists in database.
	 *
	 * @param	string $tableName name of table in database.
	 * @return	bool true if table exists or false.
	 */
	function tableExists($tableName) {
	}

	/**
	 * Check if record exists in table.
	 *
	 * @param	string $tableName name of table.
	 * @param	string $id record id.
	 * @param	string $cond SQL WHERE clause.
	 * @param	string $idField record id field.
	 * @return	bool true if record exists or false.
	 */
	function recordExists($tableName, $id, $cond = "", $idField = "id") {
	}
	
	/**
	 * Check if content_type_* table exists in database.
	 *
	 * @param	string $typeName content_type_* table name.
	 * @return	bool true if content_type_* table exists or false.
	 */
	function typeExists($typeName) {
	}

	/**
	 * Check if page exists in PAGES_TABLE.
	 *
	 * @param	integer $pageId page id.
	 * @return	bool true if page exists or false.
	 */
	function pageExists($pageId) {
	}

	/**
	 * Check if container exists in CONTAINERS_TABLE.
	 *
	 * @param	integer $containerId container id.
	 * @return	bool true if container exists or false.
	 */
	function containerExists($containerId) {
	}
	
	/**
	 * Check if menu exists in MENUS_TABLE.
	 *
	 * @param	integer	$menuId menu id.
	 * @return	bool true if menu exists or false.
	 */
	function menuExists($menuId) {
	}

	/**
	 * Check if containers list in CONTAINERS_LISTS_TABLE.
	 *
	 * @param	integer	$containersListId containers list id.
	 * @return	bool true if containers list exists or false.
	 */
	function containersListExists($containersListId) {
	}

	/**
	 * Check if template exists in TEMPLATES_TABLE.
	 *
	 * @param	string $typeName type name.
	 * @return	bool true if template exists or false.
	 */
	function templateExists($typeNname, $id) {
	}
	
	function &getTemplate($name, $id){
	}
	
	function &getTemplateId($templateName){
	}

	/**
	 * Get static container id from FRAME_CONTAINERS_TABLE.
	 *
	 * @param	integer $frameId frame id.
	 * @param	string $containerName container name.
	 * @return	integer
	 */
	function getStaticContainerId($frameId, $containerName) {
	}	 

	/**
	 * Get static containers list id from FRAME_CONTAINERS_LISTS_TABLE.
	 *
	 * @param	integer $frameId frame id.
	 * @param	string $containersListName containers list name.
	 * @return	integer
	 */
	function getStaticContainersListId($frameId, $containersListName) {
	}	 

	/**
	 * Add record to FRAME_CONTAINERS_TABLE.
	 *
	 * @param	integer $frameId frame id.
	 * @param	integer $pageId page id.
	 * @param	integer $containerId container id.
	 * @param	string $containerName container name.
	 * @return	bool true if record added successfully or false.
	 */
	function addFrameContainer($frameId, $pageId, $containerId, $containerName)
	{	
	}	 

	/**
	 * Add record to FRAME_CONTAINERS_LISTS_TABLE.
	 *
	 * @param	integer $frameId frame id.
	 * @param	integer $pageId page id.
	 * @param 	integer $containersListId containers list id.
	 * @param	string $containersListName containers list name.
	 * @return	bool true if record added successfully or false.
	 */
	function addFrameContainersList($frameId, $pageId, $containersListId, $containersListName) {
	}	
	

	/**
	 * Create new table
	 *
	 * @param  string $table
	 * @param  array  $columnsArray
	 * @return boolean
	 * @final
	 */
	function createTable($table, $columnsArray) {
	}

	/**
	 * Execute DROP statement
	 *
	 * @param  string $table
	 * @return boolean
	 * @final
	 */
	function dropTable($table) {
  }

	/**
	 * Execute ALTER statement on given table
	 *
	 * @param  string $table
	 * @param  array  $newColumnsArray
	 * @param  array  $oldColumnsArray
	 * @return boolean
	 * @final
	 */
	function updateTable($table, $newColumnsArray, $oldColumnsArray) {
	}

  /**
	 * Execute ALTER statement on given table
	 *
	 * @param  string $table
	 * @param  array  $newColumnName
	 * @param  array  $oldColumnName
	 * @return boolean
	 * @final
	 */
  function updateColumnName($table, $newColumnName, $oldColumnName) {
  }

  function updateColumnType($table, $columnName, $newColumnType) {
  }

	/**
	 * Gets Page Menus
	 *
	 * @param  integer $pageId Page Id
	 * @param  boolean $deleted
	 *
	 */
	function getPageMenus($frameId){
		$sql  = 'select b.* ';
		$sql .= 'from `'.FRAME_MENUS_TABLE.'` a ';
		$sql .= 'join `'.MENUS_TABLE.'` b on a.menu_id=b.id ';
		$sql .= 'where a.frame_id='.$this->quoteSmart($frameId);
		return $this->sendQuery($sql);
	}

	/**
	 * Gets Page ContainersLists
	 *
	 * @param  integer $pageId
	 * @param  integer $frameId
	 * @param  boolean $deleted
	 * @return resource
	 */
	function getPageContainersLists($pageId, $frameId, $deleted=false){
		$sql  = 'select b.* ';
		$sql .= 'from `'.FRAME_CONTAINERS_LISTS_TABLE.'` a ';
		$sql .= 'join `'.CONTAINERS_LISTS_TABLE.'` b on a.containers_list_id=b.id ';
		$sql .= 'where a.page_id='.$this->quoteSmart($pageId);
		$sql .= ' and a.frame_id='.$this->quoteSmart($frameId);
		if(!$deleted){
			$sql .= ' and b.deleted=0';
		}
		return $this->sendQuery($sql);
	}

	/**
	 * Gets Page Containers
	 *
	 * @param  integer $pageId
	 * @param  integer $frameId
	 * @param  boolean $deleted
	 * @return resource
	 */
	function getPageContainers($pageId, $frameId, $deleted=false){
		$sql  = 'select b.* ';
		$sql .= 'from `'.FRAME_CONTAINERS_TABLE.'` a ';
		$sql .= 'join `'.CONTAINERS_TABLE.'` b on a.container_id=b.id ';
		$sql .= 'where a.page_id='.$this->quoteSmart($pageId);
		$sql .= ' and a.frame_id='.$this->quoteSmart($frameId);
		if(!$deleted){
			$sql .= ' and b.deleted=0';
		}
		return $this->sendQuery($sql);
	}
	
	/**
	 * Gets Contents From Container
	 *
	 * @param  integer $containerId
	 * @param  string  $contentType
	 * @param  boolean $get_deleted
	 * @return mixed resource or false
	 */
	function queryGetContents($containerId, $contentType, $get_deleted=false) {
		$sql  = '';
		$sql .= 'select ct.* ';
		$sql .= 'from `'.CONTAINER_CONTENTS_TABLE.'` ccts ';
		$sql .= 'join `'.TYPE_TABLES_PREFIX.$contentType.'` ct on ccts.content_id=ct.id ';
		$sql .= 'where ccts.container_id='.$containerId;
		$sql .= !$get_deleted? ' and ct.deleted=0;' : ';';
		
		return $this->sendQuery($sql);
	}
	
	/**
	 * @param  integer $pageId
	 * @param  integer $frameId
	 * @param  integer $containerId 0 to get all
	 * @param  boolean $get_deleted get deleted containers
	 * @return mixed resource or false
	 */
	function queryGetContainersFromPage($pageId, $frameId, $get_deleted=false) {
		$sql  = '';
		$sql .= 'select';
		$sql .= ' c.id as container_id, ';
		$sql .= ' c.acl_id as container_acl_id, ';
		$sql .= ' c.type_name as container_type_name ';
		$sql .= 'from `'.FRAME_CONTAINERS_TABLE.'` fcs ';
		$sql .= 'join `'.CONTAINERS_TABLE.'` c on fcs.container_id=c.id ';
		$sql .= 'where fcs.page_id='.$pageId;
		$sql .= ' and fcs.frame_id='.$frameId;
		$sql .= !$get_deleted? ' and c.deleted=0;' : ';';
		
		return $this->sendQuery($sql);
	}
	
	function queryGetContainersFromList($containersListId, $get_deleted=false) {
		$sql  = '';
		$sql .= 'select';
		$sql .= ' c.id as container_id, ';
		$sql .= ' c.acl_id as container_acl_id, ';
		$sql .= ' c.type_name as container_type_name ';
		$sql .= 'from `'.CONTAINERS_LISTS_CONTAINERS_TABLE.'` clcs ';
		$sql .= 'join `'.CONTAINERS_TABLE.'` c on clcs.container_id=c.id ';
		$sql .= 'where clcs.containers_list_id='.$containersListId;
		$sql .= !$get_deleted? ' and c.deleted=0;' : ';';
		
		return $this->sendQuery($sql);
	}
	
	/**
	 * Gets Containers Lists
	 *
	 * Retrieve all containers lists from given page and frame id
	 *
	 * @author vinadelmar <vinadelmar@users.sourceforge.net>
	 * @since  2006-03-30
	 * @param  integer $pageId
	 * @param  integer $frameId
	 * @param  integer $load_deleted
	 * @return mixed resource or false
	 */
	function queryGetContainersLists($pageId, $frameId, $get_deleted=false) {
		$sql  = '';
		$sql .= 'select';
		$sql .= ' cl.id as containers_list_id, ';
		$sql .= ' cl.acl_id as containers_list_acl_id ';
		$sql .= 'from `'.FRAME_CONTAINERS_LISTS_TABLE.'` fcls ';
		$sql .= 'join `'.CONTAINERS_LISTS_TABLE.'` cl ';
		$sql .= 'where fcls.page_id='.$pageId;
		$sql .= ' and fcls.frame_id='.$frameId;
		$sql .= !$get_deleted? ' and cl.deleted=0;' : ';';
		
		return $this->sendQuery($sql);
	}
	
	/**
	 * Get All Menus
	 *
	 * @param  integer $frameId
	 * @return resource false if error
	 */
	function queryGetMenus($frameId) {
		$sql  = '';
		$sql .= 'select';
		$sql .= ' mnu.id as menu_id, ';
		$sql .= ' mnu.acl_id as menu_acl_id ';
		$sql .= 'from `'.FRAME_MENUS_TABLE.'` fmnus ';
		$sql .= 'join `'.MENUS_TABLE.'` mnu on fmnus.menu_id=mnu.id ';
		$sql .= 'where fmnus.frame_id='.$frameId.';';
		
		$this->sendQuery($sql);
	}
	
	function queryGetAllGroups($get_users, $groupsIdsArray) {
		$SQL  = 'SELECT gr.id AS group_id, gr.name AS group_name, gr.description AS group_description ';
		
		if ($get_users)
			$SQL .= ', grusr.id AS user_id ';
			
		$SQL .= 'FROM '.GROUPS_TABLE.' gr ';
		
		if ($get_users)
			$SQL .= 'JOIN '.GROUP_USERS_TABLE.' grusr ON grusr.group_id=gr.id ';
			
		if (!is_null($groupsIdsArray) && !empty($groupsIdsArray)) {
			$SQL .= 'where gr.id in ('.implode(',', $groupsIdsArray).') ';
		}
			
		$SQL .= 'ORDER BY group_name;';
		
		return $this->sendQuery($SQL);
	}
	
	function queryGetAllUsers($get_groups, $usersIdsArray) {
		$sql  = '';
		$sql .= 'select usr.id as user_id, usr.login as user_login, usr.password ';
		$sql .= 'as user_password, usr.lang_id as user_lang_id ';
		
		if ($get_groups)
			$sql .= ', grusrs.group_id as user_group_id ';
			
		$sql .= 'from `'.USERS_TABLE.'` usr ';
		
		if ($get_groups)
			$sql .= 'left join `'.GROUP_USERS_TABLE.'` grusrs on usr.id=grusrs.user_id ';
			
		if (!is_null($usersIdsArray) && !empty($usersIdsArray)) {
			$sql .= 'where usr.id in ('.implode(',', $usersIdsArray).') ';
		}
		
		$sql .= 'order by usr.login asc;';
			
		return $this->sendQuery($sql);
	}
	
	function queryGetContainerIdFromContentId($contentId, $typeName) {
		$sql  = 'select';
		$sql .= ' ct.id as id ';
		$sql .= ' from `'.CONTAINER_CONTENTS_TABLE.'` cct, `'.TYPE_TABLES_PREFIX.$typeName.'` c, `'.CONTAINERS_TABLE.'` ct';
		$sql .= ' where cct.content_id=c.id ';
		$sql .= ' and cct.container_id=ct.id';
		$sql .= ' and cct.content_id='.$contentId.';';
		return $this->sendQuery($sql);
	}

	function getContainersListIdFromContainerId($containerId) {
		$sql  = 'select';
		$sql .= ' clt.id as id ';
		$sql .= ' from `'.CONTAINERS_LISTS_CONTAINERS_TABLE.'` clct, `'.CONTAINERS_LISTS_TABLE.'` clt';
		$sql .= ' where clct.containers_list_id=clt.id ';
		$sql .= ' and clct.container_id='.$containerId.';';
		return $this->sendQuery($sql);
	}

	function getPageIdFromContainerId($containerId) {
		$sql  = 'select';
		$sql .= ' pt.id as id ';
		$sql .= ' from `'.FRAME_CONTAINERS_TABLE.'` fct, `'.PAGES_TABLE.'` pt ';
		$sql .= ' where fct.frame_id=pt.frame_id';
		$sql .= ' and fct.page_id=pt.id';
		$sql .= ' and fct.container_id='.$containerId.';';
		return $this->sendQuery($sql);
	}

	function getPageIdFromContainersListId($containersListId) {
		$sql  = 'select';
		$sql .= ' pt.id as id ';
		$sql .= ' from `'.FRAME_CONTAINERS_LISTS_TABLE.'` fclt, `'.PAGES_TABLE.'` pt ';
		$sql .= ' where fclt.frame_id=pt.frame_id';
		$sql .= ' and fclt.page_id=pt.id';
		$sql .= ' and fclt.containers_list_id='.$containersListId.';';
		return $this->sendQuery($sql);
	}

	/**
	 * @param  integer $contentId
	 * @param  string  $typeName
	 * @return mixed resource or false
	 */
	function queryGetContainerOfContent($contentId, $typeName) {
		$sql  = 'select';
		$sql .= ' c.id as id, ';
		$sql .= ' c.acl_id as acl_id ';
		$sql .= 'from `'.CONTAINER_CONTENTS_TABLE.'` ccts ';
		$sql .= 'join `'.TYPE_TABLES_PREFIX.$typeName.'` ct on ccts.content_id=ct.id ';
		$sql .= 'join `'.CONTAINERS_TABLE.'` c on ccts.container_id=c.id ';
		$sql .= 'where ccts.content_id='.$contentId.';';
		
		return $this->sendQuery($sql);
	}

	/**
	 * @param  integer $containerId
	 * @return mixed resource or false
	 */
	function queryGetContainersListOfContainer($containerId) {
		$sql  = 'select';
		$sql .= ' cl.id as id, ';
		$sql .= ' cl.acl_id as acl_id ';
		$sql .= ' from `'.CONTAINERS_LISTS_CONTAINERS_TABLE.'` clcs ';
		$sql .= ' join `'.CONTAINERS_LISTS_TABLE.'` cl on clcs.containers_list_id=cl.id ';
		$sql .= ' where clcs.container_id='.$containerId.';';
		return $this->sendQuery($sql);
	}

	/**
	 * @param integer $pageId
	 * @param integer $frameId
	 * @param integer $containerId
	 */
	function queryGetPageOfContainer($pageId, $frameId, $containerId) {
		$sql  = 'select';
		$sql .= ' p.id as id, ';
		$sql .= ' p.acl_id as acl_id ';
		$sql .= ' from `'.FRAME_CONTAINERS_TABLE.'` fcs ';
		$sql .= ' join `'.PAGES_TABLE.'` p on fcs.page_id=p.id ';
		$sql .= ' where fcs.page_id='.$pageId;
		$sql .= ' and fcs.frame_id='.$frameId;
		$sql .= ' and fcs.container_id='.$containerId.';';
		return $this->sendQuery($sql);
	}

	function queryGetPageOfContainersList($pageId, $frameId, $containersListId) {
		$sql  = 'select';
		$sql .= ' p.id as id, ';
		$sql .= ' p.acl_id as acl_id ';
		$sql .= 'from `'.FRAME_CONTAINERS_LISTS_TABLE.'` fcls ';
		$sql .= 'join `'.PAGES_TABLE.'` p on fcls.page_id=p.id ';
		$sql .= 'where fcls.page_id='.$pageId;
		$sql .= ' and fcls.frame_id='.$frameId;
		$sql .= ' and fcls.containers_list_id='.$containersListId;
		$sql .= ';';//' and fcls.containers_list_name=\''.$containersListName.'\';';
		
		return $this->sendQuery($sql);
	}
	
	function queryGetPageOfMenu($pageId, $frameId, $menuId) {
		$sql  = 'select';
		$sql .= ' p.id as id,';
		$sql .= ' p.acl_id as acl_id ';
		$sql .= 'from `'.FRAME_MENUS_TABLE.'` fmnus ';
		$sql .= 'join `'.PAGES_TABLE.'` p on fmnus.frame_id=p.frame_id ';
		$sql .= 'where p.id='.$pageId; 
		$sql .= ' and fmnus.frame_id='.$frameId;
		$sql .= ' and fmnus.menu_id='.$menuId;
		$sql .= ';';//' and fmnus.menu_name=\''.$menuName.'\';';
		
		return $this->sendQuery($sql);
	}
	
	function queryGetPageOfPage($pageId) {
		$sql  = 'select ';
		$sql .= ' p2.id as id, ';
		$sql .= ' p2.acl_id as acl_id ';
		$sql .= 'from `'.PAGES_TABLE.'` p1 ';
		$sql .= 'join `'.PAGES_TABLE.'` p2 on p1.parent_id=p2.id ';
		$sql .= 'where p1.id='.$pageId.';';
		
		return $this->sendQuery($sql);
	}
	
	function queryGetContentVersions($contentId, $contentType) {
		$contentId = $this->quoteSmart($contentId);
		$contentType = $this->quoteSmart($contentType);
		
		$sql  = 'select ';
		$sql .= ' v.id as version_id, ';
		$sql .= ' v.date as version_date, ';
		$sql .= ' usr.login as user_login ';
		$sql .= 'from `'.VERSIONS_TABLE.'` v ';
		$sql .= 'join `'.USERS_TABLE.'` usr on v.user_id=usr.id ';
		$sql .= 'where v.type_name='.$contentType.' ';
		$sql .= 'and v.content_id='.$contentId.' ';
		$sql .= 'order by v.date desc;';
		
		return $this->sendQuery($sql);
	}
	
	/**
	 * Gets Frame Styles
	 *
	 * @param  integer $id Frame Id
	 * @return resource
	 */
	function getFrameStyles($id) {
		$sql  = '';
		$sql .= 'select a.frame_id, b.id, b.file ';
		$sql .= 'from `'.FRAME_STYLES_TABLE.'` a ';
		$sql .= 'join `'.STYLES_TABLE.'` b on a.style_id=b.id ';
		$sql .= 'where a.frame_id in ('.implode(',', (array)$id).')';
		return $this->sendQuery($sql);
	}
	
	/**
	 * Gets Frame Scripts
	 *
	 * @param  integer $id Frame Id
	 * @return resource
	 */
	function getFrameScripts($id) {
		$sql  = '';
		$sql .= 'select a.frame_id, b.id, b.file ';
		$sql .= 'from `'.FRAME_SCRIPTS_TABLE.'` a ';
		$sql .= 'join `'.SCRIPTS_TABLE.'` b on a.script_id=b.id ';
		$sql .= 'where a.frame_id in ('.implode(',', (array)$id).')';
		return $this->sendQuery($sql);
	}
	
	function getUserGroups($id) {
		$sql  = '';
		$sql .= 'select a.id, a.login, a.password, a.lang_id, b.group_id ';
		$sql .= 'from `'.USERS_TABLE.'` as a ';
		$sql .= 'join `'.GROUP_USERS_TABLE.'` as b on (a.id=b.user_id) ';
		$sql .= 'where a.id in ('.implode(',',(array)$id).')';
		return $this->sendQuery($sql);
	}
}
?>