Dynamic Main Menu Choice

Discussion in 'Templates customization' started by karyng01, Jun 10, 2012.

  1. karyng01

    karyng01 aMember Pro Customer

    Joined:
    Jul 30, 2008
    Messages:
    71
    Does anyone know a way to dynamically display the main menu choices based on which product a member has signed up for? Meaning if a product has a certain menu choice linked to it then it shows but not otherwise.

    For example (see attached screen shot):

    1. If a member has chosen a class that has a Message Forum (vBulletin) associated with their product purchase, I would like to display the "Message Forum" menu choice in the main menu.

    2. If the member's product does not have a Message Forum linked to their product purchase then I would like to hide the "Message Forum" menu choice.

    Right now the only way I can see to do things is always have the "Message Forum" choice displaying but then you have to create a vBulletin login error screen when the member clicks on the menu choice, which is confusing to the customer and is ugly.

    The better to solution is to hide or show the main menu choice when appropriate.

    Thanks.
    Aly

    Attached Files:

  2. karyng01

    karyng01 aMember Pro Customer

    Joined:
    Jul 30, 2008
    Messages:
    71
    A partial workable (but not perfect) solution is to slightly modify the demo code provided in the API documentation for aMember 4.

    Demo Code from API documentation

    PHP:
    <?php
            
    function helpdeskEnabled(){
                
    $user Am_Di::getInstance()->auth->getUser();
                if(!
    $user) return false;
                foreach(
    $user->getActiveProducts() as $p){
                    
    $data $p->getBillingPlanData();
                    if(
    $data['helpdesk_enabled']) return true;
                }
                return 
    false;
            }
     
     
            
    Am_Di::getInstance()->billingPlanTable->customFields()
                ->
    add(new Am_CustomFieldSelect('helpdesk_enabled'"Enable access to helpdesk"'''', array('options' => array(0=>'No'1=>'Yes'))));
     
            
    Am_Di::getInstance()->hook->add('userMenu''onUserMenu');
            function 
    onUserMenu($event){
                
    $menu $event->getMenu();
                if(!
    helpdeskEnabled()) $menu->removePage($menu->findOneBy('id''helpdesk')); 
            }
     
            
    Am_Di::getInstance()->hook->add(Am_Event::INIT_CONTROLLER_PAGES'onControllerPages');
            function 
    onControllerPages($event){
                
    $c $event->getController();
                if(!
    is_a($c'Helpdesk_IndexController')) return;
                if(!
    helpdeskEnabled()) throw new Am_Exception_InputError('Access denied!');
     
            }
    ?>
    The above code is added to the site.php file and modifies aMember admin to conditionally allow the Helpdesk module menu to appear based on product. So if you say for product A the helpdesk module is enabled then the 'HelpDesk' main menu item appears in the menu. This effect can be modified to work for other tabs you add in my case 'Message Forum'.

    For this site the 'Message Forum' main menu tab is first added in the site.php

    Code:
          // add a tab
          $menu->addPage(array(
                'id' => 'forum',
                  'label' => ('Message Forum'),
                  'uri' => '/forums/',
                  'order' => 500,
          ));
    Then the mods to the HelpDesk code are made
    1. Change the function name helpdeskEnabled=> forumEnabled (for clarity) and references throughout the code.
    2. Remove the part of the code (last 7 lines) that tests the page being loaded and throws an 'Access Denied' error if its the Message Forum page (but you don't have access via any current product).
    3. Item 2 uses a controller class 'Helpdesk_IndexController' for the test of the current page but adding the 'Message Forum' tab does not offer an equivalent controller without creating one.
    4. If you can live without this test then the 'Message Forum' tab will appear only when a user has a subscribed product given access to the 'Message Forum' from within aMember's product admin.
    5. If a subscriber knows about the Message Forum URL and they type it in, without owning a qualified product subscription, they can get to it (without the controller class check), this is the draw back but if their product doesn't have access to the message forum then they can't access that content so its an acceptable situation.
    Modified API Code

    PHP:
            function forumEnabled(){
                
    $user Am_Di::getInstance()->auth->getUser();
                if(!
    $user) return false;
                foreach(
    $user->getActiveProducts() as $p){
                    
    $data $p->getBillingPlanData();
                    if(
    $data['forum_enabled']) return true;
                }
                return 
    false;
            }
     
     
            
    Am_Di::getInstance()->billingPlanTable->customFields()
                ->
    add(new Am_CustomFieldSelect('forum_enabled'"Enable access to Message Forum"'''', array('options' => array(0=>'No'1=>'Yes'))));
     
            
    Am_Di::getInstance()->hook->add('userMenu''onUserMenu');
            function 
    onUserMenu($event){
                
    $menu $event->getMenu();
                if(!
    forumEnabled()) $menu->removePage($menu->findOneBy('id''forum')); 
            }

Share This Page