<?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 AnDomainContext
{
private $__entities = array();
private $__dirty = array();
private $__new = array();
private $__delete = array();
public function __construct() {}
public function addNew(AnDomainModelInterface $entity)
{
if ( !$entity->id && !$this->isNew($entity) ) {
$this->__new[] = $entity;
}
}
public function addDirty(AnDomainModelInterface $entity)
{
if ( $entity->id && !$this->isNew($entity) && !$this->isDeleted($entity) ) {
$this->__dirty[$this->globalKey($entity)] = $entity;
}
}
public function addDelete(AnDomainModelInterface $entity)
{
if ( $entity->id && !$this->isNew($entity) && !$this->isDirty($entity) ) {
$this->__delete[$this->globalKey($entity)] = $entity;
}
}
public function addClean(AnDomainModelInterface $entity)
{
$key = $this->globalKey($entity);
unset($this->__dirty[$key]);
unset($this->__delete[$key]);
if ( in_array($entity, $this->__new, true) ) {
$pruned = array();
foreach($this->__new as $thisentitiy) {
if ( $thisentitiy !== $entity )
$pruned[] = $thisentitiy;
}
$this->__new = $pruned;
}
if ( isset($this->_properties[$entity->getHandle()]) ) {
$this->_properties_change_track[$entity->getHandle()] = array(
'original' => $entity->_properties,
'new' => array()
);
}
}
public function isNew(AnDomainModelInterface $entity)
{
return in_array($entity, $this->__new, true);
}
public function isDirty(AnDomainModelInterface $entity)
{
return in_array($entity, $this->__dirty, true);
}
public function isDeleted(AnDomainModelInterface $entity)
{
return in_array($entity, $this->__delete, true);
}
public function save($error_context = null)
{
//must use command execute
if ( is_null($error_context) )
$error_context = KFactory::tmp('lib.koowa.command.context');
while (count($this->__new) || count($this->__dirty) || count($this->__delete) ) {
foreach($this->__new as $key => $entity) {
if ( $entity->save($error_context) === false ) {
return false;
}
}
foreach($this->__dirty as $key => $entity) {
if ( $entity->save($error_context) === false ) {
return false;
}
}
foreach($this->__delete as $key=>$entity) {
if ( $entity->save($error_context) === false ) {
return false;
}
}
}
$this->__delete = array();
$this->__dirty = array();
$this->__new = array();
return true;
}
public function getEntities($identifier)
{
if ( !isset($this->__entities[$identifier]) ) {
return array();
}
return $this->__entities[$identifier];
}
protected $_properties_change_track = array();
/**
* Track the changes of the property of an entity
* @return
* @param $entity Object
* @param $name Object
* @param $newValue Object
*/
public function propertyValueChanged($entity, $name, $newValue)
{
if ( $entity->isNew() ) return;
$track = $this->_properties_change_track[$entity->getHandle()];
$original_value = $track['original'][$name];
$track['new'][$name] = array('new'=>$newValue, 'original'=>$original_value);
$this->_properties_change_track[$entity->getHandle()] = $track;
$entity->markDirty();
}
/**
*
* @return
* @param $entity Object
*/
public function propertyChangeTracks($entity)
{
if ( $entity->isNew() ) return null;
$track = $this->_properties_change_track[$entity->getHandle()];
return $track['new'];
}
public function add(AnDomainModelInterface $entity)
{
$identifiers = $entity->getMapper()->getInheritanceTree();
$values = $entity->uniqueValues();
foreach($identifiers as $identifier) {
if ( !isset($this->__entities[$identifier]) ) {
$this->__entities[$identifier] = array();
}
foreach($values as $property=>$value) {
$key = $property.$value;
$this->__entities[$identifier][$key] = $entity;
}
}
if ( !isset($this->_properties[$entity->getHandle()]) ) {
$this->_properties_change_track[$entity->getHandle()] = array(
'original' => $entity->_properties,
'new' => array()
);
}
}
public function exists($identifier, $uniques = null)
{
if ($identifier instanceof AnDomainModelAbstract) {
$values = $identifier->uniqueValues();
$identifier = get_class($identifier);
} else if ($identifier instanceof AnDomainQuery) {
$values = $identifier->uniqueConditions();
$identifier = $identifier->getMapper()->getInheritanceTree();
$identifier = $identifier[0];
} else if ( $identifier instanceof AnDomainMapper ) {
$identifier = $identifier->getInheritanceTree();
$identifier = $identifier[0];
$values = $uniques;
} else {
debug_print_backtrace();
die('cant use identifier byitself');
$values = $uniques;
}
$identifier = (string) $identifier;
if ( !isset($this->__entities[$identifier]) ) {
return null;
}
foreach($values as $key => $value) {
$key = $key.$value;
if ( isset($this->__entities[$identifier][$key]) ) {
return $this->__entities[$identifier][$key];
}
}
return null;
}
public function globalKey(AnDomainModelInterface $entity)
{
return $this->_createKey($entity->getIdentifier(), $entity->getId());
}
protected function _createKey($identifier,$id)
{
return ( (string) $identifier.$id);
}
}