Location: PHPKode > projects > Anahita Social Engine > dependencies/plg_system_socialengine/socialengine/model/person/information.php
<?php
/**
 * @version		1.0.0
 * @category	Anahita
 * @copyright	Copyright (C) 2007 - 2010 rmd Studio 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 AnModelPersonInformation extends KObject  implements IteratorAggregate, ArrayAccess
{

		
	protected $_information = array();
	
	protected $_person		= null;	
	
	/**
	 * initializes the information object with key/value pairs of information
	 * @return 
	 * @param $options Object[optional]
	 */
	public function __construct($options=array())
	{
		$this->_information = $options;
		$this->setInfo('lookingFor', isset($this->_information['lookingFor']) ? $this->_information['lookingFor'] : array());
		
	}
	
	/**
	 * 
	 * @return 
	 * @param $person Object
	 */
	public function setPerson($person)
	{
		$this->_person = $person;
		return $this;
	}
	
	/**
	 * uses the acl to check whether the current session viewer has access to certain fields
	 * @return 
	 * @param $name Object
	 */
	public function hasAccess($name)
	{
		if ( !$this->_person )
			return true;
		
		$acl = $this->_person->getAcl();
		
		return $acl->canSeePersonalInformation($name);
	}
	
	/**
	 * return the value of the information property
	 * @return string
	 * @param $name Object
	 */
	public function getInfo($name)
	{
		$value 	= isset($this->_information[$name]) && $this->hasAccess($name) ? $this->_information[$name] : '';
		
		if ( $name == 'dateOfBirth' && $value ) {
			if ( $value == '0000-00-00 00:00:00' ) {
				return null;	
			}
			$this->_information[$name] = $value = new KDate(array('date' => $value));
		} else 	if ($name == 'lookingFor' && is_string($this->_information[$name]) ) {
			//@TODO should this be here in the model - what if the value is invalid
			$this->_information[$name] = $value = explode(',', $value);
		} else if ( $name == 'givenName' || $name == 'familyName' ) {
			$value = ucfirst($this->_information[$name]);
		}
		return $value;
	}
	
	/**
	 * sets the information value
	 * @return 
	 * @param $name Object
	 * @param $value Object
	 */
	public function setInfo($name, $value)
	{						
		$this->_information[$name] = $value;
				
		if ( $this->_person ) {
			
			if ( $name == 'givenName' ) {							
				$person_name = $value.' '.$this->getInfo('familyName');
				$this->_person->name = $person_name;
			} else if ( $name == 'familyName' ) {			
				$person_name = $this->getInfo('givenName').' '.$value;
				$this->_person->name = $person_name;
			} else if ( $name == 'gender' ) {
				$this->_person->gender = $value;
			}
			//the only way to set this
			$this->_person->getDomainContext()->propertyValueChanged($this->_person, 'information', $this);
		}
	}
	
	/**
	 * 
	 * @return 
	 */		
    public function getIterator() {
       
		$accessable = array();
		foreach($this->_information as $name => $value) {
			$accessable[$name] = $this[$name];
		}
	    return new ArrayIterator($accessable);
    }
	
	/**
	 * 
	 * @return 
	 * @param $offset Object
	 */
	public function offsetExists($offset)
	{
		return $this->hasAccess($offset);
	}
	
	/**
	 * 
	 * @return 
	 * @param $offset Object
	 */
	public function offsetGet($offset)
	{
		return $this->getInfo($offset);
	}

	/**
	 * 
	 * @return 
	 * @param $offset Object
	 * @param $value Object
	 */
	public function offsetSet($offset, $value)
	{
		$this->setInfo($offset, $value);
	}
	
	/**
	 * 
	 * @return 
	 * @param $offset Object
	 */
	public function offsetUnSet($offset)
	{
		$this->setInfo($offset, null);
	}
	
	/**
	 * 
	 * @return 
	 */
	public function serialize()
	{		
		$data = (array) $this->getIterator();
		if ( isset($data['dateOfBirth']) && $data['dateOfBirth'] instanceof KDate ) {
			$data['dateOfBirth'] = $data['dateOfBirth']->getDate();
		}
		if ( isset($data['lookingFor']) && is_array($data['lookingFor']) ) {
			$data['lookingFor'] = implode(',', $data['lookingFor']);
		}
		return $data;
	}		
}
Return current item: Anahita Social Engine