<?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/db/DBConnectionMySQL.class.php');
include_once('classes/core/DBManipulator.class.php');

/**
* Translate functions and types from CMS to DBMS.
*
* @access   private
* @author	Cedric Francoys <cedric.francoys@easy-cms.org>
* @version	DBManipulatorMySQL.class, Sun Nov 14 16:48:15 CET 2004
* @package	Core
*/
class DBManipulatorMySQL extends DBManipulator {

	/**
	 * Class constructor
	 * Initialize the DBConnection object for SQL transactions.
	 *
	 * @access public
	 * @param	string	$dbmsType	typeof DBMS.
 	 * @param    string	$host		The hostname of the DB server.
	 * @param    string  $db			The name of the DB.
	 * @param    string  $user		The username to use for the connection to the DB.
	 * @param    string  $pass		The password to use for the connection to the DB.
 	 * @return   void  
	 */	    
	function DBManipulatorMySQL(&$dbData) {
		$this->debug('DBManipulatorMySQL::DBManipulatorMySQL, status : function invoked', 10, DBG_OPEN);
		$dbConnection = new DBConnectionMySQL($dbData);
		parent::DBManipulator($dbConnection);
		$this->debug('DBManipulatorMySQL::DBManipulatorMySQL, status : function completed', 10, DBG_CLOSE);
	}
	
	function getColumns($table) {
		$this->debug('DBManipulatorMySQL::getColumns, status : function invoked', 10, DBG_OPEN);
		$result = $this->sendQuery("show full columns from `{$table}`;");
		$this->debug('DBManipulatorMySQL::getColumns, status : function completed', 10, DBG_CLOSE);
		return $result;
	}

  function getDBMSType($cmsFieldType) {
    $this->debug('DBManipulatorMySQL::getDBMSType, status : function invoked ('.$cmsFieldType.')', 10, DBG_OPEN);
		$dbmsType = '';
		
    switch ($cmsFieldType) {
  		case EASY_CMS_TYPE_BOOL:
  			$dbmsType = 'TINYINT(1)';
  			break;
  		case EASY_CMS_TYPE_INTEGER:
  			$dbmsType = 'INT(11)';
  			break;
  		case EASY_CMS_TYPE_FLOAT:
  			$dbmsType = 'FLOAT';
  			break;
  		case EASY_CMS_TYPE_DATE:
  			$dbmsType = 'DATE';
  			break;
  		case EASY_CMS_TYPE_TIME:
  			$dbmsType = 'TIME';
  			break;
  		case EASY_CMS_TYPE_COLOR:
  			$dbmsType = 'CHAR(6)';
  			break;
  		case EASY_CMS_TYPE_REFERENCE:
  			$dbmsType = 'CHAR(255)';
  			break;
  		case EASY_CMS_TYPE_STRING:
  			$dbmsType = 'VARCHAR(255)';
  			break;
  		case EASY_CMS_TYPE_SHORTTEXT:
  			$dbmsType = 'TEXT';
  			break;
  		case EASY_CMS_TYPE_TEXT:
  			$dbmsType = 'LONGTEXT';
  			break;
  		case EASY_CMS_TYPE_LINK:
  			$dbmsType = 'BLOB';
  			break;
  		case EASY_CMS_TYPE_DOCUMENT:
  			$dbmsType = 'TINYTEXT';
  			break;
  		case EASY_CMS_TYPE_IMAGE:
  			$dbmsType = 'TINYBLOB';
  			break;
  		default :
  			$dbmsType = -1;
    }
    $this->debug('DBManipulatorMySQL::getDBMSType, status : function completed ('.$dbmsType.')', 10, DBG_CLOSE);
    return $dbmsType;
	}
	
  function getCMSType($dbFieldType) {
    $this->debug('DBManipulatorMySQL::getCMSType, status : function invoked ('.$dbFieldType.')', 10, DBG_OPEN);
		$cmsFieldType = '';
		
    switch (strtoupper($dbFieldType)) {
      case "TINYINT(1)" :
          $cmsFieldType = EASY_CMS_TYPE_BOOL;
          break;
      case "INT(11)" :
          $cmsFieldType = EASY_CMS_TYPE_INTEGER;
          break;
      case "FLOAT" :
          $cmsFieldType = EASY_CMS_TYPE_FLOAT;
          break;
      case "DATE" :
          $cmsFieldType = EASY_CMS_TYPE_DATE;
          break;
      case "TIME" :
          $cmsFieldType = EASY_CMS_TYPE_TIME;
          break;
      case "CHAR(6)" :
          $cmsFieldType = EASY_CMS_TYPE_COLOR;
          break;
      case "CHAR(255)" :
          $cmsFieldType = EASY_CMS_TYPE_REFERENCE;
          break;
      case "VARCHAR(255)" :
          $cmsFieldType = EASY_CMS_TYPE_STRING;
          break;
      case "TEXT" :
          $cmsFieldType = EASY_CMS_TYPE_SHORTTEXT;
          break;
      case "LONGTEXT" :
          $cmsFieldType = EASY_CMS_TYPE_TEXT;
          break;
      case "BLOB" :
          $cmsFieldType = EASY_CMS_TYPE_LINK;
          break;
      case "TINYTEXT" :
          $cmsFieldType = EASY_CMS_TYPE_DOCUMENT;
          break;
      case "TINYBLOB" :
          $cmsFieldType = EASY_CMS_TYPE_IMAGE;
          break;
      default :
          break;
    }
    $this->debug('DBManipulatorMySQL::getCMSType, status : function completed ('.$cmsFieldType.')', 10, DBG_CLOSE);
    return $cmsFieldType;
  }
	
	function getFieldsArray($table) {
    $this->debug('DBManipulatorMySQL::getFieldsArray, status : function invoked', 10, DBG_OPEN);
		$privateFieldsArray = Type::getPrivateFieldsArray();
		$fieldsArray = array();
    $result = $this->getColumns($table);
    while ($row = $this->fetchArray($result)) {
			$dbFieldName = $this->getDBFieldName($row);
			if (!array_key_exists($dbFieldName, $privateFieldsArray)) {
				$dbFieldType = $this->getDBFieldType($row);
				$fieldsArray[$dbFieldName] = $this->getCMSType($dbFieldType);
      }
    }
    $this->debug('DBManipulatorMySQL::getFieldsArray, status : function completed', 10, DBG_CLOSE);
		return $fieldsArray;
	}
	
	/**
	 * 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='') {
    $this->debug('DBManipulatorMySQL::getRecord, status : function invoked', 10, DBG_OPEN);
		$sql = 'select * from `'.$table_name.'`';
		if((!empty($id) && $id > 0) && !empty($cond)) {
			$sql .= ' where `'.$id_field.'`='.$this->quoteSmart($id).' and '.$cond;
		}
    elseif(!empty($id) && $id > 0) {
			$sql .= ' where `'.$id_field.'`='.$this->quoteSmart($id);
		}
    elseif(!empty($cond)) {
			$sql .= ' where '.$cond;
		}

		if(!empty($order)) {
			$sql .= " order by '$order'";
		}
    $result = $this->sendQuery($sql);
    $this->debug('DBManipulatorMySQL::getRecord, status : function completed', 10, DBG_CLOSE);
    return $result;
	}

  function setRecord($tableName, $id, $fieldsArray, $cond='', $idField='id'){
    $this->debug('DBManipulatorMySQL::setRecord, status : function invoked', 10, DBG_OPEN);
		$sql = 'update `'.$tableName.'` set ';
		$fields = '';
		$result = false;

		foreach ($fieldsArray as $key => $value) {
			$fields .= sprintf('`%s`=%s,', $key, $this->quoteSmart($value));
		}
    // remove trailing ','
		$sql .= substr($fields, 0, -1);
		
		if((!empty($id) && ($id > 0 || is_array($id))) && !empty($cond)){
      if(is_array($id)) {
        $sql .= ' where `'.$idField.'` in ('.implode(',', $id).')';
      }
      else {
        $sql .= ' where `'.$idField.'`='.$this->quoteSmart($id);
      }
			$sql .= ' and '.$cond;
		  $result = $this->sendQuery($sql);
		}
    elseif(!empty($id) && ($id > 0 || is_array($id))){
      if(is_array($id)) {
        $sql .= ' where `'.$idField.'` in ('.implode(',', $id).')';
      }
      else {
        $sql .= ' where `'.$idField.'`='.$this->quoteSmart($id);
      }
		  $result = $this->sendQuery($sql);
		}
    elseif(!empty($cond)){
			$sql .= ' where '.$cond;
		  $result = $this->sendQuery($sql);
		}
    else{
      trigger_error('DBManipulatorMySQL::setRecord, fatal error : unable to build sql query', E_USER_ERROR);
		}
    $this->debug('DBManipulatorMySQL::setRecord, status : function completed', 10, DBG_CLOSE);
		return $result;
	}
	
	function addRecord($tableName, $fieldsArray) {
    $this->debug('DBManipulatorMySQL::addRecord, status : function invoked', 10, DBG_OPEN);
    $result = false;
		if (!is_array($fieldsArray)) {
      $this->debug('DBManipulatorMySQL::addRecord, warning : fields_array is missing', 4);
		}
		else {
			$cols = '';
			$vals = '';
			foreach ($fieldsArray as $field => $val) {
				$cols .= '`'.$field.'`,';
				$vals .= $this->quoteSmart($val).',';
			}
      // remove trailing ','
			$cols = substr($cols, 0, -1);
      // remove trailing ','
			$vals = substr($vals, 0, -1);
			$sql = 'insert into `'.$tableName.'` ('.$cols.') values ('.$vals.');';
      $result = $this->sendQuery($sql);
		}
    $this->debug('DBManipulatorMySQL::addRecord, status : function completed', 10, DBG_CLOSE);
		return $result;
	}

	function delRecord($tableName, $id, $cond='', $idField='id') {
    $this->debug('DBManipulatorMySQL::delRecord, status : function invoked', 10, DBG_OPEN);
		$sql = 'delete from `'.$tableName.'`';
    $result = false;
		if ((!empty($id) && ($id > 0 || is_array($id))) && !empty($cond)) {
      if(is_array($id)) {
        $sql .= ' where `'.$idField.'` in ('.implode(',', $id).')';
      }
      else {
        $sql .= ' where `'.$idField.'`='.$this->quoteSmart($id);
      }
			$sql .= ' and '.$cond;
      $result = $this->sendQuery($sql);
		}
		elseif (!empty($id) && ($id > 0 || is_array($id))) {
      if(is_array($id)) {
        $sql .= ' where `'.$idField.'` in ('.implode(',', $id).')';
      }
      else {
        $sql .= ' where `'.$idField.'`='.$this->quoteSmart($id);
      }
      $result = $this->sendQuery($sql);
		}
		elseif (!empty($cond)) {
			$sql .= ' where '.$cond;
      $result = $this->sendQuery($sql);
		}
		else {
      $this->debug('DBManipulatorMySQL::delRecord, warning : fields_array is missing', 4);
		}
    $this->debug('DBManipulatorMySQL::delRecord, status : function completed', 10, DBG_CLOSE);
		return $result;
	}

	function getDBFieldName($fieldArray) {
    return $fieldArray['Field'];
	}

	function getDBFieldType($fieldArray) {
		return $fieldArray['Type'];
	}

	function getDBFieldKey($fieldArray) {
		return $fieldArray['Key'];
	}

	function getTables($prefix) {
    $this->debug('DBManipulatorMySQL::getTables, status : function invoked', 10, DBG_OPEN);
		$sql = 'SHOW TABLES LIKE \''.$prefix.'%\';';
		$result = $this->sendQuery($sql);
    $this->debug('DBManipulatorMySQL::getTables, status : function completed', 10, DBG_CLOSE);
		return $result;
	}

	function getTable($tableName) {
    $this->debug('DBManipulatorMySQL::getTable, status : function invoked', 10, DBG_OPEN);
		$sql = 'show tables like \''.$tableName.'\';';
    $result = $this->sendQuery($sql);
    $this->debug('DBManipulatorMySQL::getTable, status : function completed', 10, DBG_CLOSE);
		return $result;
	}
	
	function countTypeTables() {
    $this->debug('DBManipulatorMySQL::countTypeTables, status : function invoked', 10, DBG_OPEN);
		$count = 0;
		if(($result = $this->getTables(TYPE_TABLES_PREFIX)) != false) {
		  $count = $this->getNumRows();
		}
    $this->debug('DBManipulatorMySQL::countTypeTables, status : function completed', 10, DBG_CLOSE);
		return $count;
	}

	function tableExists($table) {
    $this->debug('DBManipulatorMySQL::tableExists, status : function invoked', 10, DBG_OPEN);
    $result = true;
		if (($this->getTable($table) === false) || ($this->getNumRows() == 0)) {
      $result = false;
		}
    $this->debug('DBManipulatorMySQL::tableExists, status : function completed', 10, DBG_CLOSE);
		return $result;
	}

	function recordExists($table, $id, $cond = '', $idField = 'id') {
    $this->debug('DBManipulatorMySQL::recordExists, status : function invoked', 10, DBG_OPEN);
    $result = false;
		if(($result_array = $this->getRecord($table, $id, $cond, $idField)) != false) {
  		$result = is_array($this->fetchArray($result_array));
    }
    $this->debug('DBManipulatorMySQL::recordExists, status : function completed', 10, DBG_CLOSE);
		return $result;
	}

	/**
	 * Create new table
	 *
	 * @param  string $table
	 * @param  array  $columnsArray
	 * @return boolean
	 * @final
	 */
	function createTable($table, $columnsArray) {
    $query  = '';
    if(!array_key_exists('id', $columnsArray)) {
      $query .= "`id` int not null auto_increment ";
    }
    if(isset($columnsArray) && count($columnsArray) > 0) {
      foreach($columnsArray as $key => $value) {
        if(strlen($query) > 0) {
          $query .= ", ";
        }
        $query .= "`$key` $value not null ";
        if(strcasecmp($key, 'id') == 0) {
          $query .= "auto_increment ";
        }
      }
    }
    $query  = "create table `{$table}` (".$query.", primary key (`id`));";
    return $this->sendQuery($query);
	}

	/**
	 * Execute DROP statement
	 *
	 * @param  string $table
	 * @return boolean
	 * @final
	 */
	function dropTable($table) {
		$sql = "drop table `$table`;";
    return $this->sendQuery($sql);
  }

	/**
	 * Execute ALTER statement on given table
	 *
	 * @param  string $table
	 * @param  array  $newColumnsArray
	 * @param  array  $oldColumnsArray
	 * @return boolean
	 * @final
	 */
	function updateTable($table, $newColumnsArray, $oldColumnsArray) {
		$query = '';
    foreach($newColumnsArray as $key => $value) {
      if(strlen($query) > 0) {
        $query .= ', ';
      }
			$query .= "add `$key` $value not null";
    }
    foreach($oldColumnsArray as $key => $value) {
			if(strlen($query) > 0) {
        $query .= ', ';
      }
			$query .= "drop `$key`";
    }
		$query = "alter table `$table` ".$query.";";
    return $this->sendQuery($query);
	}

  function updateColumnName($table, $newColumnName, $oldColumnName) {
    $query = "show full columns from `$table`;";
    $result = false;
    $result_array = $this->sendQuery($query);
    if($result_array !== false) {
      while($row = $this->fetchArray($result_array)) {
        if($row['Field'] == $oldColumnName) {
          break;
        }
      }
      if($row['Field'] == $oldColumnName) {
        $default = '';
        $null = 'null';
        $type = $row['Type'];
        if($row['Default'] != '') {
          $default = "default '".$row['Default']."'";
        }
        if($row['Null'] == '') {
          $null = 'not null';
        }
    		$query = "alter table `$table` change column `$oldColumnName` `$newColumnName` $type $default $null;";
        $result = $this->sendQuery($query);
      }
    }
    return $result;
  }

  function updateColumnType($table, $columnName, $newColumnType) {
    $query = "alter table `$table` change `$columnName` `$columnName` $newColumnType not null;";
    return $this->sendQuery($query);
  }

	/**
	 * Execute ALTER statement on given table
	 *
	 * @param  string $table
	 * @return boolean
	 * @final
	 */
	function renameTable($oldTableName, $newTableName) {
		$query = "alter table `$oldTableName` rename `$newTableName`;";
    return $this->sendQuery($query);
	}

	function typeExists($typeName) {
		return $this->tableExists(TYPE_TABLES_PREFIX.$typeName);
	}

  function pageExists($pageId) {
		return $this->recordExists(PAGES_TABLE, $pageId);
	}

  function containerExists($containerId) {
		return $this->recordExists(CONTAINERS_TABLE, $containerId);
	}
	
	function menuExists($menuId) {
		return $this->recordExists(MENUS_TABLE, $menuId);
	}

	function containersListExists($containersListId) {
		return $this->recordExists(CONTAINERS_LISTS_TABLE, $containersListId);
	}

  function templateExists($typeName, $id) {
		return $this->recordExists(TEMPLATES_TABLE, $id, "`type`='$typeName'");
	}

  function languageExists($lang_id) {
		return $this->recordExists(LANGUAGES_TABLE, $lang_id);
	}

	function getTemplateId($templateName, $typeName) {
    $this->debug('DBManipulatorMySQL::getTemplateId, status : function invoked ('.$templateName.', '.$typeName.')', 10, DBG_OPEN);
    $template_id = -1;
    if(strlen($typeName) <= 0) {
      trigger_error("DBManipulatorMySQL::getTemplateId, error : one of the arguments is missing", E_USER_ERROR);
    }
    else {
  		$result = $this->getRecord(TEMPLATES_TABLE, "", "`name`='$templateName' and `type`='$typeName'");
      if(!($row = $this->fetchArray($result))) {
      	trigger_error("DBManipulatorMySQL::getTemplateId, error : no matching template in database ($templateName)", E_USER_ERROR);
      }
      else {
        $template_id = $row["id"];
      }
    }
    $this->debug('DBManipulatorMySQL::getTemplateId, status : function completed', 10, DBG_CLOSE);
		return $template_id;
	}

	function getStaticContainerId($frameId, $containerName) {
    $this->debug('DBManipulatorMySQL::getStaticContainerId, status : function invoked', 10, DBG_OPEN);
		$result = $this->getRecord(FRAME_CONTAINERS_TABLE, $frameId, "`container_name`='$containerName'", "frame_id");
		$container_id = -1;
	  if(!($row = $this->fetchArray($result))){
    	$this->debug("DBManipulatorMySQL::getStaticContainerId, error : no matching container in database (frame $frameId, container $containerName)", 4);
		}
		else {
  		$container_id = $row['container_id'];

  		while(true) {
  			$result = $this->getRecord(CONTAINERS_TABLE, $container_id);
  		  if(($row = $this->fetchArray($result)) === false){
        	trigger_error("DBManipulatorMySQL::getStaticContainerId, error : no matching container in database ($container_id)", E_USER_ERROR);
          $container_id = -1;
  				break;
  		  }
  		  $cloneId = $row['clone_id'];
  		  if($cloneId == 0) {
  	      break;
  	    }
  			$container_id = $cloneId;
  		}
    }
    $this->debug('DBManipulatorMySQL::getStaticContainerId, status : function completed', 10, DBG_CLOSE);
		return $container_id;
	}	 

	function getStaticContainersListId($frameId, $containersListName) {
    $this->debug('DBManipulatorMySQL::getStaticContainersListId, status : function invoked', 10, DBG_OPEN);
		$result = $this->getRecord(FRAME_CONTAINERS_LISTS_TABLE, $frameId, "`containers_list_name`='$containersListName'", "frame_id");
		$containers_list_id = -1;
	  if(!($row = $this->fetchArray($result))){
    	$this->debug("DBManipulatorMySQL::getStaticContainersListId, warning : no matching containersList in database (frame $frameId, containersList $containersListName)", 4);
		}
		else {
  		$containers_list_id = $row['containers_list_id'];

  		while(true) {
  			$result = $this->getRecord(CONTAINERS_LISTS_TABLE, $containers_list_id);
  		  if(($row = $this->fetchArray($result)) === false){
        	trigger_error("DBManipulatorMySQL::getStaticContainersListId, error : no matching containersList in database ($containers_list_id)", E_USER_ERROR);
          $containers_list_id = -1;
  				break;
  		  }
  		  $cloneId = $row['clone_id'];
  		  if($cloneId == 0) {
  	      break;
  	    }
  			$containers_list_id = $cloneId;
  		}
    }
    $this->debug('DBManipulatorMySQL::getStaticContainersListId, status : function completed', 10, DBG_CLOSE);
		return $containers_list_id;
	}

	function addFrameMenu($frameId, $menuId, $menuName) {
    $this->debug('DBManipulatorMySQL::addFrameMenu, status : function invoked', 10, DBG_OPEN);
    $result = true;
		$fieldsArray["frame_id" ] = $frameId;
		$fieldsArray["menu_id"  ] = $menuId;
		$fieldsArray["menu_name"] = $menuName;
		if(($this->addRecord(FRAME_MENUS_TABLE, $fieldsArray)) <= 0){
			$this->debug("DBManipulatorMySQL::addFrameMenu, error : unable to add new record.", 2);
			$result = false;
		}
    $this->debug('DBManipulatorMySQL::addFrameMenu, status : function completed', 10, DBG_CLOSE);
		return $result;
	}

	function addFrameContainer($frameId, $pageId, $containerId, $containerName) {
    $this->debug('DBManipulatorMySQL::addFrameContainer, status : function invoked', 10, DBG_OPEN);
    $result = true;
		$fieldsArray["frame_id"      ] = $frameId;
		$fieldsArray["page_id"       ] = $pageId;
		$fieldsArray["container_id"  ] = $containerId;
		$fieldsArray["container_name"] = $containerName;
		if(($this->addRecord(FRAME_CONTAINERS_TABLE, $fieldsArray)) <= 0){
			$this->debug("DBManipulatorMySQL.addFrameContainer, error : unable to add new record.", 2);
			$result = false;
		}
    $this->debug('DBManipulatorMySQL::addFrameContainer, status : function completed', 10, DBG_CLOSE);
		return $result;
	}	 

	function removeFrameContainer($frameId, $pageId, $containerId, $containerName) {
    $this->debug('DBManipulatorMySQL::removeFrameContainer, status : function invoked', 10, DBG_OPEN);
    $result = true;
		if(($this->delRecord(FRAME_CONTAINERS_TABLE, $frameId, "`page_id` = '$pageId' and `container_name` = '$containerName' and `container_id` = '$containerId'", 'frame_id')) <= 0){
			$this->debug("DBManipulatorMySQL::removeFrameContainer, error : unable to remove record.", 2);
			$result = false;
		}
    $this->debug('DBManipulatorMySQL::removeFrameContainer, status : function completed', 10, DBG_CLOSE);
		return $result;
	}

	function addFrameContainersList($frameId, $pageId, $containersListId, $containersListName) {
    $this->debug('DBManipulatorMySQL::addFrameContainersList, status : function invoked', 10, DBG_OPEN);
    $result = true;
		$fieldsArray["frame_id"]              = $frameId;
		$fieldsArray["page_id"]               = $pageId;
		$fieldsArray["containers_list_id"]    = $containersListId;
		$fieldsArray["containers_list_name"]  = $containersListName;
		if(($this->addRecord(FRAME_CONTAINERS_LISTS_TABLE, $fieldsArray)) <= 0){
			$this->debug("DBManipulatorMySQL::addFrameContainersList, error : unable to add new record.", 2);
			$result = false;
		}
    $this->debug('DBManipulatorMySQL::addFrameContainersList, status : function completed', 10, DBG_CLOSE);
		return $result;
	}	 
	function removeFrameContainersList($frameId, $pageId, $containersListId, $containersListName) {
    $this->debug('DBManipulatorMySQL::removeFrameContainersList, status : function invoked', 10, DBG_OPEN);
    $result = true;
		if(($this->delRecord(FRAME_CONTAINERS_LISTS_TABLE, $frameId, "`page_id` = '$pageId' and `containers_list_name` = '$containersListName' and `containers_list_id` = '$containersListId'", 'frame_id')) <= 0){
			$this->debug("DBManipulatorMySQL::removeFrameContainersList, error : unable to remove record.", 2);
			$result = false;
		}
    $this->debug('DBManipulatorMySQL::removeFrameContainersList, status : function completed', 10, DBG_CLOSE);
		return $result;
	}
	
	function &getPageByAlias($alias, $langId) {
    $this->debug('DBManipulatorMySQL::getPageByAlias, status : function invoked', 10, DBG_OPEN);
    $page_id = -1;
    $alias = addslashes($alias);
		$sql = "SELECT `".PAGES_TABLE."`.`id`, `".NAMES_TABLE."`.`lang_id` FROM `".NAMES_TABLE."` inner join `".PAGES_TABLE."` on `".PAGES_TABLE."`.`alias_id` = `".NAMES_TABLE."`.`id` WHERE `".PAGES_TABLE."`.`deleted` = '0' and UPPER(`".NAMES_TABLE."`.`value`)='".strtoupper($alias)."'";
    if(($result = $this->sendQuery($sql)) != false) {
	    while(($row = $this->fetchArray($result)) != false){
				if(is_array($row) && $row['lang_id'] == $langId) {
					$page_id = $row['id'];
					break;
				}
			}
		}
    $this->debug('DBManipulatorMySQL::getPageByAlias, status : function completed', 10, DBG_CLOSE);
    return $page_id;
	}
	
	/**
	 * Create a Content Type Table
	 *
	 * @param  string $typeName
	 * @param  array  $fieldsArray
	 * @return boolean
	 */
	function addContentType($typeName, $fieldsArray) {
    $this->debug('DBManipulatorMySQL::addContentType, status : function invoked', 10, DBG_OPEN);
		$tableName = TYPE_TABLES_PREFIX.$typeName;
		$result = true;
		
		if ($this->tableExists($tableName)) {
			$this->debug("DBManipulatorMySQL::addContentType, error : type '$typeName' already exists.", 3);
		}
		else {
  		foreach ($fieldsArray as $name => $type) {
  			$dbmsType = $this->getDBMSType($type);

  			if ($dbmsType == -1) {
  				$this->debug("DBManipulatorMySQL.addContentType, error : unknown DBMS ('$dbmsType')", 2);
  				$result = false;
          break;
  			}

  			$fieldsArray[$name] = $dbmsType;
  		}
      if($result != false) {
    		// mandatory fields
    		$fieldsArray['published'] = $this->getDBMSType(constant('INTEGER'));
    		$fieldsArray['lang_id']   = $this->getDBMSType(constant('INTEGER'));
    		$fieldsArray['acl_id']    = $this->getDBMSType(constant('INTEGER'));
    		$fieldsArray['deleted']   = $this->getDBMSType(constant('BOOL'));

        $result = $this->createTable($tableName, $fieldsArray);
      }
    }
    $this->debug('DBManipulatorMySQL::addContentType, status : function completed', 10, DBG_CLOSE);
		return $result;
	}

	function quoteSmart($value) {
    $this->debug('DBManipulatorMySQL::quoteSmart, status : function invoked', 10, DBG_OPEN);
		if (get_magic_quotes_gpc()) {
			$value = stripslashes($value);
		}
		if (!is_numeric($value)) {
      $value = "'".mysql_real_escape_string($value)."'";
		}
		elseif($value == 0) {
      $value = '0';
		}
    $this->debug('DBManipulatorMySQL::quoteSmart, status : function completed', 10, DBG_CLOSE);
		return $value;
	}
}
?>