<?php

/**
 * @file LogEntryDAO.inc.php
 *
 * Copyright (c) 2003-2008 John Willinsky
 * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
 *
 * @class LogEntryDAO
 * @ingroup plugins_generic_counter
 *
 * @brief Operations for retrieving and modifying log entry objects.
 */

// $Id: LogEntryDAO.inc.php,v 1.8 2008/07/01 01:16:13 asmecher Exp $


require_once (dirname(__FILE__) . '/LogEntry.inc.php');

class LogEntryDAO extends DAO {
	/**
	 * Get the filename for the COUNTER plugin log
	 * @return string
	 */
	function getLogFilename() {
		return (dirname(__FILE__) . '/log.txt');
	}

	function addEntry(&$logEntry, $file = null) {
		if (!isset($file)) $file = $this->getLogFilename();

		$stamp = strtr(Core::getCurrentDate(), "\t", " ");
		$user = strtr($logEntry->getUser(), "\t", " ");
		$site = strtr($logEntry->getSite(), "\t", " ");
		$journal = strtr($logEntry->getJournal(), "\t", " ");
		$publisher = strtr($logEntry->getPublisher(), "\t", " ");
		$printIssn = strtr($logEntry->getPrintIssn(), "\t", " ");
		$onlineIssn = strtr($logEntry->getOnlineIssn(), "\t", " ");
		$type = strtr($logEntry->getType(), "\t", " ");
		$value = strtr($logEntry->getValue(), "\t", " ");
		$journalUrl = strtr($logEntry->getJournalUrl(), "\t", " ");

		$line = "$stamp\t$user\t$site\t$journal\t$publisher\t$printIssn\t$onlineIssn\t$type\t$value\t$journalUrl\n";

		$fp = fopen($file, 'a');
		if (!$fp) return false;

		if (!flock($fp, LOCK_EX)) {
			fclose($fp);
			return false;
		}

		fwrite($fp, $line);
		fclose($fp);
	}

	function &parse($file = null, $year = null) {
		if (!isset($file)) $file = $this->getLogFilename();

		$entries = array();

		$fp = fopen($file, 'r');
		if (!$fp) {
			$result = false;
			return $result;
		}

		while ($data = fgets($fp, 4096)) {
			$fragments = explode("\t", trim($data));
			if (sizeof($fragments) < 10) continue;
			list($stamp, $user, $site, $journal, $publisher, $printIssn, $onlineIssn, $type, $value, $journalUrl) = $fragments;
			$entryYear = strftime('%Y', strtotime($stamp));
			if ($year === null || $entryYear == $year) {
				$logEntry = &new LogEntry();
				$logEntry->setStamp($stamp);
				$logEntry->setUser($user);
				$logEntry->setSite($site);
				$logEntry->setJournal($journal);
				$logEntry->setPublisher($publisher);
				$logEntry->setPrintIssn($printIssn);
				$logEntry->setOnlineIssn($onlineIssn);
				$logEntry->setType($type);
				$logEntry->setValue($value);
				$logEntry->setJournalUrl($journalUrl);
				$entries[] = &$logEntry;
			}
		}

		fclose ($fp);
		return $entries;
	}

	function clearLog($file = null) {
		if (!isset($file)) $file = $this->getLogFilename();

		$fp = fopen($file, 'w');
		if (!$fp) return false;

		if (!flock($fp, LOCK_EX)) {
			fclose($fp);
			return false;
		}

		fclose($fp);
	}
}

?>
