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

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

	protected $_has_one = array(
		'MEDIA'=>array('foreign_key'=>'VOLUME_NAME','far_key'=>'VOLUME_NAME'),
		'STGPOOL'=>array('foreign_key'=>'STGPOOL_NAME','far_key'=>'STGPOOL_NAME'),
	);
	protected $_has_many = array(
		'VOLUMEUSAGE'=>array('foreign_key'=>'VOLUME_NAME','far_key'=>'VOLUME_NAME'),
	);

	protected $_display_filters = array(
		'LAST_READ_DATE'=>array(
			array('ORM_TSM::date',array(':value','d-M-Y')),
		),
		'LAST_WRITE_DATE'=>array(
			array('ORM_TSM::date',array(':value','d-M-Y')),
		),
	);

	/**
	 * Get all the VOLUMEUSAGE for this VOLUME
	 */
	private function _volumeusage() {
		$result = array();

		// In the interest of performance, we load all the records and get PHP to process it.
		// Our ORM caching we reduce the hit on TSM.
		foreach (ORM::factory('VOLUMEUSAGE')->find_all() as $o)
			if ($o->VOLUME_NAME == $this->VOLUME_NAME)
				array_push($result,$o);

		return $result;
	}

	/**
	 * Get FILESYSTEMS on a VOLUME
	 * @param $type is BACKUP/ARCHIVE/SPACE MANAGED
	 */
	public function fs_byctype($type) {
		$k = sprintf('%s-%s-%s',__METHOD__,$this->VOLUME_NAME,$type);
		$c = Kohana::$config->load('config')->cache;

		if (is_null($result = Cache::instance($c)->get($k))) {
			$result = array();

			foreach ($this->_volumeusage() as $vuo)
				if ($vuo->COPY_TYPE == $type)
					array_push($result,$vuo);

			Sort::MASort($result,'VOLUME_NAME');
			// @todo Cache time should be configurble
			Cache::instance($c)->set($k,$result,300);
		}

		return $result;
	}

	/**
	 * Get NODES on a VOLUME
	 * @param $type is BACKUP/ARCHIVE/SPACE MANAGED
	 */
	public function nodes_byctype($type) {
		$k = sprintf('%s-%s-%s',__METHOD__,$this->VOLUME_NAME,$type);
		$c = Kohana::$config->load('config')->cache;

		if (TRUE OR is_null($result = Cache::instance($c)->get($k))) {
			$x = $result = array();

			foreach ($this->_volumeusage() as $vuo)
				if ($vuo->COPY_TYPE == $type AND ! in_array($vuo->NODE_NAME,$x)) {
					array_push($result,$vuo);
					array_push($x,$vuo->NODE_NAME);
				}

			// @todo Cache time should be configurble
			Cache::instance($c)->set($k,$result,300);
		}

		return $result;
	}

	public function isScratch() {
		return $this->SCRATCH === 'YES' ? TRUE : FALSE;
	}

	// Age of a volume, based on last read/write access.
	public function age() {
		if ((! $this->LAST_READ_DATE AND ! $this->LAST_WRITE_DATE) OR $this->STATUS == 'EMPTY')
			return 0;
		else
			return (int)((time()-(strtotime(strtotime($this->LAST_READ_DATE) > strtotime($this->LAST_WRITE_DATE) ? $this->LAST_WRITE_DATE : $this->LAST_READ_DATE)))/86400);
	}

	public function recycle() {
		return Kohana::$config->load('config')->tsmtapeage < $this->age() ? TRUE : FALSE;
	}
}
?>
