<?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
 * @subpackage Types
 */

include_once('classes/core/DBAccessorObject.class.php');
include_once('classes/db/DBConnection.class.php');
include_once('classes/core/DBManipulator.class.php');

/**
 * Type Class
 *
 * @author	   Cedric Francoys <cedric.francoys@easy-cms.org>
 * @version	   Type.class, Sat Jan 08 20:33:55 CET 2005
 * @package    Core
 * @subpackage Types
 */
class Type extends DBAccessorObject {
	/**
	 * Type Name
	 *
	 * @var string
	 */
	var $name;
	
	var $fieldsTable;


	/**
	 * Constructor
	 *
	 * @param string  $name
	 * @param object  $dbManipulator
	 * @param integer $langId
	 */
	function Type($name, &$dbManipulator) {
		parent::DBAccessorObject($dbManipulator);        
		$this->name = $name;
    $this->fieldsTable = $this->getPrivateFieldsArray();
	}

  function getPrivateFieldsArray() {
    return array('id' => 'INT(11)', 'lang_id' => 'INT(4)', 'acl_id' => 'INT(11)', 'author_id' => 'INT(11)', 'deleted' => 'TINYINT(1)', 'published' => 'TINYINT(1)', 'mailing_list_id' => 'INT(11)');
  }

  function loadFieldsTable() {
		$this->fieldsTable = $this->dbManipulator->getFieldsArray($this->getTableName());
  }

  function getFieldsTable() {
    return $this->fieldsTable;
  }

	/**
	 * Gets Type Name
	 *
	 * @return string
	 */
	function getTypeName() {
		return $this->name;
	}
	
	/**
	 * Sets Type Name
	 *
	 * @param string $name
	 */
	function setTypeName($name) {
		$this->name = $name;
	}

	/**
	 * Gets Table Name
	 *
	 * Returns name of table that holds Type records
	 *
	 * @return string
	 */
	function getTableName() {
		return TYPE_TABLES_PREFIX.$this->getTypeName();
	}
	
	/**
	 * Checks Record
	 *
	 * Checks if record of given $id and $langId exists in table
	 *
	 * @param  integer $id record Id
	 * @param  integer $langId language Id
	 * @return boolean
	 */
	function recordExists($id, $langId)	{
		$dbManipulator = &$this->getDBManipulator();
		$tableName     = $this->getTableName();
		
		$result = $dbManipulator->getRecord($tableName, $id, '`lang_id`='.$langId);
		
		if (!$result || !$dbManipulator->getNumRows()) {
			return false;
		}
		
		return true;
	}

	/**
	 * Gets Record
	 *
	 * Gets record of given id and language from table
	 *
	 * @param  integer $id
	 * @param  integer $langId
	 * @return array
	 * @todo approve modification
	 */
	function getRecord($id, $langId) {
		$dbManipulator = &$this->getDBManipulator();
		$tableName     = $this->getTableName();		
		
		$result = $dbManipulator->getRecord($tableName, $id, '`lang_id`='.$langId);
		
		if (!$result || !$dbManipulator->getNumRows()) {
			$this->debug('Type::getRecord, error : unable to get record(id='.$id.' lang_id='.$langId.')', 3);
			return false;
		}
		
		return $dbManipulator->fetchArray($result);
	}

	/**
	 * Sets Record
	 *
	 * Updates record of given id and language in table
	 *
	 * @param  integer $id
	 * @param  array   $fieldsArray
	 * @param  inger $langId
	 * @return boolean
	 * @todo approve modification
	 */
	function setRecord($id, $fieldsArray, $langId) {
		$dbManipulator = &$this->getDBManipulator();
		$tableName     = $this->getTableName();
		
		$result = $dbManipulator->setRecord($tableName, $id, $fieldsArray, '`lang_id`='.$langId);
		
		if (!$result || !$dbManipulator->getAffectedRows()) {
			$this->debug('Type::setRecord, warning : unable to update or no change for record(id='.$id.' lang_id='.$langId.')', 5);
			return false;
		}
		
		return true;
	}

	/**
	 * Adds Record
	 *
	 * Adds record of id and language into table
	 *
	 * @param  array   $fieldsArray
	 * @param  integer $id
	 * @param  integer $langId
	 * @return integer last id or <0 if error
	 * @todo approve modification
	 */
	function addRecord($id, $fieldsArray, $langId) {
		$dbManipulator = &$this->getDBManipulator();
		$tableName     = $this->getTableName();
		
		$fieldsArray['lang_id'] = $langId;
		
		if ($id > 0) {
			$fieldsArray['id'] = $id;
		}
		
		$result = $dbManipulator->addRecord($tableName, $fieldsArray);
		
		if (!$result || !$dbManipulator->getAffectedRows()) {
			return 0;
		}
		
		if (!$id) {
			if (!($id = $dbManipulator->getLastId())) {
				return 0;
			}
		}
		
		return $id;
	}

	/**
	 * Deletes Record
	 *
	 * Deletes record of given id and language from table
	 *
	 * @param  integer $id
	 * @param  integer $langId
	 * @return boolean
	 * @todo approve modification
	 */
	function delRecord($id, $langId) {
		$dbManipulator = &$this->getDBManipulator();
		$tableName     = $this->getTableName();
		
		$result = $dbManipulator->delRecord($tableName, $id);
		
		if (!$result || !$dbManipulator->getAffectedRows()) {
			$this->debug('Type::delRecord, error : unable to delete record(id='.$id.' lang_id='.$langId.')', 3);
			return false;
		}
		
		return true;
	}

	/**
	 * Gets Field Value
	 * 
	 * @param  string $field_name
	 * @return mixed  null if error
	 * @todo check for uselessness
	 */
	function getField($field_name) {
		if (!$this->checkField($field_name)) {
      return null;
		}
		return $this->fieldsTable[$field_name];
	}

	/**
	 * Checks Fields
	 *
	 * Checks if $field_name is defined in {@link $fieldsTable}
	 *
	 * @return boolean
	 */
  function checkField($field_name) {
    return isset($this->fieldsTable[$field_name]);
  }
	
  function getFieldsCount() {
		return count($this->fieldsTable);
  }
	
  function addField($field_name, $cms_type) {
		if($this->checkField($field_name)){
			$this->debug("Type(".$this->getTypeName().")::addField, warning : field '$fieldName' already exists (add skipped).");
		}
		else {
			$this->fieldsTable[$field_name] = $cms_type;
    }
  }

  function removeField($field_name) {
		if(!$this->checkField($field_name)){
			$this->debug("Type(".$this->getTypeName().").removeField, warning : unknown field '$field_name' (remove skipped).");
		}
		else {
			$this->fieldsTable[$field_name] = null;
			unset($this->fieldsTable[$field_name]);
		}
  }

  function renameField($field_name, $new_name) {
		$dbManipulator = &$this->getDBManipulator();
		$tableName     = $this->getTableName();
    $cms_type = $this->fieldsTable[$field_name];
    unset($this->fieldsTable[$field_name]);
    $this->fieldsTable[$new_name] = $cms_type;
    return $dbManipulator->updateColumnName($tableName, $new_name, $field_name);
  }

	function setField($field_name, $cms_type) {
		$dbManipulator = &$this->getDBManipulator();
		$tableName     = $this->getTableName();
		if (!$this->checkField($field_name)) {
      return null;
		}
		$this->fieldsTable[$field_name] = $cms_type;
    return $dbManipulator->updateColumnType($tableName, $field_name, $dbManipulator->getDBMSType($cms_type));
	}

}
?>