%%document-stage1save-member-scene%%
$Date $

%%preprocess%%
// elevate member policy
berror("Current role is $sitemember->role",1);

if ($this->status=="") {
	$this->status="posted";
	$this->public=1;
	$this->rank=100;
	}

// members are allowed to create documents here.
if ($sitemember->role=="member") {
	$session->insertstatus= "posted";
	$session->insertpublic= 1;
	$session->publicwhere= "";
	$session->updatestatus= "posted";
	$session->updatepublic= "1";
	$session->updaterank= "100";
	$session->canSave['document']= 1;
	$session->canSave['image']= 1;
	$GLOBALS['session']= $session;
	}
elseif ($sitemember->role!="editor" && $sitemember->role!="admin") {
	berror("Current policy does not allow you to save documents.",0);
	}

// register title
$this->title= $_POST['title'];

// generate a title-based unique name
if (trim($this->title)=="") {
	$this->name= date("Ymd").".".substr(uniqid(""),-4);
	$this->title= "$sitemember->name's Character";
	}
else {
	$this->name= urlencode(strtolower(str_replace(" ","-",$this->title)));
	$this->name= preg_replace("/\%\d\d/","",$this->name);
	$this->name= substr($this->name,0,12)."-".substr(uniqid(""),-4);
	}
berror("Document Name is $this->name.",1);

// register other important vars
$this->id= "";
$this->flavor= "Character";
$this->status= "new";

// save the Character document
berror("Saving Character $this->name now.",1);
$this->insertObject();
if ($this->id!="") {
	berror("Saved $this->name with id=$this->id!",1);
	}
else {
	berror("<b>Couldn't save</b> Character $this->name. Cannot continue.",0);
	}

// set up a default pose
$pose= new Image;
$pose->name= $this->name;
$pose->title= $this->title;
$pose->flavor= "Pose";
$pose->parentobjtype= "document";
$pose->parentid= $this->id;
$pose->status= "posted";
$pose->rank= 100;
$pose->public= 1;

// set maxsize to 30KB
$pose->maxsize= 30720;

// save the Pose
berror("Saving Pose $pose->name now.",1);
$pose->insertObject();
if ($pose->id!="") {
	berror("Saved $pose->name with id=$pose->id!",1);
	}
else {
	berror("<b>Couldn't save</b> Pose $pose->name. Cannot continue.",0);
	}
	
// check for upload and determine type
$posto= $_FILES['imagefile'];
if ($posto['size']!="") $pose->useupload= "[bold:Uploading file] ($posto[size] bytes).";
else $pose->useupload=0;

$postp= $_POST['imageurl'];
if ($postp) $pose->useimport= "[bold:Importing file] ($postp).";
else $pose->useimport=0;
	
// import or upload...
if ($pose->useupload) {
	$uploaded= $pose->upload();
	if ($uploaded) {
		berror("Image->save(): file uploaded and saved at $pose->path ($pose->uri), continuing with db update.",1);
		$pose->updateObject();
		}
	}
elseif ($pose->useimport) {
	$imported= $pose->import();
	if ($imported) {
		berror("Image->save(): file imported and saved at $pose->path ($pose->uri), continuing with db update.",1);
		$pose->updateObject();
		}
	}
	
// render image for confirmation
$this->imageTag= bparsebml("[image:$pose->id;center]");

// make this the sitemember's primary character
$sitemember->p_characterId= $this->id;
$sitemember->updateObject();

// everything went okay, change this->status to posted
berror("<font color='green'>GOOD!</font> Changing status of $this->name from $this->status to posted.",1);
$this->status= "posted";
$this->updateObject();

// what room is behind this?
if ($_GET['return']) {
	$urlarray= parse_url($_GET['return']);
	$dirname= dirname($urlarray['path']);
	$roomname= substr($dirname, 9);
	berror("Room name seems to be $roomname.",1);

	// look up folder with name=$roomname and see if it's a room, etc.
	$room= new Folder;
	$roomquery= "SELECT obj.* FROM folder AS obj WHERE obj.name='$roomname' $statuswhere $publicwhere ";
	$room->selectObject($roomquery);
	if ($room->p_background) {
		// ROOM SETTINGS
		berror("Getting imagesize on $room->p_background now...",1);
		if ($background= getimagesize($room->p_background)) {
			berror("Got it.",1);
			$width= $background[0];
			$height= $background[1];

			$targetwidth= 630;
			$targetheight= 430;

			if ($width>$targetwidth) {
				//$height= $height * ($targetwidth/$width);
				$width= $targetwidth;
				}
			if ($height>$targetheight) {
				//$width= $width * ($targetheight/$height);
				$height= $targetheight;
				}

			$this->backgroundwidth= $width."px";
			$this->bgnumericwidth= $width;
			$this->backgroundheight= $height."px";
			$this->bgnumericheight= $height;
			$this->background= $room->p_background;
			}

		// SCENEINFO COLOR
		if ($room->p_infoColor!="") $this->infoColor= $room->p_infoColor;
		else $this->infoColor= "white";

		// INFO POSITION
		$bgcenter= $this->bgnumericwidth / 2;
		$this->infoLeft= ($bgcenter - 200)."px";
		$bgmiddle= $this->bgnumericheight / 2;
		$this->infoTop= ($bgmiddle - 75)."px";
		}
	}
	
if ($this->infoLeft=="") $this->infoLeft= 50;
if ($this->infoTop=="") $this->infoTop= 200;
$this->charLeft= $this->infoLeft + 285;
$this->charTop= $this->infoTop - 75;

%%header%%

%%css%%
.boothtable	{ background-color: #f9f9fb;
			border: 3px; border-color: #0e47b2; border-style: solid;
			color: #223344;
			font-family: Tahoma, Geneva, sans-serif; font-size: 12px; line-height: 18px;
			width: $this->backgroundwidth;
			}
.boothtable A { color: #135b9f; }
.frameInfo { font-family: Arial, Helvetica, sans-serif; font-size: 10px; line-height: 14px; color: $this->infoColor; text-align: center; }
.frameInfo A { color: $this->infoColor; font-weight: bold; }

%%template%%

%%listrow%%

%%nullobject%%
<a name="top"> </a>
<img src="/bicons/spacer.gif" width="$this->bgnumericwidth" height="$this->bgnumericheight" border=0>
<div id='background' style='position: absolute; left: 0px; top: 0px; width: $this->backgroundwidth; height: $this->backgroundheight; z-index:1; background-color: #333333; background-image: url($this->background); background-position: center center;'>
</div>
<div id='char' style='position: absolute; z-index: 5; left: $this->charLeft; top: $this->charTop;' >
$this->imageTag
</div>
<div id='frameInfo' class='boothTable' style='position: absolute; width: 300px; height: 150px; z-index: 1; left: $this->infoLeft; top: $this->infoTop;' >
	<table class='editTable'>
		<tr>
			<td><span class='objTitle'>Created Character $this->name</span><br/>(with id=$this->id)
				<p>&nbsp;</p>
				<p>This character is now assigned to you, $sitemember->name.</p>
				<p align='center'><a href="$_GET[return]">Click here to continue with your new scene.</a></p>
			</td>
		</tr>
	</table>
</div>

%%footer%%

%%postprocess%%

%%end of context%%
