<?php

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

class Strip {
	var $id, $old_id, $published, $media, $type, $title, $book, $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 $mtdb;

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

	$mtdb->query('START TRANSACTION');
	$newid = $mtdb->getOne('SELECT MAX(id) FROM strip') + 1;
	$sql = 'INSERT INTO strip ( id, published, media, type, title, book, page ) VALUES ('
		. $newid
		. ', FROM_UNIXTIME(' . (int)$strip->published
		. '), '. (int)$strip->media
		. ', ' . (int)$strip->type
		. ', "' . mysqli_real_escape_string( $mtdb->link, trim($strip->title) )
		. '", '. $strip->book
		. ', ' . $strip->page
		. ')';

	$r = $mtdb->query( $sql );
	if( !$r ) {
		$mtdb->query('ROLLBACK');
		return false;
	}
	$mtdb->query('COMMIT');
	adminlog("Comic ".$newid." posted.", MTS_STRIP, MTA_ADD);

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

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

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

	$mtdb->query('START TRANSACTION');
	$sql = 'UPDATE strip SET
		published = FROM_UNIXTIME(' . (int)$strip->published .')
		, media = '. (int)$strip->media .'
		, type = ' . (int)$strip->type .'
		, title = "' . mysqli_real_escape_string( $mtdb->link, trim($strip->title) ) .'"
		, book = ' . (int)$strip->book .'
		, page = ' . (int)$strip->page .'
		WHERE id = ' . (int)$strip->id;
	$mtdb->query( $sql );
	$mtdb->query('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 $mtdb;
	$from_id = (int) $from_id;
	$to_id = (int) $to_id;

	// Ensure our source exists
	$num_strips = $mtdb->getOne( "SELECT COUNT(*) FROM strip WHERE id = $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
	$mtdb->query( "UPDATE strip SET id = $to_id WHERE id = $from_id" );
	$strip = $mtdb->getRow( "SELECT strip.id, extension FROM strip, media_t WHERE media_t.id = strip.media AND strip.id = $to_id" );

	// 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 $mtdb;
	$r = $mtdb->query( 'DELETE FROM strip WHERE id=' . $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 $mtdb;
	return $mtdb->getRow( 'SELECT id, UNIX_TIMESTAMP(published) as published, type, media, title, book, page FROM strip WHERE id=' . (int)$id);
}

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

function get_stripid_by_rantid($rantid) {
	global $mtdb;
	return $mtdb->getOne('SELECT MAX(strip.id) FROM strip,rant WHERE strip.published<=rant.published AND rant.id=' . (int)$rantid);
}

?>
