<?php
/**
 * @copyright  2013 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_TemplateFileTagTable extends Zend_Db_Table_Abstract {

	protected $_name = 'template_file_tags';
	
	public function saveTemplateFiles($templateFiles, $sourceDir) {
		
		// delete the old rows
		$sourceDbTable = new Triumph_SourceTable($this->getAdapter());
		$sourceId = $sourceDbTable->getOrSave($sourceDir);
		$strWhere = $this->getAdapter()->quoteInto("source_id = ?", $sourceId);
		$this->delete($strWhere);
		
		if (!is_array($templateFiles)) {
			return;
		}
		
		// sqlite optimizes transactions really well; use transaction so that the inserts are faster
		$this->getAdapter()->beginTransaction();
		
		foreach ($templateFiles as $templateFile) {
			$variables = join(',', $templateFile->variables);
			$this->insert(array(
				'source_id' => $sourceId,
				'full_path' => $templateFile->fullPath,
				'variables' => $variables
			));
		}
		$this->getAdapter()->commit();
	}

}