2013-08-12 10:25:37 +00:00
< ? php
2013-08-14 03:57:03 +00:00
require_once ( 'include/menu.php' );
2013-12-07 21:05:17 +00:00
require_once ( 'include/acl_selectors.php' );
2013-08-14 03:57:03 +00:00
function mitem_init ( & $a ) {
2014-11-12 09:43:34 +00:00
2015-01-29 04:56:04 +00:00
$uid = local_channel ();
2014-11-12 09:43:34 +00:00
if ( array_key_exists ( 'sys' , $_REQUEST ) && $_REQUEST [ 'sys' ] && is_site_admin ()) {
$sys = get_sys_channel ();
$uid = intval ( $sys [ 'channel_id' ]);
$a -> is_sys = true ;
}
if ( ! $uid )
2013-08-14 03:57:03 +00:00
return ;
2014-11-12 09:43:34 +00:00
2013-08-14 03:57:03 +00:00
if ( argc () < 2 )
return ;
2014-11-12 09:43:34 +00:00
$m = menu_fetch_id ( intval ( argv ( 1 )), $uid );
2013-08-14 03:57:03 +00:00
if ( ! $m ) {
notice ( t ( 'Menu not found.' ) . EOL );
return '' ;
}
$a -> data [ 'menu' ] = $m ;
}
2013-08-12 10:25:37 +00:00
function mitem_post ( & $a ) {
2015-01-29 04:56:04 +00:00
$uid = local_channel ();
2014-11-12 09:43:34 +00:00
if ( $a -> is_sys && is_site_admin ()) {
$sys = get_sys_channel ();
$uid = intval ( $sys [ 'channel_id' ]);
}
if ( ! $uid ) {
2013-08-12 10:25:37 +00:00
return ;
2014-11-12 09:43:34 +00:00
}
2013-08-14 03:57:03 +00:00
if ( ! $a -> data [ 'menu' ])
return ;
2013-08-12 10:25:37 +00:00
2015-04-21 08:05:26 +00:00
if ( ! $_REQUEST [ 'mitem_desc' ] || ! $_REQUEST [ 'mitem_link' ]) {
notice ( t ( 'Unable to create element.' ) . EOL );
return ;
}
2014-11-12 09:43:34 +00:00
$_REQUEST [ 'mitem_channel_id' ] = $uid ;
2013-08-14 03:57:03 +00:00
$_REQUEST [ 'menu_id' ] = $a -> data [ 'menu' ][ 'menu_id' ];
2013-08-12 10:25:37 +00:00
2013-08-14 03:57:03 +00:00
$_REQUEST [ 'mitem_flags' ] = 0 ;
if ( $_REQUEST [ 'usezid' ])
$_REQUEST [ 'mitem_flags' ] |= MENU_ITEM_ZID ;
if ( $_REQUEST [ 'newwin' ])
$_REQUEST [ 'mitem_flags' ] |= MENU_ITEM_NEWWIN ;
2013-08-12 10:25:37 +00:00
2013-08-14 03:57:03 +00:00
$mitem_id = (( argc () > 2 ) ? intval ( argv ( 2 )) : 0 );
if ( $mitem_id ) {
$_REQUEST [ 'mitem_id' ] = $mitem_id ;
2014-11-12 09:43:34 +00:00
$r = menu_edit_item ( $_REQUEST [ 'menu_id' ], $uid , $_REQUEST );
2013-08-14 03:57:03 +00:00
if ( $r ) {
2015-09-09 00:51:48 +00:00
menu_sync_packet ( $uid , get_observer_hash (), $_REQUEST [ 'menu_id' ]);
2015-04-21 08:05:26 +00:00
//info( t('Menu element updated.') . EOL);
2014-11-12 09:43:34 +00:00
goaway ( z_root () . '/mitem/' . $_REQUEST [ 'menu_id' ] . (( $a -> is_sys ) ? '?f=&sys=1' : '' ));
2013-08-14 03:57:03 +00:00
}
else
notice ( t ( 'Unable to update menu element.' ) . EOL );
}
else {
2014-11-12 09:43:34 +00:00
$r = menu_add_item ( $_REQUEST [ 'menu_id' ], $uid , $_REQUEST );
2013-08-14 03:57:03 +00:00
if ( $r ) {
2015-09-09 00:51:48 +00:00
menu_sync_packet ( $uid , get_observer_hash (), $_REQUEST [ 'menu_id' ]);
2015-04-21 08:05:26 +00:00
//info( t('Menu element added.') . EOL);
if ( $_REQUEST [ 'submit' ]) {
goaway ( z_root () . '/menu' . (( $a -> is_sys ) ? '?f=&sys=1' : '' ));
}
if ( $_REQUEST [ 'submit-more' ]) {
goaway ( z_root () . '/mitem/' . $_REQUEST [ 'menu_id' ] . '?f=&display=block' . (( $a -> is_sys ) ? '&sys=1' : '' ) );
}
2013-08-14 03:57:03 +00:00
}
else
notice ( t ( 'Unable to add menu element.' ) . EOL );
}
2013-08-12 10:25:37 +00:00
}
function mitem_content ( & $a ) {
2015-01-29 04:56:04 +00:00
$uid = local_channel ();
2014-11-12 09:43:34 +00:00
$channel = $a -> get_channel ();
$observer = $a -> get_observer ();
$ob_hash = (( $observer ) ? $observer [ 'xchan_hash' ] : '' );
if ( $a -> is_sys && is_site_admin ()) {
$sys = get_sys_channel ();
$uid = intval ( $sys [ 'channel_id' ]);
$channel = $sys ;
$ob_hash = $sys [ 'xchan_hash' ];
}
if ( ! $uid ) {
2013-08-12 10:25:37 +00:00
notice ( t ( 'Permission denied.' ) . EOL );
return '' ;
}
2013-08-14 03:57:03 +00:00
if ( argc () < 2 || ( ! $a -> data [ 'menu' ])) {
notice ( t ( 'Not found.' ) . EOL );
return '' ;
2013-08-12 10:25:37 +00:00
}
2014-11-12 09:43:34 +00:00
$m = menu_fetch ( $a -> data [ 'menu' ][ 'menu_name' ], $uid , $ob_hash );
2013-12-22 10:04:55 +00:00
$a -> data [ 'menu_item' ] = $m ;
2013-08-14 03:57:03 +00:00
2015-05-10 13:00:18 +00:00
$menu_list = menu_list ( $uid );
foreach ( $menu_list as $menus ) {
if ( $menus [ 'menu_name' ] != $m [ 'menu' ][ 'menu_name' ])
$menu_names [] = $menus [ 'menu_name' ];
}
2015-08-28 04:22:59 +00:00
$acl = new AccessList ( $channel );
2015-04-21 20:45:54 +00:00
$lockstate = (( $channel [ 'channel_allow_cid' ] || $channel [ 'channel_allow_gid' ] || $channel [ 'channel_deny_cid' ] || $channel [ 'channel_deny_gid' ]) ? 'lock' : 'unlock' );
2013-08-14 03:57:03 +00:00
if ( argc () == 2 ) {
$r = q ( " select * from menu_item where mitem_menu_id = %d and mitem_channel_id = %d order by mitem_order asc, mitem_desc asc " ,
intval ( $a -> data [ 'menu' ][ 'menu_id' ]),
2014-11-12 09:43:34 +00:00
intval ( $uid )
2013-08-14 03:57:03 +00:00
);
2015-04-21 08:05:26 +00:00
if ( $_GET [ 'display' ]) {
$display = $_GET [ 'display' ];
}
else {
$display = (( $r ) ? 'none' : 'block' );
}
$create = replace_macros ( get_markup_template ( 'mitemedit.tpl' ), array (
'$menu_id' => $a -> data [ 'menu' ][ 'menu_id' ],
'$permissions' => t ( 'Menu Item Permissions' ),
'$permdesc' => t ( " \x28 click to open/close \x29 " ),
2015-08-28 04:22:59 +00:00
'$aclselect' => populate_acl ( $acl -> get (), false ),
2015-04-21 08:05:26 +00:00
'$mitem_desc' => array ( 'mitem_desc' , t ( 'Link Name' ), '' , 'Visible name of the link' , '*' ),
2015-05-12 00:50:17 +00:00
'$mitem_link' => array ( 'mitem_link' , t ( 'Link or Submenu Target' ), '' , t ( 'Enter URL of the link or select a menu name to create a submenu' ), '*' , 'list="menu-names"' ),
2015-05-29 13:23:51 +00:00
'$usezid' => array ( 'usezid' , t ( 'Use magic-auth if available' ), true , '' , array ( t ( 'No' ), t ( 'Yes' ))),
'$newwin' => array ( 'newwin' , t ( 'Open link in new window' ), false , '' , array ( t ( 'No' ), t ( 'Yes' ))),
2015-04-21 08:05:26 +00:00
'$mitem_order' => array ( 'mitem_order' , t ( 'Order in list' ), '0' , t ( 'Higher numbers will sink to bottom of listing' )),
'$submit' => t ( 'Submit and finish' ),
'$submit_more' => t ( 'Submit and continue' ),
2015-04-21 20:45:54 +00:00
'$display' => $display ,
2015-05-10 13:00:18 +00:00
'$lockstate' => $lockstate ,
2015-07-11 23:04:57 +00:00
'$menu_names' => $menu_names ,
2015-07-11 23:12:20 +00:00
'$sys' => $a -> is_sys
2015-04-21 08:05:26 +00:00
));
2013-08-12 10:25:37 +00:00
2013-09-07 09:04:15 +00:00
$o .= replace_macros ( get_markup_template ( 'mitemlist.tpl' ), array (
2015-04-21 08:05:26 +00:00
'$title' => t ( 'Menu:' ),
'$create' => $create ,
'$nametitle' => t ( 'Link Name' ),
'$targettitle' => t ( 'Link Target' ),
'$menuname' => $a -> data [ 'menu' ][ 'menu_name' ],
'$menudesc' => $a -> data [ 'menu' ][ 'menu_desc' ],
'$edmenu' => t ( 'Edit menu' ),
'$menu_id' => $a -> data [ 'menu' ][ 'menu_id' ],
'$mlist' => $r ,
'$edit' => t ( 'Edit element' ),
'$drop' => t ( 'Drop element' ),
'$new' => t ( 'New element' ),
'$hintmenu' => t ( 'Edit this menu container' ),
'$hintnew' => t ( 'Add menu element' ),
'$hintdrop' => t ( 'Delete this menu item' ),
2015-04-21 20:45:54 +00:00
'$hintedit' => t ( 'Edit this menu item' ),
2014-11-12 09:43:34 +00:00
));
2015-04-21 20:45:54 +00:00
2013-08-14 03:57:03 +00:00
return $o ;
}
2013-08-12 10:25:37 +00:00
2013-08-14 03:57:03 +00:00
if ( argc () > 2 ) {
2013-12-07 21:05:17 +00:00
2015-04-21 08:05:26 +00:00
if ( intval ( argv ( 2 ))) {
2013-08-14 03:57:03 +00:00
2013-08-12 10:25:37 +00:00
$m = q ( " select * from menu_item where mitem_id = %d and mitem_channel_id = %d limit 1 " ,
2013-08-14 03:57:03 +00:00
intval ( argv ( 2 )),
2014-11-12 09:43:34 +00:00
intval ( $uid )
2013-08-12 10:25:37 +00:00
);
2015-04-21 08:05:26 +00:00
2013-08-12 10:25:37 +00:00
if ( ! $m ) {
notice ( t ( 'Menu item not found.' ) . EOL );
2014-11-12 09:43:34 +00:00
goaway ( z_root () . '/menu' . (( $a -> is_sys ) ? '?f=&sys=1' : '' ));
2013-08-12 10:25:37 +00:00
}
$mitem = $m [ 0 ];
2013-08-14 03:57:03 +00:00
2015-04-21 20:45:54 +00:00
$lockstate = (( $mitem [ 'allow_cid' ] || $mitem [ 'allow_gid' ] || $mitem [ 'deny_cid' ] || $mitem [ 'deny_gid' ]) ? 'lock' : 'unlock' );
2013-08-14 03:57:03 +00:00
if ( argc () == 4 && argv ( 3 ) == 'drop' ) {
2015-09-09 01:40:19 +00:00
menu_sync_packet ( $uid , get_observer_hash (), $mitem [ 'mitem_menu_id' ]);
2014-11-12 09:43:34 +00:00
$r = menu_del_item ( $mitem [ 'mitem_menu_id' ], $uid , intval ( argv ( 2 )));
2015-09-09 00:51:48 +00:00
menu_sync_packet ( $uid , get_observer_hash (), $mitem [ 'mitem_menu_id' ]);
2013-08-12 10:25:37 +00:00
if ( $r )
info ( t ( 'Menu item deleted.' ) . EOL );
else
notice ( t ( 'Menu item could not be deleted.' ) . EOL );
2014-11-12 09:43:34 +00:00
goaway ( z_root () . '/mitem/' . $mitem [ 'mitem_menu_id' ] . (( $a -> is_sys ) ? '?f=&sys=1' : '' ));
2013-08-12 10:25:37 +00:00
}
2015-04-21 08:05:26 +00:00
// edit menu item
$o = replace_macros ( get_markup_template ( 'mitemedit.tpl' ), array (
'$header' => t ( 'Edit Menu Element' ),
'$menu_id' => $a -> data [ 'menu' ][ 'menu_id' ],
'$permissions' => t ( 'Menu Item Permissions' ),
'$permdesc' => t ( " \x28 click to open/close \x29 " ),
'$aclselect' => populate_acl ( $mitem , false ),
'$mitem_id' => intval ( argv ( 2 )),
'$mitem_desc' => array ( 'mitem_desc' , t ( 'Link text' ), $mitem [ 'mitem_desc' ], '' , '*' ),
2015-05-10 19:17:37 +00:00
'$mitem_link' => array ( 'mitem_link' , t ( 'Link or Submenu Target' ), $mitem [ 'mitem_link' ], 'Enter URL of the link or select a menu name to create a submenu' , '*' , 'list="menu-names"' ),
2015-05-29 13:23:51 +00:00
'$usezid' => array ( 'usezid' , t ( 'Use magic-auth if available' ), (( $mitem [ 'mitem_flags' ] & MENU_ITEM_ZID ) ? 1 : 0 ), '' , array ( t ( 'No' ), t ( 'Yes' ))),
'$newwin' => array ( 'newwin' , t ( 'Open link in new window' ), (( $mitem [ 'mitem_flags' ] & MENU_ITEM_NEWWIN ) ? 1 : 0 ), '' , array ( t ( 'No' ), t ( 'Yes' ))),
2015-04-21 08:05:26 +00:00
'$mitem_order' => array ( 'mitem_order' , t ( 'Order in list' ), $mitem [ 'mitem_order' ], t ( 'Higher numbers will sink to bottom of listing' )),
2015-04-21 20:45:54 +00:00
'$submit' => t ( 'Submit' ),
2015-05-10 19:17:37 +00:00
'$lockstate' => $lockstate ,
'$menu_names' => $menu_names
2015-04-21 08:05:26 +00:00
));
return $o ;
2013-08-12 10:25:37 +00:00
}
}
}