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

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

	// Store our show slots data
	private $slots;
	private $storagepools = array();

	protected $_has_many = array(
		'DRIVE'=>array('foreign_key'=>'LIBRARY_NAME','far_key'=>'LIBRARY_NAME'),
		'PATH'=>array('foreign_key'=>'DESTINATION_NAME','far_key'=>'LIBRARY_NAME'),
		'DEVCLASSES'=>array('foreign_key'=>'LIBRARY_NAME','far_key'=>'LIBRARY_NAME'),
	);

#zz
	public function slots() {
		return $this->slots ? $this->slots : $this->slots = DB::query(Database::SHOW,'SHOW SLOTS '.$this)->execute(Kohana::$config->load('config')->client_type);
	}

	// Return a list of scratch volumes
	public function scratchvol() {
		$return = array();

		foreach ($this->slots() as $slot)
			if ($slot->status == 'Allocated' AND $slot->LIBVOLUME->usage() == 'scratch')
				array_push($return,$slot->LIBVOLUME->VOLUME);

		return $return;
	}

	// Return a list of volumes that are readonly
	public function readonlyvol() {
		$return = array();

		foreach ($this->slots() as $slot)
			if ($slot->LIBVOLUME->VOLUME->ACCESS == 'READONLY')
				array_push($return,$slot->LIBVOLUME->VOLUME);

		return $return;
	}

	// Return the number of slots that are empty.
	public function numemptyslot() {
		$return = array();

		foreach ($this->slots() as $slot)
			if ($slot->status == 'Empty')
				array_push($return,$slot->LIBVOLUME->VOLUME);

		return count($return);
		//return $this->slots->Slots-$this->slots->Changers-count($this->slots);
	}

	// Return the slots that are used, but not checked in.
	public function notcheckedinvol() {
		$return = array();

		foreach ($this->slots() as $slot)
			if ($slot->status == 'Full')
				array_push($return,$slot);

		return $return;
	}

	// Return the device classes that use this library.
	public function devclasses() {
		return $this->DEVCLASSES->where('LIBRARY_NAME','=',$this)->find_all();
	}

	// Return a list of storage pools that potentially use this library.
	public function storagepools() {
		if (! $this->storagepools)
			foreach ($this->devclasses() as $dco)
				foreach ($dco->STGPOOL->find_all() as $spo)
					array_push($this->storagepools,$spo);

		return $this->storagepools;
	}

	public function storagepoolstype($type) {
		$result = array();

		foreach ($this->storagepools() as $spo)
			if ($spo->POOLTYPE == $type)
				array_push($result,$spo);

		return $result;
	}

	/**
	 * Return a list of volumes
	 *
	 * @param $ptype is pool type (PRIMARY,ACTIVE,COPY)
	 * @param $inout IN|OUT of the library
	 * @param $status volume status FULL|FILLING|PENDING|EMPTY
	 * @note This is an intensive method that needs caching.
	 */
	public function volstype($ptype,$inout,$status) {
		static $CACHE = array();
		$ainout = array('IN','OUT');

		if (! isset($CACHE[__METHOD__][$ptype]))
			foreach ($this->storagepoolstype($ptype) as $spo)
				foreach ($ainout as $cinout)
					foreach (Kohana::$config->load('config')->tsmvolstatus as $cstatus) {
						if (! isset($CACHE[__METHOD__][$spo->POOLTYPE][$cinout][$cstatus]))
							$CACHE[__METHOD__][$spo->POOLTYPE][$cinout][$cstatus] = array();

						$CACHE[__METHOD__][$spo->POOLTYPE][$cinout][$cstatus] =
							array_merge($CACHE[__METHOD__][$spo->POOLTYPE][$cinout][$cstatus],$spo->libvolstype($cinout,$cstatus));
					}

		return isset($CACHE[__METHOD__][$ptype][$inout][$status]) ? $CACHE[__METHOD__][$ptype][$inout][$status] : array();
	}

	/**
	 * Return a list of DB volumes
	 *
	 * @param $inout IN|OUT of the library
	 * @note This is an intensive method that needs caching.
	 */
	public function dbvolsloc($inout) {
		static $CACHE = array();

		if (! isset($CACHE[__METHOD__]))
			foreach (ORM::factory('VOLHISTORY')->where('TYPE','IN',Kohana::$config->load('config')->tsmdbtypes)->find_all() as $vho)
				$CACHE[__METHOD__][$vho->LIBVOLUME->LIBRARY_NAME ? 'IN' : 'OUT'][] = $vho;

		return isset($CACHE[__METHOD__][$inout]) ? $CACHE[__METHOD__][$inout] : array();
	}

	// Return a list of DB volumes by type
	// @param $inout IN|OUT of the library
	// @param $dtype is DB vole type (BACKUPFULL,BACKUPINCR,DBSNAPSHOT)
	public function dbvolstype($inout,$dtype) {
		$result = array();

		foreach ($this->dbvolsloc($inout) as $vho)
			if ($vho->TYPE ==$dtype)
				array_push($result,$vho);

		return $result;
	}

	public function volsnotinlib() {
		$result = array();

		foreach ($this->storagepools() as $spo)
			foreach ($spo->VOLUME->find_all() as $vo)
				if (! $vo->MEDIA->inlib())
					array_push($result,$vo);

		Sort::masort($result,'VOLUME_NAME');
		return $result;
	}

	// Volumes that are stale
	public function stalevol() {
		$result = array();

		foreach ($this->storagepools() as $spo)
			foreach ($spo->VOLUME->find_all() as $vo)
				if ($vo->recycle())
					array_push($result,$vo);

		Sort::masort($result,'VOLUME_NAME');
		return $result;
	}

	public function removablelibvol() {
		$result = array();

		foreach ($this->slots() as $slot)
			if ($slot->LIBVOLUME->removable())
				array_push($result,$slot->LIBVOLUME);

		return $result;
	}
}
?>