[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: menu



Can you be more specific?

The general Perl object you probably want to use is LedgerSMB::DBObject::Menu and since this inherits from LedgerSMB::DBObject, the simplest way to do this is to create a user defined function in PostgreSQL.  From there it is very straight-forward to pull that data into LedgerSMB as an array of hashrefs by using either $request->call_procedure if you want to use enumerated arguments, or $menu->exec_method if you want to use mapped arguments.  In essence you can use a user defined function as a named query.

For example, if I wanted to select some information for the current logged in user, I might:

CREATE OR REPLACE FUNCTION custom_get_some_info() 
returns setof menu_item language sql as $$

      SELECT u.position, u.id, u.level, u.path, to_args(array[ua.attribute, ua.value))
        FROM user_menu u
        JOIN user_menu_attribute ua ON u.id = ua.node_id
       WHERE u.login = SESSION_USER;
$$;

Note that you can also RETURN TABLE(....) and this will work as advertised.

Then from Perl you could add to a custom/menu.pl, the right namespace (LedgerSMB::Scripts::menu) and a modified version of the expanding_menu.pl.  Unfortunately for now, the best thing to do would be to just copy/paste the function in there and then modify it.  To call the procedure and get a list of hash refs it is as simple as:

@new_menu_items = $request->call_procedure(procname => 'custom_get_some_info');

or alternatively

@new_menu_items = $menu->exec_method(funcname => 'custom_get_some_info');

If you have to pass additional info to the function it is a little more complex than this, but with more info I can provide more info on how to do that in the framework.  The big difference between call_procedure (which works from either $request or $menu) is that it takes enumerated arguments, while exec_method maps in arguments from current object properties.

Best Wishes,
Chris Travers