%%uplink-chooseBlog-member-popup%%
$Date $

%%preprocess%%
if ($_POST['delete']=="delete") {
	$this->status= "deleted";
	$this->applyPolicy();
	$this->updateObject();
	
	$redirect= $_POST['return'];
	bredirect($redirect);
	}

$this->beryliumToForm();
$this->getRuntimeVars();

$this->return= $_POST['return'];

// collect vars from document-uplinkSelect or uplink-edit
$this->blogusername= $_POST['remoteuser'];
$this->blogpassword= $_POST['remotepass'];
$this->blogurl= $_POST['publishurl'];

if ($this->blogusername=="" || $this->blogpassword=="" || $this->blogurl=="") {
	berror( "<p style='color: red; margin-top: 10px; text-align: center;'>Please fill out all three fields to continue.</p>",0);
	}

// get blog list
include_once("$GLOBALS[beryliumroot]/external/xmlrpcutils.php");

// parse blogurl
$url= parse_url($this->blogurl);
$rpchost= $url['host'];
$rpcport= $url['post'];
$rpcuri= $url['path'];
if ($url['query']!="") $rpcuri= $rpcuri.="?$url[query]";
if ($url['fragment']!="") $rpcuri= $rpcuri.="#$url[fragment]";
berror("folder-weblogSetup.2: rpchost= $rpchost | rpcport= $rpcport | rpcuri= $rpcuri",1);

// what sort of blog is it?
if  (substr($url['path'],0,10)=="/berylium/") $this->p_blogtype= "Berylium";
elseif  ($url['path']=="/interface/xmlrpc") $this->p_blogtype= "LiveJournal";
else $this->p_blogtype= "Standard";

// BERYLIUM APPKEY
$appkey= "95F16C6922351F7AAC3517B87863824A7CE14D53";

if ($this->p_blogtype!="LiveJournal") {
	// blogger.getUsersBlogs -- result is an array of blog arrays, we want blog[blogid] and blog[blogName]
	$rpcresult = xu_rpc_http_concise( array(
							'method' => "blogger.getUsersBlogs",
							'args' => array($appkey,$this->blogusername,$this->blogpassword),
							'host' => $rpchost,
							'uri' => $rpcuri,
							'port' => $rpcport,
							'debug'=>0) );
	$out= "<blockquote>";

	foreach($rpcresult AS $blog) {
		// check for error
		if (!is_array($blog)) {
			berror("<p style='color: red; margin-top: 10px; text-align: center;'>Sent username and password and got back this error:<br />$rpcresult[faultString]</p>",0);
			}
		$this->blogSelect.= "<option value='$blog[blogid]:$blog[blogName]' onclick=\"document.uplinkform.remoteurl.value='$blog[url]';\">$blog[blogName]</option>\n";
		$out.= "blogid= $blog[blogid] | blogName= $blog[blogName] | url= $blog[url] <br />";
		if ($this->firstname=="") $this->firstname= $blog['blogName'];
		}
	$out.= "</blockquote>";
	berror("folder-weblogSetup.2: xml-rpc result is $out",1);
	$this->publishOption= "<tr>
	<td class='label'>publish on post?</td>
	<td class='control'><input type='radio' name='remotepublish' value='true' checked='1'>yes, publish blog when I post to it<br />
		<input type='radio' name='remotepublish' value='false' >no, just post; I'll publish it later
	</td>
</tr>";
	}
else {
	// livejournal login to check username
	// see http://www.livejournal.com/doc/server/ljp.csp.xml-rpc.login.html
	$rpcresult = xu_rpc_http_concise( array(
							'method' => "LJ.XMLRPC.login",
							'args' => array("username"=>$this->blogusername, "hpassword"=>md5($this->blogpassword), "clientversion"=>"berylium-uplink/".'$Date $'),
							'host' => $rpchost,
							'uri' => $rpcuri,
							'port' => $rpcport,
							'debug'=>0) );
	berror("LiveJournal returned $rpcresult[fullname].",1);
	// check for error
	if ($rpcresult['faultString']!="") {
		berror("<p style='color: red; margin-top: 10px; text-align: center;'>Sent username and password and got back this error:<br />$rpcresult[faultString]</p>",0);
		}

	// add default journal to select
	$this->blogSelect= "<option value='main:$rpcresult[fullname]'>$rpcresult[fullname]</option>";
	$this->firstname= $rpcresult['fullname'];

	// add any usejournals as well
	if (is_array($rpcresult['usejournals'])) {
		foreach($rpcresult['usejournals'] AS $journal) {
			$this->blogSelect.= "<option value='shared:$journal'>$journal</option>\n";
			}
		}
	}

// finish constructing select
$this->blogSelect= "<select name='blogidname'>".$this->blogSelect."</select>";


%%header%%

%%css%%

%%template%%
<form name='uplinkform' action='?method=save' method='post'>
<h1>Edit Uplink: Select Weblog</h1>
<table class='editTable'>
<tr>
	<td class='label'>select weblog:</td>
	<td class='control'>$this->blogSelect</td>
</tr>
$this->publishOption
<tr>
	<td class='label'>weblog URL:</td>
	<td class='control'><input type='text' name='remoteurl' size='42' value="$this->remoteurl" /><br />
		&nbsp;public web address of this weblog (optional)
	</td>
</tr>
<tr>
	<td>&nbsp;</td>
	<td>
		<input type='submit' name='submit' value='save' class='buttonstyle' />
		<input type='hidden' name='remoteuser' value="$this->blogusername" />
		<input type='hidden' name='remotepass' value="$this->blogpassword" />
		<input type='hidden' name='publishurl' value="$this->blogurl" />
		<input type='hidden' name='flavor' value="$this->p_blogtype" />
		<input type='hidden' name='return' value="$this->return" />
		<input type='hidden' name='id' value="$this->id" />
	</td>
</tr>
</table>

%%listrow%%

%%nullobject%%
<form action='?method=save' method='post'>
<h1>Create Uplink: Select Weblog</h1>
<table class='editTable'>
<tr>
	<td class='label'>select weblog:</td>
	<td class='control'>$this->blogSelect</td>
</tr>
$this->publishOption
<tr>
	<td class='label'>weblog URL:</td>
	<td class='control'><input type='text' name='remoteurl' size='42' /><br />
		&nbsp;public web address of this weblog (optional)
	</td>
</tr>
<tr>
	<td>&nbsp;</td>
	<td>
		<input type='submit' name='submit' value='save' class='buttonstyle' />
		<input type='hidden' name='remoteuser' value="$this->blogusername" />
		<input type='hidden' name='remotepass' value="$this->blogpassword" />
		<input type='hidden' name='publishurl' value="$this->blogurl" />
		<input type='hidden' name='flavor' value="$this->p_blogtype" />
		<input type='hidden' name='return' value="$this->return" />
	</td>
</tr>
</table>

%%footer%%

%%postprocess%%

%%end of context%%
