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

/**
 * TSM database show result.   See [Results](/database/results) for usage and examples.
 *
 * @package    PTA
 * @subpackage TSM
 * @category   Query/Show
 * @author     Deon George
 * @copyright  (c) 2010 phpTSMadmin Development Team
 * @license    http://phptsmadmin.sf.net/license.html
 */
class Database_TSM_Show extends Database_Result {
	private $data = array();

	public function __construct($result, $sql, $as_object = FALSE, array $params = NULL) {
		parent::__construct($result, $sql, $as_object, $params);

		$sql = strtolower($sql);

		$barcodewarn = FALSE;
		$eid = $sid = FALSE;
		$ec = 0;

		foreach ($result as $line) {
			if (! trim($line))
				continue;

			if (preg_match('/^show slots /',$sql)) {
				$library = strtoupper(preg_replace('/^show slots /','',$sql));
			
				if (preg_match('/^Slot /',$line)) {
					$slot = array();
					foreach ((preg_split('/,\s*/',$line,-1)) as $slotkey => $val)
						if (preg_match('/^element number\s+/',$val))
							$slot['element'] = (int)preg_replace('/^element number\s+/','',$val);
						elseif (preg_match('/^Slot\s+/',$val))
							$slot['slot'] = preg_replace('/^Slot\s+/','',$val);
						elseif (preg_match('/^status\s+/',$val))
							$slot['status'] = preg_replace('/^status\s+/','',$val);
						elseif (preg_match('/^barcode value </',$val))
							$slot['barcodelabel'] = preg_replace('/^barcode value <(.*)>/',"$1",$val);
						elseif (preg_match('/^barcode\s+/',$val))
							$slot['barcode'] = preg_replace('/^barcode /','',$val);

					// We assume that the element numbers are sequential
					// @todo This routine would miss the first empty slots, there is no way to work this out?
					if ($eid AND $eid+1 != $slot['element']) {
						while ($eid+1 != $slot['element']) {
							if ($ec++ > $this->Slots OR $sid == $slot['slot'])
								throw new Kohana_Exception('Had a problem calculating EMPTY slots (:ec, :slots, :sid, :slot)',
									array(':ec'=>$ec,':slots'=>$this->Slots,':sid'=>$sid,':slot'=>$slot['slot'])
								);

							$eid++;
							$sid++;
							$this->_rows[$this->_internal_row++] = new Slot(array(
								'element'=>"$eid",
								'slot'=>"$sid",
								'status'=>'Empty',
								'barcodelabel'=>'',
								'barcode'=>'',
							));
						}
					}
					
					$slot['library'] = $library;
					$this->_rows[$this->_internal_row++] = new Slot($slot);

					// Counters to keep track of empty slots
					$eid = $slot['element'];
					$sid = $slot['slot'];
					$ec++;

					if (! $barcodewarn AND $slot['status'] == 'Allocated' AND $slot['barcode'] == 'not present') {
						SystemMessage::add(array(
							'title'=>_('Missing Bar Code Labels'), 
							'type'=>'warning',
							'body'=>sprintf(_('Some allocated slots do not have a bar code label, you may need to run "AUDIT LIBRARY %s CHECKLABEL=BARCODE"'),$library),
						));

						$barcodewarn = TRUE;
					}

				} elseif (preg_match('/busy.$/',$line)) {
					SystemMessage::add(array(
						'title'=>_('Library is Busy'), 
						'type'=>'info',
						'body'=>_('The library appears busy at the moment.'),
					));

					return;

				} elseif (preg_match('/:/',$line)) {
					$line = str_replace(' ','',$line);
					list($k,$v) = explode(':',$line,2);

					$this->data[$k] = $v;
				}
			}
		}

		$this->_total_rows = $this->_internal_row;
		$this->_internal_row = 0;
	}

	public function __get($key) {
		if (isset($this->data[$key]))
			return $this->data[$key];
		else
			return NULL;
	}
}
?>
