2016-04-19 03:38:38 +00:00
< ? php
namespace Zotlabs\Module ;
2016-05-22 23:54:30 +00:00
2016-04-19 03:38:38 +00:00
require_once ( 'include/socgraph.php' );
2016-05-21 05:13:20 +00:00
require_once ( 'include/selectors.php' );
2016-04-19 03:38:38 +00:00
class Connections extends \Zotlabs\Web\Controller {
function init () {
if ( ! local_channel ())
return ;
$channel = \App :: get_channel ();
if ( $channel )
head_set_icon ( $channel [ 'xchan_photo_s' ]);
}
2017-06-09 02:15:55 +00:00
function get () {
2016-04-19 03:38:38 +00:00
$sort_type = 0 ;
$o = '' ;
if ( ! local_channel ()) {
notice ( t ( 'Permission denied.' ) . EOL );
return login ();
}
2017-07-21 20:36:47 +00:00
2017-10-09 19:22:57 +00:00
nav_set_selected ( 'Connections' );
2016-04-19 03:38:38 +00:00
2018-03-13 09:57:08 +00:00
$active = false ;
2016-04-19 03:38:38 +00:00
$blocked = false ;
$hidden = false ;
$ignored = false ;
$archived = false ;
$unblocked = false ;
$pending = false ;
$unconnected = false ;
$all = false ;
if ( ! $_REQUEST [ 'aj' ])
$_SESSION [ 'return_url' ] = \App :: $query_string ;
2018-03-13 09:57:08 +00:00
$search_flags = " " ;
2016-04-19 03:38:38 +00:00
$head = '' ;
if ( argc () == 2 ) {
switch ( argv ( 1 )) {
2018-03-13 09:57:08 +00:00
case 'active' :
$search_flags = " and abook_blocked = 0 and abook_ignored = 0 and abook_hidden = 0 and abook_archived = 0 AND abook_not_here = 0 " ;
$head = t ( 'Active' );
$active = true ;
break ;
2016-04-19 03:38:38 +00:00
case 'blocked' :
$search_flags = " and abook_blocked = 1 " ;
$head = t ( 'Blocked' );
$blocked = true ;
break ;
case 'ignored' :
$search_flags = " and abook_ignored = 1 " ;
$head = t ( 'Ignored' );
$ignored = true ;
break ;
case 'hidden' :
$search_flags = " and abook_hidden = 1 " ;
$head = t ( 'Hidden' );
$hidden = true ;
break ;
case 'archived' :
2017-06-09 02:15:55 +00:00
$search_flags = " and ( abook_archived = 1 OR abook_not_here = 1) " ;
$head = t ( 'Archived/Unreachable' );
2016-04-19 03:38:38 +00:00
$archived = true ;
break ;
case 'pending' :
$search_flags = " and abook_pending = 1 " ;
$head = t ( 'New' );
$pending = true ;
break ;
case 'ifpending' :
$r = q ( " SELECT COUNT(abook.abook_id) AS total FROM abook left join xchan on abook.abook_xchan = xchan.xchan_hash where abook_channel = %d and abook_pending = 1 and abook_self = 0 and abook_ignored = 0 and xchan_deleted = 0 and xchan_orphan = 0 " ,
intval ( local_channel ())
);
if ( $r && $r [ 0 ][ 'total' ]) {
$search_flags = " and abook_pending = 1 " ;
$head = t ( 'New' );
$pending = true ;
\App :: $argv [ 1 ] = 'pending' ;
}
else {
$head = t ( 'All' );
$search_flags = '' ;
$all = true ;
\App :: $argc = 1 ;
unset ( \App :: $argv [ 1 ]);
}
break ;
// case 'unconnected':
// $search_flags = " and abook_unconnected = 1 ";
// $head = t('Unconnected');
// $unconnected = true;
// break;
case 'all' :
$head = t ( 'All' );
default :
2018-03-13 09:57:08 +00:00
$search_flags = " and abook_blocked = 0 and abook_ignored = 0 and abook_hidden = 0 and abook_archived = 0 and abook_not_here = 0 " ;
$active = true ;
$head = t ( 'Active' );
2016-04-19 03:38:38 +00:00
break ;
}
$sql_extra = $search_flags ;
if ( argv ( 1 ) === 'pending' )
$sql_extra .= " and abook_ignored = 0 " ;
}
else {
$sql_extra = " and abook_blocked = 0 " ;
$unblocked = true ;
}
$search = (( x ( $_REQUEST , 'search' )) ? notags ( trim ( $_REQUEST [ 'search' ])) : '' );
$tabs = array (
/*
array (
'label' => t ( 'Suggestions' ),
'url' => z_root () . '/suggest' ,
'sel' => '' ,
'title' => t ( 'Suggest new connections' ),
),
*/
2018-03-13 09:57:08 +00:00
'active' => array (
'label' => t ( 'Active Connections' ),
'url' => z_root () . '/connections/active' ,
'sel' => ( $active ) ? 'active' : '' ,
'title' => t ( 'Show active connections' ),
),
2016-04-19 03:38:38 +00:00
'pending' => array (
'label' => t ( 'New Connections' ),
'url' => z_root () . '/connections/pending' ,
'sel' => ( $pending ) ? 'active' : '' ,
'title' => t ( 'Show pending (new) connections' ),
),
/*
array (
'label' => t ( 'Unblocked' ),
'url' => z_root () . '/connections' ,
'sel' => (( $unblocked ) && ( ! $search ) && ( ! $nets )) ? 'active' : '' ,
'title' => t ( 'Only show unblocked connections' ),
),
*/
'blocked' => array (
'label' => t ( 'Blocked' ),
'url' => z_root () . '/connections/blocked' ,
'sel' => ( $blocked ) ? 'active' : '' ,
'title' => t ( 'Only show blocked connections' ),
),
'ignored' => array (
'label' => t ( 'Ignored' ),
'url' => z_root () . '/connections/ignored' ,
'sel' => ( $ignored ) ? 'active' : '' ,
'title' => t ( 'Only show ignored connections' ),
),
'archived' => array (
2017-06-09 22:24:37 +00:00
'label' => t ( 'Archived/Unreachable' ),
2016-04-19 03:38:38 +00:00
'url' => z_root () . '/connections/archived' ,
'sel' => ( $archived ) ? 'active' : '' ,
2017-06-09 02:15:55 +00:00
'title' => t ( 'Only show archived/unreachable connections' ),
2016-04-19 03:38:38 +00:00
),
'hidden' => array (
'label' => t ( 'Hidden' ),
'url' => z_root () . '/connections/hidden' ,
'sel' => ( $hidden ) ? 'active' : '' ,
'title' => t ( 'Only show hidden connections' ),
),
// array(
// 'label' => t('Unconnected'),
// 'url' => z_root() . '/connections/unconnected',
// 'sel' => ($unconnected) ? 'active' : '',
// 'title' => t('Only show one-way connections'),
// ),
2018-03-13 09:57:08 +00:00
'all' => array (
'label' => t ( 'All Connections' ),
'url' => z_root () . '/connections' ,
'sel' => ( $all ) ? 'active' : '' ,
'title' => t ( 'Show all connections' ),
),
2016-04-19 03:38:38 +00:00
);
//$tab_tpl = get_markup_template('common_tabs.tpl');
//$t = replace_macros($tab_tpl, array('$tabs'=>$tabs));
$searching = false ;
if ( $search ) {
$search_hdr = $search ;
$search_txt = dbesc ( protect_sprintf ( preg_quote ( $search )));
$searching = true ;
}
$sql_extra .= (( $searching ) ? protect_sprintf ( " AND xchan_name like '% $search_txt %' " ) : " " );
if ( $_REQUEST [ 'gid' ]) {
$sql_extra .= " and xchan_hash in ( select xchan from group_member where gid = " . intval ( $_REQUEST [ 'gid' ]) . " and uid = " . intval ( local_channel ()) . " ) " ;
}
$r = q ( " SELECT COUNT(abook.abook_id) AS total FROM abook left join xchan on abook.abook_xchan = xchan.xchan_hash
where abook_channel = % d and abook_self = 0 and xchan_deleted = 0 and xchan_orphan = 0 $sql_extra $sql_extra2 " ,
intval ( local_channel ())
);
if ( $r ) {
\App :: set_pager_total ( $r [ 0 ][ 'total' ]);
$total = $r [ 0 ][ 'total' ];
}
$r = q ( " SELECT abook.*, xchan.* FROM abook left join xchan on abook.abook_xchan = xchan.xchan_hash
WHERE abook_channel = % d and abook_self = 0 and xchan_deleted = 0 and xchan_orphan = 0 $sql_extra $sql_extra2 ORDER BY xchan_name LIMIT % d OFFSET % d " ,
intval ( local_channel ()),
intval ( \App :: $pager [ 'itemspage' ]),
intval ( \App :: $pager [ 'start' ])
);
$contacts = array ();
2016-12-21 04:37:06 +00:00
if ( $r ) {
2016-12-21 07:26:31 +00:00
vcard_query ( $r );
2016-04-19 03:38:38 +00:00
foreach ( $r as $rr ) {
if ( $rr [ 'xchan_url' ]) {
2016-12-21 07:26:31 +00:00
if (( $rr [ 'vcard' ]) && is_array ( $rr [ 'vcard' ][ 'tels' ]) && $rr [ 'vcard' ][ 'tels' ][ 0 ][ 'nr' ])
2018-02-06 02:06:25 +00:00
$phone = $rr [ 'vcard' ][ 'tels' ][ 0 ][ 'nr' ];
2016-12-21 07:26:31 +00:00
else
$phone = '' ;
2016-04-19 03:38:38 +00:00
$status_str = '' ;
$status = array (
2018-03-13 09:57:08 +00:00
(( intval ( $rr [ 'abook_active' ])) ? t ( 'Active' ) : '' ),
2016-04-19 03:38:38 +00:00
(( intval ( $rr [ 'abook_pending' ])) ? t ( 'Pending approval' ) : '' ),
(( intval ( $rr [ 'abook_archived' ])) ? t ( 'Archived' ) : '' ),
(( intval ( $rr [ 'abook_hidden' ])) ? t ( 'Hidden' ) : '' ),
(( intval ( $rr [ 'abook_ignored' ])) ? t ( 'Ignored' ) : '' ),
2017-06-08 23:38:07 +00:00
(( intval ( $rr [ 'abook_blocked' ])) ? t ( 'Blocked' ) : '' ),
(( intval ( $rr [ 'abook_not_here' ])) ? t ( 'Not connected at this location' ) : '' )
2016-04-19 03:38:38 +00:00
);
2018-03-13 09:57:08 +00:00
$oneway = false ;
2018-05-24 05:50:33 +00:00
if ( ! their_perms_contains ( local_channel (), $rr [ 'xchan_hash' ], 'post_comments' )) {
2018-03-13 09:57:08 +00:00
$oneway = true ;
}
2016-04-19 03:38:38 +00:00
foreach ( $status as $str ) {
if ( ! $str )
continue ;
$status_str .= $str ;
$status_str .= ', ' ;
}
$status_str = rtrim ( $status_str , ', ' );
$contacts [] = array (
'img_hover' => sprintf ( t ( '%1$s [%2$s]' ), $rr [ 'xchan_name' ], $rr [ 'xchan_url' ]),
'edit_hover' => t ( 'Edit connection' ),
2017-06-26 03:03:26 +00:00
'edit' => t ( 'Edit' ),
2016-04-19 03:38:38 +00:00
'delete_hover' => t ( 'Delete connection' ),
'id' => $rr [ 'abook_id' ],
'thumb' => $rr [ 'xchan_photo_m' ],
'name' => $rr [ 'xchan_name' ],
2017-06-08 23:38:07 +00:00
'classes' => (( intval ( $rr [ 'abook_archived' ]) || intval ( $rr [ 'abook_not_here' ])) ? 'archived' : '' ),
2016-04-19 03:38:38 +00:00
'link' => z_root () . '/connedit/' . $rr [ 'abook_id' ],
'deletelink' => z_root () . '/connedit/' . intval ( $rr [ 'abook_id' ]) . '/drop' ,
'delete' => t ( 'Delete' ),
2017-02-23 09:36:00 +00:00
'url' => chanlink_hash ( $rr [ 'xchan_hash' ]),
2016-04-19 03:38:38 +00:00
'webbie_label' => t ( 'Channel address' ),
'webbie' => $rr [ 'xchan_addr' ],
'network_label' => t ( 'Network' ),
'network' => network_to_name ( $rr [ 'xchan_network' ]),
'public_forum' => (( intval ( $rr [ 'xchan_pubforum' ])) ? true : false ),
2016-12-21 07:26:31 +00:00
'call' => t ( 'Call' ),
'phone' => $phone ,
2016-04-19 03:38:38 +00:00
'status_label' => t ( 'Status' ),
'status' => $status_str ,
'connected_label' => t ( 'Connected' ),
'connected' => datetime_convert ( 'UTC' , date_default_timezone_get (), $rr [ 'abook_created' ], 'c' ),
'approve_hover' => t ( 'Approve connection' ),
'approve' => (( $rr [ 'abook_pending' ]) ? t ( 'Approve' ) : false ),
'ignore_hover' => t ( 'Ignore connection' ),
'ignore' => (( ! $rr [ 'abook_ignored' ]) ? t ( 'Ignore' ) : false ),
'recent_label' => t ( 'Recent activity' ),
2018-03-13 09:57:08 +00:00
'recentlink' => z_root () . '/network/?f=&cid=' . intval ( $rr [ 'abook_id' ]),
2018-08-16 01:56:18 +00:00
'oneway' => $oneway ,
2018-09-21 00:17:41 +00:00
'allow_delete' => ( $rr [ 'abook_pending' ] || get_pconfig ( local_channel (), 'system' , 'connections_quick_delete' )),
2016-04-19 03:38:38 +00:00
);
}
}
}
if ( $_REQUEST [ 'aj' ]) {
if ( $contacts ) {
$o = replace_macros ( get_markup_template ( 'contactsajax.tpl' ), array (
'$contacts' => $contacts ,
'$edit' => t ( 'Edit' ),
));
}
else {
$o = '<div id="content-complete"></div>' ;
}
echo $o ;
killme ();
}
else {
2018-08-27 06:00:31 +00:00
$o .= " <script> var page_query = ' " . escape_tags ( $_GET [ 'req' ]) . " '; var extra_args = ' " . extra_query_args () . " ' ; </script> " ;
2016-04-19 03:38:38 +00:00
$o .= replace_macros ( get_markup_template ( 'connections.tpl' ), array (
'$header' => t ( 'Connections' ) . (( $head ) ? ': ' . $head : '' ),
'$tabs' => $tabs ,
'$total' => $total ,
'$search' => $search_hdr ,
'$label' => t ( 'Search' ),
'$desc' => t ( 'Search your connections' ),
'$finding' => (( $searching ) ? t ( 'Connections search' ) . " : ' " . $search . " ' " : " " ),
'$submit' => t ( 'Find' ),
'$edit' => t ( 'Edit' ),
'$cmd' => \App :: $cmd ,
'$contacts' => $contacts ,
'$paginate' => paginate ( $a ),
));
}
if ( ! $contacts )
$o .= '<div id="content-complete"></div>' ;
return $o ;
}
}