$x1=q("select xchan from abconfig where chan = %d and cat = 'their_perms' and k = 'send_stream' and v = '0'",
intval(local_channel())
);
if($x1){
$xc=ids_to_querystr($x1,'xchan',true);
$x2=q("select xchan from abconfig where chan = %d and cat = 'their_perms' and k = 'tag_deliver' and v = '1' and xchan in (".$xc.") ",
intval(local_channel())
);
if($x2)
$xf=ids_to_querystr($x2,'xchan',true);
}
$sql_extra=(($xf)?" and ( xchan_hash in (".$xf.") or xchan_pubforum = 1 ) ":" and xchan_pubforum = 1 ");
$r1=q("select abook_id, xchan_hash, xchan_name, xchan_url, xchan_photo_s from abook left join xchan on abook_xchan = xchan_hash where xchan_deleted = 0 and abook_channel = %d $sql_extra order by xchan_name $limit",
intval(local_channel())
);
if(!$r1)
return$o;
$str='';
// Trying to cram all this into a single query with joins and the proper group by's is tough.
// There also should be a way to update this via ajax.
for($x=0;$x<count($r1);$x++){
$r=q("select sum(item_unseen) as unseen from item where owner_xchan = '%s' and uid = %d and item_unseen = 1 $perms_sql",
$r=q("select sum(item_unseen) as unseen from item left join term on oid = id where otype = %d and owner_xchan != '%s' and item.uid = %d and url = '%s' and ttype = %d $perms_sql",