Location: PHPKode > projects > Anahita Social Engine > dependencies/plg_system_socialengine/socialengine/domain/mapper/table.php
<?php
/**
 * @version		1.0.0
 * @category	Anahita Social Engine™
 * @copyright	Copyright (C) 2008 - 2010 rmdStudio Inc. and Peerglobe Technology Inc. All rights reserved.
 * @license		GNU GPLv2 <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>
 * @link     	http://www.anahitapolis.com
 */

class AnDomainMapperTable extends KObject
{
	public $readonly = false;
	
	protected $_table;
	
	protected $_alias;
	
	protected $_fields = null;
	
	protected $_join_conditions = null;
	
	protected $_join_type;
		
	public function __construct($options=array())
	{	
		$table   	= strtolower($options['table']);
		
		$options 	= $this->_initialize($options);
		
		$this->_db	= $options['adapter'];
		
		$matches = array();
		
		if ( preg_match('/(\w+) +as +(\w+)/', $table, $matches) ) {
		
			$this->_table = $matches[1];
			$this->_alias = $matches[2];
			
		} else {
		
		    $this->_table = $table;	
			$this->_alias = null;
		}
		
		
	}
	
	/**
	 * 
	 * @return 
	 * @param $options Object
	 */
	protected function _initialize($options)
	{
		$default = array(
			'adapter' => KFactory::get('lib.koowa.database')
		);	
		
		return array_merge($default, $options);
	}
	
	public function getFields()
	{
		if ( !$this->_fields ) {
			$this->_fields = array();
			$fields = $this->_db->fetchTableFields($this->_table);
			$fields = array_keys($fields[$this->_table]);
			foreach($fields as $field) {
				$key = $field;
				if ( $this->_alias )
					$key = $this->_alias.'.'.$key;
													
				$this->_fields[$key]   = $field;
			}							
		}
		return $this->_fields;			
	}
	
	public function getTable()
	{
		return $this->_table;
	}
	
	public function getAlias()
	{	
		return $this->_alias;
	}
	
	public function extractData($data)
	{
		$values = array();

		foreach($this->getFields() as $key => $field) {
			$key = $field;
			if ( $this->_alias )
				$key = $this->_alias.'.'.$key;
			if ( isset($data[$key]) ) {
				$values[$field] = $data[$key];
			}
		}
		
		return $values;
	}
	
	public function update($data, $condition)
	{	
		if ( $this->readonly || !count($condition) ) return;	
		$query = $this->_buildCondition($condition);
		
		if ( !count($data) ) return;
		
		$rows = $this->_db->update($this->_table, $data, $query);
		
		if ( $rows == 0 && $this->_join_conditions && $this->_join_type != 'INNER' ) {
			//if it's left then first find the record, if not found then create the record;
			$data  =   array_merge($condition, $data);
			$result =  $this->_db->fetchResult($query->select('*')->from($this->_table));
			if ( !$result ) {
				return $this->insert($data);
			}
		}
		
		return $rows;
	}
	
	public function insert($data)
	{
		if ( $this->readonly ) return;		
		if ( !count($data) ) return;
		return $this->_db->insert($this->_table, $data);
	}
	
	public function delete($condition)
	{
		if ( $this->readonly || !count($condition) ) return;
		$query = $this->_buildCondition($condition);
		return $this->_db->delete($this->_table, $query);
	}
	
	protected function _buildCondition($condition)
	{
		$query = $this->_db->getQuery();
		foreach($condition as $key => $value) {
			$query->where($key,'=',$value);
		}		
		return $query;
	}
	
	public function joins($table, array $conditions, $type = 'INNER')
	{
		$this->_join_conditions = $conditions;
		$this->_join_type 		= $type;
	}
	
	public function getCondition($data)
	{
		$conditions=array();
		$fields = $this->getFields();
		foreach($this->_join_conditions as $key1 => $key2) {
			$tableKey = null;
			if ( isset($fields[$key1]) ) {
				$key = $key1;
			} else  {
				$key = $key2;
			}
			$value = isset($data[$key1]) ? $data[$key1] : $data[$key2];
			$conditions[$key] = $value;
		}
		$conditions = $this->extractData($conditions);
		return $conditions;

	}
	
}
Return current item: Anahita Social Engine