Location: PHPKode > projects > EMIHR > emihr-0.1.3/includes/CUSTOMER/customer.class.php
<?php
// +----------------------------------------------------------------------+
// | PHP Version 4                                                        |
// +----------------------------------------------------------------------+
// | Copyright (c) 2005 Ematic Interactive                                |
// +----------------------------------------------------------------------+
// | EMIHR 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.								  |
// |																	  |
// | EMIHR 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 EMIHR; if not, write to the Free Software Foundation,     |
// | Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA		  |
// +----------------------------------------------------------------------+
// | Author: René de Kat <hide@address.com>                	              |
// +----------------------------------------------------------------------+
//
// $Id: customer.class.php,v 1.10 2005/09/04 13:31:00 RdK Exp $
/**
 * Customer class. 
 *
 * @category EMIHR
 * @package CUSTOMER
 * @version $Id: customer.class.php,v 1.10 2005/02/04 13:31:00 RdK Exp $
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 * @copyright (c) 2005 Ematic Interactive
 * @author   René de Kat <hide@address.com> 
 */
 
/**
 * Customer class. 
 *
 * @category EMIHR
 * @package CUSTOMER
 * @version $Id: customer.class.php,v 1.10 2005/02/04 13:31:00 RdK Exp $
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 * @copyright (c) 2005 Ematic Interactive
 * @author   René de Kat <hide@address.com> 
 */
class Customer extends StorableObject
{
	// {{{ properties
	
    /**
     * The primary key ID.
     * @var     integer
     * @access  public
     */	
	var $id 		=	0;
	
    /**
     * The company id. Foreign key which refers to company->id
     * @var     integer
     * @access  public
     */		
	var $companyId	= NULL;
	
    /**
     * The name of the customer
     * @var     string
     * @access  public
     */		
	var $name		= NULL;

    /**
     * Instance of project collection class
     * @var     object
     * @access  private
	 * @see		ProjectCollection::ProjectCollection()
     */	
	var $projectCollection = NULL;
	
    /**
     * The offset to start loading projects
     * @var     integer
     * @access  private
     */		
	var $projectOffSet = 0;

	// }}}
    // {{{ constructor

    /**
     * Customer constructor.
	 * @param int $id the id of the record
     * @access public
     */
	function Customer( $id = 0 )
	{
		settype($id,"integer");
		$this->initializeFields();
		$this->id = $id;			// }}}
    // {{{ initializeFields()

    /**
     * Initializes dbMappingFields which maps database tablefields to class variables
     * @access private
     */	 	
		$this->loadFromDb();
	}

	// }}}
    // {{{ deleteFromDb()

    /**
     * Deletes the record from the database
	 * It removes all related elements as well
     * @access public
     */	 
	function deleteFromDb()
	{
		$projectCollection = new ProjectCollection($this->id,0,0);
		for ($projectIndex = 0; $projectIndex < $projectCollection->count(); $projectIndex++)
		{
			$project = $projectCollection->getElement($projectIndex);
			$project->deleteFromDb();
		}
		unset($projectCollection);
		unset($projectIndex);
		
		parent::deleteFromDb();
	}

	// }}}		
    // {{{ initializeFields()

    /**
     * Initializes dbMappingFields which maps database tablefields to class variables
     * @access private
     */	
	function initializeFields()
	{
		$this->dbMappingFields = array(
			new dbMappingField( 'id',					'I', '', true ),
			new dbMappingField( 'companyId',			'I' ),
			new dbMappingField( 'name',					'S' )
		);
		$this->dbMappingTableName = 'customer';	
		
		settype($this->projectOffSet,"integer");
	}	
	
	// }}}
    // {{{ getTotalProjectCount()

    /**
     * Returns the total number of projects of the customer
     * @access private
	 * @return int the number of projects of the customer
     */	 	
	function getTotalProjectCount()
	{
		global $db, $moduleCollection;
		
		$result = 0;
		
		if (isset($moduleCollection) && $moduleCollection->isLoaded('PROJECT') )
		{
			$result = $db->querySimpleValue(sprintf("SELECT COUNT(id) as nrOfProjects FROM project WHERE project.customerId=%d",$this->id));
		}
		
		return $result;
	}

	// }}}
    // {{{ getProjects()

    /**
     * Loads projects into a collection starting at $offset. Maximum MAX_RECORDS_PER_PAGE
     * @access public
	 * @param int $offSet where to start the search. Handy for paging
	 * @param int $max_records Optional maximum nr of records to be loaded. If 0 all records will be returned. 
	 * @return array collection containing instances of project class
	 * @see Project::Project()
     */	 
	function getProjects($offSet = 0, $max_records = MAX_RECORDS_PER_PAGE)
	{
		global $moduleCollection, $project_loaded;
		
		settype($offSet, "integer");

		if (isset($moduleCollection) && $moduleCollection->isLoaded('PROJECT') )
		{
			if ($offSet >= $this->getTotalProjectCount() )
				$offSet = 0;
			
			if ( (!is_a($this->projectCollection,"ProjectCollection")) || ($this->projectOffSet !== $offSet) )
			{
				$this->projectOffSet = $offSet;
				$this->projectCollection = new ProjectCollection($this->id, $this->projectOffSet, $max_records);
			}
			return $this->projectCollection;
		}
		else
			return NULL;
	}
	
	// }}}
}

?>
Return current item: EMIHR