How do I add an Administration menu to a package?

by Stephan Borg
Friday, July 01, 2005
Posted to Developer's FAQ
Well...
First you need a package/admin/admin_package_inc.php file, which will look something like this:

<?php
$formTinyMCEFeatures 
= array(
    
"tinymce_ask" => array(
        
'label' => 'WYSIWYG confirmation',
        
'note' => 'Ask before using the WYSIWYG editor tinymce when clicking on a textarea. If you disable this feature, we strongly suggest you enable HTML content format as the only content format and also disable quicktags.',
        
'page' => '',
    ),
);
$smarty->assign'formTinyMCEFeatures'$formTinyMCEFeatures );
if( !empty( 
$_REQUEST['change_prefs'] ) ) {
    foreach( 
$formTinyMCEFeatures as $item => $data ) {
        
simple_set_toggle$item );
    }
}
?>

You add more preference settings, by adding items to the $formPackageFeatures array. This essentially provides all the info to automatically generate the page, store and retrieve settings in an automated fashion (thanks to xing). The first part sets up the preferences and their descriptions, the second half is code to save the settings when the form is posted.

Then you need package/templates/menu_package_admin.tpl Smarty template, like this:

<?php
{strip}
<
ul>
    <
li><class="item" href="{$gBitLoc.KERNEL_PKG_URL}admin/index.php?page=tinymce">{tr}TinyMCE Settings{/tr}</a></li>
</
ul>
{/
strip}
?>

This actually creates the section and associated links in the Administration menu. Its fairly straightforward. Note use of
  • and
  • tags, which are fundamental to CSS formatting the menus correctly.

    Then you need a package/templates/admin_package.tpl template, like this:
    
    <?php
    {strip}
    {
    form}
        {
    jstabs}
            {
    jstab title="General Settings"}
                {
    legend legend="General Settings"}
                    <
    div class="row">
                        {foreach 
    from=$formTinyMCEFeatures key=item item=output}
                            <
    div class="row">
                                {
    formlabel label=`$output.label` for=$item}
                                {
    forminput}
                                    {
    html_checkboxes name="$itemvalues="y" checked=`$gBitSystemPrefs.$itemlabels=false id=$item}
                                    {
    formhelp note=`$output.notepage=`$output.page`}
                                {/
    forminput}
                            </
    div>
                        {/foreach}
                    </
    div>

                {/
    legend}
            {/
    jstab}

        {/
    jstabs}

        <
    div class="row submit">
            <
    input type="hidden" name="page" value="{$page}/>
            <
    input type="submit" name="change_prefs" value="{tr}Change preferences{/tr}" />
        </
    div>
    {/
    form}
    {/
    strip}
    ?>

    This is the actualy administration page, generated from /kernel/admin/index.php?page=package. Things to note:
    • The {jstab} tags, create the tabs for different sections of your admin page.
    • The {foreach} stanza automatically generates the preferences check boxes and descriptions, and populates accordingly.
    • In the "row submit" section, the important hidden 'page' parameter is required for setting the values and redirecting after a save. Without it, your settings do not save.
Post Comment
If you are already registered, please enter your login credentials.
Anonymous Post