%%document-save-member-scene%%
$Date $

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

// check somewhere if members are allowed to create scenes before you do this.
if ($session->canSave['document'] || $session->canSave['all']) {
	if ($GLOBALS['sitemember']->role=="member") {
		$session->insertstatus= "posted";
		$session->insertpublic= 1;
		$session->publicwhere= "";
		$session->updatestatus= "posted";
		$session->updatepublic= "1";
		$session->updaterank= "100";
		$GLOBALS[session]= $session;
		}
	}
else {
	berror("Current policy here does not allow you to save Scenes. Unable to continue.",0);
	ext;
	}
	
// check somewhere if members are allowed to create frames before you do this.
if (!$session->canSave['comment'] && !$session->canSave['all']) {
	berror("Hmm, you can save Scenes, but current policy does not allow you to save Frames. Unable to continue.",0);
	exit;
	}
	
// is this a new Scene?
if ($this->id=="") $this->newScene= 1;

// make sure there is a sitemember->p_characterId
if ($this->newScene && !is_numeric($sitemember->p_characterId) || $sitemember->p_characterId<1) {
	berror("You seem to not have a Character to star in this new Scene. It would be unwise to continue.",0);
	exit;
	}

// set flavor and name
$this->flavor= "Scene";
if (trim($this->name)=="") {
	// generate a unique name
	$this->name= date("Ymd").".".substr(uniqid(""),-4);
	}
berror("Document Name is $this->name.",1);

//subscribe author automatically
$this->p_subscriptionList= $sitemember->id;

// generate automagic keywords for searching later...
$ignorewords= " the , of , and , a , to , in , is , you , that , it , he , she , for , was , on , are , as , with , his , her , they , at , be , this , from , i , have , or , by , one , had , not , but , all , were , there , can , an , your , which , their , said , if , do , so , these , has , than , did , those , 1 , 2 , 3 , 4 , 5 , 6 ";
$igwordsarray= explode(",", $ignorewords);
$ignorepunctuation= ".,?,!,:,;,[,]";
$igpuncarray= explode(",", $ignorepunctuation);
$igpuncarray[]= ",";
$this->keywords= str_replace($igwordsarray, " ", str_replace($igpuncarray, " ", strtolower($this->description)));
$this->keywords= str_replace("  ", " ", $this->keywords);


%%header%%

%%css%%

%%template%%
<table width='90%' border='0' cellspacing='20' cellpadding='5' align='center'>
<tr> <td> <p><b>UPDATED! $this->objtype object id#$this->id ($this->name):</b></p>
<p>$this->propertiesList</p>
<p><a href='$session->refreshURL'>refresh</a> | <a href='$this->baseURL?method=edit$session->cgi'>edit</a> | <a href='$this->baseURL?method=create$session->cgi'>create new</a> | <a href='$this->baseURL?method=addchild$session->cgi'>add child</a> | <a href='#top'>top</a></p>
</td>
</tr>
</table>


%%listrow%%

%%nullobject%%
FWAHH.

%%footer%%

%%postprocess%%
// If this is a new scene, add the first frame, along with the sitemember's p_characterId character
if ($this->newScene) {
	berror("<font color='red'>Adding a Frame</font> to this new Scene...",1);
	$frame= new Comment;

	// set flavor and name and stuff
	$frame->flavor= "Frame";
	if (trim($frame->name)=="") {
		// generate a unique name
		$frame->name= date("Ymd").".".substr(uniqid(""),-4);
		}
	berror("Comment Name is $frame->name.",1);
	$frame->parentobjtype= "document";
	$frame->parentid= $this->id;
	$frame->status= "posted";
	$frame->rank= 100;
	$frame->public= 1;

	// add character... take the supplied id as a document:id
	$newobject= "document:$sitemember->p_characterId";
	$colonpos= 8;
	$positionz= 50;
	$frame->newObjtype= "document";
	$frame->newId= $sitemember->p_characterId;
	berror("addObject Preprocess: going to add $frame->newObjtype #$frame->newId to frame $frame->id with body=$frame->body. Parent of frame is $frame->parentobjtype:$frame->parentid.",1);

	// look up this alleged character
	$myObject= new Document;
	$myObjectQuery= "SELECT obj.* FROM document AS obj WHERE id='$frame->newId' AND obj.siteid='$site->id' $session->statuswhere $session->publicwhere";
	$myObject->selectObject($myObjectQuery);

	if ($myObject->flavor=="Character") {
		// find default pose...
		$pose= new Image;
		$query= "SELECT obj.* FROM image AS obj WHERE obj.siteid='$site->id' AND parentobjtype='document' AND parentid='$frame->newId' $session->statuswhere $session->publicwhere ORDER BY rank DESC, created ASC";
		$pose->selectObject($query);
		$frame->poseid= $pose->id;
		$frame->imagetag= bparsebml("[icon:$frame->poseid]");
		$posetag= "^isCharacter=1^poseid=$frame->poseid";
		berror("comment-addObject: found default poseid=$pose->id for Character $frame->newId",1);
		}
	elseif ($myObject->imageid!="0") {
		$frame->poseid= $myObject->imageid;
		$frame->imagetag= bparsebml("[icon:$frame->poseid]");
		$posetag= "^poseid=$frame->poseid";
		}

	// record looks like objtype=document^id=67^position_x=200^position_y=200^position_z=5^dialogue=Ooooohh!^poseid=34^^
	if (substr($frame->body,-2)!="^^") {
		$frame->body.="^^";
		}
	$frame->body.= "objtype=$frame->newObjtype^id=$frame->newId^position_x=100^position_y=100^position_z=$positionz$posetag$labeltag^^";
	berror("addObject Preprocess: new body is $frame->body",1);
	$frame->insertObject();
	}


%%end of context%%
