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

/**
 *
 * @package    PTA
 * @subpackage File Spaces
 * @category   Models
 * @author     Deon George
 * @copyright  (c) 2010 phpTSMadmin Development Team
 * @license    http://phptsmadmin.sf.net/license.html
 */
class Model_FILESPACE extends TSM_ORM {
	protected $_table_name = 'FILESPACES';
	protected $_primary_key = 'FILESPACE_NAME'; // We need a primary key to detect that the object is loaded.
	protected $_sorting = array(
		'NODE_NAME'=>'ASC',
		'FILESPACE_NAME'=>'ASC',
	);

	protected $_has_one = array(
		'NODE'=>array('foreign_key'=>'NODE_NAME','far_key'=>'NODE_NAME'),
	);

	protected $_tsm = array(
		'db2'=>array(
			'_primary_key'=>'FSNAME',
			'_sorting'=>array(
				'NODEID'=>'ASC',
				'FSNAME'=>'ASC',
			),
			'_has_one'=>array(
				'NODE'=>array('foreign_key'=>'NODEID','far_key'=>'NODEID'),
			),
			'translate'=>array(
				'FILESPACE_NAME'=>'FSNAME',
				'BACKUP_END'=>'BACKSTART',
				'PCT_UTIL'=>NULL,
			),
		),
	);

	protected $_display_filters = array(
		'BACKUP_END'=>array(
			array('TSM_ORM::date',array(':value','d-M-Y')),
		),
	);

	public function utilsation() {
		return $this->CAPACITY*($this->PCT_UTIL/100);
	} 

	// $dtype must be Bkup or Arch
	public function storagepools($dtype) {
		$pool = array();

		foreach ($this->NODE->OCC
			->select('STGPOOL_NAME')
			->where('TYPE','=',$dtype)
			->and_where('FILESPACE_NAME','=',$this->FILESPACE_NAME)
			->group_by('STGPOOL_NAME')
			->order_by('STGPOOL_NAME')
			->find_all() as $oo)

			array_push($pool,$oo->STGPOOL);

		return $pool;
	}

	public function pool_logical_util($pool,$btype) {
		return $this->NODE->OCC
			->where('STGPOOL_NAME','=',$pool)
			->and_where('TYPE','=',$btype)
			->and_where('FILESPACE_NAME','=',$this->FILESPACE_NAME)
			->find()->LOGICAL_MB;
	}

	public function pool_numvols($pool,$ctype) {
		return $this->NODE->VOLUMEUSAGE
			->where('STGPOOL_NAME','=',$pool)
			->and_where('COPY_TYPE','=',$ctype)
			->and_where('FILESPACE_NAME','=',$this->FILESPACE_NAME)
			->find_all()->count();
	}
}
?>
