<?php defined('SYSPATH') or die('No direct access allowed.');

/**
 *
 * @package    PTA
 * @subpackage Library Volumes
 * @category   Models
 * @author     Deon George
 * @copyright  (c) 2010 phpTSMadmin Development Team
 * @license    http://phptsmadmin.sf.net/license.html
 */
class Model_LIBVOLUME extends ORM_TSM {
	protected $_table_name = 'LIBVOLUMES';
	protected $_primary_key = 'HOME_ELEMENT';
	protected $_sorting = array(
		'VOLUME_NAME'=>'ASC',
	);

	protected $_has_one = array(
		'VOLUME'=>array('foreign_key'=>'VOLUME_NAME','far_key'=>'VOLUME_NAME'),
	);
	protected $_has_many = array(
		'VOLHISTORY'=>array('foreign_key'=>'VOLUME_NAME','far_key'=>'VOLUME_NAME'),
	);

	public function usage() {
		switch (strtolower($this->STATUS)) {
			case NULL: return 'empty';
			case 'scratch': return strtolower($this->STATUS);

			case 'private':
				if ($this->VOLUME->STATUS == 'EMPTY')
					return 'empty';

				switch (strtolower($this->LAST_USE)) {
					case 'dbbackup':
					case 'data':
						return strtolower($this->LAST_USE);

					default: return 'unkown';
				}

			default: return 'notcheckedin';
		}
	}

	public function volusage() {
		switch ($this->usage()) {
			case 'data':
			case 'empty': return $this->VOLUME->STGPOOL_NAME;
			case 'dbbackup': return $this->VOLHISTORY->lastuse()->TYPE;
			case 'notcheckedin': return _('Not Checked In');
			case 'scratch': return _('Scratch');

			default: return _('Unknown');
		}
	}

	public function status() {
		switch ($this->usage()) {
			case 'data': return sprintf('%s/%s',$this->VOLUME->display('STATUS'),$this->VOLUME->display('ACCESS'));
			case 'dbbackup': return $this->VOLHISTORY->lastuse()->backupid();

			default: return '';
		}
	}

	public function access() {
		if ($this->usage() == 'scratch')
			return '';
		elseif ($this->STATUS)
			return sprintf('%s/%s',$this->display('STATUS'),$this->display('OWNER'));
		else
			return '';
	}

	public function lastwrite() {
		switch ($this->usage()) {
			case 'data':
			case 'empty': return $this->VOLUME->display('LAST_WRITE_DATE');
			case 'dbbackup': return $this->VOLHISTORY->lastuse()->display('DATE_TIME');

			default: return '';
		}
	}

	public function removable() {
		return ($this->usage() == 'dbbackup' OR (! in_array($this->usage(),array('empty','scratch')) AND $this->VOLUME->STGPOOL->POOLTYPE != 'PRIMARY')) ? TRUE : FALSE;
	}
}
?>
