%%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;
	$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();

%%header%%

%%css%%

%%template%%

%%listrow%%

%%nullobject%%
Created Character $this->name with id=$this->id. $this->imageTag
This character is now assigned to you, $sitemember->name.<br/>
<a href="$_GET[return]">Click here to continue with your new scene.</a>

%%footer%%

%%postprocess%%

%%end of context%%
