2016-04-19 03:38:38 +00:00
< ? php
2021-12-03 03:01:39 +00:00
2022-02-16 04:08:28 +00:00
namespace Code\Module ;
2016-04-19 03:38:38 +00:00
2019-04-17 23:54:06 +00:00
use App ;
2022-02-16 04:08:28 +00:00
use Code\Web\Controller ;
use Code\Lib\Libprofile ;
use Code\Lib\Channel ;
use Code\Lib as Zlib ;
use Code\Lib\MenuItem ;
use Code\Render\Theme ;
2022-02-12 20:43:29 +00:00
2022-01-26 05:43:05 +00:00
2016-04-19 03:38:38 +00:00
2021-12-02 23:02:31 +00:00
class Menu extends Controller
{
public function init ()
{
if ( argc () > 1 && argv ( 1 ) === 'sys' && is_site_admin ()) {
2022-01-25 01:26:12 +00:00
$sys = Channel :: get_system ();
2021-12-02 23:02:31 +00:00
if ( $sys && intval ( $sys [ 'channel_id' ])) {
App :: $is_sys = true ;
}
}
2021-12-03 03:01:39 +00:00
if ( argc () > 1 ) {
2021-12-02 23:02:31 +00:00
$which = argv ( 1 );
2021-12-03 03:01:39 +00:00
} else {
2021-12-02 23:02:31 +00:00
return ;
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
Libprofile :: load ( $which );
}
public function post ()
{
if ( ! App :: $profile ) {
return ;
}
$which = argv ( 1 );
$uid = App :: $profile [ 'channel_id' ];
if ( array_key_exists ( 'sys' , $_REQUEST ) && $_REQUEST [ 'sys' ] && is_site_admin ()) {
2022-01-25 01:26:12 +00:00
$sys = Channel :: get_system ();
2021-12-02 23:02:31 +00:00
$uid = intval ( $sys [ 'channel_id' ]);
App :: $is_sys = true ;
}
2021-12-03 03:01:39 +00:00
if ( ! $uid ) {
2021-12-02 23:02:31 +00:00
return ;
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
$_REQUEST [ 'menu_channel_id' ] = $uid ;
2021-12-03 03:01:39 +00:00
if ( $_REQUEST [ 'menu_bookmark' ]) {
2021-12-02 23:02:31 +00:00
$_REQUEST [ 'menu_flags' ] |= MENU_BOOKMARK ;
2021-12-03 03:01:39 +00:00
}
if ( $_REQUEST [ 'menu_system' ]) {
2021-12-02 23:02:31 +00:00
$_REQUEST [ 'menu_flags' ] |= MENU_SYSTEM ;
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
$menu_id = (( argc () > 1 ) ? intval ( argv ( 1 )) : 0 );
if ( $menu_id ) {
$_REQUEST [ 'menu_id' ] = intval ( argv ( 1 ));
2022-01-26 05:43:05 +00:00
$r = Zlib\Menu :: edit ( $_REQUEST );
2021-12-02 23:02:31 +00:00
if ( $r ) {
2022-01-26 05:43:05 +00:00
Zlib\Menu :: sync_packet ( $uid , get_observer_hash (), $menu_id );
2021-12-02 23:02:31 +00:00
//info( t('Menu updated.') . EOL);
goaway ( z_root () . '/mitem/' . $which . '/' . $menu_id . (( App :: $is_sys ) ? '?f=&sys=1' : '' ));
2021-12-03 03:01:39 +00:00
} else {
2021-12-02 23:02:31 +00:00
notice ( t ( 'Unable to update menu.' ) . EOL );
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
} else {
2022-01-26 05:43:05 +00:00
$r = Zlib\Menu :: create ( $_REQUEST );
2021-12-02 23:02:31 +00:00
if ( $r ) {
2022-01-26 05:43:05 +00:00
Zlib\Menu :: sync_packet ( $uid , get_observer_hash (), $r );
2021-12-02 23:02:31 +00:00
//info( t('Menu created.') . EOL);
goaway ( z_root () . '/mitem/' . $which . '/' . $r . (( App :: $is_sys ) ? '?f=&sys=1' : '' ));
2021-12-03 03:01:39 +00:00
} else {
2021-12-02 23:02:31 +00:00
notice ( t ( 'Unable to create menu.' ) . EOL );
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
}
}
public function get ()
{
if ( ! App :: $profile ) {
notice ( t ( 'Requested profile is not available.' ) . EOL );
App :: $error = 404 ;
return ;
}
$which = argv ( 1 );
$_SESSION [ 'return_url' ] = App :: $query_string ;
$uid = local_channel ();
$owner = 0 ;
$channel = null ;
$observer = App :: get_observer ();
$channel = App :: get_channel ();
if ( App :: $is_sys && is_site_admin ()) {
2022-01-25 01:26:12 +00:00
$sys = Channel :: get_system ();
2021-12-02 23:02:31 +00:00
if ( $sys && intval ( $sys [ 'channel_id' ])) {
$uid = $owner = intval ( $sys [ 'channel_id' ]);
$channel = $sys ;
$observer = $sys ;
}
}
if ( ! $owner ) {
// Figure out who the page owner is.
2022-01-25 01:26:12 +00:00
$r = Channel :: from_username ( $which );
2021-12-02 23:02:31 +00:00
if ( $r ) {
$owner = intval ( $r [ 'channel_id' ]);
}
}
$ob_hash = (( $observer ) ? $observer [ 'xchan_hash' ] : '' );
$perms = get_all_perms ( $owner , $ob_hash );
if ( ! $perms [ 'write_pages' ]) {
notice ( t ( 'Permission denied.' ) . EOL );
return ;
}
// Get the observer, check their permissions
$ob_hash = (( $observer ) ? $observer [ 'xchan_hash' ] : '' );
$perms = get_all_perms ( $owner , $ob_hash );
if ( ! $perms [ 'write_pages' ]) {
notice ( t ( 'Permission denied.' ) . EOL );
return ;
}
if ( argc () == 2 ) {
2022-01-25 01:26:12 +00:00
$channel = (( $sys ) ? $sys : Channel :: from_id ( $owner ));
2021-12-02 23:02:31 +00:00
// list menus
2022-01-26 05:43:05 +00:00
$x = Zlib\Menu :: list ( $owner );
2021-12-02 23:02:31 +00:00
if ( $x ) {
for ( $y = 0 ; $y < count ( $x ); $y ++ ) {
$m = menu_fetch ( $x [ $y ][ 'menu_name' ], $owner , get_observer_hash ());
2021-12-03 03:01:39 +00:00
if ( $m ) {
2021-12-02 23:02:31 +00:00
$x [ $y ][ 'element' ] = '[element]' . base64url_encode ( json_encode ( menu_element ( $channel , $m ))) . '[/element]' ;
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
$x [ $y ][ 'bookmark' ] = (( $x [ $y ][ 'menu_flags' ] & MENU_BOOKMARK ) ? true : false );
}
}
2022-02-12 20:43:29 +00:00
$create = replace_macros ( Theme :: get_template ( 'menuedit.tpl' ), array (
2021-12-02 23:02:31 +00:00
'$menu_name' => array ( 'menu_name' , t ( 'Menu Name' ), '' , t ( 'Unique name (not visible on webpage) - required' ), '*' ),
'$menu_desc' => array ( 'menu_desc' , t ( 'Menu Title' ), '' , t ( 'Visible on webpage - leave empty for no title' ), '' ),
'$menu_bookmark' => array ( 'menu_bookmark' , t ( 'Allow Bookmarks' ), 0 , t ( 'Menu may be used to store saved bookmarks' ), array ( t ( 'No' ), t ( 'Yes' ))),
'$submit' => t ( 'Submit and proceed' ),
'$sys' => App :: $is_sys ,
'$nick' => $which ,
'$display' => 'none'
));
2022-02-12 20:43:29 +00:00
$o = replace_macros ( Theme :: get_template ( 'menulist.tpl' ), array (
2021-12-02 23:02:31 +00:00
'$title' => t ( 'Menus' ),
'$create' => $create ,
'$menus' => $x ,
'$nametitle' => t ( 'Menu Name' ),
'$desctitle' => t ( 'Menu Title' ),
'$edit' => t ( 'Edit' ),
'$drop' => t ( 'Drop' ),
'$created' => t ( 'Created' ),
'$edited' => t ( 'Edited' ),
'$new' => t ( 'New' ),
'$bmark' => t ( 'Bookmarks allowed' ),
'$hintnew' => t ( 'Create' ),
'$hintdrop' => t ( 'Delete this menu' ),
'$hintcontent' => t ( 'Edit menu contents' ),
'$hintedit' => t ( 'Edit this menu' ),
'$nick' => $which ,
'$sys' => App :: $is_sys
));
return $o ;
}
if ( argc () > 2 ) {
if ( intval ( argv ( 2 ))) {
if ( argc () == 4 && argv ( 3 ) == 'drop' ) {
2022-01-26 05:43:05 +00:00
Zlib\Menu :: sync_packet ( $owner , get_observer_hash (), intval ( argv ( 1 )), true );
$r = Zlib\Menu :: delete_id ( intval ( argv ( 2 )), $owner );
2021-12-03 03:01:39 +00:00
if ( ! $r ) {
2021-12-02 23:02:31 +00:00
notice ( t ( 'Menu could not be deleted.' ) . EOL );
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
goaway ( z_root () . '/menu/' . $which . (( App :: $is_sys ) ? '?f=&sys=1' : '' ));
}
2022-01-26 05:43:05 +00:00
$m = Zlib\Menu :: fetch_id ( intval ( argv ( 2 )), $owner );
2021-12-02 23:02:31 +00:00
if ( ! $m ) {
notice ( t ( 'Menu not found.' ) . EOL );
return '' ;
}
2022-02-12 20:43:29 +00:00
$o = replace_macros ( Theme :: get_template ( 'menuedit.tpl' ), array (
2021-12-02 23:02:31 +00:00
'$header' => t ( 'Edit Menu' ),
'$sys' => App :: $is_sys ,
'$menu_id' => intval ( argv ( 2 )),
'$menu_edit_link' => 'mitem/' . $which . '/' . intval ( argv ( 1 )) . (( App :: $is_sys ) ? '?f=&sys=1' : '' ),
'$hintedit' => t ( 'Add or remove entries to this menu' ),
'$editcontents' => t ( 'Edit menu contents' ),
'$menu_name' => array ( 'menu_name' , t ( 'Menu name' ), $m [ 'menu_name' ], t ( 'Must be unique, only seen by you' ), '*' ),
'$menu_desc' => array ( 'menu_desc' , t ( 'Menu title' ), $m [ 'menu_desc' ], t ( 'Menu title as seen by others' ), '' ),
'$menu_bookmark' => array ( 'menu_bookmark' , t ( 'Allow bookmarks' ), (( $m [ 'menu_flags' ] & MENU_BOOKMARK ) ? 1 : 0 ), t ( 'Menu may be used to store saved bookmarks' ), array ( t ( 'No' ), t ( 'Yes' ))),
'$menu_system' => (( $m [ 'menu_flags' ] & MENU_SYSTEM ) ? 1 : 0 ),
'$nick' => $which ,
'$submit' => t ( 'Submit and proceed' )
));
return $o ;
} else {
notice ( t ( 'Not found.' ) . EOL );
return ;
}
}
}
2016-04-19 03:38:38 +00:00
}