<?php
/**
 * @copyright  2012 Roberto Perpuly
 *
 * This source may not be redistributed without prior consent.  Access to the 
 * source does not imply that you have consent to redistribute either this source
 * code or the generated binary. See LICENSE file for details.
 */

class Triumph_ResourceTable extends Zend_Db_Table_Abstract {

	protected $_name = 'resources';
	
	protected $_primary = array('file_tag_id', 'key');
	
	/**
	 * Retrieves all of the methods from the given files. Only resources that 
	 * are methods will be returned. Only public methods are returned.
	 *
	 * @param Zend_Db_Adapter_Abstract $db the DB to query
	 * @param Triumph_FileItem[] $fileItems the file item IDs to query
	 * @return Triumph_Resource[] the resources that were found in any of the given files.
	 */
	public function PublicMethodsFromFiles($fileItems) {
		if (empty($fileItems)) {
			return array();
		}
		$fileItemIds = array();
		foreach ($fileItems as $fileItem) {
			$fileItemIds []= $fileItem->file_item_id;
		}
				
		// key = identifier ==> don't get the fully qualified matches
		$select = $this->_db->select();
		$select->from(array('r' => 'resources'), array('class_name', 'identifier', 'signature', 'return_type', 'comment'))
			->join(array('f' => 'file_tags'), 'r.file_tag_id = f.file_tag_id', array('full_path'));
		$select->where("r.file_item_id IN(?)", $fileItemIds)
			->where('key = identifier')
			->where('type = ?', Triumph_Resource::TYPE_METHOD)
			->where('is_private = 0')
			->where('is_protected = 0');
		$stmt = $select->query();
		$methods = array();
		while ($row = $stmt->fetch(Zend_Db::FETCH_ASSOC)) {
			$method = new Triumph_Resource();
			$method->MakeMethod($row['class_name'], $row['identifier'], $row['signature'], $row['return_type'], $row['comment']);
			$method->fullPath = $row['full_path'];
			$methods[] = $method;
		}
		return $methods;
 	}
	
	public function FindPublicMethod($className, $methodName, $sourceDir) {
		$select = $this->_db->select();
		$select->from(array('r' => 'resources'), array('class_name', 'identifier', 'signature', 'return_type', 'comment'))
			->join(array('f' => 'file_tags'), 'r.file_tag_id = f.file_tag_id', array('full_path'))
			->join(array('fts' => 'file_tags_sources'), 'f.file_tag_id = fts.file_tag_id', array('source_id'))
			->join(array('s' => 'sources'), 's.source_id = r.source_id', array('directory'))
		;
		$select->where("s.directory = ?", $sourceDir)
			->where('key = ?', $className . '::' . $methodName)
			->where('type = ?', Triumph_Resource::TYPE_METHOD)
			->where('is_private = 0')
			->where('is_protected = 0');
		$stmt = $select->query();
		$methods = array();
		while ($row = $stmt->fetch(Zend_Db::FETCH_ASSOC)) {
			$method = new Triumph_Resource();
			$method->MakeMethod($row['class_name'], $row['identifier'], $row['signature'], $row['return_type'], $row['comment']);
			$method->fullPath = $row['full_path'];
			$methods[] = $method;
		}
		return $methods;
	}
	
	public function FindPublicMethodsMembersForClass($className, $sourceDir) {
		$select = $this->_db->select();
		$select->from(array('r' => 'resources'), array('class_name', 'identifier', 'signature', 'return_type', 'comment'))
			->join(array('f' => 'file_tags'), 'r.file_tag_id = f.file_tag_id', array('full_path'))
			->join(array('fts' => 'file_tags_sources'), 'f.file_tag_id = fts.file_tag_id', array('source_id'))
			->join(array('s' => 'sources'), 's.source_id = r.source_id', array('directory'))
		;
		$select->where("s.directory = ?", $sourceDir)
			->where('key >= ?', $className . '::')
			->where('key <= ?', $className  . '::zzzzzz') 
			->where('type IN(?, ?)', Triumph_Resource::TYPE_METHOD, Triumph_Resource::TYPE_MEMBER)
			->where('is_private = 0')
			->where('is_protected = 0');
		$stmt = $select->query();
		$methods = array();
		while ($row = $stmt->fetch(Zend_Db::FETCH_ASSOC)) {
			$method = new Triumph_Resource();
			$method->MakeMethod($row['class_name'], $row['identifier'], $row['signature'], $row['return_type'], $row['comment']);
			$method->fullPath = $row['full_path'];
			$methods[] = $method;
		}
		return $methods;
	}
	
}
