<?php

// Book: The offset from 0 at the beginning of time
// Page: The offset from 0 at the beginning of the volume

class Strip
{
	public $id;
	public $old_id;
	public $published;
	public $media;
	public $type;
	public $title;
	public $book;
	public $page;
}

// old_id is used to detect alterations to the strip id in forms. Not saved in database.
// media is imagetype

// Strip id is automatically incremented
function insertstrip(&$strip) {
	global $dbConnection;

	$strip->book = ($strip->book == '') ? NULL : (int)$strip->book;
	$strip->page = ($strip->page == '') ? NULL : (int)$strip->page;

	$dbConnection->beginTransaction();
	$newid = $dbConnection->fetchColumn('SELECT MAX(id) FROM strip') + 1;

	$sql = 'INSERT INTO strip (id, published, media, type, title, book, page) VALUES(?, FROM_UNIXTIME(?), ?, ?, ?, ?, ?)';
	$stmt = $dbConnection->prepare($sql);

	$stmt->bindValue(1, $newid);
	$stmt->bindValue(2, $strip->published, PDO::PARAM_INT);
	$stmt->bindValue(3, $strip->media, PDO::PARAM_INT);
	$stmt->bindValue(4, $strip->type, PDO::PARAM_INT);
	$stmt->bindValue(5, trim($strip->title));
	$stmt->bindValue(6, $strip->book);
	$stmt->bindValue(7, $strip->page);

	$r = $stmt->execute();
	if( !$r ) {
		$dbConnection->rollback();
		return false;
	}
	$dbConnection->commit();
	adminlog("Comic ".$newid." posted.", MTS_STRIP, MTA_ADD);

	$strip->id = $newid;
	if( $strip->id == 0 ) return false;
	return true;
}

function updatestrip(&$strip) {
	global $dbConnection;

	$strip->book = ($strip->book === '') ? 'NULL' : (int)$strip->book;
	$strip->page = ($strip->page === '') ? 'NULL' : (int)$strip->page;

	$dbConnection->beginTransaction();

	$sql = 'UPDATE strip SET published = FROM_UNIXTIME(?), media = ?, type = ?, title = ?, book = ?, page = ? WHERE id = ?';
	$stmt = $dbConnection->prepare($sql);

	$stmt->bindValue(1, $strip->published, PDO::PARAM_INT);
	$stmt->bindValue(2, $strip->media, PDO::PARAM_INT);
	$stmt->bindValue(3, $strip->type, PDO::PARAM_INT);
	$stmt->bindValue(4, trim($strip->title));
	$stmt->bindValue(5, $strip->book, PDO::PARAM_INT);
	$stmt->bindValue(6, $strip->page, PDO::PARAM_INT);
	$stmt->bindValue(7, $strip->id, PDO::PARAM_INT);

	$stmt->execute();
	$dbConnection->commit();
	adminlog("Comic ".$strip->id." modified.", MTS_STRIP, MTA_MODIFY);
	return true;
}

// Delete destination strip from DB and FS, and Update/Rename the source strip into place. Destructive Move!
function move_strip($from_id, $to_id)
{
	global $dbConnection;
	$from_id = (int) $from_id;
	$to_id = (int) $to_id;

	// Ensure our source exists
	$num_strips = $dbConnection->fetchColumn('SELECT COUNT(*) FROM strip WHERE id = ?', array($from_id));
	if($num_strips < 1)
		mtdie("Cannot move strip number $from_id, because it cannot be found in database.");

	// Ready the destination
	deletestrip( $to_id );

	// Update database
	$dbConnection->executeUpdate('UPDATE strip SET id = ? WHERE id = ?', array($to_id, $from_id));
	$strip = $dbConnection->executeQuery('SELECT strip.id, extension FROM strip, media_t WHERE media_t.id = strip.media AND strip.id = ?', array($to_id))->fetch();

	// Update filesystem
	foreach(glob(sprintf(SITE_PATH_ABS.'/'.SITE_STRIP.'/%04d.*', $from_id)) as $item) {
		preg_match('/\.(\w{3})$/', $item, $match) or die("Invalid filename: $item");
		rename($item, sprintf(SITE_PATH_ABS.'/'.SITE_STRIP.'/%04d.%s', $to_id, $match[1]));
	}
}

// Classic swap function, using strip 0 as temporary storage. Can cause concurrency issues. FLOCK!
function swap_strips( $from_id, $to_id ) {
	move_strip($from_id, 0);
	move_strip($to_id, $from_id);
	move_strip(0, $from_id );
	adminlog("Comics ".$from_id." and ".$to_id." swapped.", MTS_STRIP, MTA_MODIFY);
}

function deletestrip($id) {
	$id = (int)$id;
	if ( !$id ) return false;

	global $dbConnection;
	$r = $dbConnection->executeUpdate('DELETE FROM strip WHERE id = ?', array($id));
	foreach(glob(sprintf(SITE_PATH_ABS.'/'.SITE_STRIP.'/%04d*.*', $id)) as $item)
		unlink($item);
	foreach(glob(sprintf(SITE_PATH_ABS.'/'.SITE_STRIP.'/restricted/%04d*.*', $id)) as $item)
		unlink($item);
	adminlog("Comic ".$id." deleted.", MTS_STRIP, MTA_DELETE);
	return $r;
}

function getstrip($id) {
	global $dbConnection;
	return $dbConnection->executeQuery('SELECT id, UNIX_TIMESTAMP(published) as published, type, media, title, book, page FROM strip WHERE id = ?', array($id))->fetch();
}

function get_stripimage_filename( $strip ) {
	global $dbConnection;
	$ext = $dbConnection->fetchColumn('SELECT extension FROM media_t WHERE id = ?', array($strip->media)); // filename extension
	return sprintf( '%s/%04d.%s', SITE_STRIP, $strip->id, $ext );
}

function get_stripid_by_rantid($rantid) {
	global $dbConnection;
	return $dbConnection->fetchColumn('SELECT MAX(strip.id) FROM strip, rant WHERE strip.published <= rant.published AND rant.id = ?', array($rantid));
}

?>
