<?php

class DocumentationPage extends ActiveRecord {



	protected $tableName = 'documents';
	protected $hasMany = array(
		'documents' => array('DocumentationPage', 'parent_id'),
		'versions' => array('DocumentationPageVersion', 'document_id')
	);



	public function buildDBAdapter() {
		require_once 'DocumentationDBFactory.class.php';
		return DocumentationDBFactory::getAdapter();
	}



	public function getVersion($versionId = null) {
		$max = 0;
		$maxVersion = null;
		foreach ($this->versions as $version) {
			if ($versionId == $version->id) {
				return $version;
			}
			if ($version->id > $max) {
				$max = $version->id;
				$maxVersion = $version;
			}
		}
		if (!$versionId and $maxVersion) {
			return $maxVersion;
		}
		return $this->getNewVersion();
	}



	public function getNewVersion() {
		$v = new DocumentationPageVersion();
		$v->document_id = $this->id;
		return $v;
	}



} // class DocumentationPage

?>