2017-03-17 00:11:48 +00:00
< ? php
namespace Zotlabs\Widget ;
2019-03-06 01:27:16 +00:00
class Groups {
2017-03-17 00:11:48 +00:00
function widget ( $arr ) {
2020-03-04 02:00:20 +00:00
$output = EMPTY_STR ;
2017-03-17 00:11:48 +00:00
2020-03-04 02:00:20 +00:00
if ( ! local_channel ()) {
return $output ;
}
2017-03-17 00:11:48 +00:00
2020-03-04 02:00:20 +00:00
if ( is_array ( $arr ) && array_key_exists ( 'limit' , $arr ) && intval ( $arr [ 'limit' ]) >= 0 ) {
$limit = " limit " . intval ( $arr [ 'limit' ]) . " " ;
}
else {
$limit = EMPTY_STR ;
}
2017-03-17 00:11:48 +00:00
$unseen = 0 ;
2020-03-04 02:00:20 +00:00
if ( is_array ( $arr ) && array_key_exists ( 'unseen' , $arr ) && intval ( $arr [ 'unseen' ])) {
2017-03-17 00:11:48 +00:00
$unseen = 1 ;
2020-03-04 02:00:20 +00:00
}
2017-03-17 00:11:48 +00:00
$perms_sql = item_permissions_sql ( local_channel ()) . item_normal ();
$xf = false ;
2018-06-08 02:44:09 +00:00
$x1 = q ( " select xchan from abconfig where chan = %d and cat = 'system' and k = 'their_perms' and not v like '%s' " ,
intval ( local_channel ()),
dbesc ( '%send_stream%' )
2017-03-17 00:11:48 +00:00
);
2020-03-04 02:00:20 +00:00
if ( $x1 ) {
2017-03-17 00:11:48 +00:00
$xc = ids_to_querystr ( $x1 , 'xchan' , true );
2017-11-16 04:16:51 +00:00
2018-06-08 02:44:09 +00:00
$x2 = q ( " select xchan from abconfig where chan = %d and cat = 'system' and k = 'their_perms' and v like '%s' and xchan in ( " . $xc . " ) " ,
intval ( local_channel ()),
dbesc ( '%tag_deliver%' )
2017-03-17 00:11:48 +00:00
);
2017-11-16 04:16:51 +00:00
2020-03-04 02:00:20 +00:00
if ( $x2 ) {
2017-03-17 00:11:48 +00:00
$xf = ids_to_querystr ( $x2 , 'xchan' , true );
2017-11-16 04:16:51 +00:00
// private forums
2018-06-08 02:44:09 +00:00
$x3 = q ( " select xchan from abconfig where chan = %d and cat = 'system' and k = 'their_perms' and v like '%s' and xchan in ( " . $xc . " ) and not xchan in ( " . $xf . " ) " ,
intval ( local_channel ()),
dbesc ( '%post_wall%' )
2017-11-16 04:16:51 +00:00
);
2020-03-04 02:00:20 +00:00
if ( $x3 ) {
2017-11-16 04:16:51 +00:00
$xf = ids_to_querystr ( array_merge ( $x2 , $x3 ), 'xchan' , true );
}
}
2017-03-17 00:11:48 +00:00
}
2019-03-06 01:27:16 +00:00
$sql_extra = (( $xf ) ? " and ( xchan_hash in ( " . $xf . " ) or xchan_type = 1 ) " : " and xchan_type = 1 " );
2017-03-17 00:11:48 +00:00
2018-05-31 23:17:24 +00:00
$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 and abook_pending = 0 and abook_ignored = 0 and abook_blocked = 0 and abook_archived = 0 $sql_extra order by xchan_name $limit " ,
2017-03-17 00:11:48 +00:00
intval ( local_channel ())
);
2017-11-16 04:16:51 +00:00
2020-03-04 02:00:20 +00:00
if ( ! $r1 ) {
return $output ;
}
$str = EMPTY_STR ;
2017-03-17 00:11:48 +00:00
// 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.
2020-03-04 02:00:20 +00:00
for ( $x = 0 ; $x < count ( $r1 ); $x ++ ) {
2018-02-04 19:42:40 +00:00
$r = q ( " select sum(item_unseen) as unseen from item
2018-02-04 21:37:30 +00:00
where uid = % d and owner_xchan = '%s' and item_unseen = 1 $perms_sql " ,
2018-04-24 15:09:51 +00:00
intval ( local_channel ()),
dbesc ( $r1 [ $x ][ 'xchan_hash' ])
2017-03-17 00:11:48 +00:00
);
2020-03-04 02:00:20 +00:00
if ( $r ) {
2017-03-17 00:11:48 +00:00
$r1 [ $x ][ 'unseen' ] = $r [ 0 ][ 'unseen' ];
2020-03-04 02:00:20 +00:00
}
2018-01-18 22:11:15 +00:00
}
2017-03-17 00:11:48 +00:00
/**
* @ FIXME
* This SQL makes the counts correct when you get forum posts arriving from different routes / sources
2020-02-13 05:36:21 +00:00
* ( like personal channels ) . However the stream query for these posts doesn ' t yet include this
2017-03-17 00:11:48 +00:00
* correction and it makes the SQL for that query pretty hairy so this is left as a future exercise .
* It may make more sense in that query to look for the mention in the body rather than another join ,
* but that makes it very inefficient .
*
2018-01-18 22:11:15 +00:00
* $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 " ,
* intval ( TERM_OBJ_POST ),
* dbesc ( $r1 [ $x ][ 'xchan_hash' ]),
* intval ( local_channel ()),
* dbesc ( $r1 [ $x ][ 'xchan_url' ]),
* intval ( TERM_MENTION )
* );
* if ( $r )
* $r1 [ $x ][ 'unseen' ] = (( array_key_exists ( 'unseen' , $r1 [ $x ])) ? $r1 [ $x ][ 'unseen' ] + $r [ 0 ][ 'unseen' ] : $r [ 0 ][ 'unseen' ]);
2017-03-17 00:11:48 +00:00
*
* end @ FIXME
*/
2020-03-04 02:00:20 +00:00
if ( $r1 ) {
$output .= '<div class="widget">' ;
$output .= '<h3>' . t ( 'Groups' ) . '</h3><ul class="nav nav-pills flex-column">' ;
2017-03-17 00:11:48 +00:00
2020-03-04 02:00:20 +00:00
foreach ( $r1 as $rr ) {
2017-11-16 04:16:51 +00:00
2020-02-13 05:36:21 +00:00
$link = 'stream?f=&pf=1&cid=' . $rr [ 'abook_id' ];
2020-03-04 02:00:20 +00:00
if ( $x3 ) {
foreach ( $x3 as $xx ) {
if ( $rr [ 'xchan_hash' ] == $xx [ 'xchan' ]) {
2017-11-16 04:16:51 +00:00
$link = zid ( $rr [ 'xchan_url' ]);
}
}
}
2020-03-04 02:00:20 +00:00
if ( $unseen && ( ! intval ( $rr [ 'unseen' ]))) {
2017-03-17 00:11:48 +00:00
continue ;
2020-03-04 02:00:20 +00:00
}
2017-11-16 04:16:51 +00:00
2020-03-04 02:00:20 +00:00
$output .= '<li class="nav-item"><a class="nav-link" href="' . $link . '" ><span class="badge badge-secondary float-right">' . (( intval ( $rr [ 'unseen' ])) ? intval ( $rr [ 'unseen' ]) : '' ) . '</span><img class ="menu-img-1" src="' . $rr [ 'xchan_photo_s' ] . '" /> ' . $rr [ 'xchan_name' ] . '</a></li>' ;
2017-03-17 00:11:48 +00:00
}
2020-03-04 02:00:20 +00:00
$output .= '</ul></div>' ;
2017-03-17 00:11:48 +00:00
}
2020-03-04 02:00:20 +00:00
return $output ;
2017-03-17 00:11:48 +00:00
}
}