2019-12-03 02:44:32 +00:00
< ? php
2021-12-03 03:01:39 +00:00
2022-02-16 04:08:28 +00:00
namespace Code\Widget ;
2019-12-03 02:44:32 +00:00
/*
* Show pinned content
*
*/
use App ;
2022-10-23 21:18:44 +00:00
class Pinned implements WidgetInterface
2021-12-02 23:02:31 +00:00
{
2022-10-23 21:18:44 +00:00
private $allowed_types = [];
2021-12-02 23:02:31 +00:00
private $uid = 0 ;
/*
* @ brief Displays pinned items
*
*/
2022-10-24 03:39:49 +00:00
public function widget ( array $arguments ) : string
2021-12-02 23:02:31 +00:00
{
$ret = '' ;
$this -> allowed_types = get_config ( 'system' , 'pin_types' , [ ITEM_TYPE_POST ]);
$this -> uid = App :: $profile_uid ;
2022-10-24 03:39:49 +00:00
$types = (( $arguments [ 'types' ]) ? : [ ITEM_TYPE_POST ]);
2021-12-02 23:02:31 +00:00
$id_list = $this -> list ( $types );
// logger('id_list: ' . print_r($id_list,true));
if ( empty ( $id_list )) {
return $ret ;
}
$o = conversation ( $id_list , 'stream-new' , 0 , 'traditional' );
// change some id and class names so that auto-update doesn't stumble over them
$o = str_replace ( '<div id="threads-begin">' , '<div id="pins-begin">' , $o );
$o = str_replace ( '<div id="threads-end">' , '<div id="pins-end">' , $o );
$o = str_replace ( '<div id="conversation-end">' , '<div id="pin-widget-end">' , $o );
$o = str_replace ( 'class="thread-wrapper ' , 'class="pin-thread-wrapper ' , $o );
$o = str_replace ( 'class="wall-item-ago' , 'class="wall-item-ago pinned' , $o );
// logger('output: ' . $o);
return '<hr>' . $o . '<hr>' ;
}
/*
* @ brief List pinned items depend on type
*
* @ param $types
* @ return array of pinned items
*
*/
private function list ( $types )
{
if ( empty ( $types ) || ( ! is_array ( $types ))) {
return [];
}
$item_types = array_intersect ( $this -> allowed_types , $types );
if ( empty ( $item_types )) {
return [];
}
$mids_list = [];
foreach ( $item_types as $type ) {
$mids = get_pconfig ( $this -> uid , 'pinned' , $type , []);
if ( $mids ) {
foreach ( $mids as $mid ) {
if ( $mid ) {
$mids_list [] = $mid ;
}
}
}
}
if ( empty ( $mids_list )) {
return [];
}
$item_normal = item_normal ();
$sql_extra = item_permissions_sql ( $this -> uid );
2021-12-03 03:01:39 +00:00
$r = q (
" SELECT *, id as item_id FROM item WHERE parent_mid IN ( " . protect_sprintf ( stringify_array ( $mids_list , true )) . " ) AND uid = %d AND id = parent $item_normal $sql_extra ORDER BY created DESC " ,
2021-12-02 23:02:31 +00:00
intval ( $this -> uid )
);
if ( $r ) {
xchan_query ( $r , true );
2022-10-23 00:50:31 +00:00
$items = fetch_post_tags ( $r );
2021-12-02 23:02:31 +00:00
for ( $x = 0 ; $x < count ( $items ); $x ++ ) {
$items [ $x ][ 'item_id' ] = 'pin-' . $items [ $x ][ 'item_id' ];
}
return $items ;
}
return [];
}
2019-12-03 02:44:32 +00:00
}